Skip to content

Commit 0339a85

Browse files
committed
added locks to advance block and broadcast endpoints
1 parent 2bf4ceb commit 0339a85

2 files changed

Lines changed: 5 additions & 0 deletions

File tree

src/rest/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ pub struct Rest<N: Network, C: ConsensusStorage<N>> {
6262
manual_block_creation: bool,
6363
/// The Private Key used for block creation.
6464
private_key: PrivateKey<N>,
65+
/// Serializes all prepare+advance ledger operations to prevent concurrent block creation races.
66+
block_creation_lock: Arc<Mutex<()>>,
6567
/// Sender half of the shutdown channel; consumed on first use.
6668
shutdown_tx: Arc<Mutex<Option<oneshot::Sender<()>>>>,
6769
/// Path to the ledger storage directory; None when running in-memory.
@@ -87,6 +89,7 @@ impl<N: Network, C: 'static + ConsensusStorage<N>> Rest<N, C> {
8789
num_verifying_executions: Default::default(),
8890
manual_block_creation,
8991
private_key,
92+
block_creation_lock: Default::default(),
9093
shutdown_tx: Default::default(),
9194
storage_path,
9295
};

src/rest/routes.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,7 @@ impl<N: Network, C: ConsensusStorage<N>> Rest<N, C> {
528528
// Prepare and advance in a single blocking task to prevent concurrent broadcasts
529529
// from both preparing a block at the same height and racing to advance the ledger.
530530
tokio::task::spawn_blocking(move || {
531+
let _guard = rest.block_creation_lock.lock();
531532
let new_block = rest
532533
.ledger
533534
.prepare_advance_to_next_beacon_block(
@@ -659,6 +660,7 @@ impl<N: Network, C: ConsensusStorage<N>> Rest<N, C> {
659660
// Iterate and create the specified number of blocks.
660661
// Return the last created block.
661662
let last_block = tokio::task::spawn_blocking(move || -> Result<ErasedJson, RestError> {
663+
let _guard = rest.block_creation_lock.lock();
662664
let mut last_block = None;
663665

664666
// Take all unconfirmed transactions from the buffer.

0 commit comments

Comments
 (0)