Skip to content

Commit

Permalink
State lens codec fixes and docs (#3599)
Browse files Browse the repository at this point in the history
  • Loading branch information
benluelo authored Jan 21, 2025
2 parents ad5e955 + 3cdc499 commit 161d1ea
Show file tree
Hide file tree
Showing 11 changed files with 509 additions and 80 deletions.
4 changes: 3 additions & 1 deletion 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 docs/src/content/docs/protocol/deployments.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ Deployments of `ibc-union` on CosmWasm (cosmos) chains. CosmWasm contract source
| `state-lens-ics23-mpt-client` | [`bbn1fventeva948ue0fzhp6xselr522rnqwger9wg7r0g9f4jemsqh6sv9ms6p`](https://testnet.babylon.explorers.guru/contract/bbn1fventeva948ue0fzhp6xselr522rnqwger9wg7r0g9f4jemsqh6sv9ms6p)
| `ucs03` | [`bbn1a3zy68ndge5wuzxcd9cf6vz0kv0pes4j3eu64ml8pflufc5tsrxsqgd8qf`](https://testnet.babylon.explorers.guru/contract/bbn1a3zy68ndge5wuzxcd9cf6vz0kv0pes4j3eu64ml8pflufc5tsrxsqgd8qf)

### Osmosis Testnet (`osmo-test-5`)
| Name | Address |
| --- | --- |
| `ibc-union` | [`osmo1sxe4cymy9kf6m5kcu4l25zf9x304am9jsr7k0r4uw3dqmn4h9fcs3464gg`](https://celatone.osmosis.zone/osmo-test-5/contracts/osmo1sxe4cymy9kf6m5kcu4l25zf9x304am9jsr7k0r4uw3dqmn4h9fcs3464gg) |
| `cometbls-light-client` | [`osmo13y8ke43r46cnnc86gy20sg5ucdpxux9jc3nazyk447jehrz4rtqqk8xckr`](https://celatone.osmosis.zone/osmo-test-5/contracts/osmo13y8ke43r46cnnc86gy20sg5ucdpxux9jc3nazyk447jehrz4rtqqk8xckr) |


### Channels

Expand Down
54 changes: 54 additions & 0 deletions lib/state-lens-ics23-ics23-light-client-types/src/client_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,57 @@ pub struct Extra {
/// ibc contract that is running on l2
pub contract_address: H256,
}

#[cfg(feature = "ethabi")]
mod ethabi {
use alloy::{
dyn_abi::SolType,
sol_types::{private::SolTypeValue, SolValue},
};

use super::*;

impl SolType for Extra {
type RustType = Self;

type Token<'a> = <<(H256,) as SolValue>::SolType as SolType>::Token<'a>;

const SOL_NAME: &'static str = "Extra";

const ENCODED_SIZE: Option<usize> = None;

const PACKED_ENCODED_SIZE: Option<usize> = None;

fn valid_token(_token: &Self::Token<'_>) -> bool {
true
}

fn detokenize((contract_address,): Self::Token<'_>) -> Self::RustType {
Self {
contract_address: <<H256 as SolValue>::SolType as SolType>::detokenize(
contract_address,
),
}
}
}

impl SolValue for Extra {
type SolType = Self;
}

impl SolTypeValue<Self> for Extra {
fn stv_to_tokens(&self) -> <Self as SolType>::Token<'_> {
(<<H256 as SolValue>::SolType as SolType>::tokenize(
&self.contract_address,
),)
}

fn stv_abi_encode_packed_to(&self, _out: &mut Vec<u8>) {
todo!()
}

fn stv_eip712_data_word(&self) -> alloy::sol_types::Word {
todo!()
}
}
}
3 changes: 2 additions & 1 deletion lib/state-lens-ics23-mpt-light-client-types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ thiserror = { workspace = true }
unionlabs = { workspace = true, features = ["ethabi", "proto"] }

[dev-dependencies]
hex-literal = { workspace = true }
hex-literal = { workspace = true }
state-lens-ics23-mpt-light-client-types = { workspace = true, features = ["bincode", "ethabi", "serde"] }

[features]
default = []
Expand Down
89 changes: 89 additions & 0 deletions lib/state-lens-ics23-mpt-light-client-types/src/client_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,92 @@ pub struct Extra {
/// storage_root = consensus_state[storage_root_offset:storage_root_offset+32]
pub storage_root_offset: u16,
}

#[cfg(feature = "ethabi")]
mod ethabi {
use alloy::{
dyn_abi::SolType,
sol_types::{private::SolTypeValue, SolValue},
};

use super::*;

impl SolType for Extra {
type RustType = Self;

type Token<'a> = <<<Self as AsTuple>::Tuple as SolValue>::SolType as SolType>::Token<'a>;

const SOL_NAME: &'static str = "Extra";

const ENCODED_SIZE: Option<usize> = None;

const PACKED_ENCODED_SIZE: Option<usize> = None;

fn valid_token(_token: &Self::Token<'_>) -> bool {
true
}

fn detokenize(
(timestamp_offset, state_root_offset, storage_root_offset): Self::Token<'_>,
) -> Self::RustType {
Self {
timestamp_offset: <<u16 as SolValue>::SolType as SolType>::detokenize(
timestamp_offset,
),
state_root_offset: <<u16 as SolValue>::SolType as SolType>::detokenize(
state_root_offset,
),
storage_root_offset: <<u16 as SolValue>::SolType as SolType>::detokenize(
storage_root_offset,
),
}
}
}

impl SolValue for Extra {
type SolType = Self;
}

impl SolTypeValue<Self> for Extra {
fn stv_to_tokens(&self) -> <Self as SolType>::Token<'_> {
(
<<u16 as SolValue>::SolType as SolType>::tokenize(&self.timestamp_offset),
<<u16 as SolValue>::SolType as SolType>::tokenize(&self.state_root_offset),
<<u16 as SolValue>::SolType as SolType>::tokenize(&self.storage_root_offset),
)
}

fn stv_abi_encode_packed_to(&self, _out: &mut Vec<u8>) {
todo!()
}

fn stv_eip712_data_word(&self) -> alloy::sol_types::Word {
todo!()
}
}
}

#[cfg(test)]
mod tests {
use alloy::dyn_abi::SolType;

#[test]
fn ethabi() {
alloy::sol! {
struct SolClientState {
string l2ChainId;
uint32 l1ClientId;
uint32 l2ClientId;
uint64 l2LatestHeight;
uint16 timestampOffset;
uint16 stateRootOffset;
uint16 storageRootOffset;
}
}

let bz = alloy::hex::decode("000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000e000000000000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000072734500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000083131313535313131000000000000000000000000000000000000000000000000").unwrap();

SolClientState::abi_decode(&bz, true).unwrap();
assert!(SolClientState::abi_decode_params(&bz, true).is_err());
}
}
11 changes: 7 additions & 4 deletions lib/state-lens-light-client-types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,19 @@ name = "state-lens-light-client-types"
repository.workspace = true
version = "0.1.0"

[package.metadata.docs.rs]
all-features = true

[dependencies]
alloy = { workspace = true, optional = true, features = ["sol-types", "dyn-abi"] }
bincode = { workspace = true, optional = true, features = ["alloc", "derive"] }
protos = { workspace = true, optional = true, features = ["proto_full", "serde"] }
serde = { workspace = true, optional = true, features = ["derive"] }
thiserror = { workspace = true }
tuple_join = { version = "0.1.0", optional = true }
unionlabs = { workspace = true, features = ["ethabi", "proto"] }
tuple_join = { version = "0.1.0" }
unionlabs = { workspace = true }

[dev-dependencies]
bcs = { workspace = true }
hex-literal = { workspace = true }
serde_json = { workspace = true }
state-lens-light-client-types = { workspace = true, features = ["bincode", "ethabi", "serde"] }
Expand All @@ -24,7 +27,7 @@ unionlabs = { workspace = true, features = ["test-utils"] }
default = []

bincode = ["dep:bincode", "unionlabs/bincode"]
ethabi = ["unionlabs/ethabi", "dep:alloy", "dep:protos", "dep:tuple_join"]
ethabi = ["unionlabs/ethabi", "dep:alloy"]
serde = ["dep:serde"]

[lints]
Expand Down
Loading

0 comments on commit 161d1ea

Please sign in to comment.