66use dashcore:: { BlockHash , ChainLock } ;
77use dashcore:: sml:: masternode_list_engine:: MasternodeListEngine ;
88use indexmap:: IndexMap ;
9- use std:: sync:: { Arc , RwLock } ;
9+ use std:: sync:: Arc ;
10+ use tokio:: sync:: RwLock ;
1011use tracing:: { debug, error, info, warn} ;
1112
1213use crate :: error:: { StorageError , StorageResult , ValidationError , ValidationResult } ;
@@ -57,22 +58,15 @@ impl ChainLockManager {
5758 }
5859
5960 /// Set the masternode engine for validation
60- pub fn set_masternode_engine ( & self , engine : Arc < MasternodeListEngine > ) {
61- match self . masternode_engine . write ( ) {
62- Ok ( mut guard) => {
63- * guard = Some ( engine) ;
64- info ! ( "Masternode engine set for ChainLock validation" ) ;
65- }
66- Err ( e) => {
67- error ! ( "Failed to set masternode engine: {}" , e) ;
68- }
69- }
61+ pub async fn set_masternode_engine ( & self , engine : Arc < MasternodeListEngine > ) {
62+ let mut guard = self . masternode_engine . write ( ) . await ;
63+ * guard = Some ( engine) ;
64+ info ! ( "Masternode engine set for ChainLock validation" ) ;
7065 }
7166
7267 /// Queue a ChainLock for validation when masternode data is available
73- pub fn queue_pending_chainlock ( & self , chain_lock : ChainLock ) -> StorageResult < ( ) > {
74- let mut pending = self . pending_chainlocks . write ( )
75- . map_err ( |_| StorageError :: LockPoisoned ( "pending_chainlocks" . to_string ( ) ) ) ?;
68+ pub async fn queue_pending_chainlock ( & self , chain_lock : ChainLock ) -> StorageResult < ( ) > {
69+ let mut pending = self . pending_chainlocks . write ( ) . await ;
7670
7771 // If at capacity, drop the oldest ChainLock
7872 if pending. len ( ) >= MAX_PENDING_CHAINLOCKS {
@@ -95,8 +89,7 @@ impl ChainLockManager {
9589 storage : & mut dyn StorageManager ,
9690 ) -> ValidationResult < ( ) > {
9791 let pending = {
98- let mut pending_guard = self . pending_chainlocks . write ( )
99- . map_err ( |_| ValidationError :: InvalidChainLock ( "Lock poisoned" . to_string ( ) ) ) ?;
92+ let mut pending_guard = self . pending_chainlocks . write ( ) . await ;
10093 std:: mem:: take ( & mut * pending_guard)
10194 } ;
10295
@@ -138,8 +131,8 @@ impl ChainLockManager {
138131 ) ;
139132
140133 // Check if we already have this chain lock
141- if self . has_chain_lock_at_height ( chain_lock. block_height ) {
142- let existing = self . get_chain_lock_by_height ( chain_lock. block_height ) ;
134+ if self . has_chain_lock_at_height ( chain_lock. block_height ) . await {
135+ let existing = self . get_chain_lock_by_height ( chain_lock. block_height ) . await ;
143136 if let Some ( existing_entry) = existing {
144137 if existing_entry. chain_lock . block_hash != chain_lock. block_hash {
145138 error ! (
@@ -173,8 +166,7 @@ impl ChainLockManager {
173166 }
174167
175168 // Full validation with masternode engine if available
176- let engine_guard = self . masternode_engine . read ( )
177- . map_err ( |_| ValidationError :: InvalidChainLock ( "Lock poisoned" . to_string ( ) ) ) ?;
169+ let engine_guard = self . masternode_engine . read ( ) . await ;
178170
179171 let mut validated = false ;
180172
@@ -195,7 +187,7 @@ impl ChainLockManager {
195187 warn ! ( "⚠️ Masternode engine exists but lacks required masternode lists for height {} (needs list at height {} for ChainLock validation), queueing ChainLock for later validation" ,
196188 chain_lock. block_height, required_height) ;
197189 drop ( engine_guard) ; // Release the read lock before acquiring write lock
198- self . queue_pending_chainlock ( chain_lock. clone ( ) )
190+ self . queue_pending_chainlock ( chain_lock. clone ( ) ) . await
199191 . map_err ( |e| ValidationError :: InvalidChainLock (
200192 format ! ( "Failed to queue pending ChainLock: {}" , e)
201193 ) ) ?;
@@ -210,7 +202,7 @@ impl ChainLockManager {
210202 // Queue for later validation when engine becomes available
211203 warn ! ( "⚠️ Masternode engine not available, queueing ChainLock for later validation" ) ;
212204 drop ( engine_guard) ; // Release the read lock before acquiring write lock
213- self . queue_pending_chainlock ( chain_lock. clone ( ) )
205+ self . queue_pending_chainlock ( chain_lock. clone ( ) ) . await
214206 . map_err ( |e| ValidationError :: InvalidChainLock (
215207 format ! ( "Failed to queue pending ChainLock: {}" , e)
216208 ) ) ?;
@@ -266,10 +258,8 @@ impl ChainLockManager {
266258
267259 // Store in memory caches
268260 {
269- let mut by_height = self . chain_locks_by_height . write ( )
270- . map_err ( |_| StorageError :: LockPoisoned ( "chain_locks_by_height" . to_string ( ) ) ) ?;
271- let mut by_hash = self . chain_locks_by_hash . write ( )
272- . map_err ( |_| StorageError :: LockPoisoned ( "chain_locks_by_hash" . to_string ( ) ) ) ?;
261+ let mut by_height = self . chain_locks_by_height . write ( ) . await ;
262+ let mut by_hash = self . chain_locks_by_hash . write ( ) . await ;
273263
274264 by_height. insert ( chain_lock. block_height , entry. clone ( ) ) ;
275265 by_hash. insert ( chain_lock. block_hash , entry. clone ( ) ) ;
@@ -304,58 +294,51 @@ impl ChainLockManager {
304294 }
305295
306296 /// Check if we have a chain lock at the given height
307- pub fn has_chain_lock_at_height ( & self , height : u32 ) -> bool {
308- self . chain_locks_by_height . read ( )
309- . map ( |locks| locks. contains_key ( & height) )
310- . unwrap_or ( false )
297+ pub async fn has_chain_lock_at_height ( & self , height : u32 ) -> bool {
298+ let locks = self . chain_locks_by_height . read ( ) . await ;
299+ locks. contains_key ( & height)
311300 }
312301
313302 /// Get chain lock by height
314- pub fn get_chain_lock_by_height ( & self , height : u32 ) -> Option < ChainLockEntry > {
315- self . chain_locks_by_height . read ( )
316- . ok ( )
317- . and_then ( |locks| locks. get ( & height) . cloned ( ) )
303+ pub async fn get_chain_lock_by_height ( & self , height : u32 ) -> Option < ChainLockEntry > {
304+ let locks = self . chain_locks_by_height . read ( ) . await ;
305+ locks. get ( & height) . cloned ( )
318306 }
319307
320308 /// Get chain lock by block hash
321- pub fn get_chain_lock_by_hash ( & self , hash : & BlockHash ) -> Option < ChainLockEntry > {
322- self . chain_locks_by_hash . read ( )
323- . ok ( )
324- . and_then ( |locks| locks. get ( hash) . cloned ( ) )
309+ pub async fn get_chain_lock_by_hash ( & self , hash : & BlockHash ) -> Option < ChainLockEntry > {
310+ let locks = self . chain_locks_by_hash . read ( ) . await ;
311+ locks. get ( hash) . cloned ( )
325312 }
326313
327314 /// Check if a block is chain-locked
328- pub fn is_block_chain_locked ( & self , block_hash : & BlockHash , height : u32 ) -> bool {
315+ pub async fn is_block_chain_locked ( & self , block_hash : & BlockHash , height : u32 ) -> bool {
329316 // First check by hash (most specific)
330- if let Some ( entry) = self . get_chain_lock_by_hash ( block_hash) {
317+ if let Some ( entry) = self . get_chain_lock_by_hash ( block_hash) . await {
331318 return entry. validated && entry. chain_lock . block_hash == * block_hash;
332319 }
333320
334321 // Then check by height
335- if let Some ( entry) = self . get_chain_lock_by_height ( height) {
322+ if let Some ( entry) = self . get_chain_lock_by_height ( height) . await {
336323 return entry. validated && entry. chain_lock . block_hash == * block_hash;
337324 }
338325
339326 false
340327 }
341328
342329 /// Get the highest chain-locked block height
343- pub fn get_highest_chain_locked_height ( & self ) -> Option < u32 > {
344- self . chain_locks_by_height . read ( )
345- . ok ( )
346- . and_then ( |locks| locks. keys ( ) . max ( ) . cloned ( ) )
330+ pub async fn get_highest_chain_locked_height ( & self ) -> Option < u32 > {
331+ let locks = self . chain_locks_by_height . read ( ) . await ;
332+ locks. keys ( ) . max ( ) . cloned ( )
347333 }
348334
349335 /// Check if a reorganization would violate chain locks
350- pub fn would_violate_chain_lock ( & self , reorg_from_height : u32 , reorg_to_height : u32 ) -> bool {
336+ pub async fn would_violate_chain_lock ( & self , reorg_from_height : u32 , reorg_to_height : u32 ) -> bool {
351337 if !self . enforce_chain_locks {
352338 return false ;
353339 }
354340
355- let locks = match self . chain_locks_by_height . read ( ) {
356- Ok ( locks) => locks,
357- Err ( _) => return false , // If we can't read locks, assume no violation
358- } ;
341+ let locks = self . chain_locks_by_height . read ( ) . await ;
359342
360343 // Check if any chain-locked block would be reorganized
361344 for height in reorg_from_height..=reorg_to_height {
@@ -395,10 +378,8 @@ impl ChainLockManager {
395378 validated : true ,
396379 } ;
397380
398- let mut by_height = self . chain_locks_by_height . write ( )
399- . map_err ( |_| StorageError :: LockPoisoned ( "chain_locks_by_height" . to_string ( ) ) ) ?;
400- let mut by_hash = self . chain_locks_by_hash . write ( )
401- . map_err ( |_| StorageError :: LockPoisoned ( "chain_locks_by_hash" . to_string ( ) ) ) ?;
381+ let mut by_height = self . chain_locks_by_height . write ( ) . await ;
382+ let mut by_hash = self . chain_locks_by_hash . write ( ) . await ;
402383
403384 by_height. insert ( chain_lock. block_height , entry. clone ( ) ) ;
404385 by_hash. insert ( chain_lock. block_hash , entry) ;
@@ -417,29 +398,9 @@ impl ChainLockManager {
417398
418399
419400 /// Get chain lock statistics
420- pub fn get_stats ( & self ) -> ChainLockStats {
421- let by_height = match self . chain_locks_by_height . read ( ) {
422- Ok ( guard) => guard,
423- Err ( _) => return ChainLockStats {
424- total_chain_locks : 0 ,
425- cached_by_height : 0 ,
426- cached_by_hash : 0 ,
427- highest_locked_height : None ,
428- lowest_locked_height : None ,
429- enforce_chain_locks : self . enforce_chain_locks ,
430- } ,
431- } ;
432- let by_hash = match self . chain_locks_by_hash . read ( ) {
433- Ok ( guard) => guard,
434- Err ( _) => return ChainLockStats {
435- total_chain_locks : 0 ,
436- cached_by_height : 0 ,
437- cached_by_hash : 0 ,
438- highest_locked_height : None ,
439- lowest_locked_height : None ,
440- enforce_chain_locks : self . enforce_chain_locks ,
441- } ,
442- } ;
401+ pub async fn get_stats ( & self ) -> ChainLockStats {
402+ let by_height = self . chain_locks_by_height . read ( ) . await ;
403+ let by_hash = self . chain_locks_by_hash . read ( ) . await ;
443404
444405 ChainLockStats {
445406 total_chain_locks : by_height. len ( ) ,
0 commit comments