From c4d72177c297682ea4c7cbe84d0f5b19a6c5da68 Mon Sep 17 00:00:00 2001 From: Henry de Valence Date: Sat, 8 Feb 2020 13:19:32 -0800 Subject: [PATCH] Add a Block::coinbase_height() method. --- zebra-chain/src/block.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/zebra-chain/src/block.rs b/zebra-chain/src/block.rs index 2c38b0d31..80a6f92f5 100644 --- a/zebra-chain/src/block.rs +++ b/zebra-chain/src/block.rs @@ -18,6 +18,7 @@ use crate::note_commitment_tree::SaplingNoteTreeRootHash; use crate::serialization::{ReadZcashExt, SerializationError, ZcashDeserialize, ZcashSerialize}; use crate::sha256d_writer::Sha256dWriter; use crate::transaction::Transaction; +use crate::types::BlockHeight; /// A SHA-256d hash of a BlockHeader. /// @@ -163,6 +164,20 @@ pub struct Block { pub transactions: Vec, } +impl Block { + /// Return the block height reported in the coinbase transaction, if any. + pub fn coinbase_height(&self) -> Option { + use crate::transaction::TransparentInput; + self.transactions + .get(0) + .and_then(|tx| tx.inputs().next()) + .and_then(|input| match input { + TransparentInput::Coinbase { ref height, .. } => Some(*height), + _ => None, + }) + } +} + impl<'a> From<&'a Block> for BlockHeaderHash { fn from(block: &'a Block) -> BlockHeaderHash { (&block.header).into()