Consolidate validation in Block and Transaction (#1354)

* Consolidate validation in Block and Transaction

Introduce TransactionBody which is included into block and tx.
Fixes #1333
This commit is contained in:
hashmap
2018-08-15 23:14:48 +02:00
committed by GitHub
parent 37fa413329
commit 99a66c1960
20 changed files with 570 additions and 476 deletions
+1 -1
View File
@@ -421,7 +421,7 @@ impl Chain {
let height = self.next_block_height()?;
let mut txhashset = self.txhashset.write().unwrap();
txhashset::extending_readonly(&mut txhashset, |extension| {
extension.verify_coinbase_maturity(&tx.inputs, height)?;
extension.verify_coinbase_maturity(&tx.inputs(), height)?;
Ok(())
})
}
+4 -4
View File
@@ -66,9 +66,9 @@ pub fn process_block(b: &Block, ctx: &mut BlockContext) -> Result<Option<Tip>, E
"pipe: process_block {} at {} with {} inputs, {} outputs, {} kernels",
b.hash(),
b.header.height,
b.inputs.len(),
b.outputs.len(),
b.kernels.len(),
b.inputs().len(),
b.outputs().len(),
b.kernels().len(),
);
check_known(b.hash(), ctx)?;
@@ -328,7 +328,7 @@ fn validate_block(b: &Block, ctx: &mut BlockContext) -> Result<(), Error> {
fn validate_block_via_txhashset(b: &Block, ext: &mut txhashset::Extension) -> Result<(), Error> {
// First check we are not attempting to spend any coinbase outputs
// before they have matured sufficiently.
ext.verify_coinbase_maturity(&b.inputs, b.header.height)?;
ext.verify_coinbase_maturity(&b.inputs(), b.header.height)?;
// apply the new block to the MMR trees and check the new root hashes
ext.apply_block(&b)?;
+1 -1
View File
@@ -157,7 +157,7 @@ impl ChainStore {
pub fn build_block_input_bitmap(&self, block: &Block) -> Result<Bitmap, Error> {
let bitmap = block
.inputs
.inputs()
.iter()
.filter_map(|x| self.get_output_pos(&x.commitment()).ok())
.map(|x| x as u32)
+7 -7
View File
@@ -474,27 +474,27 @@ impl<'a> Extension<'a> {
// Build bitmap of output pos spent (as inputs) by this tx for rewind.
let rewind_rm_pos = tx
.inputs
.inputs()
.iter()
.filter_map(|x| self.get_output_pos(&x.commitment()).ok())
.map(|x| x as u32)
.collect();
for ref output in &tx.outputs {
for ref output in tx.outputs() {
if let Err(e) = self.apply_output(output) {
self.rewind_raw_tx(output_pos, kernel_pos, &rewind_rm_pos)?;
return Err(e);
}
}
for ref input in &tx.inputs {
for ref input in tx.inputs() {
if let Err(e) = self.apply_input(input) {
self.rewind_raw_tx(output_pos, kernel_pos, &rewind_rm_pos)?;
return Err(e);
}
}
for ref kernel in &tx.kernels {
for ref kernel in tx.kernels() {
if let Err(e) = self.apply_kernel(kernel) {
self.rewind_raw_tx(output_pos, kernel_pos, &rewind_rm_pos)?;
return Err(e);
@@ -591,15 +591,15 @@ impl<'a> Extension<'a> {
// A block is not valid if it has not been fully cut-through.
// So we can safely apply outputs first (we will not spend these in the same
// block).
for out in &b.outputs {
for out in b.outputs() {
self.apply_output(out)?;
}
for input in &b.inputs {
for input in b.inputs() {
self.apply_input(input)?;
}
for kernel in &b.kernels {
for kernel in b.kernels() {
self.apply_kernel(kernel)?;
}
+11 -19
View File
@@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
extern crate chrono;
extern crate grin_chain as chain;
extern crate grin_core as core;
extern crate grin_keychain as keychain;
@@ -19,14 +20,13 @@ extern crate grin_store as store;
extern crate grin_util as util;
extern crate grin_wallet as wallet;
extern crate rand;
extern crate chrono;
use chrono::Duration;
use std::fs;
use std::sync::Arc;
use chrono::Duration;
use chain::Chain;
use chain::types::NoopAdapter;
use chain::Chain;
use core::core::hash::Hashed;
use core::core::target::Difficulty;
use core::core::{Block, BlockHeader, OutputFeatures, OutputIdentifier, Transaction};
@@ -74,12 +74,7 @@ fn mine_empty_chain() {
global::min_sizeshift()
};
b.header.pow.cuckoo_sizeshift = sizeshift;
pow::pow_size(
&mut b.header,
difficulty,
global::proofsize(),
sizeshift,
).unwrap();
pow::pow_size(&mut b.header, difficulty, global::proofsize(), sizeshift).unwrap();
b.header.pow.cuckoo_sizeshift = sizeshift;
let bhash = b.hash();
@@ -99,7 +94,7 @@ fn mine_empty_chain() {
let block = chain.get_block(&header.hash()).unwrap();
assert_eq!(block.header.height, n);
assert_eq!(block.hash(), bhash);
assert_eq!(block.outputs.len(), 1);
assert_eq!(block.outputs().len(), 1);
// now check the block height index
let header_by_height = chain.get_header_by_height(n).unwrap();
@@ -248,7 +243,7 @@ fn spend_in_fork_and_compact() {
// so we can spend the coinbase later
let b = prepare_block(&kc, &fork_head, &chain, 2);
let block_hash = b.hash();
let out_id = OutputIdentifier::from_output(&b.outputs[0]);
let out_id = OutputIdentifier::from_output(&b.outputs()[0]);
assert!(out_id.features.contains(OutputFeatures::COINBASE_OUTPUT));
fork_head = b.header.clone();
chain
@@ -269,10 +264,7 @@ fn spend_in_fork_and_compact() {
let tx1 = build::transaction(
vec![
build::coinbase_input(
consensus::REWARD,
kc.derive_key_id(2).unwrap(),
),
build::coinbase_input(consensus::REWARD, kc.derive_key_id(2).unwrap()),
build::output(consensus::REWARD - 20000, kc.derive_key_id(30).unwrap()),
build::with_fee(20000),
],
@@ -321,12 +313,12 @@ fn spend_in_fork_and_compact() {
assert_eq!(head.hash(), prev_main.hash());
assert!(
chain
.is_unspent(&OutputIdentifier::from_output(&tx2.outputs[0]))
.is_unspent(&OutputIdentifier::from_output(&tx2.outputs()[0]))
.is_ok()
);
assert!(
chain
.is_unspent(&OutputIdentifier::from_output(&tx1.outputs[0]))
.is_unspent(&OutputIdentifier::from_output(&tx1.outputs()[0]))
.is_err()
);
@@ -344,12 +336,12 @@ fn spend_in_fork_and_compact() {
assert_eq!(head.hash(), prev_fork.hash());
assert!(
chain
.is_unspent(&OutputIdentifier::from_output(&tx2.outputs[0]))
.is_unspent(&OutputIdentifier::from_output(&tx2.outputs()[0]))
.is_ok()
);
assert!(
chain
.is_unspent(&OutputIdentifier::from_output(&tx1.outputs[0]))
.is_unspent(&OutputIdentifier::from_output(&tx1.outputs()[0]))
.is_err()
);
+4 -4
View File
@@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
extern crate chrono;
extern crate env_logger;
extern crate grin_chain as chain;
extern crate grin_core as core;
@@ -19,11 +20,10 @@ extern crate grin_keychain as keychain;
extern crate grin_store as store;
extern crate grin_wallet as wallet;
extern crate rand;
extern crate chrono;
use chrono::Duration;
use std::fs;
use std::sync::Arc;
use chrono::Duration;
use chain::types::NoopAdapter;
use chain::{Error, ErrorKind};
@@ -78,8 +78,8 @@ fn test_coinbase_maturity() {
global::min_sizeshift(),
).unwrap();
assert_eq!(block.outputs.len(), 1);
let coinbase_output = block.outputs[0];
assert_eq!(block.outputs().len(), 1);
let coinbase_output = block.outputs()[0];
assert!(
coinbase_output
.features