openethereum/ethcore/src/tests/client.rs

152 lines
4.9 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/>.
2016-02-25 14:09:39 +01:00
use client::{BlockChainClient, Client, ClientConfig, BlockId};
2016-03-02 00:34:38 +01:00
use block::IsBlock;
2016-02-02 22:50:41 +01:00
use tests::helpers::*;
use common::*;
2016-02-19 15:18:20 +01:00
use devtools::*;
2016-01-26 11:39:49 +01:00
2016-01-26 14:33:22 +01:00
#[test]
fn created() {
2016-01-27 14:21:54 +01:00
let dir = RandomTempPath::new();
2016-02-25 14:09:39 +01:00
let client_result = Client::new(ClientConfig::default(), get_test_spec(), dir.as_path(), IoChannel::disconnected());
2016-01-26 11:39:49 +01:00
assert!(client_result.is_ok());
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();
2016-02-25 14:09:39 +01:00
let client = Client::new(ClientConfig::default(), get_test_spec(), dir.as_path(), IoChannel::disconnected()).unwrap();
2016-01-26 14:33:22 +01:00
client.import_verified_blocks(&IoChannel::disconnected());
client.flush_queue();
}
2016-03-11 20:24:44 +01:00
#[test]
fn returns_state_root_basic() {
let client_result = generate_dummy_client(6);
let client = client_result.reference();
let test_spec = get_test_spec();
let test_engine = test_spec.to_engine().unwrap();
let state_root = test_engine.spec().genesis_header().state_root;
assert!(client.state_data(&state_root).is_some());
}
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();
2016-02-25 14:09:39 +01:00
let client = Client::new(ClientConfig::default(), get_test_spec(), dir.as_path(), IoChannel::disconnected()).unwrap();
2016-01-26 14:33:22 +01:00
let good_block = get_good_dummy_block();
if let Err(_) = client.import_block(good_block) {
panic!("error importing block being good by definition");
}
client.flush_queue();
client.import_verified_blocks(&IoChannel::disconnected());
let block = client.block_header(BlockId::Number(1)).unwrap();
2016-01-26 14:33:22 +01:00
assert!(!block.is_empty());
}
#[test]
fn query_none_block() {
2016-01-27 14:21:54 +01:00
let dir = RandomTempPath::new();
2016-02-25 14:09:39 +01:00
let client = Client::new(ClientConfig::default(), get_test_spec(), dir.as_path(), IoChannel::disconnected()).unwrap();
2016-01-26 14:33:22 +01:00
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() {
2016-01-28 15:38:42 +01:00
let client_result = get_test_client_with_blocks(vec![get_bad_state_dummy_block()]);
let client = client_result.reference();
let bad_block:Option<Bytes> = 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();
2016-01-28 15:38:42 +01:00
let client_result = get_test_client_with_blocks(vec![dummy_block.clone()]);
let client = client_result.reference();
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_block_body() {
let dummy_block = get_good_dummy_block();
let client_result = get_test_client_with_blocks(vec![dummy_block.clone()]);
let client = client_result.reference();
let block = BlockView::new(&dummy_block);
let body = client.block_body(BlockId::Hash(block.header().hash())).unwrap();
let body = Rlp::new(&body);
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() {
2016-01-28 15:38:42 +01:00
let client_result = generate_dummy_client(6);
let client = client_result.reference();
let block = client.block_header(BlockId::Number(5)).unwrap();
2016-01-27 13:23:24 +01:00
assert!(!block.is_empty());
}
#[test]
fn can_collect_garbage() {
2016-01-28 15:38:42 +01:00
let client_result = generate_dummy_client(100);
let client = client_result.reference();
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_handle_long_fork() {
let client_result = generate_dummy_client(1200);
let client = client_result.reference();
for _ in 0..10 {
client.import_verified_blocks(&IoChannel::disconnected());
}
assert_eq!(1200, client.chain_info().best_block_number);
push_blocks_to_client(client, 45, 1201, 800);
push_blocks_to_client(client, 49, 1201, 800);
push_blocks_to_client(client, 53, 1201, 600);
for _ in 0..20 {
client.import_verified_blocks(&IoChannel::disconnected());
}
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);
let client_result = get_test_client_with_blocks(vec![dummy_blocks[0].clone()]);
let client = client_result.reference();
2016-03-14 10:48:32 +01:00
let b = client.prepare_sealing(Address::default(), x!(31415926), vec![], vec![]).unwrap();
assert_eq!(*b.block().header().parent_hash(), BlockView::new(&dummy_blocks[0]).header_view().sha3());
assert!(client.try_seal(b, vec![]).is_ok());
}