From 963484a31961e28706f92931120034bc5338f04e Mon Sep 17 00:00:00 2001 From: Naohiro Yoshida Date: Sat, 20 Jul 2024 09:10:27 +0900 Subject: [PATCH] get turn term from extra_data Signed-off-by: Naohiro Yoshida --- light-client/src/client.rs | 4 ---- light-client/src/header/eth_header.rs | 22 +++++++--------------- light-client/src/header/eth_headers.rs | 9 ++++----- light-client/src/header/mod.rs | 6 +++--- 4 files changed, 14 insertions(+), 27 deletions(-) diff --git a/light-client/src/client.rs b/light-client/src/client.rs index 0f875a3..a267926 100644 --- a/light-client/src/client.rs +++ b/light-client/src/client.rs @@ -554,10 +554,6 @@ mod test { } } - fn mainnet() -> ChainId { - ChainId::new(56) - } - #[test] fn test_success_create_client() { let client_state = hex!("0a272f6962632e6c69676874636c69656e74732e7061726c69612e76312e436c69656e745374617465124d08381214151f3951fa218cac426edfe078fa9e5c6dcea5001a2000000000000000000000000000000000000000000000000000000000000000002205109b9ea90f2a040880a305320410c0843d").to_vec(); diff --git a/light-client/src/header/eth_header.rs b/light-client/src/header/eth_header.rs index 7f4d416..970ecfa 100644 --- a/light-client/src/header/eth_header.rs +++ b/light-client/src/header/eth_header.rs @@ -11,7 +11,6 @@ use parlia_ibc_proto::ibc::lightclients::parlia::v1::EthHeader as RawETHHeader; use crate::errors::Error; use crate::header::epoch::Epoch; -use crate::header::validator_set::ValidatorSet; use crate::header::vote_attestation::VoteAttestation; use crate::misc::{Address, BlockNumber, ChainId, Hash, RlpIterator, Validators}; @@ -255,7 +254,7 @@ impl ETHHeader { } // https://github.com/bnb-chain/bsc/blob/33e6f840d25edb95385d23d284846955327b0fcd/consensus/parlia/parlia.go#L342 -pub fn get_validator_bytes(extra_data: &[u8]) -> Option { +pub fn get_validator_bytes_and_tern_term(extra_data: &[u8]) -> Option<(Validators, u8)> { if extra_data.len() <= EXTRA_VANITY + EXTRA_SEAL { return None; } @@ -265,17 +264,13 @@ pub fn get_validator_bytes(extra_data: &[u8]) -> Option { } let start = EXTRA_VANITY + VALIDATOR_NUM_SIZE; let end = start + num * VALIDATOR_BYTES_LENGTH; - Some( + Some(( extra_data[start..end] .chunks(VALIDATOR_BYTES_LENGTH) .map(|s| s.into()) .collect(), - ) -} - -pub fn get_turn_term(_extra_data: &[u8]) -> Option { - //TODO get turn term from extra-data - Some(1) + extra_data[end], + )) } impl TryFrom for ETHHeader { @@ -400,12 +395,9 @@ impl TryFrom for ETHHeader { let hash: Hash = keccak_256(&buffer_vec); let epoch = if number % BLOCKS_PER_EPOCH == 0 { - let validators: ValidatorSet = get_validator_bytes(&extra_data) - .ok_or_else(|| Error::MissingValidatorInEpochBlock(number))? - .into(); - let turn_term = get_turn_term(&extra_data) - .ok_or_else(|| Error::MissingTurnTermInEpochBlock(number))?; - Some(Epoch::new(validators, turn_term)) + let (validators, turn_term) = get_validator_bytes_and_tern_term(&extra_data) + .ok_or_else(|| Error::MissingValidatorInEpochBlock(number))?; + Some(Epoch::new(validators.into(), turn_term)) } else { None }; diff --git a/light-client/src/header/eth_headers.rs b/light-client/src/header/eth_headers.rs index 0d09a29..28eef0b 100644 --- a/light-client/src/header/eth_headers.rs +++ b/light-client/src/header/eth_headers.rs @@ -237,7 +237,7 @@ mod test { use crate::errors::Error; use crate::header::constant::BLOCKS_PER_EPOCH; - use crate::header::eth_header::{get_turn_term, get_validator_bytes, ETHHeader}; + use crate::header::eth_header::{get_validator_bytes_and_tern_term, ETHHeader}; use crate::header::eth_headers::ETHHeaders; use crate::fixture::*; @@ -537,10 +537,9 @@ mod test { if next.is_epoch() { // set n_val next.extra_data = n_val_header.extra_data.clone(); - next.epoch = Some(Epoch::new( - get_validator_bytes(&next.extra_data).unwrap().into(), - get_turn_term(&next.extra_data).unwrap(), - )); + let (validators, turn_term) = + get_validator_bytes_and_tern_term(&next.extra_data).unwrap(); + next.epoch = Some(Epoch::new(validators.into(), turn_term)); } headers.all.push(next); } diff --git a/light-client/src/header/mod.rs b/light-client/src/header/mod.rs index f2da3c6..a0780bc 100644 --- a/light-client/src/header/mod.rs +++ b/light-client/src/header/mod.rs @@ -543,7 +543,7 @@ pub(crate) mod test { assert_eq!(t, trusted_height); assert_eq!(h, height); assert_eq!(hash, previous_epoch.hash()); - assert_eq!(cons_hash, cons_hash); + assert_eq!(cons_hash, cs.current_validators_hash); } _ => unreachable!("err {:?}", err), } @@ -584,7 +584,7 @@ pub(crate) mod test { assert_eq!(t, trusted_height); assert_eq!(h, height); assert_eq!(hash, previous_epoch.hash()); - assert_eq!(cons_hash, cons_hash); + assert_eq!(cons_hash, cs.previous_validators_hash); } _ => unreachable!("err {:?}", err), } @@ -606,7 +606,7 @@ pub(crate) mod test { assert_eq!(t, trusted_height); assert_eq!(h, height); assert_eq!(hash, current_epoch.hash()); - assert_eq!(cons_hash, cons_hash); + assert_eq!(cons_hash, cs.current_validators_hash); } _ => unreachable!("err {:?}", err), }