From 19fa36049f345e76e81a94823b84f4e9b9a52a16 Mon Sep 17 00:00:00 2001 From: teor Date: Tue, 29 Jun 2021 02:28:48 +1000 Subject: [PATCH] Provide a height in each transaction verification request (#2400) Block transactions already had a height, but mempool transactions didn't. This PR adds a height to mempool transactions, and deletes redundant and unused fields. It also adds an accessor method for that height. --- zebra-consensus/src/transaction.rs | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/zebra-consensus/src/transaction.rs b/zebra-consensus/src/transaction.rs index c5402e8c..d171be9c 100644 --- a/zebra-consensus/src/transaction.rs +++ b/zebra-consensus/src/transaction.rs @@ -82,21 +82,22 @@ pub enum Request { transaction: Arc, /// Additional UTXOs which are known at the time of verification. known_utxos: Arc>, - /// The height of the block containing this transaction, used to - /// determine the applicable network upgrade. + /// The height of the block containing this transaction. height: block::Height, }, /// Verify the supplied transaction as part of the mempool. /// + /// Mempool transactions do not have any additional UTXOs. + /// /// Note: coinbase transactions are invalid in the mempool Mempool { /// The transaction itself. transaction: Arc, - /// Additional UTXOs which are known at the time of verification. - known_utxos: Arc>, - /// Bug: this field should be the next block height, because some - /// consensus rules depend on the exact height. See #1683. - upgrade: NetworkUpgrade, + /// The height of the next block. + /// + /// The next block is the first block that could possibly contain a + /// mempool transaction. + height: block::Height, }, } @@ -113,7 +114,14 @@ impl Request { pub fn known_utxos(&self) -> Arc> { match self { Request::Block { known_utxos, .. } => known_utxos.clone(), - Request::Mempool { known_utxos, .. } => known_utxos.clone(), + Request::Mempool { .. } => HashMap::new().into(), + } + } + + /// The height used to select the consensus rules for verifying this transaction. + pub fn height(&self) -> block::Height { + match self { + Request::Block { height, .. } | Request::Mempool { height, .. } => *height, } } @@ -121,10 +129,7 @@ impl Request { /// /// This is based on the block height from the request, and the supplied `network`. pub fn upgrade(&self, network: Network) -> NetworkUpgrade { - match self { - Request::Block { height, .. } => NetworkUpgrade::current(network, *height), - Request::Mempool { upgrade, .. } => *upgrade, - } + NetworkUpgrade::current(network, self.height()) } }