Skip to content

Commit 6a9a2d4

Browse files
committed
apollo_batcher: perform commitment tasks
1 parent 4f4475a commit 6a9a2d4

File tree

1 file changed

+64
-5
lines changed

1 file changed

+64
-5
lines changed

crates/apollo_batcher/src/commitment_manager/state_committer.rs

Lines changed: 64 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
#![allow(dead_code, unused_variables, unused_mut)]
22

3-
use apollo_committer_types::communication::SharedCommitterClient;
3+
use apollo_committer_types::committer_types::{CommitBlockRequest, RevertBlockRequest};
4+
use apollo_committer_types::communication::{CommitterRequest, SharedCommitterClient};
5+
use apollo_committer_types::errors::{CommitterClientError, CommitterClientResult};
46
use tokio::sync::mpsc::{Receiver, Sender};
57
use tokio::task::JoinHandle;
68

7-
use crate::commitment_manager::types::{CommitterTaskInput, CommitterTaskOutput};
9+
use crate::commitment_manager::types::{
10+
CommitmentTaskOutput,
11+
CommitterTaskInput,
12+
CommitterTaskOutput,
13+
RevertTaskOutput,
14+
};
815

916
/// Commits state changes by calling the committer.
1017
pub(crate) trait StateCommitterTrait {
@@ -39,13 +46,65 @@ impl StateCommitterTrait for StateCommitter {
3946
}
4047

4148
impl StateCommitter {
49+
/// Repeatedly performs any task in the channel.
4250
pub(crate) async fn perform_commitment_tasks(
4351
mut tasks_receiver: Receiver<CommitterTaskInput>,
4452
mut results_sender: Sender<CommitterTaskOutput>,
4553
committer_client: SharedCommitterClient,
4654
) {
47-
// Placeholder: simply drain the receiver and do nothing.
48-
// TODO(Amos): Implement the actual commitment tasks logic.
49-
while let Some(_task) = tasks_receiver.recv().await {}
55+
while let Some(CommitterTaskInput(request)) = tasks_receiver.recv().await {
56+
let output = perform_commitment_task(request, &committer_client).await;
57+
// TODO(Yoav): wait for task channel by config.
58+
results_sender.send(output).await.unwrap();
59+
}
5060
}
5161
}
62+
63+
/// Performs a commitment task by calling the committer.
64+
/// Retries at recoverable errors.
65+
async fn perform_commitment_task(
66+
request: CommitterRequest,
67+
committer_client: &SharedCommitterClient,
68+
) -> CommitterTaskOutput {
69+
loop {
70+
let result = match &request {
71+
CommitterRequest::CommitBlock(commit_block_request) => {
72+
perform_commit_block_task(commit_block_request.clone(), committer_client).await
73+
}
74+
CommitterRequest::RevertBlock(revert_block_request) => {
75+
perform_revert_block_task(revert_block_request.clone(), committer_client).await
76+
}
77+
};
78+
match result {
79+
Ok(output) => return output,
80+
Err(err) => {
81+
handle_task_error(err).await;
82+
continue;
83+
}
84+
}
85+
}
86+
}
87+
88+
async fn perform_commit_block_task(
89+
commit_block_request: CommitBlockRequest,
90+
committer_client: &SharedCommitterClient,
91+
) -> CommitterClientResult<CommitterTaskOutput> {
92+
let height = commit_block_request.height;
93+
let response = committer_client.commit_block(commit_block_request).await?;
94+
Ok(CommitterTaskOutput::Commit(CommitmentTaskOutput { response, height }))
95+
}
96+
97+
async fn perform_revert_block_task(
98+
revert_block_request: RevertBlockRequest,
99+
committer_client: &SharedCommitterClient,
100+
) -> CommitterClientResult<CommitterTaskOutput> {
101+
let height = revert_block_request.height;
102+
let response = committer_client.revert_block(revert_block_request).await?;
103+
Ok(CommitterTaskOutput::Revert(RevertTaskOutput { response, height }))
104+
}
105+
106+
/// Panics on unrecoverable errors.
107+
async fn handle_task_error(error: CommitterClientError) {
108+
// TODO(Amos): Handle errors.
109+
unimplemented!();
110+
}

0 commit comments

Comments
 (0)