Skip to content

Commit b262867

Browse files
committed
apollo_batcher: commitment manager output uses apollo committer types
1 parent 6d87e2f commit b262867

File tree

6 files changed

+52
-19
lines changed

6 files changed

+52
-19
lines changed

crates/apollo_batcher/src/commitment_manager/commitment_manager_impl.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use std::sync::Arc;
44

55
use apollo_batcher_config::config::BatcherConfig;
6+
use apollo_committer_types::committer_types::CommitBlockResponse;
67
use apollo_committer_types::communication::SharedCommitterClient;
78
use starknet_api::block::BlockNumber;
89
use starknet_api::block_hash::block_hash_calculator::{
@@ -21,6 +22,7 @@ use crate::commitment_manager::state_committer::{StateCommitter, StateCommitterT
2122
use crate::commitment_manager::types::{
2223
CommitmentTaskInput,
2324
CommitmentTaskOutput,
25+
CommitterTaskOutput,
2426
FinalBlockCommitment,
2527
};
2628

@@ -53,7 +55,7 @@ impl Default for CommitmentManagerConfig {
5355
/// Encapsulates the block hash calculation logic.
5456
pub(crate) struct CommitmentManager<S: StateCommitterTrait> {
5557
pub(crate) tasks_sender: Sender<CommitmentTaskInput>,
56-
pub(crate) results_receiver: Receiver<CommitmentTaskOutput>,
58+
pub(crate) results_receiver: Receiver<CommitterTaskOutput>,
5759
pub(crate) config: CommitmentManagerConfig,
5860
pub(crate) commitment_task_offset: BlockNumber,
5961
pub(crate) state_committer: S,
@@ -142,12 +144,13 @@ impl<S: StateCommitterTrait> CommitmentManager<S> {
142144
}
143145
}
144146

145-
/// Fetches all ready commitment results from the state committer.
147+
/// Fetches all ready commitment results from the state committer. Panics if any task is a
148+
/// revert.
146149
pub(crate) async fn get_commitment_results(&mut self) -> Vec<CommitmentTaskOutput> {
147150
let mut results = Vec::new();
148151
loop {
149152
match self.results_receiver.try_recv() {
150-
Ok(result) => results.push(result),
153+
Ok(result) => results.push(result.expect_commitment()),
151154
Err(TryRecvError::Empty) => break,
152155
Err(err) => {
153156
panic!("Failed to receive commitment result from state committer. error: {err}")
@@ -262,7 +265,7 @@ impl<S: StateCommitterTrait> CommitmentManager<S> {
262265
// TODO(Rotem): Test this function.
263266
pub(crate) fn final_commitment_output<R: BatcherStorageReader + ?Sized>(
264267
storage_reader: Arc<R>,
265-
CommitmentTaskOutput { height, global_root }: CommitmentTaskOutput,
268+
CommitmentTaskOutput { response: CommitBlockResponse { state_root: global_root }, height }: CommitmentTaskOutput,
266269
should_finalize_block_hash: bool,
267270
) -> CommitmentManagerResult<FinalBlockCommitment> {
268271
match should_finalize_block_hash {

crates/apollo_batcher/src/commitment_manager/commitment_manager_test.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ async fn test_create_commitment_manager_with_missing_tasks(
149149
assert_eq!(get_number_of_tasks_in_sender(&commitment_manager.tasks_sender), 1,);
150150
commitment_manager.state_committer.pop_task_and_insert_result().await;
151151
let results = await_results(&mut commitment_manager.results_receiver, 1).await;
152-
let result = results.first().unwrap();
152+
let result = (results.first().unwrap()).clone().expect_commitment();
153153
assert_eq!(result.height, global_root_height);
154154
}
155155

@@ -285,8 +285,8 @@ async fn test_get_commitment_results(mut mock_dependencies: MockDependencies) {
285285
commitment_manager.state_committer.pop_task_and_insert_result().await;
286286

287287
let results = await_results(&mut commitment_manager.results_receiver, 2).await;
288-
let first_result = results.first().unwrap();
289-
let second_result = results.get(1).unwrap();
288+
let first_result = results.first().unwrap().clone().expect_commitment();
289+
let second_result = results.get(1).unwrap().clone().expect_commitment();
290290
assert_eq!(first_result.height, INITIAL_HEIGHT,);
291291
assert_eq!(second_result.height, INITIAL_HEIGHT.next().unwrap(),);
292292
}

crates/apollo_batcher/src/commitment_manager/state_committer.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ use apollo_committer_types::communication::SharedCommitterClient;
44
use tokio::sync::mpsc::{Receiver, Sender};
55
use tokio::task::JoinHandle;
66

7-
use crate::commitment_manager::types::{CommitmentTaskInput, CommitmentTaskOutput};
7+
use crate::commitment_manager::types::{CommitmentTaskInput, CommitterTaskOutput};
88

99
/// Commits state changes by calling the committer.
1010
pub(crate) trait StateCommitterTrait {
1111
/// Creates a new instance and starts thread which performs commitment tasks.
1212
fn create(
1313
tasks_receiver: Receiver<CommitmentTaskInput>,
14-
results_sender: Sender<CommitmentTaskOutput>,
14+
results_sender: Sender<CommitterTaskOutput>,
1515
committer_client: SharedCommitterClient,
1616
) -> Self;
1717
/// Returns a handle to the thread performing commitment tasks.
@@ -25,7 +25,7 @@ pub(crate) struct StateCommitter {
2525
impl StateCommitterTrait for StateCommitter {
2626
fn create(
2727
tasks_receiver: Receiver<CommitmentTaskInput>,
28-
results_sender: Sender<CommitmentTaskOutput>,
28+
results_sender: Sender<CommitterTaskOutput>,
2929
committer_client: SharedCommitterClient,
3030
) -> Self {
3131
let handle = tokio::spawn(async move {
@@ -41,7 +41,7 @@ impl StateCommitterTrait for StateCommitter {
4141
impl StateCommitter {
4242
pub(crate) async fn perform_commitment_tasks(
4343
mut tasks_receiver: Receiver<CommitmentTaskInput>,
44-
mut results_sender: Sender<CommitmentTaskOutput>,
44+
mut results_sender: Sender<CommitterTaskOutput>,
4545
committer_client: SharedCommitterClient,
4646
) {
4747
// Placeholder: simply drain the receiver and do nothing.

crates/apollo_batcher/src/commitment_manager/types.rs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
#![allow(dead_code)]
2+
3+
use apollo_committer_types::committer_types::{CommitBlockResponse, RevertBlockResponse};
24
use starknet_api::block::{BlockHash, BlockNumber};
35
use starknet_api::core::{GlobalRoot, StateDiffCommitment};
46
use starknet_api::state::ThinStateDiff;
@@ -11,13 +13,33 @@ pub(crate) struct CommitmentTaskInput {
1113
pub(crate) state_diff_commitment: Option<StateDiffCommitment>,
1214
}
1315

14-
/// Output of commitment tasks.
15-
#[cfg_attr(test, derive(Default))]
16+
#[derive(Clone)]
1617
pub(crate) struct CommitmentTaskOutput {
17-
pub(crate) global_root: GlobalRoot,
18+
pub(crate) response: CommitBlockResponse,
19+
pub(crate) height: BlockNumber,
20+
}
21+
22+
#[derive(Clone)]
23+
pub(crate) struct RevertTaskOutput {
24+
pub(crate) response: RevertBlockResponse,
1825
pub(crate) height: BlockNumber,
1926
}
2027

28+
#[derive(Clone)]
29+
pub(crate) enum CommitterTaskOutput {
30+
Commit(CommitmentTaskOutput),
31+
Revert(RevertTaskOutput),
32+
}
33+
34+
impl CommitterTaskOutput {
35+
pub(crate) fn expect_commitment(self) -> CommitmentTaskOutput {
36+
match self {
37+
Self::Commit(commitment_task_output) => commitment_task_output,
38+
Self::Revert(_) => panic!("Got revert output."),
39+
}
40+
}
41+
}
42+
2143
pub(crate) struct FinalBlockCommitment {
2244
pub(crate) height: BlockNumber,
2345
// Field is optional because for old blocks there are no component hashes, so the block hash

crates/apollo_batcher/src/test_utils.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use std::sync::Arc;
55
use apollo_batcher_config::config::{BatcherConfig, FirstBlockWithPartialBlockHash};
66
use apollo_batcher_types::batcher_types::{ProposalId, ProposeBlockInput};
77
use apollo_class_manager_types::{EmptyClassManagerClient, SharedClassManagerClient};
8+
use apollo_committer_types::committer_types::CommitBlockResponse;
89
use apollo_committer_types::communication::{MockCommitterClient, SharedCommitterClient};
910
use apollo_l1_provider_types::MockL1ProviderClient;
1011
use apollo_mempool_types::communication::MockMempoolClient;
@@ -41,7 +42,11 @@ use crate::block_builder::{
4142
MockBlockBuilderFactoryTrait,
4243
};
4344
use crate::commitment_manager::state_committer::StateCommitterTrait;
44-
use crate::commitment_manager::types::{CommitmentTaskInput, CommitmentTaskOutput};
45+
use crate::commitment_manager::types::{
46+
CommitmentTaskInput,
47+
CommitmentTaskOutput,
48+
CommitterTaskOutput,
49+
};
4550
use crate::pre_confirmed_block_writer::{
4651
MockPreconfirmedBlockWriterFactoryTrait,
4752
MockPreconfirmedBlockWriterTrait,
@@ -316,7 +321,7 @@ pub(crate) struct MockStateCommitter {
316321
impl StateCommitterTrait for MockStateCommitter {
317322
fn create(
318323
tasks_receiver: Receiver<CommitmentTaskInput>,
319-
results_sender: Sender<CommitmentTaskOutput>,
324+
results_sender: Sender<CommitterTaskOutput>,
320325
_committer_client: SharedCommitterClient,
321326
) -> Self {
322327
let (mock_task_sender, mock_task_receiver) = channel(10);
@@ -335,13 +340,15 @@ impl MockStateCommitter {
335340
/// from the task receiver and sends a result to the results sender.
336341
pub(crate) async fn wait_for_mock_tasks(
337342
mut tasks_receiver: Receiver<CommitmentTaskInput>,
338-
results_sender: Sender<CommitmentTaskOutput>,
343+
results_sender: Sender<CommitterTaskOutput>,
339344
mut mock_task_receiver: Receiver<()>,
340345
) {
341346
while mock_task_receiver.recv().await.is_some() {
342347
let task = tasks_receiver.try_recv().unwrap();
343-
let result =
344-
CommitmentTaskOutput { global_root: GlobalRoot::default(), height: task.height };
348+
let result = CommitterTaskOutput::Commit(CommitmentTaskOutput {
349+
response: CommitBlockResponse { state_root: GlobalRoot::default() },
350+
height: task.height,
351+
});
345352
results_sender.try_send(result).unwrap();
346353
}
347354
}

crates/apollo_committer_types/src/committer_types.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ pub struct CommitBlockRequest {
1313

1414
#[derive(Clone, Debug, Default, Eq, PartialEq, Serialize, Deserialize)]
1515
pub struct CommitBlockResponse {
16+
// TODO(Yoav): Rename to global_root.
1617
pub state_root: GlobalRoot,
1718
}
1819

0 commit comments

Comments
 (0)