Add a Block::coinbase_height() method.

This commit is contained in:
Henry de Valence 2020-02-08 13:19:32 -08:00 committed by Deirdre Connolly
parent 56d7391f6d
commit c4d72177c2
1 changed files with 15 additions and 0 deletions

View File

@ -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<Transaction>,
}
impl Block {
/// Return the block height reported in the coinbase transaction, if any.
pub fn coinbase_height(&self) -> Option<BlockHeight> {
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()