Skip to content

Commit

Permalink
Cosmos <> EVM state lens (#3456)
Browse files Browse the repository at this point in the history
  • Loading branch information
aeryz authored Jan 8, 2025
2 parents 5ab22bc + 588e8bf commit 881e6da
Show file tree
Hide file tree
Showing 55 changed files with 2,457 additions and 604 deletions.
103 changes: 103 additions & 0 deletions Cargo.lock

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

10 changes: 8 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,15 @@ members = [
"lib/linea-light-client-types",
"lib/movement-light-client-types",
"lib/state-lens/evm-light-client-types",
"lib/state-lens/cosmos-light-client-types",

# these will all be re enabled and updated once ethereum-light-client is updated

"cosmwasm/ibc-union/light-clients/ethereum",
"cosmwasm/ibc-union/light-clients/arbitrum",
"cosmwasm/ibc-union/light-clients/berachain",
"cosmwasm/ibc-union/light-clients/cometbls",
# "cosmwasm/ibc-union/light-clients/evm-in-cosmos",
"cosmwasm/ibc-union/light-clients/evm-in-cosmos",
# "cosmwasm/ibc-union/light-clients/scroll",
"cosmwasm/ibc-union/light-clients/tendermint",
# "cosmwasm/ibc-union/light-clients/linea",
Expand Down Expand Up @@ -117,12 +118,14 @@ members = [
"voyager/modules/client/movement",
"voyager/modules/client/tendermint",
"voyager/modules/client/state-lens/evm",
"voyager/modules/client/state-lens/ics23-ics23",

"voyager/modules/client-bootstrap/cometbls",
"voyager/modules/client-bootstrap/ethereum",
"voyager/modules/client-bootstrap/movement",
"voyager/modules/client-bootstrap/tendermint",
"voyager/modules/client-bootstrap/state-lens/evm",
"voyager/modules/client-bootstrap/state-lens/ics23-ics23",

"voyager/modules/consensus/berachain",
"voyager/modules/consensus/cometbls",
Expand All @@ -136,6 +139,7 @@ members = [
"voyager/plugins/client-update/movement",
"voyager/plugins/client-update/tendermint",
"voyager/plugins/client-update/state-lens/evm",
"voyager/plugins/client-update/state-lens/ics23-ics23",

"voyager/plugins/periodic-client-update",

Expand Down Expand Up @@ -199,6 +203,7 @@ arbitrum-light-client-types = { path = "lib/arbitrum-light-client-types", defaul
arbitrum-verifier = { path = "lib/arbitrum-verifier", default-features = false }

cometbls-groth16-verifier = { path = "lib/cometbls-groth16-verifier", default-features = false }
cometbls-light-client = { path = "cosmwasm/ibc-union/light-clients/cometbls", default-features = false }
cometbls-light-client-types = { path = "lib/cometbls-light-client-types", default-features = false }

scroll-light-client-types = { path = "lib/scroll-light-client-types", default-features = false }
Expand All @@ -213,7 +218,8 @@ scroll-api = { path = "lib/scroll-api", default-features = fal
scroll-codec = { path = "lib/scroll-codec", default-features = false }
scroll-rpc = { path = "lib/scroll-rpc", default-features = false }

evm-state-lens-light-client-types = { path = "lib/state-lens/evm-light-client-types", default-features = false }
cosmos-state-lens-light-client-types = { path = "lib/state-lens/cosmos-light-client-types", default-features = false }
evm-state-lens-light-client-types = { path = "lib/state-lens/evm-light-client-types", default-features = false }

tendermint-light-client = { path = "cosmwasm/ibc-union/light-clients/tendermint", default-features = false }
tendermint-light-client-types = { path = "lib/tendermint-light-client-types", default-features = false }
Expand Down
6 changes: 3 additions & 3 deletions cosmwasm/ibc-union/core/light-client-interface/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ repository.workspace = true
version = "0.1.0"

[dependencies]
cosmwasm-schema = { workspace = true, package = "cosmwasm-schema" }
cosmwasm-std = { workspace = true, package = "cosmwasm-std" }
cw-storage-plus = { workspace = true, package = "cw-storage-plus" }
cosmwasm-schema = { workspace = true }
cosmwasm-std = { workspace = true }
cw-storage-plus = { workspace = true }
ibc-union-msg = { workspace = true }
macros.workspace = true
schemars = { workspace = true }
Expand Down
47 changes: 46 additions & 1 deletion cosmwasm/ibc-union/core/light-client-interface/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pub mod state;
// These are only used for `key` calculation. We don't want this crate to depend on `ibc-union`.
pub const CLIENT_STATES: Map<u32, Binary> = Map::new("client_states");
pub const CLIENT_CONSENSUS_STATES: Map<(u32, u64), Binary> = Map::new("client_consensus_states");
const CLIENT_IMPLS: Map<u32, Addr> = Map::new("client_impls");
const QUERY_STORE: Item<Binary> = Item::new("query_store");

// TODO: Add #[source] to all variants
Expand Down Expand Up @@ -121,6 +122,47 @@ impl<'a, T: IbcClient> IbcClientCtx<'a, T> {
height,
)
}

pub fn verify_membership<Client: IbcClient>(
&self,
client_id: u32,
height: u64,
path: Bytes,
storage_proof: Client::StorageProof,
value: Bytes,
) -> Result<(), IbcClientError<Client>> {
let client_impl = client_impl(self.deps.querier.into_empty(), &self.ibc_host, client_id)?;
self.deps.querier.query_wasm_smart::<()>(
&client_impl,
&QueryMsg::VerifyMembership {
client_id,
height,
proof: storage_proof.encode_as::<Client::Encoding>().into(),
path,
value,
},
)?;

Ok(())
}
}

fn client_impl<T: IbcClient>(
querier: QuerierWrapper,
ibc_host: &Addr,
client_id: u32,
) -> Result<Addr, IbcClientError<T>> {
let addr = from_json::<Addr>(
querier
.query_wasm_raw(ibc_host.to_string(), CLIENT_IMPLS.key(client_id).to_vec())?
.ok_or_else(|| {
IbcClientError::Std(StdError::generic_err(format!(
"unable to read client state of client {client_id}"
)))
})?,
)?;

Ok(addr)
}

pub trait IbcClient: Sized {
Expand All @@ -136,7 +178,10 @@ pub trait IbcClient: Sized {
/// a common encoding scheme for state lenses. When doing state lenses, client X will read the
/// consensus state of client Y by assuming it's state is ethabi-encoded.
type ConsensusState: Decode<EthAbi, Error: Debug> + Encode<EthAbi> + Debug + 'static;
type StorageProof: Decode<Self::Encoding, Error: Debug> + Debug + 'static;
type StorageProof: Encode<Self::Encoding>
+ Decode<Self::Encoding, Error: Debug>
+ Debug
+ 'static;
type Encoding: Encoding;

fn verify_membership(
Expand Down
2 changes: 1 addition & 1 deletion cosmwasm/ibc-union/light-clients/cometbls/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ crate-type = ["cdylib", "rlib"]

[dependencies]
cometbls-groth16-verifier = { workspace = true }
cometbls-light-client-types = { workspace = true, features = ["serde", "ethabi", "proto"] }
cometbls-light-client-types = { workspace = true, features = ["serde", "ethabi", "bincode"] }
cosmwasm-std = { workspace = true, features = ["abort", "iterator"] }
ibc-union-light-client = { workspace = true }
ibc-union-msg = { workspace = true }
Expand Down
43 changes: 25 additions & 18 deletions cosmwasm/ibc-union/light-clients/cometbls/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use ibc_union_light_client::IbcClientCtx;
use ibc_union_msg::lightclient::Status;
use ics23::ibc_api::SDK_SPECS;
use unionlabs::{
encoding::Proto,
encoding::Bincode,
ibc::core::{
client::height::Height,
commitment::{merkle_proof::MerkleProof, merkle_root::MerkleRoot},
Expand Down Expand Up @@ -38,26 +38,33 @@ impl<T: ZkpVerifier> ibc_union_light_client::IbcClient for CometblsLightClient<T

type StorageProof = MerkleProof;

type Encoding = Proto;
type Encoding = Bincode;

fn verify_membership(
_ctx: ibc_union_light_client::IbcClientCtx<Self>,
_height: u64,
_key: Vec<u8>,
_storage_proof: Self::StorageProof,
_value: Vec<u8>,
ctx: ibc_union_light_client::IbcClientCtx<Self>,
height: u64,
key: Vec<u8>,
storage_proof: Self::StorageProof,
value: Vec<u8>,
) -> Result<(), ibc_union_light_client::IbcClientError<Self>> {
// let consensus_state = ctx.read_self_consensus_state(height)?;
// Ok(ics23::ibc_api::verify_membership(
// &storage_proof,
// &SDK_SPECS,
// &consensus_state.app_hash,
// // FIXME: concat(contract, key) right?
// &[b"wasm".to_vec(), key],
// value,
// )
// .map_err(Into::<Error>::into)?)
Ok(())
let client_state = ctx.read_self_client_state()?;
let consensus_state = ctx.read_self_consensus_state(height)?;
Ok(ics23::ibc_api::verify_membership(
&storage_proof,
&SDK_SPECS,
&consensus_state.app_hash,
&[
b"wasm".to_vec(),
0x3u8
.to_le_bytes()
.into_iter()
.chain(client_state.contract_address)
.chain(key)
.collect::<Vec<_>>(),
],
value,
)
.map_err(Into::<Error>::into)?)
}

fn verify_non_membership(
Expand Down
5 changes: 2 additions & 3 deletions cosmwasm/ibc-union/light-clients/ethereum/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@ use cosmwasm_std::{
from_json, to_json_binary, Binary, CosmosMsg, Deps, DepsMut, Env, MessageInfo, Response,
StdResult, SubMsg,
};
use ethereum_light_client_types::{ClientState, ConsensusState};
use ethereum_light_client_types::ClientState;
use ibc_union_light_client::{
msg::{InstantiateMsg, QueryMsg},
read_consensus_state,
state::IBC_HOST,
IbcClientError, CLIENT_CONSENSUS_STATES, CLIENT_STATES,
IbcClientError, CLIENT_STATES,
};
use ibc_union_msg::module::IbcUnionMsg;
use serde::{Deserialize, Serialize};
use unionlabs::{
encoding::{Bincode, EncodeAs, EthAbi},
Expand Down
21 changes: 13 additions & 8 deletions cosmwasm/ibc-union/light-clients/evm-in-cosmos/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,19 @@ workspace = true
crate-type = ["cdylib", "rlib"]

[dependencies]
cometbls-light-client-types = { workspace = true, features = ["proto"] }
cosmwasm-std = { workspace = true, features = ["abort"] }
# ethereum-light-client = { workspace = true, features = ["mainnet", "library"] }
ics008-wasm-client = { workspace = true }
ics23 = { workspace = true }
protos = { workspace = true }
thiserror = { workspace = true }
unionlabs = { workspace = true, features = ["ethabi", "stargate"] }
cometbls-light-client = { workspace = true, features = ["library"] }
cosmwasm-std = { workspace = true, features = ["abort"] }
ethereum-light-client-types = { workspace = true, features = ["serde", "ethabi", "bincode"] }
evm-state-lens-light-client-types = { workspace = true, features = ["serde", "ethabi", "bincode"] }
evm-storage-verifier = { workspace = true }
ibc-union-light-client = { workspace = true }
ibc-union-msg = { workspace = true }
ibc-union-spec = { workspace = true }
ics23 = { workspace = true }
rlp = { workspace = true }
serde = { workspace = true, features = ["derive"] }
thiserror = { workspace = true }
unionlabs = { workspace = true, features = ["ethabi", "stargate", "bincode"] }

[features]
default = []
Expand Down
Loading

0 comments on commit 881e6da

Please sign in to comment.