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/>.
|
|
|
|
|
2016-02-03 15:33:58 +01:00
|
|
|
#[cfg(feature = "json-tests")]
|
|
|
|
use client::{BlockChainClient, Client};
|
2016-01-27 11:50:48 +01:00
|
|
|
use std::env;
|
2016-02-02 22:50:41 +01:00
|
|
|
use common::*;
|
2016-01-27 11:50:48 +01:00
|
|
|
use std::path::PathBuf;
|
|
|
|
use spec::*;
|
2016-01-27 14:21:54 +01:00
|
|
|
use std::fs::{remove_dir_all};
|
2016-01-27 17:38:16 +01:00
|
|
|
use blockchain::{BlockChain};
|
2016-01-31 10:52:07 +01:00
|
|
|
use state::*;
|
|
|
|
use rocksdb::*;
|
2016-01-27 13:41:41 +01:00
|
|
|
|
2016-02-03 15:33:58 +01:00
|
|
|
|
|
|
|
#[cfg(feature = "json-tests")]
|
2016-01-29 17:49:58 +01:00
|
|
|
pub enum ChainEra {
|
|
|
|
Frontier,
|
|
|
|
Homestead,
|
|
|
|
}
|
2016-01-27 13:41:41 +01:00
|
|
|
|
2016-01-27 14:21:54 +01:00
|
|
|
pub struct RandomTempPath {
|
|
|
|
path: PathBuf
|
|
|
|
}
|
2016-01-27 11:50:48 +01:00
|
|
|
|
2016-01-27 14:21:54 +01:00
|
|
|
impl RandomTempPath {
|
|
|
|
pub fn new() -> RandomTempPath {
|
|
|
|
let mut dir = env::temp_dir();
|
|
|
|
dir.push(H32::random().hex());
|
|
|
|
RandomTempPath {
|
|
|
|
path: dir.clone()
|
|
|
|
}
|
|
|
|
}
|
2016-01-27 11:50:48 +01:00
|
|
|
|
2016-01-27 14:21:54 +01:00
|
|
|
pub fn as_path(&self) -> &PathBuf {
|
|
|
|
&self.path
|
2016-01-27 11:50:48 +01:00
|
|
|
}
|
2016-01-31 10:52:07 +01:00
|
|
|
|
|
|
|
pub fn as_str(&self) -> &str {
|
|
|
|
self.path.to_str().unwrap()
|
|
|
|
}
|
2016-01-27 11:50:48 +01:00
|
|
|
}
|
|
|
|
|
2016-01-27 14:21:54 +01:00
|
|
|
impl Drop for RandomTempPath {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
if let Err(e) = remove_dir_all(self.as_path()) {
|
|
|
|
panic!("failed to remove temp directory, probably something failed to destroyed ({})", e);
|
|
|
|
}
|
|
|
|
}
|
2016-01-27 11:50:48 +01:00
|
|
|
}
|
|
|
|
|
2016-02-03 15:57:17 +01:00
|
|
|
#[cfg(test)]
|
2016-01-28 15:38:42 +01:00
|
|
|
pub struct GuardedTempResult<T> {
|
2016-02-03 19:34:51 +01:00
|
|
|
result: Option<T>,
|
2016-02-03 15:57:17 +01:00
|
|
|
_temp: RandomTempPath
|
2016-01-28 15:38:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> GuardedTempResult<T> {
|
|
|
|
pub fn reference(&self) -> &T {
|
2016-02-03 19:34:51 +01:00
|
|
|
self.result.as_ref().unwrap()
|
2016-01-28 15:38:42 +01:00
|
|
|
}
|
2016-01-31 10:52:07 +01:00
|
|
|
|
|
|
|
pub fn reference_mut(&mut self) -> &mut T {
|
2016-02-03 19:34:51 +01:00
|
|
|
self.result.as_mut().unwrap()
|
2016-01-31 10:52:07 +01:00
|
|
|
}
|
2016-02-03 19:34:51 +01:00
|
|
|
|
|
|
|
pub fn take(&mut self) -> T {
|
|
|
|
self.result.take().unwrap()
|
|
|
|
}
|
2016-01-28 15:38:42 +01:00
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
header.gas_limit = x!(0);
|
|
|
|
header.difficulty = x!(order * 100);
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
|
2016-02-04 23:48:29 +01:00
|
|
|
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);
|
2016-01-29 17:07:23 +01:00
|
|
|
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
|
|
|
}
|
2016-01-29 17:07:23 +01:00
|
|
|
rlp.append(&uncles);
|
2016-01-29 10:16:53 +01:00
|
|
|
rlp.out()
|
|
|
|
}
|
|
|
|
|
2016-02-03 15:33:58 +01:00
|
|
|
#[cfg(feature = "json-tests")]
|
2016-01-28 15:38:42 +01:00
|
|
|
pub fn generate_dummy_client(block_number: u32) -> GuardedTempResult<Arc<Client>> {
|
2016-01-27 14:21:54 +01:00
|
|
|
let dir = RandomTempPath::new();
|
|
|
|
|
|
|
|
let client = Client::new(get_test_spec(), dir.as_path(), IoChannel::disconnected()).unwrap();
|
2016-01-27 13:23:24 +01:00
|
|
|
let test_spec = get_test_spec();
|
|
|
|
let test_engine = test_spec.to_engine().unwrap();
|
|
|
|
let state_root = test_engine.spec().genesis_header().state_root;
|
2016-01-27 11:50:48 +01:00
|
|
|
let mut rolling_hash = test_engine.spec().genesis_header().hash();
|
|
|
|
let mut rolling_block_number = 1;
|
2016-01-27 13:23:24 +01:00
|
|
|
let mut rolling_timestamp = 40;
|
2016-01-27 11:50:48 +01:00
|
|
|
|
|
|
|
for _ in 0..block_number {
|
|
|
|
let mut header = Header::new();
|
|
|
|
|
|
|
|
header.gas_limit = decode(test_engine.spec().engine_params.get("minGasLimit").unwrap());
|
|
|
|
header.difficulty = decode(test_engine.spec().engine_params.get("minimumDifficulty").unwrap());
|
2016-01-27 13:23:24 +01:00
|
|
|
header.timestamp = rolling_timestamp;
|
2016-01-27 11:50:48 +01:00
|
|
|
header.number = rolling_block_number;
|
2016-01-27 13:23:24 +01:00
|
|
|
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(_) = client.import_block(create_test_block(&header)) {
|
|
|
|
panic!("error importing block which is valid by definition");
|
|
|
|
}
|
|
|
|
|
2016-01-27 11:50:48 +01:00
|
|
|
}
|
2016-01-27 13:23:24 +01:00
|
|
|
client.flush_queue();
|
|
|
|
client.import_verified_blocks(&IoChannel::disconnected());
|
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-02-03 15:33:58 +01:00
|
|
|
#[cfg(feature = "json-tests")]
|
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 client = Client::new(get_test_spec(), dir.as_path(), IoChannel::disconnected()).unwrap();
|
|
|
|
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(&IoChannel::disconnected());
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn generate_dummy_blockchain(block_number: u32) -> GuardedTempResult<BlockChain> {
|
2016-01-27 17:32:53 +01:00
|
|
|
let temp = RandomTempPath::new();
|
2016-01-28 17:26:32 +01:00
|
|
|
let bc = BlockChain::new(&create_unverifiable_block(0, H256::zero()), temp.as_path());
|
2016-01-27 18:31:14 +01:00
|
|
|
for block_order in 1..block_number {
|
|
|
|
bc.insert_block(&create_unverifiable_block(block_order, bc.best_block_hash()));
|
|
|
|
}
|
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();
|
2016-01-28 17:26:32 +01:00
|
|
|
let bc = BlockChain::new(&create_unverifiable_block(0, H256::zero()), temp.as_path());
|
2016-01-28 15:38:42 +01:00
|
|
|
for block_order in 1..block_number {
|
|
|
|
bc.insert_block(&create_unverifiable_block_with_extra(block_order, bc.best_block_hash(), None));
|
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
2016-01-28 17:29:35 +01:00
|
|
|
let bc = BlockChain::new(&create_unverifiable_block(0, H256::zero()), temp.as_path());
|
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-01-31 10:52:07 +01:00
|
|
|
pub fn get_temp_journal_db() -> GuardedTempResult<JournalDB> {
|
|
|
|
let temp = RandomTempPath::new();
|
|
|
|
let db = DB::open_default(temp.as_str()).unwrap();
|
|
|
|
let journal_db = JournalDB::new(db);
|
|
|
|
GuardedTempResult {
|
2016-02-03 15:57:17 +01:00
|
|
|
_temp: temp,
|
2016-02-03 19:34:51 +01:00
|
|
|
result: Some(journal_db)
|
2016-01-31 10:52:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
2016-02-03 19:34:51 +01:00
|
|
|
result: Some(State::new(journal_db, U256::from(0u8)))
|
2016-01-31 10:52:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_temp_journal_db_in(path: &Path) -> JournalDB {
|
|
|
|
let db = DB::open_default(path.to_str().unwrap()).unwrap();
|
|
|
|
JournalDB::new(db)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_temp_state_in(path: &Path) -> State {
|
|
|
|
let journal_db = get_temp_journal_db_in(path);
|
|
|
|
State::new(journal_db, U256::from(0u8))
|
|
|
|
}
|
|
|
|
|
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.to_engine().unwrap();
|
|
|
|
block_header.gas_limit = decode(test_engine.spec().engine_params.get("minGasLimit").unwrap());
|
|
|
|
block_header.difficulty = decode(test_engine.spec().engine_params.get("minimumDifficulty").unwrap());
|
|
|
|
block_header.timestamp = 40;
|
|
|
|
block_header.number = 1;
|
|
|
|
block_header.parent_hash = test_engine.spec().genesis_header().hash();
|
|
|
|
block_header.state_root = test_engine.spec().genesis_header().state_root;
|
|
|
|
|
|
|
|
create_test_block(&block_header)
|
|
|
|
}
|
|
|
|
|
2016-02-03 15:33:58 +01:00
|
|
|
#[cfg(feature = "json-tests")]
|
2016-01-28 19:14:07 +01:00
|
|
|
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.to_engine().unwrap();
|
|
|
|
block_header.gas_limit = decode(test_engine.spec().engine_params.get("minGasLimit").unwrap());
|
|
|
|
block_header.difficulty = decode(test_engine.spec().engine_params.get("minimumDifficulty").unwrap());
|
|
|
|
block_header.timestamp = 40;
|
|
|
|
block_header.number = 1;
|
|
|
|
block_header.parent_hash = test_engine.spec().genesis_header().hash();
|
|
|
|
block_header.state_root = x!(0xbad);
|
|
|
|
|
|
|
|
create_test_block(&block_header)
|
|
|
|
}
|