Skip to content

Commit ea9afdf

Browse files
committed
apollo_batcher,apollo_consensus_orchestrator: consensus over partial block hash
1 parent e529641 commit ea9afdf

File tree

11 files changed

+123
-79
lines changed

11 files changed

+123
-79
lines changed

crates/apollo_batcher/src/batcher.rs

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,10 @@ use indexmap::{IndexMap, IndexSet};
6363
#[cfg(test)]
6464
use mockall::automock;
6565
use starknet_api::block::{BlockHash, BlockNumber};
66-
use starknet_api::block_hash::block_hash_calculator::PartialBlockHashComponents;
67-
use starknet_api::block_hash::state_diff_hash::calculate_state_diff_hash;
66+
use starknet_api::block_hash::block_hash_calculator::{
67+
PartialBlockHash,
68+
PartialBlockHashComponents,
69+
};
6870
use starknet_api::consensus_transaction::InternalConsensusTransaction;
6971
use starknet_api::core::{ContractAddress, GlobalRoot, Nonce};
7072
use starknet_api::state::{StateNumber, ThinStateDiff};
@@ -823,7 +825,21 @@ impl Batcher {
823825
);
824826
trace!("Rejected transactions: {:#?}, State diff: {:#?}.", rejected_tx_hashes, state_diff);
825827

826-
let state_diff_commitment = calculate_state_diff_hash(&state_diff);
828+
if let StorageCommitmentBlockHash::Partial(ref components) = &storage_commitment_block_hash
829+
{
830+
self.prev_proposal_commitment = Some((
831+
height,
832+
ProposalCommitment {
833+
partial_block_hash: PartialBlockHash::from_partial_block_hash_components(
834+
components,
835+
)
836+
.map_err(|e| {
837+
error!("Failed to compute partial block hash: {}", e);
838+
BatcherError::InternalError
839+
})?,
840+
},
841+
));
842+
}
827843

828844
// Commit the proposal to the storage.
829845
self.storage_writer
@@ -833,8 +849,6 @@ impl Batcher {
833849
BatcherError::InternalError
834850
})?;
835851
info!("Successfully committed proposal for block {} to storage.", height);
836-
self.prev_proposal_commitment =
837-
Some((height, ProposalCommitment { state_diff_commitment }));
838852

839853
// Notify the L1 provider of the new block.
840854
let rejected_l1_handler_tx_hashes = rejected_tx_hashes
@@ -1131,25 +1145,30 @@ impl Batcher {
11311145
Ok(Some(commitment))
11321146
}
11331147
None => {
1134-
// Parent proposal commitment is not cached. Compute it from the stored state diff.
1135-
let mut state_diff = self
1148+
// Parent proposal commitment is not cached. Read partial block hash
1149+
// components from storage and compute the partial block hash.
1150+
let (_, components) = self
11361151
.storage_reader
1137-
.get_state_diff(prev_height)
1152+
.get_parent_hash_and_partial_block_hash_components(prev_height)
11381153
.map_err(|err| {
11391154
error!(
1140-
"Failed to read state diff for previous height {prev_height}: {}",
1155+
"Failed to read partial block hash components for previous height \
1156+
{prev_height}: {}",
11411157
err
11421158
);
11431159
BatcherError::InternalError
1144-
})?
1145-
.expect("Missing state diff for previous height.");
1146-
1147-
// Enforcing no deprecated classes (since v0.14.0).
1148-
// TODO(dafna): Remove once the state sync bug is fixed.
1149-
state_diff.deprecated_declared_classes = Vec::new();
1160+
})?;
1161+
let components =
1162+
components.expect("Missing partial block hash components for previous height.");
11501163

11511164
Ok(Some(ProposalCommitment {
1152-
state_diff_commitment: calculate_state_diff_hash(&state_diff),
1165+
partial_block_hash: PartialBlockHash::from_partial_block_hash_components(
1166+
&components,
1167+
)
1168+
.map_err(|e| {
1169+
error!("Failed to compute partial block hash: {}", e);
1170+
BatcherError::InternalError
1171+
})?,
11531172
}))
11541173
}
11551174
}

crates/apollo_batcher/src/batcher_test.rs

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,10 @@ use starknet_api::block::{
4444
BlockNumber,
4545
StarknetVersion,
4646
};
47-
use starknet_api::block_hash::block_hash_calculator::PartialBlockHashComponents;
48-
use starknet_api::block_hash::state_diff_hash::calculate_state_diff_hash;
47+
use starknet_api::block_hash::block_hash_calculator::{
48+
PartialBlockHash,
49+
PartialBlockHashComponents,
50+
};
4951
use starknet_api::consensus_transaction::InternalConsensusTransaction;
5052
use starknet_api::core::{ClassHash, CompiledClassHash, GlobalRoot, Nonce};
5153
use starknet_api::state::ThinStateDiff;
@@ -164,7 +166,12 @@ async fn proposal_commitment() -> ProposalCommitment {
164166
}
165167

166168
fn parent_proposal_commitment() -> ProposalCommitment {
167-
ProposalCommitment { state_diff_commitment: calculate_state_diff_hash(&test_state_diff()) }
169+
ProposalCommitment {
170+
partial_block_hash: PartialBlockHash::from_partial_block_hash_components(
171+
&PartialBlockHashComponents::default(),
172+
)
173+
.expect("default partial block hash components are valid"),
174+
}
168175
}
169176

170177
fn validate_block_input(proposal_id: ProposalId) -> ValidateBlockInput {
@@ -1399,6 +1406,14 @@ async fn decision_reached() {
13991406
)
14001407
.returning(|_, _, _| Ok(()));
14011408

1409+
mock_dependencies
1410+
.storage_reader
1411+
.expect_get_parent_hash_and_partial_block_hash_components()
1412+
.with(eq(INITIAL_HEIGHT.prev().unwrap()))
1413+
.returning(|_| {
1414+
Ok((Some(BlockHash::default()), Some(PartialBlockHashComponents::default())))
1415+
});
1416+
14021417
mock_create_builder_for_propose_block(
14031418
&mut mock_dependencies.clients.block_builder_factory,
14041419
vec![],
@@ -1471,6 +1486,14 @@ async fn test_execution_info_order_is_kept() {
14711486
.collect();
14721487
verify_indexed_execution_infos(&execution_infos);
14731488

1489+
mock_dependencies
1490+
.storage_reader
1491+
.expect_get_parent_hash_and_partial_block_hash_components()
1492+
.with(eq(INITIAL_HEIGHT.prev().unwrap()))
1493+
.returning(|_| {
1494+
Ok((Some(BlockHash::default()), Some(PartialBlockHashComponents::default())))
1495+
});
1496+
14741497
mock_create_builder_for_propose_block(
14751498
&mut mock_dependencies.clients.block_builder_factory,
14761499
vec![],
@@ -1550,6 +1573,14 @@ async fn decision_reached_return_success_when_l1_commit_block_fails(
15501573

15511574
mock_dependencies.clients.mempool_client.expect_commit_block().returning(|_| Ok(()));
15521575

1576+
mock_dependencies
1577+
.storage_reader
1578+
.expect_get_parent_hash_and_partial_block_hash_components()
1579+
.with(eq(INITIAL_HEIGHT.prev().unwrap()))
1580+
.returning(|_| {
1581+
Ok((Some(BlockHash::default()), Some(PartialBlockHashComponents::default())))
1582+
});
1583+
15531584
mock_create_builder_for_propose_block(
15541585
&mut mock_dependencies.clients.block_builder_factory,
15551586
vec![],

crates/apollo_batcher/src/block_builder.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ use mockall::automock;
4141
use starknet_api::block::{BlockHashAndNumber, BlockInfo};
4242
use starknet_api::block_hash::block_hash_calculator::{
4343
calculate_block_commitments,
44+
PartialBlockHash,
4445
PartialBlockHashComponents,
4546
TransactionHashingData,
4647
};
@@ -187,10 +188,10 @@ impl BlockExecutionArtifacts {
187188

188189
pub fn commitment(&self) -> ProposalCommitment {
189190
ProposalCommitment {
190-
state_diff_commitment: self
191-
.partial_block_hash_components
192-
.header_commitments
193-
.state_diff_commitment,
191+
partial_block_hash: PartialBlockHash::from_partial_block_hash_components(
192+
&self.partial_block_hash_components,
193+
)
194+
.expect("partial_block_hash_components are valid for current version"),
194195
}
195196
}
196197

crates/apollo_batcher_types/src/batcher_types.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@ use chrono::prelude::*;
88
use indexmap::IndexMap;
99
use serde::{Deserialize, Serialize};
1010
use starknet_api::block::{BlockHashAndNumber, BlockInfo, BlockNumber};
11-
use starknet_api::block_hash::block_hash_calculator::BlockHeaderCommitments;
11+
use starknet_api::block_hash::block_hash_calculator::{BlockHeaderCommitments, PartialBlockHash};
1212
use starknet_api::consensus_transaction::InternalConsensusTransaction;
13-
use starknet_api::core::StateDiffCommitment;
1413
use starknet_api::execution_resources::GasAmount;
1514
use starknet_api::state::ThinStateDiff;
1615
use starknet_api::transaction::TransactionHash;
@@ -36,9 +35,10 @@ pub struct ProposalId(pub u64);
3635

3736
pub type Round = u32;
3837

39-
#[derive(Clone, Debug, Copy, Default, Eq, PartialEq, Serialize, Deserialize)]
38+
/// Commitment identifying a proposed block (its partial block hash).
39+
#[derive(Clone, Copy, Debug, Default, Eq, Hash, PartialEq, Serialize, Deserialize)]
4040
pub struct ProposalCommitment {
41-
pub state_diff_commitment: StateDiffCommitment,
41+
pub partial_block_hash: PartialBlockHash,
4242
}
4343

4444
#[derive(Clone, Debug, Serialize, Deserialize)]

crates/apollo_consensus_orchestrator/src/build_proposal.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ async fn get_proposal_content(
254254
})?;
255255
}
256256
GetProposalContent::Finished { id, final_n_executed_txs } => {
257-
let proposal_commitment = ProposalCommitment(id.state_diff_commitment.0.0);
257+
let proposal_commitment = ProposalCommitment(id.partial_block_hash.0);
258258
content = truncate_to_executed_txs(&mut content, final_n_executed_txs);
259259

260260
info!(

crates/apollo_consensus_orchestrator/src/build_proposal_test.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,12 @@ use apollo_consensus::types::ProposalCommitment as ConsensusProposalCommitment;
1010
use apollo_infra::component_client::ClientError;
1111
use apollo_transaction_converter::{MockTransactionConverterTrait, TransactionConverterError};
1212
use assert_matches::assert_matches;
13+
use starknet_api::block_hash::block_hash_calculator::PartialBlockHash;
1314
use starknet_api::core::ClassHash;
1415
use tokio_util::task::AbortOnDropHandle;
1516

1617
use crate::build_proposal::{build_proposal, BuildProposalError};
17-
use crate::test_utils::{
18-
create_proposal_build_arguments,
19-
INTERNAL_TX_BATCH,
20-
STATE_DIFF_COMMITMENT,
21-
};
18+
use crate::test_utils::{create_proposal_build_arguments, INTERNAL_TX_BATCH, PARTIAL_BLOCK_HASH};
2219

2320
#[tokio::test]
2421
async fn build_proposal_succeed() {
@@ -28,7 +25,7 @@ async fn build_proposal_succeed() {
2825
proposal_args.deps.batcher.expect_get_proposal_content().returning(|_| {
2926
Ok(GetProposalContentResponse {
3027
content: GetProposalContent::Finished {
31-
id: ProposalCommitment { state_diff_commitment: STATE_DIFF_COMMITMENT },
28+
id: ProposalCommitment { partial_block_hash: PartialBlockHash(PARTIAL_BLOCK_HASH) },
3229
final_n_executed_txs: 0,
3330
},
3431
})
@@ -37,7 +34,7 @@ async fn build_proposal_succeed() {
3734
tokio::time::sleep(Duration::from_millis(100)).await;
3835

3936
let res = build_proposal(proposal_args.into()).await.unwrap();
40-
assert_eq!(res, ConsensusProposalCommitment::default());
37+
assert_eq!(res, ConsensusProposalCommitment(PARTIAL_BLOCK_HASH));
4138
}
4239

4340
#[tokio::test]
@@ -112,7 +109,7 @@ async fn cende_fail() {
112109
proposal_args.deps.batcher.expect_get_proposal_content().times(1).returning(|_| {
113110
Ok(GetProposalContentResponse {
114111
content: GetProposalContent::Finished {
115-
id: ProposalCommitment { state_diff_commitment: STATE_DIFF_COMMITMENT },
112+
id: ProposalCommitment { partial_block_hash: PartialBlockHash(PARTIAL_BLOCK_HASH) },
116113
final_n_executed_txs: 0,
117114
},
118115
})

crates/apollo_consensus_orchestrator/src/sequencer_consensus_context.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ impl SequencerConsensusContext {
436436
proposal_commitment: commitment,
437437
parent_proposal_commitment: central_objects
438438
.parent_proposal_commitment
439-
.map(|commitment| ProposalCommitment(commitment.state_diff_commitment.0.0)),
439+
.map(|commitment| ProposalCommitment(commitment.partial_block_hash.0)),
440440
})
441441
.await
442442
{

0 commit comments

Comments
 (0)