Skip to content

Commit

Permalink
fix clippy
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 27, 2024
1 parent fef195f commit 68afd76
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 19 deletions.
2 changes: 1 addition & 1 deletion light-client/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ impl core::fmt::Display for Error {
}
Error::LCPError(e1) => {
write!(f, "LCPError: {}", e1)
},
}
Error::UnexpectedDifficultyInTurn(e1, e2, e3) => {
write!(f, "UnexpectedDifficultyInTurn : {} {} {}", e1, e2, e3)
}
Expand Down
28 changes: 19 additions & 9 deletions light-client/src/header/eth_header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ impl ETHHeader {
}

fn verify_validator_rotation(&self, epoch: &Epoch) -> Result<(), Error> {
let offset = (self.number / epoch.turn_length() as u64 ) as usize % epoch.validators().len();
let offset = (self.number / epoch.turn_length() as u64) as usize % epoch.validators().len();
let inturn_validator = &epoch.validators()[offset][0..VALIDATOR_BYTES_LENGTH_BEFORE_LUBAN];
if inturn_validator == self.coinbase {
if self.difficulty != DIFFICULTY_INTURN {
Expand Down Expand Up @@ -502,15 +502,18 @@ impl TryFrom<RawETHHeader> for ETHHeader {
#[cfg(test)]
pub(crate) mod test {
use crate::errors::Error;
use crate::header::eth_header::{DIFFICULTY_INTURN, DIFFICULTY_NOTURN, ETHHeader, EXTRA_SEAL, EXTRA_VANITY, PARAMS_GAS_LIMIT_BOUND_DIVISOR, VALIDATOR_BYTES_LENGTH_BEFORE_LUBAN};
use crate::header::eth_header::{
ETHHeader, DIFFICULTY_INTURN, DIFFICULTY_NOTURN, EXTRA_SEAL, EXTRA_VANITY,
PARAMS_GAS_LIMIT_BOUND_DIVISOR, VALIDATOR_BYTES_LENGTH_BEFORE_LUBAN,
};

use rlp::RlpStream;
use rstest::*;

use crate::fixture::{localnet, Network};
use crate::header::epoch::Epoch;
use alloc::boxed::Box;
use parlia_ibc_proto::ibc::lightclients::parlia::v1::EthHeader as RawETHHeader;
use crate::header::epoch::Epoch;

fn to_raw(header: &ETHHeader) -> RawETHHeader {
let mut stream = RlpStream::new();
Expand Down Expand Up @@ -819,8 +822,11 @@ pub(crate) mod test {
let mut header = hp.epoch_header();
header.difficulty = DIFFICULTY_NOTURN;
let prev = hp.previous_epoch_header();
match header.verify_validator_rotation(&prev.epoch.unwrap()).unwrap_err() {
Error::UnexpectedDifficultyInTurn(e1, e2, e3) => {
match header
.verify_validator_rotation(&prev.epoch.unwrap())
.unwrap_err()
{
Error::UnexpectedDifficultyInTurn(e1, e2, _e3) => {
assert_eq!(e1, header.number);
assert_eq!(e2, header.difficulty);
}
Expand All @@ -834,10 +840,14 @@ pub(crate) mod test {
let mut header = hp.epoch_header();
header.difficulty = DIFFICULTY_INTURN;
let prev = hp.previous_epoch_header();
header.coinbase =
prev.epoch.clone().unwrap().validators()[1][0..VALIDATOR_BYTES_LENGTH_BEFORE_LUBAN].to_vec();
match header.verify_validator_rotation(&prev.epoch.unwrap()).unwrap_err() {
Error::UnexpectedDifficultyNoTurn(e1, e2, e3) => {
header.coinbase = prev.epoch.clone().unwrap().validators()[1]
[0..VALIDATOR_BYTES_LENGTH_BEFORE_LUBAN]
.to_vec();
match header
.verify_validator_rotation(&prev.epoch.unwrap())
.unwrap_err()
{
Error::UnexpectedDifficultyNoTurn(e1, e2, _e3) => {
assert_eq!(e1, header.number);
assert_eq!(e2, header.difficulty);
}
Expand Down
12 changes: 3 additions & 9 deletions light-client/src/header/eth_headers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::errors::Error::MissingEpochInfoInEpochBlock;
use crate::header::epoch::EitherEpoch::{Trusted, Untrusted};
use crate::header::epoch::{EitherEpoch, Epoch, TrustedEpoch, UntrustedEpoch};

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

use super::eth_header::ETHHeader;
use super::BLOCKS_PER_EPOCH;
Expand Down Expand Up @@ -218,17 +218,11 @@ fn verify_finalized(
Ok(())
}

fn unwrap_n_val<'a>(
n: BlockNumber,
n_val: &'a Option<&'a Epoch>,
) -> Result<&'a Epoch, Error> {
fn unwrap_n_val<'a>(n: BlockNumber, n_val: &'a Option<&'a Epoch>) -> Result<&'a Epoch, Error> {
n_val.ok_or_else(|| Error::MissingNextValidatorSet(n))
}

fn unwrap_c_val<'a>(
n: BlockNumber,
c_val: &'a Option<&'a Epoch>,
) -> Result<&'a Epoch, Error> {
fn unwrap_c_val<'a>(n: BlockNumber, c_val: &'a Option<&'a Epoch>) -> Result<&'a Epoch, Error> {
c_val.ok_or_else(|| Error::MissingCurrentValidatorSet(n))
}

Expand Down

0 comments on commit 68afd76

Please sign in to comment.