Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: ibc module testing suite and implementation #4

Merged
merged 11 commits into from
Sep 22, 2023
Merged
672 changes: 622 additions & 50 deletions Cargo.lock

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,10 @@ secp256k1 = { version = "0.27.0", default-features = false, features = ["global-
# See reth: https://github.com/paradigmxyz/reth/blob/main/Cargo.toml#L79
revm = { git = "https://github.com/bluealloy/revm/", branch = "release/v25" }
revm-primitives = { git = "https://github.com/bluealloy/revm/", branch = "release/v25" }

# Patched to make `app_hash` accessible for the Tendermint Header generation.
# See this issue for more info: https://github.com/informalsystems/tendermint-rs/pull/1344
tendermint = { git = "https://github.com/informalsystems/tendermint-rs.git", branch = "farhad/testgen-app-hash" }
tendermint-light-client-verifier = { git = "https://github.com/informalsystems/tendermint-rs.git", branch = "farhad/testgen-app-hash" }
tendermint-proto = { git = "https://github.com/informalsystems/tendermint-rs.git", branch = "farhad/testgen-app-hash" }
tendermint-testgen = { git = "https://github.com/informalsystems/tendermint-rs.git", branch = "farhad/testgen-app-hash" }
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,9 @@ uint = "0.9"
borsh = { workspace = true, features = ["rc"] }

[dev-dependencies]
sov-modules-api = { path = "../../sov-modules-api" }
tempfile = { workspace = true }

[features]
default = ["native"]
serde = ["dep:serde", "dep:serde_json"]
native = ["serde", "sov-modules-api/native", "dep:schemars"]
native = ["serde", "sov-modules-api/native", "schemars"]
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ where
},
)?;

todo!()
Ok(sov_modules_api::CallResponse::default())
}

/// This function returns true if the token to be sent was created by IBC.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ pub mod call;
pub mod context;
mod genesis;

#[cfg(test)]
mod tests;

#[cfg(feature = "native")]
mod query;

Expand Down Expand Up @@ -42,7 +39,7 @@ pub struct Transfer<C: sov_modules_api::Context> {
/// 1. when tokens are escrowed, save the mapping `denom -> token address`
/// 2. when tokens are unescrowed, lookup the token address by `denom`
#[state]
pub(crate) escrowed_tokens: sov_state::StateMap<String, C::Address>,
pub escrowed_tokens: sov_state::StateMap<String, C::Address>,
}

impl<C: sov_modules_api::Context> sov_modules_api::Module for Transfer<C> {
Expand Down

This file was deleted.

11 changes: 8 additions & 3 deletions module-system/module-implementations/sov-ibc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ resolver = "2"
[dependencies]
anyhow = { workspace = true }
sov-bank = { path = "../sov-bank", default-features = false }
sov-ibc-transfer = { path = "../sov-ibc-transfer", default-features = false }
sov-ibc-transfer = { path = "../sov-ibc-transfer", default-features = false, features = ["native"] }
sov-modules-api = { path = "../../sov-modules-api", default-features = false }
sov-modules-macros = { path = "../../sov-modules-macros" }
sov-state = { path = "../../sov-state", default-features = false }
Expand All @@ -34,10 +34,15 @@ derive_more = { version = "0.99", default-features = false, features = ["from",


[dev-dependencies]
sov-modules-api = { path = "../../sov-modules-api" }
basecoin-app = { git = "https://github.com/informalsystems/basecoin-rs.git", rev = "91eeab8" }
basecoin-store = { git = "https://github.com/informalsystems/basecoin-rs.git", rev = "91eeab8" }
tendermint-testgen = { version = "0.33", default-features = false }
tokio = { workspace = true }
tower-abci = "0.9"
tower = { version = "0.4", features = ["full"] }
tempfile = { workspace = true }

[features]
default = ["native"]
serde = ["dep:serde", "dep:serde_json"]
native = ["serde", "sov-modules-api/native", "dep:schemars"]
native = ["serde", "sov-modules-api/native", "schemars"]
4 changes: 2 additions & 2 deletions module-system/module-implementations/sov-ibc/src/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ impl<C: sov_modules_api::Context> Ibc<C> {
let shared_working_set = Rc::new(RefCell::new(working_set));

let mut execution_context = IbcExecutionContext {
ibc: self,
ibc: self.clone(),
working_set: shared_working_set.clone(),
};

Expand All @@ -66,7 +66,7 @@ impl<C: sov_modules_api::Context> Ibc<C> {
) -> Result<sov_modules_api::CallResponse> {
let shared_working_set = Rc::new(RefCell::new(working_set));
let mut execution_context = IbcExecutionContext {
ibc: self,
ibc: self.clone(),
working_set: shared_working_set.clone(),
};

Expand Down
6 changes: 3 additions & 3 deletions module-system/module-implementations/sov-ibc/src/codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use ibc_proto::protobuf::{Error, Protobuf};
use prost::Message;
use sov_state::codec::{BorshCodec, StateCodec, StateValueCodec};

#[derive(Default)]
#[derive(Default, Clone)]
pub struct ProtobufCodec<Raw> {
borsh_codec: BorshCodec,
_raw: PhantomData<Raw>,
Expand Down Expand Up @@ -43,7 +43,7 @@ impl<Raw> StateCodec for ProtobufCodec<Raw> {
}
}

#[derive(Default)]
#[derive(Default, Clone)]
pub struct PacketCommitmentCodec {
borsh_codec: BorshCodec,
}
Expand Down Expand Up @@ -74,7 +74,7 @@ impl StateCodec for PacketCommitmentCodec {
}
}

#[derive(Default)]
#[derive(Default, Clone)]
pub struct AcknowledgementCommitmentCodec {
borsh_codec: BorshCodec,
}
Expand Down
19 changes: 12 additions & 7 deletions module-system/module-implementations/sov-ibc/src/context.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
pub(crate) mod clients;
pub mod clients;

use std::cell::RefCell;
use std::rc::Rc;
use std::time::Duration;

use ibc::clients::ics07_tendermint::client_state::ClientState as TmClientState;
use ibc::core::events::IbcEvent;
Expand All @@ -25,8 +26,9 @@ use sov_state::WorkingSet;

use crate::Ibc;

#[derive(Clone)]
pub struct IbcExecutionContext<'a, C: sov_modules_api::Context> {
pub ibc: &'a Ibc<C>,
pub ibc: Ibc<C>,
pub working_set: Rc<RefCell<&'a mut WorkingSet<C::Storage>>>,
}

Expand Down Expand Up @@ -123,11 +125,14 @@ where
}

fn host_height(&self) -> Result<Height, ContextError> {
todo!()
// TODO: Dummy value to satisfy tests.
let host_height = Height::new(0, 10)?;
Ok(host_height)
}

fn host_timestamp(&self) -> Result<Timestamp, ContextError> {
todo!()
// TODO: Dummy value to satisfy tests.
Ok(Timestamp::now())
}

fn host_consensus_state(
Expand Down Expand Up @@ -315,12 +320,12 @@ where
)
}

fn max_expected_time_per_block(&self) -> core::time::Duration {
todo!()
fn max_expected_time_per_block(&self) -> Duration {
Duration::from_secs(1)
}

fn validate_message_signer(&self, signer: &ibc::Signer) -> Result<(), ContextError> {
todo!()
Ok(())
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -314,14 +314,28 @@ where
client_id: &ClientId,
height: &ibc::Height,
) -> Result<Option<Self::AnyConsensusState>, ContextError> {
todo!()
let client_cons_state_path = ClientConsensusStatePath::new(client_id, &height.increment());
plafer marked this conversation as resolved.
Show resolved Hide resolved

let next_cons_state = self
.ibc
.consensus_state_map
.get(&client_cons_state_path, *self.working_set.borrow_mut());

Ok(next_cons_state)
}

fn prev_consensus_state(
&self,
client_id: &ClientId,
height: &ibc::Height,
) -> Result<Option<Self::AnyConsensusState>, ContextError> {
todo!()
let client_cons_state_path = ClientConsensusStatePath::new(client_id, &height.decrement()?);
plafer marked this conversation as resolved.
Show resolved Hide resolved

let next_cons_state = self
.ibc
.consensus_state_map
.get(&client_cons_state_path, *self.working_set.borrow_mut());

Ok(next_cons_state)
}
}
8 changes: 7 additions & 1 deletion module-system/module-implementations/sov-ibc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ pub mod call;
pub mod codec;
pub mod genesis;

#[cfg(test)]
pub mod test_utils;

#[cfg(test)]
pub mod tests;

pub(crate) mod context;
mod router;

Expand Down Expand Up @@ -35,7 +41,7 @@ pub struct ExampleModuleConfig {}
/// utilized to form prefixes for `state` fields. This naming adheres to the
/// module naming convention used throughout the codebase, ensuring created
/// prefixes by modules are in harmony.
#[derive(ModuleInfo)]
#[derive(ModuleInfo, Clone)]
pub struct Ibc<C: sov_modules_api::Context> {
#[address]
pub address: C::Address,
Expand Down
Loading