Skip to content
This repository has been archived by the owner on Jun 24, 2024. It is now read-only.

Commit

Permalink
imp: refactor mock rollup for committing states
Browse files Browse the repository at this point in the history
  • Loading branch information
Farhad-Shabani committed Dec 7, 2023
1 parent 45aaaf7 commit 16ac05f
Show file tree
Hide file tree
Showing 24 changed files with 1,039 additions and 802 deletions.
5 changes: 5 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions mocks/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ publish = false

[dependencies]
# external dependencies
anyhow = { workspace = true }
borsh = { workspace = true }
prost = { workspace = true }
sha2 = { version = "0.10.6", default-features = false }
serde = { workspace = true }
serde_json = { workspace = true }
tokio = { version = "1", features = ["full"] }
tower-abci = "0.11"
Expand Down Expand Up @@ -43,6 +47,8 @@ sov-modules-api = { workspace = true }
sov-mock-da = { workspace = true }
sov-state = { workspace = true }
sov-rollup-interface = { workspace = true }
jmt = { git = "https://github.com/penumbra-zone/jmt.git", rev = "1d007e11cb68aa5ca13e9a5af4a12e6439d5f7b6" }


[features]
default = ["native"]
Expand Down
4 changes: 3 additions & 1 deletion mocks/src/cosmos/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ use super::helpers::{
convert_tm_to_ics_merkle_proof, dummy_tm_client_state, genesis_app_state, MutexUtil,
};

const JAN_1_2023: i64 = 1672531200;

/// Defines a mock Cosmos chain that includes simplified store, application,
/// consensus layers.
#[derive(Clone)]
Expand Down Expand Up @@ -102,7 +104,7 @@ impl<S: ProvableStore + Default + Debug> MockCosmosChain<S> {

let genesis_height = Height::new(chain_id.revision_number(), 1).expect("never fails");

let genesis_time = Time::now();
let genesis_time = Time::from_unix_timestamp(JAN_1_2023, 0).expect("never fails");

let genesis_block = Self::generate_block(
&chain_id,
Expand Down
76 changes: 0 additions & 76 deletions mocks/src/cosmos/handle.rs

This file was deleted.

1 change: 0 additions & 1 deletion mocks/src/cosmos/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
pub mod app;
pub mod builder;
pub mod handle;
pub mod helpers;
1 change: 0 additions & 1 deletion mocks/src/mod.rs

This file was deleted.

17 changes: 6 additions & 11 deletions mocks/src/relayer/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,18 @@ use ibc_core::primitives::Signer;

use super::handle::{Handle, QueryService};

/// Holds the necessary fields for querying a mock Cosmos
/// chain endpoint.
/// Defines the chain context by which the relayer interacts with the chain.
#[derive(Clone)]
pub struct ChainContext<E: Handle> {
/// Chain handle
querier: Arc<E>,
service: Arc<E>,
/// relayer address on the chain for sending messages
signer: Signer,
}

impl<E: Handle> ChainContext<E> {
pub fn new(querier: Arc<E>, signer: Signer) -> Self {
Self { querier, signer }
}

pub fn querier(&self) -> &Arc<E> {
&self.querier
pub fn new(service: Arc<E>, signer: Signer) -> Self {
Self { service, signer }
}

pub fn signer(&self) -> &Signer {
Expand All @@ -34,7 +29,7 @@ where
{
type E = E;

fn service(&self) -> &Arc<Self::E> {
&self.querier
fn service(&self) -> &Self::E {
&self.service
}
}
92 changes: 0 additions & 92 deletions mocks/src/relayer/handle.rs

This file was deleted.

67 changes: 67 additions & 0 deletions mocks/src/relayer/handle/cosmos.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
use std::fmt::Debug;

use basecoin_store::context::ProvableStore;
use ibc_client_tendermint::types::Header;
use ibc_core::handler::types::events::IbcEvent;
use ibc_core::host::ValidationContext;
use ibc_core::primitives::proto::Any;
use ibc_core_host_cosmos::IBC_QUERY_PATH;

use crate::cosmos::app::MockCosmosChain;
use crate::relayer::handle::{Handle, QueryReq, QueryResp};

impl<S: ProvableStore + Debug + Default> Handle for MockCosmosChain<S> {
fn query(&self, request: QueryReq) -> QueryResp {
match request {
QueryReq::ChainId => QueryResp::ChainId(self.chain_id().clone()),
QueryReq::HostHeight => QueryResp::HostHeight(self.ibc_ctx().host_height().unwrap()),
QueryReq::HostConsensusState(height) => QueryResp::HostConsensusState(
self.ibc_ctx().host_consensus_state(&height).unwrap().into(),
),
QueryReq::ClientCounter => {
QueryResp::ClientCounter(self.ibc_ctx().client_counter().unwrap())
}
QueryReq::ClientState(client_id) => {
QueryResp::ClientState(self.ibc_ctx().client_state(&client_id).unwrap().into())
}
QueryReq::NextSeqSend(path) => {
QueryResp::NextSeqSend(self.ibc_ctx().get_next_sequence_send(&path).unwrap())
}
QueryReq::Header(target_height, trusted_height) => {
let blocks = self.get_blocks();

let revision_height = target_height.revision_height() as usize;

if revision_height > blocks.len() {
panic!("block index out of bounds");
}

let target_block = blocks[revision_height - 1].clone();

let header = Header {
signed_header: target_block.signed_header,
validator_set: target_block.validators,
trusted_height,
trusted_next_validator_set: target_block.next_validators,
};

QueryResp::Header(header.into())
}
QueryReq::ValueWithProof(path, height) => {
let (value, proof) = self.query(
path.to_string().as_bytes().to_vec(),
IBC_QUERY_PATH.to_string(),
&height,
);

QueryResp::ValueWithProof(value, proof.into())
}
}
}

fn send_msg(&self, msgs: Vec<Any>) -> Vec<IbcEvent> {
msgs.into_iter()
.flat_map(|msg| self.app.ibc().process_message(msg).unwrap())
.collect()
}
}
Loading

0 comments on commit 16ac05f

Please sign in to comment.