Skip to content

Commit 7cfadd9

Browse files
committed
Introduce an atomic lock over speculate and finalize
1 parent eda0ab8 commit 7cfadd9

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

synthesizer/src/vm/finalize.rs

+10
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,11 @@ impl<N: Network, C: ConsensusStorage<N>> VM<N, C> {
159159
Vec<(Transaction<N>, String)>,
160160
Vec<FinalizeOperation<N>>,
161161
)> {
162+
// Acquire the atomic lock, which is needed to ensure this function is not called concurrently
163+
// with other `atomic_finalize!` macro calls, which will cause a `bail!` to be triggered erroneously.
164+
// Note: This lock must be held for the entire scope of the call to `atomic_finalize!`.
165+
let _atomic_lock = self.atomic_lock.lock();
166+
162167
let timer = timer!("VM::atomic_speculate");
163168

164169
// Retrieve the number of transactions.
@@ -448,6 +453,11 @@ impl<N: Network, C: ConsensusStorage<N>> VM<N, C> {
448453
solutions: Option<&CoinbaseSolution<N>>,
449454
transactions: &Transactions<N>,
450455
) -> Result<Vec<FinalizeOperation<N>>> {
456+
// Acquire the atomic lock, which is needed to ensure this function is not called concurrently
457+
// with other `atomic_finalize!` macro calls, which will cause a `bail!` to be triggered erroneously.
458+
// Note: This lock must be held for the entire scope of the call to `atomic_finalize!`.
459+
let _atomic_lock = self.atomic_lock.lock();
460+
451461
let timer = timer!("VM::atomic_finalize");
452462

453463
// Perform the finalize operation on the preset finalize mode.

synthesizer/src/vm/mod.rs

+3-7
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ pub struct VM<N: Network, C: ConsensusStorage<N>> {
7272
process: Arc<RwLock<Process<N>>>,
7373
/// The VM store.
7474
store: ConsensusStore<N, C>,
75-
/// The lock for advancing blocks.
76-
block_lock: Arc<Mutex<()>>,
75+
/// The lock to guarantee atomicity over calls to speculate and finalize.
76+
atomic_lock: Arc<Mutex<()>>,
7777
}
7878

7979
impl<N: Network, C: ConsensusStorage<N>> VM<N, C> {
@@ -171,7 +171,7 @@ impl<N: Network, C: ConsensusStorage<N>> VM<N, C> {
171171
}
172172

173173
// Return the new VM.
174-
Ok(Self { process: Arc::new(RwLock::new(process)), store, block_lock: Arc::new(Mutex::new(())) })
174+
Ok(Self { process: Arc::new(RwLock::new(process)), store, atomic_lock: Arc::new(Mutex::new(())) })
175175
}
176176

177177
/// Returns `true` if a program with the given program ID exists.
@@ -312,10 +312,6 @@ impl<N: Network, C: ConsensusStorage<N>> VM<N, C> {
312312
/// Adds the given block into the VM.
313313
#[inline]
314314
pub fn add_next_block(&self, block: &Block<N>) -> Result<()> {
315-
// Acquire the block lock, which is needed to ensure this function is not called concurrently.
316-
// Note: This lock must be held for the entire scope of this function.
317-
let _block_lock = self.block_lock.lock();
318-
319315
// Construct the finalize state.
320316
let state = FinalizeGlobalState::new::<N>(
321317
block.round(),

0 commit comments

Comments
 (0)