2019-01-07 11:33:07 +01:00
|
|
|
// Copyright 2015-2019 Parity Technologies (UK) Ltd.
|
|
|
|
// This file is part of Parity Ethereum.
|
2016-03-10 20:27:50 +01:00
|
|
|
|
2019-01-07 11:33:07 +01:00
|
|
|
// Parity Ethereum is free software: you can redistribute it and/or modify
|
2016-03-10 20:27:50 +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,
|
2016-03-10 20:27:50 +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/>.
|
2016-03-10 20:27:50 +01:00
|
|
|
|
2016-03-10 21:17:58 +01:00
|
|
|
//! Test client.
|
|
|
|
|
2019-06-03 15:36:21 +02:00
|
|
|
use std::str::FromStr;
|
2018-04-03 16:49:23 +02:00
|
|
|
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering as AtomicOrder};
|
2017-07-29 21:56:42 +02:00
|
|
|
use std::sync::Arc;
|
2017-07-29 17:12:07 +02:00
|
|
|
use std::collections::{HashMap, BTreeMap};
|
|
|
|
use std::mem;
|
2019-01-04 14:05:46 +01:00
|
|
|
|
|
|
|
use blockchain::{TreeRoute, BlockReceipts};
|
|
|
|
use bytes::Bytes;
|
|
|
|
use db::{NUM_COLUMNS, COL_STATE};
|
|
|
|
use ethcore_miner::pool::VerifiedTransaction;
|
2018-01-10 13:35:18 +01:00
|
|
|
use ethereum_types::{H256, U256, Address};
|
2019-01-04 14:05:46 +01:00
|
|
|
use ethkey::{Generator, Random};
|
|
|
|
use ethtrie;
|
|
|
|
use hash::keccak;
|
|
|
|
use itertools::Itertools;
|
2018-01-10 13:35:18 +01:00
|
|
|
use kvdb::DBValue;
|
2018-03-01 19:53:15 +01:00
|
|
|
use kvdb_memorydb;
|
2019-01-04 14:05:46 +01:00
|
|
|
use parking_lot::RwLock;
|
2018-04-16 15:52:12 +02:00
|
|
|
use rlp::{Rlp, RlpStream};
|
2019-01-04 14:05:46 +01:00
|
|
|
use rustc_hex::FromHex;
|
|
|
|
use types::transaction::{self, Transaction, LocalizedTransaction, SignedTransaction, Action};
|
|
|
|
use types::BlockNumber;
|
|
|
|
use types::basic_account::BasicAccount;
|
|
|
|
use types::encoded;
|
|
|
|
use types::filter::Filter;
|
|
|
|
use types::header::Header;
|
|
|
|
use types::log_entry::LocalizedLogEntry;
|
|
|
|
use types::pruning_info::PruningInfo;
|
|
|
|
use types::receipt::{Receipt, LocalizedReceipt, TransactionOutcome};
|
|
|
|
use types::view;
|
|
|
|
use types::views::BlockView;
|
|
|
|
use vm::Schedule;
|
|
|
|
|
|
|
|
use block::{OpenBlock, SealedBlock, ClosedBlock};
|
2019-01-16 19:52:21 +01:00
|
|
|
use call_contract::{CallContract, RegistryInfo};
|
2016-08-17 19:25:02 +02:00
|
|
|
use client::{
|
2019-01-16 19:52:21 +01:00
|
|
|
Nonce, Balance, ChainInfo, BlockInfo, ReopenBlock, TransactionInfo,
|
2018-06-06 14:14:45 +02:00
|
|
|
PrepareOpenBlock, BlockChainClient, BlockChainInfo, BlockStatus, BlockId, Mode,
|
2018-09-24 12:28:54 +02:00
|
|
|
TransactionId, UncleId, TraceId, TraceFilter, LastHashes, CallAnalytics,
|
2018-03-03 18:42:13 +01:00
|
|
|
ProvingBlockChainClient, ScheduleInfo, ImportSealedBlock, BroadcastProposalBlock, ImportBlock, StateOrBlock,
|
2018-09-08 04:04:28 +02:00
|
|
|
Call, StateClient, EngineInfo, AccountData, BlockChain, BlockProducer, SealedBlockImporter, IoClient,
|
2019-04-02 17:13:55 +02:00
|
|
|
BadBlocks
|
2016-08-17 19:25:02 +02:00
|
|
|
};
|
2019-06-28 10:18:18 +02:00
|
|
|
use engines::Engine;
|
2018-09-24 12:28:54 +02:00
|
|
|
use error::{Error, EthcoreResult};
|
2019-01-04 14:05:46 +01:00
|
|
|
use executed::CallError;
|
|
|
|
use executive::Executed;
|
|
|
|
use journaldb;
|
2018-06-12 08:22:54 +02:00
|
|
|
use miner::{self, Miner, MinerService};
|
2016-06-20 10:28:38 +02:00
|
|
|
use spec::Spec;
|
2019-01-04 14:05:46 +01:00
|
|
|
use state::StateInfo;
|
|
|
|
use state_db::StateDB;
|
|
|
|
use trace::LocalizedTrace;
|
2019-04-02 17:13:55 +02:00
|
|
|
use verification::queue::QueueInfo as BlockQueueInfo;
|
2018-08-02 11:20:46 +02:00
|
|
|
use verification::queue::kind::blocks::Unverified;
|
2016-03-10 20:27:50 +01:00
|
|
|
|
2016-03-10 21:17:58 +01:00
|
|
|
/// Test client.
|
2016-03-10 20:27:50 +01:00
|
|
|
pub struct TestBlockChainClient {
|
2016-03-10 21:17:58 +01:00
|
|
|
/// Blocks.
|
2016-03-10 20:27:50 +01:00
|
|
|
pub blocks: RwLock<HashMap<H256, Bytes>>,
|
2016-03-10 21:17:58 +01:00
|
|
|
/// Mapping of numbers to hashes.
|
2017-06-19 11:41:46 +02:00
|
|
|
pub numbers: RwLock<HashMap<usize, H256>>,
|
2016-03-10 21:17:58 +01:00
|
|
|
/// Genesis block hash.
|
2016-03-10 20:27:50 +01:00
|
|
|
pub genesis_hash: H256,
|
2016-03-10 21:17:58 +01:00
|
|
|
/// Last block hash.
|
2016-03-10 20:27:50 +01:00
|
|
|
pub last_hash: RwLock<H256>,
|
2016-10-10 23:05:41 +02:00
|
|
|
/// Extra data do set for each block
|
|
|
|
pub extra_data: Bytes,
|
2016-03-10 21:17:58 +01:00
|
|
|
/// Difficulty.
|
2016-03-10 20:27:50 +01:00
|
|
|
pub difficulty: RwLock<U256>,
|
2016-03-12 19:51:24 +01:00
|
|
|
/// Balances.
|
|
|
|
pub balances: RwLock<HashMap<Address, U256>>,
|
2016-04-15 07:38:23 +02:00
|
|
|
/// Nonces.
|
|
|
|
pub nonces: RwLock<HashMap<Address, U256>>,
|
2016-03-13 12:09:30 +01:00
|
|
|
/// Storage.
|
|
|
|
pub storage: RwLock<HashMap<(Address, H256), H256>>,
|
2016-03-13 14:45:39 +01:00
|
|
|
/// Code.
|
|
|
|
pub code: RwLock<HashMap<Address, Bytes>>,
|
2016-03-20 11:34:19 +01:00
|
|
|
/// Execution result.
|
2016-08-04 18:17:21 +02:00
|
|
|
pub execution_result: RwLock<Option<Result<Executed, CallError>>>,
|
2016-03-21 11:47:50 +01:00
|
|
|
/// Transaction receipts.
|
2016-12-09 23:01:43 +01:00
|
|
|
pub receipts: RwLock<HashMap<TransactionId, LocalizedReceipt>>,
|
2016-09-21 12:51:10 +02:00
|
|
|
/// Logs
|
|
|
|
pub logs: RwLock<Vec<LocalizedLogEntry>>,
|
2018-08-13 09:47:10 +02:00
|
|
|
/// Should return errors on logs.
|
|
|
|
pub error_on_logs: RwLock<Option<BlockId>>,
|
2016-03-24 17:07:54 +01:00
|
|
|
/// Block queue size.
|
|
|
|
pub queue_size: AtomicUsize,
|
2016-05-31 22:24:32 +02:00
|
|
|
/// Miner
|
|
|
|
pub miner: Arc<Miner>,
|
2016-07-09 11:23:06 +02:00
|
|
|
/// Spec
|
|
|
|
pub spec: Spec,
|
2016-08-23 17:07:00 +02:00
|
|
|
/// Timestamp assigned to latest sealed block
|
|
|
|
pub latest_block_timestamp: RwLock<u64>,
|
2016-10-31 17:32:53 +01:00
|
|
|
/// Ancient block info.
|
|
|
|
pub ancient_block: RwLock<Option<(H256, u64)>>,
|
|
|
|
/// First block info.
|
|
|
|
pub first_block: RwLock<Option<(H256, u64)>>,
|
2016-12-14 20:07:33 +01:00
|
|
|
/// Traces to return
|
|
|
|
pub traces: RwLock<Option<Vec<LocalizedTrace>>>,
|
2016-12-23 18:43:40 +01:00
|
|
|
/// Pruning history size to report.
|
|
|
|
pub history: RwLock<Option<u64>>,
|
2018-04-03 16:49:23 +02:00
|
|
|
/// Is disabled
|
|
|
|
pub disabled: AtomicBool,
|
2016-03-10 20:27:50 +01:00
|
|
|
}
|
|
|
|
|
2016-03-10 21:17:58 +01:00
|
|
|
/// Used for generating test client blocks.
|
2018-10-03 12:35:10 +02:00
|
|
|
#[derive(Clone, Copy)]
|
2016-03-10 20:27:50 +01:00
|
|
|
pub enum EachBlockWith {
|
2016-03-10 21:17:58 +01:00
|
|
|
/// Plain block.
|
2016-03-10 20:27:50 +01:00
|
|
|
Nothing,
|
2016-03-10 21:17:58 +01:00
|
|
|
/// Block with an uncle.
|
2016-03-10 20:27:50 +01:00
|
|
|
Uncle,
|
2016-03-10 21:17:58 +01:00
|
|
|
/// Block with a transaction.
|
2016-03-10 20:27:50 +01:00
|
|
|
Transaction,
|
2019-01-04 19:58:21 +01:00
|
|
|
/// Block with multiple transactions.
|
|
|
|
Transactions(usize),
|
2016-03-10 21:17:58 +01:00
|
|
|
/// Block with an uncle and transaction.
|
2016-03-10 20:27:50 +01:00
|
|
|
UncleAndTransaction
|
|
|
|
}
|
|
|
|
|
2016-03-12 10:07:55 +01:00
|
|
|
impl Default for TestBlockChainClient {
|
|
|
|
fn default() -> Self {
|
|
|
|
TestBlockChainClient::new()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-10 20:27:50 +01:00
|
|
|
impl TestBlockChainClient {
|
2016-03-10 21:17:58 +01:00
|
|
|
/// Creates new test client.
|
2016-03-12 10:07:55 +01:00
|
|
|
pub fn new() -> Self {
|
2016-10-10 23:05:41 +02:00
|
|
|
Self::new_with_extra_data(Bytes::new())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates new test client with specified extra data for each block
|
|
|
|
pub fn new_with_extra_data(extra_data: Bytes) -> Self {
|
2016-08-05 23:33:55 +02:00
|
|
|
let spec = Spec::new_test();
|
2016-10-12 19:39:31 +02:00
|
|
|
TestBlockChainClient::new_with_spec_and_extra(spec, extra_data)
|
2016-09-09 11:49:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Create test client with custom spec.
|
|
|
|
pub fn new_with_spec(spec: Spec) -> Self {
|
2016-10-12 19:39:31 +02:00
|
|
|
TestBlockChainClient::new_with_spec_and_extra(spec, Bytes::new())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Create test client with custom spec and extra data.
|
|
|
|
pub fn new_with_spec_and_extra(spec: Spec, extra_data: Bytes) -> Self {
|
2017-01-11 14:39:03 +01:00
|
|
|
let genesis_block = spec.genesis_block();
|
|
|
|
let genesis_hash = spec.genesis_header().hash();
|
|
|
|
|
2016-03-10 20:27:50 +01:00
|
|
|
let mut client = TestBlockChainClient {
|
|
|
|
blocks: RwLock::new(HashMap::new()),
|
|
|
|
numbers: RwLock::new(HashMap::new()),
|
2019-06-03 15:36:21 +02:00
|
|
|
genesis_hash: H256::zero(),
|
2016-10-10 23:05:41 +02:00
|
|
|
extra_data: extra_data,
|
2019-06-03 15:36:21 +02:00
|
|
|
last_hash: RwLock::new(H256::zero()),
|
2017-01-20 12:41:59 +01:00
|
|
|
difficulty: RwLock::new(spec.genesis_header().difficulty().clone()),
|
2016-03-12 19:51:24 +01:00
|
|
|
balances: RwLock::new(HashMap::new()),
|
2016-04-15 07:38:23 +02:00
|
|
|
nonces: RwLock::new(HashMap::new()),
|
2016-03-13 12:09:30 +01:00
|
|
|
storage: RwLock::new(HashMap::new()),
|
2016-03-13 14:45:39 +01:00
|
|
|
code: RwLock::new(HashMap::new()),
|
2016-03-20 11:34:19 +01:00
|
|
|
execution_result: RwLock::new(None),
|
2016-03-21 11:47:50 +01:00
|
|
|
receipts: RwLock::new(HashMap::new()),
|
2016-09-21 12:51:10 +02:00
|
|
|
logs: RwLock::new(Vec::new()),
|
2016-03-24 17:07:54 +01:00
|
|
|
queue_size: AtomicUsize::new(0),
|
2018-04-13 17:34:27 +02:00
|
|
|
miner: Arc::new(Miner::new_for_tests(&spec, None)),
|
2016-08-05 23:33:55 +02:00
|
|
|
spec: spec,
|
2016-08-23 17:07:00 +02:00
|
|
|
latest_block_timestamp: RwLock::new(10_000_000),
|
2016-10-31 17:32:53 +01:00
|
|
|
ancient_block: RwLock::new(None),
|
|
|
|
first_block: RwLock::new(None),
|
2016-12-14 20:07:33 +01:00
|
|
|
traces: RwLock::new(None),
|
2016-12-23 18:43:40 +01:00
|
|
|
history: RwLock::new(None),
|
2018-04-03 16:49:23 +02:00
|
|
|
disabled: AtomicBool::new(false),
|
2018-08-13 09:47:10 +02:00
|
|
|
error_on_logs: RwLock::new(None),
|
2016-03-10 20:27:50 +01:00
|
|
|
};
|
2017-01-11 14:39:03 +01:00
|
|
|
|
|
|
|
// insert genesis hash.
|
|
|
|
client.blocks.get_mut().insert(genesis_hash, genesis_block);
|
|
|
|
client.numbers.get_mut().insert(0, genesis_hash);
|
|
|
|
*client.last_hash.get_mut() = genesis_hash;
|
|
|
|
client.genesis_hash = genesis_hash;
|
2016-03-10 20:27:50 +01:00
|
|
|
client
|
|
|
|
}
|
2016-10-28 16:42:24 +02:00
|
|
|
|
2016-03-21 11:47:50 +01:00
|
|
|
/// Set the transaction receipt result
|
2016-12-09 23:01:43 +01:00
|
|
|
pub fn set_transaction_receipt(&self, id: TransactionId, receipt: LocalizedReceipt) {
|
2016-07-13 19:59:59 +02:00
|
|
|
self.receipts.write().insert(id, receipt);
|
2016-03-21 11:47:50 +01:00
|
|
|
}
|
|
|
|
|
2016-03-20 11:34:19 +01:00
|
|
|
/// Set the execution result.
|
2016-08-04 18:17:21 +02:00
|
|
|
pub fn set_execution_result(&self, result: Result<Executed, CallError>) {
|
2016-07-13 19:59:59 +02:00
|
|
|
*self.execution_result.write() = Some(result);
|
2016-03-20 11:34:19 +01:00
|
|
|
}
|
|
|
|
|
2016-03-13 15:36:03 +01:00
|
|
|
/// Set the balance of account `address` to `balance`.
|
2016-03-14 14:18:29 +01:00
|
|
|
pub fn set_balance(&self, address: Address, balance: U256) {
|
2016-07-13 19:59:59 +02:00
|
|
|
self.balances.write().insert(address, balance);
|
2016-03-12 19:51:24 +01:00
|
|
|
}
|
|
|
|
|
2016-04-15 07:38:23 +02:00
|
|
|
/// Set nonce of account `address` to `nonce`.
|
|
|
|
pub fn set_nonce(&self, address: Address, nonce: U256) {
|
2016-07-13 19:59:59 +02:00
|
|
|
self.nonces.write().insert(address, nonce);
|
2016-04-15 07:38:23 +02:00
|
|
|
}
|
|
|
|
|
2016-03-13 16:35:52 +01:00
|
|
|
/// Set `code` at `address`.
|
2016-03-14 14:18:29 +01:00
|
|
|
pub fn set_code(&self, address: Address, code: Bytes) {
|
2016-07-13 19:59:59 +02:00
|
|
|
self.code.write().insert(address, code);
|
2016-03-13 16:35:52 +01:00
|
|
|
}
|
|
|
|
|
2016-03-13 15:36:03 +01:00
|
|
|
/// Set storage `position` to `value` for account `address`.
|
2016-03-14 14:18:29 +01:00
|
|
|
pub fn set_storage(&self, address: Address, position: H256, value: H256) {
|
2016-07-13 19:59:59 +02:00
|
|
|
self.storage.write().insert((address, position), value);
|
2016-03-13 12:09:30 +01:00
|
|
|
}
|
|
|
|
|
2016-03-24 17:07:54 +01:00
|
|
|
/// Set block queue size for testing
|
|
|
|
pub fn set_queue_size(&self, size: usize) {
|
|
|
|
self.queue_size.store(size, AtomicOrder::Relaxed);
|
|
|
|
}
|
|
|
|
|
2016-08-23 17:07:00 +02:00
|
|
|
/// Set timestamp assigned to latest sealed block
|
|
|
|
pub fn set_latest_block_timestamp(&self, ts: u64) {
|
|
|
|
*self.latest_block_timestamp.write() = ts;
|
|
|
|
}
|
|
|
|
|
2016-09-21 12:51:10 +02:00
|
|
|
/// Set logs to return for each logs call.
|
|
|
|
pub fn set_logs(&self, logs: Vec<LocalizedLogEntry>) {
|
|
|
|
*self.logs.write() = logs;
|
|
|
|
}
|
|
|
|
|
2018-08-13 09:47:10 +02:00
|
|
|
/// Set return errors on logs.
|
|
|
|
pub fn set_error_on_logs(&self, val: Option<BlockId>) {
|
|
|
|
*self.error_on_logs.write() = val;
|
|
|
|
}
|
|
|
|
|
2018-10-03 12:35:10 +02:00
|
|
|
/// Add a block to test client.
|
|
|
|
pub fn add_block<F>(&self, with: EachBlockWith, hook: F)
|
2019-01-04 14:05:46 +01:00
|
|
|
where F: Fn(Header) -> Header
|
2018-10-03 12:35:10 +02:00
|
|
|
{
|
|
|
|
let n = self.numbers.read().len();
|
2016-03-10 20:27:50 +01:00
|
|
|
|
2019-01-04 14:05:46 +01:00
|
|
|
let mut header = Header::new();
|
2018-10-03 12:35:10 +02:00
|
|
|
header.set_difficulty(From::from(n));
|
|
|
|
header.set_parent_hash(self.last_hash.read().clone());
|
|
|
|
header.set_number(n as BlockNumber);
|
|
|
|
header.set_gas_limit(U256::from(1_000_000));
|
|
|
|
header.set_extra_data(self.extra_data.clone());
|
|
|
|
|
|
|
|
header = hook(header);
|
|
|
|
|
|
|
|
let uncles = match with {
|
|
|
|
EachBlockWith::Uncle | EachBlockWith::UncleAndTransaction => {
|
|
|
|
let mut uncles = RlpStream::new_list(1);
|
2019-01-04 14:05:46 +01:00
|
|
|
let mut uncle_header = Header::new();
|
2018-10-03 12:35:10 +02:00
|
|
|
uncle_header.set_difficulty(From::from(n));
|
|
|
|
uncle_header.set_parent_hash(self.last_hash.read().clone());
|
|
|
|
uncle_header.set_number(n as BlockNumber);
|
|
|
|
uncles.append(&uncle_header);
|
|
|
|
header.set_uncles_hash(keccak(uncles.as_raw()));
|
|
|
|
uncles
|
|
|
|
},
|
|
|
|
_ => RlpStream::new_list(0)
|
|
|
|
};
|
|
|
|
let txs = match with {
|
2019-01-04 19:58:21 +01:00
|
|
|
EachBlockWith::Transaction | EachBlockWith::UncleAndTransaction | EachBlockWith::Transactions(_) => {
|
|
|
|
let num_transactions = match with {
|
|
|
|
EachBlockWith::Transactions(num) => num,
|
|
|
|
_ => 1,
|
2018-10-03 12:35:10 +02:00
|
|
|
};
|
2019-01-04 19:58:21 +01:00
|
|
|
let mut txs = RlpStream::new_list(num_transactions);
|
|
|
|
let keypair = Random.generate().unwrap();
|
|
|
|
let mut nonce = U256::zero();
|
|
|
|
|
|
|
|
for _ in 0..num_transactions {
|
|
|
|
// Update nonces value
|
|
|
|
let tx = Transaction {
|
|
|
|
action: Action::Create,
|
|
|
|
value: U256::from(100),
|
|
|
|
data: "3331600055".from_hex().unwrap(),
|
|
|
|
gas: U256::from(100_000),
|
|
|
|
gas_price: U256::from(200_000_000_000u64),
|
|
|
|
nonce: nonce
|
|
|
|
};
|
|
|
|
let signed_tx = tx.sign(keypair.secret(), None);
|
|
|
|
txs.append(&signed_tx);
|
|
|
|
nonce += U256::one();
|
|
|
|
}
|
|
|
|
|
|
|
|
self.nonces.write().insert(keypair.address(), nonce);
|
2018-10-03 12:35:10 +02:00
|
|
|
txs.out()
|
|
|
|
},
|
|
|
|
_ => ::rlp::EMPTY_LIST_RLP.to_vec()
|
|
|
|
};
|
2016-03-10 20:27:50 +01:00
|
|
|
|
2016-05-16 14:41:41 +02:00
|
|
|
let mut rlp = RlpStream::new_list(3);
|
|
|
|
rlp.append(&header);
|
2018-10-03 12:35:10 +02:00
|
|
|
rlp.append_raw(&txs, 1);
|
|
|
|
rlp.append_raw(uncles.as_raw(), 1);
|
|
|
|
let unverified = Unverified::from_rlp(rlp.out()).unwrap();
|
|
|
|
self.import_block(unverified).unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Add a sequence of blocks to test client.
|
|
|
|
pub fn add_blocks(&self, count: usize, with: EachBlockWith) {
|
|
|
|
for _ in 0..count {
|
|
|
|
self.add_block(with, |header| header);
|
|
|
|
}
|
2016-05-16 14:41:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Make a bad block by setting invalid parent hash.
|
2016-12-06 19:23:15 +01:00
|
|
|
pub fn corrupt_block_parent(&self, n: BlockNumber) {
|
2016-12-09 23:01:43 +01:00
|
|
|
let hash = self.block_hash(BlockId::Number(n)).unwrap();
|
2019-01-04 14:05:46 +01:00
|
|
|
let mut header: Header = self.block_header(BlockId::Number(n)).unwrap().decode().expect("decoding failed");
|
2019-06-03 15:36:21 +02:00
|
|
|
header.set_parent_hash(H256::from_low_u64_be(42));
|
2016-03-10 20:27:50 +01:00
|
|
|
let mut rlp = RlpStream::new_list(3);
|
|
|
|
rlp.append(&header);
|
2016-09-01 14:29:59 +02:00
|
|
|
rlp.append_raw(&::rlp::NULL_RLP, 1);
|
|
|
|
rlp.append_raw(&::rlp::NULL_RLP, 1);
|
2016-07-13 19:59:59 +02:00
|
|
|
self.blocks.write().insert(hash, rlp.out());
|
2016-03-10 20:27:50 +01:00
|
|
|
}
|
|
|
|
|
2018-04-03 09:32:38 +02:00
|
|
|
/// Get block hash with `delta` as offset from the most recent blocks.
|
2016-03-10 20:27:50 +01:00
|
|
|
pub fn block_hash_delta_minus(&mut self, delta: usize) -> H256 {
|
2016-07-13 19:59:59 +02:00
|
|
|
let blocks_read = self.numbers.read();
|
2016-03-10 20:27:50 +01:00
|
|
|
let index = blocks_read.len() - delta;
|
|
|
|
blocks_read[&index].clone()
|
|
|
|
}
|
|
|
|
|
2016-12-09 23:01:43 +01:00
|
|
|
fn block_hash(&self, id: BlockId) -> Option<H256> {
|
2016-03-10 20:27:50 +01:00
|
|
|
match id {
|
2016-12-09 23:01:43 +01:00
|
|
|
BlockId::Hash(hash) => Some(hash),
|
|
|
|
BlockId::Number(n) => self.numbers.read().get(&(n as usize)).cloned(),
|
|
|
|
BlockId::Earliest => self.numbers.read().get(&0).cloned(),
|
2018-03-03 18:42:13 +01:00
|
|
|
BlockId::Latest => self.numbers.read().get(&(self.numbers.read().len() - 1)).cloned()
|
2016-03-10 20:27:50 +01:00
|
|
|
}
|
|
|
|
}
|
2016-09-01 19:07:58 +02:00
|
|
|
|
2017-01-22 16:15:22 +01:00
|
|
|
/// Inserts a transaction with given gas price to miners transactions queue.
|
|
|
|
pub fn insert_transaction_with_gas_price_to_queue(&self, gas_price: U256) -> H256 {
|
2016-09-01 19:07:58 +02:00
|
|
|
let keypair = Random.generate().unwrap();
|
|
|
|
let tx = Transaction {
|
|
|
|
action: Action::Create,
|
|
|
|
value: U256::from(100),
|
|
|
|
data: "3331600055".from_hex().unwrap(),
|
|
|
|
gas: U256::from(100_000),
|
2017-01-22 16:15:22 +01:00
|
|
|
gas_price: gas_price,
|
2016-09-01 19:07:58 +02:00
|
|
|
nonce: U256::zero()
|
|
|
|
};
|
2016-11-03 22:22:25 +01:00
|
|
|
let signed_tx = tx.sign(keypair.secret(), None);
|
2017-01-22 16:15:22 +01:00
|
|
|
self.set_balance(signed_tx.sender(), 10_000_000_000_000_000_000u64.into());
|
|
|
|
let hash = signed_tx.hash();
|
2017-01-13 09:51:36 +01:00
|
|
|
let res = self.miner.import_external_transactions(self, vec![signed_tx.into()]);
|
2018-04-13 17:34:27 +02:00
|
|
|
let res = res.into_iter().next().unwrap();
|
|
|
|
assert!(res.is_ok());
|
2017-01-22 16:15:22 +01:00
|
|
|
hash
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Inserts a transaction to miners transactions queue.
|
|
|
|
pub fn insert_transaction_to_queue(&self) -> H256 {
|
|
|
|
self.insert_transaction_with_gas_price_to_queue(U256::from(20_000_000_000u64))
|
2016-09-01 19:07:58 +02:00
|
|
|
}
|
2016-12-23 18:43:40 +01:00
|
|
|
|
|
|
|
/// Set reported history size.
|
|
|
|
pub fn set_history(&self, h: Option<u64>) {
|
|
|
|
*self.history.write() = h;
|
|
|
|
}
|
2018-04-03 16:49:23 +02:00
|
|
|
|
|
|
|
/// Returns true if the client has been disabled.
|
|
|
|
pub fn is_disabled(&self) -> bool {
|
|
|
|
self.disabled.load(AtomicOrder::Relaxed)
|
|
|
|
}
|
2016-03-10 20:27:50 +01:00
|
|
|
}
|
|
|
|
|
2018-03-01 19:53:15 +01:00
|
|
|
pub fn get_temp_state_db() -> StateDB {
|
|
|
|
let db = kvdb_memorydb::create(NUM_COLUMNS.unwrap_or(0));
|
2016-10-03 12:02:43 +02:00
|
|
|
let journal_db = journaldb::new(Arc::new(db), journaldb::Algorithm::EarlyMerge, COL_STATE);
|
2018-03-01 19:53:15 +01:00
|
|
|
StateDB::new(journal_db, 1024 * 1024)
|
2016-07-09 11:23:06 +02:00
|
|
|
}
|
|
|
|
|
2018-03-03 18:42:13 +01:00
|
|
|
impl ReopenBlock for TestBlockChainClient {
|
|
|
|
fn reopen_block(&self, block: ClosedBlock) -> OpenBlock {
|
|
|
|
block.reopen(&*self.spec.engine)
|
2016-10-28 16:42:24 +02:00
|
|
|
}
|
2018-03-03 18:42:13 +01:00
|
|
|
}
|
2016-10-28 16:42:24 +02:00
|
|
|
|
2018-03-03 18:42:13 +01:00
|
|
|
impl PrepareOpenBlock for TestBlockChainClient {
|
2018-07-16 13:53:55 +02:00
|
|
|
fn prepare_open_block(&self, author: Address, gas_range_target: (U256, U256), extra_data: Bytes) -> Result<OpenBlock, Error> {
|
2016-08-10 16:29:40 +02:00
|
|
|
let engine = &*self.spec.engine;
|
2016-07-09 11:23:06 +02:00
|
|
|
let genesis_header = self.spec.genesis_header();
|
2018-03-01 19:53:15 +01:00
|
|
|
let db = self.spec.ensure_db_good(get_temp_state_db(), &Default::default()).unwrap();
|
2016-07-14 12:16:53 +02:00
|
|
|
|
2016-07-09 11:23:06 +02:00
|
|
|
let last_hashes = vec![genesis_header.hash()];
|
2016-08-03 15:31:00 +02:00
|
|
|
let mut open_block = OpenBlock::new(
|
2016-08-10 16:29:40 +02:00
|
|
|
engine,
|
2016-07-09 11:23:06 +02:00
|
|
|
Default::default(),
|
|
|
|
false,
|
|
|
|
db,
|
|
|
|
&genesis_header,
|
2016-08-03 22:03:40 +02:00
|
|
|
Arc::new(last_hashes),
|
2016-08-03 15:31:00 +02:00
|
|
|
author,
|
|
|
|
gas_range_target,
|
2017-06-28 13:17:36 +02:00
|
|
|
extra_data,
|
|
|
|
false,
|
2018-07-16 13:53:55 +02:00
|
|
|
)?;
|
2018-04-05 10:11:21 +02:00
|
|
|
// TODO [todr] Override timestamp for predictability
|
2016-08-23 17:07:00 +02:00
|
|
|
open_block.set_timestamp(*self.latest_block_timestamp.read());
|
2018-07-16 13:53:55 +02:00
|
|
|
Ok(open_block)
|
2016-05-31 16:41:15 +02:00
|
|
|
}
|
2018-03-03 18:42:13 +01:00
|
|
|
}
|
2016-07-01 12:26:44 +02:00
|
|
|
|
2018-03-03 18:42:13 +01:00
|
|
|
impl ScheduleInfo for TestBlockChainClient {
|
|
|
|
fn latest_schedule(&self) -> Schedule {
|
|
|
|
Schedule::new_post_eip150(24576, true, true, true)
|
2017-07-10 13:36:42 +02:00
|
|
|
}
|
2018-03-03 18:42:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ImportSealedBlock for TestBlockChainClient {
|
2018-09-24 12:28:54 +02:00
|
|
|
fn import_sealed_block(&self, _block: SealedBlock) -> EthcoreResult<H256> {
|
2019-06-03 15:36:21 +02:00
|
|
|
Ok(H256::zero())
|
2018-03-03 18:42:13 +01:00
|
|
|
}
|
|
|
|
}
|
2017-07-10 13:36:42 +02:00
|
|
|
|
2018-03-03 18:42:13 +01:00
|
|
|
impl BlockProducer for TestBlockChainClient {}
|
|
|
|
|
|
|
|
impl BroadcastProposalBlock for TestBlockChainClient {
|
|
|
|
fn broadcast_proposal_block(&self, _block: SealedBlock) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SealedBlockImporter for TestBlockChainClient {}
|
|
|
|
|
2018-04-13 17:34:27 +02:00
|
|
|
impl ::miner::TransactionVerifierClient for TestBlockChainClient {}
|
|
|
|
impl ::miner::BlockChainClient for TestBlockChainClient {}
|
2016-06-29 21:49:12 +02:00
|
|
|
|
2018-03-03 18:42:13 +01:00
|
|
|
impl Nonce for TestBlockChainClient {
|
|
|
|
fn nonce(&self, address: &Address, id: BlockId) -> Option<U256> {
|
|
|
|
match id {
|
|
|
|
BlockId::Latest => Some(self.nonces.read().get(address).cloned().unwrap_or(self.spec.params().account_start_nonce)),
|
|
|
|
_ => None,
|
|
|
|
}
|
2016-06-29 21:49:12 +02:00
|
|
|
}
|
2016-12-08 12:03:34 +01:00
|
|
|
|
2018-03-03 18:42:13 +01:00
|
|
|
fn latest_nonce(&self, address: &Address) -> U256 {
|
|
|
|
self.nonce(address, BlockId::Latest).unwrap()
|
|
|
|
}
|
2016-05-31 16:41:15 +02:00
|
|
|
}
|
|
|
|
|
2018-03-03 18:42:13 +01:00
|
|
|
impl Balance for TestBlockChainClient {
|
|
|
|
fn balance(&self, address: &Address, state: StateOrBlock) -> Option<U256> {
|
|
|
|
match state {
|
|
|
|
StateOrBlock::Block(BlockId::Latest) | StateOrBlock::State(_) => Some(self.balances.read().get(address).cloned().unwrap_or_else(U256::zero)),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn latest_balance(&self, address: &Address) -> U256 {
|
|
|
|
self.balance(address, BlockId::Latest.into()).unwrap()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AccountData for TestBlockChainClient {}
|
|
|
|
|
|
|
|
impl ChainInfo for TestBlockChainClient {
|
|
|
|
fn chain_info(&self) -> BlockChainInfo {
|
|
|
|
let number = self.blocks.read().len() as BlockNumber - 1;
|
|
|
|
BlockChainInfo {
|
|
|
|
total_difficulty: *self.difficulty.read(),
|
|
|
|
pending_total_difficulty: *self.difficulty.read(),
|
|
|
|
genesis_hash: self.genesis_hash.clone(),
|
|
|
|
best_block_hash: self.last_hash.read().clone(),
|
|
|
|
best_block_number: number,
|
|
|
|
best_block_timestamp: number,
|
|
|
|
first_block_hash: self.first_block.read().as_ref().map(|x| x.0),
|
|
|
|
first_block_number: self.first_block.read().as_ref().map(|x| x.1),
|
|
|
|
ancient_block_hash: self.ancient_block.read().as_ref().map(|x| x.0),
|
|
|
|
ancient_block_number: self.ancient_block.read().as_ref().map(|x| x.1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl BlockInfo for TestBlockChainClient {
|
|
|
|
fn block_header(&self, id: BlockId) -> Option<encoded::Header> {
|
|
|
|
self.block_hash(id)
|
2018-04-16 15:52:12 +02:00
|
|
|
.and_then(|hash| self.blocks.read().get(&hash).map(|r| view!(BlockView, r).header_rlp().as_raw().to_vec()))
|
2018-03-03 18:42:13 +01:00
|
|
|
.map(encoded::Header::new)
|
|
|
|
}
|
|
|
|
|
2018-04-03 10:01:28 +02:00
|
|
|
fn best_block_header(&self) -> Header {
|
2018-03-03 18:42:13 +01:00
|
|
|
self.block_header(BlockId::Hash(self.chain_info().best_block_hash))
|
|
|
|
.expect("Best block always has header.")
|
2018-04-03 10:01:28 +02:00
|
|
|
.decode()
|
2018-05-09 12:05:56 +02:00
|
|
|
.expect("decoding failed")
|
2018-03-03 18:42:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn block(&self, id: BlockId) -> Option<encoded::Block> {
|
|
|
|
self.block_hash(id)
|
|
|
|
.and_then(|hash| self.blocks.read().get(&hash).cloned())
|
|
|
|
.map(encoded::Block::new)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn code_hash(&self, address: &Address, id: BlockId) -> Option<H256> {
|
|
|
|
match id {
|
|
|
|
BlockId::Latest => self.code.read().get(address).map(|c| keccak(&c)),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl CallContract for TestBlockChainClient {
|
|
|
|
fn call_contract(&self, _id: BlockId, _address: Address, _data: Bytes) -> Result<Bytes, String> { Ok(vec![]) }
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TransactionInfo for TestBlockChainClient {
|
|
|
|
fn transaction_block(&self, _id: TransactionId) -> Option<H256> {
|
|
|
|
None // Simple default.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl BlockChain for TestBlockChainClient {}
|
|
|
|
|
|
|
|
impl RegistryInfo for TestBlockChainClient {
|
|
|
|
fn registry_address(&self, _name: String, _block: BlockId) -> Option<Address> { None }
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ImportBlock for TestBlockChainClient {
|
2018-09-24 12:28:54 +02:00
|
|
|
fn import_block(&self, unverified: Unverified) -> EthcoreResult<H256> {
|
2018-08-02 11:20:46 +02:00
|
|
|
let header = unverified.header;
|
2018-03-03 18:42:13 +01:00
|
|
|
let h = header.hash();
|
|
|
|
let number: usize = header.number() as usize;
|
|
|
|
if number > self.blocks.read().len() {
|
|
|
|
panic!("Unexpected block number. Expected {}, got {}", self.blocks.read().len(), number);
|
|
|
|
}
|
|
|
|
if number > 0 {
|
|
|
|
match self.blocks.read().get(header.parent_hash()) {
|
|
|
|
Some(parent) => {
|
2018-04-16 15:52:12 +02:00
|
|
|
let parent = view!(BlockView, parent).header();
|
2018-03-03 18:42:13 +01:00
|
|
|
if parent.number() != (header.number() - 1) {
|
|
|
|
panic!("Unexpected block parent");
|
|
|
|
}
|
|
|
|
},
|
|
|
|
None => {
|
|
|
|
panic!("Unknown block parent {:?} for block {}", header.parent_hash(), number);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let len = self.numbers.read().len();
|
|
|
|
if number == len {
|
|
|
|
{
|
|
|
|
let mut difficulty = self.difficulty.write();
|
|
|
|
*difficulty = *difficulty + header.difficulty().clone();
|
|
|
|
}
|
|
|
|
mem::replace(&mut *self.last_hash.write(), h.clone());
|
2018-08-02 11:20:46 +02:00
|
|
|
self.blocks.write().insert(h.clone(), unverified.bytes);
|
2018-03-03 18:42:13 +01:00
|
|
|
self.numbers.write().insert(number, h.clone());
|
|
|
|
let mut parent_hash = header.parent_hash().clone();
|
|
|
|
if number > 0 {
|
|
|
|
let mut n = number - 1;
|
|
|
|
while n > 0 && self.numbers.read()[&n] != parent_hash {
|
|
|
|
*self.numbers.write().get_mut(&n).unwrap() = parent_hash.clone();
|
|
|
|
n -= 1;
|
2018-04-16 15:52:12 +02:00
|
|
|
parent_hash = view!(BlockView, &self.blocks.read()[&parent_hash]).header().parent_hash().clone();
|
2018-03-03 18:42:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2018-08-02 11:20:46 +02:00
|
|
|
self.blocks.write().insert(h.clone(), unverified.bytes);
|
2018-03-03 18:42:13 +01:00
|
|
|
}
|
|
|
|
Ok(h)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Call for TestBlockChainClient {
|
|
|
|
// State will not be used by test client anyway, since all methods that accept state are mocked
|
|
|
|
type State = ();
|
|
|
|
|
|
|
|
fn call(&self, _t: &SignedTransaction, _analytics: CallAnalytics, _state: &mut Self::State, _header: &Header) -> Result<Executed, CallError> {
|
2016-08-04 18:17:21 +02:00
|
|
|
self.execution_result.read().clone().unwrap()
|
2016-03-19 21:37:11 +01:00
|
|
|
}
|
|
|
|
|
2018-03-03 18:42:13 +01:00
|
|
|
fn call_many(&self, txs: &[(SignedTransaction, CallAnalytics)], state: &mut Self::State, header: &Header) -> Result<Vec<Executed>, CallError> {
|
2017-08-04 15:58:14 +02:00
|
|
|
let mut res = Vec::with_capacity(txs.len());
|
|
|
|
for &(ref tx, analytics) in txs {
|
2018-03-03 18:42:13 +01:00
|
|
|
res.push(self.call(tx, analytics, state, header)?);
|
2017-08-04 15:58:14 +02:00
|
|
|
}
|
|
|
|
Ok(res)
|
|
|
|
}
|
|
|
|
|
2018-03-03 18:42:13 +01:00
|
|
|
fn estimate_gas(&self, _t: &SignedTransaction, _state: &Self::State, _header: &Header) -> Result<U256, CallError> {
|
2017-01-10 18:56:41 +01:00
|
|
|
Ok(21000.into())
|
|
|
|
}
|
2018-03-03 18:42:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl StateInfo for () {
|
2018-07-02 18:50:05 +02:00
|
|
|
fn nonce(&self, _address: &Address) -> ethtrie::Result<U256> { unimplemented!() }
|
|
|
|
fn balance(&self, _address: &Address) -> ethtrie::Result<U256> { unimplemented!() }
|
|
|
|
fn storage_at(&self, _address: &Address, _key: &H256) -> ethtrie::Result<H256> { unimplemented!() }
|
|
|
|
fn code(&self, _address: &Address) -> ethtrie::Result<Option<Arc<Bytes>>> { unimplemented!() }
|
2018-03-03 18:42:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl StateClient for TestBlockChainClient {
|
|
|
|
// State will not be used by test client anyway, since all methods that accept state are mocked
|
|
|
|
type State = ();
|
|
|
|
|
|
|
|
fn latest_state(&self) -> Self::State {
|
|
|
|
()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn state_at(&self, _id: BlockId) -> Option<Self::State> {
|
|
|
|
Some(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl EngineInfo for TestBlockChainClient {
|
2019-06-28 10:18:18 +02:00
|
|
|
fn engine(&self) -> &dyn Engine {
|
2018-03-03 18:42:13 +01:00
|
|
|
unimplemented!()
|
|
|
|
}
|
|
|
|
}
|
2017-01-10 18:56:41 +01:00
|
|
|
|
2018-09-08 04:04:28 +02:00
|
|
|
impl BadBlocks for TestBlockChainClient {
|
|
|
|
fn bad_blocks(&self) -> Vec<(Unverified, String)> {
|
|
|
|
vec![
|
|
|
|
(Unverified {
|
|
|
|
header: Default::default(),
|
|
|
|
transactions: vec![],
|
|
|
|
uncles: vec![],
|
|
|
|
bytes: vec![1, 2, 3],
|
|
|
|
}, "Invalid block".into())
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-03 18:42:13 +01:00
|
|
|
impl BlockChainClient for TestBlockChainClient {
|
2016-12-09 23:01:43 +01:00
|
|
|
fn replay(&self, _id: TransactionId, _analytics: CallAnalytics) -> Result<Executed, CallError> {
|
2016-08-04 18:17:21 +02:00
|
|
|
self.execution_result.read().clone().unwrap()
|
2016-07-27 21:34:32 +02:00
|
|
|
}
|
|
|
|
|
2019-04-02 17:13:55 +02:00
|
|
|
fn queue_info(&self) -> BlockQueueInfo {
|
|
|
|
BlockQueueInfo {
|
|
|
|
verified_queue_size: self.queue_size.load(AtomicOrder::Relaxed),
|
|
|
|
unverified_queue_size: 0,
|
|
|
|
verifying_queue_size: 0,
|
|
|
|
max_queue_size: 0,
|
|
|
|
max_mem_use: 0,
|
|
|
|
mem_used: 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-14 18:48:35 +02:00
|
|
|
fn replay_block_transactions(&self, _block: BlockId, _analytics: CallAnalytics) -> Result<Box<dyn Iterator<Item = (H256, Executed)>>, CallError> {
|
2019-06-03 15:36:21 +02:00
|
|
|
Ok(Box::new(
|
|
|
|
self.traces
|
|
|
|
.read()
|
|
|
|
.clone()
|
|
|
|
.unwrap()
|
|
|
|
.into_iter()
|
|
|
|
.map(|t| t.transaction_hash.unwrap_or_default())
|
|
|
|
.zip(self.execution_result.read().clone().unwrap().into_iter())
|
|
|
|
))
|
2018-01-10 11:34:34 +01:00
|
|
|
}
|
|
|
|
|
2016-12-09 23:01:43 +01:00
|
|
|
fn block_total_difficulty(&self, _id: BlockId) -> Option<U256> {
|
2016-03-10 20:27:50 +01:00
|
|
|
Some(U256::zero())
|
|
|
|
}
|
|
|
|
|
2016-12-09 23:01:43 +01:00
|
|
|
fn block_hash(&self, id: BlockId) -> Option<H256> {
|
2016-05-16 14:41:41 +02:00
|
|
|
Self::block_hash(self, id)
|
2016-03-10 20:27:50 +01:00
|
|
|
}
|
|
|
|
|
2016-12-09 23:01:43 +01:00
|
|
|
fn storage_root(&self, _address: &Address, _id: BlockId) -> Option<H256> {
|
2016-11-27 11:11:56 +01:00
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2018-03-03 18:42:13 +01:00
|
|
|
fn code(&self, address: &Address, state: StateOrBlock) -> Option<Option<Bytes>> {
|
|
|
|
match state {
|
|
|
|
StateOrBlock::Block(BlockId::Latest) => Some(self.code.read().get(address).cloned()),
|
2016-08-08 17:25:15 +02:00
|
|
|
_ => None,
|
|
|
|
}
|
2016-03-10 20:27:50 +01:00
|
|
|
}
|
|
|
|
|
2018-03-03 18:42:13 +01:00
|
|
|
fn storage_at(&self, address: &Address, position: &H256, state: StateOrBlock) -> Option<H256> {
|
|
|
|
match state {
|
2019-06-03 15:36:21 +02:00
|
|
|
StateOrBlock::Block(BlockId::Latest) =>
|
|
|
|
Some(self.storage.read().get(&(address.clone(), position.clone())).cloned().unwrap_or_default()),
|
2017-08-04 15:58:14 +02:00
|
|
|
_ => None,
|
2016-05-26 11:46:45 +02:00
|
|
|
}
|
2016-03-13 12:09:30 +01:00
|
|
|
}
|
|
|
|
|
2016-12-09 23:01:43 +01:00
|
|
|
fn list_accounts(&self, _id: BlockId, _after: Option<&Address>, _count: u64) -> Option<Vec<Address>> {
|
2016-10-03 11:13:10 +02:00
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2016-12-09 23:01:43 +01:00
|
|
|
fn list_storage(&self, _id: BlockId, _account: &Address, _after: Option<&H256>, _count: u64) -> Option<Vec<H256>> {
|
2016-11-27 11:11:56 +01:00
|
|
|
None
|
|
|
|
}
|
2016-12-09 23:01:43 +01:00
|
|
|
fn transaction(&self, _id: TransactionId) -> Option<LocalizedTransaction> {
|
2016-09-27 12:17:02 +02:00
|
|
|
None // Simple default.
|
2016-03-10 20:27:50 +01:00
|
|
|
}
|
|
|
|
|
2016-12-28 13:44:51 +01:00
|
|
|
fn uncle(&self, _id: UncleId) -> Option<encoded::Header> {
|
2016-09-27 12:17:02 +02:00
|
|
|
None // Simple default.
|
2016-03-10 20:27:50 +01:00
|
|
|
}
|
|
|
|
|
2016-12-09 23:01:43 +01:00
|
|
|
fn uncle_extra_info(&self, _id: UncleId) -> Option<BTreeMap<String, String>> {
|
2016-11-04 17:35:02 +01:00
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2016-12-09 23:01:43 +01:00
|
|
|
fn transaction_receipt(&self, id: TransactionId) -> Option<LocalizedReceipt> {
|
2016-07-13 19:59:59 +02:00
|
|
|
self.receipts.read().get(&id).cloned()
|
2016-03-10 20:27:50 +01:00
|
|
|
}
|
|
|
|
|
2018-11-18 00:06:34 +01:00
|
|
|
fn localized_block_receipts(&self, _id: BlockId) -> Option<Vec<LocalizedReceipt>> {
|
2018-09-25 19:06:14 +02:00
|
|
|
Some(self.receipts.read().values().cloned().collect())
|
|
|
|
}
|
|
|
|
|
2018-08-13 09:47:10 +02:00
|
|
|
fn logs(&self, filter: Filter) -> Result<Vec<LocalizedLogEntry>, BlockId> {
|
|
|
|
match self.error_on_logs.read().as_ref() {
|
|
|
|
Some(id) => return Err(id.clone()),
|
|
|
|
None => (),
|
|
|
|
}
|
|
|
|
|
2016-09-21 12:51:10 +02:00
|
|
|
let mut logs = self.logs.read().clone();
|
|
|
|
let len = logs.len();
|
2018-08-13 09:47:10 +02:00
|
|
|
Ok(match filter.limit {
|
2016-09-21 12:51:10 +02:00
|
|
|
Some(limit) if limit <= len => logs.split_off(len - limit),
|
|
|
|
_ => logs,
|
2018-08-13 09:47:10 +02:00
|
|
|
})
|
2016-03-10 20:27:50 +01:00
|
|
|
}
|
|
|
|
|
2016-04-28 21:47:44 +02:00
|
|
|
fn last_hashes(&self) -> LastHashes {
|
|
|
|
unimplemented!();
|
|
|
|
}
|
|
|
|
|
2018-04-03 16:49:23 +02:00
|
|
|
fn block_number(&self, id: BlockId) -> Option<BlockNumber> {
|
|
|
|
match id {
|
|
|
|
BlockId::Number(number) => Some(number),
|
|
|
|
BlockId::Earliest => Some(0),
|
|
|
|
BlockId::Latest => Some(self.chain_info().best_block_number),
|
|
|
|
BlockId::Hash(ref h) =>
|
|
|
|
self.numbers.read().iter().find(|&(_, hash)| hash == h).map(|e| *e.0 as u64)
|
|
|
|
}
|
2016-12-11 02:02:40 +01:00
|
|
|
}
|
|
|
|
|
2016-12-28 13:44:51 +01:00
|
|
|
fn block_body(&self, id: BlockId) -> Option<encoded::Body> {
|
2016-07-13 19:59:59 +02:00
|
|
|
self.block_hash(id).and_then(|hash| self.blocks.read().get(&hash).map(|r| {
|
2018-04-16 15:52:12 +02:00
|
|
|
let block = view!(BlockView, r);
|
2016-03-10 20:27:50 +01:00
|
|
|
let mut stream = RlpStream::new_list(2);
|
2018-03-29 11:19:45 +02:00
|
|
|
stream.append_raw(block.transactions_rlp().as_raw(), 1);
|
|
|
|
stream.append_raw(block.uncles_rlp().as_raw(), 1);
|
2016-12-28 13:44:51 +01:00
|
|
|
encoded::Body::new(stream.out())
|
2016-03-10 20:27:50 +01:00
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
2016-12-09 23:01:43 +01:00
|
|
|
fn block_extra_info(&self, id: BlockId) -> Option<BTreeMap<String, String>> {
|
2016-11-04 17:35:02 +01:00
|
|
|
self.block(id)
|
2016-12-28 13:44:51 +01:00
|
|
|
.map(|block| block.view().header())
|
2016-11-04 17:35:02 +01:00
|
|
|
.map(|header| self.spec.engine.extra_info(&header))
|
|
|
|
}
|
|
|
|
|
2016-12-09 23:01:43 +01:00
|
|
|
fn block_status(&self, id: BlockId) -> BlockStatus {
|
2016-03-10 20:27:50 +01:00
|
|
|
match id {
|
2016-12-09 23:01:43 +01:00
|
|
|
BlockId::Number(number) if (number as usize) < self.blocks.read().len() => BlockStatus::InChain,
|
|
|
|
BlockId::Hash(ref hash) if self.blocks.read().get(hash).is_some() => BlockStatus::InChain,
|
2017-08-04 15:58:14 +02:00
|
|
|
BlockId::Latest | BlockId::Earliest => BlockStatus::InChain,
|
2016-12-23 18:46:17 +01:00
|
|
|
_ => BlockStatus::Unknown,
|
2016-03-10 20:27:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// works only if blocks are one after another 1 -> 2 -> 3
|
|
|
|
fn tree_route(&self, from: &H256, to: &H256) -> Option<TreeRoute> {
|
|
|
|
Some(TreeRoute {
|
2019-06-03 15:36:21 +02:00
|
|
|
ancestor: H256::zero(),
|
2016-03-10 20:27:50 +01:00
|
|
|
index: 0,
|
|
|
|
blocks: {
|
2016-07-13 19:59:59 +02:00
|
|
|
let numbers_read = self.numbers.read();
|
2016-03-10 20:27:50 +01:00
|
|
|
let mut adding = false;
|
|
|
|
|
|
|
|
let mut blocks = Vec::new();
|
2017-02-06 17:21:35 +01:00
|
|
|
for (_, hash) in numbers_read.iter().sorted_by(|tuple1, tuple2| tuple1.0.cmp(tuple2.0)) {
|
2016-03-10 20:27:50 +01:00
|
|
|
if hash == to {
|
|
|
|
if adding {
|
|
|
|
blocks.push(hash.clone());
|
|
|
|
}
|
|
|
|
adding = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if hash == from {
|
|
|
|
adding = true;
|
|
|
|
}
|
|
|
|
if adding {
|
|
|
|
blocks.push(hash.clone());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if adding { Vec::new() } else { blocks }
|
2018-05-16 08:58:01 +02:00
|
|
|
},
|
|
|
|
is_from_route_finalized: false,
|
2016-03-10 20:27:50 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-05-24 21:56:17 +02:00
|
|
|
fn find_uncles(&self, _hash: &H256) -> Option<Vec<H256>> {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2016-03-10 20:27:50 +01:00
|
|
|
// TODO: returns just hashes instead of node state rlp(?)
|
|
|
|
fn state_data(&self, hash: &H256) -> Option<Bytes> {
|
|
|
|
// starts with 'f' ?
|
2019-06-03 15:36:21 +02:00
|
|
|
if *hash > H256::from_str("f000000000000000000000000000000000000000000000000000000000000000").unwrap() {
|
2016-03-10 20:27:50 +01:00
|
|
|
let mut rlp = RlpStream::new();
|
|
|
|
rlp.append(&hash.clone());
|
|
|
|
return Some(rlp.out());
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2018-11-18 00:06:34 +01:00
|
|
|
fn block_receipts(&self, hash: &H256) -> Option<BlockReceipts> {
|
2016-03-10 20:27:50 +01:00
|
|
|
// starts with 'f' ?
|
2019-06-03 15:36:21 +02:00
|
|
|
if *hash > H256::from_str("f000000000000000000000000000000000000000000000000000000000000000").unwrap() {
|
2016-03-12 19:23:17 +01:00
|
|
|
let receipt = BlockReceipts::new(vec![Receipt::new(
|
2017-09-21 10:11:53 +02:00
|
|
|
TransactionOutcome::StateRoot(H256::zero()),
|
2016-03-10 20:27:50 +01:00
|
|
|
U256::zero(),
|
2016-03-12 19:23:17 +01:00
|
|
|
vec![])]);
|
2018-11-18 00:06:34 +01:00
|
|
|
return Some(receipt);
|
2016-03-10 20:27:50 +01:00
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
|
|
|
fn clear_queue(&self) {
|
|
|
|
}
|
|
|
|
|
2016-05-02 12:17:30 +02:00
|
|
|
fn filter_traces(&self, _filter: TraceFilter) -> Option<Vec<LocalizedTrace>> {
|
2016-12-14 20:07:33 +01:00
|
|
|
self.traces.read().clone()
|
2016-05-02 12:17:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn trace(&self, _trace: TraceId) -> Option<LocalizedTrace> {
|
2016-12-14 20:07:33 +01:00
|
|
|
self.traces.read().clone().and_then(|vec| vec.into_iter().next())
|
2016-05-02 12:17:30 +02:00
|
|
|
}
|
|
|
|
|
2016-12-09 23:01:43 +01:00
|
|
|
fn transaction_traces(&self, _trace: TransactionId) -> Option<Vec<LocalizedTrace>> {
|
2016-12-14 20:07:33 +01:00
|
|
|
self.traces.read().clone()
|
2016-05-02 12:17:30 +02:00
|
|
|
}
|
|
|
|
|
2016-12-09 23:01:43 +01:00
|
|
|
fn block_traces(&self, _trace: BlockId) -> Option<Vec<LocalizedTrace>> {
|
2016-12-14 20:07:33 +01:00
|
|
|
self.traces.read().clone()
|
2016-05-02 12:17:30 +02:00
|
|
|
}
|
2016-05-31 19:52:53 +02:00
|
|
|
|
2018-08-02 12:58:02 +02:00
|
|
|
fn transactions_to_propagate(&self) -> Vec<Arc<VerifiedTransaction>> {
|
|
|
|
self.miner.ready_transactions(self, 4096, miner::PendingOrdering::Priority)
|
2016-05-31 19:52:53 +02:00
|
|
|
}
|
2016-10-31 16:58:35 +01:00
|
|
|
|
2017-08-21 13:46:58 +02:00
|
|
|
fn signing_chain_id(&self) -> Option<u64> { None }
|
2016-11-03 22:22:25 +01:00
|
|
|
|
2016-10-31 16:58:35 +01:00
|
|
|
fn mode(&self) -> Mode { Mode::Active }
|
|
|
|
|
|
|
|
fn set_mode(&self, _: Mode) { unimplemented!(); }
|
2016-11-09 23:25:54 +01:00
|
|
|
|
2017-03-13 12:10:53 +01:00
|
|
|
fn spec_name(&self) -> String { "foundation".into() }
|
|
|
|
|
2019-03-04 20:24:53 +01:00
|
|
|
fn set_spec_name(&self, _: String) -> Result<(), ()> { unimplemented!(); }
|
2017-03-13 12:10:53 +01:00
|
|
|
|
2018-04-03 16:49:23 +02:00
|
|
|
fn disable(&self) { self.disabled.store(true, AtomicOrder::Relaxed); }
|
2016-12-11 16:52:41 +01:00
|
|
|
|
2016-11-09 23:25:54 +01:00
|
|
|
fn pruning_info(&self) -> PruningInfo {
|
2017-01-20 13:25:53 +01:00
|
|
|
let best_num = self.chain_info().best_block_number;
|
2016-11-09 23:25:54 +01:00
|
|
|
PruningInfo {
|
|
|
|
earliest_chain: 1,
|
2017-01-20 13:25:53 +01:00
|
|
|
earliest_state: self.history.read().as_ref().map(|x| best_num - x).unwrap_or(0),
|
2016-11-09 23:25:54 +01:00
|
|
|
}
|
|
|
|
}
|
2016-12-10 23:58:39 +01:00
|
|
|
|
2018-04-13 17:34:27 +02:00
|
|
|
fn transact_contract(&self, address: Address, data: Bytes) -> Result<(), transaction::Error> {
|
2017-01-24 10:03:58 +01:00
|
|
|
let transaction = Transaction {
|
2018-04-13 17:34:27 +02:00
|
|
|
nonce: self.latest_nonce(&self.miner.authoring_params().author),
|
2017-01-24 10:03:58 +01:00
|
|
|
action: Action::Call(address),
|
|
|
|
gas: self.spec.gas_limit,
|
|
|
|
gas_price: U256::zero(),
|
|
|
|
value: U256::default(),
|
|
|
|
data: data,
|
|
|
|
};
|
2017-08-21 13:46:58 +02:00
|
|
|
let chain_id = Some(self.spec.chain_id());
|
|
|
|
let sig = self.spec.engine.sign(transaction.hash(chain_id)).unwrap();
|
|
|
|
let signed = SignedTransaction::new(transaction.with_signature(sig, chain_id)).unwrap();
|
2017-01-24 10:03:58 +01:00
|
|
|
self.miner.import_own_transaction(self, signed.into())
|
|
|
|
}
|
|
|
|
|
2016-12-10 23:58:39 +01:00
|
|
|
fn registrar_address(&self) -> Option<Address> { None }
|
2016-03-10 20:27:50 +01:00
|
|
|
}
|
2017-01-10 12:23:59 +01:00
|
|
|
|
2018-05-09 08:49:34 +02:00
|
|
|
impl IoClient for TestBlockChainClient {
|
|
|
|
fn queue_transactions(&self, transactions: Vec<Bytes>, _peer_id: usize) {
|
|
|
|
// import right here
|
|
|
|
let txs = transactions.into_iter().filter_map(|bytes| Rlp::new(&bytes).as_val().ok()).collect();
|
|
|
|
self.miner.import_external_transactions(self, txs);
|
|
|
|
}
|
|
|
|
|
2018-09-24 12:28:54 +02:00
|
|
|
fn queue_ancient_block(&self, unverified: Unverified, _r: Bytes) -> EthcoreResult<H256> {
|
2018-08-02 11:20:46 +02:00
|
|
|
self.import_block(unverified)
|
2018-05-09 08:49:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn queue_consensus_message(&self, message: Bytes) {
|
|
|
|
self.spec.engine.handle_message(&message).unwrap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-11 14:39:03 +01:00
|
|
|
impl ProvingBlockChainClient for TestBlockChainClient {
|
2017-03-23 13:17:05 +01:00
|
|
|
fn prove_storage(&self, _: H256, _: H256, _: BlockId) -> Option<(Vec<Bytes>, H256)> {
|
|
|
|
None
|
2017-01-11 14:39:03 +01:00
|
|
|
}
|
|
|
|
|
2017-03-23 13:17:05 +01:00
|
|
|
fn prove_account(&self, _: H256, _: BlockId) -> Option<(Vec<Bytes>, BasicAccount)> {
|
|
|
|
None
|
2017-01-11 14:39:03 +01:00
|
|
|
}
|
2017-03-08 14:39:44 +01:00
|
|
|
|
2017-04-19 14:58:19 +02:00
|
|
|
fn prove_transaction(&self, _: SignedTransaction, _: BlockId) -> Option<(Bytes, Vec<DBValue>)> {
|
2017-03-08 14:39:44 +01:00
|
|
|
None
|
|
|
|
}
|
2017-09-05 17:54:05 +02:00
|
|
|
|
|
|
|
fn epoch_signal(&self, _: H256) -> Option<Vec<u8>> {
|
|
|
|
None
|
|
|
|
}
|
2017-01-11 14:39:03 +01:00
|
|
|
}
|
|
|
|
|
2017-09-05 17:54:05 +02:00
|
|
|
impl super::traits::EngineClient for TestBlockChainClient {
|
2017-01-10 12:23:59 +01:00
|
|
|
fn update_sealing(&self) {
|
|
|
|
self.miner.update_sealing(self)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn submit_seal(&self, block_hash: H256, seal: Vec<Bytes>) {
|
2018-04-13 17:34:27 +02:00
|
|
|
let import = self.miner.submit_seal(block_hash, seal).and_then(|block| self.import_sealed_block(block));
|
|
|
|
if let Err(err) = import {
|
|
|
|
warn!(target: "poa", "Wrong internal seal submission! {:?}", err);
|
2017-01-10 12:23:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn broadcast_consensus_message(&self, _message: Bytes) {}
|
2017-06-28 13:17:36 +02:00
|
|
|
|
|
|
|
fn epoch_transition_for(&self, _block_hash: H256) -> Option<::engines::EpochTransition> {
|
|
|
|
None
|
|
|
|
}
|
2017-09-05 17:54:05 +02:00
|
|
|
|
2019-06-14 18:48:35 +02:00
|
|
|
fn as_full_client(&self) -> Option<&dyn BlockChainClient> { Some(self) }
|
2017-09-05 17:54:05 +02:00
|
|
|
|
|
|
|
fn block_number(&self, id: BlockId) -> Option<BlockNumber> {
|
|
|
|
BlockChainClient::block_number(self, id)
|
|
|
|
}
|
2018-02-15 01:39:29 +01:00
|
|
|
|
2019-01-04 14:05:46 +01:00
|
|
|
fn block_header(&self, id: BlockId) -> Option<encoded::Header> {
|
2018-02-15 01:39:29 +01:00
|
|
|
BlockChainClient::block_header(self, id)
|
|
|
|
}
|
2017-01-10 12:23:59 +01:00
|
|
|
}
|