verify_cut_through and test coverage (#3424)
* fix inconsistent verify_cut_through() logic * add test coverage for chain::process_block() and cut_through logic * fix comment
This commit is contained in:
@@ -810,6 +810,12 @@ impl TransactionBody {
|
||||
self
|
||||
}
|
||||
|
||||
/// Fully replace inputs.
|
||||
pub fn replace_outputs(mut self, outputs: &[Output]) -> TransactionBody {
|
||||
self.outputs = outputs.to_vec();
|
||||
self
|
||||
}
|
||||
|
||||
/// Builds a new TransactionBody with the provided output added. Existing
|
||||
/// outputs, if any, are kept intact.
|
||||
/// Sort order is maintained.
|
||||
@@ -968,23 +974,24 @@ impl TransactionBody {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// Returns a single sorted vec of all input and output commitments.
|
||||
// This gives us a convenient way of verifying cut_through.
|
||||
fn inputs_outputs_committed(&self) -> Vec<Commitment> {
|
||||
let mut commits = self.inputs_committed();
|
||||
commits.extend_from_slice(self.outputs_committed().as_slice());
|
||||
commits.sort_unstable();
|
||||
commits
|
||||
}
|
||||
|
||||
// Verify that no input is spending an output from the same block.
|
||||
// Assumes inputs and outputs are sorted
|
||||
// The inputs and outputs are not guaranteed to be sorted consistently once we support "commit only" inputs.
|
||||
// We need to allocate as we need to sort the commitments so we keep this very simple and just look
|
||||
// for duplicates across all input and output commitments.
|
||||
fn verify_cut_through(&self) -> Result<(), Error> {
|
||||
let inputs: Vec<_> = self.inputs().into();
|
||||
let mut inputs = inputs.iter().map(|x| x.hash()).peekable();
|
||||
let mut outputs = self.outputs.iter().map(|x| x.hash()).peekable();
|
||||
while let (Some(ih), Some(oh)) = (inputs.peek(), outputs.peek()) {
|
||||
match ih.cmp(oh) {
|
||||
Ordering::Less => {
|
||||
inputs.next();
|
||||
}
|
||||
Ordering::Greater => {
|
||||
outputs.next();
|
||||
}
|
||||
Ordering::Equal => {
|
||||
return Err(Error::CutThrough);
|
||||
}
|
||||
let commits = self.inputs_outputs_committed();
|
||||
for pair in commits.windows(2) {
|
||||
if pair[0] == pair[1] {
|
||||
return Err(Error::CutThrough);
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
@@ -1023,6 +1030,7 @@ impl TransactionBody {
|
||||
self.verify_weight(weighting)?;
|
||||
self.verify_no_nrd_duplicates()?;
|
||||
self.verify_sorted()?;
|
||||
self.verify_cut_through()?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -1035,7 +1043,6 @@ impl TransactionBody {
|
||||
verifier: Arc<RwLock<dyn VerifierCache>>,
|
||||
) -> Result<(), Error> {
|
||||
self.validate_read(weighting)?;
|
||||
self.verify_cut_through()?;
|
||||
|
||||
// Find all the outputs that have not had their rangeproofs verified.
|
||||
let outputs = {
|
||||
|
||||
@@ -784,3 +784,131 @@ fn validate_header_proof() {
|
||||
)
|
||||
.is_err());
|
||||
}
|
||||
|
||||
// Test coverage for verifying cut-through during block validation.
|
||||
// It is not valid for a block to spend an output and produce a new output with the same commitment.
|
||||
// This test covers the case where a plain output is spent, producing a plain output with the same commitment.
|
||||
#[test]
|
||||
fn test_verify_cut_through_plain() -> Result<(), Error> {
|
||||
global::set_local_chain_type(global::ChainTypes::UserTesting);
|
||||
|
||||
let keychain = ExtKeychain::from_random_seed(false).unwrap();
|
||||
|
||||
let key_id1 = ExtKeychain::derive_key_id(1, 1, 0, 0, 0);
|
||||
let key_id2 = ExtKeychain::derive_key_id(1, 2, 0, 0, 0);
|
||||
let key_id3 = ExtKeychain::derive_key_id(1, 3, 0, 0, 0);
|
||||
|
||||
let builder = ProofBuilder::new(&keychain);
|
||||
|
||||
let tx = build::transaction(
|
||||
KernelFeatures::Plain { fee: 0 },
|
||||
&[
|
||||
build::input(10, key_id1.clone()),
|
||||
build::input(10, key_id2.clone()),
|
||||
build::output(10, key_id1.clone()),
|
||||
build::output(6, key_id2.clone()),
|
||||
build::output(4, key_id3.clone()),
|
||||
],
|
||||
&keychain,
|
||||
&builder,
|
||||
)
|
||||
.expect("valid tx");
|
||||
|
||||
let prev = BlockHeader::default();
|
||||
let key_id = ExtKeychain::derive_key_id(0, 0, 0, 0, 0);
|
||||
let mut block = new_block(&[tx], &keychain, &builder, &prev, &key_id);
|
||||
|
||||
// The block should fail validation due to cut-through.
|
||||
assert_eq!(
|
||||
block.validate(&BlindingFactor::zero(), verifier_cache()),
|
||||
Err(Error::Transaction(transaction::Error::CutThrough))
|
||||
);
|
||||
|
||||
// The block should fail lightweight "read" validation due to cut-through.
|
||||
assert_eq!(
|
||||
block.validate_read(),
|
||||
Err(Error::Transaction(transaction::Error::CutThrough))
|
||||
);
|
||||
|
||||
// Apply cut-through to eliminate the offending input and output.
|
||||
let mut inputs: Vec<_> = block.inputs().into();
|
||||
let mut outputs = block.outputs().to_vec();
|
||||
let (inputs, outputs, _, _) = transaction::cut_through(&mut inputs[..], &mut outputs[..])?;
|
||||
|
||||
block.body = block
|
||||
.body
|
||||
.replace_inputs(inputs.into())
|
||||
.replace_outputs(outputs);
|
||||
|
||||
// Block validates successfully after applying cut-through.
|
||||
block.validate(&BlindingFactor::zero(), verifier_cache())?;
|
||||
|
||||
// Block validates via lightweight "read" validation.
|
||||
block.validate_read()?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// Test coverage for verifying cut-through during block validation.
|
||||
// It is not valid for a block to spend an output and produce a new output with the same commitment.
|
||||
// This test covers the case where a coinbase output is spent, producing a plain output with the same commitment.
|
||||
#[test]
|
||||
fn test_verify_cut_through_coinbase() -> Result<(), Error> {
|
||||
global::set_local_chain_type(global::ChainTypes::UserTesting);
|
||||
|
||||
let keychain = ExtKeychain::from_random_seed(false).unwrap();
|
||||
|
||||
let key_id1 = ExtKeychain::derive_key_id(1, 1, 0, 0, 0);
|
||||
let key_id2 = ExtKeychain::derive_key_id(1, 2, 0, 0, 0);
|
||||
let key_id3 = ExtKeychain::derive_key_id(1, 3, 0, 0, 0);
|
||||
|
||||
let builder = ProofBuilder::new(&keychain);
|
||||
|
||||
let tx = build::transaction(
|
||||
KernelFeatures::Plain { fee: 0 },
|
||||
&[
|
||||
build::coinbase_input(consensus::REWARD, key_id1.clone()),
|
||||
build::coinbase_input(consensus::REWARD, key_id2.clone()),
|
||||
build::output(60_000_000_000, key_id1.clone()),
|
||||
build::output(50_000_000_000, key_id2.clone()),
|
||||
build::output(10_000_000_000, key_id3.clone()),
|
||||
],
|
||||
&keychain,
|
||||
&builder,
|
||||
)
|
||||
.expect("valid tx");
|
||||
|
||||
let prev = BlockHeader::default();
|
||||
let key_id = ExtKeychain::derive_key_id(0, 0, 0, 0, 0);
|
||||
let mut block = new_block(&[tx], &keychain, &builder, &prev, &key_id);
|
||||
|
||||
// The block should fail validation due to cut-through.
|
||||
assert_eq!(
|
||||
block.validate(&BlindingFactor::zero(), verifier_cache()),
|
||||
Err(Error::Transaction(transaction::Error::CutThrough))
|
||||
);
|
||||
|
||||
// The block should fail lightweight "read" validation due to cut-through.
|
||||
assert_eq!(
|
||||
block.validate_read(),
|
||||
Err(Error::Transaction(transaction::Error::CutThrough))
|
||||
);
|
||||
|
||||
// Apply cut-through to eliminate the offending input and output.
|
||||
let mut inputs: Vec<_> = block.inputs().into();
|
||||
let mut outputs = block.outputs().to_vec();
|
||||
let (inputs, outputs, _, _) = transaction::cut_through(&mut inputs[..], &mut outputs[..])?;
|
||||
|
||||
block.body = block
|
||||
.body
|
||||
.replace_inputs(inputs.into())
|
||||
.replace_outputs(outputs);
|
||||
|
||||
// Block validates successfully after applying cut-through.
|
||||
block.validate(&BlindingFactor::zero(), verifier_cache())?;
|
||||
|
||||
// Block validates via lightweight "read" validation.
|
||||
block.validate_read()?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
+128
-4
@@ -16,11 +16,17 @@
|
||||
|
||||
pub mod common;
|
||||
|
||||
use self::core::core::{Output, OutputFeatures};
|
||||
use self::core::libtx::proof;
|
||||
use self::core::ser;
|
||||
use crate::core::core::transaction::{self, Error};
|
||||
use crate::core::core::verifier_cache::LruVerifierCache;
|
||||
use crate::core::core::{KernelFeatures, Output, OutputFeatures, Weighting};
|
||||
use crate::core::global;
|
||||
use crate::core::libtx::build;
|
||||
use crate::core::libtx::proof::{self, ProofBuilder};
|
||||
use crate::core::{consensus, ser};
|
||||
use grin_core as core;
|
||||
use keychain::{ExtKeychain, Keychain};
|
||||
use std::sync::Arc;
|
||||
use util::RwLock;
|
||||
|
||||
#[test]
|
||||
fn test_output_ser_deser() {
|
||||
@@ -28,7 +34,7 @@ fn test_output_ser_deser() {
|
||||
let key_id = ExtKeychain::derive_key_id(1, 1, 0, 0, 0);
|
||||
let switch = keychain::SwitchCommitmentType::Regular;
|
||||
let commit = keychain.commit(5, &key_id, switch).unwrap();
|
||||
let builder = proof::ProofBuilder::new(&keychain);
|
||||
let builder = ProofBuilder::new(&keychain);
|
||||
let proof = proof::create(&keychain, &builder, 5, &key_id, switch, commit, None).unwrap();
|
||||
|
||||
let out = Output {
|
||||
@@ -45,3 +51,121 @@ fn test_output_ser_deser() {
|
||||
assert_eq!(dout.commit, out.commit);
|
||||
assert_eq!(dout.proof, out.proof);
|
||||
}
|
||||
|
||||
// Test coverage for verifying cut-through during transaction validation.
|
||||
// It is not valid for a transaction to spend an output and produce a new output with the same commitment.
|
||||
// This test covers the case where a plain output is spent, producing a plain output with the same commitment.
|
||||
#[test]
|
||||
fn test_verify_cut_through_plain() -> Result<(), Error> {
|
||||
global::set_local_chain_type(global::ChainTypes::UserTesting);
|
||||
|
||||
let keychain = ExtKeychain::from_random_seed(false)?;
|
||||
|
||||
let key_id1 = ExtKeychain::derive_key_id(1, 1, 0, 0, 0);
|
||||
let key_id2 = ExtKeychain::derive_key_id(1, 2, 0, 0, 0);
|
||||
let key_id3 = ExtKeychain::derive_key_id(1, 3, 0, 0, 0);
|
||||
|
||||
let builder = proof::ProofBuilder::new(&keychain);
|
||||
|
||||
let mut tx = build::transaction(
|
||||
KernelFeatures::Plain { fee: 0 },
|
||||
&[
|
||||
build::input(10, key_id1.clone()),
|
||||
build::input(10, key_id2.clone()),
|
||||
build::output(10, key_id1.clone()),
|
||||
build::output(6, key_id2.clone()),
|
||||
build::output(4, key_id3.clone()),
|
||||
],
|
||||
&keychain,
|
||||
&builder,
|
||||
)
|
||||
.expect("valid tx");
|
||||
|
||||
let verifier_cache = Arc::new(RwLock::new(LruVerifierCache::new()));
|
||||
|
||||
// Transaction should fail validation due to cut-through.
|
||||
assert_eq!(
|
||||
tx.validate(Weighting::AsTransaction, verifier_cache.clone()),
|
||||
Err(Error::CutThrough),
|
||||
);
|
||||
|
||||
// Transaction should fail lightweight "read" validation due to cut-through.
|
||||
assert_eq!(tx.validate_read(), Err(Error::CutThrough));
|
||||
|
||||
// Apply cut-through to eliminate the offending input and output.
|
||||
let mut inputs: Vec<_> = tx.inputs().into();
|
||||
let mut outputs = tx.outputs().to_vec();
|
||||
let (inputs, outputs, _, _) = transaction::cut_through(&mut inputs[..], &mut outputs[..])?;
|
||||
|
||||
tx.body = tx
|
||||
.body
|
||||
.replace_inputs(inputs.into())
|
||||
.replace_outputs(outputs);
|
||||
|
||||
// Transaction validates successfully after applying cut-through.
|
||||
tx.validate(Weighting::AsTransaction, verifier_cache.clone())?;
|
||||
|
||||
// Transaction validates via lightweight "read" validation as well.
|
||||
tx.validate_read()?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// Test coverage for verifying cut-through during transaction validation.
|
||||
// It is not valid for a transaction to spend an output and produce a new output with the same commitment.
|
||||
// This test covers the case where a coinbase output is spent, producing a plain output with the same commitment.
|
||||
#[test]
|
||||
fn test_verify_cut_through_coinbase() -> Result<(), Error> {
|
||||
global::set_local_chain_type(global::ChainTypes::UserTesting);
|
||||
|
||||
let keychain = ExtKeychain::from_random_seed(false)?;
|
||||
|
||||
let key_id1 = ExtKeychain::derive_key_id(1, 1, 0, 0, 0);
|
||||
let key_id2 = ExtKeychain::derive_key_id(1, 2, 0, 0, 0);
|
||||
let key_id3 = ExtKeychain::derive_key_id(1, 3, 0, 0, 0);
|
||||
|
||||
let builder = ProofBuilder::new(&keychain);
|
||||
|
||||
let mut tx = build::transaction(
|
||||
KernelFeatures::Plain { fee: 0 },
|
||||
&[
|
||||
build::coinbase_input(consensus::REWARD, key_id1.clone()),
|
||||
build::coinbase_input(consensus::REWARD, key_id2.clone()),
|
||||
build::output(60_000_000_000, key_id1.clone()),
|
||||
build::output(50_000_000_000, key_id2.clone()),
|
||||
build::output(10_000_000_000, key_id3.clone()),
|
||||
],
|
||||
&keychain,
|
||||
&builder,
|
||||
)
|
||||
.expect("valid tx");
|
||||
|
||||
let verifier_cache = Arc::new(RwLock::new(LruVerifierCache::new()));
|
||||
|
||||
// Transaction should fail validation due to cut-through.
|
||||
assert_eq!(
|
||||
tx.validate(Weighting::AsTransaction, verifier_cache.clone()),
|
||||
Err(Error::CutThrough),
|
||||
);
|
||||
|
||||
// Transaction should fail lightweight "read" validation due to cut-through.
|
||||
assert_eq!(tx.validate_read(), Err(Error::CutThrough));
|
||||
|
||||
// Apply cut-through to eliminate the offending input and output.
|
||||
let mut inputs: Vec<_> = tx.inputs().into();
|
||||
let mut outputs = tx.outputs().to_vec();
|
||||
let (inputs, outputs, _, _) = transaction::cut_through(&mut inputs[..], &mut outputs[..])?;
|
||||
|
||||
tx.body = tx
|
||||
.body
|
||||
.replace_inputs(inputs.into())
|
||||
.replace_outputs(outputs);
|
||||
|
||||
// Transaction validates successfully after applying cut-through.
|
||||
tx.validate(Weighting::AsTransaction, verifier_cache.clone())?;
|
||||
|
||||
// Transaction validates via lightweight "read" validation as well.
|
||||
tx.validate_read()?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user