2016-03-10 20:27:50 +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-03-10 21:17:58 +01:00
|
|
|
//! Test client.
|
|
|
|
|
2016-03-24 17:07:54 +01:00
|
|
|
use std::sync::atomic::{AtomicUsize, Ordering as AtomicOrder};
|
2016-03-10 20:27:50 +01:00
|
|
|
use util::*;
|
2016-03-11 14:26:23 +01:00
|
|
|
use transaction::{Transaction, LocalizedTransaction, SignedTransaction, Action};
|
2016-03-10 20:27:50 +01:00
|
|
|
use blockchain::TreeRoute;
|
2016-06-02 12:28:09 +02:00
|
|
|
use client::{BlockChainClient, MiningBlockChainClient, BlockChainInfo, BlockStatus, BlockID, TransactionID, UncleID, TraceId, TraceFilter, LastHashes, CallAnalytics};
|
2016-03-10 20:27:50 +01:00
|
|
|
use header::{Header as BlockHeader, BlockNumber};
|
|
|
|
use filter::Filter;
|
|
|
|
use log_entry::LocalizedLogEntry;
|
2016-03-20 18:44:57 +01:00
|
|
|
use receipt::{Receipt, LocalizedReceipt};
|
2016-05-26 18:24:51 +02:00
|
|
|
use blockchain::extras::BlockReceipts;
|
2016-03-11 14:26:23 +01:00
|
|
|
use error::{ImportResult};
|
2016-05-19 00:44:49 +02:00
|
|
|
use evm::Factory as EvmFactory;
|
2016-06-01 12:44:11 +02:00
|
|
|
use miner::{Miner, MinerService};
|
2016-06-20 10:28:38 +02:00
|
|
|
use spec::Spec;
|
2016-03-14 11:06:28 +01:00
|
|
|
|
2016-03-10 20:27:50 +01:00
|
|
|
use block_queue::BlockQueueInfo;
|
2016-06-29 21:49:12 +02:00
|
|
|
use block::{OpenBlock, SealedBlock};
|
2016-03-19 21:37:11 +01:00
|
|
|
use executive::Executed;
|
2016-05-14 14:28:44 +02:00
|
|
|
use error::{ExecutionError};
|
2016-05-02 12:17:30 +02:00
|
|
|
use trace::LocalizedTrace;
|
2016-03-10 20:27:50 +01:00
|
|
|
|
2016-05-31 19:52:53 +02:00
|
|
|
use miner::{TransactionImportResult, AccountDetails};
|
|
|
|
use error::Error as EthError;
|
|
|
|
|
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.
|
2016-03-10 20:27:50 +01: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-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.
|
|
|
|
pub execution_result: RwLock<Option<Executed>>,
|
2016-03-21 11:47:50 +01:00
|
|
|
/// Transaction receipts.
|
2016-05-19 11:00:32 +02:00
|
|
|
pub receipts: RwLock<HashMap<TransactionID, LocalizedReceipt>>,
|
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-03-10 20:27:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
2016-03-10 21:17:58 +01:00
|
|
|
/// Used for generating test client blocks.
|
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,
|
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-03-10 20:27:50 +01:00
|
|
|
|
|
|
|
let mut client = TestBlockChainClient {
|
|
|
|
blocks: RwLock::new(HashMap::new()),
|
|
|
|
numbers: RwLock::new(HashMap::new()),
|
|
|
|
genesis_hash: H256::new(),
|
|
|
|
last_hash: RwLock::new(H256::new()),
|
|
|
|
difficulty: RwLock::new(From::from(0)),
|
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-03-24 17:07:54 +01:00
|
|
|
queue_size: AtomicUsize::new(0),
|
2016-06-20 10:28:38 +02:00
|
|
|
miner: Arc::new(Miner::with_spec(Spec::new_test())),
|
2016-03-10 20:27:50 +01:00
|
|
|
};
|
|
|
|
client.add_blocks(1, EachBlockWith::Nothing); // add genesis block
|
|
|
|
client.genesis_hash = client.last_hash.read().unwrap().clone();
|
|
|
|
client
|
|
|
|
}
|
|
|
|
|
2016-03-21 11:47:50 +01:00
|
|
|
/// Set the transaction receipt result
|
2016-05-19 11:00:32 +02:00
|
|
|
pub fn set_transaction_receipt(&self, id: TransactionID, receipt: LocalizedReceipt) {
|
2016-03-21 11:47:50 +01:00
|
|
|
self.receipts.write().unwrap().insert(id, receipt);
|
|
|
|
}
|
|
|
|
|
2016-03-20 11:34:19 +01:00
|
|
|
/// Set the execution result.
|
|
|
|
pub fn set_execution_result(&self, result: Executed) {
|
|
|
|
*self.execution_result.write().unwrap() = Some(result);
|
|
|
|
}
|
|
|
|
|
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-03-12 19:51:24 +01:00
|
|
|
self.balances.write().unwrap().insert(address, balance);
|
|
|
|
}
|
|
|
|
|
2016-04-15 07:38:23 +02:00
|
|
|
/// Set nonce of account `address` to `nonce`.
|
|
|
|
pub fn set_nonce(&self, address: Address, nonce: U256) {
|
|
|
|
self.nonces.write().unwrap().insert(address, nonce);
|
|
|
|
}
|
|
|
|
|
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-03-13 16:35:52 +01:00
|
|
|
self.code.write().unwrap().insert(address, code);
|
|
|
|
}
|
|
|
|
|
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-03-13 12:09:30 +01:00
|
|
|
self.storage.write().unwrap().insert((address, position), value);
|
|
|
|
}
|
|
|
|
|
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-03-10 21:17:58 +01:00
|
|
|
/// Add blocks to test client.
|
2016-03-14 14:18:29 +01:00
|
|
|
pub fn add_blocks(&self, count: usize, with: EachBlockWith) {
|
2016-03-10 20:27:50 +01:00
|
|
|
let len = self.numbers.read().unwrap().len();
|
|
|
|
for n in len..(len + count) {
|
|
|
|
let mut header = BlockHeader::new();
|
|
|
|
header.difficulty = From::from(n);
|
|
|
|
header.parent_hash = self.last_hash.read().unwrap().clone();
|
|
|
|
header.number = n as BlockNumber;
|
2016-03-18 10:36:01 +01:00
|
|
|
header.gas_limit = U256::from(1_000_000);
|
2016-03-10 20:27:50 +01:00
|
|
|
let uncles = match with {
|
|
|
|
EachBlockWith::Uncle | EachBlockWith::UncleAndTransaction => {
|
|
|
|
let mut uncles = RlpStream::new_list(1);
|
|
|
|
let mut uncle_header = BlockHeader::new();
|
|
|
|
uncle_header.difficulty = From::from(n);
|
|
|
|
uncle_header.parent_hash = self.last_hash.read().unwrap().clone();
|
|
|
|
uncle_header.number = n as BlockNumber;
|
|
|
|
uncles.append(&uncle_header);
|
|
|
|
header.uncles_hash = uncles.as_raw().sha3();
|
|
|
|
uncles
|
|
|
|
},
|
|
|
|
_ => RlpStream::new_list(0)
|
|
|
|
};
|
|
|
|
let txs = match with {
|
|
|
|
EachBlockWith::Transaction | EachBlockWith::UncleAndTransaction => {
|
|
|
|
let mut txs = RlpStream::new_list(1);
|
|
|
|
let keypair = KeyPair::create().unwrap();
|
2016-04-15 07:38:23 +02:00
|
|
|
// Update nonces value
|
|
|
|
self.nonces.write().unwrap().insert(keypair.address(), U256::one());
|
2016-03-10 20:27:50 +01:00
|
|
|
let tx = Transaction {
|
|
|
|
action: Action::Create,
|
|
|
|
value: U256::from(100),
|
|
|
|
data: "3331600055".from_hex().unwrap(),
|
|
|
|
gas: U256::from(100_000),
|
|
|
|
gas_price: U256::one(),
|
|
|
|
nonce: U256::zero()
|
|
|
|
};
|
|
|
|
let signed_tx = tx.sign(&keypair.secret());
|
|
|
|
txs.append(&signed_tx);
|
|
|
|
txs.out()
|
|
|
|
},
|
2016-06-20 10:44:15 +02:00
|
|
|
_ => rlp::EMPTY_LIST_RLP.to_vec()
|
2016-03-10 20:27:50 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
let mut rlp = RlpStream::new_list(3);
|
|
|
|
rlp.append(&header);
|
|
|
|
rlp.append_raw(&txs, 1);
|
|
|
|
rlp.append_raw(uncles.as_raw(), 1);
|
|
|
|
self.import_block(rlp.as_raw().to_vec()).unwrap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-16 14:41:41 +02:00
|
|
|
/// Make a bad block by setting invalid extra data.
|
2016-03-10 20:27:50 +01:00
|
|
|
pub fn corrupt_block(&mut self, n: BlockNumber) {
|
2016-05-19 11:00:32 +02:00
|
|
|
let hash = self.block_hash(BlockID::Number(n)).unwrap();
|
|
|
|
let mut header: BlockHeader = decode(&self.block_header(BlockID::Number(n)).unwrap());
|
2016-05-16 14:41:41 +02:00
|
|
|
header.extra_data = b"This extra data is way too long to be considered valid".to_vec();
|
|
|
|
let mut rlp = RlpStream::new_list(3);
|
|
|
|
rlp.append(&header);
|
|
|
|
rlp.append_raw(&rlp::NULL_RLP, 1);
|
|
|
|
rlp.append_raw(&rlp::NULL_RLP, 1);
|
|
|
|
self.blocks.write().unwrap().insert(hash, rlp.out());
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Make a bad block by setting invalid parent hash.
|
|
|
|
pub fn corrupt_block_parent(&mut self, n: BlockNumber) {
|
|
|
|
let hash = self.block_hash(BlockID::Number(n)).unwrap();
|
|
|
|
let mut header: BlockHeader = decode(&self.block_header(BlockID::Number(n)).unwrap());
|
|
|
|
header.parent_hash = H256::from(42);
|
2016-03-10 20:27:50 +01:00
|
|
|
let mut rlp = RlpStream::new_list(3);
|
|
|
|
rlp.append(&header);
|
|
|
|
rlp.append_raw(&rlp::NULL_RLP, 1);
|
|
|
|
rlp.append_raw(&rlp::NULL_RLP, 1);
|
|
|
|
self.blocks.write().unwrap().insert(hash, rlp.out());
|
|
|
|
}
|
|
|
|
|
2016-03-10 21:17:58 +01:00
|
|
|
/// TODO:
|
2016-03-10 20:27:50 +01:00
|
|
|
pub fn block_hash_delta_minus(&mut self, delta: usize) -> H256 {
|
|
|
|
let blocks_read = self.numbers.read().unwrap();
|
|
|
|
let index = blocks_read.len() - delta;
|
|
|
|
blocks_read[&index].clone()
|
|
|
|
}
|
|
|
|
|
2016-05-19 11:00:32 +02:00
|
|
|
fn block_hash(&self, id: BlockID) -> Option<H256> {
|
2016-03-10 20:27:50 +01:00
|
|
|
match id {
|
2016-05-19 11:00:32 +02:00
|
|
|
BlockID::Hash(hash) => Some(hash),
|
|
|
|
BlockID::Number(n) => self.numbers.read().unwrap().get(&(n as usize)).cloned(),
|
|
|
|
BlockID::Earliest => self.numbers.read().unwrap().get(&0).cloned(),
|
|
|
|
BlockID::Latest => self.numbers.read().unwrap().get(&(self.numbers.read().unwrap().len() - 1)).cloned()
|
2016-03-10 20:27:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-31 20:33:26 +02:00
|
|
|
impl MiningBlockChainClient for TestBlockChainClient {
|
2016-06-23 14:29:16 +02:00
|
|
|
fn prepare_open_block(&self, _author: Address, _gas_range_target: (U256, U256), _extra_data: Bytes) -> OpenBlock {
|
2016-06-06 14:33:12 +02:00
|
|
|
unimplemented!();
|
2016-05-31 16:41:15 +02:00
|
|
|
}
|
2016-07-01 12:26:44 +02:00
|
|
|
|
2016-06-29 16:23:29 +02:00
|
|
|
fn vm_factory(&self) -> &EvmFactory {
|
|
|
|
unimplemented!();
|
|
|
|
}
|
2016-06-29 21:49:12 +02:00
|
|
|
|
|
|
|
fn import_sealed_block(&self, _block: SealedBlock) -> ImportResult {
|
|
|
|
unimplemented!();
|
|
|
|
}
|
2016-05-31 16:41:15 +02:00
|
|
|
}
|
|
|
|
|
2016-03-10 20:27:50 +01:00
|
|
|
impl BlockChainClient for TestBlockChainClient {
|
2016-05-31 21:03:44 +02:00
|
|
|
fn call(&self, _t: &SignedTransaction, _analytics: CallAnalytics) -> Result<Executed, ExecutionError> {
|
2016-03-20 11:34:19 +01:00
|
|
|
Ok(self.execution_result.read().unwrap().clone().unwrap())
|
2016-03-19 21:37:11 +01:00
|
|
|
}
|
|
|
|
|
2016-05-19 11:00:32 +02:00
|
|
|
fn block_total_difficulty(&self, _id: BlockID) -> Option<U256> {
|
2016-03-10 20:27:50 +01:00
|
|
|
Some(U256::zero())
|
|
|
|
}
|
|
|
|
|
2016-05-16 14:41:41 +02:00
|
|
|
fn block_hash(&self, id: BlockID) -> Option<H256> {
|
|
|
|
Self::block_hash(self, id)
|
2016-03-10 20:27:50 +01:00
|
|
|
}
|
|
|
|
|
2016-05-26 12:40:29 +02:00
|
|
|
fn nonce(&self, address: &Address, id: BlockID) -> Option<U256> {
|
|
|
|
match id {
|
|
|
|
BlockID::Latest => Some(self.nonces.read().unwrap().get(address).cloned().unwrap_or_else(U256::zero)),
|
|
|
|
_ => None,
|
|
|
|
}
|
2016-03-10 20:27:50 +01:00
|
|
|
}
|
|
|
|
|
2016-03-13 14:45:39 +01:00
|
|
|
fn code(&self, address: &Address) -> Option<Bytes> {
|
|
|
|
self.code.read().unwrap().get(address).cloned()
|
2016-03-10 20:27:50 +01:00
|
|
|
}
|
|
|
|
|
2016-05-26 11:46:45 +02:00
|
|
|
fn balance(&self, address: &Address, id: BlockID) -> Option<U256> {
|
|
|
|
if let BlockID::Latest = id {
|
|
|
|
Some(self.balances.read().unwrap().get(address).cloned().unwrap_or_else(U256::zero))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
2016-03-12 19:51:24 +01:00
|
|
|
}
|
|
|
|
|
2016-05-26 11:46:45 +02:00
|
|
|
fn storage_at(&self, address: &Address, position: &H256, id: BlockID) -> Option<H256> {
|
|
|
|
if let BlockID::Latest = id {
|
|
|
|
Some(self.storage.read().unwrap().get(&(address.clone(), position.clone())).cloned().unwrap_or_else(H256::new))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
2016-03-13 12:09:30 +01:00
|
|
|
}
|
|
|
|
|
2016-05-19 11:00:32 +02:00
|
|
|
fn transaction(&self, _id: TransactionID) -> Option<LocalizedTransaction> {
|
2016-03-10 20:27:50 +01:00
|
|
|
unimplemented!();
|
|
|
|
}
|
|
|
|
|
2016-07-01 12:26:44 +02:00
|
|
|
fn uncle(&self, _id: UncleID) -> Option<Bytes> {
|
2016-03-20 17:29:39 +01:00
|
|
|
unimplemented!();
|
2016-03-10 20:27:50 +01:00
|
|
|
}
|
|
|
|
|
2016-05-19 11:00:32 +02:00
|
|
|
fn transaction_receipt(&self, id: TransactionID) -> Option<LocalizedReceipt> {
|
2016-03-21 11:47:50 +01:00
|
|
|
self.receipts.read().unwrap().get(&id).cloned()
|
2016-03-10 20:27:50 +01:00
|
|
|
}
|
|
|
|
|
2016-05-19 11:00:32 +02:00
|
|
|
fn blocks_with_bloom(&self, _bloom: &H2048, _from_block: BlockID, _to_block: BlockID) -> Option<Vec<BlockNumber>> {
|
2016-03-10 20:27:50 +01:00
|
|
|
unimplemented!();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn logs(&self, _filter: Filter) -> Vec<LocalizedLogEntry> {
|
|
|
|
unimplemented!();
|
|
|
|
}
|
|
|
|
|
2016-04-28 21:47:44 +02:00
|
|
|
fn last_hashes(&self) -> LastHashes {
|
|
|
|
unimplemented!();
|
|
|
|
}
|
|
|
|
|
2016-05-19 11:00:32 +02:00
|
|
|
fn block_header(&self, id: BlockID) -> Option<Bytes> {
|
2016-03-10 20:27:50 +01:00
|
|
|
self.block_hash(id).and_then(|hash| self.blocks.read().unwrap().get(&hash).map(|r| Rlp::new(r).at(0).as_raw().to_vec()))
|
|
|
|
}
|
|
|
|
|
2016-05-19 11:00:32 +02:00
|
|
|
fn block_body(&self, id: BlockID) -> Option<Bytes> {
|
2016-03-10 20:27:50 +01:00
|
|
|
self.block_hash(id).and_then(|hash| self.blocks.read().unwrap().get(&hash).map(|r| {
|
|
|
|
let mut stream = RlpStream::new_list(2);
|
|
|
|
stream.append_raw(Rlp::new(&r).at(1).as_raw(), 1);
|
|
|
|
stream.append_raw(Rlp::new(&r).at(2).as_raw(), 1);
|
|
|
|
stream.out()
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
2016-05-19 11:00:32 +02:00
|
|
|
fn block(&self, id: BlockID) -> Option<Bytes> {
|
2016-03-10 20:27:50 +01:00
|
|
|
self.block_hash(id).and_then(|hash| self.blocks.read().unwrap().get(&hash).cloned())
|
|
|
|
}
|
|
|
|
|
2016-05-19 11:00:32 +02:00
|
|
|
fn block_status(&self, id: BlockID) -> BlockStatus {
|
2016-03-10 20:27:50 +01:00
|
|
|
match id {
|
2016-05-19 11:00:32 +02:00
|
|
|
BlockID::Number(number) if (number as usize) < self.blocks.read().unwrap().len() => BlockStatus::InChain,
|
|
|
|
BlockID::Hash(ref hash) if self.blocks.read().unwrap().get(hash).is_some() => BlockStatus::InChain,
|
2016-03-10 20:27:50 +01:00
|
|
|
_ => BlockStatus::Unknown
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// works only if blocks are one after another 1 -> 2 -> 3
|
|
|
|
fn tree_route(&self, from: &H256, to: &H256) -> Option<TreeRoute> {
|
|
|
|
Some(TreeRoute {
|
|
|
|
ancestor: H256::new(),
|
|
|
|
index: 0,
|
|
|
|
blocks: {
|
|
|
|
let numbers_read = self.numbers.read().unwrap();
|
|
|
|
let mut adding = false;
|
|
|
|
|
|
|
|
let mut blocks = Vec::new();
|
|
|
|
for (_, hash) in numbers_read.iter().sort_by(|tuple1, tuple2| tuple1.0.cmp(tuple2.0)) {
|
|
|
|
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 }
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
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' ?
|
|
|
|
if *hash > H256::from("f000000000000000000000000000000000000000000000000000000000000000") {
|
|
|
|
let mut rlp = RlpStream::new();
|
|
|
|
rlp.append(&hash.clone());
|
|
|
|
return Some(rlp.out());
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
|
|
|
fn block_receipts(&self, hash: &H256) -> Option<Bytes> {
|
|
|
|
// starts with 'f' ?
|
|
|
|
if *hash > H256::from("f000000000000000000000000000000000000000000000000000000000000000") {
|
2016-03-12 19:23:17 +01:00
|
|
|
let receipt = BlockReceipts::new(vec![Receipt::new(
|
2016-03-10 20:27:50 +01:00
|
|
|
H256::zero(),
|
|
|
|
U256::zero(),
|
2016-03-12 19:23:17 +01:00
|
|
|
vec![])]);
|
2016-03-10 20:27:50 +01:00
|
|
|
let mut rlp = RlpStream::new();
|
|
|
|
rlp.append(&receipt);
|
|
|
|
return Some(rlp.out());
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
|
|
|
fn import_block(&self, b: Bytes) -> ImportResult {
|
|
|
|
let header = Rlp::new(&b).val_at::<BlockHeader>(0);
|
|
|
|
let h = header.hash();
|
|
|
|
let number: usize = header.number as usize;
|
|
|
|
if number > self.blocks.read().unwrap().len() {
|
|
|
|
panic!("Unexpected block number. Expected {}, got {}", self.blocks.read().unwrap().len(), number);
|
|
|
|
}
|
|
|
|
if number > 0 {
|
|
|
|
match self.blocks.read().unwrap().get(&header.parent_hash) {
|
|
|
|
Some(parent) => {
|
|
|
|
let parent = Rlp::new(parent).val_at::<BlockHeader>(0);
|
|
|
|
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().unwrap().len();
|
|
|
|
if number == len {
|
|
|
|
{
|
|
|
|
let mut difficulty = self.difficulty.write().unwrap();
|
|
|
|
*difficulty.deref_mut() = *difficulty.deref() + header.difficulty;
|
|
|
|
}
|
|
|
|
mem::replace(self.last_hash.write().unwrap().deref_mut(), h.clone());
|
|
|
|
self.blocks.write().unwrap().insert(h.clone(), b);
|
|
|
|
self.numbers.write().unwrap().insert(number, h.clone());
|
|
|
|
let mut parent_hash = header.parent_hash;
|
|
|
|
if number > 0 {
|
|
|
|
let mut n = number - 1;
|
|
|
|
while n > 0 && self.numbers.read().unwrap()[&n] != parent_hash {
|
|
|
|
*self.numbers.write().unwrap().get_mut(&n).unwrap() = parent_hash.clone();
|
|
|
|
n -= 1;
|
|
|
|
parent_hash = Rlp::new(&self.blocks.read().unwrap()[&parent_hash]).val_at::<BlockHeader>(0).parent_hash;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
self.blocks.write().unwrap().insert(h.clone(), b.to_vec());
|
|
|
|
}
|
|
|
|
Ok(h)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn queue_info(&self) -> BlockQueueInfo {
|
|
|
|
BlockQueueInfo {
|
2016-03-24 17:07:54 +01:00
|
|
|
verified_queue_size: self.queue_size.load(AtomicOrder::Relaxed),
|
2016-03-10 20:27:50 +01:00
|
|
|
unverified_queue_size: 0,
|
|
|
|
verifying_queue_size: 0,
|
|
|
|
max_queue_size: 0,
|
|
|
|
max_mem_use: 0,
|
|
|
|
mem_used: 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn clear_queue(&self) {
|
|
|
|
}
|
|
|
|
|
|
|
|
fn chain_info(&self) -> BlockChainInfo {
|
|
|
|
BlockChainInfo {
|
|
|
|
total_difficulty: *self.difficulty.read().unwrap(),
|
|
|
|
pending_total_difficulty: *self.difficulty.read().unwrap(),
|
|
|
|
genesis_hash: self.genesis_hash.clone(),
|
|
|
|
best_block_hash: self.last_hash.read().unwrap().clone(),
|
|
|
|
best_block_number: self.blocks.read().unwrap().len() as BlockNumber - 1,
|
|
|
|
}
|
|
|
|
}
|
2016-03-24 23:03:22 +01:00
|
|
|
|
2016-05-02 12:17:30 +02:00
|
|
|
fn filter_traces(&self, _filter: TraceFilter) -> Option<Vec<LocalizedTrace>> {
|
|
|
|
unimplemented!();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn trace(&self, _trace: TraceId) -> Option<LocalizedTrace> {
|
|
|
|
unimplemented!();
|
|
|
|
}
|
|
|
|
|
2016-05-19 11:00:32 +02:00
|
|
|
fn transaction_traces(&self, _trace: TransactionID) -> Option<Vec<LocalizedTrace>> {
|
2016-05-02 12:17:30 +02:00
|
|
|
unimplemented!();
|
|
|
|
}
|
|
|
|
|
2016-05-19 11:00:32 +02:00
|
|
|
fn block_traces(&self, _trace: BlockID) -> Option<Vec<LocalizedTrace>> {
|
2016-05-02 12:17:30 +02:00
|
|
|
unimplemented!();
|
|
|
|
}
|
2016-05-31 19:52:53 +02:00
|
|
|
|
|
|
|
fn import_transactions(&self, transactions: Vec<SignedTransaction>) -> Vec<Result<TransactionImportResult, EthError>> {
|
2016-06-01 12:44:11 +02:00
|
|
|
let nonces = self.nonces.read().unwrap();
|
|
|
|
let balances = self.balances.read().unwrap();
|
|
|
|
let fetch_account = |a: &Address| AccountDetails {
|
|
|
|
nonce: nonces[a],
|
|
|
|
balance: balances[a],
|
|
|
|
};
|
|
|
|
|
2016-06-21 16:00:34 +02:00
|
|
|
self.miner.import_transactions(self, transactions, &fetch_account)
|
2016-05-31 19:52:53 +02:00
|
|
|
}
|
|
|
|
|
2016-06-19 14:35:42 +02:00
|
|
|
fn queue_transactions(&self, transactions: Vec<Bytes>) {
|
|
|
|
// import right here
|
|
|
|
let tx = transactions.into_iter().filter_map(|bytes| UntrustedRlp::new(&bytes).as_val().ok()).collect();
|
|
|
|
self.import_transactions(tx);
|
2016-05-31 19:52:53 +02:00
|
|
|
}
|
|
|
|
|
2016-06-27 19:06:54 +02:00
|
|
|
fn pending_transactions(&self) -> Vec<SignedTransaction> {
|
|
|
|
self.miner.pending_transactions()
|
2016-05-31 19:52:53 +02:00
|
|
|
}
|
2016-03-10 20:27:50 +01:00
|
|
|
}
|