Skip to content

Commit 1d3d155

Browse files
authored
Merge pull request #789 from openmina/feat/block-verify-works
feat: Enable stronger verification for blocks close to the best tip
2 parents e695a4c + fddc06d commit 1d3d155

File tree

6 files changed

+37
-6
lines changed

6 files changed

+37
-6
lines changed

node/src/ledger/ledger_manager.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::collections::BTreeMap;
22

3-
use ledger::staged_ledger::staged_ledger::StagedLedger;
3+
use ledger::staged_ledger::staged_ledger::{SkipVerification, StagedLedger};
44
use mina_p2p_messages::v2::{self, LedgerHash, MinaBaseAccountBinableArgStableV2};
55
use openmina_core::channels::mpsc;
66
use openmina_core::thread;
@@ -154,9 +154,18 @@ impl LedgerRequest {
154154
result: result.map(Into::into),
155155
}
156156
}
157-
LedgerWriteRequest::BlockApply { block, pred_block } => {
157+
LedgerWriteRequest::BlockApply {
158+
block,
159+
pred_block,
160+
skip_verification,
161+
} => {
158162
let block_hash = block.hash().clone();
159-
let result = ledger_ctx.block_apply(block, pred_block);
163+
let skip_verification = if skip_verification {
164+
Some(SkipVerification::All)
165+
} else {
166+
None
167+
};
168+
let result = ledger_ctx.block_apply(block, pred_block, skip_verification);
160169
LedgerWriteResponse::BlockApply { block_hash, result }
161170
}
162171
LedgerWriteRequest::Commit {

node/src/ledger/ledger_service.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -638,6 +638,7 @@ impl LedgerCtx {
638638
&mut self,
639639
block: ArcBlockWithHash,
640640
pred_block: AppliedBlock,
641+
skip_verification: Option<SkipVerification>,
641642
) -> Result<BlockApplyResult, String> {
642643
openmina_core::info!(openmina_core::log::system_time();
643644
kind = "LedgerService::block_apply",
@@ -674,8 +675,7 @@ impl LedgerCtx {
674675

675676
let result = staged_ledger
676677
.apply(
677-
// TODO(binier): SEC
678-
Some(SkipVerification::All),
678+
skip_verification,
679679
constraint_constants(),
680680
Slot::from_u32(global_slot),
681681
diff,

node/src/ledger/write/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ pub enum LedgerWriteRequest {
5050
BlockApply {
5151
block: ArcBlockWithHash,
5252
pred_block: AppliedBlock,
53+
skip_verification: bool,
5354
},
5455
Commit {
5556
ledgers_to_keep: BTreeSet<v2::LedgerHash>,

node/src/transition_frontier/sync/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,8 @@ pub enum SyncError {
2525
#[error("sync failed due to block({}, {}) application error: {1}", .0.height(), .0.hash())]
2626
BlockApplyFailed(ArcBlockWithHash, String),
2727
}
28+
29+
/// How close to the best tip we have to be for the full
30+
/// verification of proofs contained in the block
31+
/// body (zkApp txns and completed works) to be enabled.
32+
const CATCHUP_BLOCK_VERIFY_TAIL_LENGTH: usize = 5;

node/src/transition_frontier/sync/transition_frontier_sync_effects.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,17 +243,27 @@ impl TransitionFrontierSyncAction {
243243
};
244244
let hash = block.hash.clone();
245245

246+
// During catchup, we skip the verificationf of completed work and zkApp txn proofs
247+
// until get closer to the best tip, at which point full verification is enabled.
248+
let skip_verification = super::CATCHUP_BLOCK_VERIFY_TAIL_LENGTH
249+
< store.state().transition_frontier.sync.pending_count();
250+
246251
if let Some(stats) = store.service.stats() {
247252
stats.block_producer().block_apply_start(meta.time(), &hash);
248253
}
249254

250255
store.dispatch(LedgerWriteAction::Init {
251-
request: LedgerWriteRequest::BlockApply { block, pred_block },
256+
request: LedgerWriteRequest::BlockApply {
257+
block,
258+
pred_block,
259+
skip_verification,
260+
},
252261
on_init: redux::callback!(
253262
on_block_next_apply_init(request: LedgerWriteRequest) -> crate::Action {
254263
let LedgerWriteRequest::BlockApply {
255264
block,
256265
pred_block: _,
266+
skip_verification: _,
257267
} = request
258268
else {
259269
unreachable!()

node/src/transition_frontier/sync/transition_frontier_sync_state.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,12 @@ impl TransitionFrontierSyncState {
277277
}
278278
}
279279

280+
pub fn pending_count(&self) -> usize {
281+
self.blocks_iter()
282+
.filter(|b| !matches!(b, TransitionFrontierSyncBlockState::ApplySuccess { .. }))
283+
.count()
284+
}
285+
280286
pub fn blocks_fetch_retry_iter(&self) -> impl '_ + Iterator<Item = StateHash> {
281287
self.blocks_iter().filter_map(|s| s.retry_hash()).cloned()
282288
}

0 commit comments

Comments
 (0)