iteration test layout

This commit is contained in:
NikVolf 2016-10-21 21:26:24 +03:00
parent 2a98795485
commit 44213d1170
3 changed files with 55 additions and 3 deletions

View File

@ -28,3 +28,8 @@ pub fn block_h2() -> Block {
let block: Block = "010000004860eb18bf1b1620e37e9490fc8a427514416fd75159ab86688e9a8300000000d5fdcc541e25de1c7a5addedf24858b8bb665c9f36ef744ee42c316022c90f9bb0bc6649ffff001d08d2bd610101000000010000000000000000000000000000000000000000000000000000000000000000ffffffff0704ffff001d010bffffffff0100f2052a010000004341047211a824f55b505228e4c3d5194c1fcfaa15a456abdf37f9b9d97a4040afc073dee6c89064984f03385237d92167c13e236446b417ab79a0fcae412ae3316b77ac00000000".into();
block
}
pub fn genesis() -> Block {
let block: Block = "0100000000000000000000000000000000000000000000000000000000000000000000003ba3edfd7a7b12b27ac72c3e67768f617fc81bc3888a51323a9fb8aa4b1e5e4a29ab5f49ffff001d1dac2b7c0101000000010000000000000000000000000000000000000000000000000000000000000000ffffffff4d04ffff001d0104455468652054696d65732030332f4a616e2f32303039204368616e63656c6c6f72206f6e206272696e6b206f66207365636f6e64206261696c6f757420666f722062616e6b73ffffffff0100f2052a01000000434104678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5fac00000000".into();
block
}

View File

@ -2,16 +2,27 @@
use std::sync::Arc;
use db;
use db::{self, BlockRef};
use chain;
use super::{Verify, VerificationResult, Chain};
struct ChainVerifier {
pub struct ChainVerifier {
store: Arc<db::Store>,
}
impl ChainVerifier {
pub fn new(store: Arc<db::Store>) -> Self {
ChainVerifier { store: store }
}
}
impl Verify for ChainVerifier {
fn verify(&self, block: &chain::Block) -> VerificationResult {
let parent = match self.store.block(BlockRef::Hash(block.header().previous_header_hash.clone())) {
Some(b) => b,
None => { return Ok(Chain::Orphan); }
};
Ok(Chain::Main)
}
}
@ -19,13 +30,18 @@ impl Verify for ChainVerifier {
#[cfg(test)]
mod tests {
use super::ChainVerifier;
use super::super::{Verify, Chain};
use primitives::hash::H256;
use chain;
use std::collections::HashMap;
use db::{self, BlockRef, Store};
use serialization;
use serialization::bytes::Bytes;
use test_data;
use std::sync::Arc;
#[derive(Default)]
struct TestStorage {
blocks: HashMap<H256, chain::Block>,
heights: HashMap<usize, H256>,
@ -38,6 +54,17 @@ mod tests {
BlockRef::Hash(h) => Some(h),
}
}
fn with_blocks(blocks: &[chain::Block]) -> Self {
let mut storage = TestStorage::default();
let mut height = 0;
for (idx, block) in blocks.iter().enumerate() {
let hash = block.hash();
storage.blocks.insert(hash.clone(), block.clone());
storage.heights.insert(height, hash);
}
storage
}
}
impl Store for TestStorage {
@ -69,7 +96,9 @@ mod tests {
}
fn block_transactions(&self, block_ref: BlockRef) -> Vec<chain::Transaction> {
self.blocks.iter().flat_map(|(_, b)| b.transactions()).cloned().collect()
self.block(block_ref)
.map(|b| b.transactions().iter().cloned().collect())
.unwrap_or(Vec::new())
}
fn block(&self, block_ref: BlockRef) -> Option<chain::Block> {
@ -83,5 +112,22 @@ mod tests {
}
}
#[test]
fn verify_orphan() {
let storage = TestStorage::with_blocks(&vec![test_data::genesis()]);
let b2 = test_data::block_h2();
let verifier = ChainVerifier::new(Arc::new(storage));
assert_eq!(Chain::Orphan, verifier.verify(&b2).unwrap());
}
#[test]
fn verify_smoky() {
let storage = TestStorage::with_blocks(&vec![test_data::genesis()]);
let b1 = test_data::block_h1();
let verifier = ChainVerifier::new(Arc::new(storage));
assert_eq!(Chain::Main, verifier.verify(&b1).unwrap());
}
}

View File

@ -47,6 +47,7 @@ pub enum TransactionError {
Signature,
}
#[derive(PartialEq, Debug)]
/// Block verification chain
pub enum Chain {
/// Main chain