github.com/muhammedhassanm/blockchain@v0.0.0-20200120143007-697261defd4d/sawtooth-core-master/validator/src/journal/chain_head_lock.rs (about)

     1  use std::sync::RwLockWriteGuard;
     2  
     3  use batch::Batch;
     4  use journal::block_wrapper::BlockWrapper;
     5  use journal::publisher::{BlockPublisherState, SyncBlockPublisher};
     6  
     7  /// Abstracts acquiring the lock used by the BlockPublisher without exposing access to the
     8  /// publisher itself.
     9  #[derive(Clone)]
    10  pub struct ChainHeadLock {
    11      publisher: SyncBlockPublisher,
    12  }
    13  
    14  impl ChainHeadLock {
    15      pub fn new(publisher: SyncBlockPublisher) -> Self {
    16          ChainHeadLock { publisher }
    17      }
    18  
    19      pub fn acquire(&self) -> ChainHeadGuard {
    20          ChainHeadGuard {
    21              state: self.publisher.state.write().expect("Lock is not poisoned"),
    22              publisher: self.publisher.clone(),
    23          }
    24      }
    25  }
    26  
    27  /// RAII type that represents having acquired the lock used by the BlockPublisher
    28  pub struct ChainHeadGuard<'a> {
    29      state: RwLockWriteGuard<'a, BlockPublisherState>,
    30      publisher: SyncBlockPublisher,
    31  }
    32  
    33  impl<'a> ChainHeadGuard<'a> {
    34      pub fn notify_on_chain_updated(
    35          &mut self,
    36          chain_head: Option<BlockWrapper>,
    37          committed_batches: Vec<Batch>,
    38          uncommitted_batches: Vec<Batch>,
    39      ) {
    40          self.publisher.on_chain_updated(
    41              &mut self.state,
    42              chain_head,
    43              committed_batches,
    44              uncommitted_batches,
    45          )
    46      }
    47  }