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.
This commit is contained in:
teor 2021-06-29 02:28:48 +10:00 committed by GitHub
parent df426cba9e
commit 19fa36049f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 17 additions and 12 deletions

View File

@ -82,21 +82,22 @@ pub enum Request {
transaction: Arc<Transaction>, transaction: Arc<Transaction>,
/// Additional UTXOs which are known at the time of verification. /// Additional UTXOs which are known at the time of verification.
known_utxos: Arc<HashMap<transparent::OutPoint, zs::Utxo>>, known_utxos: Arc<HashMap<transparent::OutPoint, zs::Utxo>>,
/// The height of the block containing this transaction, used to /// The height of the block containing this transaction.
/// determine the applicable network upgrade.
height: block::Height, height: block::Height,
}, },
/// Verify the supplied transaction as part of the mempool. /// 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 /// Note: coinbase transactions are invalid in the mempool
Mempool { Mempool {
/// The transaction itself. /// The transaction itself.
transaction: Arc<Transaction>, transaction: Arc<Transaction>,
/// Additional UTXOs which are known at the time of verification. /// The height of the next block.
known_utxos: Arc<HashMap<transparent::OutPoint, zs::Utxo>>, ///
/// Bug: this field should be the next block height, because some /// The next block is the first block that could possibly contain a
/// consensus rules depend on the exact height. See #1683. /// mempool transaction.
upgrade: NetworkUpgrade, height: block::Height,
}, },
} }
@ -113,7 +114,14 @@ impl Request {
pub fn known_utxos(&self) -> Arc<HashMap<transparent::OutPoint, zs::Utxo>> { pub fn known_utxos(&self) -> Arc<HashMap<transparent::OutPoint, zs::Utxo>> {
match self { match self {
Request::Block { known_utxos, .. } => known_utxos.clone(), 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`. /// This is based on the block height from the request, and the supplied `network`.
pub fn upgrade(&self, network: Network) -> NetworkUpgrade { pub fn upgrade(&self, network: Network) -> NetworkUpgrade {
match self { NetworkUpgrade::current(network, self.height())
Request::Block { height, .. } => NetworkUpgrade::current(network, *height),
Request::Mempool { upgrade, .. } => *upgrade,
}
} }
} }