[WIP] txpool (tx validation) using block sums for full validation (#1567)
tx validation (txpool) rework/simplify
This commit is contained in:
+5
-14
@@ -26,7 +26,7 @@ use consensus::{self, reward, REWARD};
|
||||
use core::committed::{self, Committed};
|
||||
use core::compact_block::{CompactBlock, CompactBlockBody};
|
||||
use core::hash::{Hash, HashWriter, Hashed, ZERO_HASH};
|
||||
use core::verifier_cache::{LruVerifierCache, VerifierCache};
|
||||
use core::verifier_cache::VerifierCache;
|
||||
use core::{
|
||||
transaction, Commitment, Input, KernelFeatures, Output, OutputFeatures, Transaction,
|
||||
TransactionBody, TxKernel,
|
||||
@@ -415,15 +415,8 @@ impl Block {
|
||||
difficulty: Difficulty,
|
||||
reward_output: (Output, TxKernel),
|
||||
) -> Result<Block, Error> {
|
||||
let verifier_cache = Arc::new(RwLock::new(LruVerifierCache::new()));
|
||||
let mut block = Block::with_reward(
|
||||
prev,
|
||||
txs,
|
||||
reward_output.0,
|
||||
reward_output.1,
|
||||
difficulty,
|
||||
verifier_cache,
|
||||
)?;
|
||||
let mut block =
|
||||
Block::with_reward(prev, txs, reward_output.0, reward_output.1, difficulty)?;
|
||||
|
||||
// Now set the pow on the header so block hashing works as expected.
|
||||
{
|
||||
@@ -497,12 +490,10 @@ impl Block {
|
||||
reward_out: Output,
|
||||
reward_kern: TxKernel,
|
||||
difficulty: Difficulty,
|
||||
verifier: Arc<RwLock<VerifierCache>>,
|
||||
) -> Result<Block, Error> {
|
||||
// A block is just a big transaction, aggregate as such.
|
||||
// Note that aggregation also runs transaction validation
|
||||
// and duplicate commitment checks.
|
||||
let mut agg_tx = transaction::aggregate(txs, verifier)?;
|
||||
let mut agg_tx = transaction::aggregate(txs)?;
|
||||
|
||||
// Now add the reward output and reward kernel to the aggregate tx.
|
||||
// At this point the tx is technically invalid,
|
||||
// but the tx body is valid if we account for the reward (i.e. as a block).
|
||||
|
||||
@@ -59,7 +59,6 @@ impl Default for BlockSums {
|
||||
}
|
||||
}
|
||||
|
||||
/// WAT?
|
||||
/// It's a tuple but we can verify the "full" kernel sums on it.
|
||||
/// This means we can take a previous block_sums, apply a new block to it
|
||||
/// and verify the full kernel sums (full UTXO and kernel sets).
|
||||
|
||||
@@ -404,7 +404,7 @@ impl TransactionBody {
|
||||
}
|
||||
|
||||
/// Total fee for a TransactionBody is the sum of fees of all kernels.
|
||||
pub fn fee(&self) -> u64 {
|
||||
fn fee(&self) -> u64 {
|
||||
self.kernels
|
||||
.iter()
|
||||
.fold(0, |acc, ref x| acc.saturating_add(x.fee))
|
||||
@@ -748,7 +748,8 @@ impl Transaction {
|
||||
self.body.fee()
|
||||
}
|
||||
|
||||
fn overage(&self) -> i64 {
|
||||
/// Total overage across all kernels.
|
||||
pub fn overage(&self) -> i64 {
|
||||
self.body.overage()
|
||||
}
|
||||
|
||||
@@ -822,10 +823,7 @@ pub fn cut_through(inputs: &mut Vec<Input>, outputs: &mut Vec<Output>) -> Result
|
||||
}
|
||||
|
||||
/// Aggregate a vec of txs into a multi-kernel tx with cut_through.
|
||||
pub fn aggregate(
|
||||
mut txs: Vec<Transaction>,
|
||||
verifier: Arc<RwLock<VerifierCache>>,
|
||||
) -> Result<Transaction, Error> {
|
||||
pub fn aggregate(mut txs: Vec<Transaction>) -> Result<Transaction, Error> {
|
||||
// convenience short-circuiting
|
||||
if txs.is_empty() {
|
||||
return Ok(Transaction::empty());
|
||||
@@ -867,22 +865,12 @@ pub fn aggregate(
|
||||
// * sum of all kernel offsets
|
||||
let tx = Transaction::new(inputs, outputs, kernels).with_offset(total_kernel_offset);
|
||||
|
||||
// Now validate the aggregate tx to ensure we have not built something invalid.
|
||||
// The resulting tx could be invalid for a variety of reasons -
|
||||
// * tx too large (too many inputs|outputs|kernels)
|
||||
// * cut-through may have invalidated the sums
|
||||
tx.validate(verifier)?;
|
||||
|
||||
Ok(tx)
|
||||
}
|
||||
|
||||
/// Attempt to deaggregate a multi-kernel transaction based on multiple
|
||||
/// transactions
|
||||
pub fn deaggregate(
|
||||
mk_tx: Transaction,
|
||||
txs: Vec<Transaction>,
|
||||
verifier: Arc<RwLock<VerifierCache>>,
|
||||
) -> Result<Transaction, Error> {
|
||||
pub fn deaggregate(mk_tx: Transaction, txs: Vec<Transaction>) -> Result<Transaction, Error> {
|
||||
let mut inputs: Vec<Input> = vec![];
|
||||
let mut outputs: Vec<Output> = vec![];
|
||||
let mut kernels: Vec<TxKernel> = vec![];
|
||||
@@ -891,7 +879,7 @@ pub fn deaggregate(
|
||||
// transaction
|
||||
let mut kernel_offsets = vec![];
|
||||
|
||||
let tx = aggregate(txs, verifier.clone())?;
|
||||
let tx = aggregate(txs)?;
|
||||
|
||||
for mk_input in mk_tx.body.inputs {
|
||||
if !tx.body.inputs.contains(&mk_input) && !inputs.contains(&mk_input) {
|
||||
@@ -941,9 +929,6 @@ pub fn deaggregate(
|
||||
|
||||
// Build a new tx from the above data.
|
||||
let tx = Transaction::new(inputs, outputs, kernels).with_offset(total_kernel_offset);
|
||||
|
||||
// Now validate the resulting tx to ensure we have not built something invalid.
|
||||
tx.validate(verifier)?;
|
||||
Ok(tx)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user