Skip to content

Commit

Permalink
get turn term from extra_data
Browse files Browse the repository at this point in the history
Signed-off-by: Naohiro Yoshida <[email protected]>
  • Loading branch information
Naohiro Yoshida committed Jul 20, 2024
1 parent d69b045 commit 963484a
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 27 deletions.
4 changes: 0 additions & 4 deletions light-client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
22 changes: 7 additions & 15 deletions light-client/src/header/eth_header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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<Validators> {
pub fn get_validator_bytes_and_tern_term(extra_data: &[u8]) -> Option<(Validators, u8)> {
if extra_data.len() <= EXTRA_VANITY + EXTRA_SEAL {
return None;
}
Expand All @@ -265,17 +264,13 @@ pub fn get_validator_bytes(extra_data: &[u8]) -> Option<Validators> {
}
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<u8> {
//TODO get turn term from extra-data
Some(1)
extra_data[end],
))
}

impl TryFrom<RawETHHeader> for ETHHeader {
Expand Down Expand Up @@ -400,12 +395,9 @@ impl TryFrom<RawETHHeader> 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
};
Expand Down
9 changes: 4 additions & 5 deletions light-client/src/header/eth_headers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand Down Expand Up @@ -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);
}
Expand Down
6 changes: 3 additions & 3 deletions light-client/src/header/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
Expand Down Expand Up @@ -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),
}
Expand All @@ -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),
}
Expand Down

0 comments on commit 963484a

Please sign in to comment.