Genesis is height 0 (not height 1) (#1896)

This commit is contained in:
Antioch Peverell 2018-10-31 20:24:21 +00:00 committed by Ignotus Peverell
parent b01fcd2f56
commit d51522a9cd
2 changed files with 11 additions and 3 deletions

View file

@ -713,7 +713,7 @@ impl<'a> HeaderExtension<'a> {
pub fn validate_root(&self, header: &BlockHeader) -> Result<(), Error> { pub fn validate_root(&self, header: &BlockHeader) -> Result<(), Error> {
// If we are validating the genesis block then we have no prev_root. // If we are validating the genesis block then we have no prev_root.
// So we are done here. // So we are done here.
if header.height == 1 { if header.height == 0 {
return Ok(()); return Ok(());
} }
@ -1102,7 +1102,7 @@ impl<'a> Extension<'a> {
/// Validate the provided header by comparing its prev_root to the /// Validate the provided header by comparing its prev_root to the
/// root of the current header MMR. /// root of the current header MMR.
pub fn validate_header_root(&self, header: &BlockHeader) -> Result<(), Error> { pub fn validate_header_root(&self, header: &BlockHeader) -> Result<(), Error> {
if header.height == 1 { if header.height == 0 {
return Ok(()); return Ok(());
} }

View file

@ -17,7 +17,7 @@ use std::u64;
use croaring::Bitmap; use croaring::Bitmap;
use core::hash::Hash; use core::hash::{Hash, ZERO_HASH};
use core::merkle_proof::MerkleProof; use core::merkle_proof::MerkleProof;
use core::pmmr::{Backend, ReadonlyPMMR}; use core::pmmr::{Backend, ReadonlyPMMR};
use core::BlockHeader; use core::BlockHeader;
@ -124,6 +124,9 @@ where
/// Computes the root of the MMR. Find all the peaks in the current /// Computes the root of the MMR. Find all the peaks in the current
/// tree and "bags" them to get a single peak. /// tree and "bags" them to get a single peak.
pub fn root(&self) -> Hash { pub fn root(&self) -> Hash {
if self.is_empty() {
return ZERO_HASH;
}
let mut res = None; let mut res = None;
for peak in self.peaks().iter().rev() { for peak in self.peaks().iter().rev() {
res = match res { res = match res {
@ -350,6 +353,11 @@ where
Ok(()) Ok(())
} }
/// Check if this PMMR is (unpruned_size == 0).
pub fn is_empty(&self) -> bool {
self.unpruned_size() == 0
}
/// Total size of the tree, including intermediary nodes and ignoring any /// Total size of the tree, including intermediary nodes and ignoring any
/// pruning. /// pruning.
pub fn unpruned_size(&self) -> u64 { pub fn unpruned_size(&self) -> u64 {