failing (so far) test for coinbase maturity check

This commit is contained in:
NikVolf 2016-11-10 20:12:03 +03:00
parent 390c7d2854
commit cb298ebe8e
2 changed files with 46 additions and 3 deletions

View File

@ -101,6 +101,12 @@ impl<F> BlockBuilder<F> where F: Invoke<chain::Block> {
BlockHeaderBuilder::with_callback(self) BlockHeaderBuilder::with_callback(self)
} }
pub fn merkled_header(self) -> BlockHeaderBuilder<Self> {
let hashes: Vec<H256> = self.transactions.iter().map(|t| t.hash()).collect();
let builder = self.header().merkle_root(chain::merkle_root(&hashes));
builder
}
pub fn transaction(self) -> TransactionBuilder<Self> { pub fn transaction(self) -> TransactionBuilder<Self> {
TransactionBuilder::with_callback(self) TransactionBuilder::with_callback(self)
} }

View File

@ -12,11 +12,17 @@ const COINBASE_MATURITY: u32 = 100; // 2 hours
pub struct ChainVerifier { pub struct ChainVerifier {
store: Arc<db::Store>, store: Arc<db::Store>,
skip_pow: bool,
} }
impl ChainVerifier { impl ChainVerifier {
pub fn new(store: Arc<db::Store>) -> Self { pub fn new(store: Arc<db::Store>) -> Self {
ChainVerifier { store: store } ChainVerifier { store: store, skip_pow: false, }
}
pub fn pow_skip(mut self) -> Self {
self.skip_pow = true;
self
} }
fn ordered_verify(&self, block: &chain::Block, at_height: u32) -> Result<(), Error> { fn ordered_verify(&self, block: &chain::Block, at_height: u32) -> Result<(), Error> {
@ -136,7 +142,7 @@ impl Verify for ChainVerifier {
} }
// target difficulty threshold // target difficulty threshold
if !utils::check_nbits(&hash, block.header().nbits) { if !self.skip_pow && !utils::check_nbits(&hash, block.header().nbits) {
return Err(Error::Pow); return Err(Error::Pow);
} }
@ -201,9 +207,11 @@ mod tests {
use super::ChainVerifier; use super::ChainVerifier;
use super::super::{Verify, Chain, Error, TransactionError}; use super::super::{Verify, Chain, Error, TransactionError};
use db::TestStorage; use db::{TestStorage, Storage, Store};
use test_data; use test_data;
use std::sync::Arc; use std::sync::Arc;
use devtools::RandomTempPath;
use chain::RepresentH256;
#[test] #[test]
fn verify_orphan() { fn verify_orphan() {
@ -251,4 +259,33 @@ mod tests {
)); ));
assert_eq!(should_be, verifier.verify(&b170)); assert_eq!(should_be, verifier.verify(&b170));
} }
#[test]
fn coinbase_maturity() {
let path = RandomTempPath::create_dir();
let storage = Storage::new(path.as_path()).unwrap();
let genesis = test_data::genesis();
storage.insert_block(&genesis).unwrap();
let genesis_coinbase = genesis.transactions()[0].hash();
let block = test_data::block_builder()
.transaction().coinbase().build()
.transaction()
.input().hash(genesis_coinbase.clone()).build()
.build()
.merkled_header().parent(genesis.hash()).build()
.build();
let verifier = ChainVerifier::new(Arc::new(storage)).pow_skip();
let expected = Err(Error::Transaction(
1,
TransactionError::Maturity,
));
assert_eq!(expected, verifier.verify(&block));
}
} }