openethereum/ethcore/src/blockchain/generator/generator.rs

179 lines
5.4 KiB
Rust
Raw Normal View History

2016-12-11 19:30:54 +01:00
// Copyright 2015, 2016 Parity Technologies (UK) Ltd.
2016-03-01 13:46:33 +01:00
// This file is part of Parity.
// Parity is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Parity is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
2016-08-03 18:05:17 +02:00
use util::{U256, H2048, Bytes};
2016-03-02 04:25:03 +01:00
use header::BlockNumber;
use transaction::SignedTransaction;
2016-03-02 04:25:03 +01:00
use super::fork::Fork;
use super::bloom::Bloom;
use super::complete::{BlockFinalizer, CompleteBlock, Complete};
use super::block::Block;
use super::transaction::Transaction;
2016-03-01 16:22:06 +01:00
2016-03-01 13:44:09 +01:00
/// Chain iterator interface.
2016-03-02 04:25:03 +01:00
pub trait ChainIterator: Iterator + Sized {
2016-03-01 13:44:09 +01:00
/// Should be called to create a fork of current iterator.
/// Blocks generated by fork will have lower difficulty than current chain.
2016-03-02 04:25:03 +01:00
fn fork(&self, fork_number: usize) -> Fork<Self> where Self: Clone;
2016-03-01 16:22:06 +01:00
/// Should be called to make every consecutive block have given bloom.
2016-03-07 14:40:39 +01:00
fn with_bloom(&mut self, bloom: H2048) -> Bloom<Self>;
/// Should be called to make every consecutive block have given transaction.
fn with_transaction(&mut self, transaction: SignedTransaction) -> Transaction<Self>;
2016-03-02 04:54:47 +01:00
/// Should be called to complete block. Without complete, block may have incorrect hash.
2016-03-02 04:25:03 +01:00
fn complete<'a>(&'a mut self, finalizer: &'a mut BlockFinalizer) -> Complete<'a, Self>;
/// Completes and generates block.
fn generate<'a>(&'a mut self, finalizer: &'a mut BlockFinalizer) -> Option<Bytes> where Self::Item: CompleteBlock;
2016-03-01 16:22:06 +01:00
}
2016-03-02 04:25:03 +01:00
impl<I> ChainIterator for I where I: Iterator + Sized {
fn fork(&self, fork_number: usize) -> Fork<Self> where I: Clone {
2016-03-01 16:22:06 +01:00
Fork {
iter: self.clone(),
fork_number: fork_number
}
}
2016-03-07 14:40:39 +01:00
fn with_bloom(&mut self, bloom: H2048) -> Bloom<Self> {
2016-03-01 16:22:06 +01:00
Bloom {
2016-03-02 04:25:03 +01:00
iter: self,
2016-03-01 16:22:06 +01:00
bloom: bloom
}
}
fn with_transaction(&mut self, transaction: SignedTransaction) -> Transaction<Self> {
Transaction {
iter: self,
transaction: transaction,
}
}
2016-03-02 04:25:03 +01:00
fn complete<'a>(&'a mut self, finalizer: &'a mut BlockFinalizer) -> Complete<'a, Self> {
Complete {
iter: self,
finalizer: finalizer
}
2016-03-01 16:22:06 +01:00
}
2016-03-02 04:25:03 +01:00
fn generate<'a>(&'a mut self, finalizer: &'a mut BlockFinalizer) -> Option<Bytes> where <I as Iterator>::Item: CompleteBlock {
self.complete(finalizer).next()
2016-03-01 16:22:06 +01:00
}
}
2016-03-01 13:44:09 +01:00
/// Blockchain generator.
2016-03-01 16:22:06 +01:00
#[derive(Clone)]
2016-03-01 13:44:09 +01:00
pub struct ChainGenerator {
/// Next block number.
number: BlockNumber,
/// Next block difficulty.
difficulty: U256,
}
impl ChainGenerator {
fn prepare_block(&self) -> Block {
let mut block = Block::default();
block.header.set_number(self.number);
block.header.set_difficulty(self.difficulty);
2016-03-01 13:44:09 +01:00
block
}
}
impl Default for ChainGenerator {
fn default() -> Self {
ChainGenerator {
number: 0,
difficulty: 1000.into(),
2016-03-01 13:44:09 +01:00
}
}
}
impl Iterator for ChainGenerator {
2016-03-01 16:22:06 +01:00
type Item = Block;
2016-03-01 13:44:09 +01:00
fn next(&mut self) -> Option<Self::Item> {
let block = self.prepare_block();
self.number += 1;
2016-03-01 16:22:06 +01:00
Some(block)
2016-03-01 13:44:09 +01:00
}
}
mod tests {
2016-03-02 04:25:03 +01:00
use util::hash::{H256, H2048};
2016-03-01 13:44:09 +01:00
use util::sha3::Hashable;
use views::BlockView;
2016-03-02 04:25:03 +01:00
use blockchain::generator::{ChainIterator, ChainGenerator, BlockFinalizer};
2016-03-01 13:44:09 +01:00
#[test]
fn canon_chain_generator() {
let mut canon_chain = ChainGenerator::default();
2016-03-02 04:25:03 +01:00
let mut finalizer = BlockFinalizer::default();
2016-03-01 13:44:09 +01:00
2016-03-02 04:25:03 +01:00
let genesis_rlp = canon_chain.generate(&mut finalizer).unwrap();
2016-03-01 13:44:09 +01:00
let genesis = BlockView::new(&genesis_rlp);
assert_eq!(genesis.header_view().parent_hash(), H256::default());
assert_eq!(genesis.header_view().number(), 0);
2016-03-02 04:25:03 +01:00
let b1_rlp = canon_chain.generate(&mut finalizer).unwrap();
2016-03-01 13:44:09 +01:00
let b1 = BlockView::new(&b1_rlp);
assert_eq!(b1.header_view().parent_hash(), genesis.header_view().sha3());
assert_eq!(b1.header_view().number(), 1);
2016-03-01 16:22:06 +01:00
let mut fork_chain = canon_chain.fork(1);
2016-03-01 13:44:09 +01:00
2016-03-02 04:25:03 +01:00
let b2_rlp_fork = fork_chain.generate(&mut finalizer.fork()).unwrap();
2016-03-01 13:44:09 +01:00
let b2_fork = BlockView::new(&b2_rlp_fork);
assert_eq!(b2_fork.header_view().parent_hash(), b1.header_view().sha3());
assert_eq!(b2_fork.header_view().number(), 2);
2016-03-02 04:25:03 +01:00
let b2_rlp = canon_chain.generate(&mut finalizer).unwrap();
2016-03-01 13:44:09 +01:00
let b2 = BlockView::new(&b2_rlp);
assert_eq!(b2.header_view().parent_hash(), b1.header_view().sha3());
assert_eq!(b2.header_view().number(), 2);
assert!(b2.header_view().difficulty() > b2_fork.header_view().difficulty());
}
2016-03-02 04:25:03 +01:00
#[test]
fn with_bloom_generator() {
let bloom = H2048([0x1; 256]);
let mut gen = ChainGenerator::default();
let mut finalizer = BlockFinalizer::default();
let block0_rlp = gen.with_bloom(bloom).generate(&mut finalizer).unwrap();
let block1_rlp = gen.generate(&mut finalizer).unwrap();
let block0 = BlockView::new(&block0_rlp);
let block1 = BlockView::new(&block1_rlp);
assert_eq!(block0.header_view().number(), 0);
assert_eq!(block0.header_view().parent_hash(), H256::default());
assert_eq!(block1.header_view().number(), 1);
assert_eq!(block1.header_view().parent_hash(), block0.header_view().sha3());
}
#[test]
fn generate_1000_blocks() {
let generator = ChainGenerator::default();
let mut finalizer = BlockFinalizer::default();
let blocks: Vec<_> = generator.take(1000).complete(&mut finalizer).collect();
assert_eq!(blocks.len(), 1000);
}
2016-03-01 13:44:09 +01:00
}
2016-03-02 04:25:03 +01:00