Skip to content

Commit 3b63677

Browse files
author
Naohiro Yoshida
committed
Fix S3
Signed-off-by: Naohiro Yoshida <naohiro.yoshida@datachain.jp>
1 parent ddfd92e commit 3b63677

File tree

3 files changed

+62
-2
lines changed

3 files changed

+62
-2
lines changed

light-client/src/errors.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,8 @@ pub enum Error {
141141
UnexpectedMissingForkSpecInCurrentEpochCalculation(BlockNumber, alloc::boxed::Box<Error>),
142142
UnexpectedMissingForkSpecInPreviousEpochCalculation(BlockNumber, alloc::boxed::Box<Error>),
143143
UnexpectedPreviousEpochInCalculatingNextEpoch(BlockNumber, BlockNumber, BlockNumber),
144+
MustBeEpoch(BlockNumber, ForkSpec),
145+
MustNotBeEpoch(BlockNumber, ForkSpec),
144146

145147
// Misbehaviour
146148
MissingHeader1,
@@ -506,6 +508,12 @@ impl core::fmt::Display for Error {
506508
Error::UnexpectedEpochInfo(e1, e2) => {
507509
write!(f, "UnexpectedEpochInfo : {} {}", e1, e2)
508510
}
511+
Error::MustBeEpoch(e1, e2) => {
512+
write!(f, "MustBeEpoch : {} {:?}", e1, e2)
513+
}
514+
Error::MustNotBeEpoch(e1, e2) => {
515+
write!(f, "MustNotBeEpoch : {} {:?}", e1, e2)
516+
}
509517
}
510518
}
511519
}

light-client/src/header/eth_header.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,29 @@ impl ETHHeader {
361361
}
362362
}
363363

364+
pub fn verify_epoch_info(&self) -> Result<(), Error> {
365+
let be = self
366+
.boundary_epochs
367+
.as_ref()
368+
.ok_or(Error::MissingBoundaryEpochs(self.number))?;
369+
if self.number == be.current_epoch_block_number(self.number) {
370+
if !self.is_epoch() {
371+
return Err(Error::MustBeEpoch(
372+
self.number,
373+
be.current_fork_spec().clone(),
374+
));
375+
}
376+
} else {
377+
if self.is_epoch() {
378+
return Err(Error::MustNotBeEpoch(
379+
self.number,
380+
be.current_fork_spec().clone(),
381+
));
382+
}
383+
}
384+
Ok(())
385+
}
386+
364387
// https://github.com/bnb-chain/BEPs/blob/master/BEPs/BEP-520.md#411-millisecond-representation-in-block-header
365388
pub fn milli_timestamp(&self) -> u64 {
366389
let mut milliseconds: u64 = 0;
@@ -999,4 +1022,29 @@ pub(crate) mod test {
9991022
_ => unreachable!("invalid error {:?}", err),
10001023
}
10011024
}
1025+
1026+
#[rstest]
1027+
#[case::localnet(localnet())]
1028+
fn test_error_verify_epoch_info(#[case] hp: Box<dyn Network>) {
1029+
let mut header = hp.epoch_header();
1030+
header.epoch = None;
1031+
let err = header.verify_epoch_info().unwrap_err();
1032+
match err {
1033+
Error::MustBeEpoch(number, _fs) => {
1034+
assert_eq!(number, header.number);
1035+
}
1036+
_ => unreachable!("invalid error {:?}", err),
1037+
}
1038+
1039+
let mut header = hp.epoch_header();
1040+
header.epoch = Some(Epoch::new(vec![].into(), 1));
1041+
header.number += 1;
1042+
let err = header.verify_epoch_info().unwrap_err();
1043+
match err {
1044+
Error::MustNotBeEpoch(number, _fs) => {
1045+
assert_eq!(number, header.number);
1046+
}
1047+
_ => unreachable!("invalid error {:?}", err),
1048+
}
1049+
}
10021050
}

light-client/src/header/mod.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ impl Header {
8282
}
8383
// Ensure HF height is required for target without seeking next headers
8484
self.headers.target.set_boundary_epochs(&fork_specs)?;
85+
// Verify epoch is really epoch
86+
self.headers.target.verify_epoch_info()?;
8587

8688
// Try to set HF height
8789
if !fork_specs.is_empty() {
@@ -97,9 +99,11 @@ impl Header {
9799
}
98100
}
99101

100-
// Set boundary epoch to verify header size.
101102
for header in &mut self.headers.all {
102-
header.set_boundary_epochs(&fork_specs)?
103+
// Set boundary epoch to verify header size.
104+
header.set_boundary_epochs(&fork_specs)?;
105+
// Verify epoch is really epoch
106+
header.verify_epoch_info()?;
103107
}
104108

105109
self.fork_specs = fork_specs;

0 commit comments

Comments
 (0)