Skip to content

Commit

Permalink
add type conversion tests
Browse files Browse the repository at this point in the history
Signed-off-by: Jun Kimura <[email protected]>
  • Loading branch information
bluele committed Nov 11, 2024
1 parent 4bfad48 commit f25c496
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 10 deletions.
27 changes: 17 additions & 10 deletions crates/ibc/src/client_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -940,19 +940,10 @@ mod tests {
use ethereum_consensus::preset::minimal::PRESET;
use hex_literal::hex;
use time::{macros::datetime, OffsetDateTime};
use tiny_keccak::{Hasher, Keccak};

fn keccak256(s: &str) -> H256 {
let mut hasher = Keccak::v256();
let mut output = [0u8; 32];
hasher.update(s.as_bytes());
hasher.finalize(&mut output);
H256::from_slice(&output)
}

#[test]
fn test_client_state_conversion() {
let mut client_state =
let client_state =
ClientState::<{ ethereum_consensus::preset::minimal::PRESET.SYNC_COMMITTEE_SIZE }> {
genesis_validators_root: keccak256("genesis_validators_root"),
min_sync_committee_participants: 1.into(),
Expand Down Expand Up @@ -982,6 +973,13 @@ mod tests {
};
let res = client_state.validate();
assert!(res.is_ok(), "{:?}", res);

let any_client_state: Any = client_state.clone().into();
let client_state2 = ClientState::try_from(any_client_state).unwrap();
assert_eq!(client_state, client_state2);

// Unexpected fork parameters
let mut client_state = client_state.clone();
client_state.fork_parameters = ForkParameters::new(
Version([0, 0, 0, 1]),
vec![ForkParameter::new(
Expand Down Expand Up @@ -1176,4 +1174,13 @@ mod tests {
assert!(trim_left_zero(&[0, 0, 0, 0]).is_empty());
assert!(trim_left_zero(&[]).is_empty());
}

fn keccak256(s: &str) -> H256 {
use tiny_keccak::{Hasher, Keccak};
let mut hasher = Keccak::v256();
let mut output = [0u8; 32];
hasher.update(s.as_bytes());
hasher.finalize(&mut output);
H256::from_slice(&output)
}
}
32 changes: 32 additions & 0 deletions crates/ibc/src/consensus_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@ pub struct ConsensusState {
/// timestamp from execution payload
pub timestamp: Timestamp,
/// aggregate public key of current sync committee
/// "current" indicates a period corresponding to the `slot`
pub current_sync_committee: PublicKey,
/// aggregate public key of next sync committee
/// "next" indicates `current + 1` period
pub next_sync_committee: PublicKey,
}

Expand Down Expand Up @@ -269,8 +271,29 @@ impl<const SYNC_COMMITTEE_SIZE: usize> From<TrustedConsensusState<SYNC_COMMITTEE
#[cfg(test)]
mod tests {
use super::*;
use ethereum_consensus::types::H256;
use hex_literal::hex;
use time::macros::datetime;

#[test]
fn test_consensus_state_conversion() {
let consensus_state = ConsensusState {
slot: 1.into(),
storage_root: CommitmentRoot::from_bytes(keccak256("storage").as_bytes()),
timestamp: Timestamp::from_nanoseconds(
datetime!(2023-08-20 0:00 UTC).unix_timestamp_nanos() as u64,
)
.unwrap(),
current_sync_committee: PublicKey::try_from(hex!("a145063e1b5eda80fa55960296f2c4b2c021f75767318ea2572a9f7abb649010b746754ca7fc2ba57c1156881516a357").to_vec()).unwrap(),
next_sync_committee: PublicKey::try_from(hex!("a42dffb90d85cec7acfcb53be0e8792155d8f18c0dc9efc2a5587d5a0ba3e578df366fc3e2b743de6ecd3b53e345c266").to_vec()).unwrap(),
};
let res = consensus_state.validate();
assert!(res.is_ok(), "{:?}", res);
let any_consensus_state = IBCAny::from(consensus_state.clone());
let consensus_state2 = ConsensusState::try_from(any_consensus_state).unwrap();
assert_eq!(consensus_state, consensus_state2);
}

#[test]
fn test_timestamp() {
{
Expand All @@ -295,4 +318,13 @@ mod tests {
assert_eq!(it1, it2);
}
}

fn keccak256(s: &str) -> H256 {
use tiny_keccak::{Hasher, Keccak};
let mut hasher = Keccak::v256();
let mut output = [0u8; 32];
hasher.update(s.as_bytes());
hasher.finalize(&mut output);
H256::from_slice(&output)
}
}

0 comments on commit f25c496

Please sign in to comment.