Skip to content

Commit

Permalink
update rs files
Browse files Browse the repository at this point in the history
  • Loading branch information
apenzk committed Feb 12, 2025
1 parent 8de7899 commit 87adb1d
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 46 deletions.
2 changes: 1 addition & 1 deletion protocol-units/settlement/mcr/client/abis/MCR.json

Large diffs are not rendered by default.

34 changes: 17 additions & 17 deletions protocol-units/settlement/mcr/client/src/eth_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,11 +188,11 @@ where
{
async fn post_block_commitment(
&self,
block_commitment: BlockCommitment,
block_commitment: SuperBlockCommitment,
) -> Result<(), anyhow::Error> {
let contract = MCR::new(self.contract_address, &self.rpc_provider);

let eth_block_commitment = MCR::BlockCommitment {
let eth_block_commitment = MCR::SuperBlockCommitment {
// Currently, to simplify the API, we'll say 0 is uncommitted all other numbers are legitimate heights
height: U256::from(block_commitment.height()),
commitment: alloy_primitives::FixedBytes(
Expand All @@ -211,7 +211,7 @@ where
)
.await
} else {
let call_builder = contract.submitBlockCommitment(eth_block_commitment);
let call_builder = contract.submitSuperBlockCommitment(eth_block_commitment);
crate::send_eth_transaction::send_transaction(
call_builder,
&self.send_transaction_error_rules,
Expand All @@ -224,14 +224,14 @@ where

async fn post_block_commitment_batch(
&self,
block_commitments: Vec<BlockCommitment>,
block_commitments: Vec<SuperBlockCommitment>,
) -> Result<(), anyhow::Error> {
let contract = MCR::new(self.contract_address, &self.rpc_provider);

let eth_block_commitment: Vec<_> = block_commitments
.into_iter()
.map(|block_commitment| {
Ok(MCR::BlockCommitment {
Ok(MCR::SuperBlockCommitment {
// Currently, to simplify the API, we'll say 0 is uncommitted all other numbers are legitimate heights
height: U256::from(block_commitment.height()),
commitment: alloy_primitives::FixedBytes(
Expand All @@ -244,7 +244,7 @@ where
})
.collect::<Result<Vec<_>, TryFromSliceError>>()?;

let call_builder = contract.submitBatchBlockCommitment(eth_block_commitment);
let call_builder = contract.submitBatchSuperBlockCommitment(eth_block_commitment);

crate::send_eth_transaction::send_transaction(
call_builder,
Expand All @@ -257,11 +257,11 @@ where

async fn force_block_commitment(
&self,
block_commitment: BlockCommitment,
block_commitment: SuperBlockCommitment,
) -> Result<(), anyhow::Error> {
let contract = MCR::new(self.contract_address, &self.rpc_provider);

let eth_block_commitment = MCR::BlockCommitment {
let eth_block_commitment = MCR::SuperBlockCommitment {
// Currently, to simplify the API, we'll say 0 is uncommitted all other numbers are legitimate heights
height: U256::from(block_commitment.height()),
commitment: alloy_primitives::FixedBytes(
Expand Down Expand Up @@ -294,7 +294,7 @@ where
alloy_sol_types::Error::Other(err.to_string().into())
},
)?;
Ok(BlockCommitment::new(
Ok(SuperBlockCommitment::new(
height,
Id::new(commitment.blockHash.0),
Commitment::new(commitment.stateCommitment.0),
Expand All @@ -308,17 +308,17 @@ where
async fn get_commitment_at_height(
&self,
height: u64,
) -> Result<Option<BlockCommitment>, anyhow::Error> {
) -> Result<Option<SuperBlockCommitment>, anyhow::Error> {
let contract = MCR::new(self.contract_address, &self.ws_provider);
let MCR::getAcceptedCommitmentAtBlockHeightReturn { _0: commitment } =
contract.getAcceptedCommitmentAtBlockHeight(U256::from(height)).call().await?;
let MCR::getAcceptedCommitmentAtSuperBlockHeightReturn { _0: commitment } =
contract.getAcceptedCommitmentAtSuperBlockHeight(U256::from(height)).call().await?;

let return_height: u64 = commitment
.height
.try_into()
.context("Failed to convert the commitment height from U256 to u64")?;
// Commitment with height 0 mean not found
Ok((return_height != 0).then_some(BlockCommitment::new(
Ok((return_height != 0).then_some(SuperBlockCommitment::new(
commitment
.height
.try_into()
Expand All @@ -331,10 +331,10 @@ where
async fn get_posted_commitment_at_height(
&self,
height: u64,
) -> Result<Option<BlockCommitment>, anyhow::Error> {
) -> Result<Option<SuperBlockCommitment>, anyhow::Error> {
let contract = MCR::new(self.contract_address, &self.ws_provider);
let MCR::getValidatorCommitmentAtBlockHeightReturn { _0: commitment } = contract
.getValidatorCommitmentAtBlockHeight(U256::from(height), self.signer_address)
let MCR::getValidatorCommitmentAtSuperBlockHeightReturn { _0: commitment } = contract
.getValidatorCommitmentAtSuperBlockHeight(U256::from(height), self.signer_address)
.call()
.await?;

Expand All @@ -343,7 +343,7 @@ where
.try_into()
.context("Failed to convert the commitment height from U256 to u64")?;

Ok((return_height != 0).then_some(BlockCommitment::new(
Ok((return_height != 0).then_some(SuperBlockCommitment::new(
commitment
.height
.try_into()
Expand Down
14 changes: 7 additions & 7 deletions protocol-units/settlement/mcr/client/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use movement_types::block::BlockCommitment;
use movement_types::block::SuperBlockCommitment;
use tokio_stream::Stream;
pub mod mock;

Expand All @@ -14,27 +14,27 @@ pub use eth_client::McrSettlementClient;
pub mod send_eth_transaction;

type CommitmentStream =
std::pin::Pin<Box<dyn Stream<Item = Result<BlockCommitment, anyhow::Error>> + Send>>;
std::pin::Pin<Box<dyn Stream<Item = Result<SuperBlockCommitment, anyhow::Error>> + Send>>;

#[async_trait::async_trait]
pub trait McrSettlementClientOperations {
/// Posts a block commitment to the settlement client.
async fn post_block_commitment(
&self,
block_commitment: BlockCommitment,
block_commitment: SuperBlockCommitment,
) -> Result<(), anyhow::Error>;

/// Posts a batch of block commitments to the settlement client.
async fn post_block_commitment_batch(
&self,
block_commitment: Vec<BlockCommitment>,
block_commitment: Vec<SuperBlockCommitment>,
) -> Result<(), anyhow::Error>;

/// Forces a block commitment
/// This will only work in admin mode
async fn force_block_commitment(
&self,
block_commitment: BlockCommitment,
block_commitment: SuperBlockCommitment,
) -> Result<(), anyhow::Error>;

/// Streams block commitments from the settlement client.
Expand All @@ -44,13 +44,13 @@ pub trait McrSettlementClientOperations {
async fn get_commitment_at_height(
&self,
height: u64,
) -> Result<Option<BlockCommitment>, anyhow::Error>;
) -> Result<Option<SuperBlockCommitment>, anyhow::Error>;

/// Gets the commitment this validator has made at a given height
async fn get_posted_commitment_at_height(
&self,
height: u64,
) -> Result<Option<BlockCommitment>, anyhow::Error>;
) -> Result<Option<SuperBlockCommitment>, anyhow::Error>;

/// Gets the max tolerable block height.
async fn get_max_tolerable_block_height(&self) -> Result<u64, anyhow::Error>;
Expand Down
42 changes: 21 additions & 21 deletions protocol-units/settlement/mcr/client/src/mock.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{CommitmentStream, McrSettlementClientOperations};
use mcr_settlement_config::Config;
use movement_types::block::BlockCommitment;
use movement_types::block::SuperBlockCommitment;
use std::collections::BTreeMap;
use std::sync::{Arc, Mutex};
use tokio::sync::{mpsc, RwLock};
Expand All @@ -9,9 +9,9 @@ use tracing::info;

#[derive(Clone)]
pub struct McrSettlementClient {
commitments: Arc<RwLock<BTreeMap<u64, BlockCommitment>>>,
stream_sender: mpsc::Sender<Result<BlockCommitment, anyhow::Error>>,
stream_receiver: Arc<Mutex<Option<mpsc::Receiver<Result<BlockCommitment, anyhow::Error>>>>>,
commitments: Arc<RwLock<BTreeMap<u64, SuperBlockCommitment>>>,
stream_sender: mpsc::Sender<Result<SuperBlockCommitment, anyhow::Error>>,
stream_receiver: Arc<Mutex<Option<mpsc::Receiver<Result<SuperBlockCommitment, anyhow::Error>>>>>,
pub current_height: Arc<RwLock<u64>>,
pub block_lead_tolerance: u64,
paused_at_height: Arc<RwLock<Option<u64>>>,
Expand Down Expand Up @@ -39,7 +39,7 @@ impl McrSettlementClient {
///
/// To have effect, this method needs to be called before a commitment is
/// posted for this height with the `McrSettlementClientOperations` API.
pub async fn override_block_commitment(&self, commitment: BlockCommitment) {
pub async fn override_block_commitment(&self, commitment: SuperBlockCommitment) {
let mut commitments = self.commitments.write().await;
commitments.insert(commitment.height(), commitment);
}
Expand Down Expand Up @@ -74,7 +74,7 @@ impl McrSettlementClient {
impl McrSettlementClientOperations for McrSettlementClient {
async fn post_block_commitment(
&self,
block_commitment: BlockCommitment,
block_commitment: SuperBlockCommitment,
) -> Result<(), anyhow::Error> {
let height = block_commitment.height();

Expand Down Expand Up @@ -104,25 +104,25 @@ impl McrSettlementClientOperations for McrSettlementClient {

async fn post_block_commitment_batch(
&self,
block_commitment: Vec<BlockCommitment>,
block_commitments: Vec<SuperBlockCommitment>,
) -> Result<(), anyhow::Error> {
for commitment in block_commitment {
for commitment in block_commitments {
self.post_block_commitment(commitment).await?;
}
Ok(())
}

async fn force_block_commitment(
&self,
_block_commitment: BlockCommitment,
block_commitment: SuperBlockCommitment,
) -> Result<(), anyhow::Error> {
unimplemented!()
}

async fn get_posted_commitment_at_height(
&self,
height: u64,
) -> Result<Option<BlockCommitment>, anyhow::Error> {
) -> Result<Option<SuperBlockCommitment>, anyhow::Error> {
unimplemented!();
}

Expand All @@ -139,7 +139,7 @@ impl McrSettlementClientOperations for McrSettlementClient {
async fn get_commitment_at_height(
&self,
height: u64,
) -> Result<Option<BlockCommitment>, anyhow::Error> {
) -> Result<Option<SuperBlockCommitment>, anyhow::Error> {
let guard = self.commitments.read().await;
Ok(guard.get(&height).cloned())
}
Expand All @@ -162,7 +162,7 @@ pub mod test {
#[tokio::test]
async fn test_post_block_commitment() -> Result<(), anyhow::Error> {
let client = McrSettlementClient::new();
let commitment = BlockCommitment::new(1, Default::default(), Commitment::test());
let commitment = SuperBlockCommitment::new(1, Default::default(), Commitment::test());
client.post_block_commitment(commitment.clone()).await.unwrap();
let guard = client.commitments.write().await;
assert_eq!(guard.get(&1), Some(&commitment));
Expand All @@ -176,8 +176,8 @@ pub mod test {
#[tokio::test]
async fn test_post_block_commitment_batch() -> Result<(), anyhow::Error> {
let client = McrSettlementClient::new();
let commitment = BlockCommitment::new(1, Default::default(), Commitment::test());
let commitment2 = BlockCommitment::new(1, Default::default(), Commitment::test());
let commitment = SuperBlockCommitment::new(1, Default::default(), Commitment::test());
let commitment2 = SuperBlockCommitment::new(1, Default::default(), Commitment::test());
client
.post_block_commitment_batch(vec![commitment.clone(), commitment2.clone()])
.await
Expand All @@ -191,7 +191,7 @@ pub mod test {
#[tokio::test]
async fn test_stream_block_commitments() -> Result<(), anyhow::Error> {
let client = McrSettlementClient::new();
let commitment = BlockCommitment::new(1, Default::default(), Commitment::test());
let commitment = SuperBlockCommitment::new(1, Default::default(), Commitment::test());
client.post_block_commitment(commitment.clone()).await.unwrap();
let mut stream = client.stream_block_commitments().await?;
assert_eq!(stream.next().await.unwrap().unwrap(), commitment);
Expand All @@ -201,10 +201,10 @@ pub mod test {
#[tokio::test]
async fn test_override_block_commitments() -> Result<(), anyhow::Error> {
let client = McrSettlementClient::new();
let commitment = BlockCommitment::new(2, Default::default(), Commitment::test());
let commitment = SuperBlockCommitment::new(2, Default::default(), Commitment::test());
client.override_block_commitment(commitment.clone()).await;
client
.post_block_commitment(BlockCommitment::new(2, Default::default(), Commitment::test()))
.post_block_commitment(SuperBlockCommitment::new(2, Default::default(), Commitment::test()))
.await
.unwrap();
let mut stream = client.stream_block_commitments().await?;
Expand All @@ -215,10 +215,10 @@ pub mod test {
#[tokio::test]
async fn test_pause() -> Result<(), anyhow::Error> {
let client = McrSettlementClient::new();
let commitment = BlockCommitment::new(2, Default::default(), Commitment::test());
let commitment = SuperBlockCommitment::new(2, Default::default(), Commitment::test());
client.pause_after(1).await;
client.post_block_commitment(commitment.clone()).await?;
let commitment2 = BlockCommitment::new(2, Default::default(), Commitment::test());
let commitment2 = SuperBlockCommitment::new(2, Default::default(), Commitment::test());
client.post_block_commitment(commitment2).await?;
let mut stream = client.stream_block_commitments().await?;
assert_eq!(stream.next().await.expect("stream has ended")?, commitment);
Expand All @@ -233,10 +233,10 @@ pub mod test {
#[tokio::test]
async fn test_resume() -> Result<(), anyhow::Error> {
let client = McrSettlementClient::new();
let commitment = BlockCommitment::new(2, Default::default(), Commitment::test());
let commitment = SuperBlockCommitment::new(2, Default::default(), Commitment::test());
client.pause_after(1).await;
client.post_block_commitment(commitment.clone()).await?;
let commitment2 = BlockCommitment::new(2, Default::default(), Commitment::test());
let commitment2 = SuperBlockCommitment::new(2, Default::default(), Commitment::test());
client.post_block_commitment(commitment2.clone()).await?;
let mut stream = client.stream_block_commitments().await?;
assert_eq!(stream.next().await.expect("stream has ended")?, commitment);
Expand Down

0 comments on commit 87adb1d

Please sign in to comment.