openethereum/ethcore/src/tests/client.rs

367 lines
11 KiB
Rust
Raw Normal View History

// Copyright 2015-2017 Parity Technologies (UK) Ltd.
2016-02-05 13:40:41 +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/>.
use io::IoChannel;
use client::{BlockChainClient, MiningBlockChainClient, Client, ClientConfig, BlockId};
use state::{self, State, CleanupMode};
use executive::Executive;
use ethereum;
2016-03-02 00:34:38 +01:00
use block::IsBlock;
2016-02-02 22:50:41 +01:00
use tests::helpers::*;
use types::filter::Filter;
use util::*;
2016-02-19 15:18:20 +01:00
use devtools::*;
2016-05-31 22:24:32 +02:00
use miner::Miner;
use spec::Spec;
use views::BlockView;
use ethkey::KeyPair;
use transaction::{PendingTransaction, Transaction, Action, Condition};
2016-12-16 13:27:14 +01:00
use miner::MinerService;
2016-01-26 11:39:49 +01:00
2016-01-26 14:33:22 +01:00
#[test]
fn imports_from_empty() {
2016-01-27 14:21:54 +01:00
let dir = RandomTempPath::new();
let spec = get_test_spec();
let db_config = DatabaseConfig::with_columns(::db::NUM_COLUMNS);
let client_db = Arc::new(Database::open(&db_config, dir.as_path().to_str().unwrap()).unwrap());
let client = Client::new(
ClientConfig::default(),
&spec,
client_db,
Arc::new(Miner::with_spec(&spec)),
IoChannel::disconnected(),
).unwrap();
client.import_verified_blocks();
2016-01-26 14:33:22 +01:00
client.flush_queue();
}
#[test]
fn should_return_registrar() {
let dir = RandomTempPath::new();
let spec = ethereum::new_morden();
let db_config = DatabaseConfig::with_columns(::db::NUM_COLUMNS);
let client_db = Arc::new(Database::open(&db_config, dir.as_path().to_str().unwrap()).unwrap());
let client = Client::new(
ClientConfig::default(),
&spec,
client_db,
Arc::new(Miner::with_spec(&spec)),
IoChannel::disconnected(),
).unwrap();
let params = client.additional_params();
2016-11-28 13:20:49 +01:00
let address = &params["registrar"];
assert_eq!(address.len(), 40);
assert!(U256::from_str(address).is_ok());
}
2016-03-11 20:24:44 +01:00
#[test]
fn returns_state_root_basic() {
2017-04-06 19:26:17 +02:00
let client = generate_dummy_client(6);
2016-03-11 20:24:44 +01:00
let test_spec = get_test_spec();
let genesis_header = test_spec.genesis_header();
2016-03-11 20:24:44 +01:00
assert!(client.state_data(genesis_header.state_root()).is_some());
2016-03-11 20:24:44 +01:00
}
2016-01-26 14:33:22 +01:00
#[test]
fn imports_good_block() {
2016-01-27 14:21:54 +01:00
let dir = RandomTempPath::new();
let spec = get_test_spec();
let db_config = DatabaseConfig::with_columns(::db::NUM_COLUMNS);
let client_db = Arc::new(Database::open(&db_config, dir.as_path().to_str().unwrap()).unwrap());
let client = Client::new(
ClientConfig::default(),
&spec,
client_db,
Arc::new(Miner::with_spec(&spec)),
IoChannel::disconnected(),
).unwrap();
2016-01-26 14:33:22 +01:00
let good_block = get_good_dummy_block();
2016-11-28 13:20:49 +01:00
if client.import_block(good_block).is_err() {
2016-01-26 14:33:22 +01:00
panic!("error importing block being good by definition");
}
client.flush_queue();
client.import_verified_blocks();
2016-01-26 14:33:22 +01:00
let block = client.block_header(BlockId::Number(1)).unwrap();
assert!(!block.into_inner().is_empty());
2016-01-26 14:33:22 +01:00
}
#[test]
fn query_none_block() {
2016-01-27 14:21:54 +01:00
let dir = RandomTempPath::new();
let spec = get_test_spec();
let db_config = DatabaseConfig::with_columns(::db::NUM_COLUMNS);
let client_db = Arc::new(Database::open(&db_config, dir.as_path().to_str().unwrap()).unwrap());
2016-01-26 14:33:22 +01:00
let client = Client::new(
ClientConfig::default(),
&spec,
client_db,
Arc::new(Miner::with_spec(&spec)),
IoChannel::disconnected(),
).unwrap();
let non_existant = client.block_header(BlockId::Number(188));
2016-01-26 14:55:08 +01:00
assert!(non_existant.is_none());
}
#[test]
fn query_bad_block() {
2017-04-06 19:26:17 +02:00
let client = get_test_client_with_blocks(vec![get_bad_state_dummy_block()]);
let bad_block: Option<_> = client.block_header(BlockId::Number(1));
2016-01-26 14:55:08 +01:00
assert!(bad_block.is_none());
2016-01-26 18:05:25 +01:00
}
#[test]
fn returns_chain_info() {
let dummy_block = get_good_dummy_block();
2017-04-06 19:26:17 +02:00
let client = get_test_client_with_blocks(vec![dummy_block.clone()]);
2016-01-26 18:05:25 +01:00
let block = BlockView::new(&dummy_block);
let info = client.chain_info();
assert_eq!(info.best_block_hash, block.header().hash());
2016-01-27 13:23:24 +01:00
}
#[test]
fn returns_logs() {
let dummy_block = get_good_dummy_block();
2017-04-06 19:26:17 +02:00
let client = get_test_client_with_blocks(vec![dummy_block.clone()]);
let logs = client.logs(Filter {
from_block: BlockId::Earliest,
to_block: BlockId::Latest,
address: None,
topics: vec![],
limit: None,
});
assert_eq!(logs.len(), 0);
}
#[test]
fn returns_logs_with_limit() {
let dummy_block = get_good_dummy_block();
2017-04-06 19:26:17 +02:00
let client = get_test_client_with_blocks(vec![dummy_block.clone()]);
let logs = client.logs(Filter {
from_block: BlockId::Earliest,
to_block: BlockId::Latest,
address: None,
topics: vec![],
limit: None,
});
assert_eq!(logs.len(), 0);
}
#[test]
fn returns_block_body() {
let dummy_block = get_good_dummy_block();
2017-04-06 19:26:17 +02:00
let client = get_test_client_with_blocks(vec![dummy_block.clone()]);
let block = BlockView::new(&dummy_block);
let body = client.block_body(BlockId::Hash(block.header().hash())).unwrap();
let body = body.rlp();
assert_eq!(body.item_count(), 2);
assert_eq!(body.at(0).as_raw()[..], block.rlp().at(1).as_raw()[..]);
assert_eq!(body.at(1).as_raw()[..], block.rlp().at(2).as_raw()[..]);
}
2016-01-27 13:23:24 +01:00
#[test]
fn imports_block_sequence() {
2017-04-06 19:26:17 +02:00
let client = generate_dummy_client(6);
let block = client.block_header(BlockId::Number(5)).unwrap();
2016-01-27 13:23:24 +01:00
assert!(!block.into_inner().is_empty());
2016-01-27 13:23:24 +01:00
}
#[test]
fn can_collect_garbage() {
2017-04-06 19:26:17 +02:00
let client = generate_dummy_client(100);
2016-01-27 13:23:24 +01:00
client.tick();
2016-02-25 14:09:39 +01:00
assert!(client.blockchain_cache_info().blocks < 100 * 1024);
2016-02-02 22:50:41 +01:00
}
2016-03-02 15:06:53 +01:00
#[test]
fn can_generate_gas_price_median() {
2017-04-06 19:26:17 +02:00
let client = generate_dummy_client_with_data(3, 1, slice_into![1, 2, 3]);
2017-02-17 16:18:31 +01:00
assert_eq!(Some(&U256::from(2)), client.gas_price_corpus(3).median());
2017-04-06 19:26:17 +02:00
let client = generate_dummy_client_with_data(4, 1, slice_into![1, 4, 3, 2]);
2017-02-17 16:18:31 +01:00
assert_eq!(Some(&U256::from(3)), client.gas_price_corpus(3).median());
}
#[test]
fn can_generate_gas_price_histogram() {
2017-04-06 19:26:17 +02:00
let client = generate_dummy_client_with_data(20, 1, slice_into![6354,8593,6065,4842,7845,7002,689,4958,4250,6098,5804,4320,643,8895,2296,8589,7145,2000,2512,1408]);
2017-02-17 16:18:31 +01:00
let hist = client.gas_price_corpus(20).histogram(5).unwrap();
let correct_hist = ::stats::Histogram { bucket_bounds: vec_into![643, 2294, 3945, 5596, 7247, 8898], counts: vec![4,2,4,6,4] };
assert_eq!(hist, correct_hist);
}
#[test]
fn empty_gas_price_histogram() {
2017-04-06 19:26:17 +02:00
let client = generate_dummy_client_with_data(20, 0, slice_into![]);
2017-02-17 16:18:31 +01:00
assert!(client.gas_price_corpus(20).histogram(5).is_none());
}
#[test]
fn corpus_is_sorted() {
2017-04-06 19:26:17 +02:00
let client = generate_dummy_client_with_data(2, 1, slice_into![U256::from_str("11426908979").unwrap(), U256::from_str("50426908979").unwrap()]);
let corpus = client.gas_price_corpus(20);
assert!(corpus[0] < corpus[1]);
}
2016-03-02 15:06:53 +01:00
#[test]
fn can_handle_long_fork() {
2017-04-06 19:26:17 +02:00
let client = generate_dummy_client(1200);
2016-05-27 18:57:12 +02:00
for _ in 0..20 {
client.import_verified_blocks();
2016-03-02 15:06:53 +01:00
}
assert_eq!(1200, client.chain_info().best_block_number);
2017-04-06 19:26:17 +02:00
push_blocks_to_client(&client, 45, 1201, 800);
push_blocks_to_client(&client, 49, 1201, 800);
push_blocks_to_client(&client, 53, 1201, 600);
2016-03-02 15:06:53 +01:00
for _ in 0..400 {
client.import_verified_blocks();
2016-03-02 15:06:53 +01:00
}
assert_eq!(2000, client.chain_info().best_block_number);
}
2016-03-02 00:34:38 +01:00
#[test]
fn can_mine() {
let dummy_blocks = get_good_dummy_block_seq(2);
2017-04-06 19:26:17 +02:00
let client = get_test_client_with_blocks(vec![dummy_blocks[0].clone()]);
2016-06-23 14:43:20 +02:00
let b = client.prepare_open_block(Address::default(), (3141562.into(), 31415620.into()), vec![]).close();
assert_eq!(*b.block().header().parent_hash(), BlockView::new(&dummy_blocks[0]).header_view().sha3());
}
#[test]
fn change_history_size() {
let dir = RandomTempPath::new();
let test_spec = Spec::new_null();
let mut config = ClientConfig::default();
let db_config = DatabaseConfig::with_columns(::db::NUM_COLUMNS);
let client_db = Arc::new(Database::open(&db_config, dir.as_path().to_str().unwrap()).unwrap());
config.history = 2;
let address = Address::random();
{
let client = Client::new(
ClientConfig::default(),
&test_spec,
client_db.clone(),
Arc::new(Miner::with_spec(&test_spec)),
IoChannel::disconnected()
).unwrap();
for _ in 0..20 {
let mut b = client.prepare_open_block(Address::default(), (3141562.into(), 31415620.into()), vec![]);
b.block_mut().fields_mut().state.add_balance(&address, &5.into(), CleanupMode::NoEmpty).unwrap();
b.block_mut().fields_mut().state.commit().unwrap();
let b = b.close_and_lock().seal(&*test_spec.engine, vec![]).unwrap();
client.import_sealed_block(b).unwrap(); // account change is in the journal overlay
}
}
let mut config = ClientConfig::default();
config.history = 10;
let client = Client::new(
config,
&test_spec,
client_db,
Arc::new(Miner::with_spec(&test_spec)),
IoChannel::disconnected(),
).unwrap();
assert_eq!(client.state().balance(&address).unwrap(), 100.into());
}
2016-12-16 13:27:14 +01:00
#[test]
fn does_not_propagate_delayed_transactions() {
let key = KeyPair::from_secret("test".sha3().into()).unwrap();
2016-12-16 13:27:14 +01:00
let secret = key.secret();
let tx0 = PendingTransaction::new(Transaction {
nonce: 0.into(),
gas_price: 0.into(),
gas: 21000.into(),
action: Action::Call(Address::default()),
value: 0.into(),
data: Vec::new(),
}.sign(secret, None), Some(Condition::Number(2)));
2016-12-16 13:27:14 +01:00
let tx1 = PendingTransaction::new(Transaction {
nonce: 1.into(),
gas_price: 0.into(),
gas: 21000.into(),
action: Action::Call(Address::default()),
value: 0.into(),
data: Vec::new(),
}.sign(secret, None), None);
2017-04-06 19:26:17 +02:00
let client = generate_dummy_client(1);
2016-12-16 13:27:14 +01:00
2017-04-06 19:26:17 +02:00
client.miner().import_own_transaction(&*client, tx0).unwrap();
client.miner().import_own_transaction(&*client, tx1).unwrap();
2016-12-16 14:54:26 +01:00
assert_eq!(0, client.ready_transactions().len());
assert_eq!(2, client.miner().pending_transactions().len());
2017-04-06 19:26:17 +02:00
push_blocks_to_client(&client, 53, 2, 2);
2016-12-17 12:28:31 +01:00
client.flush_queue();
2016-12-16 14:54:26 +01:00
assert_eq!(2, client.ready_transactions().len());
assert_eq!(2, client.miner().pending_transactions().len());
2016-12-16 13:27:14 +01:00
}
#[test]
fn transaction_proof() {
use ::client::ProvingBlockChainClient;
2017-04-06 19:26:17 +02:00
let client = generate_dummy_client(0);
let address = Address::random();
let test_spec = Spec::new_test();
for _ in 0..20 {
let mut b = client.prepare_open_block(Address::default(), (3141562.into(), 31415620.into()), vec![]);
b.block_mut().fields_mut().state.add_balance(&address, &5.into(), CleanupMode::NoEmpty).unwrap();
b.block_mut().fields_mut().state.commit().unwrap();
let b = b.close_and_lock().seal(&*test_spec.engine, vec![]).unwrap();
client.import_sealed_block(b).unwrap(); // account change is in the journal overlay
}
let transaction = Transaction {
nonce: 0.into(),
gas_price: 0.into(),
gas: 21000.into(),
action: Action::Call(Address::default()),
value: 5.into(),
data: Vec::new(),
}.fake_sign(address);
2017-04-19 14:58:19 +02:00
let proof = client.prove_transaction(transaction.clone(), BlockId::Latest).unwrap().1;
let backend = state::backend::ProofCheck::new(&proof);
let mut factories = ::factory::Factories::default();
factories.accountdb = ::account_db::Factory::Plain; // raw state values, no mangled keys.
let root = client.best_block_header().state_root();
let mut state = State::from_existing(backend, root, 0.into(), factories.clone()).unwrap();
Executive::new(&mut state, &client.latest_env_info(), &*test_spec.engine, &factories.vm)
.transact(&transaction, Default::default()).unwrap();
assert_eq!(state.balance(&Address::default()).unwrap(), 5.into());
assert_eq!(state.balance(&address).unwrap(), 95.into());
}