From d80a0c74026f6285aada7828220edab9b3f39104 Mon Sep 17 00:00:00 2001 From: teor Date: Mon, 16 Nov 2020 13:06:40 +1000 Subject: [PATCH] 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. --- zebra-state/src/service.rs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/zebra-state/src/service.rs b/zebra-state/src/service.rs index 90769ed9d..d502b3fae 100644 --- a/zebra-state/src/service.rs +++ b/zebra-state/src/service.rs @@ -190,9 +190,10 @@ impl StateService { ); check::block_is_not_orphaned(finalized_tip_height, block)?; - let parent_block = self - .block(block.hash().into()) - .expect("the parent's presence has already been checked"); + let mut relevant_chain = self.chain(block.header.previous_block_hash); + let parent_block = relevant_chain + .next() + .expect("state must contain parent block to do contextual validation"); let parent_height = parent_block .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 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(()) } @@ -272,7 +274,6 @@ impl StateService { /// /// The block identified by `hash` is included in the chain of blocks yielded /// by the iterator. - #[allow(dead_code)] pub fn chain(&self, hash: block::Hash) -> Iter<'_> { Iter { service: self,