Stop panicking during contextual validation

`check_contextual_validity` mistakenly used the new block's hash to try
to get the parent block from the state. This caused a panic, because the
new block isn't in the state yet.

Use `StateService::chain` to get the parent block, because we'll be
using `chain` for difficulty adjustment contextual verification anyway.
This commit is contained in:
teor 2020-11-16 13:06:40 +10:00
parent 54cb9277ef
commit d80a0c7402
1 changed files with 6 additions and 5 deletions

View File

@ -190,9 +190,10 @@ impl StateService {
); );
check::block_is_not_orphaned(finalized_tip_height, block)?; check::block_is_not_orphaned(finalized_tip_height, block)?;
let parent_block = self let mut relevant_chain = self.chain(block.header.previous_block_hash);
.block(block.hash().into()) let parent_block = relevant_chain
.expect("the parent's presence has already been checked"); .next()
.expect("state must contain parent block to do contextual validation");
let parent_height = parent_block let parent_height = parent_block
.coinbase_height() .coinbase_height()
.expect("valid blocks have a coinbase height"); .expect("valid blocks have a coinbase height");
@ -201,7 +202,8 @@ impl StateService {
// should be impossible by design, so no handleable error is thrown // should be impossible by design, so no handleable error is thrown
assert_eq!(parent_hash, block.header.previous_block_hash); assert_eq!(parent_hash, block.header.previous_block_hash);
// TODO: contextual validation design and implementation // TODO: validate difficulty adjustment
// TODO: other contextual validation design and implelentation
Ok(()) Ok(())
} }
@ -272,7 +274,6 @@ impl StateService {
/// ///
/// The block identified by `hash` is included in the chain of blocks yielded /// The block identified by `hash` is included in the chain of blocks yielded
/// by the iterator. /// by the iterator.
#[allow(dead_code)]
pub fn chain(&self, hash: block::Hash) -> Iter<'_> { pub fn chain(&self, hash: block::Hash) -> Iter<'_> {
Iter { Iter {
service: self, service: self,