openethereum/ethcore/src/tests/helpers.rs

392 lines
12 KiB
Rust
Raw Normal View History

2016-02-05 13:40:41 +01:00
// Copyright 2015, 2016 Ethcore (UK) Ltd.
// 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/>.
use io::*;
use client::{self, BlockChainClient, Client, ClientConfig};
2016-02-02 22:50:41 +01:00
use common::*;
2016-01-27 11:50:48 +01:00
use spec::*;
2016-06-29 21:49:12 +02:00
use block::{OpenBlock, Drain};
use blockchain::{BlockChain, Config as BlockChainConfig};
use state::*;
use evm::Schedule;
use engines::Engine;
2016-02-09 16:31:57 +01:00
use ethereum;
2016-02-19 15:18:20 +01:00
use devtools::*;
2016-05-31 22:24:32 +02:00
use miner::Miner;
#[cfg(feature = "json-tests")]
2016-01-29 17:49:58 +01:00
pub enum ChainEra {
Frontier,
Homestead,
DaoHardfork,
2016-01-29 17:49:58 +01:00
}
2016-01-27 13:41:41 +01:00
2016-02-09 16:31:57 +01:00
pub struct TestEngine {
Snapshot creation and restoration (#1679) * to_rlp takes self by-reference * clean up some derefs * out-of-order insertion for blockchain * implement block rebuilder without verification * group block chunk header into struct * block rebuilder does verification * integrate snapshot service with client service; flesh out implementation more * initial implementation of snapshot service * remove snapshottaker trait * snapshot writer trait with packed and loose implementations * write chunks using "snapshotwriter" in service * have snapshot taking use snapshotwriter * implement snapshot readers * back up client dbs when replacing * use snapshot reader in snapshot service * describe offset format * use new get_db_path in parity, allow some errors in service * blockchain formatting * implement parity snapshot * implement snapshot restore * force blocks to be submitted in order * fix bug loading block hashes in packed reader * fix seal field loading * fix uncle hash computation * fix a few bugs * store genesis state in db. reverse block chunk order in packed writer * allow out-of-order import for blocks * bring restoration types together * only snapshot the last 30000 blocks * restore into overlaydb instead of journaldb * commit version to database * use memorydbs and commit directly * fix trie test compilation * fix failing tests * sha3_null_rlp, not H256::zero * move overlaydb to ref_overlaydb, add new overlaydb without on-disk rc * port archivedb to new overlaydb * add deletion mode tests for overlaydb * use new overlaydb, check state root at end * share chain info between state and block snapshotting * create blocks snapshot using blockchain directly * allow snapshot from arbitrary block, remove panickers from snapshot creation * begin test framework * blockchain chunking test * implement stateproducer::tick * state snapshot test * create block and state chunks concurrently, better restoration informant * fix tests * add deletion mode tests for overlaydb * address comments * more tests * Fix up tests. * remove a few printlns * add a little more documentation to `commit` * fix tests * fix ref_overlaydb test names * snapshot command skeleton * revert ref_overlaydb renaming * reimplement snapshot commands * fix many errors * everything but inject * get ethcore compiling * get snapshot tests passing again * instrument snapshot commands again * fix fallout from other changes, mark snapshots as experimental * optimize injection patterns * do two injections * fix up tests * take snapshots from 1000 blocks efore * address minor comments * fix a few io crate related errors * clarify names about total difficulty [ci skip]
2016-08-05 17:00:46 +02:00
engine: Arc<Engine>,
2016-02-09 16:31:57 +01:00
max_depth: usize
}
impl TestEngine {
pub fn new(max_depth: usize) -> TestEngine {
2016-02-09 16:31:57 +01:00
TestEngine {
engine: ethereum::new_frontier_test().engine,
2016-02-09 16:31:57 +01:00
max_depth: max_depth
}
}
}
impl Engine for TestEngine {
fn name(&self) -> &str {
"TestEngine"
}
fn params(&self) -> &CommonParams {
self.engine.params()
}
fn builtins(&self) -> &BTreeMap<Address, Builtin> {
self.engine.builtins()
}
2016-02-09 16:31:57 +01:00
fn schedule(&self, _env_info: &EnvInfo) -> Schedule {
let mut schedule = Schedule::new_frontier();
schedule.max_depth = self.max_depth;
schedule
}
}
// TODO: move everything over to get_null_spec.
2016-01-27 11:50:48 +01:00
pub fn get_test_spec() -> Spec {
Spec::new_test()
}
2016-01-27 13:23:24 +01:00
pub fn create_test_block(header: &Header) -> Bytes {
let mut rlp = RlpStream::new_list(3);
rlp.append(header);
rlp.append_raw(&rlp::EMPTY_LIST_RLP, 1);
rlp.append_raw(&rlp::EMPTY_LIST_RLP, 1);
rlp.out()
}
2016-01-28 15:38:42 +01:00
fn create_unverifiable_block_header(order: u32, parent_hash: H256) -> Header {
2016-01-27 18:31:14 +01:00
let mut header = Header::new();
2016-05-31 16:59:01 +02:00
header.gas_limit = 0.into();
2016-05-31 21:38:05 +02:00
header.difficulty = (order * 100).into();
2016-01-27 18:31:14 +01:00
header.timestamp = (order * 10) as u64;
header.number = order as u64;
header.parent_hash = parent_hash;
2016-01-28 17:26:32 +01:00
header.state_root = H256::zero();
2016-01-27 18:31:14 +01:00
2016-01-28 12:24:16 +01:00
header
}
2016-01-28 15:38:42 +01:00
fn create_unverifiable_block_with_extra(order: u32, parent_hash: H256, extra: Option<Bytes>) -> Bytes {
let mut header = create_unverifiable_block_header(order, parent_hash);
2016-01-28 12:24:16 +01:00
header.extra_data = match extra {
Some(extra_data) => extra_data,
None => {
2016-01-28 15:38:42 +01:00
let base = (order & 0x000000ff) as u8;
2016-01-28 16:59:31 +01:00
let generated: Vec<u8> = vec![base + 1, base + 2, base + 3];
generated
2016-01-28 12:24:16 +01:00
}
2016-01-28 15:38:42 +01:00
};
2016-01-27 18:31:14 +01:00
create_test_block(&header)
}
2016-01-28 17:15:45 +01:00
fn create_unverifiable_block(order: u32, parent_hash: H256) -> Bytes {
create_test_block(&create_unverifiable_block_header(order, parent_hash))
}
pub fn create_test_block_with_data(header: &Header, transactions: &[SignedTransaction], uncles: &[Header]) -> Bytes {
2016-01-29 10:16:53 +01:00
let mut rlp = RlpStream::new_list(3);
rlp.append(header);
rlp.begin_list(transactions.len());
2016-01-29 10:16:53 +01:00
for t in transactions {
2016-02-04 23:48:29 +01:00
rlp.append_raw(&encode::<SignedTransaction>(t).to_vec(), 1);
2016-01-29 10:16:53 +01:00
}
rlp.append(&uncles);
2016-01-29 10:16:53 +01:00
rlp.out()
}
2016-01-28 15:38:42 +01:00
pub fn generate_dummy_client(block_number: u32) -> GuardedTempResult<Arc<Client>> {
2016-06-18 15:11:10 +02:00
generate_dummy_client_with_spec_and_data(Spec::new_test, block_number, 0, &[])
}
pub fn generate_dummy_client_with_data(block_number: u32, txs_per_block: usize, tx_gas_prices: &[U256]) -> GuardedTempResult<Arc<Client>> {
generate_dummy_client_with_spec_and_data(Spec::new_null, block_number, txs_per_block, tx_gas_prices)
}
pub fn generate_dummy_client_with_spec_and_data<F>(get_test_spec: F, block_number: u32, txs_per_block: usize, tx_gas_prices: &[U256]) -> GuardedTempResult<Arc<Client>> where F: Fn()->Spec {
2016-01-27 14:21:54 +01:00
let dir = RandomTempPath::new();
2016-01-27 13:23:24 +01:00
let test_spec = get_test_spec();
let client = Client::new(ClientConfig::default(), &test_spec, dir.as_path(), Arc::new(Miner::with_spec(&test_spec)), IoChannel::disconnected()).unwrap();
let test_engine = &*test_spec.engine;
let mut db_result = get_temp_journal_db();
let mut db = db_result.take();
test_spec.ensure_db_good(db.as_hashdb_mut()).unwrap();
let vm_factory = Default::default();
let genesis_header = test_spec.genesis_header();
2016-01-27 13:23:24 +01:00
let mut rolling_timestamp = 40;
let mut last_hashes = vec![];
let mut last_header = genesis_header.clone();
2016-01-27 11:50:48 +01:00
let kp = KeyPair::from_secret("".sha3()).unwrap() ;
let author = kp.address();
2016-01-27 11:50:48 +01:00
let mut n = 0;
for _ in 0..block_number {
last_hashes.push(last_header.hash());
// forge block.
let mut b = OpenBlock::new(
test_engine,
&vm_factory,
Default::default(),
false,
db,
&last_header,
Arc::new(last_hashes.clone()),
author.clone(),
2016-06-23 14:43:20 +02:00
(3141562.into(), 31415620.into()),
vec![]
).unwrap();
b.set_difficulty(U256::from(0x20000));
rolling_timestamp += 10;
b.set_timestamp(rolling_timestamp);
// first block we don't have any balance, so can't send any transactions.
for _ in 0..txs_per_block {
b.push_transaction(Transaction {
nonce: n.into(),
gas_price: tx_gas_prices[n % tx_gas_prices.len()],
gas: 100000.into(),
action: Action::Create,
data: vec![],
value: U256::zero(),
}.sign(kp.secret()), None).unwrap();
n += 1;
}
2016-01-27 13:23:24 +01:00
let b = b.close_and_lock().seal(test_engine, vec![]).unwrap();
2016-01-27 13:23:24 +01:00
if let Err(e) = client.import_block(b.rlp_bytes()) {
2016-03-02 15:06:53 +01:00
panic!("error importing block which is valid by definition: {:?}", e);
2016-01-27 13:23:24 +01:00
}
2016-06-18 15:11:10 +02:00
last_header = BlockView::new(&b.rlp_bytes()).header();
db = b.drain();
2016-01-27 11:50:48 +01:00
}
2016-01-27 13:23:24 +01:00
client.flush_queue();
client.import_verified_blocks();
2016-01-28 15:38:42 +01:00
GuardedTempResult::<Arc<Client>> {
2016-02-03 15:57:17 +01:00
_temp: dir,
2016-02-03 19:34:51 +01:00
result: Some(client)
2016-01-28 15:38:42 +01:00
}
2016-01-27 17:32:53 +01:00
}
2016-03-02 15:06:53 +01:00
pub fn push_blocks_to_client(client: &Arc<Client>, timestamp_salt: u64, starting_number: usize, block_number: usize) {
let test_spec = get_test_spec();
let test_engine = &test_spec.engine;
//let test_engine = test_spec.to_engine().unwrap();
let state_root = test_spec.genesis_header().state_root;
2016-03-02 15:06:53 +01:00
let mut rolling_hash = client.chain_info().best_block_hash;
let mut rolling_block_number = starting_number as u64;
let mut rolling_timestamp = timestamp_salt + starting_number as u64 * 10;
for _ in 0..block_number {
let mut header = Header::new();
header.gas_limit = test_engine.params().min_gas_limit;
header.difficulty = U256::from(0x20000);
2016-03-02 15:06:53 +01:00
header.timestamp = rolling_timestamp;
header.number = rolling_block_number;
header.parent_hash = rolling_hash;
header.state_root = state_root.clone();
rolling_hash = header.hash();
rolling_block_number = rolling_block_number + 1;
rolling_timestamp = rolling_timestamp + 10;
if let Err(e) = client.import_block(create_test_block(&header)) {
panic!("error importing block which is valid by definition: {:?}", e);
}
}
}
2016-01-28 15:38:42 +01:00
pub fn get_test_client_with_blocks(blocks: Vec<Bytes>) -> GuardedTempResult<Arc<Client>> {
let dir = RandomTempPath::new();
let test_spec = get_test_spec();
let client = Client::new(ClientConfig::default(), &test_spec, dir.as_path(), Arc::new(Miner::with_spec(&test_spec)), IoChannel::disconnected()).unwrap();
2016-01-28 15:38:42 +01:00
for block in &blocks {
if let Err(_) = client.import_block(block.clone()) {
panic!("panic importing block which is well-formed");
}
}
client.flush_queue();
client.import_verified_blocks();
2016-01-28 15:38:42 +01:00
GuardedTempResult::<Arc<Client>> {
2016-02-03 15:57:17 +01:00
_temp: dir,
2016-02-03 19:34:51 +01:00
result: Some(client)
2016-01-28 15:38:42 +01:00
}
}
fn new_db(path: &str) -> Arc<Database> {
Arc::new(
Database::open(&DatabaseConfig::with_columns(client::DB_NO_OF_COLUMNS), path)
.expect("Opening database for tests should always work.")
)
}
2016-01-28 15:38:42 +01:00
pub fn generate_dummy_blockchain(block_number: u32) -> GuardedTempResult<BlockChain> {
2016-01-27 17:32:53 +01:00
let temp = RandomTempPath::new();
let db = new_db(temp.as_str());
let bc = BlockChain::new(BlockChainConfig::default(), &create_unverifiable_block(0, H256::zero()), db.clone());
let batch = db.transaction();
2016-01-27 18:31:14 +01:00
for block_order in 1..block_number {
bc.insert_block(&batch, &create_unverifiable_block(block_order, bc.best_block_hash()), vec![]);
bc.commit();
2016-01-27 18:31:14 +01:00
}
db.write(batch).unwrap();
2016-01-28 15:38:42 +01:00
GuardedTempResult::<BlockChain> {
2016-02-03 15:57:17 +01:00
_temp: temp,
2016-02-03 19:34:51 +01:00
result: Some(bc)
2016-01-28 15:38:42 +01:00
}
2016-01-28 12:24:16 +01:00
}
2016-01-28 15:38:42 +01:00
pub fn generate_dummy_blockchain_with_extra(block_number: u32) -> GuardedTempResult<BlockChain> {
let temp = RandomTempPath::new();
let db = new_db(temp.as_str());
let bc = BlockChain::new(BlockChainConfig::default(), &create_unverifiable_block(0, H256::zero()), db.clone());
let batch = db.transaction();
2016-01-28 15:38:42 +01:00
for block_order in 1..block_number {
bc.insert_block(&batch, &create_unverifiable_block_with_extra(block_order, bc.best_block_hash(), None), vec![]);
bc.commit();
2016-01-28 15:38:42 +01:00
}
db.write(batch).unwrap();
2016-01-28 15:38:42 +01:00
GuardedTempResult::<BlockChain> {
2016-02-03 15:57:17 +01:00
_temp: temp,
2016-02-03 19:34:51 +01:00
result: Some(bc)
2016-01-28 15:38:42 +01:00
}
}
pub fn generate_dummy_empty_blockchain() -> GuardedTempResult<BlockChain> {
let temp = RandomTempPath::new();
let db = new_db(temp.as_str());
let bc = BlockChain::new(BlockChainConfig::default(), &create_unverifiable_block(0, H256::zero()), db.clone());
2016-01-28 15:38:42 +01:00
GuardedTempResult::<BlockChain> {
2016-02-03 15:57:17 +01:00
_temp: temp,
2016-02-03 19:34:51 +01:00
result: Some(bc)
2016-01-28 15:38:42 +01:00
}
2016-01-28 19:14:07 +01:00
}
2016-03-11 13:21:53 +01:00
pub fn get_temp_journal_db() -> GuardedTempResult<Box<JournalDB>> {
let temp = RandomTempPath::new();
let journal_db = get_temp_journal_db_in(temp.as_path());
GuardedTempResult {
2016-02-03 15:57:17 +01:00
_temp: temp,
2016-02-03 19:34:51 +01:00
result: Some(journal_db)
}
}
pub fn get_temp_state() -> GuardedTempResult<State> {
let temp = RandomTempPath::new();
let journal_db = get_temp_journal_db_in(temp.as_path());
GuardedTempResult {
2016-02-03 15:57:17 +01:00
_temp: temp,
result: Some(State::new(journal_db, U256::from(0), Default::default())),
}
}
2016-03-11 13:21:53 +01:00
pub fn get_temp_journal_db_in(path: &Path) -> Box<JournalDB> {
let db = new_db(path.to_str().expect("Only valid utf8 paths for tests."));
journaldb::new(db.clone(), journaldb::Algorithm::EarlyMerge, None)
}
pub fn get_temp_state_in(path: &Path) -> State {
let journal_db = get_temp_journal_db_in(path);
State::new(journal_db, U256::from(0), Default::default())
}
2016-02-25 17:14:45 +01:00
pub fn get_good_dummy_block_seq(count: usize) -> Vec<Bytes> {
let test_spec = get_test_spec();
get_good_dummy_block_fork_seq(1, count, &test_spec.genesis_header().hash())
2016-03-02 15:06:53 +01:00
}
pub fn get_good_dummy_block_fork_seq(start_number: usize, count: usize, parent_hash: &H256) -> Vec<Bytes> {
let test_spec = get_test_spec();
let test_engine = &test_spec.engine;
2016-03-02 15:06:53 +01:00
let mut rolling_timestamp = start_number as u64 * 10;
let mut parent = *parent_hash;
2016-02-25 17:14:45 +01:00
let mut r = Vec::new();
2016-03-02 15:06:53 +01:00
for i in start_number .. start_number + count + 1 {
2016-02-25 17:14:45 +01:00
let mut block_header = Header::new();
block_header.gas_limit = test_engine.params().min_gas_limit;
2016-03-02 15:06:53 +01:00
block_header.difficulty = U256::from(i).mul(U256([0, 1, 0, 0]));
block_header.timestamp = rolling_timestamp;
2016-02-25 17:14:45 +01:00
block_header.number = i as u64;
block_header.parent_hash = parent;
block_header.state_root = test_spec.genesis_header().state_root;
2016-03-02 15:06:53 +01:00
2016-02-25 17:14:45 +01:00
parent = block_header.hash();
2016-03-02 15:06:53 +01:00
rolling_timestamp = rolling_timestamp + 10;
2016-02-25 17:14:45 +01:00
r.push(create_test_block(&block_header));
2016-03-02 15:06:53 +01:00
2016-02-25 17:14:45 +01:00
}
r
}
2016-01-28 19:14:07 +01:00
pub fn get_good_dummy_block() -> Bytes {
let mut block_header = Header::new();
let test_spec = get_test_spec();
let test_engine = &test_spec.engine;
block_header.gas_limit = test_engine.params().min_gas_limit;
block_header.difficulty = U256::from(0x20000);
2016-01-28 19:14:07 +01:00
block_header.timestamp = 40;
block_header.number = 1;
block_header.parent_hash = test_spec.genesis_header().hash();
block_header.state_root = test_spec.genesis_header().state_root;
2016-01-28 19:14:07 +01:00
create_test_block(&block_header)
}
pub fn get_bad_state_dummy_block() -> Bytes {
let mut block_header = Header::new();
let test_spec = get_test_spec();
let test_engine = &test_spec.engine;
block_header.gas_limit = test_engine.params().min_gas_limit;
block_header.difficulty = U256::from(0x20000);
2016-01-28 19:14:07 +01:00
block_header.timestamp = 40;
block_header.number = 1;
block_header.parent_hash = test_spec.genesis_header().hash();
2016-05-31 16:59:01 +02:00
block_header.state_root = 0xbad.into();
2016-01-28 19:14:07 +01:00
create_test_block(&block_header)
}