Skip to content

Commit

Permalink
Lock mutex in more client methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
afck committed Oct 3, 2024
1 parent 2d5b67e commit fef2b2f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
12 changes: 6 additions & 6 deletions linera-core/src/client/chain_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ pub struct ChainState {
/// been processed by (i.e. been proposed to) our own local chain manager yet.
pending_blobs: BTreeMap<BlobId, Blob>,

/// A mutex that is held whilst we are preparing the next block, to ensure that no
/// other client can begin preparing a block.
preparing_block: Arc<Mutex<()>>,
/// A mutex that is held whilst we are performing operations that should not be
/// attempted by multiple clients at the same time.
client_mutex: Arc<Mutex<()>>,
}

impl ChainState {
Expand All @@ -72,7 +72,7 @@ impl ChainState {
pending_block: None,
pending_blobs,
received_certificate_trackers: HashMap::new(),
preparing_block: Arc::default(),
client_mutex: Arc::default(),
};
if let Some(block) = pending_block {
state.set_pending_block(block);
Expand Down Expand Up @@ -161,7 +161,7 @@ impl ChainState {
self.pending_blobs.clear();
}

pub fn preparing_block(&self) -> Arc<Mutex<()>> {
self.preparing_block.clone()
pub fn client_mutex(&self) -> Arc<Mutex<()>> {
self.client_mutex.clone()
}
}
12 changes: 9 additions & 3 deletions linera-core/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -871,6 +871,9 @@ where
#[cfg(with_metrics)]
let _latency = metrics::PREPARE_CHAIN_LATENCY.measure_latency();

let mutex = self.state().client_mutex();
let _guard = mutex.lock_owned().await;

// Verify that our local storage contains enough history compared to the
// expected block height. Otherwise, download the missing history from the
// network.
Expand Down Expand Up @@ -1845,8 +1848,8 @@ where
#[cfg(with_metrics)]
let _latency = metrics::EXECUTE_BLOCK_LATENCY.measure_latency();

let block_mutex = self.state().preparing_block();
let _block_guard = block_mutex.lock_owned().await;
let mutex = self.state().client_mutex();
let _guard = mutex.lock_owned().await;
match self.process_pending_block_without_prepare().await? {
ClientOutcome::Committed(Some(certificate)) => {
return Ok(ExecuteBlockOutcome::Conflict(certificate))
Expand Down Expand Up @@ -3001,10 +3004,13 @@ where
/// This is similar to `find_received_certificates` but for only one validator.
/// We also don't try to synchronize the admin chain.
#[tracing::instrument(level = "trace")]
pub async fn find_received_certificates_from_validator(
async fn find_received_certificates_from_validator(
&self,
remote_node: RemoteNode<P::Node>,
) -> Result<(), ChainClientError> {
let mutex = self.state().client_mutex();
let _guard = mutex.lock_owned().await;

let chain_id = self.chain_id;
// Proceed to downloading received certificates.
let (name, tracker, certificates) = self
Expand Down

0 comments on commit fef2b2f

Please sign in to comment.