Skip to content

Commit dbda80c

Browse files
authored
Merge pull request #957 from openmina/feat/block-proof-sharing
feat(node): Wrap ledger and block proofs in `Arc` and share them
2 parents f224013 + 4e1fc72 commit dbda80c

File tree

20 files changed

+53
-39
lines changed

20 files changed

+53
-39
lines changed

ledger/src/proofs/block.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1855,7 +1855,7 @@ pub(super) fn generate_block_proof(
18551855
} = params;
18561856

18571857
let (txn_snark_statement, txn_snark_proof) =
1858-
ledger_proof_opt(ledger_proof.as_ref(), next_state)?;
1858+
ledger_proof_opt(ledger_proof.as_deref(), next_state)?;
18591859
let prev_state_proof = &chain.proof;
18601860

18611861
let (new_state_hash, previous_proof_statements) = block_main(

mina-p2p-messages/src/v2/generated.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use derive_more::Deref;
33
use openmina_macros::SerdeYojsonEnum;
44
use rsexp_derive::{OfSexp, SexpOf};
55
use serde::{Deserialize, Serialize};
6+
use std::sync::Arc;
67

78
use crate::{array::ArrayN16, list::List, pseq::PaddedSeq};
89

@@ -243,7 +244,7 @@ pub struct TrustSystemPeerStatusStableV1 {
243244
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, BinProtRead, BinProtWrite)]
244245
pub struct BlockchainSnarkBlockchainStableV2 {
245246
pub state: MinaStateProtocolStateValueStableV2,
246-
pub proof: MinaBaseProofStableV2,
247+
pub proof: Arc<MinaBaseProofStableV2>,
247248
}
248249

249250
/// **OCaml name**: `Transaction_witness.Stable.V2`
@@ -270,7 +271,7 @@ pub struct ProverExtendBlockchainInputStableV2 {
270271
pub chain: BlockchainSnarkBlockchainStableV2,
271272
pub next_state: MinaStateProtocolStateValueStableV2,
272273
pub block: MinaStateSnarkTransitionValueStableV2,
273-
pub ledger_proof: Option<LedgerProofProdStableV2>,
274+
pub ledger_proof: Option<Arc<LedgerProofProdStableV2>>,
274275
pub prover_state: ConsensusStakeProofStableV2,
275276
pub pending_coinbase: MinaBasePendingCoinbaseWitnessStableV2,
276277
}
@@ -3199,7 +3200,7 @@ pub struct TransactionSnarkScanStateLedgerProofWithSokMessageStableV2(
31993200
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, BinProtRead, BinProtWrite)]
32003201
pub struct MinaBlockHeaderStableV2 {
32013202
pub protocol_state: MinaStateProtocolStateValueStableV2,
3202-
pub protocol_state_proof: MinaBaseProofStableV2,
3203+
pub protocol_state_proof: Arc<MinaBaseProofStableV2>,
32033204
pub delta_block_chain_proof: (StateHash, List<StateBodyHash>),
32043205
pub current_protocol_version: ProtocolVersionStableV2,
32053206
pub proposed_protocol_version_opt: Option<ProtocolVersionStableV2>,

node/common/src/service/block_producer/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
mod vrf_evaluator;
22

3+
use std::sync::Arc;
4+
35
use ledger::proofs::{
46
block::BlockParams, generate_block_proof, provers::BlockProver, transaction::ProofError,
57
};
@@ -67,7 +69,7 @@ pub fn prove(
6769
mut input: Box<ProverExtendBlockchainInputStableV2>,
6870
keypair: AccountSecretKey,
6971
only_verify_constraints: bool,
70-
) -> Result<Box<MinaBaseProofStableV2>, ProofError> {
72+
) -> Result<Arc<MinaBaseProofStableV2>, ProofError> {
7173
let height = input
7274
.next_state
7375
.body

node/src/block_producer/block_producer_actions.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,13 @@ pub enum BlockProducerAction {
5353
StagedLedgerDiffCreateInit,
5454
StagedLedgerDiffCreatePending,
5555
StagedLedgerDiffCreateSuccess {
56-
output: Box<StagedLedgerDiffCreateOutput>,
56+
output: Arc<StagedLedgerDiffCreateOutput>,
5757
},
5858
BlockUnprovenBuild,
5959
BlockProveInit,
6060
BlockProvePending,
6161
BlockProveSuccess {
62-
proof: Box<MinaBaseProofStableV2>,
62+
proof: Arc<MinaBaseProofStableV2>,
6363
},
6464
BlockProduced,
6565
#[action_event(level = trace)]

node/src/block_producer/block_producer_event.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::sync::Arc;
2+
13
use mina_p2p_messages::v2::{MinaBaseProofStableV2, StateHash};
24
use serde::{Deserialize, Serialize};
35

@@ -6,7 +8,7 @@ pub use super::vrf_evaluator::BlockProducerVrfEvaluatorEvent;
68
#[derive(derive_more::From, Serialize, Deserialize, Debug, Clone)]
79
pub enum BlockProducerEvent {
810
VrfEvaluator(BlockProducerVrfEvaluatorEvent),
9-
BlockProve(StateHash, Result<Box<MinaBaseProofStableV2>, String>),
11+
BlockProve(StateHash, Result<Arc<MinaBaseProofStableV2>, String>),
1012
}
1113

1214
impl std::fmt::Display for BlockProducerEvent {

node/src/block_producer/block_producer_reducer.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ impl BlockProducerEnabled {
299299
time: meta.time(),
300300
won_slot,
301301
chain,
302-
block: block.with_hash_and_proof(block_hash, *proof),
302+
block: block.with_hash_and_proof(block_hash, proof),
303303
};
304304
} else {
305305
bug_condition!("Invalid state for `BlockProducerAction::BlockProduced` expected: `BlockProducerCurrentState::BlockProveSuccess`, found: {:?}", current_state);
@@ -767,7 +767,7 @@ fn broadcast_injected_block(global_state: &State, dispatcher: &mut Dispatcher<Ac
767767
return;
768768
};
769769

770-
let message = Box::new(GossipNetMessageV2::NewState(block));
770+
let message = GossipNetMessageV2::NewState(block);
771771
dispatcher.push(P2pNetworkPubsubAction::Broadcast { message });
772772
}
773773

node/src/block_producer/block_producer_state.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{collections::BTreeSet, time::Duration};
1+
use std::{collections::BTreeSet, sync::Arc, time::Duration};
22

33
use ledger::scan_state::transaction_logic::valid;
44
use mina_p2p_messages::v2;
@@ -81,7 +81,7 @@ pub enum BlockProducerCurrentState {
8181
/// `protocol_state.blockchain_state.body_reference`
8282
diff_hash: v2::ConsensusBodyReferenceStableV1,
8383
staged_ledger_hash: v2::MinaBaseStagedLedgerHashStableV1,
84-
emitted_ledger_proof: Option<Box<v2::LedgerProofProdStableV2>>,
84+
emitted_ledger_proof: Option<Arc<v2::LedgerProofProdStableV2>>,
8585
pending_coinbase_update: v2::MinaBasePendingCoinbaseUpdateStableV1,
8686
pending_coinbase_witness: v2::MinaBasePendingCoinbaseWitnessStableV2,
8787
stake_proof_sparse_ledger: v2::MinaBaseSparseLedgerBaseStableV2,
@@ -91,7 +91,7 @@ pub enum BlockProducerCurrentState {
9191
won_slot: BlockProducerWonSlot,
9292
/// Chain that we are extending.
9393
chain: Vec<AppliedBlock>,
94-
emitted_ledger_proof: Option<Box<v2::LedgerProofProdStableV2>>,
94+
emitted_ledger_proof: Option<Arc<v2::LedgerProofProdStableV2>>,
9595
pending_coinbase_update: v2::MinaBasePendingCoinbaseUpdateStableV1,
9696
pending_coinbase_witness: v2::MinaBasePendingCoinbaseWitnessStableV2,
9797
stake_proof_sparse_ledger: v2::MinaBaseSparseLedgerBaseStableV2,
@@ -103,7 +103,7 @@ pub enum BlockProducerCurrentState {
103103
won_slot: BlockProducerWonSlot,
104104
/// Chain that we are extending.
105105
chain: Vec<AppliedBlock>,
106-
emitted_ledger_proof: Option<Box<v2::LedgerProofProdStableV2>>,
106+
emitted_ledger_proof: Option<Arc<v2::LedgerProofProdStableV2>>,
107107
pending_coinbase_update: v2::MinaBasePendingCoinbaseUpdateStableV1,
108108
pending_coinbase_witness: v2::MinaBasePendingCoinbaseWitnessStableV2,
109109
stake_proof_sparse_ledger: v2::MinaBaseSparseLedgerBaseStableV2,
@@ -117,7 +117,7 @@ pub enum BlockProducerCurrentState {
117117
chain: Vec<AppliedBlock>,
118118
block: BlockWithoutProof,
119119
block_hash: v2::StateHash,
120-
proof: Box<v2::MinaBaseProofStableV2>,
120+
proof: Arc<v2::MinaBaseProofStableV2>,
121121
},
122122
Produced {
123123
time: redux::Timestamp,

node/src/block_producer/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
pub mod vrf_evaluator;
22

33
mod block_producer_config;
4+
use std::sync::Arc;
5+
46
pub use block_producer_config::*;
57

68
mod block_producer_state;
@@ -188,7 +190,7 @@ impl BlockWithoutProof {
188190
pub fn with_hash_and_proof(
189191
self,
190192
hash: v2::StateHash,
191-
proof: v2::MinaBaseProofStableV2,
193+
proof: Arc<v2::MinaBaseProofStableV2>,
192194
) -> ArcBlockWithHash {
193195
let block = v2::MinaBlockBlockStableV2 {
194196
header: v2::MinaBlockHeaderStableV2 {

node/src/block_producer_effectful/block_producer_effectful_effects.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ pub fn block_producer_effects<S: crate::Service>(
164164
.clone(),
165165
pending_coinbase_update: pending_coinbase_update.clone(),
166166
},
167-
ledger_proof: emitted_ledger_proof.as_ref().map(|proof| (**proof).clone()),
167+
ledger_proof: emitted_ledger_proof.clone(),
168168
prover_state: ConsensusStakeProofStableV2 {
169169
delegator: won_slot.delegator.1.into(),
170170
delegator_pk: won_slot.delegator.0.clone(),

node/src/block_producer_effectful/block_producer_effectful_service.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::sync::Arc;
2+
13
use ledger::proofs::provers::BlockProver;
24
use mina_p2p_messages::v2::{
35
ConsensusBodyReferenceStableV1, LedgerProofProdStableV2, MinaBasePendingCoinbaseUpdateStableV1,
@@ -13,7 +15,7 @@ pub struct StagedLedgerDiffCreateOutput {
1315
/// `protocol_state.blockchain_state.body_reference`
1416
pub diff_hash: ConsensusBodyReferenceStableV1,
1517
pub staged_ledger_hash: MinaBaseStagedLedgerHashStableV1,
16-
pub emitted_ledger_proof: Option<Box<LedgerProofProdStableV2>>,
18+
pub emitted_ledger_proof: Option<Arc<LedgerProofProdStableV2>>,
1719
pub pending_coinbase_update: MinaBasePendingCoinbaseUpdateStableV1,
1820
pub pending_coinbase_witness: MinaBasePendingCoinbaseWitnessStableV2,
1921
pub stake_proof_sparse_ledger: MinaBaseSparseLedgerBaseStableV2,

0 commit comments

Comments
 (0)