Skip to content

Commit

Permalink
validate header version during header deserialization
Browse files Browse the repository at this point in the history
  • Loading branch information
antiochp committed May 8, 2019
1 parent 94943e4 commit 540681d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
10 changes: 9 additions & 1 deletion core/src/core/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use std::fmt;
use std::iter::FromIterator;
use std::sync::Arc;

use crate::consensus::{reward, REWARD};
use crate::consensus::{self, reward, REWARD};
use crate::core::committed::{self, Committed};
use crate::core::compact_block::{CompactBlock, CompactBlockBody};
use crate::core::hash::{DefaultHashable, Hash, Hashed, ZERO_HASH};
Expand Down Expand Up @@ -300,6 +300,14 @@ impl Readable for BlockHeader {
return Err(ser::Error::CorruptedData);
}

// Check the block version before proceeding any further.
// We want to do this here because blocks can be pretty large
// and we want to halt processing as early as possible.
// If we receive an invalid block version then the peer is not on our hard-fork.
if !consensus::valid_header_version(height, version) {
return Err(ser::Error::InvalidBlockVersion);
}

Ok(BlockHeader {
version,
height,
Expand Down
4 changes: 4 additions & 0 deletions core/src/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ pub enum Error {
SortError,
/// Inputs/outputs/kernels must be unique.
DuplicateError,
/// Block header version (hard-fork schedule).
InvalidBlockVersion,
}

impl From<io::Error> for Error {
Expand All @@ -87,6 +89,7 @@ impl fmt::Display for Error {
Error::DuplicateError => f.write_str("duplicate"),
Error::TooLargeReadErr => f.write_str("too large read"),
Error::HexError(ref e) => write!(f, "hex error {:?}", e),
Error::InvalidBlockVersion => f.write_str("invalid block version"),
}
}
}
Expand All @@ -109,6 +112,7 @@ impl error::Error for Error {
Error::DuplicateError => "duplicate error",
Error::TooLargeReadErr => "too large read",
Error::HexError(_) => "hex error",
Error::InvalidBlockVersion => "invalid block version",
}
}
}
Expand Down

0 comments on commit 540681d

Please sign in to comment.