Skip to content

Commit

Permalink
should error if next checkpoint found
Browse files Browse the repository at this point in the history
Signed-off-by: Naohiro Yoshida <[email protected]>
  • Loading branch information
Naohiro Yoshida committed Nov 29, 2023
1 parent 63dee41 commit cb19407
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 400 deletions.
6 changes: 3 additions & 3 deletions light-client/src/header/eth_header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use parlia_ibc_proto::ibc::lightclients::parlia::v1::EthHeader as RawETHHeader;

use crate::errors::Error;

use crate::header::vote_attestation::{VoteAttestation, BLS_PUBKEY_LENGTH};
use crate::header::vote_attestation::VoteAttestation;
use crate::misc::{Address, BlockNumber, ChainId, Hash, RlpIterator, Validators};

use super::BLOCKS_PER_EPOCH;
Expand All @@ -22,8 +22,8 @@ const DIFFICULTY_NOTURN: u64 = 1;
pub(crate) const EXTRA_VANITY: usize = 32;
pub(crate) const EXTRA_SEAL: usize = 65;
const VALIDATOR_BYTES_LENGTH_BEFORE_LUBAN: usize = 20;
pub(crate) const VALIDATOR_BYTES_LENGTH: usize =
VALIDATOR_BYTES_LENGTH_BEFORE_LUBAN + BLS_PUBKEY_LENGTH;
const BLS_PUBKEY_LENGTH: usize = 48;
const VALIDATOR_BYTES_LENGTH: usize = VALIDATOR_BYTES_LENGTH_BEFORE_LUBAN + BLS_PUBKEY_LENGTH;
const VALIDATOR_NUM_SIZE: usize = 1;

const PARAMS_GAS_LIMIT_BOUND_DIVISOR: u64 = 256;
Expand Down
42 changes: 25 additions & 17 deletions light-client/src/header/eth_headers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ use alloc::vec::Vec;
use parlia_ibc_proto::ibc::lightclients::parlia::v1::EthHeader;

use crate::errors::Error;
use crate::header::validator_set::{ValidatorRanges, ValidatorSet};
use crate::header::validator_set::ValidatorSet;

use crate::misc::ChainId;
use crate::misc::{ChainId, Validators};

Check failure on line 8 in light-client/src/header/eth_headers.rs

View workflow job for this annotation

GitHub Actions / ci

unused import: `Validators`

use super::eth_header::ETHHeader;
use super::BLOCKS_PER_EPOCH;

#[derive(Clone, Debug, PartialEq)]
pub struct ETHHeaders {
Expand All @@ -30,23 +31,31 @@ impl ETHHeaders {
}
}

let validator_ranges =
ValidatorRanges::new(&self.all, previous_validators, current_validators)?;

// Ensure valid seals
for h in &self.all {
let val = validator_ranges.get_to_verify_seal(h.number)?;
h.verify_seal(val, chain_id)?;
let epoch = self.target.number / BLOCKS_PER_EPOCH;
let checkpoint = epoch * BLOCKS_PER_EPOCH + previous_validators.checkpoint();
let previous_validators = previous_validators.validators()?;
for h in self.all.iter() {
if h.number >= checkpoint {
//TODO error if h.number reaches next checkpoint
h.verify_seal(current_validators.validators()?, chain_id)?;
} else {
h.verify_seal(previous_validators, chain_id)?;
}
}

// Ensure target is finalized
let headers_for_finalize = self.verify_finalized()?;

// Ensure BLS signature is collect
// At the just checkpoint BLS signature uses previous validator set.
for h in headers_for_finalize {
let val = validator_ranges.get_to_verify_vote(h.number)?;
let vote = h.get_vote_attestation()?;
vote.verify(h.number, val)?;
if h.number > checkpoint {
vote.verify(h.number, current_validators.validators()?)?;
} else {
vote.verify(h.number, previous_validators)?;
}
}
Ok(())
}
Expand Down Expand Up @@ -286,7 +295,7 @@ mod test {
}

#[test]
fn test_error_verify_untrusted_current_validators() {
fn test_error_verify_too_many_headers_to_seal() {
let v = vec![
header_31297200(),
header_31297201(),
Expand All @@ -307,8 +316,8 @@ mod test {

// success ( untrusted validator not used )
let p_val = trust(validators_in_31297000().into());
let c_val = empty();
let result = headers.verify(&mainnet(), &c_val, &p_val);
let untrusted_c_val = validators_in_31297000().into();
let result = headers.verify(&mainnet(), &untrusted_c_val, &p_val);
match result.unwrap_err() {
Error::UnexpectedTooManyHeadersToFinalize(e1, e2) => {
assert_eq!(e1, headers.target.number, "block error");
Expand All @@ -317,13 +326,12 @@ mod test {
e => unreachable!("{:?}", e),
}

// error (c_val doesn't contains any p_val)
// error
headers.all.push(header_31297211());
let c_val = vec![vec![1]].into();
let result = headers.verify(&mainnet(), &c_val, &p_val);
let result = headers.verify(&mainnet(), &untrusted_c_val, &p_val);
match result.unwrap_err() {
Error::ValidatorNotTrusted(e1) => {
assert_eq!(e1, c_val.hash);
assert_eq!(e1, untrusted_c_val.hash);
}
e => unreachable!("{:?}", e),
}
Expand Down
28 changes: 27 additions & 1 deletion light-client/src/header/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ fn verify_validator_set(
consensus_state.current_validators_hash,
));
}

// Try set trust by previous trusted validators
current_validators.trust(previous_validators.validators()?)
} else {
if header_epoch != trusted_epoch {
return Err(Error::UnexpectedTrustedHeight(
Expand Down Expand Up @@ -406,12 +409,18 @@ pub(crate) mod test {
}

fn to_validator_set(h: Hash) -> ValidatorSet {
let validators: Validators = vec![];
let validators: Validators = vec![vec![1]];
let mut v: ValidatorSet = validators.into();
v.hash = h;
v
}

fn to_validator_set_with_validators(h: Hash, v: Validators) -> ValidatorSet {
let mut v: ValidatorSet = v.into();
v.hash = h;
v
}

#[test]
fn test_success_verify_validator_set() {
let cs = ConsensusState {
Expand All @@ -423,6 +432,8 @@ pub(crate) mod test {

let height = new_height(0, 400);
let trusted_height = new_height(0, 201);

// Same validator set as previous
let current_validators = &mut to_validator_set([3u8; 32]);
let previous_validators = &mut to_validator_set(cs.current_validators_hash);
verify_validator_set(
Expand All @@ -434,6 +445,21 @@ pub(crate) mod test {
current_validators,
)
.unwrap();
assert!(current_validators.trusted);

let current_validators = &mut to_validator_set([3u8; 32]);
let previous_validators =
&mut to_validator_set_with_validators(cs.current_validators_hash, vec![vec![2]]);
verify_validator_set(
&cs,
true,
height,
trusted_height,
previous_validators,
current_validators,
)
.unwrap();
assert!(!current_validators.trusted);

let height = new_height(0, 599);
let trusted_height = new_height(0, 400);
Expand Down
Loading

0 comments on commit cb19407

Please sign in to comment.