Skip to content

Commit

Permalink
node-data: fix overflow while faults validation
Browse files Browse the repository at this point in the history
  • Loading branch information
herr-seppia committed Aug 8, 2024
1 parent 29df7cc commit 2ad5860
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions node-data/src/ledger/faults.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ pub enum InvalidFault {
Expired,
#[error("Fault is from future")]
Future,
#[error("Fault is from genesis block")]
Genesis,
#[error("Previous hash mismatch")]
PrevHashMismatch,
#[error("Iteration mismatch")]
Expand Down Expand Up @@ -153,8 +155,12 @@ impl Fault {
return Err(InvalidFault::Duplicated);
}

if h1.round == 0 {
return Err(InvalidFault::Genesis);
}

// Check that fault is not expired. A fault expires after an epoch
if h1.round < current_height - EPOCH {
if h1.round < current_height.saturating_sub(EPOCH) {
return Err(InvalidFault::Expired);
}

Expand Down Expand Up @@ -298,10 +304,10 @@ impl Serializable for Fault {
FaultData::read(r)?,
FaultData::read(r)?,
),
p => {
println!("{p}");
Err(io::Error::new(io::ErrorKind::InvalidData, "Invalid faul"))?
}
p => Err(io::Error::new(
io::ErrorKind::InvalidData,
format!("Invalid fault: {p}"),
))?,
};
Ok(fault)
}
Expand Down

0 comments on commit 2ad5860

Please sign in to comment.