Skip to content

Commit 959fbbb

Browse files
committed
feat: add max tolerable height.
1 parent 1f6792d commit 959fbbb

File tree

2 files changed

+42
-13
lines changed

2 files changed

+42
-13
lines changed

protocol-units/settlement/mcr/client/src/lib.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ type CommitmentStream =
1212

1313
#[async_trait::async_trait]
1414
pub trait McrSettlementClientOperations {
15-
15+
1616
async fn post_block_commitment(
1717
&self,
1818
block_commitment: BlockCommitment,
@@ -24,4 +24,6 @@ pub trait McrSettlementClientOperations {
2424

2525
async fn get_commitment_at_height(&self, height : u64) -> Result<Option<BlockCommitment>, anyhow::Error>;
2626

27+
async fn get_max_tolerable_block_height(&self) -> Result<u64, anyhow::Error>;
28+
2729
}

protocol-units/settlement/mcr/client/src/stub.rs

+39-12
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,26 @@ use crate::{CommitmentStream, McrSettlementClientOperations};
22
use movement_types::BlockCommitment;
33
use std::collections::HashMap;
44
use std::sync::Arc;
5-
use tokio::sync::{mpsc, Mutex};
5+
use tokio::sync::{mpsc, RwLock};
66

77
pub struct McrSettlementClient {
8-
commitments: Arc<Mutex<HashMap<u64, BlockCommitment>>>,
8+
commitments: Arc<RwLock<HashMap<u64, BlockCommitment>>>,
99
stream_sender: mpsc::Sender<Result<BlockCommitment, anyhow::Error>>,
1010
// todo: this is logically dangerous, but it's just a stub
11-
stream_receiver: Arc<Mutex<mpsc::Receiver<Result<BlockCommitment, anyhow::Error>>>>,
11+
stream_receiver: Arc<RwLock<mpsc::Receiver<Result<BlockCommitment, anyhow::Error>>>>,
12+
pub current_height: Arc<RwLock<u64>>,
13+
pub block_lead_tolerance: u64
1214
}
1315

1416
impl McrSettlementClient {
1517
pub fn new() -> Self {
1618
let (stream_sender, receiver) = mpsc::channel(10);
1719
McrSettlementClient {
18-
commitments: Arc::new(Mutex::new(HashMap::new())),
20+
commitments: Arc::new(RwLock::new(HashMap::new())),
1921
stream_sender,
20-
stream_receiver: Arc::new(Mutex::new(receiver)),
22+
stream_receiver: Arc::new(RwLock::new(receiver)),
23+
current_height: Arc::new(RwLock::new(0)),
24+
block_lead_tolerance: 16,
2125
}
2226
}
2327
}
@@ -29,9 +33,22 @@ impl McrSettlementClientOperations for McrSettlementClient {
2933
&self,
3034
block_commitment: BlockCommitment,
3135
) -> Result<(), anyhow::Error> {
32-
let mut commitments = self.commitments.lock().await;
33-
commitments.insert(block_commitment.height, block_commitment.clone());
34-
self.stream_sender.send(Ok(block_commitment)).await?; // Simulate sending to the stream.
36+
37+
let height = block_commitment.height;
38+
39+
{
40+
let mut commitments = self.commitments.write().await;
41+
commitments.insert(block_commitment.height, block_commitment.clone());
42+
self.stream_sender.send(Ok(block_commitment)).await?; // Simulate sending to the stream.
43+
}
44+
45+
{
46+
let mut current_height = self.current_height.write().await;
47+
if height > *current_height {
48+
*current_height = height;
49+
}
50+
}
51+
3552
Ok(())
3653
}
3754

@@ -45,7 +62,7 @@ impl McrSettlementClientOperations for McrSettlementClient {
4562
async fn stream_block_commitments(&self) -> Result<CommitmentStream, anyhow::Error> {
4663
let receiver = self.stream_receiver.clone();
4764
let stream = async_stream::try_stream! {
48-
let mut receiver = receiver.lock().await;
65+
let mut receiver = receiver.write().await;
4966
while let Some(commitment) = receiver.recv().await {
5067
yield commitment?;
5168
}
@@ -57,9 +74,14 @@ impl McrSettlementClientOperations for McrSettlementClient {
5774
&self,
5875
height: u64,
5976
) -> Result<Option<BlockCommitment>, anyhow::Error> {
60-
let guard = self.commitments.lock().await;
77+
let guard = self.commitments.write().await;
6178
Ok(guard.get(&height).cloned())
6279
}
80+
81+
async fn get_max_tolerable_block_height(&self) -> Result<u64, anyhow::Error> {
82+
Ok(*self.current_height.read().await + self.block_lead_tolerance)
83+
}
84+
6385
}
6486

6587
#[cfg(test)]
@@ -71,15 +93,20 @@ pub mod test {
7193

7294
#[tokio::test]
7395
async fn test_post_block_commitment() -> Result<(), anyhow::Error> {
96+
7497
let client = McrSettlementClient::new();
7598
let commitment = BlockCommitment {
7699
height: 1,
77100
block_id: Default::default(),
78101
commitment: Commitment::test(),
79102
};
80103
client.post_block_commitment(commitment.clone()).await.unwrap();
81-
let guard = client.commitments.lock().await;
104+
let guard = client.commitments.write().await;
82105
assert_eq!(guard.get(&1), Some(&commitment));
106+
107+
assert_eq!(*client.current_height.read().await, 1);
108+
assert_eq!(client.get_max_tolerable_block_height().await?, 17);
109+
83110
Ok(())
84111
}
85112

@@ -100,7 +127,7 @@ pub mod test {
100127
commitment.clone(),
101128
commitment2.clone(),
102129
]).await.unwrap();
103-
let guard = client.commitments.lock().await;
130+
let guard = client.commitments.write().await;
104131
assert_eq!(guard.get(&1), Some(&commitment));
105132
assert_eq!(guard.get(&2), Some(&commitment2));
106133
Ok(())

0 commit comments

Comments
 (0)