2019-01-07 11:33:07 +01:00
|
|
|
// Copyright 2015-2019 Parity Technologies (UK) Ltd.
|
|
|
|
// This file is part of Parity Ethereum.
|
2018-02-16 10:11:29 +01:00
|
|
|
|
2019-01-07 11:33:07 +01:00
|
|
|
// Parity Ethereum is free software: you can redistribute it and/or modify
|
2018-02-16 10:11:29 +01:00
|
|
|
// 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.
|
|
|
|
|
2019-01-07 11:33:07 +01:00
|
|
|
// Parity Ethereum is distributed in the hope that it will be useful,
|
2018-02-16 10:11:29 +01:00
|
|
|
// 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
|
2019-01-07 11:33:07 +01:00
|
|
|
// along with Parity Ethereum. If not, see <http://www.gnu.org/licenses/>.
|
2018-02-16 10:11:29 +01:00
|
|
|
|
|
|
|
//! Blockchain generator for tests.
|
|
|
|
|
|
|
|
use std::collections::VecDeque;
|
|
|
|
use ethereum_types::{U256, H256, Bloom};
|
|
|
|
|
2019-01-04 14:05:46 +01:00
|
|
|
use common_types::encoded;
|
|
|
|
use common_types::header::Header;
|
2019-07-08 19:21:06 +02:00
|
|
|
use common_types::transaction::{SignedTransaction, Transaction, Action};
|
2019-01-04 14:05:46 +01:00
|
|
|
use common_types::view;
|
|
|
|
use common_types::views::BlockView;
|
2019-07-08 19:21:06 +02:00
|
|
|
use keccak_hash::keccak;
|
2018-02-16 10:11:29 +01:00
|
|
|
use rlp::encode;
|
2019-01-04 14:05:46 +01:00
|
|
|
use rlp_derive::RlpEncodable;
|
2019-07-08 19:21:06 +02:00
|
|
|
use triehash_ethereum::ordered_trie_root;
|
2018-02-16 10:11:29 +01:00
|
|
|
|
|
|
|
/// Helper structure, used for encoding blocks.
|
|
|
|
#[derive(Default, Clone, RlpEncodable)]
|
|
|
|
pub struct Block {
|
2019-01-04 14:05:46 +01:00
|
|
|
/// Block header
|
2018-02-16 10:11:29 +01:00
|
|
|
pub header: Header,
|
2019-01-04 14:05:46 +01:00
|
|
|
/// Block transactions
|
2018-02-16 10:11:29 +01:00
|
|
|
pub transactions: Vec<SignedTransaction>,
|
2019-01-04 14:05:46 +01:00
|
|
|
/// Block uncles
|
2018-02-16 10:11:29 +01:00
|
|
|
pub uncles: Vec<Header>
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Block {
|
2019-01-04 14:05:46 +01:00
|
|
|
/// Get a copy of the header
|
2018-02-16 10:11:29 +01:00
|
|
|
#[inline]
|
|
|
|
pub fn header(&self) -> Header {
|
|
|
|
self.header.clone()
|
|
|
|
}
|
|
|
|
|
2019-01-04 14:05:46 +01:00
|
|
|
/// Get block hash
|
2018-02-16 10:11:29 +01:00
|
|
|
#[inline]
|
|
|
|
pub fn hash(&self) -> H256 {
|
2018-07-30 11:45:10 +02:00
|
|
|
view!(BlockView, &self.encoded().raw()).header_view().hash()
|
2018-02-16 10:11:29 +01:00
|
|
|
}
|
|
|
|
|
2019-01-04 14:05:46 +01:00
|
|
|
/// Get block number
|
2018-02-16 10:11:29 +01:00
|
|
|
#[inline]
|
|
|
|
pub fn number(&self) -> u64 {
|
|
|
|
self.header.number()
|
|
|
|
}
|
|
|
|
|
2019-01-04 14:05:46 +01:00
|
|
|
/// Get RLP encoding of this block
|
2018-02-16 10:11:29 +01:00
|
|
|
#[inline]
|
2018-07-30 11:45:10 +02:00
|
|
|
pub fn encoded(&self) -> encoded::Block {
|
2018-10-09 22:07:25 +02:00
|
|
|
encoded::Block::new(encode(self))
|
2018-02-16 10:11:29 +01:00
|
|
|
}
|
2018-02-22 11:22:56 +01:00
|
|
|
|
2019-01-04 14:05:46 +01:00
|
|
|
/// Get block difficulty
|
2018-02-22 11:22:56 +01:00
|
|
|
#[inline]
|
|
|
|
pub fn difficulty(&self) -> U256 {
|
|
|
|
*self.header.difficulty()
|
|
|
|
}
|
2018-02-16 10:11:29 +01:00
|
|
|
}
|
|
|
|
|
2019-01-04 14:05:46 +01:00
|
|
|
/// Specify block options for generator
|
2018-02-16 10:11:29 +01:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct BlockOptions {
|
2019-01-04 14:05:46 +01:00
|
|
|
/// Difficulty
|
2018-02-16 10:11:29 +01:00
|
|
|
pub difficulty: U256,
|
2019-01-04 14:05:46 +01:00
|
|
|
/// Set bloom filter
|
2018-02-16 10:11:29 +01:00
|
|
|
pub bloom: Bloom,
|
2019-01-04 14:05:46 +01:00
|
|
|
/// Transactions included in blocks
|
2018-02-16 10:11:29 +01:00
|
|
|
pub transactions: Vec<SignedTransaction>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for BlockOptions {
|
|
|
|
fn default() -> Self {
|
|
|
|
BlockOptions {
|
|
|
|
difficulty: 10.into(),
|
|
|
|
bloom: Bloom::default(),
|
|
|
|
transactions: Vec::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-04 14:05:46 +01:00
|
|
|
/// Utility to create blocks
|
2018-02-16 10:11:29 +01:00
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct BlockBuilder {
|
|
|
|
blocks: VecDeque<Block>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl BlockBuilder {
|
2019-01-04 14:05:46 +01:00
|
|
|
/// Create new BlockBuilder starting at genesis.
|
2018-02-16 10:11:29 +01:00
|
|
|
pub fn genesis() -> Self {
|
|
|
|
let mut blocks = VecDeque::with_capacity(1);
|
|
|
|
blocks.push_back(Block::default());
|
|
|
|
|
|
|
|
BlockBuilder {
|
|
|
|
blocks,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-04 14:05:46 +01:00
|
|
|
/// Add new block with default options.
|
2018-02-16 10:11:29 +01:00
|
|
|
#[inline]
|
|
|
|
pub fn add_block(&self) -> Self {
|
|
|
|
self.add_block_with(|| BlockOptions::default())
|
|
|
|
}
|
|
|
|
|
2019-01-04 14:05:46 +01:00
|
|
|
/// Add `count` number of blocks with default options.
|
2018-02-16 10:11:29 +01:00
|
|
|
#[inline]
|
|
|
|
pub fn add_blocks(&self, count: usize) -> Self {
|
|
|
|
self.add_blocks_with(count, || BlockOptions::default())
|
|
|
|
}
|
|
|
|
|
2019-01-04 14:05:46 +01:00
|
|
|
/// Add block with specified options.
|
2018-02-16 10:11:29 +01:00
|
|
|
#[inline]
|
|
|
|
pub fn add_block_with<T>(&self, get_metadata: T) -> Self where T: Fn() -> BlockOptions {
|
|
|
|
self.add_blocks_with(1, get_metadata)
|
|
|
|
}
|
|
|
|
|
2019-01-04 14:05:46 +01:00
|
|
|
/// Add a block with given difficulty
|
2018-02-16 10:11:29 +01:00
|
|
|
#[inline]
|
|
|
|
pub fn add_block_with_difficulty<T>(&self, difficulty: T) -> Self where T: Into<U256> {
|
|
|
|
let difficulty = difficulty.into();
|
|
|
|
self.add_blocks_with(1, move || BlockOptions {
|
|
|
|
difficulty,
|
|
|
|
..Default::default()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-07-08 19:21:06 +02:00
|
|
|
/// Add a block with randomly generated transactions.
|
|
|
|
#[inline]
|
|
|
|
pub fn add_block_with_random_transactions(&self) -> Self {
|
|
|
|
// Maximum of ~50 transactions
|
|
|
|
let count = rand::random::<u8>() as usize / 5;
|
|
|
|
let transactions = std::iter::repeat_with(|| {
|
|
|
|
let data_len = rand::random::<u8>();
|
|
|
|
let data = std::iter::repeat_with(|| rand::random::<u8>())
|
|
|
|
.take(data_len as usize)
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
Transaction {
|
|
|
|
nonce: 0.into(),
|
|
|
|
gas_price: 0.into(),
|
|
|
|
gas: 100_000.into(),
|
|
|
|
action: Action::Create,
|
|
|
|
value: 100.into(),
|
|
|
|
data,
|
|
|
|
}.sign(&keccak("").into(), None)
|
|
|
|
}).take(count);
|
|
|
|
|
|
|
|
self.add_block_with_transactions(transactions)
|
|
|
|
}
|
|
|
|
|
2019-01-04 14:05:46 +01:00
|
|
|
/// Add a block with given transactions.
|
2018-02-16 10:11:29 +01:00
|
|
|
#[inline]
|
|
|
|
pub fn add_block_with_transactions<T>(&self, transactions: T) -> Self
|
|
|
|
where T: IntoIterator<Item = SignedTransaction> {
|
|
|
|
let transactions = transactions.into_iter().collect::<Vec<_>>();
|
|
|
|
self.add_blocks_with(1, || BlockOptions {
|
|
|
|
transactions: transactions.clone(),
|
|
|
|
..Default::default()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-01-04 14:05:46 +01:00
|
|
|
/// Add a block with given bloom filter.
|
2018-02-16 10:11:29 +01:00
|
|
|
#[inline]
|
|
|
|
pub fn add_block_with_bloom(&self, bloom: Bloom) -> Self {
|
|
|
|
self.add_blocks_with(1, move || BlockOptions {
|
|
|
|
bloom,
|
|
|
|
..Default::default()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-01-04 14:05:46 +01:00
|
|
|
/// Add a bunch of blocks with given metadata.
|
2018-02-16 10:11:29 +01:00
|
|
|
pub fn add_blocks_with<T>(&self, count: usize, get_metadata: T) -> Self where T: Fn() -> BlockOptions {
|
|
|
|
assert!(count > 0, "There must be at least 1 block");
|
|
|
|
let mut parent_hash = self.last().hash();
|
|
|
|
let mut parent_number = self.last().number();
|
|
|
|
let mut blocks = VecDeque::with_capacity(count);
|
|
|
|
for _ in 0..count {
|
|
|
|
let mut block = Block::default();
|
|
|
|
let metadata = get_metadata();
|
|
|
|
let block_number = parent_number + 1;
|
2019-07-08 19:21:06 +02:00
|
|
|
let transactions = metadata.transactions;
|
|
|
|
let transactions_root = ordered_trie_root(transactions.iter().map(rlp::encode));
|
|
|
|
|
2018-02-16 10:11:29 +01:00
|
|
|
block.header.set_parent_hash(parent_hash);
|
|
|
|
block.header.set_number(block_number);
|
|
|
|
block.header.set_log_bloom(metadata.bloom);
|
|
|
|
block.header.set_difficulty(metadata.difficulty);
|
2019-07-08 19:21:06 +02:00
|
|
|
block.header.set_transactions_root(transactions_root);
|
|
|
|
block.transactions = transactions;
|
2018-02-16 10:11:29 +01:00
|
|
|
|
|
|
|
parent_hash = block.hash();
|
|
|
|
parent_number = block_number;
|
|
|
|
|
|
|
|
blocks.push_back(block);
|
|
|
|
}
|
|
|
|
|
|
|
|
BlockBuilder {
|
|
|
|
blocks,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-04 14:05:46 +01:00
|
|
|
/// Get a reference to the last generated block.
|
2018-02-16 10:11:29 +01:00
|
|
|
#[inline]
|
|
|
|
pub fn last(&self) -> &Block {
|
|
|
|
self.blocks.back().expect("There is always at least 1 block")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-04 14:05:46 +01:00
|
|
|
/// Generates a blockchain from given block builders (blocks will be concatenated).
|
2018-02-16 10:11:29 +01:00
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct BlockGenerator {
|
|
|
|
builders: VecDeque<BlockBuilder>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl BlockGenerator {
|
2019-01-04 14:05:46 +01:00
|
|
|
/// Create new block generator.
|
2018-02-16 10:11:29 +01:00
|
|
|
pub fn new<T>(builders: T) -> Self where T: IntoIterator<Item = BlockBuilder> {
|
|
|
|
BlockGenerator {
|
|
|
|
builders: builders.into_iter().collect(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Iterator for BlockGenerator {
|
|
|
|
type Item = Block;
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
|
|
loop {
|
|
|
|
match self.builders.front_mut() {
|
|
|
|
Some(ref mut builder) => {
|
|
|
|
if let Some(block) = builder.blocks.pop_front() {
|
|
|
|
return Some(block);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
None => return None,
|
|
|
|
}
|
|
|
|
self.builders.pop_front();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::{BlockBuilder, BlockOptions, BlockGenerator};
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_block_builder() {
|
|
|
|
let genesis = BlockBuilder::genesis();
|
|
|
|
let block_1 = genesis.add_block();
|
|
|
|
let block_1001 = block_1.add_blocks(1000);
|
|
|
|
let block_1002 = block_1001.add_block_with(|| BlockOptions::default());
|
|
|
|
let generator = BlockGenerator::new(vec![genesis, block_1, block_1001, block_1002]);
|
|
|
|
assert_eq!(generator.count(), 1003);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_block_builder_fork() {
|
|
|
|
let genesis = BlockBuilder::genesis();
|
|
|
|
let block_10a = genesis.add_blocks(10);
|
|
|
|
let block_11b = genesis.add_blocks(11);
|
|
|
|
assert_eq!(block_10a.last().number(), 10);
|
|
|
|
assert_eq!(block_11b.last().number(), 11);
|
|
|
|
}
|
|
|
|
}
|