Skip to content

Commit

Permalink
feat(lightclients): add codec tests to ethereum-light-client
Browse files Browse the repository at this point in the history
  • Loading branch information
benluelo committed Jan 5, 2025
1 parent a173b99 commit 8f18826
Show file tree
Hide file tree
Showing 12 changed files with 456 additions and 15 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion cosmwasm/ibc-union/light-clients/ethereum/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ pub fn verify_membership(
storage_root,
storage_proof.key,
&rlp::encode(&storage_proof.value),
&storage_proof.proof,
storage_proof.proof,
)
.map_err(Error::VerifyStorageProof)
}
Expand Down
9 changes: 6 additions & 3 deletions lib/ethereum-light-client-types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ thiserror = { workspace = true }
unionlabs = { workspace = true }

[features]
bincode = ["dep:bincode", "beacon-api-types/bincode"]
default = ["serde"]
default = []

bincode = ["dep:bincode", "unionlabs/bincode", "beacon-api-types/bincode"]
ethabi = ["unionlabs/ethabi", "dep:alloy"]
serde = [
# TODO: Feature gate serde in unionlabs
Expand All @@ -23,4 +24,6 @@ serde = [
]

[dev-dependencies]
hex-literal = { workspace = true }
ethereum-light-client-types = { workspace = true, features = ["bincode", "ethabi", "serde"] }
hex-literal = { workspace = true }
unionlabs = { workspace = true, features = ["test-utils"] }
54 changes: 54 additions & 0 deletions lib/ethereum-light-client-types/src/client_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,57 @@ pub struct ClientState {
/// the ibc contract on the counterparty chain that contains the ICS23 commitments
pub ibc_contract_address: H160,
}

#[cfg(test)]
mod tests {
use beacon_api_types::{Fork, Version};
use unionlabs::{
encoding::{Bincode, Json},
primitives::{FixedBytes, H256},
test_utils::assert_codec_iso,
};

use super::*;

fn mk_client_state() -> ClientState {
ClientState {
chain_id: U256::from(1u64),
chain_spec: PresetBaseKind::Minimal,
genesis_validators_root: H256::new([0xAA; 32]),
genesis_time: 123,
fork_parameters: ForkParameters {
genesis_fork_version: Version(FixedBytes::new([1, 2, 3, 4])),
genesis_slot: 1,
altair: Fork {
version: Version(FixedBytes::new([2, 3, 4, 5])),
epoch: 2,
},
bellatrix: Fork {
version: Version(FixedBytes::new([3, 4, 5, 6])),
epoch: 3,
},
capella: Fork {
version: Version(FixedBytes::new([4, 5, 6, 7])),
epoch: 4,
},
deneb: Fork {
version: Version(FixedBytes::new([5, 6, 7, 8])),
epoch: 5,
},
},
latest_height: 987,
frozen_height: Height::new(1),
ibc_contract_address: H160::new([0xAA; 20]),
}
}

#[test]
fn bincode_iso() {
assert_codec_iso::<_, Bincode>(&mk_client_state());
}

#[test]
fn json_iso() {
assert_codec_iso::<_, Json>(&mk_client_state());
}
}
32 changes: 32 additions & 0 deletions lib/ethereum-light-client-types/src/consensus_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,35 @@ pub mod ethabi {
}
}
}

#[cfg(test)]
mod tests {
use unionlabs::{
encoding::{EthAbi, Json},
primitives::H256,
test_utils::assert_codec_iso,
};

use super::*;

fn mk_consensus_state() -> ConsensusState {
ConsensusState {
slot: 42,
state_root: H256::new([0xAA; 32]),
storage_root: H256::new([0xAA; 32]),
timestamp: 123_456_789,
current_sync_committee: H384::new([0xAA; 48]),
next_sync_committee: H384::new([0xAA; 48]),
}
}

#[test]
fn ethabi_iso() {
assert_codec_iso::<_, EthAbi>(&mk_consensus_state());
}

#[test]
fn json_iso() {
assert_codec_iso::<_, Json>(&mk_consensus_state());
}
}
114 changes: 114 additions & 0 deletions lib/ethereum-light-client-types/src/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,117 @@ pub struct Header {
/// Proof of the IBC handler contract against the execution state root provided in `consensus_update`.
pub ibc_account_proof: AccountProof,
}

#[cfg(test)]
mod tests {
use beacon_api_types::{
execution_payload_header::ExecutionPayloadHeader, BeaconBlockHeader, LightClientHeader,
SyncAggregate, SyncCommittee,
};
use unionlabs::{
encoding::{Bincode, Json},
primitives::{H160, H256, H384, H768},
test_utils::assert_codec_iso,
uint::U256,
};

use super::*;
use crate::{EpochChangeUpdate, LightClientUpdateData};

fn mk_header() -> Header {
Header {
trusted_height: Height::new(123),
consensus_update: LightClientUpdate::EpochChange(Box::new(EpochChangeUpdate {
sync_committee: SyncCommittee {
pubkeys: vec![H384::new([0xAA; 48])],
aggregate_pubkey: H384::new([0xAA; 48]),
},
next_sync_committee: SyncCommittee {
pubkeys: vec![H384::new([0xAA; 48])],
aggregate_pubkey: H384::new([0xAA; 48]),
},
next_sync_committee_branch: [H256::new([0xAA; 32]); 5],
update_data: LightClientUpdateData {
attested_header: LightClientHeader {
beacon: BeaconBlockHeader {
slot: 123,
proposer_index: 456,
parent_root: H256::new([0xAA; 32]),
state_root: H256::new([0xBB; 32]),
body_root: H256::new([0xCC; 32]),
},
execution: ExecutionPayloadHeader {
parent_hash: H256::new([0xAA; 32]),
fee_recipient: H160::new([0xAA; 20]),
state_root: H256::new([0xAA; 32]),
receipts_root: H256::new([0xAA; 32]),
logs_bloom: b"bloom".into(),
prev_randao: H256::new([0xAA; 32]),
block_number: 69,
gas_limit: 1_987_654_321,
gas_used: 987_654_321,
timestamp: 123_456_789,
extra_data: b"extra".into(),
base_fee_per_gas: U256::from(1u64),
block_hash: H256::new([0xAA; 32]),
transactions_root: H256::new([0xAA; 32]),
withdrawals_root: H256::new([0xAA; 32]),
blob_gas_used: 100,
excess_blob_gas: 100,
},
execution_branch: [H256::new([0xAA; 32]); 4],
},
finalized_header: LightClientHeader {
beacon: BeaconBlockHeader {
slot: 123,
proposer_index: 456,
parent_root: H256::new([0xAA; 32]),
state_root: H256::new([0xBB; 32]),
body_root: H256::new([0xCC; 32]),
},
execution: ExecutionPayloadHeader {
parent_hash: H256::new([0xAA; 32]),
fee_recipient: H160::new([0xAA; 20]),
state_root: H256::new([0xAA; 32]),
receipts_root: H256::new([0xAA; 32]),
logs_bloom: b"bloom".into(),
prev_randao: H256::new([0xAA; 32]),
block_number: 69,
gas_limit: 1_987_654_321,
gas_used: 987_654_321,
timestamp: 123_456_789,
extra_data: b"extra".into(),
base_fee_per_gas: U256::from(1u64),
block_hash: H256::new([0xAA; 32]),
transactions_root: H256::new([0xAA; 32]),
withdrawals_root: H256::new([0xAA; 32]),
blob_gas_used: 100,
excess_blob_gas: 100,
},
execution_branch: [H256::new([0xAA; 32]); 4],
},
finality_branch: [H256::new([0xAA; 32]); 6],
sync_aggregate: SyncAggregate {
sync_committee_bits: [1, 2, 3].to_vec(),
sync_committee_signature: H768::new([0xAA; 96]),
},
signature_slot: 123,
},
})),
ibc_account_proof: AccountProof {
storage_root: H256::new([0xAA; 32]),
proof: vec![b"ooga".to_vec(), b"booga".to_vec()],
},
}
}

#[test]
fn bincode_iso() {
assert_codec_iso::<_, Bincode>(&mk_header());
}

#[test]
fn json_iso() {
assert_codec_iso::<_, Json>(&mk_header());
}
}
Loading

0 comments on commit 8f18826

Please sign in to comment.