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:
Antioch Peverell
2020-08-18 20:09:54 +01:00
committed by GitHub
parent 6a012d7e5b
commit 29cffe9b3c
7 changed files with 508 additions and 22 deletions
+128
View File
@@ -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
View File
@@ -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(())
}