Handle attempts to get block headers at invalid heights (#3683)

* handle attempts to get block headers at invalid heights

* compare requested height against header pmmr size

* gte
This commit is contained in:
Yeastplume
2021-12-31 09:47:04 +00:00
committed by GitHub
parent 382e914c50
commit 2237f42144
3 changed files with 16 additions and 3 deletions
+4
View File
@@ -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),
+3
View File
@@ -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) {
+9 -3
View File
@@ -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 {