Skip to content

Commit

Permalink
Handle attempts to get block headers at invalid heights (#3683)
Browse files Browse the repository at this point in the history
* handle attempts to get block headers at invalid heights

* compare requested height against header pmmr size

* gte
  • Loading branch information
yeastplume authored Dec 31, 2021
1 parent 382e914 commit 2237f42
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
4 changes: 4 additions & 0 deletions chain/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,10 @@ pub enum ErrorKind {
/// Error from underlying block handling
#[fail(display = "Block Validation Error: {:?}", _0)]
Block(block::Error),
/// Attempt to retrieve a header at a height greater than
/// the max allowed by u64 limits
#[fail(display = "Invalid Header Height: {}", _0)]
InvalidHeaderHeight(u64),
/// Anything else
#[fail(display = "Other Error: {}", _0)]
Other(String),
Expand Down
3 changes: 3 additions & 0 deletions chain/src/txhashset/txhashset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ impl PMMRHandle<BlockHeader> {

/// Get the header hash at the specified height based on the current header MMR state.
pub fn get_header_hash_by_height(&self, height: u64) -> Result<Hash, Error> {
if height >= self.size {
return Err(ErrorKind::InvalidHeaderHeight(height).into());
}
let pos = pmmr::insertion_to_pmmr_index(height);
let header_pmmr = ReadonlyPMMR::at(&self.backend, self.size);
if let Some(entry) = header_pmmr.get_data(pos) {
Expand Down
12 changes: 9 additions & 3 deletions servers/src/grin/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -454,9 +454,15 @@ impl Server {
// We need to query again for the actual block hash, as
// the difficulty iterator doesn't contain enough info to
// create a hash
let block_hash = match self.chain.get_header_by_height(height as u64) {
Ok(h) => h.hash(),
Err(_) => ZERO_HASH,
// The diff iterator returns 59 block headers 'before' 0 to give callers
// enough detail to calculate initial block difficulties. Ignore these.
let block_hash = if height < 0 {
ZERO_HASH
} else {
match self.chain.get_header_by_height(height as u64) {
Ok(h) => h.hash(),
Err(_) => ZERO_HASH,
}
};

DiffBlock {
Expand Down

0 comments on commit 2237f42

Please sign in to comment.