rework pool tx validation logic a bit (#1826)

This commit is contained in:
Antioch Peverell 2018-10-24 10:55:42 +01:00 committed by GitHub
parent 3efa7bdac9
commit 3d59cce7c5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -133,8 +133,7 @@ impl Pool {
// Iteratively apply the txs to the current chain state, // Iteratively apply the txs to the current chain state,
// rejecting any that do not result in a valid state. // rejecting any that do not result in a valid state.
// Return a vec of all the valid txs. // Return a vec of all the valid txs.
let block_sums = self.blockchain.get_block_sums(&header.hash())?; let txs = self.validate_raw_txs(flat_txs, None, &header)?;
let txs = self.validate_raw_txs(flat_txs, None, &header, &block_sums)?;
Ok(txs) Ok(txs)
} }
@ -159,8 +158,7 @@ impl Pool {
extra_tx: Option<Transaction>, extra_tx: Option<Transaction>,
header: &BlockHeader, header: &BlockHeader,
) -> Result<Vec<Transaction>, PoolError> { ) -> Result<Vec<Transaction>, PoolError> {
let block_sums = self.blockchain.get_block_sums(&header.hash())?; let valid_txs = self.validate_raw_txs(txs, extra_tx, header)?;
let valid_txs = self.validate_raw_txs(txs, extra_tx, header, &block_sums)?;
Ok(valid_txs) Ok(valid_txs)
} }
@ -238,8 +236,14 @@ impl Pool {
tx: &Transaction, tx: &Transaction,
header: &BlockHeader, header: &BlockHeader,
) -> Result<BlockSums, PoolError> { ) -> Result<BlockSums, PoolError> {
let block_sums = self.blockchain.get_block_sums(&header.hash())?; tx.validate(self.verifier_cache.clone())?;
let new_sums = self.apply_txs_to_block_sums(&block_sums, vec![tx.clone()], header)?;
// Validate the tx against current chain state.
// Check all inputs are in the current UTXO set.
// Check all outputs are unique in current UTXO set.
self.blockchain.validate_tx(tx)?;
let new_sums = self.apply_tx_to_block_sums(tx, header)?;
Ok(new_sums) Ok(new_sums)
} }
@ -248,7 +252,6 @@ impl Pool {
txs: Vec<Transaction>, txs: Vec<Transaction>,
extra_tx: Option<Transaction>, extra_tx: Option<Transaction>,
header: &BlockHeader, header: &BlockHeader,
block_sums: &BlockSums,
) -> Result<Vec<Transaction>, PoolError> { ) -> Result<Vec<Transaction>, PoolError> {
let mut valid_txs = vec![]; let mut valid_txs = vec![];
@ -259,10 +262,12 @@ impl Pool {
}; };
candidate_txs.extend(valid_txs.clone()); candidate_txs.extend(valid_txs.clone());
candidate_txs.push(tx.clone()); candidate_txs.push(tx.clone());
if self
.apply_txs_to_block_sums(&block_sums, candidate_txs, header) // Build a single aggregate tx from candidate txs.
.is_ok() let agg_tx = transaction::aggregate(candidate_txs)?;
{
// We know the tx is valid if the entire aggregate tx is valid.
if self.validate_raw_tx(&agg_tx, header).is_ok() {
valid_txs.push(tx); valid_txs.push(tx);
} }
} }
@ -270,28 +275,20 @@ impl Pool {
Ok(valid_txs) Ok(valid_txs)
} }
fn apply_txs_to_block_sums( fn apply_tx_to_block_sums(
&self, &self,
block_sums: &BlockSums, tx: &Transaction,
txs: Vec<Transaction>,
header: &BlockHeader, header: &BlockHeader,
) -> Result<BlockSums, PoolError> { ) -> Result<BlockSums, PoolError> {
// Build a single aggregate tx and validate it.
let tx = transaction::aggregate(txs)?;
tx.validate(self.verifier_cache.clone())?;
// Validate the tx against current chain state.
// Check all inputs are in the current UTXO set.
// Check all outputs are unique in current UTXO set.
self.blockchain.validate_tx(&tx)?;
let overage = tx.overage(); let overage = tx.overage();
let offset = (header.total_kernel_offset() + tx.offset)?; let offset = (header.total_kernel_offset() + tx.offset)?;
let block_sums = self.blockchain.get_block_sums(&header.hash())?;
// Verify the kernel sums for the block_sums with the new tx applied, // Verify the kernel sums for the block_sums with the new tx applied,
// accounting for overage and offset. // accounting for overage and offset.
let (utxo_sum, kernel_sum) = let (utxo_sum, kernel_sum) =
(block_sums.clone(), &tx as &Committed).verify_kernel_sums(overage, offset)?; (block_sums, tx as &Committed).verify_kernel_sums(overage, offset)?;
Ok(BlockSums { Ok(BlockSums {
utxo_sum, utxo_sum,