Skip to content

Commit a713750

Browse files
authored
apollo_committer_types: extend mock committer client (#12115)
1 parent e171196 commit a713750

File tree

4 files changed

+64
-1
lines changed

4 files changed

+64
-1
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/apollo_committer_types/Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ license.workspace = true
77
description = "Type definitions and interfaces for the Apollo committer component."
88

99
[features]
10-
testing = ["mockall"]
10+
testing = ["mockall", "tokio"]
1111

1212
[dependencies]
1313
apollo_infra.workspace = true
@@ -20,9 +20,11 @@ starknet_committer.workspace = true
2020
strum.workspace = true
2121
strum_macros.workspace = true
2222
thiserror.workspace = true
23+
tokio = { workspace = true, optional = true }
2324

2425
[dev-dependencies]
2526
mockall.workspace = true
27+
tokio.workspace = true
2628

2729
[lints]
2830
workspace = true
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
pub mod committer_types;
22
pub mod communication;
33
pub mod errors;
4+
5+
#[cfg(any(feature = "testing", test))]
6+
pub mod test_utils;
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
use std::sync::Arc;
2+
3+
use async_trait::async_trait;
4+
use starknet_api::block::BlockNumber;
5+
use tokio::sync::Mutex;
6+
7+
use crate::committer_types::{
8+
CommitBlockRequest,
9+
CommitBlockResponse,
10+
RevertBlockRequest,
11+
RevertBlockResponse,
12+
};
13+
use crate::communication::{CommitterClient, MockCommitterClient};
14+
use crate::errors::CommitterClientResult;
15+
16+
/// A wrapper around MockCommitterClient that tracks an offset field.
17+
/// The offset is set to the input height on commit_block and set to the input height on
18+
/// revert_block.
19+
pub struct MockCommitterClientWithOffset {
20+
pub inner: MockCommitterClient,
21+
offset: Arc<Mutex<BlockNumber>>,
22+
}
23+
24+
#[async_trait]
25+
#[cfg(any(feature = "testing", test))]
26+
impl CommitterClient for MockCommitterClientWithOffset {
27+
async fn commit_block(
28+
&self,
29+
input: CommitBlockRequest,
30+
) -> CommitterClientResult<CommitBlockResponse> {
31+
self.set_offset(input.height).await;
32+
self.inner.commit_block(input).await
33+
}
34+
35+
async fn revert_block(
36+
&self,
37+
input: RevertBlockRequest,
38+
) -> CommitterClientResult<RevertBlockResponse> {
39+
self.set_offset(input.height).await;
40+
self.inner.revert_block(input).await
41+
}
42+
}
43+
44+
impl MockCommitterClientWithOffset {
45+
pub fn new(inner: MockCommitterClient, initial_offset: Option<BlockNumber>) -> Self {
46+
let offset = initial_offset.unwrap_or_default();
47+
Self { inner, offset: Arc::new(Mutex::new(offset)) }
48+
}
49+
50+
pub async fn set_offset(&self, offset: BlockNumber) {
51+
*self.offset.lock().await = offset;
52+
}
53+
54+
pub fn get_offset(&self) -> Arc<Mutex<BlockNumber>> {
55+
Arc::clone(&self.offset)
56+
}
57+
}

0 commit comments

Comments
 (0)