Merge branch 'master' into Dockerfile

This commit is contained in:
debris 2016-01-28 19:49:10 +01:00
commit b4780250aa
110 changed files with 3451 additions and 1541 deletions

3
.gitignore vendored
View File

@ -23,4 +23,5 @@ Cargo.lock
/json-tests/target/ /json-tests/target/
# jetbrains ide stuff
.idea

1
.gitmodules vendored
View File

@ -1,3 +1,4 @@
[submodule "res/ethereum/tests"] [submodule "res/ethereum/tests"]
path = res/ethereum/tests path = res/ethereum/tests
url = git@github.com:ethereum/tests url = git@github.com:ethereum/tests
branch = develop

16
.travis.yml Normal file
View File

@ -0,0 +1,16 @@
language: rust
rust:
- nightly
os:
- osx
before_script:
- brew update
- brew install rocksdb
cache:
directories:
- $TRAVIS_BUILD_DIR/target
- $HOME/.cargo

View File

@ -17,14 +17,13 @@ heapsize = "0.2.0"
rust-crypto = "0.2.34" rust-crypto = "0.2.34"
time = "0.1" time = "0.1"
#interpolate_idents = { git = "https://github.com/SkylerLipthay/interpolate_idents" } #interpolate_idents = { git = "https://github.com/SkylerLipthay/interpolate_idents" }
evmjit = { path = "rust-evmjit", optional = true } evmjit = { path = "evmjit", optional = true }
ethash = { path = "ethash" } ethash = { path = "ethash" }
num_cpus = "0.2" num_cpus = "0.2"
clippy = "0.0.37"
crossbeam = "0.1.5"
[features] [features]
jit = ["evmjit"] jit = ["evmjit"]
evm_debug = [] evm_debug = []
test-heavy = []
[[bin]]
name = "client"
path = "src/bin/client/main.rs"

21
bin/Cargo.toml Normal file
View File

@ -0,0 +1,21 @@
[package]
description = "Ethcore client."
name = "ethcore-client"
version = "0.1.0"
license = "GPL-3.0"
authors = ["Ethcore <admin@ethcore.io>"]
[dependencies]
log = "0.3"
env_logger = "0.3"
rustc-serialize = "0.3"
docopt = "0.6"
docopt_macros = "0.6"
ctrlc = "1.0"
ethcore-util = { path = "../util" }
ethcore-rpc = { path = "../rpc", optional = true }
ethcore = { path = ".." }
clippy = "0.0.37"
[features]
rpc = ["ethcore-rpc"]

168
bin/src/main.rs Normal file
View File

@ -0,0 +1,168 @@
//! Ethcore client application.
#![warn(missing_docs)]
#![feature(plugin)]
#![plugin(docopt_macros)]
#![plugin(clippy)]
extern crate docopt;
extern crate rustc_serialize;
extern crate ethcore_util as util;
extern crate ethcore;
extern crate log;
extern crate env_logger;
extern crate ctrlc;
#[cfg(feature = "rpc")]
extern crate ethcore_rpc as rpc;
use std::env;
use log::{LogLevelFilter};
use env_logger::LogBuilder;
use ctrlc::CtrlC;
use util::*;
use ethcore::client::*;
use ethcore::service::{ClientService, NetSyncMessage};
use ethcore::ethereum;
use ethcore::blockchain::CacheSize;
use ethcore::sync::EthSync;
docopt!(Args derive Debug, "
Parity. Ethereum Client.
Usage:
parity [options]
parity [options] <enode>...
Options:
-l --logging LOGGING Specify the logging level
-h --help Show this screen.
");
fn setup_log(init: &str) {
let mut builder = LogBuilder::new();
builder.filter(None, LogLevelFilter::Info);
if env::var("RUST_LOG").is_ok() {
builder.parse(&env::var("RUST_LOG").unwrap());
}
builder.parse(init);
builder.init().unwrap();
}
#[cfg(feature = "rpc")]
fn setup_rpc_server(client: Arc<Client>, sync: Arc<EthSync>) {
use rpc::v1::*;
let mut server = rpc::HttpServer::new(1);
server.add_delegate(Web3Client::new().to_delegate());
server.add_delegate(EthClient::new(client.clone()).to_delegate());
server.add_delegate(EthFilterClient::new(client).to_delegate());
server.add_delegate(NetClient::new(sync).to_delegate());
server.start_async("127.0.0.1:3030");
}
#[cfg(not(feature = "rpc"))]
fn setup_rpc_server(_client: Arc<Client>, _sync: Arc<EthSync>) {
}
fn main() {
let args: Args = Args::docopt().decode().unwrap_or_else(|e| e.exit());
setup_log(&args.flag_logging);
let spec = ethereum::new_frontier();
let init_nodes = match args.arg_enode.len() {
0 => spec.nodes().clone(),
_ => args.arg_enode.clone(),
};
let mut net_settings = NetworkConfiguration::new();
net_settings.boot_nodes = init_nodes;
let mut service = ClientService::start(spec, net_settings).unwrap();
setup_rpc_server(service.client(), service.sync());
let io_handler = Arc::new(ClientIoHandler { client: service.client(), info: Default::default(), sync: service.sync() });
service.io().register_handler(io_handler).expect("Error registering IO handler");
let exit = Arc::new(Condvar::new());
let e = exit.clone();
CtrlC::set_handler(move || { e.notify_all(); });
let mutex = Mutex::new(());
let _ = exit.wait(mutex.lock().unwrap()).unwrap();
}
struct Informant {
chain_info: RwLock<Option<BlockChainInfo>>,
cache_info: RwLock<Option<CacheSize>>,
report: RwLock<Option<ClientReport>>,
}
impl Default for Informant {
fn default() -> Self {
Informant {
chain_info: RwLock::new(None),
cache_info: RwLock::new(None),
report: RwLock::new(None),
}
}
}
impl Informant {
pub fn tick(&self, client: &Client, sync: &EthSync) {
// 5 seconds betwen calls. TODO: calculate this properly.
let dur = 5usize;
let chain_info = client.chain_info();
let queue_info = client.queue_info();
let cache_info = client.cache_info();
let report = client.report();
let sync_info = sync.status();
if let (_, &Some(ref last_cache_info), &Some(ref last_report)) = (self.chain_info.read().unwrap().deref(), self.cache_info.read().unwrap().deref(), self.report.read().unwrap().deref()) {
println!("[ {} {} ]---[ {} blk/s | {} tx/s | {} gas/s //··· {}/{} peers, {} downloaded, {}+{} queued ···// {} ({}) bl {} ({}) ex ]",
chain_info.best_block_number,
chain_info.best_block_hash,
(report.blocks_imported - last_report.blocks_imported) / dur,
(report.transactions_applied - last_report.transactions_applied) / dur,
(report.gas_processed - last_report.gas_processed) / From::from(dur),
sync_info.num_active_peers,
sync_info.num_peers,
sync_info.blocks_received,
queue_info.unverified_queue_size,
queue_info.verified_queue_size,
cache_info.blocks,
cache_info.blocks as isize - last_cache_info.blocks as isize,
cache_info.block_details,
cache_info.block_details as isize - last_cache_info.block_details as isize
);
}
*self.chain_info.write().unwrap().deref_mut() = Some(chain_info);
*self.cache_info.write().unwrap().deref_mut() = Some(cache_info);
*self.report.write().unwrap().deref_mut() = Some(report);
}
}
const INFO_TIMER: TimerToken = 0;
struct ClientIoHandler {
client: Arc<Client>,
sync: Arc<EthSync>,
info: Informant,
}
impl IoHandler<NetSyncMessage> for ClientIoHandler {
fn initialize(&self, io: &IoContext<NetSyncMessage>) {
io.register_timer(INFO_TIMER, 5000).expect("Error registering timer");
}
fn timeout(&self, _io: &IoContext<NetSyncMessage>, timer: TimerToken) {
if INFO_TIMER == timer {
self.info.tick(&self.client, &self.sync);
}
}
}

2
cov.sh
View File

@ -17,5 +17,5 @@ fi
cargo test --no-run || exit $? cargo test --no-run || exit $?
mkdir -p target/coverage mkdir -p target/coverage
kcov --exclude-pattern ~/.multirust --include-pattern src --verify target/coverage target/debug/ethcore* kcov --exclude-pattern ~/.multirust,rocksdb,secp256k1 --include-pattern src --verify target/coverage target/debug/ethcore*
xdg-open target/coverage/index.html xdg-open target/coverage/index.html

View File

@ -30,13 +30,15 @@ impl EthashManager {
/// `nonce` - The nonce to pack into the mix /// `nonce` - The nonce to pack into the mix
pub fn compute_light(&self, block_number: u64, header_hash: &H256, nonce: u64) -> ProofOfWork { pub fn compute_light(&self, block_number: u64, header_hash: &H256, nonce: u64) -> ProofOfWork {
let epoch = block_number / ETHASH_EPOCH_LENGTH; let epoch = block_number / ETHASH_EPOCH_LENGTH;
if !self.lights.read().unwrap().contains_key(&epoch) { while !self.lights.read().unwrap().contains_key(&epoch) {
let mut lights = self.lights.write().unwrap(); // obtain write lock if let Ok(mut lights) = self.lights.try_write()
{
if !lights.contains_key(&epoch) { if !lights.contains_key(&epoch) {
let light = Light::new(block_number); let light = Light::new(block_number);
lights.insert(epoch, light); lights.insert(epoch, light);
} }
} }
}
self.lights.read().unwrap().get(&epoch).unwrap().compute(header_hash, nonce) self.lights.read().unwrap().get(&epoch).unwrap().compute(header_hash, nonce)
} }
} }

3
hook.sh Executable file
View File

@ -0,0 +1,3 @@
#!/bin/sh
echo "#!/bin/sh\ncargo test" >> ./.git/hooks/pre-push
chmod +x ./.git/hooks/pre-push

View File

@ -26,6 +26,11 @@
"gasLimit": "0x1388", "gasLimit": "0x1388",
"stateRoot": "0xd7f8974fb5ac78d9ac099b9ad5018bedc2ce0a72dad1827a1709da30580f0544" "stateRoot": "0xd7f8974fb5ac78d9ac099b9ad5018bedc2ce0a72dad1827a1709da30580f0544"
}, },
"nodes": [
"enode://a979fb575495b8d6db44f750317d0f4622bf4c2aa3365d6af7c284339968eef29b69ad0dce72a4d8db5ebb4968de0e3bec910127f134779fbcb0cb6d3331163c@52.16.188.185:30303",
"enode://de471bccee3d042261d52e9bff31458daecc406142b401d4cd848f677479f73104b9fdeb090af9583d3391b7f10cb2ba9e26865dd5fca4fcdc0fb1e3b723c786@54.94.239.50:30303",
"enode://1118980bf48b0a3640bdba04e0fe78b1add18e1cd99bf22d53daac1fd9972ad650df52176e7c7d89d1114cfef2bc23a2959aa54998a46afcf7d91809f0855082@52.74.57.123:30303"
],
"accounts": { "accounts": {
"0000000000000000000000000000000000000001": { "builtin": { "name": "ecrecover", "linear": { "base": 3000, "word": 0 } } }, "0000000000000000000000000000000000000001": { "builtin": { "name": "ecrecover", "linear": { "base": 3000, "word": 0 } } },
"0000000000000000000000000000000000000002": { "builtin": { "name": "sha256", "linear": { "base": 60, "word": 12 } } }, "0000000000000000000000000000000000000002": { "builtin": { "name": "sha256", "linear": { "base": 60, "word": 12 } } },

@ -1 +1 @@
Subproject commit dc86e6359675440aea59ddb48648a01c799925d8 Subproject commit c670b1d8c9f09593a6758ab2c099360e16c7c25b

19
rpc/Cargo.toml Normal file
View File

@ -0,0 +1,19 @@
[package]
description = "Ethcore jsonrpc"
name = "ethcore-rpc"
version = "0.1.0"
license = "GPL-3.0"
authors = ["Ethcore <admin@ethcore.io"]
[lib]
[dependencies]
serde = "0.6.7"
serde_macros = "0.6.10"
serde_json = "0.6.0"
jsonrpc-core = "1.1"
jsonrpc-http-server = "1.1"
ethcore-util = { path = "../util" }
ethcore = { path = ".." }
clippy = "0.0.37"

43
rpc/src/lib.rs Normal file
View File

@ -0,0 +1,43 @@
//! Ethcore rpc.
#![warn(missing_docs)]
#![feature(custom_derive, custom_attribute, plugin)]
#![plugin(serde_macros)]
#![plugin(clippy)]
extern crate serde;
extern crate serde_json;
extern crate jsonrpc_core;
extern crate jsonrpc_http_server;
extern crate ethcore_util as util;
extern crate ethcore;
use self::jsonrpc_core::{IoHandler, IoDelegate};
pub mod v1;
/// Http server.
pub struct HttpServer {
handler: IoHandler,
threads: usize
}
impl HttpServer {
/// Construct new http server object with given number of threads.
pub fn new(threads: usize) -> HttpServer {
HttpServer {
handler: IoHandler::new(),
threads: threads
}
}
/// Add io delegate.
pub fn add_delegate<D>(&mut self, delegate: IoDelegate<D>) where D: Send + Sync + 'static {
self.handler.add_delegate(delegate);
}
/// Start server asynchronously in new thread
pub fn start_async(self, addr: &str) {
let server = jsonrpc_http_server::Server::new(self.handler, self.threads);
server.start_async(addr)
}
}

132
rpc/src/v1/impls/eth.rs Normal file
View File

@ -0,0 +1,132 @@
//! Eth rpc implementation.
use std::sync::Arc;
use jsonrpc_core::*;
use util::hash::*;
use util::uint::*;
use util::sha3::*;
use ethcore::client::*;
use ethcore::views::*;
use v1::traits::{Eth, EthFilter};
use v1::types::Block;
/// Eth rpc implementation.
pub struct EthClient {
client: Arc<Client>,
}
impl EthClient {
/// Creates new EthClient.
pub fn new(client: Arc<Client>) -> Self {
EthClient {
client: client
}
}
}
impl Eth for EthClient {
fn protocol_version(&self, params: Params) -> Result<Value, Error> {
match params {
Params::None => Ok(Value::U64(63)),
_ => Err(Error::invalid_params())
}
}
fn author(&self, params: Params) -> Result<Value, Error> {
match params {
Params::None => to_value(&Address::new()),
_ => Err(Error::invalid_params())
}
}
fn gas_price(&self, params: Params) -> Result<Value, Error> {
match params {
Params::None => Ok(Value::U64(0)),
_ => Err(Error::invalid_params())
}
}
fn block_number(&self, params: Params) -> Result<Value, Error> {
match params {
Params::None => Ok(Value::U64(self.client.chain_info().best_block_number)),
_ => Err(Error::invalid_params())
}
}
fn is_mining(&self, params: Params) -> Result<Value, Error> {
match params {
Params::None => Ok(Value::Bool(false)),
_ => Err(Error::invalid_params())
}
}
fn hashrate(&self, params: Params) -> Result<Value, Error> {
match params {
Params::None => Ok(Value::U64(0)),
_ => Err(Error::invalid_params())
}
}
fn block_transaction_count(&self, _: Params) -> Result<Value, Error> {
Ok(Value::U64(0))
}
fn block(&self, params: Params) -> Result<Value, Error> {
match from_params::<(H256, bool)>(params) {
Ok((hash, _include_txs)) => match (self.client.block_header(&hash), self.client.block_total_difficulty(&hash)) {
(Some(bytes), Some(total_difficulty)) => {
let view = HeaderView::new(&bytes);
let block = Block {
hash: view.sha3(),
parent_hash: view.parent_hash(),
uncles_hash: view.uncles_hash(),
author: view.author(),
miner: view.author(),
state_root: view.state_root(),
transactions_root: view.transactions_root(),
receipts_root: view.receipts_root(),
number: U256::from(view.number()),
gas_used: view.gas_used(),
gas_limit: view.gas_limit(),
logs_bloom: view.log_bloom(),
timestamp: U256::from(view.timestamp()),
difficulty: view.difficulty(),
total_difficulty: total_difficulty,
uncles: vec![],
transactions: vec![]
};
to_value(&block)
},
_ => Ok(Value::Null)
},
Err(err) => Err(err)
}
}
}
/// Eth filter rpc implementation.
pub struct EthFilterClient {
client: Arc<Client>
}
impl EthFilterClient {
/// Creates new Eth filter client.
pub fn new(client: Arc<Client>) -> Self {
EthFilterClient {
client: client
}
}
}
impl EthFilter for EthFilterClient {
fn new_block_filter(&self, _params: Params) -> Result<Value, Error> {
Ok(Value::U64(0))
}
fn new_pending_transaction_filter(&self, _params: Params) -> Result<Value, Error> {
Ok(Value::U64(1))
}
fn filter_changes(&self, _: Params) -> Result<Value, Error> {
to_value(&self.client.chain_info().best_block_hash).map(|v| Value::Array(vec![v]))
}
}

8
rpc/src/v1/impls/mod.rs Normal file
View File

@ -0,0 +1,8 @@
//! Ethereum rpc interface implementation.
mod web3;
mod eth;
mod net;
pub use self::web3::Web3Client;
pub use self::eth::{EthClient, EthFilterClient};
pub use self::net::NetClient;

29
rpc/src/v1/impls/net.rs Normal file
View File

@ -0,0 +1,29 @@
//! Net rpc implementation.
use std::sync::Arc;
use jsonrpc_core::*;
use ethcore::sync::EthSync;
use v1::traits::Net;
/// Net rpc implementation.
pub struct NetClient {
sync: Arc<EthSync>
}
impl NetClient {
/// Creates new NetClient.
pub fn new(sync: Arc<EthSync>) -> Self {
NetClient {
sync: sync
}
}
}
impl Net for NetClient {
fn version(&self, _: Params) -> Result<Value, Error> {
Ok(Value::U64(self.sync.status().protocol_version as u64))
}
fn peer_count(&self, _params: Params) -> Result<Value, Error> {
Ok(Value::U64(self.sync.status().num_peers as u64))
}
}

21
rpc/src/v1/impls/web3.rs Normal file
View File

@ -0,0 +1,21 @@
//! Web3 rpc implementation.
use jsonrpc_core::*;
use v1::traits::Web3;
/// Web3 rpc implementation.
pub struct Web3Client;
impl Web3Client {
/// Creates new Web3Client.
pub fn new() -> Self { Web3Client }
}
impl Web3 for Web3Client {
fn client_version(&self, params: Params) -> Result<Value, Error> {
match params {
//Params::None => Ok(Value::String("parity/0.1.0/-/rust1.7-nightly".to_owned())),
Params::None => Ok(Value::String("surprise/0.1.0/surprise/surprise".to_owned())),
_ => Err(Error::invalid_params())
}
}
}

10
rpc/src/v1/mod.rs Normal file
View File

@ -0,0 +1,10 @@
//! Ethcore rpc v1.
//!
//! Compliant with ethereum rpc.
pub mod traits;
mod impls;
mod types;
pub use self::traits::{Web3, Eth, EthFilter, Net};
pub use self::impls::*;

163
rpc/src/v1/traits/eth.rs Normal file
View File

@ -0,0 +1,163 @@
//! Eth rpc interface.
use std::sync::Arc;
use jsonrpc_core::*;
/// Eth rpc interface.
pub trait Eth: Sized + Send + Sync + 'static {
/// Returns protocol version.
fn protocol_version(&self, _: Params) -> Result<Value, Error> { rpc_unimplemented!() }
/// Returns the number of hashes per second that the node is mining with.
fn hashrate(&self, _: Params) -> Result<Value, Error> { rpc_unimplemented!() }
/// Returns block author.
fn author(&self, _: Params) -> Result<Value, Error> { rpc_unimplemented!() }
/// Returns true if client is actively mining new blocks.
fn is_mining(&self, _: Params) -> Result<Value, Error> { rpc_unimplemented!() }
/// Returns current gas_price.
fn gas_price(&self, _: Params) -> Result<Value, Error> { rpc_unimplemented!() }
/// Returns accounts list.
fn accounts(&self, _: Params) -> Result<Value, Error> { rpc_unimplemented!() }
/// Returns highest block number.
fn block_number(&self, _: Params) -> Result<Value, Error> { rpc_unimplemented!() }
/// Returns balance of the given account.
fn balance(&self, _: Params) -> Result<Value, Error> { rpc_unimplemented!() }
/// Returns content of the storage at given address.
fn storage_at(&self, _: Params) -> Result<Value, Error> { rpc_unimplemented!() }
/// Returns block with given index / hash.
fn block(&self, _: Params) -> Result<Value, Error> { rpc_unimplemented!() }
/// Returns the number of transactions sent from given address at given time (block number).
fn transaction_count(&self, _: Params) -> Result<Value, Error> { rpc_unimplemented!() }
/// Returns the number of transactions in a block.
fn block_transaction_count(&self, _: Params) -> Result<Value, Error> { rpc_unimplemented!() }
/// Returns the number of uncles in a given block.
fn block_uncles_count(&self, _: Params) -> Result<Value, Error> { rpc_unimplemented!() }
/// Returns the code at given address at given time (block number).
fn code_at(&self, _: Params) -> Result<Value, Error> { rpc_unimplemented!() }
/// Sends transaction.
fn send_transaction(&self, _: Params) -> Result<Value, Error> { rpc_unimplemented!() }
/// Call contract.
fn call(&self, _: Params) -> Result<Value, Error> { rpc_unimplemented!() }
/// Estimate gas needed for execution of given contract.
fn estimate_gas(&self, _: Params) -> Result<Value, Error> { rpc_unimplemented!() }
/// Returns transaction at given block and index.
fn transaction_at(&self, _: Params) -> Result<Value, Error> { rpc_unimplemented!() }
/// Returns transaction receipt.
fn transaction_receipt(&self, _: Params) -> Result<Value, Error> { rpc_unimplemented!() }
/// Returns an uncles at given block and index.
fn uncle_at(&self, _: Params) -> Result<Value, Error> { rpc_unimplemented!() }
/// Returns available compilers.
fn compilers(&self, _: Params) -> Result<Value, Error> { rpc_unimplemented!() }
/// Compiles lll code.
fn compile_lll(&self, _: Params) -> Result<Value, Error> { rpc_unimplemented!() }
/// Compiles solidity.
fn compile_solidity(&self, _: Params) -> Result<Value, Error> { rpc_unimplemented!() }
/// Compiles serpent.
fn compile_serpent(&self, _: Params) -> Result<Value, Error> { rpc_unimplemented!() }
/// Returns logs matching given filter object.
fn logs(&self, _: Params) -> Result<Value, Error> { rpc_unimplemented!() }
/// Returns the hash of the current block, the seedHash, and the boundary condition to be met.
fn work(&self, _: Params) -> Result<Value, Error> { rpc_unimplemented!() }
/// Used for submitting a proof-of-work solution.
fn submit_work(&self, _: Params) -> Result<Value, Error> { rpc_unimplemented!() }
/// Used for submitting mining hashrate.
fn submit_hashrate(&self, _: Params) -> Result<Value, Error> { rpc_unimplemented!() }
/// Should be used to convert object to io delegate.
fn to_delegate(self) -> IoDelegate<Self> {
let mut delegate = IoDelegate::new(Arc::new(self));
delegate.add_method("eth_protocolVersion", Eth::protocol_version);
delegate.add_method("eth_hashrate", Eth::hashrate);
delegate.add_method("eth_coinbase", Eth::author);
delegate.add_method("eth_mining", Eth::is_mining);
delegate.add_method("eth_gasPrice", Eth::gas_price);
delegate.add_method("eth_accounts", Eth::accounts);
delegate.add_method("eth_blockNumber", Eth::block_number);
delegate.add_method("eth_balance", Eth::balance);
delegate.add_method("eth_getStorageAt", Eth::storage_at);
delegate.add_method("eth_getTransactionCount", Eth::transaction_count);
delegate.add_method("eth_getBlockTransactionCountByHash", Eth::block_transaction_count);
delegate.add_method("eth_getBlockTransactionCountByNumber", Eth::block_transaction_count);
delegate.add_method("eth_getUncleCountByBlockHash", Eth::block_uncles_count);
delegate.add_method("eth_getUncleCountByBlockNumber", Eth::block_uncles_count);
delegate.add_method("eth_code", Eth::code_at);
delegate.add_method("eth_sendTransaction", Eth::send_transaction);
delegate.add_method("eth_call", Eth::call);
delegate.add_method("eth_estimateGas", Eth::estimate_gas);
delegate.add_method("eth_getBlockByHash", Eth::block);
delegate.add_method("eth_getBlockByNumber", Eth::block);
delegate.add_method("eth_getTransactionByBlockHashAndIndex", Eth::transaction_at);
delegate.add_method("eth_getTransactionByBlockNumberAndIndex", Eth::transaction_at);
delegate.add_method("eth_getTransactionReceipt", Eth::transaction_receipt);
delegate.add_method("eth_getUncleByBlockHashAndIndex", Eth::uncle_at);
delegate.add_method("eth_getUncleByBlockNumberAndIndex", Eth::uncle_at);
delegate.add_method("eth_getCompilers", Eth::compilers);
delegate.add_method("eth_compileLLL", Eth::compile_lll);
delegate.add_method("eth_compileSolidity", Eth::compile_solidity);
delegate.add_method("eth_compileSerpent", Eth::compile_serpent);
delegate.add_method("eth_getLogs", Eth::logs);
delegate.add_method("eth_getWork", Eth::work);
delegate.add_method("eth_submitWork", Eth::submit_work);
delegate.add_method("eth_submitHashrate", Eth::submit_hashrate);
delegate
}
}
/// Eth filters rpc api (polling).
// TODO: do filters api properly
pub trait EthFilter: Sized + Send + Sync + 'static {
/// Returns id of new filter.
fn new_filter(&self, _: Params) -> Result<Value, Error> { rpc_unimplemented!() }
/// Returns id of new block filter.
fn new_block_filter(&self, _: Params) -> Result<Value, Error> { rpc_unimplemented!() }
/// Returns id of new block filter.
fn new_pending_transaction_filter(&self, _: Params) -> Result<Value, Error> { rpc_unimplemented!() }
/// Returns filter changes since last poll.
fn filter_changes(&self, _: Params) -> Result<Value, Error> { rpc_unimplemented!() }
/// Returns filter logs.
fn filter_logs(&self, _: Params) -> Result<Value, Error> { rpc_unimplemented!() }
/// Uninstalls filter.
fn uninstall_filter(&self, _: Params) -> Result<Value, Error> { rpc_unimplemented!() }
/// Should be used to convert object to io delegate.
fn to_delegate(self) -> IoDelegate<Self> {
let mut delegate = IoDelegate::new(Arc::new(self));
delegate.add_method("eth_newFilter", EthFilter::new_filter);
delegate.add_method("eth_newBlockFilter", EthFilter::new_block_filter);
delegate.add_method("eth_newPendingTransactionFilter", EthFilter::new_pending_transaction_filter);
delegate.add_method("eth_getFilterChanges", EthFilter::filter_changes);
delegate.add_method("eth_getFilterLogs", EthFilter::filter_logs);
delegate.add_method("eth_uninstallFilter", EthFilter::uninstall_filter);
delegate
}
}

13
rpc/src/v1/traits/mod.rs Normal file
View File

@ -0,0 +1,13 @@
//! Ethereum rpc interfaces.
macro_rules! rpc_unimplemented {
() => (Err(Error::internal_error()))
}
pub mod web3;
pub mod eth;
pub mod net;
pub use self::web3::Web3;
pub use self::eth::{Eth, EthFilter};
pub use self::net::Net;

25
rpc/src/v1/traits/net.rs Normal file
View File

@ -0,0 +1,25 @@
//! Net rpc interface.
use std::sync::Arc;
use jsonrpc_core::*;
/// Net rpc interface.
pub trait Net: Sized + Send + Sync + 'static {
/// Returns protocol version.
fn version(&self, _: Params) -> Result<Value, Error> { rpc_unimplemented!() }
/// Returns number of peers connected to node.
fn peer_count(&self, _: Params) -> Result<Value, Error> { rpc_unimplemented!() }
/// Returns true if client is actively listening for network connections.
/// Otherwise false.
fn is_listening(&self, _: Params) -> Result<Value, Error> { rpc_unimplemented!() }
/// Should be used to convert object to io delegate.
fn to_delegate(self) -> IoDelegate<Self> {
let mut delegate = IoDelegate::new(Arc::new(self));
delegate.add_method("net_version", Net::version);
delegate.add_method("net_peerCount", Net::peer_count);
delegate.add_method("net_listening", Net::is_listening);
delegate
}
}

16
rpc/src/v1/traits/web3.rs Normal file
View File

@ -0,0 +1,16 @@
//! Web3 rpc interface.
use std::sync::Arc;
use jsonrpc_core::*;
/// Web3 rpc interface.
pub trait Web3: Sized + Send + Sync + 'static {
/// Returns current client version.
fn client_version(&self, _: Params) -> Result<Value, Error> { rpc_unimplemented!() }
/// Should be used to convert object to io delegate.
fn to_delegate(self) -> IoDelegate<Self> {
let mut delegate = IoDelegate::new(Arc::new(self));
delegate.add_method("web3_clientVersion", Web3::client_version);
delegate
}
}

36
rpc/src/v1/types/block.rs Normal file
View File

@ -0,0 +1,36 @@
use util::hash::*;
use util::uint::*;
#[derive(Default, Debug, Serialize)]
pub struct Block {
pub hash: H256,
#[serde(rename="parentHash")]
pub parent_hash: H256,
#[serde(rename="sha3Uncles")]
pub uncles_hash: H256,
pub author: Address,
// TODO: get rid of this one
pub miner: Address,
#[serde(rename="stateRoot")]
pub state_root: H256,
#[serde(rename="transactionsRoot")]
pub transactions_root: H256,
#[serde(rename="receiptsRoot")]
pub receipts_root: H256,
pub number: U256,
#[serde(rename="gasUsed")]
pub gas_used: U256,
#[serde(rename="gasLimit")]
pub gas_limit: U256,
// TODO: figure out how to properly serialize bytes
//#[serde(rename="extraData")]
//extra_data: Vec<u8>,
#[serde(rename="logsBloom")]
pub logs_bloom: H2048,
pub timestamp: U256,
pub difficulty: U256,
#[serde(rename="totalDifficulty")]
pub total_difficulty: U256,
pub uncles: Vec<U256>,
pub transactions: Vec<U256>
}

3
rpc/src/v1/types/mod.rs Normal file
View File

@ -0,0 +1,3 @@
mod block;
pub use self::block::Block;

View File

@ -103,7 +103,7 @@ impl Account {
/// Get (and cache) the contents of the trie's storage at `key`. /// Get (and cache) the contents of the trie's storage at `key`.
pub fn storage_at(&self, db: &HashDB, key: &H256) -> H256 { pub fn storage_at(&self, db: &HashDB, key: &H256) -> H256 {
self.storage_overlay.borrow_mut().entry(key.clone()).or_insert_with(||{ self.storage_overlay.borrow_mut().entry(key.clone()).or_insert_with(||{
(Filth::Clean, H256::from(SecTrieDB::new(db, &self.storage_root).get(key.bytes()).map(|v| -> U256 {decode(v)}).unwrap_or(U256::zero()))) (Filth::Clean, H256::from(SecTrieDB::new(db, &self.storage_root).get(key.bytes()).map_or(U256::zero(), |v| -> U256 {decode(v)})))
}).1.clone() }).1.clone()
} }
@ -149,11 +149,15 @@ impl Account {
/// Provide a database to lookup `code_hash`. Should not be called if it is a contract without code. /// Provide a database to lookup `code_hash`. Should not be called if it is a contract without code.
pub fn cache_code(&mut self, db: &HashDB) -> bool { pub fn cache_code(&mut self, db: &HashDB) -> bool {
// TODO: fill out self.code_cache; // TODO: fill out self.code_cache;
return self.is_cached() || trace!("Account::cache_code: ic={}; self.code_hash={:?}, self.code_cache={}", self.is_cached(), self.code_hash, self.code_cache.pretty());
self.is_cached() ||
match self.code_hash { match self.code_hash {
Some(ref h) => match db.lookup(h) { Some(ref h) => match db.lookup(h) {
Some(x) => { self.code_cache = x.to_vec(); true }, Some(x) => { self.code_cache = x.to_vec(); true },
_ => false, _ => {
warn!("Failed reverse lookup of {}", h);
false
},
}, },
_ => false, _ => false,
} }
@ -248,8 +252,8 @@ mod tests {
let a = Account::from_rlp(&rlp); let a = Account::from_rlp(&rlp);
assert_eq!(a.storage_root().unwrap().hex(), "c57e1afb758b07f8d2c8f13a3b6e44fa5ff94ab266facc5a4fd3f062426e50b2"); assert_eq!(a.storage_root().unwrap().hex(), "c57e1afb758b07f8d2c8f13a3b6e44fa5ff94ab266facc5a4fd3f062426e50b2");
assert_eq!(a.storage_at(&mut db, &H256::from(&U256::from(0x00u64))), H256::from(&U256::from(0x1234u64))); assert_eq!(a.storage_at(&db, &H256::from(&U256::from(0x00u64))), H256::from(&U256::from(0x1234u64)));
assert_eq!(a.storage_at(&mut db, &H256::from(&U256::from(0x01u64))), H256::new()); assert_eq!(a.storage_at(&db, &H256::from(&U256::from(0x01u64))), H256::new());
} }
#[test] #[test]

View File

@ -15,10 +15,10 @@ pub enum Existance {
impl fmt::Display for Existance { impl fmt::Display for Existance {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self { match *self {
&Existance::Born => try!(write!(f, "+++")), Existance::Born => try!(write!(f, "+++")),
&Existance::Alive => try!(write!(f, "***")), Existance::Alive => try!(write!(f, "***")),
&Existance::Died => try!(write!(f, "XXX")), Existance::Died => try!(write!(f, "XXX")),
} }
Ok(()) Ok(())
} }
@ -72,11 +72,11 @@ impl AccountDiff {
code: Diff::new(pre.code.clone(), post.code.clone()), code: Diff::new(pre.code.clone(), post.code.clone()),
storage: storage.into_iter().map(|k| storage: storage.into_iter().map(|k|
(k.clone(), Diff::new( (k.clone(), Diff::new(
pre.storage.get(&k).cloned().unwrap_or(H256::new()), pre.storage.get(&k).cloned().unwrap_or_else(H256::new),
post.storage.get(&k).cloned().unwrap_or(H256::new()) post.storage.get(&k).cloned().unwrap_or_else(H256::new)
))).collect(), ))).collect(),
}; };
if r.balance.is_same() && r.nonce.is_same() && r.code.is_same() && r.storage.len() == 0 { if r.balance.is_same() && r.nonce.is_same() && r.code.is_same() && r.storage.is_empty() {
None None
} else { } else {
Some(r) Some(r)
@ -112,16 +112,15 @@ impl fmt::Display for AccountDiff {
Diff::Changed(ref pre, ref post) => try!(write!(f, "${} ({} {} {})", post, pre, if pre > post {"-"} else {"+"}, *max(pre, post) - *min(pre, post))), Diff::Changed(ref pre, ref post) => try!(write!(f, "${} ({} {} {})", post, pre, if pre > post {"-"} else {"+"}, *max(pre, post) - *min(pre, post))),
_ => {}, _ => {},
} }
match self.code { if let Diff::Born(ref x) = self.code {
Diff::Born(ref x) => try!(write!(f, " code {}", x.pretty())), try!(write!(f, " code {}", x.pretty()));
_ => {},
} }
try!(write!(f, "\n")); try!(write!(f, "\n"));
for (k, dv) in self.storage.iter() { for (k, dv) in &self.storage {
match dv { match *dv {
&Diff::Born(ref v) => try!(write!(f, " + {} => {}\n", interpreted_hash(k), interpreted_hash(v))), Diff::Born(ref v) => try!(write!(f, " + {} => {}\n", interpreted_hash(k), interpreted_hash(v))),
&Diff::Changed(ref pre, ref post) => try!(write!(f, " * {} => {} (was {})\n", interpreted_hash(k), interpreted_hash(post), interpreted_hash(pre))), Diff::Changed(ref pre, ref post) => try!(write!(f, " * {} => {} (was {})\n", interpreted_hash(k), interpreted_hash(post), interpreted_hash(pre))),
&Diff::Died(_) => try!(write!(f, " X {}\n", interpreted_hash(k))), Diff::Died(_) => try!(write!(f, " X {}\n", interpreted_hash(k))),
_ => {}, _ => {},
} }
} }

View File

@ -3,8 +3,16 @@ use util::hash::*;
use util::uint::*; use util::uint::*;
use util::bytes::*; use util::bytes::*;
// TODO: should be a trait, possible to avoid cloning everything from a Transaction(/View). /// Transaction value
#[derive(Clone, Debug)]
pub enum ActionValue {
/// Value that should be transfered
Transfer(U256),
/// Apparent value for transaction (not transfered)
Apparent(U256)
}
// TODO: should be a trait, possible to avoid cloning everything from a Transaction(/View).
/// Action (call/create) input params. Everything else should be specified in Externalities. /// Action (call/create) input params. Everything else should be specified in Externalities.
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct ActionParams { pub struct ActionParams {
@ -22,16 +30,16 @@ pub struct ActionParams {
/// Gas price. /// Gas price.
pub gas_price: U256, pub gas_price: U256,
/// Transaction value. /// Transaction value.
pub value: U256, pub value: ActionValue,
/// Code being executed. /// Code being executed.
pub code: Option<Bytes>, pub code: Option<Bytes>,
/// Input data. /// Input data.
pub data: Option<Bytes> pub data: Option<Bytes>
} }
impl ActionParams { impl Default for ActionParams {
/// TODO [Gav Wood] Please document me /// Returns default ActionParams initialized with zeros
pub fn new() -> ActionParams { fn default() -> ActionParams {
ActionParams { ActionParams {
code_address: Address::new(), code_address: Address::new(),
address: Address::new(), address: Address::new(),
@ -39,7 +47,7 @@ impl ActionParams {
origin: Address::new(), origin: Address::new(),
gas: U256::zero(), gas: U256::zero(),
gas_price: U256::zero(), gas_price: U256::zero(),
value: U256::zero(), value: ActionValue::Transfer(U256::zero()),
code: None, code: None,
data: None data: None
} }

View File

@ -1,100 +0,0 @@
extern crate ethcore_util as util;
extern crate ethcore;
extern crate rustc_serialize;
extern crate log;
extern crate env_logger;
use std::io::stdin;
use std::env;
use log::{LogLevelFilter};
use env_logger::LogBuilder;
use util::*;
use ethcore::client::*;
use ethcore::service::ClientService;
use ethcore::ethereum;
use ethcore::blockchain::CacheSize;
use ethcore::sync::*;
fn setup_log() {
let mut builder = LogBuilder::new();
builder.filter(None, LogLevelFilter::Info);
if env::var("RUST_LOG").is_ok() {
builder.parse(&env::var("RUST_LOG").unwrap());
}
builder.init().unwrap();
}
fn main() {
setup_log();
let spec = ethereum::new_frontier();
let mut service = ClientService::start(spec).unwrap();
let io_handler = Box::new(ClientIoHandler { client: service.client(), timer: 0, info: Default::default() });
service.io().register_handler(io_handler).expect("Error registering IO handler");
loop {
let mut cmd = String::new();
stdin().read_line(&mut cmd).unwrap();
if cmd == "quit\n" || cmd == "exit\n" || cmd == "q\n" {
break;
}
}
}
#[derive(Default, Debug)]
struct Informant {
chain_info: Option<BlockChainInfo>,
cache_info: Option<CacheSize>,
report: Option<ClientReport>,
}
impl Informant {
pub fn tick(&mut self, client: &Client) {
// 5 seconds betwen calls. TODO: calculate this properly.
let dur = 5usize;
let chain_info = client.chain_info();
let cache_info = client.cache_info();
let report = client.report();
if let (_, &Some(ref last_cache_info), &Some(ref last_report)) = (&self.chain_info, &self.cache_info, &self.report) {
println!("[ {} {} ]---[ {} blk/s | {} tx/s | {} gas/s //···{}···// {} ({}) bl {} ({}) ex ]",
chain_info.best_block_number,
chain_info.best_block_hash,
(report.blocks_imported - last_report.blocks_imported) / dur,
(report.transactions_applied - last_report.transactions_applied) / dur,
(report.gas_processed - last_report.gas_processed) / From::from(dur),
0, // TODO: peers
cache_info.blocks,
cache_info.blocks as isize - last_cache_info.blocks as isize,
cache_info.block_details,
cache_info.block_details as isize - last_cache_info.block_details as isize
);
}
self.chain_info = Some(chain_info);
self.cache_info = Some(cache_info);
self.report = Some(report);
}
}
struct ClientIoHandler {
client: Arc<RwLock<Client>>,
timer: TimerToken,
info: Informant,
}
impl IoHandler<NetSyncMessage> for ClientIoHandler {
fn initialize<'s>(&'s mut self, io: &mut IoContext<'s, NetSyncMessage>) {
self.timer = io.register_timer(5000).expect("Error registering timer");
}
fn timeout<'s>(&'s mut self, _io: &mut IoContext<'s, NetSyncMessage>, timer: TimerToken) {
if self.timer == timer {
let client = self.client.read().unwrap();
client.tick();
self.info.tick(client.deref());
}
}
}

View File

@ -1,82 +1,128 @@
#![allow(ptr_arg)] // Because of &LastHashes -> &Vec<_>
use common::*; use common::*;
use engine::*; use engine::*;
use state::*; use state::*;
use verification::PreVerifiedBlock; use verification::PreVerifiedBlock;
/// A transaction/receipt execution entry. /// A block, encoded as it is on the block chain.
pub struct Entry { // TODO: rename to Block
transaction: Transaction, #[derive(Default, Debug, Clone)]
receipt: Receipt, pub struct Block {
/// The header of this block.
pub header: Header,
/// The transactions in this block.
pub transactions: Vec<Transaction>,
/// The uncles of this block.
pub uncles: Vec<Header>,
}
impl Block {
/// Returns true iff the given bytes form a valid encoding of a block in RLP.
// TODO: implement Decoder for this and have this use that.
pub fn is_good(b: &[u8]) -> bool {
/*
let urlp = UntrustedRlp::new(&b);
if !urlp.is_list() || urlp.item_count() != 3 || urlp.size() != b.len() { return false; }
if urlp.val_at::<Header>(0).is_err() { return false; }
if !urlp.at(1).unwrap().is_list() { return false; }
if urlp.at(1).unwrap().iter().find(|i| i.as_val::<Transaction>().is_err()).is_some() {
return false;
}
if !urlp.at(2).unwrap().is_list() { return false; }
if urlp.at(2).unwrap().iter().find(|i| i.as_val::<Header>().is_err()).is_some() {
return false;
}
true*/
UntrustedRlp::new(b).as_val::<Block>().is_ok()
}
}
impl Decodable for Block {
fn decode<D>(decoder: &D) -> Result<Self, DecoderError> where D: Decoder {
if decoder.as_raw().len() != try!(decoder.as_rlp().payload_info()).total() {
return Err(DecoderError::RlpIsTooBig);
}
let d = try!(decoder.as_list());
if d.len() != 3 {
return Err(DecoderError::RlpIncorrectListLen);
}
Ok(Block {
header: try!(Decodable::decode(&d[0])),
transactions: try!(Decodable::decode(&d[1])),
uncles: try!(Decodable::decode(&d[2])),
})
}
} }
/// Internal type for a block's common elements. /// Internal type for a block's common elements.
pub struct Block { // TODO: rename to ExecutedBlock
header: Header, // TODO: use BareBlock
#[derive(Debug, Clone)]
pub struct ExecutedBlock {
base: Block,
/// State is the most final state in the block. receipts: Vec<Receipt>,
transactions_set: HashSet<H256>,
state: State, state: State,
archive: Vec<Entry>,
archive_set: HashSet<H256>,
uncles: Vec<Header>,
} }
/// A set of references to `Block` fields that are publicly accessible. /// A set of references to `ExecutedBlock` fields that are publicly accessible.
pub struct BlockRefMut<'a> { pub struct BlockRefMut<'a> {
/// TODO [Gav Wood] Please document me /// TODO [Gav Wood] Please document me
pub header: &'a Header, pub header: &'a Header,
/// TODO [Gav Wood] Please document me /// TODO [Gav Wood] Please document me
pub state: &'a mut State, pub transactions: &'a Vec<Transaction>,
/// TODO [Gav Wood] Please document me
pub archive: &'a Vec<Entry>,
/// TODO [Gav Wood] Please document me /// TODO [Gav Wood] Please document me
pub uncles: &'a Vec<Header>, pub uncles: &'a Vec<Header>,
/// TODO [Gav Wood] Please document me
pub receipts: &'a Vec<Receipt>,
/// TODO [Gav Wood] Please document me
pub state: &'a mut State,
} }
impl Block { impl ExecutedBlock {
/// Create a new block from the given `state`. /// Create a new block from the given `state`.
fn new(state: State) -> Block { fn new(state: State) -> ExecutedBlock { ExecutedBlock { base: Default::default(), receipts: Default::default(), transactions_set: Default::default(), state: state } }
Block {
header: Header::new(),
state: state,
archive: Vec::new(),
archive_set: HashSet::new(),
uncles: Vec::new(),
}
}
/// Get a structure containing individual references to all public fields. /// Get a structure containing individual references to all public fields.
pub fn fields(&mut self) -> BlockRefMut { pub fn fields(&mut self) -> BlockRefMut {
BlockRefMut { BlockRefMut {
header: &self.header, header: &self.base.header,
transactions: &self.base.transactions,
uncles: &self.base.uncles,
state: &mut self.state, state: &mut self.state,
archive: &self.archive, receipts: &self.receipts,
uncles: &self.uncles,
} }
} }
} }
/// Trait for a object that is_a `Block`. /// Trait for a object that is_a `ExecutedBlock`.
pub trait IsBlock { pub trait IsBlock {
/// Get the block associated with this object. /// Get the block associated with this object.
fn block(&self) -> &Block; fn block(&self) -> &ExecutedBlock;
/// Get the header associated with this object's block. /// Get the header associated with this object's block.
fn header(&self) -> &Header { &self.block().header } fn header(&self) -> &Header { &self.block().base.header }
/// Get the final state associated with this object's block. /// Get the final state associated with this object's block.
fn state(&self) -> &State { &self.block().state } fn state(&self) -> &State { &self.block().state }
/// Get all information on transactions in this block. /// Get all information on transactions in this block.
fn archive(&self) -> &Vec<Entry> { &self.block().archive } fn transactions(&self) -> &Vec<Transaction> { &self.block().base.transactions }
/// Get all information on receipts in this block.
fn receipts(&self) -> &Vec<Receipt> { &self.block().receipts }
/// Get all uncles in this block. /// Get all uncles in this block.
fn uncles(&self) -> &Vec<Header> { &self.block().uncles } fn uncles(&self) -> &Vec<Header> { &self.block().base.uncles }
} }
impl IsBlock for Block { impl IsBlock for ExecutedBlock {
fn block(&self) -> &Block { self } fn block(&self) -> &ExecutedBlock { self }
} }
/// Block that is ready for transactions to be added. /// Block that is ready for transactions to be added.
@ -84,7 +130,7 @@ impl IsBlock for Block {
/// It's a bit like a Vec<Transaction>, eccept that whenever a transaction is pushed, we execute it and /// It's a bit like a Vec<Transaction>, eccept that whenever a transaction is pushed, we execute it and
/// maintain the system `state()`. We also archive execution receipts in preparation for later block creation. /// maintain the system `state()`. We also archive execution receipts in preparation for later block creation.
pub struct OpenBlock<'x, 'y> { pub struct OpenBlock<'x, 'y> {
block: Block, block: ExecutedBlock,
engine: &'x Engine, engine: &'x Engine,
last_hashes: &'y LastHashes, last_hashes: &'y LastHashes,
} }
@ -102,7 +148,7 @@ pub struct ClosedBlock<'x, 'y> {
/// ///
/// The block's header has valid seal arguments. The block cannot be reversed into a ClosedBlock or OpenBlock. /// The block's header has valid seal arguments. The block cannot be reversed into a ClosedBlock or OpenBlock.
pub struct SealedBlock { pub struct SealedBlock {
block: Block, block: ExecutedBlock,
uncle_bytes: Bytes, uncle_bytes: Bytes,
} }
@ -110,42 +156,42 @@ impl<'x, 'y> OpenBlock<'x, 'y> {
/// Create a new OpenBlock ready for transaction pushing. /// Create a new OpenBlock ready for transaction pushing.
pub fn new<'a, 'b>(engine: &'a Engine, db: JournalDB, parent: &Header, last_hashes: &'b LastHashes, author: Address, extra_data: Bytes) -> OpenBlock<'a, 'b> { pub fn new<'a, 'b>(engine: &'a Engine, db: JournalDB, parent: &Header, last_hashes: &'b LastHashes, author: Address, extra_data: Bytes) -> OpenBlock<'a, 'b> {
let mut r = OpenBlock { let mut r = OpenBlock {
block: Block::new(State::from_existing(db, parent.state_root().clone(), engine.account_start_nonce())), block: ExecutedBlock::new(State::from_existing(db, parent.state_root().clone(), engine.account_start_nonce())),
engine: engine, engine: engine,
last_hashes: last_hashes, last_hashes: last_hashes,
}; };
r.block.header.set_number(parent.number() + 1); r.block.base.header.set_number(parent.number() + 1);
r.block.header.set_author(author); r.block.base.header.set_author(author);
r.block.header.set_extra_data(extra_data); r.block.base.header.set_extra_data(extra_data);
r.block.header.set_timestamp_now(); r.block.base.header.set_timestamp_now();
engine.populate_from_parent(&mut r.block.header, parent); engine.populate_from_parent(&mut r.block.base.header, parent);
engine.on_new_block(&mut r.block); engine.on_new_block(&mut r.block);
r r
} }
/// Alter the author for the block. /// Alter the author for the block.
pub fn set_author(&mut self, author: Address) { self.block.header.set_author(author); } pub fn set_author(&mut self, author: Address) { self.block.base.header.set_author(author); }
/// Alter the timestamp of the block. /// Alter the timestamp of the block.
pub fn set_timestamp(&mut self, timestamp: u64) { self.block.header.set_timestamp(timestamp); } pub fn set_timestamp(&mut self, timestamp: u64) { self.block.base.header.set_timestamp(timestamp); }
/// Alter the difficulty for the block. /// Alter the difficulty for the block.
pub fn set_difficulty(&mut self, a: U256) { self.block.header.set_difficulty(a); } pub fn set_difficulty(&mut self, a: U256) { self.block.base.header.set_difficulty(a); }
/// Alter the gas limit for the block. /// Alter the gas limit for the block.
pub fn set_gas_limit(&mut self, a: U256) { self.block.header.set_gas_limit(a); } pub fn set_gas_limit(&mut self, a: U256) { self.block.base.header.set_gas_limit(a); }
/// Alter the gas limit for the block. /// Alter the gas limit for the block.
pub fn set_gas_used(&mut self, a: U256) { self.block.header.set_gas_used(a); } pub fn set_gas_used(&mut self, a: U256) { self.block.base.header.set_gas_used(a); }
/// Alter the extra_data for the block. /// Alter the extra_data for the block.
pub fn set_extra_data(&mut self, extra_data: Bytes) -> Result<(), BlockError> { pub fn set_extra_data(&mut self, extra_data: Bytes) -> Result<(), BlockError> {
if extra_data.len() > self.engine.maximum_extra_data_size() { if extra_data.len() > self.engine.maximum_extra_data_size() {
Err(BlockError::ExtraDataOutOfBounds(OutOfBounds{min: None, max: Some(self.engine.maximum_extra_data_size()), found: extra_data.len()})) Err(BlockError::ExtraDataOutOfBounds(OutOfBounds{min: None, max: Some(self.engine.maximum_extra_data_size()), found: extra_data.len()}))
} else { } else {
self.block.header.set_extra_data(extra_data); self.block.base.header.set_extra_data(extra_data);
Ok(()) Ok(())
} }
} }
@ -155,12 +201,12 @@ impl<'x, 'y> OpenBlock<'x, 'y> {
/// NOTE Will check chain constraints and the uncle number but will NOT check /// NOTE Will check chain constraints and the uncle number but will NOT check
/// that the header itself is actually valid. /// that the header itself is actually valid.
pub fn push_uncle(&mut self, valid_uncle_header: Header) -> Result<(), BlockError> { pub fn push_uncle(&mut self, valid_uncle_header: Header) -> Result<(), BlockError> {
if self.block.uncles.len() >= self.engine.maximum_uncle_count() { if self.block.base.uncles.len() >= self.engine.maximum_uncle_count() {
return Err(BlockError::TooManyUncles(OutOfBounds{min: None, max: Some(self.engine.maximum_uncle_count()), found: self.block.uncles.len()})); return Err(BlockError::TooManyUncles(OutOfBounds{min: None, max: Some(self.engine.maximum_uncle_count()), found: self.block.base.uncles.len()}));
} }
// TODO: check number // TODO: check number
// TODO: check not a direct ancestor (use last_hashes for that) // TODO: check not a direct ancestor (use last_hashes for that)
self.block.uncles.push(valid_uncle_header); self.block.base.uncles.push(valid_uncle_header);
Ok(()) Ok(())
} }
@ -168,13 +214,13 @@ impl<'x, 'y> OpenBlock<'x, 'y> {
pub fn env_info(&self) -> EnvInfo { pub fn env_info(&self) -> EnvInfo {
// TODO: memoise. // TODO: memoise.
EnvInfo { EnvInfo {
number: self.block.header.number, number: self.block.base.header.number,
author: self.block.header.author.clone(), author: self.block.base.header.author.clone(),
timestamp: self.block.header.timestamp, timestamp: self.block.base.header.timestamp,
difficulty: self.block.header.difficulty.clone(), difficulty: self.block.base.header.difficulty.clone(),
last_hashes: self.last_hashes.clone(), // TODO: should be a reference. last_hashes: self.last_hashes.clone(), // TODO: should be a reference.
gas_used: self.block.archive.last().map(|t| t.receipt.gas_used).unwrap_or(U256::from(0)), gas_used: self.block.receipts.last().map_or(U256::zero(), |r| r.gas_used),
gas_limit: self.block.header.gas_limit.clone(), gas_limit: self.block.base.header.gas_limit.clone(),
} }
} }
@ -186,9 +232,10 @@ impl<'x, 'y> OpenBlock<'x, 'y> {
// info!("env_info says gas_used={}", env_info.gas_used); // info!("env_info says gas_used={}", env_info.gas_used);
match self.block.state.apply(&env_info, self.engine, &t) { match self.block.state.apply(&env_info, self.engine, &t) {
Ok(receipt) => { Ok(receipt) => {
self.block.archive_set.insert(h.unwrap_or_else(||t.hash())); self.block.transactions_set.insert(h.unwrap_or_else(||t.hash()));
self.block.archive.push(Entry { transaction: t, receipt: receipt }); self.block.base.transactions.push(t);
Ok(&self.block.archive.last().unwrap().receipt) self.block.receipts.push(receipt);
Ok(&self.block.receipts.last().unwrap())
} }
Err(x) => Err(From::from(x)) Err(x) => Err(From::from(x))
} }
@ -198,25 +245,25 @@ impl<'x, 'y> OpenBlock<'x, 'y> {
pub fn close(self) -> ClosedBlock<'x, 'y> { pub fn close(self) -> ClosedBlock<'x, 'y> {
let mut s = self; let mut s = self;
s.engine.on_close_block(&mut s.block); s.engine.on_close_block(&mut s.block);
s.block.header.transactions_root = ordered_trie_root(s.block.archive.iter().map(|ref e| e.transaction.rlp_bytes()).collect()); s.block.base.header.transactions_root = ordered_trie_root(s.block.base.transactions.iter().map(|ref e| e.rlp_bytes()).collect());
let uncle_bytes = s.block.uncles.iter().fold(RlpStream::new_list(s.block.uncles.len()), |mut s, u| {s.append(&u.rlp(Seal::With)); s} ).out(); let uncle_bytes = s.block.base.uncles.iter().fold(RlpStream::new_list(s.block.base.uncles.len()), |mut s, u| {s.append(&u.rlp(Seal::With)); s} ).out();
s.block.header.uncles_hash = uncle_bytes.sha3(); s.block.base.header.uncles_hash = uncle_bytes.sha3();
s.block.header.state_root = s.block.state.root().clone(); s.block.base.header.state_root = s.block.state.root().clone();
s.block.header.receipts_root = ordered_trie_root(s.block.archive.iter().map(|ref e| e.receipt.rlp_bytes()).collect()); s.block.base.header.receipts_root = ordered_trie_root(s.block.receipts.iter().map(|ref r| r.rlp_bytes()).collect());
s.block.header.log_bloom = s.block.archive.iter().fold(LogBloom::zero(), |mut b, e| {b |= &e.receipt.log_bloom; b}); s.block.base.header.log_bloom = s.block.receipts.iter().fold(LogBloom::zero(), |mut b, r| {b |= &r.log_bloom; b});
s.block.header.gas_used = s.block.archive.last().map(|t| t.receipt.gas_used).unwrap_or(U256::from(0)); s.block.base.header.gas_used = s.block.receipts.last().map_or(U256::zero(), |r| r.gas_used);
s.block.header.note_dirty(); s.block.base.header.note_dirty();
ClosedBlock::new(s, uncle_bytes) ClosedBlock::new(s, uncle_bytes)
} }
} }
impl<'x, 'y> IsBlock for OpenBlock<'x, 'y> { impl<'x, 'y> IsBlock for OpenBlock<'x, 'y> {
fn block(&self) -> &Block { &self.block } fn block(&self) -> &ExecutedBlock { &self.block }
} }
impl<'x, 'y> IsBlock for ClosedBlock<'x, 'y> { impl<'x, 'y> IsBlock for ClosedBlock<'x, 'y> {
fn block(&self) -> &Block { &self.open_block.block } fn block(&self) -> &ExecutedBlock { &self.open_block.block }
} }
impl<'x, 'y> ClosedBlock<'x, 'y> { impl<'x, 'y> ClosedBlock<'x, 'y> {
@ -238,7 +285,7 @@ impl<'x, 'y> ClosedBlock<'x, 'y> {
if seal.len() != s.open_block.engine.seal_fields() { if seal.len() != s.open_block.engine.seal_fields() {
return Err(BlockError::InvalidSealArity(Mismatch{expected: s.open_block.engine.seal_fields(), found: seal.len()})); return Err(BlockError::InvalidSealArity(Mismatch{expected: s.open_block.engine.seal_fields(), found: seal.len()}));
} }
s.open_block.block.header.set_seal(seal); s.open_block.block.base.header.set_seal(seal);
Ok(SealedBlock { block: s.open_block.block, uncle_bytes: s.uncle_bytes }) Ok(SealedBlock { block: s.open_block.block, uncle_bytes: s.uncle_bytes })
} }
@ -253,9 +300,9 @@ impl SealedBlock {
/// Get the RLP-encoding of the block. /// Get the RLP-encoding of the block.
pub fn rlp_bytes(&self) -> Bytes { pub fn rlp_bytes(&self) -> Bytes {
let mut block_rlp = RlpStream::new_list(3); let mut block_rlp = RlpStream::new_list(3);
self.block.header.stream_rlp(&mut block_rlp, Seal::With); self.block.base.header.stream_rlp(&mut block_rlp, Seal::With);
block_rlp.append_list(self.block.archive.len()); block_rlp.append_list(self.block.receipts.len());
for e in self.block.archive.iter() { e.transaction.rlp_append(&mut block_rlp); } for t in &self.block.base.transactions { t.rlp_append(&mut block_rlp); }
block_rlp.append_raw(&self.uncle_bytes, 1); block_rlp.append_raw(&self.uncle_bytes, 1);
block_rlp.out() block_rlp.out()
} }
@ -265,7 +312,7 @@ impl SealedBlock {
} }
impl IsBlock for SealedBlock { impl IsBlock for SealedBlock {
fn block(&self) -> &Block { &self.block } fn block(&self) -> &ExecutedBlock { &self.block }
} }
/// Enact the block given by block header, transactions and uncles /// Enact the block given by block header, transactions and uncles

View File

@ -1,12 +1,35 @@
//! A queue of blocks. Sits between network or other I/O and the BlockChain.
//! Sorts them ready for blockchain insertion.
use std::thread::{JoinHandle, self}; use std::thread::{JoinHandle, self};
use std::sync::atomic::{AtomicBool, Ordering as AtomicOrdering}; use std::sync::atomic::{AtomicBool, Ordering as AtomicOrdering};
use util::*; use util::*;
use verification::*; use verification::*;
use error::*; use error::*;
use engine::Engine; use engine::Engine;
use sync::*;
use views::*; use views::*;
use header::*; use header::*;
use service::*;
/// Block queue status
#[derive(Debug)]
pub struct BlockQueueInfo {
/// Indicates that queue is full
pub full: bool,
/// Number of queued blocks pending verification
pub unverified_queue_size: usize,
/// Number of verified queued blocks pending import
pub verified_queue_size: usize,
/// Number of blocks being verified
pub verifying_queue_size: usize,
}
impl BlockQueueInfo {
/// The total size of the queues.
pub fn total_queue_size(&self) -> usize { self.unverified_queue_size + self.verified_queue_size + self.verifying_queue_size }
/// The size of the unverified and verifying queues.
pub fn incomplete_queue_size(&self) -> usize { self.unverified_queue_size + self.verifying_queue_size }
}
/// A queue of blocks. Sits between network or other I/O and the BlockChain. /// A queue of blocks. Sits between network or other I/O and the BlockChain.
/// Sorts them ready for blockchain insertion. /// Sorts them ready for blockchain insertion.
@ -17,6 +40,7 @@ pub struct BlockQueue {
verifiers: Vec<JoinHandle<()>>, verifiers: Vec<JoinHandle<()>>,
deleting: Arc<AtomicBool>, deleting: Arc<AtomicBool>,
ready_signal: Arc<QueueSignal>, ready_signal: Arc<QueueSignal>,
empty: Arc<Condvar>,
processing: HashSet<H256> processing: HashSet<H256>
} }
@ -61,16 +85,19 @@ impl BlockQueue {
let more_to_verify = Arc::new(Condvar::new()); let more_to_verify = Arc::new(Condvar::new());
let ready_signal = Arc::new(QueueSignal { signalled: AtomicBool::new(false), message_channel: message_channel }); let ready_signal = Arc::new(QueueSignal { signalled: AtomicBool::new(false), message_channel: message_channel });
let deleting = Arc::new(AtomicBool::new(false)); let deleting = Arc::new(AtomicBool::new(false));
let empty = Arc::new(Condvar::new());
let mut verifiers: Vec<JoinHandle<()>> = Vec::new(); let mut verifiers: Vec<JoinHandle<()>> = Vec::new();
let thread_count = max(::num_cpus::get(), 2) - 1; let thread_count = max(::num_cpus::get(), 3) - 2;
for _ in 0..thread_count { for i in 0..thread_count {
let verification = verification.clone(); let verification = verification.clone();
let engine = engine.clone(); let engine = engine.clone();
let more_to_verify = more_to_verify.clone(); let more_to_verify = more_to_verify.clone();
let ready_signal = ready_signal.clone(); let ready_signal = ready_signal.clone();
let empty = empty.clone();
let deleting = deleting.clone(); let deleting = deleting.clone();
verifiers.push(thread::spawn(move || BlockQueue::verify(verification, engine, more_to_verify, ready_signal, deleting))); verifiers.push(thread::Builder::new().name(format!("Verifier #{}", i)).spawn(move || BlockQueue::verify(verification, engine, more_to_verify, ready_signal, deleting, empty))
.expect("Error starting block verification thread"));
} }
BlockQueue { BlockQueue {
engine: engine, engine: engine,
@ -80,13 +107,19 @@ impl BlockQueue {
verifiers: verifiers, verifiers: verifiers,
deleting: deleting.clone(), deleting: deleting.clone(),
processing: HashSet::new(), processing: HashSet::new(),
empty: empty.clone(),
} }
} }
fn verify(verification: Arc<Mutex<Verification>>, engine: Arc<Box<Engine>>, wait: Arc<Condvar>, ready: Arc<QueueSignal>, deleting: Arc<AtomicBool>) { fn verify(verification: Arc<Mutex<Verification>>, engine: Arc<Box<Engine>>, wait: Arc<Condvar>, ready: Arc<QueueSignal>, deleting: Arc<AtomicBool>, empty: Arc<Condvar>) {
while !deleting.load(AtomicOrdering::Relaxed) { while !deleting.load(AtomicOrdering::Relaxed) {
{ {
let mut lock = verification.lock().unwrap(); let mut lock = verification.lock().unwrap();
if lock.unverified.is_empty() && lock.verifying.is_empty() {
empty.notify_all();
}
while lock.unverified.is_empty() && !deleting.load(AtomicOrdering::Relaxed) { while lock.unverified.is_empty() && !deleting.load(AtomicOrdering::Relaxed) {
lock = wait.wait(lock).unwrap(); lock = wait.wait(lock).unwrap();
} }
@ -155,36 +188,46 @@ impl BlockQueue {
verification.verifying.clear(); verification.verifying.clear();
} }
/// Wait for queue to be empty
pub fn flush(&mut self) {
let mut verification = self.verification.lock().unwrap();
while !verification.unverified.is_empty() || !verification.verifying.is_empty() {
verification = self.empty.wait(verification).unwrap();
}
}
/// Add a block to the queue. /// Add a block to the queue.
pub fn import_block(&mut self, bytes: Bytes) -> ImportResult { pub fn import_block(&mut self, bytes: Bytes) -> ImportResult {
let header = BlockView::new(&bytes).header(); let header = BlockView::new(&bytes).header();
if self.processing.contains(&header.hash()) { let h = header.hash();
if self.processing.contains(&h) {
return Err(ImportError::AlreadyQueued); return Err(ImportError::AlreadyQueued);
} }
{ {
let mut verification = self.verification.lock().unwrap(); let mut verification = self.verification.lock().unwrap();
if verification.bad.contains(&header.hash()) { if verification.bad.contains(&h) {
return Err(ImportError::Bad(None)); return Err(ImportError::Bad(None));
} }
if verification.bad.contains(&header.parent_hash) { if verification.bad.contains(&header.parent_hash) {
verification.bad.insert(header.hash()); verification.bad.insert(h.clone());
return Err(ImportError::Bad(None)); return Err(ImportError::Bad(None));
} }
} }
match verify_block_basic(&header, &bytes, self.engine.deref().deref()) { match verify_block_basic(&header, &bytes, self.engine.deref().deref()) {
Ok(()) => { Ok(()) => {
self.processing.insert(header.hash()); self.processing.insert(h.clone());
self.verification.lock().unwrap().unverified.push_back(UnVerifiedBlock { header: header, bytes: bytes }); self.verification.lock().unwrap().unverified.push_back(UnVerifiedBlock { header: header, bytes: bytes });
self.more_to_verify.notify_all(); self.more_to_verify.notify_all();
Ok(h)
}, },
Err(err) => { Err(err) => {
warn!(target: "client", "Stage 1 block verification failed for {}\nError: {:?}", BlockView::new(&bytes).header_view().sha3(), err); warn!(target: "client", "Stage 1 block verification failed for {}\nError: {:?}", BlockView::new(&bytes).header_view().sha3(), err);
self.verification.lock().unwrap().bad.insert(header.hash()); self.verification.lock().unwrap().bad.insert(h.clone());
Err(From::from(err))
} }
} }
Ok(())
} }
/// Mark given block and all its children as bad. Stops verification. /// Mark given block and all its children as bad. Stops verification.
@ -204,7 +247,7 @@ impl BlockQueue {
verification.verified = new_verified; verification.verified = new_verified;
} }
/// TODO [arkpar] Please document me /// Removes up to `max` verified blocks from the queue
pub fn drain(&mut self, max: usize) -> Vec<PreVerifiedBlock> { pub fn drain(&mut self, max: usize) -> Vec<PreVerifiedBlock> {
let mut verification = self.verification.lock().unwrap(); let mut verification = self.verification.lock().unwrap();
let count = min(max, verification.verified.len()); let count = min(max, verification.verified.len());
@ -215,8 +258,22 @@ impl BlockQueue {
result.push(block); result.push(block);
} }
self.ready_signal.reset(); self.ready_signal.reset();
if !verification.verified.is_empty() {
self.ready_signal.set();
}
result result
} }
/// Get queue status.
pub fn queue_info(&self) -> BlockQueueInfo {
let verification = self.verification.lock().unwrap();
BlockQueueInfo {
full: false,
verified_queue_size: verification.verified.len(),
unverified_queue_size: verification.unverified.len(),
verifying_queue_size: verification.verifying.len(),
}
}
} }
impl Drop for BlockQueue { impl Drop for BlockQueue {
@ -234,7 +291,7 @@ impl Drop for BlockQueue {
mod tests { mod tests {
use util::*; use util::*;
use spec::*; use spec::*;
use queue::*; use block_queue::*;
#[test] #[test]
fn test_block_queue() { fn test_block_queue() {

View File

@ -107,6 +107,11 @@ pub trait BlockProvider {
fn genesis_hash(&self) -> H256 { fn genesis_hash(&self) -> H256 {
self.block_hash(0).expect("Genesis hash should always exist") self.block_hash(0).expect("Genesis hash should always exist")
} }
/// Returns the header of the genesis block.
fn genesis_header(&self) -> Header {
self.block_header(&self.genesis_hash()).unwrap()
}
} }
#[derive(Debug, Hash, Eq, PartialEq, Clone)] #[derive(Debug, Hash, Eq, PartialEq, Clone)]
@ -153,9 +158,8 @@ impl BlockProvider for BlockChain {
fn block(&self, hash: &H256) -> Option<Bytes> { fn block(&self, hash: &H256) -> Option<Bytes> {
{ {
let read = self.blocks.read().unwrap(); let read = self.blocks.read().unwrap();
match read.get(hash) { if let Some(v) = read.get(hash) {
Some(v) => return Some(v.clone()), return Some(v.clone());
None => ()
} }
} }
@ -188,7 +192,7 @@ impl BlockProvider for BlockChain {
const COLLECTION_QUEUE_SIZE: usize = 2; const COLLECTION_QUEUE_SIZE: usize = 2;
const MIN_CACHE_SIZE: usize = 1; const MIN_CACHE_SIZE: usize = 1;
const MAX_CACHE_SIZE: usize = 1024 * 1024 * 1; const MAX_CACHE_SIZE: usize = 1024 * 1024;
impl BlockChain { impl BlockChain {
/// Create new instance of blockchain from given Genesis /// Create new instance of blockchain from given Genesis
@ -284,13 +288,6 @@ impl BlockChain {
bc bc
} }
/// Ensure that the best block does indeed have a state_root in the state DB.
/// If it doesn't, then rewind down until we find one that does and delete data to ensure that
/// later blocks will be reimported.
pub fn ensure_good(&mut self, _state: &JournalDB) {
unimplemented!();
}
/// Returns a tree route between `from` and `to`, which is a tuple of: /// Returns a tree route between `from` and `to`, which is a tuple of:
/// ///
/// - a vector of hashes of all blocks, ordered from `from` to `to`. /// - a vector of hashes of all blocks, ordered from `from` to `to`.
@ -342,19 +339,19 @@ impl BlockChain {
Some(h) => h, Some(h) => h,
None => return None, None => return None,
}; };
Some(self._tree_route((from_details, from), (to_details, to))) Some(self._tree_route((&from_details, &from), (&to_details, &to)))
} }
/// Similar to `tree_route` function, but can be used to return a route /// Similar to `tree_route` function, but can be used to return a route
/// between blocks which may not be in database yet. /// between blocks which may not be in database yet.
fn _tree_route(&self, from: (BlockDetails, H256), to: (BlockDetails, H256)) -> TreeRoute { fn _tree_route(&self, from: (&BlockDetails, &H256), to: (&BlockDetails, &H256)) -> TreeRoute {
let mut from_branch = vec![]; let mut from_branch = vec![];
let mut to_branch = vec![]; let mut to_branch = vec![];
let mut from_details = from.0; let mut from_details = from.0.clone();
let mut to_details = to.0; let mut to_details = to.0.clone();
let mut current_from = from.1; let mut current_from = from.1.clone();
let mut current_to = to.1; let mut current_to = to.1.clone();
// reset from && to to the same level // reset from && to to the same level
while from_details.number > to_details.number { while from_details.number > to_details.number {
@ -393,7 +390,6 @@ impl BlockChain {
} }
} }
/// Inserts the block into backing cache database. /// Inserts the block into backing cache database.
/// Expects the block to be valid and already verified. /// Expects the block to be valid and already verified.
/// If the block is already known, does nothing. /// If the block is already known, does nothing.
@ -409,7 +405,7 @@ impl BlockChain {
// store block in db // store block in db
self.blocks_db.put(&hash, &bytes).unwrap(); self.blocks_db.put(&hash, &bytes).unwrap();
let (batch, new_best) = self.block_to_extras_insert_batch(bytes); let (batch, new_best, details) = self.block_to_extras_insert_batch(bytes);
// update best block // update best block
let mut best_block = self.best_block.write().unwrap(); let mut best_block = self.best_block.write().unwrap();
@ -420,6 +416,8 @@ impl BlockChain {
// update caches // update caches
let mut write = self.block_details.write().unwrap(); let mut write = self.block_details.write().unwrap();
write.remove(&header.parent_hash()); write.remove(&header.parent_hash());
write.insert(hash.clone(), details);
self.note_used(CacheID::Block(hash));
// update extras database // update extras database
self.extras_db.write(batch).unwrap(); self.extras_db.write(batch).unwrap();
@ -427,7 +425,7 @@ impl BlockChain {
/// Transforms block into WriteBatch that may be written into database /// Transforms block into WriteBatch that may be written into database
/// Additionally, if it's new best block it returns new best block object. /// Additionally, if it's new best block it returns new best block object.
fn block_to_extras_insert_batch(&self, bytes: &[u8]) -> (WriteBatch, Option<BestBlock>) { fn block_to_extras_insert_batch(&self, bytes: &[u8]) -> (WriteBatch, Option<BestBlock>, BlockDetails) {
// create views onto rlp // create views onto rlp
let block = BlockView::new(bytes); let block = BlockView::new(bytes);
let header = block.header_view(); let header = block.header_view();
@ -459,7 +457,7 @@ impl BlockChain {
// if it's not new best block, just return // if it's not new best block, just return
if !is_new_best { if !is_new_best {
return (batch, None); return (batch, None, details);
} }
// if its new best block we need to make sure that all ancestors // if its new best block we need to make sure that all ancestors
@ -467,7 +465,7 @@ impl BlockChain {
// find the route between old best block and the new one // find the route between old best block and the new one
let best_hash = self.best_block_hash(); let best_hash = self.best_block_hash();
let best_details = self.block_details(&best_hash).expect("best block hash is invalid!"); let best_details = self.block_details(&best_hash).expect("best block hash is invalid!");
let route = self._tree_route((best_details, best_hash), (details, hash.clone())); let route = self._tree_route((&best_details, &best_hash), (&details, &hash));
match route.blocks.len() { match route.blocks.len() {
// its our parent // its our parent
@ -494,7 +492,7 @@ impl BlockChain {
total_difficulty: total_difficulty total_difficulty: total_difficulty
}; };
(batch, Some(best_block)) (batch, Some(best_block), details)
} }
/// Returns true if transaction is known. /// Returns true if transaction is known.
@ -527,9 +525,8 @@ impl BlockChain {
K: ExtrasSliceConvertable + Eq + Hash + Clone { K: ExtrasSliceConvertable + Eq + Hash + Clone {
{ {
let read = cache.read().unwrap(); let read = cache.read().unwrap();
match read.get(hash) { if let Some(v) = read.get(hash) {
Some(v) => return Some(v.clone()), return Some(v.clone());
None => ()
} }
} }
@ -549,9 +546,8 @@ impl BlockChain {
T: ExtrasIndexable { T: ExtrasIndexable {
{ {
let read = cache.read().unwrap(); let read = cache.read().unwrap();
match read.get(hash) { if let Some(_) = read.get(hash) {
Some(_) => return true, return true;
None => ()
} }
} }
@ -569,15 +565,6 @@ impl BlockChain {
} }
} }
/// Tries to squeeze the cache if its too big.
pub fn squeeze_to_fit(&self, size: CacheSize) {
self.blocks.write().unwrap().squeeze(size.blocks);
self.block_details.write().unwrap().squeeze(size.block_details);
self.transaction_addresses.write().unwrap().squeeze(size.transaction_addresses);
self.block_logs.write().unwrap().squeeze(size.block_logs);
self.blocks_blooms.write().unwrap().squeeze(size.blocks_blooms);
}
/// Let the cache system know that a cacheable item has been used. /// Let the cache system know that a cacheable item has been used.
fn note_used(&self, id: CacheID) { fn note_used(&self, id: CacheID) {
let mut cache_man = self.cache_man.write().unwrap(); let mut cache_man = self.cache_man.write().unwrap();
@ -631,20 +618,18 @@ impl BlockChain {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use std::env;
use std::str::FromStr; use std::str::FromStr;
use rustc_serialize::hex::FromHex; use rustc_serialize::hex::FromHex;
use util::hash::*; use util::hash::*;
use blockchain::*; use blockchain::*;
use tests::helpers::*;
#[test] #[test]
fn valid_tests_extra32() { fn valid_tests_extra32() {
let genesis = "f901fcf901f7a00000000000000000000000000000000000000000000000000000000000000000a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347948888f1f195afa192cfee860698584c030f4c9db1a0925002c3260b44e44c3edebad1cc442142b03020209df1ab8bb86752edbd2cd7a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421b90100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008302000080832fefd8808454c98c8142a0363659b251bf8b819179874c8cce7b9b983d7f3704cbb58a3b334431f7032871889032d09c281e1236c0c0".from_hex().unwrap(); let genesis = "f901fcf901f7a00000000000000000000000000000000000000000000000000000000000000000a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347948888f1f195afa192cfee860698584c030f4c9db1a0925002c3260b44e44c3edebad1cc442142b03020209df1ab8bb86752edbd2cd7a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421b90100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008302000080832fefd8808454c98c8142a0363659b251bf8b819179874c8cce7b9b983d7f3704cbb58a3b334431f7032871889032d09c281e1236c0c0".from_hex().unwrap();
let mut dir = env::temp_dir(); let temp = RandomTempPath::new();
dir.push(H32::random().hex()); let bc = BlockChain::new(&genesis, temp.as_path());
let bc = BlockChain::new(&genesis, &dir);
let genesis_hash = H256::from_str("3caa2203f3d7c136c0295ed128a7d31cea520b1ca5e27afe17d0853331798942").unwrap(); let genesis_hash = H256::from_str("3caa2203f3d7c136c0295ed128a7d31cea520b1ca5e27afe17d0853331798942").unwrap();
@ -670,6 +655,7 @@ mod tests {
} }
#[test] #[test]
#[allow(cyclomatic_complexity)]
fn test_small_fork() { fn test_small_fork() {
let genesis = "f901fcf901f7a00000000000000000000000000000000000000000000000000000000000000000a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347948888f1f195afa192cfee860698584c030f4c9db1a07dba07d6b448a186e9612e5f737d1c909dce473e53199901a302c00646d523c1a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421b90100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008302000080832fefd8808454c98c8142a059262c330941f3fe2a34d16d6e3c7b30d2ceb37c6a0e9a994c494ee1a61d2410885aa4c8bf8e56e264c0c0".from_hex().unwrap(); let genesis = "f901fcf901f7a00000000000000000000000000000000000000000000000000000000000000000a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347948888f1f195afa192cfee860698584c030f4c9db1a07dba07d6b448a186e9612e5f737d1c909dce473e53199901a302c00646d523c1a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421b90100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008302000080832fefd8808454c98c8142a059262c330941f3fe2a34d16d6e3c7b30d2ceb37c6a0e9a994c494ee1a61d2410885aa4c8bf8e56e264c0c0".from_hex().unwrap();
let b1 = "f90261f901f9a05716670833ec874362d65fea27a7cd35af5897d275b31a44944113111e4e96d2a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347948888f1f195afa192cfee860698584c030f4c9db1a0cb52de543653d86ccd13ba3ddf8b052525b04231c6884a4db3188a184681d878a0e78628dd45a1f8dc495594d83b76c588a3ee67463260f8b7d4a42f574aeab29aa0e9244cf7503b79c03d3a099e07a80d2dbc77bb0b502d8a89d51ac0d68dd31313b90100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008302000001832fefd882520884562791e580a051b3ecba4e3f2b49c11d42dd0851ec514b1be3138080f72a2b6e83868275d98f8877671f479c414b47f862f86080018304cb2f94095e7baea6a6c7c4c2dfeb977efac326af552d870a801ca09e2709d7ec9bbe6b1bbbf0b2088828d14cd5e8642a1fee22dc74bfa89761a7f9a04bd8813dee4be989accdb708b1c2e325a7e9c695a8024e30e89d6c644e424747c0".from_hex().unwrap(); let b1 = "f90261f901f9a05716670833ec874362d65fea27a7cd35af5897d275b31a44944113111e4e96d2a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347948888f1f195afa192cfee860698584c030f4c9db1a0cb52de543653d86ccd13ba3ddf8b052525b04231c6884a4db3188a184681d878a0e78628dd45a1f8dc495594d83b76c588a3ee67463260f8b7d4a42f574aeab29aa0e9244cf7503b79c03d3a099e07a80d2dbc77bb0b502d8a89d51ac0d68dd31313b90100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008302000001832fefd882520884562791e580a051b3ecba4e3f2b49c11d42dd0851ec514b1be3138080f72a2b6e83868275d98f8877671f479c414b47f862f86080018304cb2f94095e7baea6a6c7c4c2dfeb977efac326af552d870a801ca09e2709d7ec9bbe6b1bbbf0b2088828d14cd5e8642a1fee22dc74bfa89761a7f9a04bd8813dee4be989accdb708b1c2e325a7e9c695a8024e30e89d6c644e424747c0".from_hex().unwrap();
@ -686,10 +672,8 @@ mod tests {
// b3a is a part of canon chain, whereas b3b is part of sidechain // b3a is a part of canon chain, whereas b3b is part of sidechain
let best_block_hash = H256::from_str("c208f88c9f5bf7e00840439742c12e5226d9752981f3ec0521bdcb6dd08af277").unwrap(); let best_block_hash = H256::from_str("c208f88c9f5bf7e00840439742c12e5226d9752981f3ec0521bdcb6dd08af277").unwrap();
let mut dir = env::temp_dir(); let temp = RandomTempPath::new();
dir.push(H32::random().hex()); let bc = BlockChain::new(&genesis, temp.as_path());
let bc = BlockChain::new(&genesis, &dir);
bc.insert_block(&b1); bc.insert_block(&b1);
bc.insert_block(&b2); bc.insert_block(&b2);
bc.insert_block(&b3a); bc.insert_block(&b3a);
@ -766,18 +750,16 @@ mod tests {
let genesis_hash = H256::from_str("5716670833ec874362d65fea27a7cd35af5897d275b31a44944113111e4e96d2").unwrap(); let genesis_hash = H256::from_str("5716670833ec874362d65fea27a7cd35af5897d275b31a44944113111e4e96d2").unwrap();
let b1_hash = H256::from_str("437e51676ff10756fcfee5edd9159fa41dbcb1b2c592850450371cbecd54ee4f").unwrap(); let b1_hash = H256::from_str("437e51676ff10756fcfee5edd9159fa41dbcb1b2c592850450371cbecd54ee4f").unwrap();
let mut dir = env::temp_dir(); let temp = RandomTempPath::new();
dir.push(H32::random().hex());
{ {
let bc = BlockChain::new(&genesis, &dir); let bc = BlockChain::new(&genesis, temp.as_path());
assert_eq!(bc.best_block_hash(), genesis_hash); assert_eq!(bc.best_block_hash(), genesis_hash);
bc.insert_block(&b1); bc.insert_block(&b1);
assert_eq!(bc.best_block_hash(), b1_hash); assert_eq!(bc.best_block_hash(), b1_hash);
} }
{ {
let bc = BlockChain::new(&genesis, &dir); let bc = BlockChain::new(&genesis, temp.as_path());
assert_eq!(bc.best_block_hash(), b1_hash); assert_eq!(bc.best_block_hash(), b1_hash);
} }
} }

View File

@ -94,16 +94,13 @@ pub fn new_builtin_exec(name: &str) -> Option<Box<Fn(&[u8], &mut [u8])>> {
if it.v == H256::from(&U256::from(27)) || it.v == H256::from(&U256::from(28)) { if it.v == H256::from(&U256::from(27)) || it.v == H256::from(&U256::from(28)) {
let s = Signature::from_rsv(&it.r, &it.s, it.v[31] - 27); let s = Signature::from_rsv(&it.r, &it.s, it.v[31] - 27);
if ec::is_valid(&s) { if ec::is_valid(&s) {
match ec::recover(&s, &it.hash) { if let Ok(p) = ec::recover(&s, &it.hash) {
Ok(p) => {
let r = p.as_slice().sha3(); let r = p.as_slice().sha3();
// NICE: optimise and separate out into populate-like function // NICE: optimise and separate out into populate-like function
for i in 0..min(32, output.len()) { for i in 0..min(32, output.len()) {
output[i] = if i < 12 {0} else {r[i]}; output[i] = if i < 12 {0} else {r[i]};
} }
} }
_ => {}
};
} }
} }
})), })),

View File

@ -4,10 +4,12 @@ use blockchain::{BlockChain, BlockProvider, CacheSize};
use views::BlockView; use views::BlockView;
use error::*; use error::*;
use header::BlockNumber; use header::BlockNumber;
use state::State;
use spec::Spec; use spec::Spec;
use engine::Engine; use engine::Engine;
use queue::BlockQueue; use views::HeaderView;
use sync::NetSyncMessage; use block_queue::{BlockQueue, BlockQueueInfo};
use service::NetSyncMessage;
use env_info::LastHashes; use env_info::LastHashes;
use verification::*; use verification::*;
use block::*; use block::*;
@ -46,13 +48,6 @@ impl fmt::Display for BlockChainInfo {
} }
} }
/// Block queue status
#[derive(Debug)]
pub struct BlockQueueStatus {
/// TODO [arkpar] Please document me
pub full: bool,
}
/// TODO [arkpar] Please document me /// TODO [arkpar] Please document me
pub type TreeRoute = ::blockchain::TreeRoute; pub type TreeRoute = ::blockchain::TreeRoute;
@ -71,6 +66,9 @@ pub trait BlockChainClient : Sync + Send {
/// Get block status by block header hash. /// Get block status by block header hash.
fn block_status(&self, hash: &H256) -> BlockStatus; fn block_status(&self, hash: &H256) -> BlockStatus;
/// Get block total difficulty.
fn block_total_difficulty(&self, hash: &H256) -> Option<U256>;
/// Get raw block header data by block number. /// Get raw block header data by block number.
fn block_header_at(&self, n: BlockNumber) -> Option<Bytes>; fn block_header_at(&self, n: BlockNumber) -> Option<Bytes>;
@ -84,6 +82,9 @@ pub trait BlockChainClient : Sync + Send {
/// Get block status by block number. /// Get block status by block number.
fn block_status_at(&self, n: BlockNumber) -> BlockStatus; fn block_status_at(&self, n: BlockNumber) -> BlockStatus;
/// Get block total difficulty.
fn block_total_difficulty_at(&self, n: BlockNumber) -> Option<U256>;
/// Get a tree route between `from` and `to`. /// Get a tree route between `from` and `to`.
/// See `BlockChain::tree_route`. /// See `BlockChain::tree_route`.
fn tree_route(&self, from: &H256, to: &H256) -> Option<TreeRoute>; fn tree_route(&self, from: &H256, to: &H256) -> Option<TreeRoute>;
@ -95,16 +96,21 @@ pub trait BlockChainClient : Sync + Send {
fn block_receipts(&self, hash: &H256) -> Option<Bytes>; fn block_receipts(&self, hash: &H256) -> Option<Bytes>;
/// Import a block into the blockchain. /// Import a block into the blockchain.
fn import_block(&mut self, bytes: Bytes) -> ImportResult; fn import_block(&self, bytes: Bytes) -> ImportResult;
/// Get block queue information. /// Get block queue information.
fn queue_status(&self) -> BlockQueueStatus; fn queue_info(&self) -> BlockQueueInfo;
/// Clear block queue and abort all import activity. /// Clear block queue and abort all import activity.
fn clear_queue(&mut self); fn clear_queue(&self);
/// Get blockchain information. /// Get blockchain information.
fn chain_info(&self) -> BlockChainInfo; fn chain_info(&self) -> BlockChainInfo;
/// Get the best block header.
fn best_block_header(&self) -> Bytes {
self.block_header(&self.chain_info().best_block_hash).unwrap()
}
} }
#[derive(Default, Clone, Debug, Eq, PartialEq)] #[derive(Default, Clone, Debug, Eq, PartialEq)]
@ -128,24 +134,29 @@ impl ClientReport {
} }
/// Blockchain database client backed by a persistent database. Owns and manages a blockchain and a block queue. /// Blockchain database client backed by a persistent database. Owns and manages a blockchain and a block queue.
/// Call `import_block()` to import a block asynchronously; `flush_queue()` flushes the queue.
pub struct Client { pub struct Client {
chain: Arc<RwLock<BlockChain>>, chain: Arc<RwLock<BlockChain>>,
engine: Arc<Box<Engine>>, engine: Arc<Box<Engine>>,
state_db: JournalDB, state_db: JournalDB,
queue: BlockQueue, block_queue: RwLock<BlockQueue>,
report: ClientReport, report: RwLock<ClientReport>,
uncommited_states: RwLock<HashMap<H256, JournalDB>>,
import_lock: Mutex<()>
} }
const HISTORY: u64 = 1000; const HISTORY: u64 = 1000;
impl Client { impl Client {
/// Create a new client with given spec and DB path. /// Create a new client with given spec and DB path.
pub fn new(spec: Spec, path: &Path, message_channel: IoChannel<NetSyncMessage> ) -> Result<Client, Error> { pub fn new(spec: Spec, path: &Path, message_channel: IoChannel<NetSyncMessage> ) -> Result<Arc<Client>, Error> {
let chain = Arc::new(RwLock::new(BlockChain::new(&spec.genesis_block(), path))); let gb = spec.genesis_block();
let chain = Arc::new(RwLock::new(BlockChain::new(&gb, path)));
let mut opts = Options::new(); let mut opts = Options::new();
opts.create_if_missing(true);
opts.set_max_open_files(256); opts.set_max_open_files(256);
/*opts.set_use_fsync(false); opts.create_if_missing(true);
opts.set_use_fsync(false);
/*
opts.set_bytes_per_sync(8388608); opts.set_bytes_per_sync(8388608);
opts.set_disable_data_sync(false); opts.set_disable_data_sync(false);
opts.set_block_cache_size_mb(1024); opts.set_block_cache_size_mb(1024);
@ -164,37 +175,38 @@ impl Client {
let mut state_path = path.to_path_buf(); let mut state_path = path.to_path_buf();
state_path.push("state"); state_path.push("state");
let db = DB::open(&opts, state_path.to_str().unwrap()).unwrap(); let db = Arc::new(DB::open(&opts, state_path.to_str().unwrap()).unwrap());
let mut state_db = JournalDB::new(db);
let engine = Arc::new(try!(spec.to_engine())); let engine = Arc::new(try!(spec.to_engine()));
let mut state_db = JournalDB::new_with_arc(db.clone());
if engine.spec().ensure_db_good(&mut state_db) { if engine.spec().ensure_db_good(&mut state_db) {
state_db.commit(0, &engine.spec().genesis_header().hash(), None).expect("Error commiting genesis state to state DB"); state_db.commit(0, &engine.spec().genesis_header().hash(), None).expect("Error commiting genesis state to state DB");
} }
Ok(Arc::new(Client {
// chain.write().unwrap().ensure_good(&state_db);
Ok(Client {
chain: chain, chain: chain,
engine: engine.clone(), engine: engine.clone(),
state_db: state_db, state_db: state_db,
queue: BlockQueue::new(engine, message_channel), block_queue: RwLock::new(BlockQueue::new(engine, message_channel)),
report: Default::default(), report: RwLock::new(Default::default()),
}) uncommited_states: RwLock::new(HashMap::new()),
import_lock: Mutex::new(()),
}))
}
/// Flush the block import queue.
pub fn flush_queue(&self) {
self.block_queue.write().unwrap().flush();
} }
/// This is triggered by a message coming from a block queue when the block is ready for insertion /// This is triggered by a message coming from a block queue when the block is ready for insertion
pub fn import_verified_blocks(&mut self) { pub fn import_verified_blocks(&self, _io: &IoChannel<NetSyncMessage>) -> usize {
let mut ret = 0;
let mut bad = HashSet::new(); let mut bad = HashSet::new();
let blocks = self.queue.drain(128); let _import_lock = self.import_lock.lock();
if blocks.is_empty() { let blocks = self.block_queue.write().unwrap().drain(128);
return;
}
for block in blocks { for block in blocks {
if bad.contains(&block.header.parent_hash) { if bad.contains(&block.header.parent_hash) {
self.queue.mark_as_bad(&block.header.hash()); self.block_queue.write().unwrap().mark_as_bad(&block.header.hash());
bad.insert(block.header.hash()); bad.insert(block.header.hash());
continue; continue;
} }
@ -202,17 +214,17 @@ impl Client {
let header = &block.header; let header = &block.header;
if let Err(e) = verify_block_family(&header, &block.bytes, self.engine.deref().deref(), self.chain.read().unwrap().deref()) { if let Err(e) = verify_block_family(&header, &block.bytes, self.engine.deref().deref(), self.chain.read().unwrap().deref()) {
warn!(target: "client", "Stage 3 block verification failed for #{} ({})\nError: {:?}", header.number(), header.hash(), e); warn!(target: "client", "Stage 3 block verification failed for #{} ({})\nError: {:?}", header.number(), header.hash(), e);
self.queue.mark_as_bad(&header.hash()); self.block_queue.write().unwrap().mark_as_bad(&header.hash());
bad.insert(block.header.hash()); bad.insert(block.header.hash());
return; break;
}; };
let parent = match self.chain.read().unwrap().block_header(&header.parent_hash) { let parent = match self.chain.read().unwrap().block_header(&header.parent_hash) {
Some(p) => p, Some(p) => p,
None => { None => {
warn!(target: "client", "Block import failed for #{} ({}): Parent not found ({}) ", header.number(), header.hash(), header.parent_hash); warn!(target: "client", "Block import failed for #{} ({}): Parent not found ({}) ", header.number(), header.hash(), header.parent_hash);
self.queue.mark_as_bad(&header.hash()); self.block_queue.write().unwrap().mark_as_bad(&header.hash());
bad.insert(block.header.hash()); bad.insert(block.header.hash());
return; break;
}, },
}; };
// build last hashes // build last hashes
@ -228,19 +240,20 @@ impl Client {
} }
} }
let result = match enact_verified(&block, self.engine.deref().deref(), self.state_db.clone(), &parent, &last_hashes) { let db = self.state_db.clone();
let result = match enact_verified(&block, self.engine.deref().deref(), db, &parent, &last_hashes) {
Ok(b) => b, Ok(b) => b,
Err(e) => { Err(e) => {
warn!(target: "client", "Block import failed for #{} ({})\nError: {:?}", header.number(), header.hash(), e); warn!(target: "client", "Block import failed for #{} ({})\nError: {:?}", header.number(), header.hash(), e);
bad.insert(block.header.hash()); bad.insert(block.header.hash());
self.queue.mark_as_bad(&header.hash()); self.block_queue.write().unwrap().mark_as_bad(&header.hash());
return; break;
} }
}; };
if let Err(e) = verify_block_final(&header, result.block().header()) { if let Err(e) = verify_block_final(&header, result.block().header()) {
warn!(target: "client", "Stage 4 block verification failed for #{} ({})\nError: {:?}", header.number(), header.hash(), e); warn!(target: "client", "Stage 4 block verification failed for #{} ({})\nError: {:?}", header.number(), header.hash(), e);
self.queue.mark_as_bad(&header.hash()); self.block_queue.write().unwrap().mark_as_bad(&header.hash());
return; break;
} }
self.chain.write().unwrap().insert_block(&block.bytes); //TODO: err here? self.chain.write().unwrap().insert_block(&block.bytes); //TODO: err here?
@ -249,13 +262,24 @@ impl Client {
Ok(_) => (), Ok(_) => (),
Err(e) => { Err(e) => {
warn!(target: "client", "State DB commit failed: {:?}", e); warn!(target: "client", "State DB commit failed: {:?}", e);
return; break;
} }
} }
self.report.accrue_block(&block); self.report.write().unwrap().accrue_block(&block);
trace!(target: "client", "Imported #{} ({})", header.number(), header.hash()); trace!(target: "client", "Imported #{} ({})", header.number(), header.hash());
ret += 1;
} }
ret
}
/// Clear cached state overlay
pub fn clear_state(&self, hash: &H256) {
self.uncommited_states.write().unwrap().remove(hash);
}
/// Get a copy of the best block's state.
pub fn state(&self) -> State {
State::from_existing(self.state_db.clone(), HeaderView::new(&self.best_block_header()).state_root(), self.engine.account_start_nonce())
} }
/// Get info on the cache. /// Get info on the cache.
@ -265,7 +289,7 @@ impl Client {
/// Get the report. /// Get the report.
pub fn report(&self) -> ClientReport { pub fn report(&self) -> ClientReport {
self.report.clone() self.report.read().unwrap().clone()
} }
/// Tick the client. /// Tick the client.
@ -297,6 +321,10 @@ impl BlockChainClient for Client {
if self.chain.read().unwrap().is_known(&hash) { BlockStatus::InChain } else { BlockStatus::Unknown } if self.chain.read().unwrap().is_known(&hash) { BlockStatus::InChain } else { BlockStatus::Unknown }
} }
fn block_total_difficulty(&self, hash: &H256) -> Option<U256> {
self.chain.read().unwrap().block_details(hash).map(|d| d.total_difficulty)
}
fn block_header_at(&self, n: BlockNumber) -> Option<Bytes> { fn block_header_at(&self, n: BlockNumber) -> Option<Bytes> {
self.chain.read().unwrap().block_hash(n).and_then(|h| self.block_header(&h)) self.chain.read().unwrap().block_hash(n).and_then(|h| self.block_header(&h))
} }
@ -316,6 +344,10 @@ impl BlockChainClient for Client {
} }
} }
fn block_total_difficulty_at(&self, n: BlockNumber) -> Option<U256> {
self.chain.read().unwrap().block_hash(n).and_then(|h| self.block_total_difficulty(&h))
}
fn tree_route(&self, from: &H256, to: &H256) -> Option<TreeRoute> { fn tree_route(&self, from: &H256, to: &H256) -> Option<TreeRoute> {
self.chain.read().unwrap().tree_route(from.clone(), to.clone()) self.chain.read().unwrap().tree_route(from.clone(), to.clone())
} }
@ -328,21 +360,20 @@ impl BlockChainClient for Client {
unimplemented!(); unimplemented!();
} }
fn import_block(&mut self, bytes: Bytes) -> ImportResult { fn import_block(&self, bytes: Bytes) -> ImportResult {
let header = BlockView::new(&bytes).header(); let header = BlockView::new(&bytes).header();
if self.chain.read().unwrap().is_known(&header.hash()) { if self.chain.read().unwrap().is_known(&header.hash()) {
return Err(ImportError::AlreadyInChain); return Err(ImportError::AlreadyInChain);
} }
self.queue.import_block(bytes) self.block_queue.write().unwrap().import_block(bytes)
} }
fn queue_status(&self) -> BlockQueueStatus { fn queue_info(&self) -> BlockQueueInfo {
BlockQueueStatus { self.block_queue.read().unwrap().queue_info()
full: false
}
} }
fn clear_queue(&mut self) { fn clear_queue(&self) {
self.block_queue.write().unwrap().clear();
} }
fn chain_info(&self) -> BlockChainInfo { fn chain_info(&self) -> BlockChainInfo {

View File

@ -1,5 +1,5 @@
use common::*; use common::*;
use block::Block; use block::ExecutedBlock;
use spec::Spec; use spec::Spec;
use evm::Schedule; use evm::Schedule;
use evm::Factory; use evm::Factory;
@ -37,9 +37,9 @@ pub trait Engine : Sync + Send {
fn account_start_nonce(&self) -> U256 { decode(&self.spec().engine_params.get("accountStartNonce").unwrap()) } fn account_start_nonce(&self) -> U256 { decode(&self.spec().engine_params.get("accountStartNonce").unwrap()) }
/// Block transformation functions, before and after the transactions. /// Block transformation functions, before and after the transactions.
fn on_new_block(&self, _block: &mut Block) {} fn on_new_block(&self, _block: &mut ExecutedBlock) {}
/// TODO [Gav Wood] Please document me /// TODO [Gav Wood] Please document me
fn on_close_block(&self, _block: &mut Block) {} fn on_close_block(&self, _block: &mut ExecutedBlock) {}
// TODO: consider including State in the params for verification functions. // TODO: consider including State in the params for verification functions.
/// Phase 1 quick block verification. Only does checks that are cheap. `block` (the header's full block) /// Phase 1 quick block verification. Only does checks that are cheap. `block` (the header's full block)

View File

@ -25,8 +25,14 @@ pub struct EnvInfo {
} }
impl EnvInfo { impl EnvInfo {
/// TODO [debris] Please document me /// Create empty env_info initialized with zeros
pub fn new() -> EnvInfo { pub fn new() -> EnvInfo {
EnvInfo::default()
}
}
impl Default for EnvInfo {
fn default() -> Self {
EnvInfo { EnvInfo {
number: 0, number: 0,
author: Address::new(), author: Address::new(),
@ -53,11 +59,3 @@ impl FromJson for EnvInfo {
} }
} }
} }
/// TODO: it should be the other way around.
/// `new` should call `default`.
impl Default for EnvInfo {
fn default() -> Self {
EnvInfo::new()
}
}

View File

@ -120,7 +120,9 @@ pub enum BlockError {
/// TODO [arkpar] Please document me /// TODO [arkpar] Please document me
InvalidParentHash(Mismatch<H256>), InvalidParentHash(Mismatch<H256>),
/// TODO [arkpar] Please document me /// TODO [arkpar] Please document me
InvalidNumber(OutOfBounds<BlockNumber>), InvalidNumber(Mismatch<BlockNumber>),
/// Block number isn't sensible.
RidiculousNumber(OutOfBounds<BlockNumber>),
/// TODO [arkpar] Please document me /// TODO [arkpar] Please document me
UnknownParent(H256), UnknownParent(H256),
/// TODO [Gav Wood] Please document me /// TODO [Gav Wood] Please document me
@ -145,7 +147,7 @@ impl From<Error> for ImportError {
} }
/// Result of import block operation. /// Result of import block operation.
pub type ImportResult = Result<(), ImportError>; pub type ImportResult = Result<H256, ImportError>;
#[derive(Debug)] #[derive(Debug)]
/// General error type which should be capable of representing all errors in ethcore. /// General error type which should be capable of representing all errors in ethcore.

View File

@ -32,13 +32,13 @@ impl Ethash {
} }
fn u64_param(&self, name: &str) -> u64 { fn u64_param(&self, name: &str) -> u64 {
*self.u64_params.write().unwrap().entry(name.to_string()).or_insert_with(|| *self.u64_params.write().unwrap().entry(name.to_owned()).or_insert_with(||
self.spec().engine_params.get(name).map(|a| decode(&a)).unwrap_or(0u64)) self.spec().engine_params.get(name).map_or(0u64, |a| decode(&a)))
} }
fn u256_param(&self, name: &str) -> U256 { fn u256_param(&self, name: &str) -> U256 {
*self.u256_params.write().unwrap().entry(name.to_string()).or_insert_with(|| *self.u256_params.write().unwrap().entry(name.to_owned()).or_insert_with(||
self.spec().engine_params.get(name).map(|a| decode(&a)).unwrap_or(x!(0))) self.spec().engine_params.get(name).map_or(x!(0), |a| decode(&a)))
} }
} }
@ -83,8 +83,8 @@ impl Engine for Ethash {
/// Apply the block reward on finalisation of the block. /// Apply the block reward on finalisation of the block.
/// This assumes that all uncles are valid uncles (i.e. of at least one generation before the current). /// This assumes that all uncles are valid uncles (i.e. of at least one generation before the current).
fn on_close_block(&self, block: &mut Block) { fn on_close_block(&self, block: &mut ExecutedBlock) {
let reward = self.spec().engine_params.get("blockReward").map(|a| decode(&a)).unwrap_or(U256::from(0u64)); let reward = self.spec().engine_params.get("blockReward").map_or(U256::from(0u64), |a| decode(&a));
let fields = block.fields(); let fields = block.fields();
// Bestow block reward // Bestow block reward
@ -99,13 +99,17 @@ impl Engine for Ethash {
} }
fn verify_block_basic(&self, header: &Header, _block: Option<&[u8]>) -> result::Result<(), Error> { fn verify_block_basic(&self, header: &Header, _block: Option<&[u8]>) -> result::Result<(), Error> {
// check the seal fields.
try!(UntrustedRlp::new(&header.seal[0]).as_val::<H256>());
try!(UntrustedRlp::new(&header.seal[1]).as_val::<H64>());
let min_difficulty = decode(self.spec().engine_params.get("minimumDifficulty").unwrap()); let min_difficulty = decode(self.spec().engine_params.get("minimumDifficulty").unwrap());
if header.difficulty < min_difficulty { if header.difficulty < min_difficulty {
return Err(From::from(BlockError::InvalidDifficulty(Mismatch { expected: min_difficulty, found: header.difficulty }))) return Err(From::from(BlockError::InvalidDifficulty(Mismatch { expected: min_difficulty, found: header.difficulty })))
} }
let difficulty = Ethash::boundary_to_difficulty(&Ethash::from_ethash(quick_get_difficulty( let difficulty = Ethash::boundary_to_difficulty(&Ethash::from_ethash(quick_get_difficulty(
&Ethash::to_ethash(header.bare_hash()), &Ethash::to_ethash(header.bare_hash()),
header.nonce(), header.nonce().low_u64(),
&Ethash::to_ethash(header.mix_hash())))); &Ethash::to_ethash(header.mix_hash()))));
if difficulty < header.difficulty { if difficulty < header.difficulty {
return Err(From::from(BlockError::InvalidEthashDifficulty(Mismatch { expected: header.difficulty, found: difficulty }))); return Err(From::from(BlockError::InvalidEthashDifficulty(Mismatch { expected: header.difficulty, found: difficulty })));
@ -114,7 +118,7 @@ impl Engine for Ethash {
} }
fn verify_block_unordered(&self, header: &Header, _block: Option<&[u8]>) -> result::Result<(), Error> { fn verify_block_unordered(&self, header: &Header, _block: Option<&[u8]>) -> result::Result<(), Error> {
let result = self.pow.compute_light(header.number as u64, &Ethash::to_ethash(header.bare_hash()), header.nonce()); let result = self.pow.compute_light(header.number as u64, &Ethash::to_ethash(header.bare_hash()), header.nonce().low_u64());
let mix = Ethash::from_ethash(result.mix_hash); let mix = Ethash::from_ethash(result.mix_hash);
let difficulty = Ethash::boundary_to_difficulty(&Ethash::from_ethash(result.value)); let difficulty = Ethash::boundary_to_difficulty(&Ethash::from_ethash(result.value));
if mix != header.mix_hash() { if mix != header.mix_hash() {
@ -153,6 +157,7 @@ impl Engine for Ethash {
} }
} }
#[allow(wrong_self_convention)] // to_ethash should take self
impl Ethash { impl Ethash {
fn calculate_difficuty(&self, header: &Header, parent: &Header) -> U256 { fn calculate_difficuty(&self, header: &Header, parent: &Header) -> U256 {
const EXP_DIFF_PERIOD: u64 = 100000; const EXP_DIFF_PERIOD: u64 = 100000;
@ -203,7 +208,7 @@ impl Ethash {
} }
impl Header { impl Header {
fn nonce(&self) -> u64 { fn nonce(&self) -> H64 {
decode(&self.seal()[1]) decode(&self.seal()[1])
} }
fn mix_hash(&self) -> H256 { fn mix_hash(&self) -> H256 {

View File

@ -26,7 +26,7 @@ pub enum MessageCallResult {
Failed Failed
} }
/// TODO [debris] Please document me /// Externalities interface for EVMs
pub trait Ext { pub trait Ext {
/// Returns a value for given key. /// Returns a value for given key.
fn storage_at(&self, key: &H256) -> H256; fn storage_at(&self, key: &H256) -> H256;
@ -55,8 +55,9 @@ pub trait Ext {
/// and true if subcall was successfull. /// and true if subcall was successfull.
fn call(&mut self, fn call(&mut self,
gas: &U256, gas: &U256,
address: &Address, sender_address: &Address,
value: &U256, receive_address: &Address,
value: Option<U256>,
data: &[u8], data: &[u8],
code_address: &Address, code_address: &Address,
output: &mut [u8]) -> MessageCallResult; output: &mut [u8]) -> MessageCallResult;

View File

@ -68,10 +68,11 @@ impl Factory {
fn jit() -> Box<Evm> { fn jit() -> Box<Evm> {
unimplemented!() unimplemented!()
} }
}
impl Default for Factory {
/// Returns jitvm factory /// Returns jitvm factory
#[cfg(feature = "jit")] #[cfg(feature = "jit")]
pub fn default() -> Factory { fn default() -> Factory {
Factory { Factory {
evm: VMType::Jit evm: VMType::Jit
} }
@ -79,7 +80,7 @@ impl Factory {
/// Returns native rust evm factory /// Returns native rust evm factory
#[cfg(not(feature = "jit"))] #[cfg(not(feature = "jit"))]
pub fn default() -> Factory { fn default() -> Factory {
Factory { Factory {
evm: VMType::Interpreter evm: VMType::Interpreter
} }

View File

@ -7,19 +7,19 @@ use super::instructions::Instruction;
use std::marker::Copy; use std::marker::Copy;
use evm::{MessageCallResult, ContractCreateResult}; use evm::{MessageCallResult, ContractCreateResult};
#[cfg(not(feature = "evm_debug"))] #[cfg(not(feature = "evm-debug"))]
macro_rules! evm_debug { macro_rules! evm_debug {
($x: expr) => {} ($x: expr) => {}
} }
#[cfg(feature = "evm_debug")] #[cfg(feature = "evm-debug")]
macro_rules! evm_debug { macro_rules! evm_debug {
($x: expr) => { ($x: expr) => {
$x $x
} }
} }
#[cfg(feature = "evm_debug")] #[cfg(feature = "evm-debug")]
fn color(instruction: Instruction, name: &'static str) -> String { fn color(instruction: Instruction, name: &'static str) -> String {
let c = instruction as usize % 6; let c = instruction as usize % 6;
let colors = [31, 34, 33, 32, 35, 36]; let colors = [31, 34, 33, 32, 35, 36];
@ -72,7 +72,7 @@ impl<S : Copy> VecStack<S> {
impl<S : fmt::Display> Stack<S> for VecStack<S> { impl<S : fmt::Display> Stack<S> for VecStack<S> {
fn peek(&self, no_from_top: usize) -> &S { fn peek(&self, no_from_top: usize) -> &S {
return &self.stack[self.stack.len() - no_from_top - 1]; &self.stack[self.stack.len() - no_from_top - 1]
} }
fn swap_with_top(&mut self, no_from_top: usize) { fn swap_with_top(&mut self, no_from_top: usize) {
@ -157,7 +157,7 @@ impl Memory for Vec<u8> {
} }
fn size(&self) -> usize { fn size(&self) -> usize {
return self.len() self.len()
} }
fn read_slice(&self, init_off_u: U256, init_size_u: U256) -> &[u8] { fn read_slice(&self, init_off_u: U256, init_size_u: U256) -> &[u8] {
@ -228,6 +228,7 @@ struct CodeReader<'a> {
code: &'a Bytes code: &'a Bytes
} }
#[allow(len_without_is_empty)]
impl<'a> CodeReader<'a> { impl<'a> CodeReader<'a> {
/// Get `no_of_bytes` from code and convert to U256. Move PC /// Get `no_of_bytes` from code and convert to U256. Move PC
fn read(&mut self, no_of_bytes: usize) -> U256 { fn read(&mut self, no_of_bytes: usize) -> U256 {
@ -330,6 +331,7 @@ impl evm::Evm for Interpreter {
} }
impl Interpreter { impl Interpreter {
#[allow(cyclomatic_complexity)]
fn get_gas_cost_mem(&self, fn get_gas_cost_mem(&self,
ext: &evm::Ext, ext: &evm::Ext,
instruction: Instruction, instruction: Instruction,
@ -569,16 +571,10 @@ impl Interpreter {
let call_gas = stack.pop_back(); let call_gas = stack.pop_back();
let code_address = stack.pop_back(); let code_address = stack.pop_back();
let code_address = u256_to_address(&code_address); let code_address = u256_to_address(&code_address);
let is_delegatecall = instruction == instructions::DELEGATECALL;
let value = match is_delegatecall { let value = match instruction == instructions::DELEGATECALL {
true => params.value, true => None,
false => stack.pop_back() false => Some(stack.pop_back())
};
let address = match instruction == instructions::CALL {
true => &code_address,
false => &params.address
}; };
let in_off = stack.pop_back(); let in_off = stack.pop_back();
@ -586,13 +582,27 @@ impl Interpreter {
let out_off = stack.pop_back(); let out_off = stack.pop_back();
let out_size = stack.pop_back(); let out_size = stack.pop_back();
let call_gas = call_gas + match !is_delegatecall && value > U256::zero() { // Add stipend (only CALL|CALLCODE when value > 0)
let call_gas = call_gas + value.map_or_else(U256::zero, |val| match val > U256::zero() {
true => U256::from(ext.schedule().call_stipend), true => U256::from(ext.schedule().call_stipend),
false => U256::zero() false => U256::zero()
});
// Get sender & receive addresses, check if we have balance
let (sender_address, receive_address, has_balance) = match instruction {
instructions::CALL => {
let has_balance = ext.balance(&params.address) >= value.unwrap();
(&params.address, &code_address, has_balance)
},
instructions::CALLCODE => {
let has_balance = ext.balance(&params.address) >= value.unwrap();
(&params.address, &params.address, has_balance)
},
instructions::DELEGATECALL => (&params.sender, &params.address, true),
_ => panic!(format!("Unexpected instruction {} in CALL branch.", instruction))
}; };
let can_call = (is_delegatecall || ext.balance(&params.address) >= value) && ext.depth() < ext.schedule().max_depth; let can_call = has_balance && ext.depth() < ext.schedule().max_depth;
if !can_call { if !can_call {
stack.push(U256::zero()); stack.push(U256::zero());
return Ok(InstructionResult::UnusedGas(call_gas)); return Ok(InstructionResult::UnusedGas(call_gas));
@ -603,7 +613,7 @@ impl Interpreter {
// and we don't want to copy // and we don't want to copy
let input = unsafe { ::std::mem::transmute(mem.read_slice(in_off, in_size)) }; let input = unsafe { ::std::mem::transmute(mem.read_slice(in_off, in_size)) };
let output = mem.writeable_slice(out_off, out_size); let output = mem.writeable_slice(out_off, out_size);
ext.call(&call_gas, address, &value, input, &code_address, output) ext.call(&call_gas, sender_address, receive_address, value, input, &code_address, output)
}; };
return match call_result { return match call_result {
@ -710,13 +720,16 @@ impl Interpreter {
stack.push(address_to_u256(params.sender.clone())); stack.push(address_to_u256(params.sender.clone()));
}, },
instructions::CALLVALUE => { instructions::CALLVALUE => {
stack.push(params.value.clone()); stack.push(match params.value {
ActionValue::Transfer(val) => val,
ActionValue::Apparent(val) => val,
});
}, },
instructions::CALLDATALOAD => { instructions::CALLDATALOAD => {
let big_id = stack.pop_back(); let big_id = stack.pop_back();
let id = big_id.low_u64() as usize; let id = big_id.low_u64() as usize;
let max = id.wrapping_add(32); let max = id.wrapping_add(32);
let data = params.data.clone().unwrap_or(vec![]); let data = params.data.clone().unwrap_or_else(|| vec![]);
let bound = cmp::min(data.len(), max); let bound = cmp::min(data.len(), max);
if id < bound && big_id < U256::from(data.len()) { if id < bound && big_id < U256::from(data.len()) {
let mut v = data[id..bound].to_vec(); let mut v = data[id..bound].to_vec();
@ -727,7 +740,7 @@ impl Interpreter {
} }
}, },
instructions::CALLDATASIZE => { instructions::CALLDATASIZE => {
stack.push(U256::from(params.data.clone().unwrap_or(vec![]).len())); stack.push(U256::from(params.data.clone().map_or(0, |l| l.len())));
}, },
instructions::CODESIZE => { instructions::CODESIZE => {
stack.push(U256::from(code.len())); stack.push(U256::from(code.len()));
@ -738,10 +751,10 @@ impl Interpreter {
stack.push(U256::from(len)); stack.push(U256::from(len));
}, },
instructions::CALLDATACOPY => { instructions::CALLDATACOPY => {
self.copy_data_to_memory(mem, stack, &params.data.clone().unwrap_or(vec![])); self.copy_data_to_memory(mem, stack, &params.data.clone().unwrap_or_else(|| vec![]));
}, },
instructions::CODECOPY => { instructions::CODECOPY => {
self.copy_data_to_memory(mem, stack, &params.code.clone().unwrap_or(vec![])); self.copy_data_to_memory(mem, stack, &params.code.clone().unwrap_or_else(|| vec![]));
}, },
instructions::EXTCODECOPY => { instructions::EXTCODECOPY => {
let address = u256_to_address(&stack.pop_back()); let address = u256_to_address(&stack.pop_back());
@ -781,7 +794,7 @@ impl Interpreter {
fn copy_data_to_memory(&self, fn copy_data_to_memory(&self,
mem: &mut Memory, mem: &mut Memory,
stack: &mut Stack<U256>, stack: &mut Stack<U256>,
data: &Bytes) { data: &[u8]) {
let offset = stack.pop_back(); let offset = stack.pop_back();
let index = stack.pop_back(); let index = stack.pop_back();
let size = stack.pop_back(); let size = stack.pop_back();
@ -1051,7 +1064,7 @@ impl Interpreter {
Ok(()) Ok(())
} }
fn find_jump_destinations(&self, code: &Bytes) -> HashSet<CodePosition> { fn find_jump_destinations(&self, code: &[u8]) -> HashSet<CodePosition> {
let mut jump_dests = HashSet::new(); let mut jump_dests = HashSet::new();
let mut position = 0; let mut position = 0;
@ -1066,7 +1079,7 @@ impl Interpreter {
position += 1; position += 1;
} }
return jump_dests; jump_dests
} }
} }

View File

@ -64,7 +64,7 @@ impl IntoJit<evmjit::I256> for H256 {
for i in 0..self.bytes().len() { for i in 0..self.bytes().len() {
let rev = self.bytes().len() - 1 - i; let rev = self.bytes().len() - 1 - i;
let pos = rev / 8; let pos = rev / 8;
ret[pos] += (self.bytes()[i] as u64) << (rev % 8) * 8; ret[pos] += (self.bytes()[i] as u64) << ((rev % 8) * 8);
} }
evmjit::I256 { words: ret } evmjit::I256 { words: ret }
} }
@ -218,9 +218,11 @@ impl<'a> evmjit::Ext for ExtAdapter<'a> {
} }
} }
match self.ext.call(&call_gas, match self.ext.call(
&call_gas,
&self.address,
&receive_address, &receive_address,
&value, Some(value),
unsafe { slice::from_raw_parts(in_beg, in_size as usize) }, unsafe { slice::from_raw_parts(in_beg, in_size as usize) },
&code_address, &code_address,
unsafe { slice::from_raw_parts_mut(out_beg, out_size as usize) }) { unsafe { slice::from_raw_parts_mut(out_beg, out_size as usize) }) {
@ -262,7 +264,7 @@ impl<'a> evmjit::Ext for ExtAdapter<'a> {
} }
let bytes_ref: &[u8] = slice::from_raw_parts(beg, size as usize); let bytes_ref: &[u8] = slice::from_raw_parts(beg, size as usize);
self.ext.log(topics, bytes_ref.to_vec()); self.ext.log(topics, bytes_ref);
} }
} }
@ -287,8 +289,8 @@ impl evm::Evm for JitEvm {
assert!(params.gas <= U256::from(i64::max_value() as u64), "evmjit max gas is 2 ^ 63"); assert!(params.gas <= U256::from(i64::max_value() as u64), "evmjit max gas is 2 ^ 63");
assert!(params.gas_price <= U256::from(i64::max_value() as u64), "evmjit max gas is 2 ^ 63"); assert!(params.gas_price <= U256::from(i64::max_value() as u64), "evmjit max gas is 2 ^ 63");
let call_data = params.data.unwrap_or(vec![]); let call_data = params.data.unwrap_or_else(Vec::new);
let code = params.code.unwrap_or(vec![]); let code = params.code.unwrap_or_else(Vec::new);
let mut data = evmjit::RuntimeDataHandle::new(); let mut data = evmjit::RuntimeDataHandle::new();
data.gas = params.gas.low_u64() as i64; data.gas = params.gas.low_u64() as i64;
@ -303,7 +305,10 @@ impl evm::Evm for JitEvm {
data.address = params.address.into_jit(); data.address = params.address.into_jit();
data.caller = params.sender.into_jit(); data.caller = params.sender.into_jit();
data.origin = params.origin.into_jit(); data.origin = params.origin.into_jit();
data.call_value = params.value.into_jit(); data.call_value = match params.value {
ActionValue::Transfer(val) => val.into_jit(),
ActionValue::Apparent(val) => val.into_jit()
};
data.author = ext.env_info().author.clone().into_jit(); data.author = ext.env_info().author.clone().into_jit();
data.difficulty = ext.env_info().difficulty.into_jit(); data.difficulty = ext.env_info().difficulty.into_jit();

View File

@ -2,67 +2,67 @@
/// Definition of the cost schedule and other parameterisations for the EVM. /// Definition of the cost schedule and other parameterisations for the EVM.
pub struct Schedule { pub struct Schedule {
/// TODO [Gav Wood] Please document me /// Does it support exceptional failed code deposit
pub exceptional_failed_code_deposit: bool, pub exceptional_failed_code_deposit: bool,
/// TODO [Gav Wood] Please document me /// Does it have a delegate cal
pub have_delegate_call: bool, pub have_delegate_call: bool,
/// TODO [Tomusdrw] Please document me /// VM stack limit
pub stack_limit: usize, pub stack_limit: usize,
/// TODO [Gav Wood] Please document me /// Max number of nested calls/creates
pub max_depth: usize, pub max_depth: usize,
/// TODO [Gav Wood] Please document me /// Gas prices for instructions in all tiers
pub tier_step_gas: [usize; 8], pub tier_step_gas: [usize; 8],
/// TODO [Gav Wood] Please document me /// Gas price for `EXP` opcode
pub exp_gas: usize, pub exp_gas: usize,
/// TODO [Gav Wood] Please document me /// Additional gas for `EXP` opcode for each byte of exponent
pub exp_byte_gas: usize, pub exp_byte_gas: usize,
/// TODO [Gav Wood] Please document me /// Gas price for `SHA3` opcode
pub sha3_gas: usize, pub sha3_gas: usize,
/// TODO [Gav Wood] Please document me /// Additional gas for `SHA3` opcode for each word of hashed memory
pub sha3_word_gas: usize, pub sha3_word_gas: usize,
/// TODO [Gav Wood] Please document me /// Gas price for loading from storage
pub sload_gas: usize, pub sload_gas: usize,
/// TODO [Gav Wood] Please document me /// Gas price for setting new value to storage (`storage==0`, `new!=0`)
pub sstore_set_gas: usize, pub sstore_set_gas: usize,
/// TODO [Gav Wood] Please document me /// Gas price for altering value in storage
pub sstore_reset_gas: usize, pub sstore_reset_gas: usize,
/// TODO [Gav Wood] Please document me /// Gas refund for `SSTORE` clearing (when `storage!=0`, `new==0`)
pub sstore_refund_gas: usize, pub sstore_refund_gas: usize,
/// TODO [Gav Wood] Please document me /// Gas price for `JUMPDEST` opcode
pub jumpdest_gas: usize, pub jumpdest_gas: usize,
/// TODO [Gav Wood] Please document me /// Gas price for `LOG*`
pub log_gas: usize, pub log_gas: usize,
/// TODO [Gav Wood] Please document me /// Additional gas for data in `LOG*`
pub log_data_gas: usize, pub log_data_gas: usize,
/// TODO [Gav Wood] Please document me /// Additional gas for each topic in `LOG*`
pub log_topic_gas: usize, pub log_topic_gas: usize,
/// TODO [Gav Wood] Please document me /// Gas price for `CREATE` opcode
pub create_gas: usize, pub create_gas: usize,
/// TODO [Gav Wood] Please document me /// Gas price for `*CALL*` opcodes
pub call_gas: usize, pub call_gas: usize,
/// TODO [Gav Wood] Please document me /// Stipend for transfer for `CALL|CALLCODE` opcode when `value>0`
pub call_stipend: usize, pub call_stipend: usize,
/// TODO [Gav Wood] Please document me /// Additional gas required for value transfer (`CALL|CALLCODE`)
pub call_value_transfer_gas: usize, pub call_value_transfer_gas: usize,
/// TODO [Gav Wood] Please document me /// Additional gas for creating new account (`CALL|CALLCODE`)
pub call_new_account_gas: usize, pub call_new_account_gas: usize,
/// TODO [Gav Wood] Please document me /// Refund for SUICIDE
pub suicide_refund_gas: usize, pub suicide_refund_gas: usize,
/// TODO [Gav Wood] Please document me /// Gas for used memory
pub memory_gas: usize, pub memory_gas: usize,
/// TODO [Gav Wood] Please document me /// Coefficient used to convert memory size to gas price for memory
pub quad_coeff_div: usize, pub quad_coeff_div: usize,
/// TODO [Gav Wood] Please document me /// Cost for contract length when executing `CREATE`
pub create_data_gas: usize, pub create_data_gas: usize,
/// TODO [Gav Wood] Please document me /// Transaction cost
pub tx_gas: usize, pub tx_gas: usize,
/// TODO [Gav Wood] Please document me /// `CREATE` transaction cost
pub tx_create_gas: usize, pub tx_create_gas: usize,
/// TODO [Gav Wood] Please document me /// Additional cost for empty data transaction
pub tx_data_zero_gas: usize, pub tx_data_zero_gas: usize,
/// TODO [Gav Wood] Please document me /// Aditional cost for non-empty data transaction
pub tx_data_non_zero_gas: usize, pub tx_data_non_zero_gas: usize,
/// TODO [Gav Wood] Please document me /// Gas price for copying memory
pub copy_gas: usize, pub copy_gas: usize,
} }

View File

@ -19,7 +19,7 @@ struct FakeExt {
logs: Vec<FakeLogEntry>, logs: Vec<FakeLogEntry>,
_suicides: HashSet<Address>, _suicides: HashSet<Address>,
info: EnvInfo, info: EnvInfo,
_schedule: Schedule schedule: Schedule
} }
impl FakeExt { impl FakeExt {
@ -61,8 +61,9 @@ impl Ext for FakeExt {
fn call(&mut self, fn call(&mut self,
_gas: &U256, _gas: &U256,
_address: &Address, _sender_address: &Address,
_value: &U256, _receive_address: &Address,
_value: Option<U256>,
_data: &[u8], _data: &[u8],
_code_address: &Address, _code_address: &Address,
_output: &mut [u8]) -> MessageCallResult { _output: &mut [u8]) -> MessageCallResult {
@ -89,7 +90,7 @@ impl Ext for FakeExt {
} }
fn schedule(&self) -> &Schedule { fn schedule(&self) -> &Schedule {
&self._schedule &self.schedule
} }
fn env_info(&self) -> &EnvInfo { fn env_info(&self) -> &EnvInfo {
@ -110,7 +111,7 @@ fn test_stack_underflow() {
let address = Address::from_str("0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6").unwrap(); let address = Address::from_str("0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6").unwrap();
let code = "01600055".from_hex().unwrap(); let code = "01600055".from_hex().unwrap();
let mut params = ActionParams::new(); let mut params = ActionParams::default();
params.address = address.clone(); params.address = address.clone();
params.gas = U256::from(100_000); params.gas = U256::from(100_000);
params.code = Some(code); params.code = Some(code);
@ -122,7 +123,7 @@ fn test_stack_underflow() {
}; };
match err { match err {
evm::Error::StackUnderflow {instruction: _, wanted, on_stack} => { evm::Error::StackUnderflow {wanted, on_stack, ..} => {
assert_eq!(wanted, 2); assert_eq!(wanted, 2);
assert_eq!(on_stack, 0); assert_eq!(on_stack, 0);
} }
@ -137,7 +138,7 @@ fn test_add(factory: super::Factory) {
let address = Address::from_str("0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6").unwrap(); let address = Address::from_str("0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6").unwrap();
let code = "7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff01600055".from_hex().unwrap(); let code = "7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff01600055".from_hex().unwrap();
let mut params = ActionParams::new(); let mut params = ActionParams::default();
params.address = address.clone(); params.address = address.clone();
params.gas = U256::from(100_000); params.gas = U256::from(100_000);
params.code = Some(code); params.code = Some(code);
@ -157,7 +158,7 @@ fn test_sha3(factory: super::Factory) {
let address = Address::from_str("0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6").unwrap(); let address = Address::from_str("0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6").unwrap();
let code = "6000600020600055".from_hex().unwrap(); let code = "6000600020600055".from_hex().unwrap();
let mut params = ActionParams::new(); let mut params = ActionParams::default();
params.address = address.clone(); params.address = address.clone();
params.gas = U256::from(100_000); params.gas = U256::from(100_000);
params.code = Some(code); params.code = Some(code);
@ -177,7 +178,7 @@ fn test_address(factory: super::Factory) {
let address = Address::from_str("0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6").unwrap(); let address = Address::from_str("0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6").unwrap();
let code = "30600055".from_hex().unwrap(); let code = "30600055".from_hex().unwrap();
let mut params = ActionParams::new(); let mut params = ActionParams::default();
params.address = address.clone(); params.address = address.clone();
params.gas = U256::from(100_000); params.gas = U256::from(100_000);
params.code = Some(code); params.code = Some(code);
@ -198,7 +199,7 @@ fn test_origin(factory: super::Factory) {
let origin = Address::from_str("cd1722f2947def4cf144679da39c4c32bdc35681").unwrap(); let origin = Address::from_str("cd1722f2947def4cf144679da39c4c32bdc35681").unwrap();
let code = "32600055".from_hex().unwrap(); let code = "32600055".from_hex().unwrap();
let mut params = ActionParams::new(); let mut params = ActionParams::default();
params.address = address.clone(); params.address = address.clone();
params.origin = origin.clone(); params.origin = origin.clone();
params.gas = U256::from(100_000); params.gas = U256::from(100_000);
@ -214,13 +215,14 @@ fn test_origin(factory: super::Factory) {
assert_eq!(ext.store.get(&H256::new()).unwrap(), &H256::from_str("000000000000000000000000cd1722f2947def4cf144679da39c4c32bdc35681").unwrap()); assert_eq!(ext.store.get(&H256::new()).unwrap(), &H256::from_str("000000000000000000000000cd1722f2947def4cf144679da39c4c32bdc35681").unwrap());
} }
// TODO [todr] Fails with Signal 11 on JIT
evm_test!{test_sender: test_sender_jit, test_sender_int} evm_test!{test_sender: test_sender_jit, test_sender_int}
fn test_sender(factory: super::Factory) { fn test_sender(factory: super::Factory) {
let address = Address::from_str("0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6").unwrap(); let address = Address::from_str("0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6").unwrap();
let sender = Address::from_str("cd1722f2947def4cf144679da39c4c32bdc35681").unwrap(); let sender = Address::from_str("cd1722f2947def4cf144679da39c4c32bdc35681").unwrap();
let code = "33600055".from_hex().unwrap(); let code = "33600055".from_hex().unwrap();
let mut params = ActionParams::new(); let mut params = ActionParams::default();
params.address = address.clone(); params.address = address.clone();
params.sender = sender.clone(); params.sender = sender.clone();
params.gas = U256::from(100_000); params.gas = U256::from(100_000);
@ -254,7 +256,7 @@ fn test_extcodecopy(factory: super::Factory) {
let code = "333b60006000333c600051600055".from_hex().unwrap(); let code = "333b60006000333c600051600055".from_hex().unwrap();
let sender_code = "6005600055".from_hex().unwrap(); let sender_code = "6005600055".from_hex().unwrap();
let mut params = ActionParams::new(); let mut params = ActionParams::default();
params.address = address.clone(); params.address = address.clone();
params.sender = sender.clone(); params.sender = sender.clone();
params.gas = U256::from(100_000); params.gas = U256::from(100_000);
@ -276,7 +278,7 @@ fn test_log_empty(factory: super::Factory) {
let address = Address::from_str("0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6").unwrap(); let address = Address::from_str("0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6").unwrap();
let code = "60006000a0".from_hex().unwrap(); let code = "60006000a0".from_hex().unwrap();
let mut params = ActionParams::new(); let mut params = ActionParams::default();
params.address = address.clone(); params.address = address.clone();
params.gas = U256::from(100_000); params.gas = U256::from(100_000);
params.code = Some(code); params.code = Some(code);
@ -307,7 +309,7 @@ fn test_log_sender(factory: super::Factory) {
let sender = Address::from_str("cd1722f3947def4cf144679da39c4c32bdc35681").unwrap(); let sender = Address::from_str("cd1722f3947def4cf144679da39c4c32bdc35681").unwrap();
let code = "60ff6000533360206000a1".from_hex().unwrap(); let code = "60ff6000533360206000a1".from_hex().unwrap();
let mut params = ActionParams::new(); let mut params = ActionParams::default();
params.address = address.clone(); params.address = address.clone();
params.sender = sender.clone(); params.sender = sender.clone();
params.gas = U256::from(100_000); params.gas = U256::from(100_000);
@ -332,7 +334,7 @@ fn test_blockhash(factory: super::Factory) {
let code = "600040600055".from_hex().unwrap(); let code = "600040600055".from_hex().unwrap();
let blockhash = H256::from_str("123400000000000000000000cd1722f2947def4cf144679da39c4c32bdc35681").unwrap(); let blockhash = H256::from_str("123400000000000000000000cd1722f2947def4cf144679da39c4c32bdc35681").unwrap();
let mut params = ActionParams::new(); let mut params = ActionParams::default();
params.address = address.clone(); params.address = address.clone();
params.gas = U256::from(100_000); params.gas = U256::from(100_000);
params.code = Some(code); params.code = Some(code);
@ -354,7 +356,7 @@ fn test_calldataload(factory: super::Factory) {
let code = "600135600055".from_hex().unwrap(); let code = "600135600055".from_hex().unwrap();
let data = "0123ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff23".from_hex().unwrap(); let data = "0123ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff23".from_hex().unwrap();
let mut params = ActionParams::new(); let mut params = ActionParams::default();
params.address = address.clone(); params.address = address.clone();
params.gas = U256::from(100_000); params.gas = U256::from(100_000);
params.code = Some(code); params.code = Some(code);
@ -376,7 +378,7 @@ fn test_author(factory: super::Factory) {
let author = Address::from_str("0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6").unwrap(); let author = Address::from_str("0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6").unwrap();
let code = "41600055".from_hex().unwrap(); let code = "41600055".from_hex().unwrap();
let mut params = ActionParams::new(); let mut params = ActionParams::default();
params.gas = U256::from(100_000); params.gas = U256::from(100_000);
params.code = Some(code); params.code = Some(code);
let mut ext = FakeExt::new(); let mut ext = FakeExt::new();
@ -396,7 +398,7 @@ fn test_timestamp(factory: super::Factory) {
let timestamp = 0x1234; let timestamp = 0x1234;
let code = "42600055".from_hex().unwrap(); let code = "42600055".from_hex().unwrap();
let mut params = ActionParams::new(); let mut params = ActionParams::default();
params.gas = U256::from(100_000); params.gas = U256::from(100_000);
params.code = Some(code); params.code = Some(code);
let mut ext = FakeExt::new(); let mut ext = FakeExt::new();
@ -416,7 +418,7 @@ fn test_number(factory: super::Factory) {
let number = 0x1234; let number = 0x1234;
let code = "43600055".from_hex().unwrap(); let code = "43600055".from_hex().unwrap();
let mut params = ActionParams::new(); let mut params = ActionParams::default();
params.gas = U256::from(100_000); params.gas = U256::from(100_000);
params.code = Some(code); params.code = Some(code);
let mut ext = FakeExt::new(); let mut ext = FakeExt::new();
@ -436,7 +438,7 @@ fn test_difficulty(factory: super::Factory) {
let difficulty = U256::from(0x1234); let difficulty = U256::from(0x1234);
let code = "44600055".from_hex().unwrap(); let code = "44600055".from_hex().unwrap();
let mut params = ActionParams::new(); let mut params = ActionParams::default();
params.gas = U256::from(100_000); params.gas = U256::from(100_000);
params.code = Some(code); params.code = Some(code);
let mut ext = FakeExt::new(); let mut ext = FakeExt::new();
@ -456,7 +458,7 @@ fn test_gas_limit(factory: super::Factory) {
let gas_limit = U256::from(0x1234); let gas_limit = U256::from(0x1234);
let code = "45600055".from_hex().unwrap(); let code = "45600055".from_hex().unwrap();
let mut params = ActionParams::new(); let mut params = ActionParams::default();
params.gas = U256::from(100_000); params.gas = U256::from(100_000);
params.code = Some(code); params.code = Some(code);
let mut ext = FakeExt::new(); let mut ext = FakeExt::new();

View File

@ -5,6 +5,12 @@ use engine::*;
use evm::{self, Ext}; use evm::{self, Ext};
use externalities::*; use externalities::*;
use substate::*; use substate::*;
use crossbeam;
/// Max depth to avoid stack overflow (when it's reached we start a new thread with VM)
/// TODO [todr] We probably need some more sophisticated calculations here (limit on my machine 132)
/// Maybe something like here: https://github.com/ethereum/libethereum/blob/4db169b8504f2b87f7d5a481819cfb959fc65f6c/libethereum/ExtVM.cpp
const MAX_VM_DEPTH_FOR_THREAD: usize = 128;
/// Returns new address created from address and given nonce. /// Returns new address created from address and given nonce.
pub fn contract_address(address: &Address, nonce: &U256) -> Address { pub fn contract_address(address: &Address, nonce: &U256) -> Address {
@ -75,7 +81,7 @@ impl<'a> Executive<'a> {
} }
/// Creates `Externalities` from `Executive`. /// Creates `Externalities` from `Executive`.
pub fn to_externalities<'_>(&'_ mut self, origin_info: OriginInfo, substate: &'_ mut Substate, output: OutputPolicy<'_>) -> Externalities { pub fn as_externalities<'_>(&'_ mut self, origin_info: OriginInfo, substate: &'_ mut Substate, output: OutputPolicy<'_>) -> Externalities {
Externalities::new(self.state, self.info, self.engine, self.depth, origin_info, substate, output) Externalities::new(self.state, self.info, self.engine, self.depth, origin_info, substate, output)
} }
@ -123,8 +129,8 @@ impl<'a> Executive<'a> {
let mut substate = Substate::new(); let mut substate = Substate::new();
let res = match t.action() { let res = match *t.action() {
&Action::Create => { Action::Create => {
let new_address = contract_address(&sender, &nonce); let new_address = contract_address(&sender, &nonce);
let params = ActionParams { let params = ActionParams {
code_address: new_address.clone(), code_address: new_address.clone(),
@ -133,13 +139,13 @@ impl<'a> Executive<'a> {
origin: sender.clone(), origin: sender.clone(),
gas: init_gas, gas: init_gas,
gas_price: t.gas_price, gas_price: t.gas_price,
value: t.value, value: ActionValue::Transfer(t.value),
code: Some(t.data.clone()), code: Some(t.data.clone()),
data: None, data: None,
}; };
self.create(params, &mut substate) self.create(params, &mut substate)
}, },
&Action::Call(ref address) => { Action::Call(ref address) => {
let params = ActionParams { let params = ActionParams {
code_address: address.clone(), code_address: address.clone(),
address: address.clone(), address: address.clone(),
@ -147,7 +153,7 @@ impl<'a> Executive<'a> {
origin: sender.clone(), origin: sender.clone(),
gas: init_gas, gas: init_gas,
gas_price: t.gas_price, gas_price: t.gas_price,
value: t.value, value: ActionValue::Transfer(t.value),
code: self.state.code(address), code: self.state.code(address),
data: Some(t.data.clone()), data: Some(t.data.clone()),
}; };
@ -161,6 +167,27 @@ impl<'a> Executive<'a> {
Ok(try!(self.finalize(t, substate, res))) Ok(try!(self.finalize(t, substate, res)))
} }
fn exec_vm(&mut self, params: ActionParams, unconfirmed_substate: &mut Substate, output_policy: OutputPolicy) -> evm::Result {
// Ordinary execution - keep VM in same thread
if (self.depth + 1) % MAX_VM_DEPTH_FOR_THREAD != 0 {
let mut ext = self.as_externalities(OriginInfo::from(&params), unconfirmed_substate, output_policy);
let vm_factory = self.engine.vm_factory();
return vm_factory.create().exec(params, &mut ext);
}
// Start in new thread to reset stack
// TODO [todr] No thread builder yet, so we need to reset once for a while
// https://github.com/aturon/crossbeam/issues/16
crossbeam::scope(|scope| {
let mut ext = self.as_externalities(OriginInfo::from(&params), unconfirmed_substate, output_policy);
let vm_factory = self.engine.vm_factory();
scope.spawn(move || {
vm_factory.create().exec(params, &mut ext)
})
}).join()
}
/// Calls contract function with given contract params. /// Calls contract function with given contract params.
/// NOTE. It does not finalize the transaction (doesn't do refunds, nor suicides). /// NOTE. It does not finalize the transaction (doesn't do refunds, nor suicides).
/// Modifies the substate and the output. /// Modifies the substate and the output.
@ -170,14 +197,16 @@ impl<'a> Executive<'a> {
let backup = self.state.clone(); let backup = self.state.clone();
// at first, transfer value to destination // at first, transfer value to destination
self.state.transfer_balance(&params.sender, &params.address, &params.value); if let ActionValue::Transfer(val) = params.value {
self.state.transfer_balance(&params.sender, &params.address, &val);
}
trace!("Executive::call(params={:?}) self.env_info={:?}", params, self.info); trace!("Executive::call(params={:?}) self.env_info={:?}", params, self.info);
if self.engine.is_builtin(&params.code_address) { if self.engine.is_builtin(&params.code_address) {
// if destination is builtin, try to execute it // if destination is builtin, try to execute it
let default = []; let default = [];
let data = if let &Some(ref d) = &params.data { d as &[u8] } else { &default as &[u8] }; let data = if let Some(ref d) = params.data { d as &[u8] } else { &default as &[u8] };
let cost = self.engine.cost_of_builtin(&params.code_address, data); let cost = self.engine.cost_of_builtin(&params.code_address, data);
match cost <= params.gas { match cost <= params.gas {
@ -198,8 +227,7 @@ impl<'a> Executive<'a> {
let mut unconfirmed_substate = Substate::new(); let mut unconfirmed_substate = Substate::new();
let res = { let res = {
let mut ext = self.to_externalities(OriginInfo::from(&params), &mut unconfirmed_substate, OutputPolicy::Return(output)); self.exec_vm(params, &mut unconfirmed_substate, OutputPolicy::Return(output))
self.engine.vm_factory().create().exec(params, &mut ext)
}; };
trace!("exec: sstore-clears={}\n", unconfirmed_substate.sstore_clears_count); trace!("exec: sstore-clears={}\n", unconfirmed_substate.sstore_clears_count);
@ -227,11 +255,12 @@ impl<'a> Executive<'a> {
self.state.new_contract(&params.address); self.state.new_contract(&params.address);
// then transfer value to it // then transfer value to it
self.state.transfer_balance(&params.sender, &params.address, &params.value); if let ActionValue::Transfer(val) = params.value {
self.state.transfer_balance(&params.sender, &params.address, &val);
}
let res = { let res = {
let mut ext = self.to_externalities(OriginInfo::from(&params), &mut unconfirmed_substate, OutputPolicy::InitContract); self.exec_vm(params, &mut unconfirmed_substate, OutputPolicy::InitContract)
self.engine.vm_factory().create().exec(params, &mut ext)
}; };
self.enact_result(&res, substate, unconfirmed_substate, backup); self.enact_result(&res, substate, unconfirmed_substate, backup);
res res
@ -248,7 +277,7 @@ impl<'a> Executive<'a> {
let refunds_bound = sstore_refunds + suicide_refunds; let refunds_bound = sstore_refunds + suicide_refunds;
// real ammount to refund // real ammount to refund
let gas_left_prerefund = match &result { &Ok(x) => x, _ => x!(0) }; let gas_left_prerefund = match result { Ok(x) => x, _ => x!(0) };
let refunded = cmp::min(refunds_bound, (t.gas - gas_left_prerefund) / U256::from(2)); let refunded = cmp::min(refunds_bound, (t.gas - gas_left_prerefund) / U256::from(2));
let gas_left = gas_left_prerefund + refunded; let gas_left = gas_left_prerefund + refunded;
@ -265,19 +294,13 @@ impl<'a> Executive<'a> {
self.state.add_balance(&self.info.author, &fees_value); self.state.add_balance(&self.info.author, &fees_value);
// perform suicides // perform suicides
for address in substate.suicides.iter() { for address in &substate.suicides {
trace!("Killing {}", address);
self.state.kill_account(address); self.state.kill_account(address);
} }
match result { match result {
Err(evm::Error::Internal) => Err(ExecutionError::Internal), Err(evm::Error::Internal) => Err(ExecutionError::Internal),
// TODO [ToDr] BadJumpDestination @debris - how to handle that? Err(_) => {
Err(evm::Error::OutOfGas)
| Err(evm::Error::BadJumpDestination { destination: _ })
| Err(evm::Error::BadInstruction { instruction: _ })
| Err(evm::Error::StackUnderflow {instruction: _, wanted: _, on_stack: _})
| Err(evm::Error::OutOfStack {instruction: _, wanted: _, limit: _}) => {
Ok(Executed { Ok(Executed {
gas: t.gas, gas: t.gas,
gas_used: t.gas, gas_used: t.gas,
@ -301,16 +324,15 @@ impl<'a> Executive<'a> {
} }
fn enact_result(&mut self, result: &evm::Result, substate: &mut Substate, un_substate: Substate, backup: State) { fn enact_result(&mut self, result: &evm::Result, substate: &mut Substate, un_substate: Substate, backup: State) {
// TODO: handle other evm::Errors same as OutOfGas once they are implemented match *result {
match result { Err(evm::Error::OutOfGas)
&Err(evm::Error::OutOfGas) | Err(evm::Error::BadJumpDestination {..})
| &Err(evm::Error::BadJumpDestination { destination: _ }) | Err(evm::Error::BadInstruction {.. })
| &Err(evm::Error::BadInstruction { instruction: _ }) | Err(evm::Error::StackUnderflow {..})
| &Err(evm::Error::StackUnderflow {instruction: _, wanted: _, on_stack: _}) | Err(evm::Error::OutOfStack {..}) => {
| &Err(evm::Error::OutOfStack {instruction: _, wanted: _, limit: _}) => {
self.state.revert(backup); self.state.revert(backup);
}, },
&Ok(_) | &Err(evm::Error::Internal) => substate.accrue(un_substate) Ok(_) | Err(evm::Error::Internal) => substate.accrue(un_substate)
} }
} }
} }
@ -367,12 +389,12 @@ mod tests {
fn test_sender_balance(factory: Factory) { fn test_sender_balance(factory: Factory) {
let sender = Address::from_str("0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6").unwrap(); let sender = Address::from_str("0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6").unwrap();
let address = contract_address(&sender, &U256::zero()); let address = contract_address(&sender, &U256::zero());
let mut params = ActionParams::new(); let mut params = ActionParams::default();
params.address = address.clone(); params.address = address.clone();
params.sender = sender.clone(); params.sender = sender.clone();
params.gas = U256::from(100_000); params.gas = U256::from(100_000);
params.code = Some("3331600055".from_hex().unwrap()); params.code = Some("3331600055".from_hex().unwrap());
params.value = U256::from(0x7); params.value = ActionValue::Transfer(U256::from(0x7));
let mut state = State::new_temp(); let mut state = State::new_temp();
state.add_balance(&sender, &U256::from(0x100u64)); state.add_balance(&sender, &U256::from(0x100u64));
let info = EnvInfo::new(); let info = EnvInfo::new();
@ -424,13 +446,13 @@ mod tests {
let address = contract_address(&sender, &U256::zero()); let address = contract_address(&sender, &U256::zero());
// TODO: add tests for 'callcreate' // TODO: add tests for 'callcreate'
//let next_address = contract_address(&address, &U256::zero()); //let next_address = contract_address(&address, &U256::zero());
let mut params = ActionParams::new(); let mut params = ActionParams::default();
params.address = address.clone(); params.address = address.clone();
params.sender = sender.clone(); params.sender = sender.clone();
params.origin = sender.clone(); params.origin = sender.clone();
params.gas = U256::from(100_000); params.gas = U256::from(100_000);
params.code = Some(code.clone()); params.code = Some(code.clone());
params.value = U256::from(100); params.value = ActionValue::Transfer(U256::from(100));
let mut state = State::new_temp(); let mut state = State::new_temp();
state.add_balance(&sender, &U256::from(100)); state.add_balance(&sender, &U256::from(100));
let info = EnvInfo::new(); let info = EnvInfo::new();
@ -477,13 +499,13 @@ mod tests {
let address = contract_address(&sender, &U256::zero()); let address = contract_address(&sender, &U256::zero());
// TODO: add tests for 'callcreate' // TODO: add tests for 'callcreate'
//let next_address = contract_address(&address, &U256::zero()); //let next_address = contract_address(&address, &U256::zero());
let mut params = ActionParams::new(); let mut params = ActionParams::default();
params.address = address.clone(); params.address = address.clone();
params.sender = sender.clone(); params.sender = sender.clone();
params.origin = sender.clone(); params.origin = sender.clone();
params.gas = U256::from(100_000); params.gas = U256::from(100_000);
params.code = Some(code.clone()); params.code = Some(code.clone());
params.value = U256::from(100); params.value = ActionValue::Transfer(U256::from(100));
let mut state = State::new_temp(); let mut state = State::new_temp();
state.add_balance(&sender, &U256::from(100)); state.add_balance(&sender, &U256::from(100));
let info = EnvInfo::new(); let info = EnvInfo::new();
@ -528,13 +550,13 @@ mod tests {
let sender = Address::from_str("cd1722f3947def4cf144679da39c4c32bdc35681").unwrap(); let sender = Address::from_str("cd1722f3947def4cf144679da39c4c32bdc35681").unwrap();
let address = contract_address(&sender, &U256::zero()); let address = contract_address(&sender, &U256::zero());
let next_address = contract_address(&address, &U256::zero()); let next_address = contract_address(&address, &U256::zero());
let mut params = ActionParams::new(); let mut params = ActionParams::default();
params.address = address.clone(); params.address = address.clone();
params.sender = sender.clone(); params.sender = sender.clone();
params.origin = sender.clone(); params.origin = sender.clone();
params.gas = U256::from(100_000); params.gas = U256::from(100_000);
params.code = Some(code.clone()); params.code = Some(code.clone());
params.value = U256::from(100); params.value = ActionValue::Transfer(U256::from(100));
let mut state = State::new_temp(); let mut state = State::new_temp();
state.add_balance(&sender, &U256::from(100)); state.add_balance(&sender, &U256::from(100));
let info = EnvInfo::new(); let info = EnvInfo::new();
@ -584,12 +606,12 @@ mod tests {
let address_b = Address::from_str("945304eb96065b2a98b57a48a06ae28d285a71b5" ).unwrap(); let address_b = Address::from_str("945304eb96065b2a98b57a48a06ae28d285a71b5" ).unwrap();
let sender = Address::from_str("cd1722f3947def4cf144679da39c4c32bdc35681").unwrap(); let sender = Address::from_str("cd1722f3947def4cf144679da39c4c32bdc35681").unwrap();
let mut params = ActionParams::new(); let mut params = ActionParams::default();
params.address = address_a.clone(); params.address = address_a.clone();
params.sender = sender.clone(); params.sender = sender.clone();
params.gas = U256::from(100_000); params.gas = U256::from(100_000);
params.code = Some(code_a.clone()); params.code = Some(code_a.clone());
params.value = U256::from(100_000); params.value = ActionValue::Transfer(U256::from(100_000));
let mut state = State::new_temp(); let mut state = State::new_temp();
state.init_code(&address_a, code_a.clone()); state.init_code(&address_a, code_a.clone());
@ -633,7 +655,7 @@ mod tests {
let sender = Address::from_str("cd1722f3947def4cf144679da39c4c32bdc35681").unwrap(); let sender = Address::from_str("cd1722f3947def4cf144679da39c4c32bdc35681").unwrap();
let code = "600160005401600055600060006000600060003060e05a03f1600155".from_hex().unwrap(); let code = "600160005401600055600060006000600060003060e05a03f1600155".from_hex().unwrap();
let address = contract_address(&sender, &U256::zero()); let address = contract_address(&sender, &U256::zero());
let mut params = ActionParams::new(); let mut params = ActionParams::default();
params.address = address.clone(); params.address = address.clone();
params.gas = U256::from(100_000); params.gas = U256::from(100_000);
params.code = Some(code.clone()); params.code = Some(code.clone());
@ -789,13 +811,13 @@ mod tests {
let address = contract_address(&sender, &U256::zero()); let address = contract_address(&sender, &U256::zero());
// TODO: add tests for 'callcreate' // TODO: add tests for 'callcreate'
//let next_address = contract_address(&address, &U256::zero()); //let next_address = contract_address(&address, &U256::zero());
let mut params = ActionParams::new(); let mut params = ActionParams::default();
params.address = address.clone(); params.address = address.clone();
params.sender = sender.clone(); params.sender = sender.clone();
params.origin = sender.clone(); params.origin = sender.clone();
params.gas = U256::from(0x0186a0); params.gas = U256::from(0x0186a0);
params.code = Some(code.clone()); params.code = Some(code.clone());
params.value = U256::from_str("0de0b6b3a7640000").unwrap(); params.value = ActionValue::Transfer(U256::from_str("0de0b6b3a7640000").unwrap());
let mut state = State::new_temp(); let mut state = State::new_temp();
state.add_balance(&sender, &U256::from_str("152d02c7e14af6800000").unwrap()); state.add_balance(&sender, &U256::from_str("152d02c7e14af6800000").unwrap());
let info = EnvInfo::new(); let info = EnvInfo::new();

View File

@ -19,7 +19,8 @@ pub enum OutputPolicy<'a> {
pub struct OriginInfo { pub struct OriginInfo {
address: Address, address: Address,
origin: Address, origin: Address,
gas_price: U256 gas_price: U256,
value: U256
} }
impl OriginInfo { impl OriginInfo {
@ -28,7 +29,11 @@ impl OriginInfo {
OriginInfo { OriginInfo {
address: params.address.clone(), address: params.address.clone(),
origin: params.origin.clone(), origin: params.origin.clone(),
gas_price: params.gas_price.clone() gas_price: params.gas_price.clone(),
value: match params.value {
ActionValue::Transfer(val) => val,
ActionValue::Apparent(val) => val,
}
} }
} }
} }
@ -111,7 +116,7 @@ impl<'a> Ext for Externalities<'a> {
origin: self.origin_info.origin.clone(), origin: self.origin_info.origin.clone(),
gas: *gas, gas: *gas,
gas_price: self.origin_info.gas_price.clone(), gas_price: self.origin_info.gas_price.clone(),
value: value.clone(), value: ActionValue::Transfer(value.clone()),
code: Some(code.to_vec()), code: Some(code.to_vec()),
data: None, data: None,
}; };
@ -131,24 +136,29 @@ impl<'a> Ext for Externalities<'a> {
fn call(&mut self, fn call(&mut self,
gas: &U256, gas: &U256,
address: &Address, sender_address: &Address,
value: &U256, receive_address: &Address,
value: Option<U256>,
data: &[u8], data: &[u8],
code_address: &Address, code_address: &Address,
output: &mut [u8]) -> MessageCallResult { output: &mut [u8]) -> MessageCallResult {
let params = ActionParams { let mut params = ActionParams {
sender: sender_address.clone(),
address: receive_address.clone(),
value: ActionValue::Apparent(self.origin_info.value.clone()),
code_address: code_address.clone(), code_address: code_address.clone(),
address: address.clone(),
sender: self.origin_info.address.clone(),
origin: self.origin_info.origin.clone(), origin: self.origin_info.origin.clone(),
gas: *gas, gas: *gas,
gas_price: self.origin_info.gas_price.clone(), gas_price: self.origin_info.gas_price.clone(),
value: value.clone(),
code: self.state.code(code_address), code: self.state.code(code_address),
data: Some(data.to_vec()), data: Some(data.to_vec()),
}; };
if let Some(value) = value {
params.value = ActionValue::Transfer(value);
}
let mut ex = Executive::from_parent(self.state, self.env_info, self.engine, self.depth); let mut ex = Executive::from_parent(self.state, self.env_info, self.engine, self.depth);
match ex.call(params, self.substate, BytesRef::Fixed(output)) { match ex.call(params, self.substate, BytesRef::Fixed(output)) {
@ -158,9 +168,10 @@ impl<'a> Ext for Externalities<'a> {
} }
fn extcode(&self, address: &Address) -> Bytes { fn extcode(&self, address: &Address) -> Bytes {
self.state.code(address).unwrap_or(vec![]) self.state.code(address).unwrap_or_else(|| vec![])
} }
#[allow(match_ref_pats)]
fn ret(&mut self, gas: &U256, data: &[u8]) -> Result<U256, evm::Error> { fn ret(&mut self, gas: &U256, data: &[u8]) -> Result<U256, evm::Error> {
match &mut self.output { match &mut self.output {
&mut OutputPolicy::Return(BytesRef::Fixed(ref mut slice)) => unsafe { &mut OutputPolicy::Return(BytesRef::Fixed(ref mut slice)) => unsafe {
@ -204,7 +215,13 @@ impl<'a> Ext for Externalities<'a> {
fn suicide(&mut self, refund_address: &Address) { fn suicide(&mut self, refund_address: &Address) {
let address = self.origin_info.address.clone(); let address = self.origin_info.address.clone();
let balance = self.balance(&address); let balance = self.balance(&address);
if &address == refund_address {
// TODO [todr] To be consisted with CPP client we set balance to 0 in that case.
self.state.sub_balance(&address, &balance);
} else {
trace!("Suiciding {} -> {} (xfer: {})", address, refund_address, balance);
self.state.transfer_balance(&address, refund_address, &balance); self.state.transfer_balance(&address, refund_address, &balance);
}
self.substate.suicides.insert(address); self.substate.suicides.insert(address);
} }

View File

@ -2,7 +2,7 @@ use util::*;
use basic_types::*; use basic_types::*;
use time::now_utc; use time::now_utc;
/// TODO [Gav Wood] Please document me /// Type for Block number
pub type BlockNumber = u64; pub type BlockNumber = u64;
/// A block header. /// A block header.
@ -11,7 +11,7 @@ pub type BlockNumber = u64;
/// which is non-specific. /// which is non-specific.
/// ///
/// Doesn't do all that much on its own. /// Doesn't do all that much on its own.
#[derive(Debug, Clone)] #[derive(Default, Debug, Clone)]
pub struct Header { pub struct Header {
// TODO: make all private. // TODO: make all private.
/// TODO [Gav Wood] Please document me /// TODO [Gav Wood] Please document me
@ -171,9 +171,10 @@ impl Header {
s.append(&self.gas_used); s.append(&self.gas_used);
s.append(&self.timestamp); s.append(&self.timestamp);
s.append(&self.extra_data); s.append(&self.extra_data);
match with_seal { if let Seal::With = with_seal {
Seal::With => for b in self.seal.iter() { s.append_raw(&b, 1); }, for b in &self.seal {
_ => {} s.append_raw(&b, 1);
}
} }
} }
@ -236,7 +237,7 @@ impl Encodable for Header {
self.timestamp.encode(e); self.timestamp.encode(e);
self.extra_data.encode(e); self.extra_data.encode(e);
for b in self.seal.iter() { for b in &self.seal {
e.emit_raw(&b); e.emit_raw(&b);
} }
}) })

View File

@ -1,8 +1,11 @@
#![warn(missing_docs)] #![warn(missing_docs)]
#![feature(cell_extras)] #![feature(cell_extras)]
#![feature(augmented_assignments)] #![feature(augmented_assignments)]
//#![feature(plugin)] #![feature(plugin)]
//#![plugin(interpolate_idents)] //#![plugin(interpolate_idents)]
#![plugin(clippy)]
#![allow(needless_range_loop, match_bool)]
//! Ethcore's ethereum implementation //! Ethcore's ethereum implementation
//! //!
//! ### Rust version //! ### Rust version
@ -73,7 +76,6 @@
//! sudo make install //! sudo make install
//! sudo ldconfig //! sudo ldconfig
//! ``` //! ```
#[macro_use] #[macro_use]
extern crate log; extern crate log;
extern crate rustc_serialize; extern crate rustc_serialize;
@ -88,6 +90,9 @@ extern crate num_cpus;
extern crate evmjit; extern crate evmjit;
#[macro_use] #[macro_use]
extern crate ethcore_util as util; extern crate ethcore_util as util;
extern crate crossbeam;
// NOTE: Add doc parser exception for these pub declarations.
/// TODO [Gav Wood] Please document me /// TODO [Gav Wood] Please document me
pub mod common; pub mod common;
@ -149,6 +154,5 @@ pub mod sync;
pub mod block; pub mod block;
/// TODO [arkpar] Please document me /// TODO [arkpar] Please document me
pub mod verification; pub mod verification;
/// TODO [debris] Please document me pub mod block_queue;
pub mod queue;
pub mod ethereum; pub mod ethereum;

View File

@ -2,7 +2,7 @@ use util::*;
use basic_types::LogBloom; use basic_types::LogBloom;
/// A single log's entry. /// A single log's entry.
#[derive(Debug,PartialEq,Eq)] #[derive(Default, Debug, Clone, PartialEq, Eq)]
pub struct LogEntry { pub struct LogEntry {
/// TODO [Gav Wood] Please document me /// TODO [Gav Wood] Please document me
pub address: Address, pub address: Address,

View File

@ -11,7 +11,7 @@ pub struct NullEngine {
} }
impl NullEngine { impl NullEngine {
/// TODO [Tomusdrw] Please document me /// Returns new instance of NullEngine with default VM Factory
pub fn new_boxed(spec: Spec) -> Box<Engine> { pub fn new_boxed(spec: Spec) -> Box<Engine> {
Box::new(NullEngine{ Box::new(NullEngine{
spec: spec, spec: spec,

View File

@ -31,7 +31,7 @@ impl PodAccount {
} }
} }
/// TODO [Gav Wood] Please document me /// Returns the RLP for this account.
pub fn rlp(&self) -> Bytes { pub fn rlp(&self) -> Bytes {
let mut stream = RlpStream::new_list(4); let mut stream = RlpStream::new_list(4);
stream.append(&self.nonce); stream.append(&self.nonce);
@ -40,6 +40,18 @@ impl PodAccount {
stream.append(&self.code.sha3()); stream.append(&self.code.sha3());
stream.out() stream.out()
} }
/// Place additional data into given hash DB.
pub fn insert_additional(&self, db: &mut HashDB) {
if !self.code.is_empty() {
db.insert(&self.code);
}
let mut r = H256::new();
let mut t = SecTrieDBMut::new(db, &mut r);
for (k, v) in &self.storage {
t.insert(k, &encode(&U256::from(v.as_slice())));
}
}
} }
impl fmt::Display for PodAccount { impl fmt::Display for PodAccount {

View File

@ -1,17 +1,25 @@
use util::*; use util::*;
use pod_account::*; use pod_account::*;
#[derive(Debug,Clone,PartialEq,Eq)] #[derive(Debug,Clone,PartialEq,Eq,Default)]
/// TODO [Gav Wood] Please document me /// TODO [Gav Wood] Please document me
pub struct PodState (BTreeMap<Address, PodAccount>); pub struct PodState (BTreeMap<Address, PodAccount>);
impl PodState { impl PodState {
/// Contruct a new object from the `m`. /// Contruct a new object from the `m`.
pub fn new(m: BTreeMap<Address, PodAccount>) -> PodState { PodState(m) } pub fn new() -> PodState { Default::default() }
/// Contruct a new object from the `m`.
pub fn from(m: BTreeMap<Address, PodAccount>) -> PodState { PodState(m) }
/// Get the underlying map. /// Get the underlying map.
pub fn get(&self) -> &BTreeMap<Address, PodAccount> { &self.0 } pub fn get(&self) -> &BTreeMap<Address, PodAccount> { &self.0 }
/// Get the root hash of the trie of the RLP of this.
pub fn root(&self) -> H256 {
sec_trie_root(self.0.iter().map(|(k, v)| (k.to_vec(), v.rlp())).collect())
}
/// Drain object to get the underlying map. /// Drain object to get the underlying map.
pub fn drain(self) -> BTreeMap<Address, PodAccount> { self.0 } pub fn drain(self) -> BTreeMap<Address, PodAccount> { self.0 }
} }
@ -26,10 +34,10 @@ impl FromJson for PodState {
let code = acc.find("code").map(&Bytes::from_json); let code = acc.find("code").map(&Bytes::from_json);
if balance.is_some() || nonce.is_some() || storage.is_some() || code.is_some() { if balance.is_some() || nonce.is_some() || storage.is_some() || code.is_some() {
state.insert(address_from_hex(address), PodAccount{ state.insert(address_from_hex(address), PodAccount{
balance: balance.unwrap_or(U256::zero()), balance: balance.unwrap_or_else(U256::zero),
nonce: nonce.unwrap_or(U256::zero()), nonce: nonce.unwrap_or_else(U256::zero),
storage: storage.unwrap_or(BTreeMap::new()), storage: storage.unwrap_or_else(BTreeMap::new),
code: code.unwrap_or(Vec::new()) code: code.unwrap_or_else(Vec::new)
}); });
} }
state state

View File

@ -3,7 +3,7 @@ use basic_types::LogBloom;
use log_entry::LogEntry; use log_entry::LogEntry;
/// Information describing execution of a transaction. /// Information describing execution of a transaction.
#[derive(Debug)] #[derive(Default, Debug, Clone)]
pub struct Receipt { pub struct Receipt {
/// TODO [Gav Wood] Please document me /// TODO [Gav Wood] Please document me
pub state_root: H256, pub state_root: H256,
@ -36,7 +36,7 @@ impl RlpStandard for Receipt {
// TODO: make work: // TODO: make work:
//s.append(&self.logs); //s.append(&self.logs);
s.append_list(self.logs.len()); s.append_list(self.logs.len());
for l in self.logs.iter() { for l in &self.logs {
l.rlp_append(s); l.rlp_append(s);
} }
} }

View File

@ -5,24 +5,37 @@ use error::*;
use std::env; use std::env;
use client::Client; use client::Client;
/// Message type for external and internal events
#[derive(Clone)]
pub enum SyncMessage {
/// New block has been imported into the blockchain
NewChainBlock(Bytes), //TODO: use Cow
/// A block is ready
BlockVerified,
}
/// TODO [arkpar] Please document me
pub type NetSyncMessage = NetworkIoMessage<SyncMessage>;
/// Client service setup. Creates and registers client and network services with the IO subsystem. /// Client service setup. Creates and registers client and network services with the IO subsystem.
pub struct ClientService { pub struct ClientService {
net_service: NetworkService<SyncMessage>, net_service: NetworkService<SyncMessage>,
client: Arc<RwLock<Client>>, client: Arc<Client>,
sync: Arc<EthSync>,
} }
impl ClientService { impl ClientService {
/// Start the service in a separate thread. /// Start the service in a separate thread.
pub fn start(spec: Spec) -> Result<ClientService, Error> { pub fn start(spec: Spec, net_config: NetworkConfiguration) -> Result<ClientService, Error> {
let mut net_service = try!(NetworkService::start()); let mut net_service = try!(NetworkService::start(net_config));
info!("Starting {}", net_service.host_info()); info!("Starting {}", net_service.host_info());
info!("Configured for {} using {} engine", spec.name, spec.engine_name); info!("Configured for {} using {} engine", spec.name, spec.engine_name);
let mut dir = env::home_dir().unwrap(); let mut dir = env::home_dir().unwrap();
dir.push(".parity"); dir.push(".parity");
dir.push(H64::from(spec.genesis_header().hash()).hex()); dir.push(H64::from(spec.genesis_header().hash()).hex());
let client = Arc::new(RwLock::new(try!(Client::new(spec, &dir, net_service.io().channel())))); let client = try!(Client::new(spec, &dir, net_service.io().channel()));
EthSync::register(&mut net_service, client.clone()); let sync = EthSync::register(&mut net_service, client.clone());
let client_io = Box::new(ClientIoHandler { let client_io = Arc::new(ClientIoHandler {
client: client.clone() client: client.clone()
}); });
try!(net_service.io().register_handler(client_io)); try!(net_service.io().register_handler(client_io));
@ -30,43 +43,62 @@ impl ClientService {
Ok(ClientService { Ok(ClientService {
net_service: net_service, net_service: net_service,
client: client, client: client,
sync: sync,
}) })
} }
/// Get the network service.
pub fn add_node(&mut self, _enode: &str) {
unimplemented!();
}
/// TODO [arkpar] Please document me /// TODO [arkpar] Please document me
pub fn io(&mut self) -> &mut IoService<NetSyncMessage> { pub fn io(&mut self) -> &mut IoService<NetSyncMessage> {
self.net_service.io() self.net_service.io()
} }
/// TODO [arkpar] Please document me /// TODO [arkpar] Please document me
pub fn client(&self) -> Arc<RwLock<Client>> { pub fn client(&self) -> Arc<Client> {
self.client.clone() self.client.clone()
}
/// Get shared sync handler
pub fn sync(&self) -> Arc<EthSync> {
self.sync.clone()
} }
} }
/// IO interface for the Client handler /// IO interface for the Client handler
struct ClientIoHandler { struct ClientIoHandler {
client: Arc<RwLock<Client>> client: Arc<Client>
} }
const CLIENT_TICK_TIMER: TimerToken = 0;
const CLIENT_TICK_MS: u64 = 5000;
impl IoHandler<NetSyncMessage> for ClientIoHandler { impl IoHandler<NetSyncMessage> for ClientIoHandler {
fn initialize<'s>(&'s mut self, _io: &mut IoContext<'s, NetSyncMessage>) { fn initialize(&self, io: &IoContext<NetSyncMessage>) {
io.register_timer(CLIENT_TICK_TIMER, CLIENT_TICK_MS).expect("Error registering client timer");
} }
fn message<'s>(&'s mut self, _io: &mut IoContext<'s, NetSyncMessage>, net_message: &'s mut NetSyncMessage) { fn timeout(&self, _io: &IoContext<NetSyncMessage>, timer: TimerToken) {
match net_message { if timer == CLIENT_TICK_TIMER {
&mut UserMessage(ref mut message) => { self.client.tick();
}
}
#[allow(match_ref_pats)]
#[allow(single_match)]
fn message(&self, io: &IoContext<NetSyncMessage>, net_message: &NetSyncMessage) {
if let &UserMessage(ref message) = net_message {
match message { match message {
&mut SyncMessage::BlockVerified => { &SyncMessage::BlockVerified => {
self.client.write().unwrap().import_verified_blocks(); self.client.import_verified_blocks(&io.channel());
}, },
_ => {}, // ignore other messages _ => {}, // ignore other messages
} }
} }
_ => {}, // ignore other messages
}
} }
} }

View File

@ -1,6 +1,7 @@
use common::*; use common::*;
use flate2::read::GzDecoder; use flate2::read::GzDecoder;
use engine::*; use engine::*;
use pod_state::*;
use null_engine::*; use null_engine::*;
/// Converts file from base64 gzipped bytes to json /// Converts file from base64 gzipped bytes to json
@ -10,7 +11,7 @@ pub fn gzip64res_to_json(source: &[u8]) -> Json {
let data = source.from_base64().expect("Genesis block is malformed!"); let data = source.from_base64().expect("Genesis block is malformed!");
let data_ref: &[u8] = &data; let data_ref: &[u8] = &data;
let mut decoder = GzDecoder::new(data_ref).expect("Gzip is invalid"); let mut decoder = GzDecoder::new(data_ref).expect("Gzip is invalid");
let mut s: String = "".to_string(); let mut s: String = "".to_owned();
decoder.read_to_string(&mut s).expect("Gzip is invalid"); decoder.read_to_string(&mut s).expect("Gzip is invalid");
Json::from_str(&s).expect("Json is invalid") Json::from_str(&s).expect("Json is invalid")
} }
@ -18,14 +19,14 @@ pub fn gzip64res_to_json(source: &[u8]) -> Json {
/// Convert JSON value to equivlaent RLP representation. /// Convert JSON value to equivlaent RLP representation.
// TODO: handle container types. // TODO: handle container types.
fn json_to_rlp(json: &Json) -> Bytes { fn json_to_rlp(json: &Json) -> Bytes {
match json { match *json {
&Json::Boolean(o) => encode(&(if o {1u64} else {0})), Json::Boolean(o) => encode(&(if o {1u64} else {0})),
&Json::I64(o) => encode(&(o as u64)), Json::I64(o) => encode(&(o as u64)),
&Json::U64(o) => encode(&o), Json::U64(o) => encode(&o),
&Json::String(ref s) if s.len() >= 2 && &s[0..2] == "0x" && U256::from_str(&s[2..]).is_ok() => { Json::String(ref s) if s.len() >= 2 && &s[0..2] == "0x" && U256::from_str(&s[2..]).is_ok() => {
encode(&U256::from_str(&s[2..]).unwrap()) encode(&U256::from_str(&s[2..]).unwrap())
}, },
&Json::String(ref s) => { Json::String(ref s) => {
encode(s) encode(s)
}, },
_ => panic!() _ => panic!()
@ -40,28 +41,6 @@ fn json_to_rlp_map(json: &Json) -> HashMap<String, Bytes> {
}) })
} }
//TODO: add code and data
#[derive(Debug)]
/// Genesis account data. Does no thave a DB overlay cache
pub struct GenesisAccount {
// Balance of the account.
balance: U256,
// Nonce of the account.
nonce: U256,
}
impl GenesisAccount {
/// TODO [arkpar] Please document me
pub fn rlp(&self) -> Bytes {
let mut stream = RlpStream::new_list(4);
stream.append(&self.nonce);
stream.append(&self.balance);
stream.append(&SHA3_NULL_RLP);
stream.append(&SHA3_EMPTY);
stream.out()
}
}
/// Parameters for a block chain; includes both those intrinsic to the design of the /// Parameters for a block chain; includes both those intrinsic to the design of the
/// chain and those to be interpreted by the active chain engine. /// chain and those to be interpreted by the active chain engine.
#[derive(Debug)] #[derive(Debug)]
@ -73,6 +52,9 @@ pub struct Spec {
/// TODO [Gav Wood] Please document me /// TODO [Gav Wood] Please document me
pub engine_name: String, pub engine_name: String,
/// Known nodes on the network in enode format.
pub nodes: Vec<String>,
// Parameters concerning operation of the specific engine we're using. // Parameters concerning operation of the specific engine we're using.
// Name -> RLP-encoded value // Name -> RLP-encoded value
/// TODO [Gav Wood] Please document me /// TODO [Gav Wood] Please document me
@ -80,7 +62,7 @@ pub struct Spec {
// Builtin-contracts are here for now but would like to abstract into Engine API eventually. // Builtin-contracts are here for now but would like to abstract into Engine API eventually.
/// TODO [Gav Wood] Please document me /// TODO [Gav Wood] Please document me
pub builtins: HashMap<Address, Builtin>, pub builtins: BTreeMap<Address, Builtin>,
// Genesis params. // Genesis params.
/// TODO [Gav Wood] Please document me /// TODO [Gav Wood] Please document me
@ -98,7 +80,7 @@ pub struct Spec {
/// TODO [arkpar] Please document me /// TODO [arkpar] Please document me
pub extra_data: Bytes, pub extra_data: Bytes,
/// TODO [Gav Wood] Please document me /// TODO [Gav Wood] Please document me
pub genesis_state: HashMap<Address, GenesisAccount>, genesis_state: PodState,
/// TODO [Gav Wood] Please document me /// TODO [Gav Wood] Please document me
pub seal_fields: usize, pub seal_fields: usize,
/// TODO [Gav Wood] Please document me /// TODO [Gav Wood] Please document me
@ -108,6 +90,7 @@ pub struct Spec {
state_root_memo: RwLock<Option<H256>>, state_root_memo: RwLock<Option<H256>>,
} }
#[allow(wrong_self_convention)] // because to_engine(self) should be to_engine(&self)
impl Spec { impl Spec {
/// Convert this object into a boxed Engine of the right underlying type. /// Convert this object into a boxed Engine of the right underlying type.
// TODO avoid this hard-coded nastiness - use dynamic-linked plugin framework instead. // TODO avoid this hard-coded nastiness - use dynamic-linked plugin framework instead.
@ -122,11 +105,14 @@ impl Spec {
/// Return the state root for the genesis state, memoising accordingly. /// Return the state root for the genesis state, memoising accordingly.
pub fn state_root(&self) -> H256 { pub fn state_root(&self) -> H256 {
if self.state_root_memo.read().unwrap().is_none() { if self.state_root_memo.read().unwrap().is_none() {
*self.state_root_memo.write().unwrap() = Some(sec_trie_root(self.genesis_state.iter().map(|(k, v)| (k.to_vec(), v.rlp())).collect())); *self.state_root_memo.write().unwrap() = Some(self.genesis_state.root());
} }
self.state_root_memo.read().unwrap().as_ref().unwrap().clone() self.state_root_memo.read().unwrap().as_ref().unwrap().clone()
} }
/// Get the known knodes of the network in enode format.
pub fn nodes(&self) -> &Vec<String> { &self.nodes }
/// TODO [Gav Wood] Please document me /// TODO [Gav Wood] Please document me
pub fn genesis_header(&self) -> Header { pub fn genesis_header(&self) -> Header {
Header { Header {
@ -167,6 +153,46 @@ impl Spec {
ret.append_raw(&empty_list, 1); ret.append_raw(&empty_list, 1);
ret.out() ret.out()
} }
/// Overwrite the genesis components with the given JSON, assuming standard Ethereum test format.
pub fn overwrite_genesis(&mut self, genesis: &Json) {
let (seal_fields, seal_rlp) = {
if genesis.find("mixHash").is_some() && genesis.find("nonce").is_some() {
let mut s = RlpStream::new();
s.append(&H256::from_json(&genesis["mixHash"]));
s.append(&H64::from_json(&genesis["nonce"]));
(2, s.out())
} else {
// backup algo that will work with sealFields/sealRlp (and without).
(
u64::from_json(&genesis["sealFields"]) as usize,
Bytes::from_json(&genesis["sealRlp"])
)
}
};
self.parent_hash = H256::from_json(&genesis["parentHash"]);
self.author = Address::from_json(&genesis["coinbase"]);
self.difficulty = U256::from_json(&genesis["difficulty"]);
self.gas_limit = U256::from_json(&genesis["gasLimit"]);
self.gas_used = U256::from_json(&genesis["gasUsed"]);
self.timestamp = u64::from_json(&genesis["timestamp"]);
self.extra_data = Bytes::from_json(&genesis["extraData"]);
self.seal_fields = seal_fields;
self.seal_rlp = seal_rlp;
self.state_root_memo = RwLock::new(genesis.find("stateRoot").and_then(|_| Some(H256::from_json(&genesis["stateRoot"]))));
}
/// Alter the value of the genesis state.
pub fn set_genesis_state(&mut self, s: PodState) {
self.genesis_state = s;
*self.state_root_memo.write().unwrap() = None;
}
/// Returns `false` if the memoized state root is invalid. `true` otherwise.
pub fn is_state_root_valid(&self) -> bool {
self.state_root_memo.read().unwrap().clone().map_or(true, |sr| sr == self.genesis_state.root())
}
} }
impl FromJson for Spec { impl FromJson for Spec {
@ -174,8 +200,8 @@ impl FromJson for Spec {
fn from_json(json: &Json) -> Spec { fn from_json(json: &Json) -> Spec {
// once we commit ourselves to some json parsing library (serde?) // once we commit ourselves to some json parsing library (serde?)
// move it to proper data structure // move it to proper data structure
let mut state = HashMap::new(); let mut builtins = BTreeMap::new();
let mut builtins = HashMap::new(); let mut state = PodState::new();
if let Some(&Json::Object(ref accounts)) = json.find("accounts") { if let Some(&Json::Object(ref accounts)) = json.find("accounts") {
for (address, acc) in accounts.iter() { for (address, acc) in accounts.iter() {
@ -185,17 +211,14 @@ impl FromJson for Spec {
builtins.insert(addr.clone(), builtin); builtins.insert(addr.clone(), builtin);
} }
} }
let balance = acc.find("balance").and_then(|x| match x { &Json::String(ref b) => U256::from_dec_str(b).ok(), _ => None });
let nonce = acc.find("nonce").and_then(|x| match x { &Json::String(ref b) => U256::from_dec_str(b).ok(), _ => None });
// let balance = if let Some(&Json::String(ref b)) = acc.find("balance") {U256::from_dec_str(b).unwrap_or(U256::from(0))} else {U256::from(0)};
// let nonce = if let Some(&Json::String(ref n)) = acc.find("nonce") {U256::from_dec_str(n).unwrap_or(U256::from(0))} else {U256::from(0)};
// TODO: handle code & data if they exist.
if balance.is_some() || nonce.is_some() {
state.insert(addr, GenesisAccount { balance: balance.unwrap_or(U256::from(0)), nonce: nonce.unwrap_or(U256::from(0)) });
}
} }
state = xjson!(&json["accounts"]);
} }
let nodes = if let Some(&Json::Array(ref ns)) = json.find("nodes") {
ns.iter().filter_map(|n| if let Json::String(ref s) = *n { Some(s.clone()) } else {None}).collect()
} else { Vec::new() };
let genesis = &json["genesis"];//.as_object().expect("No genesis object in JSON"); let genesis = &json["genesis"];//.as_object().expect("No genesis object in JSON");
let (seal_fields, seal_rlp) = { let (seal_fields, seal_rlp) = {
@ -213,11 +236,11 @@ impl FromJson for Spec {
} }
}; };
Spec { Spec {
name: json.find("name").map(|j| j.as_string().unwrap()).unwrap_or("unknown").to_string(), name: json.find("name").map_or("unknown", |j| j.as_string().unwrap()).to_owned(),
engine_name: json["engineName"].as_string().unwrap().to_string(), engine_name: json["engineName"].as_string().unwrap().to_owned(),
engine_params: json_to_rlp_map(&json["params"]), engine_params: json_to_rlp_map(&json["params"]),
nodes: nodes,
builtins: builtins, builtins: builtins,
parent_hash: H256::from_str(&genesis["parentHash"].as_string().unwrap()[2..]).unwrap(), parent_hash: H256::from_str(&genesis["parentHash"].as_string().unwrap()[2..]).unwrap(),
author: Address::from_str(&genesis["author"].as_string().unwrap()[2..]).unwrap(), author: Address::from_str(&genesis["author"].as_string().unwrap()[2..]).unwrap(),
@ -238,16 +261,17 @@ impl Spec {
/// Ensure that the given state DB has the trie nodes in for the genesis state. /// Ensure that the given state DB has the trie nodes in for the genesis state.
pub fn ensure_db_good(&self, db: &mut HashDB) -> bool { pub fn ensure_db_good(&self, db: &mut HashDB) -> bool {
if !db.contains(&self.state_root()) { if !db.contains(&self.state_root()) {
info!("Populating genesis state...");
let mut root = H256::new(); let mut root = H256::new();
{ {
let mut t = SecTrieDBMut::new(db, &mut root); let mut t = SecTrieDBMut::new(db, &mut root);
for (address, account) in self.genesis_state.iter() { for (address, account) in self.genesis_state.get().iter() {
t.insert(address.as_slice(), &account.rlp()); t.insert(address.as_slice(), &account.rlp());
} }
} }
for (_, account) in self.genesis_state.get().iter() {
account.insert_additional(db);
}
assert!(db.contains(&self.state_root())); assert!(db.contains(&self.state_root()));
info!("Genesis state is ready");
true true
} else { false } } else { false }
} }

View File

@ -3,7 +3,7 @@ use engine::Engine;
use executive::Executive; use executive::Executive;
use pod_account::*; use pod_account::*;
use pod_state::*; use pod_state::*;
use state_diff::*; //use state_diff::*; // TODO: uncomment once to_pod() works correctly.
/// TODO [Gav Wood] Please document me /// TODO [Gav Wood] Please document me
pub type ApplyResult = Result<Receipt, Error>; pub type ApplyResult = Result<Receipt, Error>;
@ -88,22 +88,22 @@ impl State {
/// Get the balance of account `a`. /// Get the balance of account `a`.
pub fn balance(&self, a: &Address) -> U256 { pub fn balance(&self, a: &Address) -> U256 {
self.get(a, false).as_ref().map(|account| account.balance().clone()).unwrap_or(U256::from(0u8)) self.get(a, false).as_ref().map_or(U256::zero(), |account| account.balance().clone())
} }
/// Get the nonce of account `a`. /// Get the nonce of account `a`.
pub fn nonce(&self, a: &Address) -> U256 { pub fn nonce(&self, a: &Address) -> U256 {
self.get(a, false).as_ref().map(|account| account.nonce().clone()).unwrap_or(U256::from(0u8)) self.get(a, false).as_ref().map_or(U256::zero(), |account| account.nonce().clone())
} }
/// Mutate storage of account `a` so that it is `value` for `key`. /// Mutate storage of account `a` so that it is `value` for `key`.
pub fn storage_at(&self, a: &Address, key: &H256) -> H256 { pub fn storage_at(&self, a: &Address, key: &H256) -> H256 {
self.get(a, false).as_ref().map(|a|a.storage_at(&self.db, key)).unwrap_or(H256::new()) self.get(a, false).as_ref().map_or(H256::new(), |a|a.storage_at(&self.db, key))
} }
/// Mutate storage of account `a` so that it is `value` for `key`. /// Mutate storage of account `a` so that it is `value` for `key`.
pub fn code(&self, a: &Address) -> Option<Bytes> { pub fn code(&self, a: &Address) -> Option<Bytes> {
self.get(a, true).as_ref().map(|a|a.code().map(|x|x.to_vec())).unwrap_or(None) self.get(a, true).as_ref().map_or(None, |a|a.code().map(|x|x.to_vec()))
} }
/// Add `incr` to the balance of account `a`. /// Add `incr` to the balance of account `a`.
@ -145,16 +145,16 @@ impl State {
/// Execute a given transaction. /// Execute a given transaction.
/// This will change the state accordingly. /// This will change the state accordingly.
pub fn apply(&mut self, env_info: &EnvInfo, engine: &Engine, t: &Transaction) -> ApplyResult { pub fn apply(&mut self, env_info: &EnvInfo, engine: &Engine, t: &Transaction) -> ApplyResult {
// let old = self.to_pod();
let old = self.to_pod();
let e = try!(Executive::new(self, env_info, engine).transact(t)); let e = try!(Executive::new(self, env_info, engine).transact(t));
//println!("Executed: {:?}", e); //println!("Executed: {:?}", e);
trace!("Applied transaction. Diff:\n{}\n", StateDiff::diff_pod(&old, &self.to_pod())); // TODO uncomment once to_pod() works correctly.
// trace!("Applied transaction. Diff:\n{}\n", StateDiff::diff_pod(&old, &self.to_pod()));
self.commit(); self.commit();
let receipt = Receipt::new(self.root().clone(), e.cumulative_gas_used, e.logs); let receipt = Receipt::new(self.root().clone(), e.cumulative_gas_used, e.logs);
trace!("Transaction receipt: {:?}", receipt); // trace!("Transaction receipt: {:?}", receipt);
Ok(receipt) Ok(receipt)
} }
@ -170,6 +170,7 @@ impl State {
/// Commit accounts to SecTrieDBMut. This is similar to cpp-ethereum's dev::eth::commit. /// Commit accounts to SecTrieDBMut. This is similar to cpp-ethereum's dev::eth::commit.
/// `accounts` is mutable because we may need to commit the code or storage and record that. /// `accounts` is mutable because we may need to commit the code or storage and record that.
#[allow(match_ref_pats)]
pub fn commit_into(db: &mut HashDB, root: &mut H256, accounts: &mut HashMap<Address, Option<Account>>) { pub fn commit_into(db: &mut HashDB, root: &mut H256, accounts: &mut HashMap<Address, Option<Account>>) {
// first, commit the sub trees. // first, commit the sub trees.
// TODO: is this necessary or can we dispense with the `ref mut a` for just `a`? // TODO: is this necessary or can we dispense with the `ref mut a` for just `a`?
@ -186,9 +187,9 @@ impl State {
{ {
let mut trie = SecTrieDBMut::from_existing(db, root); let mut trie = SecTrieDBMut::from_existing(db, root);
for (address, ref a) in accounts.iter() { for (address, ref a) in accounts.iter() {
match a { match **a {
&&Some(ref account) => trie.insert(address, &account.rlp()), Some(ref account) => trie.insert(address, &account.rlp()),
&&None => trie.remove(address), None => trie.remove(address),
} }
} }
} }
@ -210,7 +211,7 @@ impl State {
pub fn to_hashmap_pod(&self) -> HashMap<Address, PodAccount> { pub fn to_hashmap_pod(&self) -> HashMap<Address, PodAccount> {
// TODO: handle database rather than just the cache. // TODO: handle database rather than just the cache.
self.cache.borrow().iter().fold(HashMap::new(), |mut m, (add, opt)| { self.cache.borrow().iter().fold(HashMap::new(), |mut m, (add, opt)| {
if let &Some(ref acc) = opt { if let Some(ref acc) = *opt {
m.insert(add.clone(), PodAccount::from_account(acc)); m.insert(add.clone(), PodAccount::from_account(acc));
} }
m m
@ -220,8 +221,8 @@ impl State {
/// Populate a PodAccount map from this state. /// Populate a PodAccount map from this state.
pub fn to_pod(&self) -> PodState { pub fn to_pod(&self) -> PodState {
// TODO: handle database rather than just the cache. // TODO: handle database rather than just the cache.
PodState::new(self.cache.borrow().iter().fold(BTreeMap::new(), |mut m, (add, opt)| { PodState::from(self.cache.borrow().iter().fold(BTreeMap::new(), |mut m, (add, opt)| {
if let &Some(ref acc) = opt { if let Some(ref acc) = *opt {
m.insert(add.clone(), PodAccount::from_account(acc)); m.insert(add.clone(), PodAccount::from_account(acc));
} }
m m

View File

@ -15,7 +15,7 @@ impl StateDiff {
impl fmt::Display for StateDiff { impl fmt::Display for StateDiff {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
for (add, acc) in self.0.iter() { for (add, acc) in &self.0 {
try!(write!(f, "{} {}: {}", acc.existance(), add, acc)); try!(write!(f, "{} {}: {}", acc.existance(), add, acc));
} }
Ok(()) Ok(())
@ -32,8 +32,8 @@ mod test {
#[test] #[test]
fn create_delete() { fn create_delete() {
let a = PodState::new(map![ x!(1) => PodAccount::new(x!(69), x!(0), vec![], map![]) ]); let a = PodState::from(map![ x!(1) => PodAccount::new(x!(69), x!(0), vec![], map![]) ]);
assert_eq!(StateDiff::diff_pod(&a, &PodState::new(map![])), StateDiff(map![ assert_eq!(StateDiff::diff_pod(&a, &PodState::new()), StateDiff(map![
x!(1) => AccountDiff{ x!(1) => AccountDiff{
balance: Diff::Died(x!(69)), balance: Diff::Died(x!(69)),
nonce: Diff::Died(x!(0)), nonce: Diff::Died(x!(0)),
@ -41,7 +41,7 @@ mod test {
storage: map![], storage: map![],
} }
])); ]));
assert_eq!(StateDiff::diff_pod(&PodState::new(map![]), &a), StateDiff(map![ assert_eq!(StateDiff::diff_pod(&PodState::new(), &a), StateDiff(map![
x!(1) => AccountDiff{ x!(1) => AccountDiff{
balance: Diff::Born(x!(69)), balance: Diff::Born(x!(69)),
nonce: Diff::Born(x!(0)), nonce: Diff::Born(x!(0)),
@ -53,8 +53,8 @@ mod test {
#[test] #[test]
fn create_delete_with_unchanged() { fn create_delete_with_unchanged() {
let a = PodState::new(map![ x!(1) => PodAccount::new(x!(69), x!(0), vec![], map![]) ]); let a = PodState::from(map![ x!(1) => PodAccount::new(x!(69), x!(0), vec![], map![]) ]);
let b = PodState::new(map![ let b = PodState::from(map![
x!(1) => PodAccount::new(x!(69), x!(0), vec![], map![]), x!(1) => PodAccount::new(x!(69), x!(0), vec![], map![]),
x!(2) => PodAccount::new(x!(69), x!(0), vec![], map![]) x!(2) => PodAccount::new(x!(69), x!(0), vec![], map![])
]); ]);
@ -78,11 +78,11 @@ mod test {
#[test] #[test]
fn change_with_unchanged() { fn change_with_unchanged() {
let a = PodState::new(map![ let a = PodState::from(map![
x!(1) => PodAccount::new(x!(69), x!(0), vec![], map![]), x!(1) => PodAccount::new(x!(69), x!(0), vec![], map![]),
x!(2) => PodAccount::new(x!(69), x!(0), vec![], map![]) x!(2) => PodAccount::new(x!(69), x!(0), vec![], map![])
]); ]);
let b = PodState::new(map![ let b = PodState::from(map![
x!(1) => PodAccount::new(x!(69), x!(1), vec![], map![]), x!(1) => PodAccount::new(x!(69), x!(1), vec![], map![]),
x!(2) => PodAccount::new(x!(69), x!(0), vec![], map![]) x!(2) => PodAccount::new(x!(69), x!(0), vec![], map![])
]); ]);

View File

@ -107,6 +107,10 @@ pub struct SyncStatus {
pub blocks_total: usize, pub blocks_total: usize,
/// Number of blocks downloaded so far. /// Number of blocks downloaded so far.
pub blocks_received: usize, pub blocks_received: usize,
/// Total number of connected peers
pub num_peers: usize,
/// Total number of active peers
pub num_active_peers: usize,
} }
#[derive(PartialEq, Eq, Debug)] #[derive(PartialEq, Eq, Debug)]
@ -195,8 +199,10 @@ impl ChainSync {
start_block_number: self.starting_block, start_block_number: self.starting_block,
last_imported_block_number: self.last_imported_block, last_imported_block_number: self.last_imported_block,
highest_block_number: self.highest_block, highest_block_number: self.highest_block,
blocks_total: (self.last_imported_block - self.starting_block) as usize, blocks_received: (self.last_imported_block - self.starting_block) as usize,
blocks_received: (self.highest_block - self.starting_block) as usize, blocks_total: (self.highest_block - self.starting_block) as usize,
num_peers: self.peers.len(),
num_active_peers: self.peers.values().filter(|p| p.asking != PeerAsking::Nothing).count(),
} }
} }
@ -212,7 +218,7 @@ impl ChainSync {
self.downloading_bodies.clear(); self.downloading_bodies.clear();
self.headers.clear(); self.headers.clear();
self.bodies.clear(); self.bodies.clear();
for (_, ref mut p) in self.peers.iter_mut() { for (_, ref mut p) in &mut self.peers {
p.asking_blocks.clear(); p.asking_blocks.clear();
} }
self.header_ids.clear(); self.header_ids.clear();
@ -268,6 +274,7 @@ impl ChainSync {
Ok(()) Ok(())
} }
#[allow(cyclomatic_complexity)]
/// Called by peer once it has new block headers during sync /// Called by peer once it has new block headers during sync
fn on_peer_block_headers(&mut self, io: &mut SyncIo, peer_id: PeerId, r: &UntrustedRlp) -> Result<(), PacketDecodeError> { fn on_peer_block_headers(&mut self, io: &mut SyncIo, peer_id: PeerId, r: &UntrustedRlp) -> Result<(), PacketDecodeError> {
self.reset_peer_asking(peer_id, PeerAsking::BlockHeaders); self.reset_peer_asking(peer_id, PeerAsking::BlockHeaders);
@ -375,7 +382,7 @@ impl ChainSync {
transactions_root: tx_root, transactions_root: tx_root,
uncles: uncles uncles: uncles
}; };
match self.header_ids.get(&header_id).map(|n| *n) { match self.header_ids.get(&header_id).cloned() {
Some(n) => { Some(n) => {
self.header_ids.remove(&header_id); self.header_ids.remove(&header_id);
self.bodies.insert_item(n, body.as_raw().to_vec()); self.bodies.insert_item(n, body.as_raw().to_vec());
@ -408,7 +415,7 @@ impl ChainSync {
Err(ImportError::AlreadyQueued) => { Err(ImportError::AlreadyQueued) => {
trace!(target: "sync", "New block already queued {:?}", h); trace!(target: "sync", "New block already queued {:?}", h);
}, },
Ok(()) => { Ok(_) => {
trace!(target: "sync", "New block queued {:?}", h); trace!(target: "sync", "New block queued {:?}", h);
}, },
Err(e) => { Err(e) => {
@ -424,6 +431,10 @@ impl ChainSync {
let peer_difficulty = self.peers.get_mut(&peer_id).expect("ChainSync: unknown peer").difficulty; let peer_difficulty = self.peers.get_mut(&peer_id).expect("ChainSync: unknown peer").difficulty;
if difficulty > peer_difficulty { if difficulty > peer_difficulty {
trace!(target: "sync", "Received block {:?} with no known parent. Peer needs syncing...", h); trace!(target: "sync", "Received block {:?} with no known parent. Peer needs syncing...", h);
{
let peer = self.peers.get_mut(&peer_id).expect("ChainSync: unknown peer");
peer.latest = header_view.sha3();
}
self.sync_peer(io, peer_id, true); self.sync_peer(io, peer_id, true);
} }
} }
@ -464,6 +475,9 @@ impl ChainSync {
} }
} }
}; };
if max_height != x!(0) {
self.sync_peer(io, peer_id, true);
}
Ok(()) Ok(())
} }
@ -540,7 +554,7 @@ impl ChainSync {
fn request_blocks(&mut self, io: &mut SyncIo, peer_id: PeerId) { fn request_blocks(&mut self, io: &mut SyncIo, peer_id: PeerId) {
self.clear_peer_download(peer_id); self.clear_peer_download(peer_id);
if io.chain().queue_status().full { if io.chain().queue_info().full {
self.pause_sync(); self.pause_sync();
return; return;
} }
@ -666,7 +680,7 @@ impl ChainSync {
self.last_imported_block = headers.0 + i as BlockNumber; self.last_imported_block = headers.0 + i as BlockNumber;
self.last_imported_hash = h.clone(); self.last_imported_hash = h.clone();
}, },
Ok(()) => { Ok(_) => {
trace!(target: "sync", "Block queued {:?}", h); trace!(target: "sync", "Block queued {:?}", h);
self.last_imported_block = headers.0 + i as BlockNumber; self.last_imported_block = headers.0 + i as BlockNumber;
self.last_imported_hash = h.clone(); self.last_imported_hash = h.clone();
@ -699,16 +713,13 @@ impl ChainSync {
/// Used to recover from an error and re-download parts of the chain detected as bad. /// Used to recover from an error and re-download parts of the chain detected as bad.
fn remove_downloaded_blocks(&mut self, start: BlockNumber) { fn remove_downloaded_blocks(&mut self, start: BlockNumber) {
for n in self.headers.get_tail(&start) { for n in self.headers.get_tail(&start) {
match self.headers.find_item(&n) { if let Some(ref header_data) = self.headers.find_item(&n) {
Some(ref header_data) => {
let header_to_delete = HeaderView::new(&header_data.data); let header_to_delete = HeaderView::new(&header_data.data);
let header_id = HeaderId { let header_id = HeaderId {
transactions_root: header_to_delete.transactions_root(), transactions_root: header_to_delete.transactions_root(),
uncles: header_to_delete.uncles_hash() uncles: header_to_delete.uncles_hash()
}; };
self.header_ids.remove(&header_id); self.header_ids.remove(&header_id);
},
None => {}
} }
self.downloading_bodies.remove(&n); self.downloading_bodies.remove(&n);
self.downloading_headers.remove(&n); self.downloading_headers.remove(&n);
@ -796,13 +807,10 @@ impl ChainSync {
packet.append(&chain.best_block_hash); packet.append(&chain.best_block_hash);
packet.append(&chain.genesis_hash); packet.append(&chain.genesis_hash);
//TODO: handle timeout for status request //TODO: handle timeout for status request
match io.send(peer_id, STATUS_PACKET, packet.out()) { if let Err(e) = io.send(peer_id, STATUS_PACKET, packet.out()) {
Err(e) => {
warn!(target:"sync", "Error sending status request: {:?}", e); warn!(target:"sync", "Error sending status request: {:?}", e);
io.disable_peer(peer_id); io.disable_peer(peer_id);
} }
Ok(_) => ()
}
} }
/// Respond to GetBlockHeaders request /// Respond to GetBlockHeaders request
@ -837,13 +845,10 @@ impl ChainSync {
let mut data = Bytes::new(); let mut data = Bytes::new();
let inc = (skip + 1) as BlockNumber; let inc = (skip + 1) as BlockNumber;
while number <= last && number > 0 && count < max_count { while number <= last && number > 0 && count < max_count {
match io.chain().block_header_at(number) { if let Some(mut hdr) = io.chain().block_header_at(number) {
Some(mut hdr) => {
data.append(&mut hdr); data.append(&mut hdr);
count += 1; count += 1;
} }
None => {}
}
if reverse { if reverse {
if number <= inc { if number <= inc {
break; break;
@ -874,13 +879,10 @@ impl ChainSync {
let mut added = 0usize; let mut added = 0usize;
let mut data = Bytes::new(); let mut data = Bytes::new();
for i in 0..count { for i in 0..count {
match io.chain().block_body(&try!(r.val_at::<H256>(i))) { if let Some(mut hdr) = io.chain().block_body(&try!(r.val_at::<H256>(i))) {
Some(mut hdr) => {
data.append(&mut hdr); data.append(&mut hdr);
added += 1; added += 1;
} }
None => {}
}
} }
let mut rlp = RlpStream::new_list(added); let mut rlp = RlpStream::new_list(added);
rlp.append_raw(&data, added); rlp.append_raw(&data, added);
@ -901,13 +903,10 @@ impl ChainSync {
let mut added = 0usize; let mut added = 0usize;
let mut data = Bytes::new(); let mut data = Bytes::new();
for i in 0..count { for i in 0..count {
match io.chain().state_data(&try!(r.val_at::<H256>(i))) { if let Some(mut hdr) = io.chain().state_data(&try!(r.val_at::<H256>(i))) {
Some(mut hdr) => {
data.append(&mut hdr); data.append(&mut hdr);
added += 1; added += 1;
} }
None => {}
}
} }
let mut rlp = RlpStream::new_list(added); let mut rlp = RlpStream::new_list(added);
rlp.append_raw(&data, added); rlp.append_raw(&data, added);
@ -927,13 +926,10 @@ impl ChainSync {
let mut added = 0usize; let mut added = 0usize;
let mut data = Bytes::new(); let mut data = Bytes::new();
for i in 0..count { for i in 0..count {
match io.chain().block_receipts(&try!(r.val_at::<H256>(i))) { if let Some(mut hdr) = io.chain().block_receipts(&try!(r.val_at::<H256>(i))) {
Some(mut hdr) => {
data.append(&mut hdr); data.append(&mut hdr);
added += 1; added += 1;
} }
None => {}
}
} }
let mut rlp = RlpStream::new_list(added); let mut rlp = RlpStream::new_list(added);
rlp.append_raw(&data, added); rlp.append_raw(&data, added);
@ -967,7 +963,7 @@ impl ChainSync {
} }
/// Maintain other peers. Send out any new blocks and transactions /// Maintain other peers. Send out any new blocks and transactions
pub fn maintain_sync(&mut self, _io: &mut SyncIo) { pub fn _maintain_sync(&mut self, _io: &mut SyncIo) {
} }
} }

View File

@ -1,7 +1,7 @@
use client::BlockChainClient; use client::BlockChainClient;
use util::{NetworkContext, PeerId, PacketId,}; use util::{NetworkContext, PeerId, PacketId,};
use util::error::UtilError; use util::error::UtilError;
use sync::SyncMessage; use service::SyncMessage;
/// IO interface for the syning handler. /// IO interface for the syning handler.
/// Provides peer connection management and an interface to the blockchain client. /// Provides peer connection management and an interface to the blockchain client.
@ -14,7 +14,7 @@ pub trait SyncIo {
/// Send a packet to a peer. /// Send a packet to a peer.
fn send(&mut self, peer_id: PeerId, packet_id: PacketId, data: Vec<u8>) -> Result<(), UtilError>; fn send(&mut self, peer_id: PeerId, packet_id: PacketId, data: Vec<u8>) -> Result<(), UtilError>;
/// Get the blockchain /// Get the blockchain
fn chain<'s>(&'s mut self) -> &'s mut BlockChainClient; fn chain(&self) -> &BlockChainClient;
/// Returns peer client identifier string /// Returns peer client identifier string
fn peer_info(&self, peer_id: PeerId) -> String { fn peer_info(&self, peer_id: PeerId) -> String {
peer_id.to_string() peer_id.to_string()
@ -22,14 +22,14 @@ pub trait SyncIo {
} }
/// Wraps `NetworkContext` and the blockchain client /// Wraps `NetworkContext` and the blockchain client
pub struct NetSyncIo<'s, 'h, 'io> where 'h: 's, 'io: 'h { pub struct NetSyncIo<'s, 'h> where 'h: 's {
network: &'s mut NetworkContext<'h, 'io, SyncMessage>, network: &'s NetworkContext<'h, SyncMessage>,
chain: &'s mut BlockChainClient chain: &'s BlockChainClient
} }
impl<'s, 'h, 'io> NetSyncIo<'s, 'h, 'io> { impl<'s, 'h> NetSyncIo<'s, 'h> {
/// Creates a new instance from the `NetworkContext` and the blockchain client reference. /// Creates a new instance from the `NetworkContext` and the blockchain client reference.
pub fn new(network: &'s mut NetworkContext<'h, 'io, SyncMessage>, chain: &'s mut BlockChainClient) -> NetSyncIo<'s,'h,'io> { pub fn new(network: &'s NetworkContext<'h, SyncMessage>, chain: &'s BlockChainClient) -> NetSyncIo<'s, 'h> {
NetSyncIo { NetSyncIo {
network: network, network: network,
chain: chain, chain: chain,
@ -37,7 +37,7 @@ impl<'s, 'h, 'io> NetSyncIo<'s, 'h, 'io> {
} }
} }
impl<'s, 'h, 'op> SyncIo for NetSyncIo<'s, 'h, 'op> { impl<'s, 'h> SyncIo for NetSyncIo<'s, 'h> {
fn disable_peer(&mut self, peer_id: PeerId) { fn disable_peer(&mut self, peer_id: PeerId) {
self.network.disable_peer(peer_id); self.network.disable_peer(peer_id);
} }
@ -50,7 +50,7 @@ impl<'s, 'h, 'op> SyncIo for NetSyncIo<'s, 'h, 'op> {
self.network.send(peer_id, packet_id, data) self.network.send(peer_id, packet_id, data)
} }
fn chain<'a>(&'a mut self) -> &'a mut BlockChainClient { fn chain(&self) -> &BlockChainClient {
self.chain self.chain
} }

View File

@ -9,15 +9,15 @@
/// extern crate ethcore; /// extern crate ethcore;
/// use std::env; /// use std::env;
/// use std::sync::Arc; /// use std::sync::Arc;
/// use util::network::NetworkService; /// use util::network::{NetworkService, NetworkConfiguration};
/// use ethcore::client::Client; /// use ethcore::client::Client;
/// use ethcore::sync::EthSync; /// use ethcore::sync::EthSync;
/// use ethcore::ethereum; /// use ethcore::ethereum;
/// ///
/// fn main() { /// fn main() {
/// let mut service = NetworkService::start().unwrap(); /// let mut service = NetworkService::start(NetworkConfiguration::new()).unwrap();
/// let dir = env::temp_dir(); /// let dir = env::temp_dir();
/// let client = Arc::new(Client::new(ethereum::new_frontier(), &dir).unwrap()); /// let client = Client::new(ethereum::new_frontier(), &dir, service.io().channel()).unwrap();
/// EthSync::register(&mut service, client); /// EthSync::register(&mut service, client);
/// } /// }
/// ``` /// ```
@ -25,10 +25,9 @@
use std::ops::*; use std::ops::*;
use std::sync::*; use std::sync::*;
use client::Client; use client::Client;
use util::network::{NetworkProtocolHandler, NetworkService, NetworkContext, PeerId, NetworkIoMessage}; use util::network::{NetworkProtocolHandler, NetworkService, NetworkContext, PeerId};
use util::TimerToken;
use util::Bytes;
use sync::chain::ChainSync; use sync::chain::ChainSync;
use service::SyncMessage;
use sync::io::NetSyncIo; use sync::io::NetSyncIo;
mod chain; mod chain;
@ -38,76 +37,57 @@ mod range_collection;
#[cfg(test)] #[cfg(test)]
mod tests; mod tests;
/// Message type for external events
pub enum SyncMessage {
/// New block has been imported into the blockchain
NewChainBlock(Bytes),
/// A block is ready
BlockVerified,
}
/// TODO [arkpar] Please document me
pub type NetSyncMessage = NetworkIoMessage<SyncMessage>;
/// Ethereum network protocol handler /// Ethereum network protocol handler
pub struct EthSync { pub struct EthSync {
/// Shared blockchain client. TODO: this should evetually become an IPC endpoint /// Shared blockchain client. TODO: this should evetually become an IPC endpoint
chain: Arc<RwLock<Client>>, chain: Arc<Client>,
/// Sync strategy /// Sync strategy
sync: ChainSync sync: RwLock<ChainSync>
} }
pub use self::chain::SyncStatus; pub use self::chain::SyncStatus;
impl EthSync { impl EthSync {
/// Creates and register protocol with the network service /// Creates and register protocol with the network service
pub fn register(service: &mut NetworkService<SyncMessage>, chain: Arc<RwLock<Client>>) { pub fn register(service: &mut NetworkService<SyncMessage>, chain: Arc<Client>) -> Arc<EthSync> {
let sync = Box::new(EthSync { let sync = Arc::new(EthSync {
chain: chain, chain: chain,
sync: ChainSync::new(), sync: RwLock::new(ChainSync::new()),
}); });
service.register_protocol(sync, "eth", &[62u8, 63u8]).expect("Error registering eth protocol handler"); service.register_protocol(sync.clone(), "eth", &[62u8, 63u8]).expect("Error registering eth protocol handler");
sync
} }
/// Get sync status /// Get sync status
pub fn status(&self) -> SyncStatus { pub fn status(&self) -> SyncStatus {
self.sync.status() self.sync.read().unwrap().status()
} }
/// Stop sync /// Stop sync
pub fn stop(&mut self, io: &mut NetworkContext<SyncMessage>) { pub fn stop(&mut self, io: &mut NetworkContext<SyncMessage>) {
self.sync.abort(&mut NetSyncIo::new(io, self.chain.write().unwrap().deref_mut())); self.sync.write().unwrap().abort(&mut NetSyncIo::new(io, self.chain.deref()));
} }
/// Restart sync /// Restart sync
pub fn restart(&mut self, io: &mut NetworkContext<SyncMessage>) { pub fn restart(&mut self, io: &mut NetworkContext<SyncMessage>) {
self.sync.restart(&mut NetSyncIo::new(io, self.chain.write().unwrap().deref_mut())); self.sync.write().unwrap().restart(&mut NetSyncIo::new(io, self.chain.deref()));
} }
} }
impl NetworkProtocolHandler<SyncMessage> for EthSync { impl NetworkProtocolHandler<SyncMessage> for EthSync {
fn initialize(&mut self, io: &mut NetworkContext<SyncMessage>) { fn initialize(&self, _io: &NetworkContext<SyncMessage>) {
self.sync.restart(&mut NetSyncIo::new(io, self.chain.write().unwrap().deref_mut()));
io.register_timer(1000).unwrap();
} }
fn read(&mut self, io: &mut NetworkContext<SyncMessage>, peer: &PeerId, packet_id: u8, data: &[u8]) { fn read(&self, io: &NetworkContext<SyncMessage>, peer: &PeerId, packet_id: u8, data: &[u8]) {
self.sync.on_packet(&mut NetSyncIo::new(io, self.chain.write().unwrap().deref_mut()) , *peer, packet_id, data); self.sync.write().unwrap().on_packet(&mut NetSyncIo::new(io, self.chain.deref()) , *peer, packet_id, data);
} }
fn connected(&mut self, io: &mut NetworkContext<SyncMessage>, peer: &PeerId) { fn connected(&self, io: &NetworkContext<SyncMessage>, peer: &PeerId) {
self.sync.on_peer_connected(&mut NetSyncIo::new(io, self.chain.write().unwrap().deref_mut()), *peer); self.sync.write().unwrap().on_peer_connected(&mut NetSyncIo::new(io, self.chain.deref()), *peer);
} }
fn disconnected(&mut self, io: &mut NetworkContext<SyncMessage>, peer: &PeerId) { fn disconnected(&self, io: &NetworkContext<SyncMessage>, peer: &PeerId) {
self.sync.on_peer_aborting(&mut NetSyncIo::new(io, self.chain.write().unwrap().deref_mut()), *peer); self.sync.write().unwrap().on_peer_aborting(&mut NetSyncIo::new(io, self.chain.deref()), *peer);
}
fn timeout(&mut self, io: &mut NetworkContext<SyncMessage>, _timer: TimerToken) {
self.sync.maintain_sync(&mut NetSyncIo::new(io, self.chain.write().unwrap().deref_mut()));
}
fn message(&mut self, _io: &mut NetworkContext<SyncMessage>, _message: &SyncMessage) {
} }
} }

View File

@ -29,7 +29,7 @@ pub trait RangeCollection<K, V> {
/// Remove all elements >= `tail` /// Remove all elements >= `tail`
fn insert_item(&mut self, key: K, value: V); fn insert_item(&mut self, key: K, value: V);
/// Get an iterator over ranges /// Get an iterator over ranges
fn range_iter<'c>(&'c self) -> RangeIterator<'c, K, V>; fn range_iter(& self) -> RangeIterator<K, V>;
} }
/// Range iterator. For each range yelds a key for the first element of the range and a vector of values. /// Range iterator. For each range yelds a key for the first element of the range and a vector of values.
@ -60,7 +60,7 @@ impl<'c, K:'c, V:'c> Iterator for RangeIterator<'c, K, V> where K: Add<Output =
} }
impl<K, V> RangeCollection<K, V> for Vec<(K, Vec<V>)> where K: Ord + PartialEq + Add<Output = K> + Sub<Output = K> + Copy + FromUsize + ToUsize { impl<K, V> RangeCollection<K, V> for Vec<(K, Vec<V>)> where K: Ord + PartialEq + Add<Output = K> + Sub<Output = K> + Copy + FromUsize + ToUsize {
fn range_iter<'c>(&'c self) -> RangeIterator<'c, K, V> { fn range_iter(&self) -> RangeIterator<K, V> {
RangeIterator { RangeIterator {
range: self.len(), range: self.len(),
collection: self collection: self
@ -191,6 +191,7 @@ impl<K, V> RangeCollection<K, V> for Vec<(K, Vec<V>)> where K: Ord + PartialEq +
} }
#[test] #[test]
#[allow(cyclomatic_complexity)]
fn test_range() { fn test_range() {
use std::cmp::{Ordering}; use std::cmp::{Ordering};

View File

@ -1,38 +1,40 @@
use util::*; use util::*;
use client::{BlockChainClient, BlockStatus, TreeRoute, BlockQueueStatus, BlockChainInfo}; use client::{BlockChainClient, BlockStatus, TreeRoute, BlockChainInfo};
use block_queue::BlockQueueInfo;
use header::{Header as BlockHeader, BlockNumber}; use header::{Header as BlockHeader, BlockNumber};
use error::*; use error::*;
use sync::io::SyncIo; use sync::io::SyncIo;
use sync::chain::ChainSync; use sync::chain::ChainSync;
struct TestBlockChainClient { struct TestBlockChainClient {
blocks: HashMap<H256, Bytes>, blocks: RwLock<HashMap<H256, Bytes>>,
numbers: HashMap<usize, H256>, numbers: RwLock<HashMap<usize, H256>>,
genesis_hash: H256, genesis_hash: H256,
last_hash: H256, last_hash: RwLock<H256>,
difficulty: U256 difficulty: RwLock<U256>,
} }
impl TestBlockChainClient { impl TestBlockChainClient {
fn new() -> TestBlockChainClient { fn new() -> TestBlockChainClient {
let mut client = TestBlockChainClient { let mut client = TestBlockChainClient {
blocks: HashMap::new(), blocks: RwLock::new(HashMap::new()),
numbers: HashMap::new(), numbers: RwLock::new(HashMap::new()),
genesis_hash: H256::new(), genesis_hash: H256::new(),
last_hash: H256::new(), last_hash: RwLock::new(H256::new()),
difficulty: From::from(0), difficulty: RwLock::new(From::from(0)),
}; };
client.add_blocks(1, true); // add genesis block client.add_blocks(1, true); // add genesis block
client.genesis_hash = client.last_hash.clone(); client.genesis_hash = client.last_hash.read().unwrap().clone();
client client
} }
pub fn add_blocks(&mut self, count: usize, empty: bool) { pub fn add_blocks(&mut self, count: usize, empty: bool) {
for n in self.numbers.len()..(self.numbers.len() + count) { let len = self.numbers.read().unwrap().len();
for n in len..(len + count) {
let mut header = BlockHeader::new(); let mut header = BlockHeader::new();
header.difficulty = From::from(n); header.difficulty = From::from(n);
header.parent_hash = self.last_hash.clone(); header.parent_hash = self.last_hash.read().unwrap().clone();
header.number = n as BlockNumber; header.number = n as BlockNumber;
let mut uncles = RlpStream::new_list(if empty {0} else {1}); let mut uncles = RlpStream::new_list(if empty {0} else {1});
if !empty { if !empty {
@ -49,13 +51,17 @@ impl TestBlockChainClient {
} }
impl BlockChainClient for TestBlockChainClient { impl BlockChainClient for TestBlockChainClient {
fn block_total_difficulty(&self, _h: &H256) -> Option<U256> {
unimplemented!();
}
fn block_header(&self, h: &H256) -> Option<Bytes> { fn block_header(&self, h: &H256) -> Option<Bytes> {
self.blocks.get(h).map(|r| Rlp::new(r).at(0).as_raw().to_vec()) self.blocks.read().unwrap().get(h).map(|r| Rlp::new(r).at(0).as_raw().to_vec())
} }
fn block_body(&self, h: &H256) -> Option<Bytes> { fn block_body(&self, h: &H256) -> Option<Bytes> {
self.blocks.get(h).map(|r| { self.blocks.read().unwrap().get(h).map(|r| {
let mut stream = RlpStream::new_list(2); 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(1).as_raw(), 1);
stream.append_raw(Rlp::new(&r).at(2).as_raw(), 1); stream.append_raw(Rlp::new(&r).at(2).as_raw(), 1);
@ -64,30 +70,34 @@ impl BlockChainClient for TestBlockChainClient {
} }
fn block(&self, h: &H256) -> Option<Bytes> { fn block(&self, h: &H256) -> Option<Bytes> {
self.blocks.get(h).map(|b| b.clone()) self.blocks.read().unwrap().get(h).cloned()
} }
fn block_status(&self, h: &H256) -> BlockStatus { fn block_status(&self, h: &H256) -> BlockStatus {
match self.blocks.get(h) { match self.blocks.read().unwrap().get(h) {
Some(_) => BlockStatus::InChain, Some(_) => BlockStatus::InChain,
None => BlockStatus::Unknown None => BlockStatus::Unknown
} }
} }
fn block_total_difficulty_at(&self, _number: BlockNumber) -> Option<U256> {
unimplemented!();
}
fn block_header_at(&self, n: BlockNumber) -> Option<Bytes> { fn block_header_at(&self, n: BlockNumber) -> Option<Bytes> {
self.numbers.get(&(n as usize)).and_then(|h| self.block_header(h)) self.numbers.read().unwrap().get(&(n as usize)).and_then(|h| self.block_header(h))
} }
fn block_body_at(&self, n: BlockNumber) -> Option<Bytes> { fn block_body_at(&self, n: BlockNumber) -> Option<Bytes> {
self.numbers.get(&(n as usize)).and_then(|h| self.block_body(h)) self.numbers.read().unwrap().get(&(n as usize)).and_then(|h| self.block_body(h))
} }
fn block_at(&self, n: BlockNumber) -> Option<Bytes> { fn block_at(&self, n: BlockNumber) -> Option<Bytes> {
self.numbers.get(&(n as usize)).map(|h| self.blocks.get(h).unwrap().clone()) self.numbers.read().unwrap().get(&(n as usize)).map(|h| self.blocks.read().unwrap().get(h).unwrap().clone())
} }
fn block_status_at(&self, n: BlockNumber) -> BlockStatus { fn block_status_at(&self, n: BlockNumber) -> BlockStatus {
if (n as usize) < self.blocks.len() { if (n as usize) < self.blocks.read().unwrap().len() {
BlockStatus::InChain BlockStatus::InChain
} else { } else {
BlockStatus::Unknown BlockStatus::Unknown
@ -110,14 +120,15 @@ impl BlockChainClient for TestBlockChainClient {
None None
} }
fn import_block(&mut self, b: Bytes) -> ImportResult { fn import_block(&self, b: Bytes) -> ImportResult {
let header = Rlp::new(&b).val_at::<BlockHeader>(0); let header = Rlp::new(&b).val_at::<BlockHeader>(0);
let h = header.hash();
let number: usize = header.number as usize; let number: usize = header.number as usize;
if number > self.blocks.len() { if number > self.blocks.read().unwrap().len() {
panic!("Unexpected block number. Expected {}, got {}", self.blocks.len(), number); panic!("Unexpected block number. Expected {}, got {}", self.blocks.read().unwrap().len(), number);
} }
if number > 0 { if number > 0 {
match self.blocks.get(&header.parent_hash) { match self.blocks.read().unwrap().get(&header.parent_hash) {
Some(parent) => { Some(parent) => {
let parent = Rlp::new(parent).val_at::<BlockHeader>(0); let parent = Rlp::new(parent).val_at::<BlockHeader>(0);
if parent.number != (header.number - 1) { if parent.number != (header.number - 1) {
@ -129,43 +140,47 @@ impl BlockChainClient for TestBlockChainClient {
} }
} }
} }
if number == self.numbers.len() { let len = self.numbers.read().unwrap().len();
self.difficulty = self.difficulty + header.difficulty; if number == len {
self.last_hash = header.hash(); *self.difficulty.write().unwrap().deref_mut() += header.difficulty;
self.blocks.insert(header.hash(), b); mem::replace(self.last_hash.write().unwrap().deref_mut(), h.clone());
self.numbers.insert(number, header.hash()); self.blocks.write().unwrap().insert(h.clone(), b);
self.numbers.write().unwrap().insert(number, h.clone());
let mut parent_hash = header.parent_hash; let mut parent_hash = header.parent_hash;
if number > 0 { if number > 0 {
let mut n = number - 1; let mut n = number - 1;
while n > 0 && self.numbers[&n] != parent_hash { while n > 0 && self.numbers.read().unwrap()[&n] != parent_hash {
*self.numbers.get_mut(&n).unwrap() = parent_hash.clone(); *self.numbers.write().unwrap().get_mut(&n).unwrap() = parent_hash.clone();
n -= 1; n -= 1;
parent_hash = Rlp::new(&self.blocks[&parent_hash]).val_at::<BlockHeader>(0).parent_hash; parent_hash = Rlp::new(&self.blocks.read().unwrap()[&parent_hash]).val_at::<BlockHeader>(0).parent_hash;
} }
} }
} }
else { else {
self.blocks.insert(header.hash(), b.to_vec()); self.blocks.write().unwrap().insert(h.clone(), b.to_vec());
} }
Ok(()) Ok(h)
} }
fn queue_status(&self) -> BlockQueueStatus { fn queue_info(&self) -> BlockQueueInfo {
BlockQueueStatus { BlockQueueInfo {
full: false, full: false,
verified_queue_size: 0,
unverified_queue_size: 0,
verifying_queue_size: 0,
} }
} }
fn clear_queue(&mut self) { fn clear_queue(&self) {
} }
fn chain_info(&self) -> BlockChainInfo { fn chain_info(&self) -> BlockChainInfo {
BlockChainInfo { BlockChainInfo {
total_difficulty: self.difficulty, total_difficulty: *self.difficulty.read().unwrap(),
pending_total_difficulty: self.difficulty, pending_total_difficulty: *self.difficulty.read().unwrap(),
genesis_hash: self.genesis_hash.clone(), genesis_hash: self.genesis_hash.clone(),
best_block_hash: self.last_hash.clone(), best_block_hash: self.last_hash.read().unwrap().clone(),
best_block_number: self.blocks.len() as BlockNumber - 1, best_block_number: self.blocks.read().unwrap().len() as BlockNumber - 1,
} }
} }
} }
@ -208,7 +223,7 @@ impl<'p> SyncIo for TestIo<'p> {
Ok(()) Ok(())
} }
fn chain<'a>(&'a mut self) -> &'a mut BlockChainClient { fn chain(&self) -> &BlockChainClient {
self.chain self.chain
} }
} }
@ -265,17 +280,14 @@ impl TestNet {
pub fn sync_step(&mut self) { pub fn sync_step(&mut self) {
for peer in 0..self.peers.len() { for peer in 0..self.peers.len() {
match self.peers[peer].queue.pop_front() { if let Some(packet) = self.peers[peer].queue.pop_front() {
Some(packet) => {
let mut p = self.peers.get_mut(packet.recipient).unwrap(); let mut p = self.peers.get_mut(packet.recipient).unwrap();
trace!("--- {} -> {} ---", peer, packet.recipient); trace!("--- {} -> {} ---", peer, packet.recipient);
p.sync.on_packet(&mut TestIo::new(&mut p.chain, &mut p.queue, Some(peer as PeerId)), peer as PeerId, packet.packet_id, &packet.data); p.sync.on_packet(&mut TestIo::new(&mut p.chain, &mut p.queue, Some(peer as PeerId)), peer as PeerId, packet.packet_id, &packet.data);
trace!("----------------"); trace!("----------------");
},
None => {}
} }
let mut p = self.peers.get_mut(peer).unwrap(); let mut p = self.peers.get_mut(peer).unwrap();
p.sync.maintain_sync(&mut TestIo::new(&mut p.chain, &mut p.queue, None)); p.sync._maintain_sync(&mut TestIo::new(&mut p.chain, &mut p.queue, None));
} }
} }
@ -300,7 +312,7 @@ fn full_sync_two_peers() {
net.peer_mut(2).chain.add_blocks(1000, false); net.peer_mut(2).chain.add_blocks(1000, false);
net.sync(); net.sync();
assert!(net.peer(0).chain.block_at(1000).is_some()); assert!(net.peer(0).chain.block_at(1000).is_some());
assert_eq!(net.peer(0).chain.blocks, net.peer(1).chain.blocks); assert_eq!(net.peer(0).chain.blocks.read().unwrap().deref(), net.peer(1).chain.blocks.read().unwrap().deref());
} }
#[test] #[test]
@ -313,7 +325,7 @@ fn full_sync_empty_blocks() {
} }
net.sync(); net.sync();
assert!(net.peer(0).chain.block_at(1000).is_some()); assert!(net.peer(0).chain.block_at(1000).is_some());
assert_eq!(net.peer(0).chain.blocks, net.peer(1).chain.blocks); assert_eq!(net.peer(0).chain.blocks.read().unwrap().deref(), net.peer(1).chain.blocks.read().unwrap().deref());
} }
#[test] #[test]
@ -329,9 +341,9 @@ fn forked_sync() {
net.peer_mut(1).chain.add_blocks(100, false); //fork between 1 and 2 net.peer_mut(1).chain.add_blocks(100, false); //fork between 1 and 2
net.peer_mut(2).chain.add_blocks(10, true); net.peer_mut(2).chain.add_blocks(10, true);
// peer 1 has the best chain of 601 blocks // peer 1 has the best chain of 601 blocks
let peer1_chain = net.peer(1).chain.numbers.clone(); let peer1_chain = net.peer(1).chain.numbers.read().unwrap().clone();
net.sync(); net.sync();
assert_eq!(net.peer(0).chain.numbers, peer1_chain); assert_eq!(net.peer(0).chain.numbers.read().unwrap().deref(), &peer1_chain);
assert_eq!(net.peer(1).chain.numbers, peer1_chain); assert_eq!(net.peer(1).chain.numbers.read().unwrap().deref(), &peer1_chain);
assert_eq!(net.peer(2).chain.numbers, peer1_chain); assert_eq!(net.peer(2).chain.numbers.read().unwrap().deref(), &peer1_chain);
} }

82
src/tests/chain.rs Normal file
View File

@ -0,0 +1,82 @@
use super::test_common::*;
use client::{BlockChainClient,Client};
use pod_state::*;
use block::Block;
use ethereum;
use super::helpers::*;
pub enum ChainEra {
Frontier,
Homestead,
}
pub fn json_chain_test(json_data: &[u8], era: ChainEra) -> Vec<String> {
let json = Json::from_str(::std::str::from_utf8(json_data).unwrap()).expect("Json is invalid");
let mut failed = Vec::new();
for (name, test) in json.as_object().unwrap() {
let mut fail = false;
{
let mut fail_unless = |cond: bool| if !cond && !fail {
failed.push(name.clone());
flush(format!("FAIL\n"));
fail = true;
true
} else {false};
flush(format!(" - {}...", name));
let blocks: Vec<(Bytes, bool)> = test["blocks"].as_array().unwrap().iter().map(|e| (xjson!(&e["rlp"]), e.find("blockHeader").is_some())).collect();
let mut spec = match era {
ChainEra::Frontier => ethereum::new_frontier_test(),
ChainEra::Homestead => ethereum::new_homestead_test(),
};
let s = PodState::from_json(test.find("pre").unwrap());
spec.set_genesis_state(s);
spec.overwrite_genesis(test.find("genesisBlockHeader").unwrap());
assert!(spec.is_state_root_valid());
let temp = RandomTempPath::new();
{
let client = Client::new(spec, temp.as_path(), IoChannel::disconnected()).unwrap();
for (b, is_valid) in blocks.into_iter() {
if Block::is_good(&b) {
let _ = client.import_block(b.clone());
}
client.flush_queue();
let imported_ok = client.import_verified_blocks(&IoChannel::disconnected()) > 0;
assert_eq!(imported_ok, is_valid);
}
fail_unless(client.chain_info().best_block_hash == H256::from_json(&test["lastblockhash"]));
}
}
if !fail {
flush(format!("ok\n"));
}
}
println!("!!! {:?} tests from failed.", failed.len());
failed
}
fn do_json_test(json_data: &[u8]) -> Vec<String> {
json_chain_test(json_data, ChainEra::Frontier)
}
declare_test!{BlockchainTests_bcBlockGasLimitTest, "BlockchainTests/bcBlockGasLimitTest"}
declare_test!{BlockchainTests_bcForkBlockTest, "BlockchainTests/bcForkBlockTest"}
declare_test!{BlockchainTests_bcForkStressTest, "BlockchainTests/bcForkStressTest"}
declare_test!{BlockchainTests_bcForkUncle, "BlockchainTests/bcForkUncle"}
declare_test!{BlockchainTests_bcGasPricerTest, "BlockchainTests/bcGasPricerTest"}
declare_test!{BlockchainTests_bcInvalidHeaderTest, "BlockchainTests/bcInvalidHeaderTest"}
declare_test!{BlockchainTests_bcInvalidRLPTest, "BlockchainTests/bcInvalidRLPTest"}
declare_test!{BlockchainTests_bcMultiChainTest, "BlockchainTests/bcMultiChainTest"}
declare_test!{BlockchainTests_bcRPC_API_Test, "BlockchainTests/bcRPC_API_Test"}
declare_test!{BlockchainTests_bcStateTest, "BlockchainTests/bcStateTest"}
declare_test!{BlockchainTests_bcTotalDifficultyTest, "BlockchainTests/bcTotalDifficultyTest"}
declare_test!{BlockchainTests_bcUncleHeaderValiditiy, "BlockchainTests/bcUncleHeaderValiditiy"}
declare_test!{BlockchainTests_bcUncleTest, "BlockchainTests/bcUncleTest"}
declare_test!{BlockchainTests_bcValidBlockTest, "BlockchainTests/bcValidBlockTest"}
declare_test!{BlockchainTests_bcWalletTest, "BlockchainTests/bcWalletTest"}
declare_test!{BlockchainTests_RandomTests_bl10251623GO, "BlockchainTests/RandomTests/bl10251623GO"}
declare_test!{BlockchainTests_RandomTests_bl201507071825GO, "BlockchainTests/RandomTests/bl201507071825GO"}

117
src/tests/client.rs Normal file
View File

@ -0,0 +1,117 @@
use client::{BlockChainClient,Client};
use super::test_common::*;
use super::helpers::*;
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)
}
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)
}
fn get_test_client_with_blocks(blocks: Vec<Bytes>) -> 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());
client
}
#[test]
fn created() {
let dir = RandomTempPath::new();
let client_result = Client::new(get_test_spec(), dir.as_path(), IoChannel::disconnected());
assert!(client_result.is_ok());
}
#[test]
fn imports_from_empty() {
let dir = RandomTempPath::new();
let client = Client::new(get_test_spec(), dir.as_path(), IoChannel::disconnected()).unwrap();
client.import_verified_blocks(&IoChannel::disconnected());
client.flush_queue();
}
#[test]
fn imports_good_block() {
let dir = RandomTempPath::new();
let client = Client::new(get_test_spec(), dir.as_path(), IoChannel::disconnected()).unwrap();
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_at(1).unwrap();
assert!(!block.is_empty());
}
#[test]
fn query_none_block() {
let dir = RandomTempPath::new();
let client = Client::new(get_test_spec(), dir.as_path(), IoChannel::disconnected()).unwrap();
let non_existant = client.block_header_at(188);
assert!(non_existant.is_none());
}
#[test]
fn query_bad_block() {
let client = get_test_client_with_blocks(vec![get_bad_state_dummy_block()]);
let bad_block:Option<Bytes> = client.block_header_at(1);
assert!(bad_block.is_none());
}
#[test]
fn returns_chain_info() {
let dummy_block = get_good_dummy_block();
let client = get_test_client_with_blocks(vec![dummy_block.clone()]);
let block = BlockView::new(&dummy_block);
let info = client.chain_info();
assert_eq!(info.best_block_hash, block.header().hash());
}
#[test]
fn imports_block_sequence() {
let client = generate_dummy_client(6);
let block = client.block_header_at(5).unwrap();
assert!(!block.is_empty());
}
#[test]
fn can_collect_garbage() {
let client = generate_dummy_client(100);
client.tick();
assert!(client.cache_info().blocks < 100 * 1024);
}

View File

@ -101,8 +101,9 @@ impl<'a> Ext for TestExt<'a> {
fn call(&mut self, fn call(&mut self,
gas: &U256, gas: &U256,
_sender_address: &Address,
receive_address: &Address, receive_address: &Address,
value: &U256, value: Option<U256>,
data: &[u8], data: &[u8],
_code_address: &Address, _code_address: &Address,
_output: &mut [u8]) -> MessageCallResult { _output: &mut [u8]) -> MessageCallResult {
@ -110,7 +111,7 @@ impl<'a> Ext for TestExt<'a> {
data: data.to_vec(), data: data.to_vec(),
destination: Some(receive_address.clone()), destination: Some(receive_address.clone()),
gas_limit: *gas, gas_limit: *gas,
value: *value value: value.unwrap()
}); });
MessageCallResult::Success(*gas) MessageCallResult::Success(*gas)
} }
@ -168,7 +169,7 @@ fn do_json_test_for(vm: &VMType, json_data: &[u8]) -> Vec<String> {
let mut fail = false; let mut fail = false;
//let mut fail_unless = |cond: bool| if !cond && !fail { failed.push(name.to_string()); fail = true }; //let mut fail_unless = |cond: bool| if !cond && !fail { failed.push(name.to_string()); fail = true };
let mut fail_unless = |cond: bool, s: &str | if !cond && !fail { let mut fail_unless = |cond: bool, s: &str | if !cond && !fail {
failed.push(format!("[{}] {}: {}", vm, name.to_string(), s)); failed.push(format!("[{}] {}: {}", vm, name, s));
fail = true fail = true
}; };
@ -187,20 +188,14 @@ fn do_json_test_for(vm: &VMType, json_data: &[u8]) -> Vec<String> {
BTreeMap::from_json(&s["storage"]).into_iter().foreach(|(k, v)| state.set_storage(&address, k, v)); BTreeMap::from_json(&s["storage"]).into_iter().foreach(|(k, v)| state.set_storage(&address, k, v));
}); });
let mut info = EnvInfo::new(); let info = test.find("env").map(|env| {
EnvInfo::from_json(env)
test.find("env").map(|env| { }).unwrap_or_default();
info.author = xjson!(&env["currentCoinbase"]);
info.difficulty = xjson!(&env["currentDifficulty"]);
info.gas_limit = xjson!(&env["currentGasLimit"]);
info.number = xjson!(&env["currentNumber"]);
info.timestamp = xjson!(&env["currentTimestamp"]);
});
let engine = TestEngine::new(1, vm.clone()); let engine = TestEngine::new(1, vm.clone());
// params // params
let mut params = ActionParams::new(); let mut params = ActionParams::default();
test.find("exec").map(|exec| { test.find("exec").map(|exec| {
params.address = xjson!(&exec["address"]); params.address = xjson!(&exec["address"]);
params.sender = xjson!(&exec["caller"]); params.sender = xjson!(&exec["caller"]);
@ -209,7 +204,7 @@ fn do_json_test_for(vm: &VMType, json_data: &[u8]) -> Vec<String> {
params.data = xjson!(&exec["data"]); params.data = xjson!(&exec["data"]);
params.gas = xjson!(&exec["gas"]); params.gas = xjson!(&exec["gas"]);
params.gas_price = xjson!(&exec["gasPrice"]); params.gas_price = xjson!(&exec["gasPrice"]);
params.value = xjson!(&exec["value"]); params.value = ActionValue::Transfer(xjson!(&exec["value"]));
}); });
let out_of_gas = test.find("callcreates").map(|_calls| { let out_of_gas = test.find("callcreates").map(|_calls| {
@ -245,7 +240,7 @@ fn do_json_test_for(vm: &VMType, json_data: &[u8]) -> Vec<String> {
test.find("post").map(|pre| for (addr, s) in pre.as_object().unwrap() { test.find("post").map(|pre| for (addr, s) in pre.as_object().unwrap() {
let address = Address::from(addr.as_ref()); let address = Address::from(addr.as_ref());
fail_unless(state.code(&address).unwrap_or(vec![]) == Bytes::from_json(&s["code"]), "code is incorrect"); fail_unless(state.code(&address).unwrap_or_else(|| vec![]) == Bytes::from_json(&s["code"]), "code is incorrect");
fail_unless(state.balance(&address) == xjson!(&s["balance"]), "balance is incorrect"); fail_unless(state.balance(&address) == xjson!(&s["balance"]), "balance is incorrect");
fail_unless(state.nonce(&address) == xjson!(&s["nonce"]), "nonce is incorrect"); fail_unless(state.nonce(&address) == xjson!(&s["nonce"]), "nonce is incorrect");
BTreeMap::from_json(&s["storage"]).iter().foreach(|(k, v)| fail_unless(&state.storage_at(&address, &k) == v, "storage is incorrect")); BTreeMap::from_json(&s["storage"]).iter().foreach(|(k, v)| fail_unless(&state.storage_at(&address, &k) == v, "storage is incorrect"));
@ -266,7 +261,7 @@ fn do_json_test_for(vm: &VMType, json_data: &[u8]) -> Vec<String> {
} }
for f in failed.iter() { for f in &failed {
println!("FAILED: {:?}", f); println!("FAILED: {:?}", f);
} }
@ -276,12 +271,11 @@ fn do_json_test_for(vm: &VMType, json_data: &[u8]) -> Vec<String> {
declare_test!{ExecutiveTests_vmArithmeticTest, "VMTests/vmArithmeticTest"} declare_test!{ExecutiveTests_vmArithmeticTest, "VMTests/vmArithmeticTest"}
declare_test!{ExecutiveTests_vmBitwiseLogicOperationTest, "VMTests/vmBitwiseLogicOperationTest"} declare_test!{ExecutiveTests_vmBitwiseLogicOperationTest, "VMTests/vmBitwiseLogicOperationTest"}
// this one crashes with some vm internal error. Separately they pass. declare_test!{ExecutiveTests_vmBlockInfoTest, "VMTests/vmBlockInfoTest"}
declare_test_ignore!{ExecutiveTests_vmBlockInfoTest, "VMTests/vmBlockInfoTest"} // TODO [todr] Fails with Signal 11 when using JIT
declare_test!{ExecutiveTests_vmEnvironmentalInfoTest, "VMTests/vmEnvironmentalInfoTest"} declare_test!{ExecutiveTests_vmEnvironmentalInfoTest, "VMTests/vmEnvironmentalInfoTest"}
declare_test!{ExecutiveTests_vmIOandFlowOperationsTest, "VMTests/vmIOandFlowOperationsTest"} declare_test!{ExecutiveTests_vmIOandFlowOperationsTest, "VMTests/vmIOandFlowOperationsTest"}
// this one take way too long. declare_test!{heavy => ExecutiveTests_vmInputLimits, "VMTests/vmInputLimits"}
declare_test_ignore!{ExecutiveTests_vmInputLimits, "VMTests/vmInputLimits"}
declare_test!{ExecutiveTests_vmLogTest, "VMTests/vmLogTest"} declare_test!{ExecutiveTests_vmLogTest, "VMTests/vmLogTest"}
declare_test!{ExecutiveTests_vmPerformanceTest, "VMTests/vmPerformanceTest"} declare_test!{ExecutiveTests_vmPerformanceTest, "VMTests/vmPerformanceTest"}
declare_test!{ExecutiveTests_vmPushDupSwapTest, "VMTests/vmPushDupSwapTest"} declare_test!{ExecutiveTests_vmPushDupSwapTest, "VMTests/vmPushDupSwapTest"}

80
src/tests/helpers.rs Normal file
View File

@ -0,0 +1,80 @@
use client::{BlockChainClient,Client};
use std::env;
use super::test_common::*;
use std::path::PathBuf;
use spec::*;
use std::fs::{remove_dir_all};
pub struct RandomTempPath {
path: PathBuf
}
impl RandomTempPath {
pub fn new() -> RandomTempPath {
let mut dir = env::temp_dir();
dir.push(H32::random().hex());
RandomTempPath {
path: dir.clone()
}
}
pub fn as_path(&self) -> &PathBuf {
&self.path
}
}
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);
}
}
}
pub fn get_test_spec() -> Spec {
Spec::new_test()
}
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()
}
pub fn generate_dummy_client(block_number: usize) -> Arc<Client> {
let dir = RandomTempPath::new();
let client = Client::new(get_test_spec(), dir.as_path(), IoChannel::disconnected()).unwrap();
let test_spec = get_test_spec();
let test_engine = test_spec.to_engine().unwrap();
let state_root = test_engine.spec().genesis_header().state_root;
let mut rolling_hash = test_engine.spec().genesis_header().hash();
let mut rolling_block_number = 1;
let mut rolling_timestamp = 40;
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());
header.timestamp = rolling_timestamp;
header.number = rolling_block_number;
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");
}
}
client.flush_queue();
client.import_verified_blocks(&IoChannel::disconnected());
client
}

View File

@ -0,0 +1,20 @@
use super::test_common::*;
use super::chain::{ChainEra, json_chain_test};
fn do_json_test(json_data: &[u8]) -> Vec<String> {
json_chain_test(json_data, ChainEra::Homestead)
}
declare_test!{BlockchainTests_Homestead_bcBlockGasLimitTest, "BlockchainTests/Homestead/bcBlockGasLimitTest"}
declare_test!{BlockchainTests_Homestead_bcForkStressTest, "BlockchainTests/Homestead/bcForkStressTest"}
declare_test!{BlockchainTests_Homestead_bcGasPricerTest, "BlockchainTests/Homestead/bcGasPricerTest"}
declare_test!{BlockchainTests_Homestead_bcInvalidHeaderTest, "BlockchainTests/Homestead/bcInvalidHeaderTest"}
declare_test!{BlockchainTests_Homestead_bcInvalidRLPTest, "BlockchainTests/Homestead/bcInvalidRLPTest"}
declare_test!{BlockchainTests_Homestead_bcMultiChainTest, "BlockchainTests/Homestead/bcMultiChainTest"}
declare_test!{BlockchainTests_Homestead_bcRPC_API_Test, "BlockchainTests/Homestead/bcRPC_API_Test"}
declare_test!{BlockchainTests_Homestead_bcStateTest, "BlockchainTests/Homestead/bcStateTest"}
declare_test!{BlockchainTests_Homestead_bcTotalDifficultyTest, "BlockchainTests/Homestead/bcTotalDifficultyTest"}
declare_test!{BlockchainTests_Homestead_bcUncleHeaderValiditiy, "BlockchainTests/Homestead/bcUncleHeaderValiditiy"}
declare_test!{BlockchainTests_Homestead_bcUncleTest, "BlockchainTests/Homestead/bcUncleTest"}
declare_test!{BlockchainTests_Homestead_bcValidBlockTest, "BlockchainTests/Homestead/bcValidBlockTest"}
declare_test!{BlockchainTests_Homestead_bcWalletTest, "BlockchainTests/Homestead/bcWalletTest"}

View File

@ -4,3 +4,7 @@ mod test_common;
mod transaction; mod transaction;
mod executive; mod executive;
mod state; mod state;
mod client;
mod chain;
pub mod helpers;
mod homestead_chain;

View File

@ -15,7 +15,7 @@ fn do_json_test(json_data: &[u8]) -> Vec<String> {
let mut fail = false; let mut fail = false;
{ {
let mut fail_unless = |cond: bool| if !cond && !fail { let mut fail_unless = |cond: bool| if !cond && !fail {
failed.push(name.to_string()); failed.push(name.clone());
flush(format!("FAIL\n")); flush(format!("FAIL\n"));
fail = true; fail = true;
true true
@ -73,20 +73,20 @@ fn do_json_test(json_data: &[u8]) -> Vec<String> {
declare_test!{StateTests_stBlockHashTest, "StateTests/stBlockHashTest"} declare_test!{StateTests_stBlockHashTest, "StateTests/stBlockHashTest"}
declare_test!{StateTests_stCallCodes, "StateTests/stCallCodes"} declare_test!{StateTests_stCallCodes, "StateTests/stCallCodes"}
declare_test_ignore!{StateTests_stCallCreateCallCodeTest, "StateTests/stCallCreateCallCodeTest"} //<< Out of stack declare_test!{StateTests_stCallCreateCallCodeTest, "StateTests/stCallCreateCallCodeTest"}
declare_test!{StateTests_stDelegatecallTest, "StateTests/stDelegatecallTest"} //<< FAIL - gas too high declare_test!{StateTests_stDelegatecallTest, "StateTests/stDelegatecallTest"}
declare_test!{StateTests_stExample, "StateTests/stExample"} declare_test!{StateTests_stExample, "StateTests/stExample"}
declare_test!{StateTests_stInitCodeTest, "StateTests/stInitCodeTest"} declare_test!{StateTests_stInitCodeTest, "StateTests/stInitCodeTest"}
declare_test!{StateTests_stLogTests, "StateTests/stLogTests"} declare_test!{StateTests_stLogTests, "StateTests/stLogTests"}
declare_test!{StateTests_stMemoryStressTest, "StateTests/stMemoryStressTest"} declare_test!{heavy => StateTests_stMemoryStressTest, "StateTests/stMemoryStressTest"}
declare_test!{StateTests_stMemoryTest, "StateTests/stMemoryTest"} declare_test!{heavy => StateTests_stMemoryTest, "StateTests/stMemoryTest"}
declare_test!{StateTests_stPreCompiledContracts, "StateTests/stPreCompiledContracts"} declare_test!{StateTests_stPreCompiledContracts, "StateTests/stPreCompiledContracts"}
declare_test_ignore!{StateTests_stQuadraticComplexityTest, "StateTests/stQuadraticComplexityTest"} //<< Too long declare_test!{heavy => StateTests_stQuadraticComplexityTest, "StateTests/stQuadraticComplexityTest"}
declare_test_ignore!{StateTests_stRecursiveCreate, "StateTests/stRecursiveCreate"} //<< Out of stack declare_test!{StateTests_stRecursiveCreate, "StateTests/stRecursiveCreate"}
declare_test!{StateTests_stRefundTest, "StateTests/stRefundTest"} declare_test!{StateTests_stRefundTest, "StateTests/stRefundTest"}
declare_test!{StateTests_stSolidityTest, "StateTests/stSolidityTest"} declare_test!{StateTests_stSolidityTest, "StateTests/stSolidityTest"}
declare_test_ignore!{StateTests_stSpecialTest, "StateTests/stSpecialTest"} //<< Signal 11 declare_test!{StateTests_stSpecialTest, "StateTests/stSpecialTest"}
declare_test_ignore!{StateTests_stSystemOperationsTest, "StateTests/stSystemOperationsTest"} //<< Signal 11 declare_test!{StateTests_stSystemOperationsTest, "StateTests/stSystemOperationsTest"}
declare_test!{StateTests_stTransactionTest, "StateTests/stTransactionTest"} declare_test!{StateTests_stTransactionTest, "StateTests/stTransactionTest"}
declare_test!{StateTests_stTransitionTest, "StateTests/stTransitionTest"} declare_test!{StateTests_stTransitionTest, "StateTests/stTransitionTest"}
declare_test!{StateTests_stWalletTest, "StateTests/stWalletTest"} declare_test!{StateTests_stWalletTest, "StateTests/stWalletTest"}

View File

@ -1,24 +1,34 @@
pub use common::*; pub use common::*;
macro_rules! test {
($name: expr) => {
assert!(do_json_test(include_bytes!(concat!("../../res/ethereum/tests/", $name, ".json"))).is_empty());
}
}
#[macro_export] #[macro_export]
macro_rules! declare_test { macro_rules! declare_test {
($id: ident, $name: expr) => { (ignore => $id: ident, $name: expr) => {
#[test]
#[allow(non_snake_case)]
fn $id() {
assert!(do_json_test(include_bytes!(concat!("../../res/ethereum/tests/", $name, ".json"))).len() == 0);
}
};
}
#[macro_export]
macro_rules! declare_test_ignore {
($id: ident, $name: expr) => {
#[test]
#[ignore] #[ignore]
#[test]
#[allow(non_snake_case)] #[allow(non_snake_case)]
fn $id() { fn $id() {
assert!(do_json_test(include_bytes!(concat!("../../res/ethereum/tests/", $name, ".json"))).len() == 0); test!($name);
} }
}; };
(heavy => $id: ident, $name: expr) => {
#[cfg(feature = "test-heavy")]
#[test]
#[allow(non_snake_case)]
fn $id() {
test!($name);
}
};
($id: ident, $name: expr) => {
#[test]
#[allow(non_snake_case)]
fn $id() {
test!($name);
}
}
} }

View File

@ -9,13 +9,13 @@ fn do_json_test(json_data: &[u8]) -> Vec<String> {
let ot = RefCell::new(Transaction::new()); let ot = RefCell::new(Transaction::new());
for (name, test) in json.as_object().unwrap() { for (name, test) in json.as_object().unwrap() {
let mut fail = false; let mut fail = false;
let mut fail_unless = |cond: bool| if !cond && !fail { failed.push(name.to_string()); println!("Transaction: {:?}", ot.borrow()); fail = true }; let mut fail_unless = |cond: bool| if !cond && !fail { failed.push(name.clone()); println!("Transaction: {:?}", ot.borrow()); fail = true };
let schedule = match test.find("blocknumber") let schedule = match test.find("blocknumber")
.and_then(|j| j.as_string()) .and_then(|j| j.as_string())
.and_then(|s| BlockNumber::from_str(s).ok()) .and_then(|s| BlockNumber::from_str(s).ok())
.unwrap_or(0) { x if x < 900000 => &old_schedule, _ => &new_schedule }; .unwrap_or(0) { x if x < 900000 => &old_schedule, _ => &new_schedule };
let rlp = Bytes::from_json(&test["rlp"]); let rlp = Bytes::from_json(&test["rlp"]);
let res = UntrustedRlp::new(&rlp).as_val().map_err(|e| From::from(e)).and_then(|t: Transaction| t.validate(schedule, schedule.have_delegate_call)); let res = UntrustedRlp::new(&rlp).as_val().map_err(From::from).and_then(|t: Transaction| t.validate(schedule, schedule.have_delegate_call));
fail_unless(test.find("transaction").is_none() == res.is_err()); fail_unless(test.find("transaction").is_none() == res.is_err());
if let (Some(&Json::Object(ref tx)), Some(&Json::String(ref expect_sender))) = (test.find("transaction"), test.find("sender")) { if let (Some(&Json::Object(ref tx)), Some(&Json::String(ref expect_sender))) = (test.find("transaction"), test.find("sender")) {
let t = res.unwrap(); let t = res.unwrap();
@ -30,11 +30,11 @@ fn do_json_test(json_data: &[u8]) -> Vec<String> {
fail_unless(to == &xjson!(&tx["to"])); fail_unless(to == &xjson!(&tx["to"]));
} else { } else {
*ot.borrow_mut() = t.clone(); *ot.borrow_mut() = t.clone();
fail_unless(Bytes::from_json(&tx["to"]).len() == 0); fail_unless(Bytes::from_json(&tx["to"]).is_empty());
} }
} }
} }
for f in failed.iter() { for f in &failed {
println!("FAILED: {:?}", f); println!("FAILED: {:?}", f);
} }
failed failed
@ -65,14 +65,14 @@ declare_test!{TransactionTests/ttTransactionTest}
declare_test!{TransactionTests/tt10mbDataField} declare_test!{TransactionTests/tt10mbDataField}
declare_test!{TransactionTests/ttWrongRLPTransaction} declare_test!{TransactionTests/ttWrongRLPTransaction}
declare_test!{TransactionTests/Homestead/ttTransactionTest} declare_test!{TransactionTests/Homestead/ttTransactionTest}
declare_test!{TransactionTests/Homestead/tt10mbDataField} declare_test!{heavy => TransactionTests/Homestead/tt10mbDataField}
declare_test!{TransactionTests/Homestead/ttWrongRLPTransaction} declare_test!{TransactionTests/Homestead/ttWrongRLPTransaction}
declare_test!{TransactionTests/RandomTests/tr201506052141PYTHON}*/ declare_test!{TransactionTests/RandomTests/tr201506052141PYTHON}*/
declare_test!{TransactionTests_ttTransactionTest, "TransactionTests/ttTransactionTest"} declare_test!{TransactionTests_ttTransactionTest, "TransactionTests/ttTransactionTest"}
declare_test_ignore!{TransactionTests_tt10mbDataField, "TransactionTests/tt10mbDataField"} declare_test!{heavy => TransactionTests_tt10mbDataField, "TransactionTests/tt10mbDataField"}
declare_test!{TransactionTests_ttWrongRLPTransaction, "TransactionTests/ttWrongRLPTransaction"} declare_test!{TransactionTests_ttWrongRLPTransaction, "TransactionTests/ttWrongRLPTransaction"}
declare_test!{TransactionTests_Homestead_ttTransactionTest, "TransactionTests/Homestead/ttTransactionTest"} declare_test!{TransactionTests_Homestead_ttTransactionTest, "TransactionTests/Homestead/ttTransactionTest"}
declare_test_ignore!{TransactionTests_Homestead_tt10mbDataField, "TransactionTests/Homestead/tt10mbDataField"} declare_test!{heavy => TransactionTests_Homestead_tt10mbDataField, "TransactionTests/Homestead/tt10mbDataField"}
declare_test!{TransactionTests_Homestead_ttWrongRLPTransaction, "TransactionTests/Homestead/ttWrongRLPTransaction"} declare_test!{TransactionTests_Homestead_ttWrongRLPTransaction, "TransactionTests/Homestead/ttWrongRLPTransaction"}
declare_test!{TransactionTests_RandomTests_tr201506052141PYTHON, "TransactionTests/RandomTests/tr201506052141PYTHON"} declare_test!{TransactionTests_RandomTests_tr201506052141PYTHON, "TransactionTests/RandomTests/tr201506052141PYTHON"}

View File

@ -3,7 +3,7 @@ use basic_types::*;
use error::*; use error::*;
use evm::Schedule; use evm::Schedule;
#[derive(Debug,Clone)] #[derive(Debug, Clone)]
/// TODO [Gav Wood] Please document me /// TODO [Gav Wood] Please document me
pub enum Action { pub enum Action {
/// TODO [Gav Wood] Please document me /// TODO [Gav Wood] Please document me
@ -12,9 +12,13 @@ pub enum Action {
Call(Address), Call(Address),
} }
impl Default for Action {
fn default() -> Action { Action::Create }
}
/// A set of information describing an externally-originating message call /// A set of information describing an externally-originating message call
/// or contract creation operation. /// or contract creation operation.
#[derive(Debug,Clone)] #[derive(Default, Debug, Clone)]
pub struct Transaction { pub struct Transaction {
/// TODO [debris] Please document me /// TODO [debris] Please document me
pub nonce: U256, pub nonce: U256,
@ -117,9 +121,8 @@ impl Transaction {
}; };
s.append(&self.value); s.append(&self.value);
s.append(&self.data); s.append(&self.data);
match with_seal { if let Seal::With = with_seal {
Seal::With => { s.append(&(self.v as u16)).append(&self.r).append(&self.s); }, s.append(&(self.v as u16)).append(&self.r).append(&self.s);
_ => {}
} }
} }
@ -138,7 +141,7 @@ impl FromJson for Transaction {
gas_price: xjson!(&json["gasPrice"]), gas_price: xjson!(&json["gasPrice"]),
gas: xjson!(&json["gasLimit"]), gas: xjson!(&json["gasLimit"]),
action: match Bytes::from_json(&json["to"]) { action: match Bytes::from_json(&json["to"]) {
ref x if x.len() == 0 => Action::Create, ref x if x.is_empty() => Action::Create,
ref x => Action::Call(Address::from_slice(x)), ref x => Action::Call(Address::from_slice(x)),
}, },
value: xjson!(&json["value"]), value: xjson!(&json["value"]),

View File

@ -64,7 +64,7 @@ pub fn verify_block_unordered(header: Header, bytes: Bytes, engine: &Engine) ->
/// Phase 3 verification. Check block information against parent and uncles. /// Phase 3 verification. Check block information against parent and uncles.
pub fn verify_block_family<BC>(header: &Header, bytes: &[u8], engine: &Engine, bc: &BC) -> Result<(), Error> where BC: BlockProvider { pub fn verify_block_family<BC>(header: &Header, bytes: &[u8], engine: &Engine, bc: &BC) -> Result<(), Error> where BC: BlockProvider {
// TODO: verify timestamp // TODO: verify timestamp
let parent = try!(bc.block_header(&header.parent_hash).ok_or::<Error>(From::from(BlockError::UnknownParent(header.parent_hash.clone())))); let parent = try!(bc.block_header(&header.parent_hash).ok_or_else(|| Error::from(BlockError::UnknownParent(header.parent_hash.clone()))));
try!(verify_parent(&header, &parent)); try!(verify_parent(&header, &parent));
try!(engine.verify_block_family(&header, &parent, Some(bytes))); try!(engine.verify_block_family(&header, &parent, Some(bytes)));
@ -122,7 +122,7 @@ pub fn verify_block_family<BC>(header: &Header, bytes: &[u8], engine: &Engine, b
// cB.p^7 -------------/ // cB.p^7 -------------/
// cB.p^8 // cB.p^8
let mut expected_uncle_parent = header.parent_hash.clone(); let mut expected_uncle_parent = header.parent_hash.clone();
let uncle_parent = try!(bc.block_header(&uncle.parent_hash).ok_or::<Error>(From::from(BlockError::UnknownUncleParent(uncle.parent_hash.clone())))); let uncle_parent = try!(bc.block_header(&uncle.parent_hash).ok_or_else(|| Error::from(BlockError::UnknownUncleParent(uncle.parent_hash.clone()))));
for _ in 0..depth { for _ in 0..depth {
match bc.block_details(&expected_uncle_parent) { match bc.block_details(&expected_uncle_parent) {
Some(details) => { Some(details) => {
@ -162,7 +162,7 @@ pub fn verify_block_final(expected: &Header, got: &Header) -> Result<(), Error>
/// Check basic header parameters. /// Check basic header parameters.
fn verify_header(header: &Header, engine: &Engine) -> Result<(), Error> { fn verify_header(header: &Header, engine: &Engine) -> Result<(), Error> {
if header.number >= From::from(BlockNumber::max_value()) { if header.number >= From::from(BlockNumber::max_value()) {
return Err(From::from(BlockError::InvalidNumber(OutOfBounds { max: Some(From::from(BlockNumber::max_value())), min: None, found: header.number }))) return Err(From::from(BlockError::RidiculousNumber(OutOfBounds { max: Some(From::from(BlockNumber::max_value())), min: None, found: header.number })))
} }
if header.gas_used > header.gas_limit { if header.gas_used > header.gas_limit {
return Err(From::from(BlockError::TooMuchGasUsed(OutOfBounds { max: Some(header.gas_limit), min: None, found: header.gas_used }))); return Err(From::from(BlockError::TooMuchGasUsed(OutOfBounds { max: Some(header.gas_limit), min: None, found: header.gas_used })));
@ -186,8 +186,8 @@ fn verify_parent(header: &Header, parent: &Header) -> Result<(), Error> {
if header.timestamp <= parent.timestamp { if header.timestamp <= parent.timestamp {
return Err(From::from(BlockError::InvalidTimestamp(OutOfBounds { max: None, min: Some(parent.timestamp + 1), found: header.timestamp }))) return Err(From::from(BlockError::InvalidTimestamp(OutOfBounds { max: None, min: Some(parent.timestamp + 1), found: header.timestamp })))
} }
if header.number <= parent.number { if header.number != parent.number + 1 {
return Err(From::from(BlockError::InvalidNumber(OutOfBounds { max: None, min: Some(parent.number + 1), found: header.number }))); return Err(From::from(BlockError::InvalidNumber(Mismatch { expected: parent.number + 1, found: header.number })));
} }
Ok(()) Ok(())
} }
@ -284,7 +284,7 @@ mod tests {
/// Get raw block data /// Get raw block data
fn block(&self, hash: &H256) -> Option<Bytes> { fn block(&self, hash: &H256) -> Option<Bytes> {
self.blocks.get(hash).map(|b| b.clone()) self.blocks.get(hash).cloned()
} }
/// Get the familial details concerning a block. /// Get the familial details concerning a block.
@ -302,7 +302,7 @@ mod tests {
/// Get the hash of given block's number. /// Get the hash of given block's number.
fn block_hash(&self, index: BlockNumber) -> Option<H256> { fn block_hash(&self, index: BlockNumber) -> Option<H256> {
self.numbers.get(&index).map(|h| h.clone()) self.numbers.get(&index).cloned()
} }
} }
@ -400,7 +400,7 @@ mod tests {
header = good.clone(); header = good.clone();
header.number = BlockNumber::max_value(); header.number = BlockNumber::max_value();
check_fail(basic_test(&create_test_block(&header), engine.deref()), check_fail(basic_test(&create_test_block(&header), engine.deref()),
InvalidNumber(OutOfBounds { max: Some(BlockNumber::max_value()), min: None, found: header.number })); RidiculousNumber(OutOfBounds { max: Some(BlockNumber::max_value()), min: None, found: header.number }));
header = good.clone(); header = good.clone();
header.gas_used = header.gas_limit + From::from(1); header.gas_used = header.gas_limit + From::from(1);
@ -443,7 +443,7 @@ mod tests {
header = good.clone(); header = good.clone();
header.number = 9; header.number = 9;
check_fail(family_test(&create_test_block_with_data(&header, &good_transactions, &good_uncles), engine.deref(), &bc), check_fail(family_test(&create_test_block_with_data(&header, &good_transactions, &good_uncles), engine.deref(), &bc),
InvalidNumber(OutOfBounds { max: None, min: Some(parent.number + 1), found: header.number })); InvalidNumber(Mismatch { expected: parent.number + 1, found: header.number }));
header = good.clone(); header = good.clone();
let mut bad_uncles = good_uncles.clone(); let mut bad_uncles = good_uncles.clone();

View File

@ -141,7 +141,7 @@ impl<'a> BlockView<'a> {
/// Return List of transactions in given block. /// Return List of transactions in given block.
pub fn transaction_views(&self) -> Vec<TransactionView> { pub fn transaction_views(&self) -> Vec<TransactionView> {
self.rlp.at(1).iter().map(|rlp| TransactionView::new_from_rlp(rlp)).collect() self.rlp.at(1).iter().map(TransactionView::new_from_rlp).collect()
} }
/// Return transaction hashes. /// Return transaction hashes.
@ -156,7 +156,7 @@ impl<'a> BlockView<'a> {
/// Return List of transactions in given block. /// Return List of transactions in given block.
pub fn uncle_views(&self) -> Vec<HeaderView> { pub fn uncle_views(&self) -> Vec<HeaderView> {
self.rlp.at(2).iter().map(|rlp| HeaderView::new_from_rlp(rlp)).collect() self.rlp.at(2).iter().map(HeaderView::new_from_rlp).collect()
} }
/// Return list of uncle hashes of given block. /// Return list of uncle hashes of given block.

View File

@ -22,8 +22,11 @@ rust-crypto = "0.2.34"
elastic-array = "0.4" elastic-array = "0.4"
heapsize = "0.2" heapsize = "0.2"
itertools = "0.4" itertools = "0.4"
crossbeam = "0.2"
slab = { git = "https://github.com/arkpar/slab.git" } slab = { git = "https://github.com/arkpar/slab.git" }
sha3 = { path = "sha3" } sha3 = { path = "sha3" }
serde = "0.6.7"
clippy = "0.0.37"
[dev-dependencies] [dev-dependencies]
json-tests = { path = "json-tests" } json-tests = { path = "json-tests" }

View File

@ -106,18 +106,18 @@ impl<'a> Deref for BytesRef<'a> {
type Target = [u8]; type Target = [u8];
fn deref(&self) -> &[u8] { fn deref(&self) -> &[u8] {
match self { match *self {
&BytesRef::Flexible(ref bytes) => bytes, BytesRef::Flexible(ref bytes) => bytes,
&BytesRef::Fixed(ref bytes) => bytes BytesRef::Fixed(ref bytes) => bytes
} }
} }
} }
impl <'a> DerefMut for BytesRef<'a> { impl <'a> DerefMut for BytesRef<'a> {
fn deref_mut(&mut self) -> &mut [u8] { fn deref_mut(&mut self) -> &mut [u8] {
match self { match *self {
&mut BytesRef::Flexible(ref mut bytes) => bytes, BytesRef::Flexible(ref mut bytes) => bytes,
&mut BytesRef::Fixed(ref mut bytes) => bytes BytesRef::Fixed(ref mut bytes) => bytes
} }
} }
} }
@ -273,7 +273,9 @@ pub enum FromBytesError {
/// TODO [debris] Please document me /// TODO [debris] Please document me
DataIsTooShort, DataIsTooShort,
/// TODO [debris] Please document me /// TODO [debris] Please document me
DataIsTooLong DataIsTooLong,
/// Integer-representation is non-canonically prefixed with zero byte(s).
ZeroPrefixedInt,
} }
impl StdError for FromBytesError { impl StdError for FromBytesError {
@ -299,7 +301,7 @@ pub trait FromBytes: Sized {
impl FromBytes for String { impl FromBytes for String {
fn from_bytes(bytes: &[u8]) -> FromBytesResult<String> { fn from_bytes(bytes: &[u8]) -> FromBytesResult<String> {
Ok(::std::str::from_utf8(bytes).unwrap().to_string()) Ok(::std::str::from_utf8(bytes).unwrap().to_owned())
} }
} }
@ -310,6 +312,9 @@ macro_rules! impl_uint_from_bytes {
match bytes.len() { match bytes.len() {
0 => Ok(0), 0 => Ok(0),
l if l <= mem::size_of::<$to>() => { l if l <= mem::size_of::<$to>() => {
if bytes[0] == 0 {
return Err(FromBytesError::ZeroPrefixedInt)
}
let mut res = 0 as $to; let mut res = 0 as $to;
for i in 0..l { for i in 0..l {
let shift = (l - 1 - i) * 8; let shift = (l - 1 - i) * 8;
@ -344,7 +349,9 @@ macro_rules! impl_uint_from_bytes {
($name: ident) => { ($name: ident) => {
impl FromBytes for $name { impl FromBytes for $name {
fn from_bytes(bytes: &[u8]) -> FromBytesResult<$name> { fn from_bytes(bytes: &[u8]) -> FromBytesResult<$name> {
if bytes.len() <= $name::SIZE { if !bytes.is_empty() && bytes[0] == 0 {
Err(FromBytesError::ZeroPrefixedInt)
} else if bytes.len() <= $name::SIZE {
Ok($name::from(bytes)) Ok($name::from(bytes))
} else { } else {
Err(FromBytesError::DataIsTooLong) Err(FromBytesError::DataIsTooLong)

View File

@ -323,10 +323,9 @@ impl<'a, D> ChainFilter<'a, D> where D: FilterDataSource
let offset = level_size * index; let offset = level_size * index;
// go doooown! // go doooown!
match self.blocks(bloom, from_block, to_block, max_level, offset) { if let Some(blocks) = self.blocks(bloom, from_block, to_block, max_level, offset) {
Some(blocks) => result.extend(blocks), result.extend(blocks);
None => () }
};
} }
result result

View File

@ -8,6 +8,8 @@ use rand::os::OsRng;
use bytes::{BytesConvertable,Populatable}; use bytes::{BytesConvertable,Populatable};
use from_json::*; use from_json::*;
use uint::{Uint, U256}; use uint::{Uint, U256};
use rustc_serialize::hex::ToHex;
use serde;
/// Trait for a fixed-size byte array to be used as the output of hash functions. /// Trait for a fixed-size byte array to be used as the output of hash functions.
/// ///
@ -41,6 +43,8 @@ pub trait FixedHash: Sized + BytesConvertable + Populatable + FromStr + Default
fn contains<'a>(&'a self, b: &'a Self) -> bool; fn contains<'a>(&'a self, b: &'a Self) -> bool;
/// TODO [debris] Please document me /// TODO [debris] Please document me
fn is_zero(&self) -> bool; fn is_zero(&self) -> bool;
/// Return the lowest 8 bytes interpreted as a BigEndian integer.
fn low_u64(&self) -> u64;
} }
fn clean_0x(s: &str) -> &str { fn clean_0x(s: &str) -> &str {
@ -71,8 +75,8 @@ macro_rules! impl_hash {
&self.0 &self.0
} }
} }
impl DerefMut for $from {
impl DerefMut for $from {
#[inline] #[inline]
fn deref_mut(&mut self) -> &mut [u8] { fn deref_mut(&mut self) -> &mut [u8] {
&mut self.0 &mut self.0
@ -190,6 +194,14 @@ macro_rules! impl_hash {
fn is_zero(&self) -> bool { fn is_zero(&self) -> bool {
self.eq(&Self::new()) self.eq(&Self::new())
} }
fn low_u64(&self) -> u64 {
let mut ret = 0u64;
for i in 0..min($size, 8) {
ret |= (self.0[$size - 1 - i] as u64) << (i * 8);
}
ret
}
} }
impl FromStr for $from { impl FromStr for $from {
@ -205,13 +217,48 @@ macro_rules! impl_hash {
} }
} }
impl serde::Serialize for $from {
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
where S: serde::Serializer {
let mut hex = "0x".to_owned();
hex.push_str(self.to_hex().as_ref());
serializer.visit_str(hex.as_ref())
}
}
impl serde::Deserialize for $from {
fn deserialize<D>(deserializer: &mut D) -> Result<$from, D::Error>
where D: serde::Deserializer {
struct HashVisitor;
impl serde::de::Visitor for HashVisitor {
type Value = $from;
fn visit_str<E>(&mut self, value: &str) -> Result<Self::Value, E> where E: serde::Error {
// 0x + len
if value.len() != 2 + $size * 2 {
return Err(serde::Error::syntax("Invalid length."));
}
value[2..].from_hex().map(|ref v| $from::from_slice(v)).map_err(|_| serde::Error::syntax("Invalid valid hex."))
}
fn visit_string<E>(&mut self, value: String) -> Result<Self::Value, E> where E: serde::Error {
self.visit_str(value.as_ref())
}
}
deserializer.visit(HashVisitor)
}
}
impl FromJson for $from { impl FromJson for $from {
fn from_json(json: &Json) -> Self { fn from_json(json: &Json) -> Self {
match json { match *json {
&Json::String(ref s) => { Json::String(ref s) => {
match s.len() % 2 { match s.len() % 2 {
0 => FromStr::from_str(clean_0x(s)).unwrap(), 0 => FromStr::from_str(clean_0x(s)).unwrap(),
_ => FromStr::from_str(&("0".to_string() + &(clean_0x(s).to_string()))[..]).unwrap() _ => FromStr::from_str(&("0".to_owned() + &(clean_0x(s).to_owned()))[..]).unwrap()
} }
}, },
_ => Default::default(), _ => Default::default(),
@ -221,7 +268,7 @@ macro_rules! impl_hash {
impl fmt::Debug for $from { impl fmt::Debug for $from {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
for i in self.0.iter() { for i in &self.0[..] {
try!(write!(f, "{:02x}", i)); try!(write!(f, "{:02x}", i));
} }
Ok(()) Ok(())
@ -229,11 +276,11 @@ macro_rules! impl_hash {
} }
impl fmt::Display for $from { impl fmt::Display for $from {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
for i in self.0[0..2].iter() { for i in &self.0[0..2] {
try!(write!(f, "{:02x}", i)); try!(write!(f, "{:02x}", i));
} }
try!(write!(f, "")); try!(write!(f, ""));
for i in self.0[$size - 4..$size].iter() { for i in &self.0[$size - 4..$size] {
try!(write!(f, "{:02x}", i)); try!(write!(f, "{:02x}", i));
} }
Ok(()) Ok(())
@ -291,36 +338,36 @@ macro_rules! impl_hash {
impl Index<usize> for $from { impl Index<usize> for $from {
type Output = u8; type Output = u8;
fn index<'a>(&'a self, index: usize) -> &'a u8 { fn index(&self, index: usize) -> &u8 {
&self.0[index] &self.0[index]
} }
} }
impl IndexMut<usize> for $from { impl IndexMut<usize> for $from {
fn index_mut<'a>(&'a mut self, index: usize) -> &'a mut u8 { fn index_mut(&mut self, index: usize) -> &mut u8 {
&mut self.0[index] &mut self.0[index]
} }
} }
impl Index<ops::Range<usize>> for $from { impl Index<ops::Range<usize>> for $from {
type Output = [u8]; type Output = [u8];
fn index<'a>(&'a self, index: ops::Range<usize>) -> &'a [u8] { fn index(&self, index: ops::Range<usize>) -> &[u8] {
&self.0[index] &self.0[index]
} }
} }
impl IndexMut<ops::Range<usize>> for $from { impl IndexMut<ops::Range<usize>> for $from {
fn index_mut<'a>(&'a mut self, index: ops::Range<usize>) -> &'a mut [u8] { fn index_mut(&mut self, index: ops::Range<usize>) -> &mut [u8] {
&mut self.0[index] &mut self.0[index]
} }
} }
impl Index<ops::RangeFull> for $from { impl Index<ops::RangeFull> for $from {
type Output = [u8]; type Output = [u8];
fn index<'a>(&'a self, _index: ops::RangeFull) -> &'a [u8] { fn index(&self, _index: ops::RangeFull) -> &[u8] {
&self.0 &self.0
} }
} }
impl IndexMut<ops::RangeFull> for $from { impl IndexMut<ops::RangeFull> for $from {
fn index_mut<'a>(&'a mut self, _index: ops::RangeFull) -> &'a mut [u8] { fn index_mut(&mut self, _index: ops::RangeFull) -> &mut [u8] {
&mut self.0 &mut self.0
} }
} }
@ -440,9 +487,9 @@ macro_rules! impl_hash {
fn from(s: &'_ str) -> $from { fn from(s: &'_ str) -> $from {
use std::str::FromStr; use std::str::FromStr;
if s.len() % 2 == 1 { if s.len() % 2 == 1 {
$from::from_str(&("0".to_string() + &(clean_0x(s).to_string()))[..]).unwrap_or($from::new()) $from::from_str(&("0".to_owned() + &(clean_0x(s).to_owned()))[..]).unwrap_or_else(|_| $from::new())
} else { } else {
$from::from_str(clean_0x(s)).unwrap_or($from::new()) $from::from_str(clean_0x(s)).unwrap_or_else(|_| $from::new())
} }
} }
} }
@ -469,6 +516,18 @@ impl<'_> From<&'_ U256> for H256 {
} }
} }
impl From<H256> for U256 {
fn from(value: H256) -> U256 {
U256::from(value.bytes())
}
}
impl<'_> From<&'_ H256> for U256 {
fn from(value: &'_ H256) -> U256 {
U256::from(value.bytes())
}
}
impl From<H256> for Address { impl From<H256> for Address {
fn from(value: H256) -> Address { fn from(value: H256) -> Address {
unsafe { unsafe {
@ -562,9 +621,11 @@ pub static ZERO_H256: H256 = H256([0x00; 32]);
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use hash::*; use hash::*;
use uint::*;
use std::str::FromStr; use std::str::FromStr;
#[test] #[test]
#[allow(eq_op)]
fn hash() { fn hash() {
let h = H64([0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef]); let h = H64([0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef]);
assert_eq!(H64::from_str("0123456789abcdef").unwrap(), h); assert_eq!(H64::from_str("0123456789abcdef").unwrap(), h);
@ -634,5 +695,18 @@ mod tests {
// too short. // too short.
assert_eq!(H64::from(0), H64::from("0x34567890abcdef")); assert_eq!(H64::from(0), H64::from("0x34567890abcdef"));
} }
#[test]
fn from_and_to_u256() {
let u: U256 = x!(0x123456789abcdef0u64);
let h = H256::from(u);
assert_eq!(H256::from(u), H256::from("000000000000000000000000000000000000000000000000123456789abcdef0"));
let h_ref = H256::from(&u);
assert_eq!(h, h_ref);
let r_ref: U256 = From::from(&h);
assert_eq!(r_ref, u);
let r: U256 = From::from(h);
assert_eq!(r, u);
}
} }

View File

@ -8,27 +8,28 @@
/// ///
/// struct MyHandler; /// struct MyHandler;
/// ///
/// #[derive(Clone)]
/// struct MyMessage { /// struct MyMessage {
/// data: u32 /// data: u32
/// } /// }
/// ///
/// impl IoHandler<MyMessage> for MyHandler { /// impl IoHandler<MyMessage> for MyHandler {
/// fn initialize(&mut self, io: &mut IoContext<MyMessage>) { /// fn initialize(&self, io: &IoContext<MyMessage>) {
/// io.register_timer(1000).unwrap(); /// io.register_timer(0, 1000).unwrap();
/// } /// }
/// ///
/// fn timeout(&mut self, _io: &mut IoContext<MyMessage>, timer: TimerToken) { /// fn timeout(&self, _io: &IoContext<MyMessage>, timer: TimerToken) {
/// println!("Timeout {}", timer); /// println!("Timeout {}", timer);
/// } /// }
/// ///
/// fn message(&mut self, _io: &mut IoContext<MyMessage>, message: &mut MyMessage) { /// fn message(&self, _io: &IoContext<MyMessage>, message: &MyMessage) {
/// println!("Message {}", message.data); /// println!("Message {}", message.data);
/// } /// }
/// } /// }
/// ///
/// fn main () { /// fn main () {
/// let mut service = IoService::<MyMessage>::start().expect("Error creating network service"); /// let mut service = IoService::<MyMessage>::start().expect("Error creating network service");
/// service.register_handler(Box::new(MyHandler)).unwrap(); /// service.register_handler(Arc::new(MyHandler)).unwrap();
/// ///
/// // Wait for quit condition /// // Wait for quit condition
/// // ... /// // ...
@ -36,6 +37,9 @@
/// } /// }
/// ``` /// ```
mod service; mod service;
mod worker;
use mio::{EventLoop, Token};
#[derive(Debug)] #[derive(Debug)]
/// TODO [arkpar] Please document me /// TODO [arkpar] Please document me
@ -44,7 +48,7 @@ pub enum IoError {
Mio(::std::io::Error), Mio(::std::io::Error),
} }
impl<Message> From<::mio::NotifyError<service::IoMessage<Message>>> for IoError where Message: Send { impl<Message> From<::mio::NotifyError<service::IoMessage<Message>>> for IoError where Message: Send + Clone {
fn from(_err: ::mio::NotifyError<service::IoMessage<Message>>) -> IoError { fn from(_err: ::mio::NotifyError<service::IoMessage<Message>>) -> IoError {
IoError::Mio(::std::io::Error::new(::std::io::ErrorKind::ConnectionAborted, "Network IO notification error")) IoError::Mio(::std::io::Error::new(::std::io::ErrorKind::ConnectionAborted, "Network IO notification error"))
} }
@ -53,54 +57,65 @@ impl<Message> From<::mio::NotifyError<service::IoMessage<Message>>> for IoError
/// Generic IO handler. /// Generic IO handler.
/// All the handler function are called from within IO event loop. /// All the handler function are called from within IO event loop.
/// `Message` type is used as notification data /// `Message` type is used as notification data
pub trait IoHandler<Message>: Send where Message: Send + 'static { pub trait IoHandler<Message>: Send + Sync where Message: Send + Sync + Clone + 'static {
/// Initialize the handler /// Initialize the handler
fn initialize<'s>(&'s mut self, _io: &mut IoContext<'s, Message>) {} fn initialize(&self, _io: &IoContext<Message>) {}
/// Timer function called after a timeout created with `HandlerIo::timeout`. /// Timer function called after a timeout created with `HandlerIo::timeout`.
fn timeout<'s>(&'s mut self, _io: &mut IoContext<'s, Message>, _timer: TimerToken) {} fn timeout(&self, _io: &IoContext<Message>, _timer: TimerToken) {}
/// Called when a broadcasted message is received. The message can only be sent from a different IO handler. /// Called when a broadcasted message is received. The message can only be sent from a different IO handler.
fn message<'s>(&'s mut self, _io: &mut IoContext<'s, Message>, _message: &'s mut Message) {} // TODO: make message immutable and provide internal channel for adding network handler fn message(&self, _io: &IoContext<Message>, _message: &Message) {}
/// Called when an IO stream gets closed /// Called when an IO stream gets closed
fn stream_hup<'s>(&'s mut self, _io: &mut IoContext<'s, Message>, _stream: StreamToken) {} fn stream_hup(&self, _io: &IoContext<Message>, _stream: StreamToken) {}
/// Called when an IO stream can be read from /// Called when an IO stream can be read from
fn stream_readable<'s>(&'s mut self, _io: &mut IoContext<'s, Message>, _stream: StreamToken) {} fn stream_readable(&self, _io: &IoContext<Message>, _stream: StreamToken) {}
/// Called when an IO stream can be written to /// Called when an IO stream can be written to
fn stream_writable<'s>(&'s mut self, _io: &mut IoContext<'s, Message>, _stream: StreamToken) {} fn stream_writable(&self, _io: &IoContext<Message>, _stream: StreamToken) {}
/// Register a new stream with the event loop
fn register_stream(&self, _stream: StreamToken, _reg: Token, _event_loop: &mut EventLoop<IoManager<Message>>) {}
/// Re-register a stream with the event loop
fn update_stream(&self, _stream: StreamToken, _reg: Token, _event_loop: &mut EventLoop<IoManager<Message>>) {}
/// Deregister a stream. Called whenstream is removed from event loop
fn deregister_stream(&self, _stream: StreamToken, _event_loop: &mut EventLoop<IoManager<Message>>) {}
} }
/// TODO [arkpar] Please document me /// TODO [arkpar] Please document me
pub type TimerToken = service::TimerToken; pub use io::service::TimerToken;
/// TODO [arkpar] Please document me /// TODO [arkpar] Please document me
pub type StreamToken = service::StreamToken; pub use io::service::StreamToken;
/// TODO [arkpar] Please document me /// TODO [arkpar] Please document me
pub type IoContext<'s, M> = service::IoContext<'s, M>; pub use io::service::IoContext;
/// TODO [arkpar] Please document me /// TODO [arkpar] Please document me
pub type IoService<M> = service::IoService<M>; pub use io::service::IoService;
/// TODO [arkpar] Please document me /// TODO [arkpar] Please document me
pub type IoChannel<M> = service::IoChannel<M>; pub use io::service::IoChannel;
//pub const USER_TOKEN_START: usize = service::USER_TOKEN; // TODO: ICE in rustc 1.7.0-nightly (49c382779 2016-01-12) /// TODO [arkpar] Please document me
pub use io::service::IoManager;
/// TODO [arkpar] Please document me
pub use io::service::TOKENS_PER_HANDLER;
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use std::sync::Arc;
use io::*; use io::*;
struct MyHandler; struct MyHandler;
#[derive(Clone)]
struct MyMessage { struct MyMessage {
data: u32 data: u32
} }
impl IoHandler<MyMessage> for MyHandler { impl IoHandler<MyMessage> for MyHandler {
fn initialize(&mut self, io: &mut IoContext<MyMessage>) { fn initialize(&self, io: &IoContext<MyMessage>) {
io.register_timer(1000).unwrap(); io.register_timer(0, 1000).unwrap();
} }
fn timeout(&mut self, _io: &mut IoContext<MyMessage>, timer: TimerToken) { fn timeout(&self, _io: &IoContext<MyMessage>, timer: TimerToken) {
println!("Timeout {}", timer); println!("Timeout {}", timer);
} }
fn message(&mut self, _io: &mut IoContext<MyMessage>, message: &mut MyMessage) { fn message(&self, _io: &IoContext<MyMessage>, message: &MyMessage) {
println!("Message {}", message.data); println!("Message {}", message.data);
} }
} }
@ -108,7 +123,7 @@ mod tests {
#[test] #[test]
fn test_service_register_handler () { fn test_service_register_handler () {
let mut service = IoService::<MyMessage>::start().expect("Error creating network service"); let mut service = IoService::<MyMessage>::start().expect("Error creating network service");
service.register_handler(Box::new(MyHandler)).unwrap(); service.register_handler(Arc::new(MyHandler)).unwrap();
} }
} }

View File

@ -1,148 +1,247 @@
use std::sync::*;
use std::thread::{self, JoinHandle}; use std::thread::{self, JoinHandle};
use std::collections::HashMap;
use mio::*; use mio::*;
use mio::util::{Slab};
use hash::*; use hash::*;
use rlp::*; use rlp::*;
use error::*; use error::*;
use io::{IoError, IoHandler}; use io::{IoError, IoHandler};
use arrayvec::*;
use crossbeam::sync::chase_lev;
use io::worker::{Worker, Work, WorkType};
/// Timer ID
pub type TimerToken = usize; pub type TimerToken = usize;
/// Timer ID
pub type StreamToken = usize; pub type StreamToken = usize;
/// IO Hadndler ID
pub type HandlerId = usize;
// Tokens /// Maximum number of tokens a handler can use
const MAX_USER_TIMERS: usize = 32; pub const TOKENS_PER_HANDLER: usize = 16384;
const USER_TIMER: usize = 0;
const LAST_USER_TIMER: usize = USER_TIMER + MAX_USER_TIMERS - 1;
//const USER_TOKEN: usize = LAST_USER_TIMER + 1;
/// Messages used to communicate with the event loop from other threads. /// Messages used to communicate with the event loop from other threads.
pub enum IoMessage<Message> where Message: Send + Sized { #[derive(Clone)]
pub enum IoMessage<Message> where Message: Send + Clone + Sized {
/// Shutdown the event loop /// Shutdown the event loop
Shutdown, Shutdown,
/// Register a new protocol handler. /// Register a new protocol handler.
AddHandler { AddHandler {
handler: Box<IoHandler<Message>+Send>, handler: Arc<IoHandler<Message>+Send>,
},
AddTimer {
handler_id: HandlerId,
token: TimerToken,
delay: u64,
},
RemoveTimer {
handler_id: HandlerId,
token: TimerToken,
},
RegisterStream {
handler_id: HandlerId,
token: StreamToken,
},
DeregisterStream {
handler_id: HandlerId,
token: StreamToken,
},
UpdateStreamRegistration {
handler_id: HandlerId,
token: StreamToken,
}, },
/// Broadcast a message across all protocol handlers. /// Broadcast a message across all protocol handlers.
UserMessage(Message) UserMessage(Message)
} }
/// IO access point. This is passed to all IO handlers and provides an interface to the IO subsystem. /// IO access point. This is passed to all IO handlers and provides an interface to the IO subsystem.
pub struct IoContext<'s, Message> where Message: Send + 'static { pub struct IoContext<Message> where Message: Send + Clone + 'static {
timers: &'s mut Slab<UserTimer>, channel: IoChannel<Message>,
/// Low leve MIO Event loop for custom handler registration. handler: HandlerId,
pub event_loop: &'s mut EventLoop<IoManager<Message>>,
} }
impl<'s, Message> IoContext<'s, Message> where Message: Send + 'static { impl<Message> IoContext<Message> where Message: Send + Clone + 'static {
/// Create a new IO access point. Takes references to all the data that can be updated within the IO handler. /// Create a new IO access point. Takes references to all the data that can be updated within the IO handler.
fn new(event_loop: &'s mut EventLoop<IoManager<Message>>, timers: &'s mut Slab<UserTimer>) -> IoContext<'s, Message> { pub fn new(channel: IoChannel<Message>, handler: HandlerId) -> IoContext<Message> {
IoContext { IoContext {
event_loop: event_loop, handler: handler,
timers: timers, channel: channel,
} }
} }
/// Register a new IO timer. Returns a new timer token. 'IoHandler::timeout' will be called with the token. /// Register a new IO timer. 'IoHandler::timeout' will be called with the token.
pub fn register_timer(&mut self, ms: u64) -> Result<TimerToken, UtilError> { pub fn register_timer(&self, token: TimerToken, ms: u64) -> Result<(), UtilError> {
match self.timers.insert(UserTimer { try!(self.channel.send_io(IoMessage::AddTimer {
token: token,
delay: ms, delay: ms,
}) { handler_id: self.handler,
Ok(token) => { }));
self.event_loop.timeout_ms(token, ms).expect("Error registering user timer"); Ok(())
Ok(token.as_usize())
},
_ => { panic!("Max timers reached") }
} }
/// Delete a timer.
pub fn clear_timer(&self, token: TimerToken) -> Result<(), UtilError> {
try!(self.channel.send_io(IoMessage::RemoveTimer {
token: token,
handler_id: self.handler,
}));
Ok(())
}
/// Register a new IO stream.
pub fn register_stream(&self, token: StreamToken) -> Result<(), UtilError> {
try!(self.channel.send_io(IoMessage::RegisterStream {
token: token,
handler_id: self.handler,
}));
Ok(())
}
/// Deregister an IO stream.
pub fn deregister_stream(&self, token: StreamToken) -> Result<(), UtilError> {
try!(self.channel.send_io(IoMessage::DeregisterStream {
token: token,
handler_id: self.handler,
}));
Ok(())
}
/// Reregister an IO stream.
pub fn update_registration(&self, token: StreamToken) -> Result<(), UtilError> {
try!(self.channel.send_io(IoMessage::UpdateStreamRegistration {
token: token,
handler_id: self.handler,
}));
Ok(())
} }
/// Broadcast a message to other IO clients /// Broadcast a message to other IO clients
pub fn message(&mut self, message: Message) { pub fn message(&self, message: Message) {
match self.event_loop.channel().send(IoMessage::UserMessage(message)) { self.channel.send(message).expect("Error seding message");
Ok(_) => {}
Err(e) => { panic!("Error sending io message {:?}", e); }
} }
/// Get message channel
pub fn channel(&self) -> IoChannel<Message> {
self.channel.clone()
} }
} }
#[derive(Clone)]
struct UserTimer { struct UserTimer {
delay: u64, delay: u64,
timeout: Timeout,
} }
/// Root IO handler. Manages user handlers, messages and IO timers. /// Root IO handler. Manages user handlers, messages and IO timers.
pub struct IoManager<Message> where Message: Send { pub struct IoManager<Message> where Message: Send + Sync {
timers: Slab<UserTimer>, timers: Arc<RwLock<HashMap<HandlerId, UserTimer>>>,
handlers: Vec<Box<IoHandler<Message>>>, handlers: Vec<Arc<IoHandler<Message>>>,
_workers: Vec<Worker>,
worker_channel: chase_lev::Worker<Work<Message>>,
work_ready: Arc<Condvar>,
} }
impl<Message> IoManager<Message> where Message: Send + 'static { impl<Message> IoManager<Message> where Message: Send + Sync + Clone + 'static {
/// Creates a new instance and registers it with the event loop. /// Creates a new instance and registers it with the event loop.
pub fn start(event_loop: &mut EventLoop<IoManager<Message>>) -> Result<(), UtilError> { pub fn start(event_loop: &mut EventLoop<IoManager<Message>>) -> Result<(), UtilError> {
let (worker, stealer) = chase_lev::deque();
let num_workers = 4;
let work_ready_mutex = Arc::new(Mutex::new(()));
let work_ready = Arc::new(Condvar::new());
let workers = (0..num_workers).map(|i|
Worker::new(i, stealer.clone(), IoChannel::new(event_loop.channel()), work_ready.clone(), work_ready_mutex.clone())).collect();
let mut io = IoManager { let mut io = IoManager {
timers: Slab::new_starting_at(Token(USER_TIMER), MAX_USER_TIMERS), timers: Arc::new(RwLock::new(HashMap::new())),
handlers: Vec::new(), handlers: Vec::new(),
worker_channel: worker,
_workers: workers,
work_ready: work_ready,
}; };
try!(event_loop.run(&mut io)); try!(event_loop.run(&mut io));
Ok(()) Ok(())
} }
} }
impl<Message> Handler for IoManager<Message> where Message: Send + 'static { impl<Message> Handler for IoManager<Message> where Message: Send + Clone + Sync + 'static {
type Timeout = Token; type Timeout = Token;
type Message = IoMessage<Message>; type Message = IoMessage<Message>;
fn ready(&mut self, event_loop: &mut EventLoop<Self>, token: Token, events: EventSet) { fn ready(&mut self, _event_loop: &mut EventLoop<Self>, token: Token, events: EventSet) {
let handler_index = token.as_usize() / TOKENS_PER_HANDLER;
let token_id = token.as_usize() % TOKENS_PER_HANDLER;
if handler_index >= self.handlers.len() {
panic!("Unexpected stream token: {}", token.as_usize());
}
let handler = self.handlers[handler_index].clone();
if events.is_hup() { if events.is_hup() {
for h in self.handlers.iter_mut() { self.worker_channel.push(Work { work_type: WorkType::Hup, token: token_id, handler: handler.clone(), handler_id: handler_index });
h.stream_hup(&mut IoContext::new(event_loop, &mut self.timers), token.as_usize()); }
} else {
} if events.is_readable() {
else if events.is_readable() { self.worker_channel.push(Work { work_type: WorkType::Readable, token: token_id, handler: handler.clone(), handler_id: handler_index });
for h in self.handlers.iter_mut() { }
h.stream_readable(&mut IoContext::new(event_loop, &mut self.timers), token.as_usize()); if events.is_writable() {
} self.worker_channel.push(Work { work_type: WorkType::Writable, token: token_id, handler: handler.clone(), handler_id: handler_index });
}
else if events.is_writable() {
for h in self.handlers.iter_mut() {
h.stream_writable(&mut IoContext::new(event_loop, &mut self.timers), token.as_usize());
} }
} }
self.work_ready.notify_all();
} }
fn timeout(&mut self, event_loop: &mut EventLoop<Self>, token: Token) { fn timeout(&mut self, event_loop: &mut EventLoop<Self>, token: Token) {
match token.as_usize() { let handler_index = token.as_usize() / TOKENS_PER_HANDLER;
USER_TIMER ... LAST_USER_TIMER => { let token_id = token.as_usize() % TOKENS_PER_HANDLER;
let delay = { if handler_index >= self.handlers.len() {
let timer = self.timers.get_mut(token).expect("Unknown user timer token"); panic!("Unexpected timer token: {}", token.as_usize());
timer.delay
};
for h in self.handlers.iter_mut() {
h.timeout(&mut IoContext::new(event_loop, &mut self.timers), token.as_usize());
}
event_loop.timeout_ms(token, delay).expect("Error re-registering user timer");
}
_ => { // Just pass the event down. IoHandler is supposed to re-register it if required.
for h in self.handlers.iter_mut() {
h.timeout(&mut IoContext::new(event_loop, &mut self.timers), token.as_usize());
}
} }
if let Some(timer) = self.timers.read().unwrap().get(&token.as_usize()) {
event_loop.timeout_ms(token, timer.delay).expect("Error re-registering user timer");
let handler = self.handlers[handler_index].clone();
self.worker_channel.push(Work { work_type: WorkType::Timeout, token: token_id, handler: handler, handler_id: handler_index });
self.work_ready.notify_all();
} }
} }
fn notify(&mut self, event_loop: &mut EventLoop<Self>, msg: Self::Message) { fn notify(&mut self, event_loop: &mut EventLoop<Self>, msg: Self::Message) {
let mut m = msg; match msg {
match m {
IoMessage::Shutdown => event_loop.shutdown(), IoMessage::Shutdown => event_loop.shutdown(),
IoMessage::AddHandler { IoMessage::AddHandler { handler } => {
handler, let handler_id = {
} => { self.handlers.push(handler.clone());
self.handlers.push(handler); self.handlers.len() - 1
self.handlers.last_mut().unwrap().initialize(&mut IoContext::new(event_loop, &mut self.timers)); };
handler.initialize(&IoContext::new(IoChannel::new(event_loop.channel()), handler_id));
}, },
IoMessage::UserMessage(ref mut data) => { IoMessage::AddTimer { handler_id, token, delay } => {
for h in self.handlers.iter_mut() { let timer_id = token + handler_id * TOKENS_PER_HANDLER;
h.message(&mut IoContext::new(event_loop, &mut self.timers), data); let timeout = event_loop.timeout_ms(Token(timer_id), delay).expect("Error registering user timer");
self.timers.write().unwrap().insert(timer_id, UserTimer { delay: delay, timeout: timeout });
},
IoMessage::RemoveTimer { handler_id, token } => {
let timer_id = token + handler_id * TOKENS_PER_HANDLER;
if let Some(timer) = self.timers.write().unwrap().remove(&timer_id) {
event_loop.clear_timeout(timer.timeout);
} }
},
IoMessage::RegisterStream { handler_id, token } => {
let handler = self.handlers.get(handler_id).expect("Unknown handler id").clone();
handler.register_stream(token, Token(token + handler_id * TOKENS_PER_HANDLER), event_loop);
},
IoMessage::DeregisterStream { handler_id, token } => {
let handler = self.handlers.get(handler_id).expect("Unknown handler id").clone();
handler.deregister_stream(token, event_loop);
},
IoMessage::UpdateStreamRegistration { handler_id, token } => {
let handler = self.handlers.get(handler_id).expect("Unknown handler id").clone();
handler.update_stream(token, Token(token + handler_id * TOKENS_PER_HANDLER), event_loop);
},
IoMessage::UserMessage(data) => {
for n in 0 .. self.handlers.len() {
let handler = self.handlers[n].clone();
self.worker_channel.push(Work { work_type: WorkType::Message(data.clone()), token: 0, handler: handler, handler_id: n });
}
self.work_ready.notify_all();
} }
} }
} }
@ -150,11 +249,19 @@ impl<Message> Handler for IoManager<Message> where Message: Send + 'static {
/// Allows sending messages into the event loop. All the IO handlers will get the message /// Allows sending messages into the event loop. All the IO handlers will get the message
/// in the `message` callback. /// in the `message` callback.
pub struct IoChannel<Message> where Message: Send { pub struct IoChannel<Message> where Message: Send + Clone{
channel: Option<Sender<IoMessage<Message>>> channel: Option<Sender<IoMessage<Message>>>
} }
impl<Message> IoChannel<Message> where Message: Send { impl<Message> Clone for IoChannel<Message> where Message: Send + Clone {
fn clone(&self) -> IoChannel<Message> {
IoChannel {
channel: self.channel.clone()
}
}
}
impl<Message> IoChannel<Message> where Message: Send + Clone {
/// Send a msessage through the channel /// Send a msessage through the channel
pub fn send(&self, message: Message) -> Result<(), IoError> { pub fn send(&self, message: Message) -> Result<(), IoError> {
if let Some(ref channel) = self.channel { if let Some(ref channel) = self.channel {
@ -163,20 +270,31 @@ impl<Message> IoChannel<Message> where Message: Send {
Ok(()) Ok(())
} }
/// Send low level io message
pub fn send_io(&self, message: IoMessage<Message>) -> Result<(), IoError> {
if let Some(ref channel) = self.channel {
try!(channel.send(message))
}
Ok(())
}
/// Create a new channel to connected to event loop. /// Create a new channel to connected to event loop.
pub fn disconnected() -> IoChannel<Message> { pub fn disconnected() -> IoChannel<Message> {
IoChannel { channel: None } IoChannel { channel: None }
} }
fn new(channel: Sender<IoMessage<Message>>) -> IoChannel<Message> {
IoChannel { channel: Some(channel) }
}
} }
/// General IO Service. Starts an event loop and dispatches IO requests. /// General IO Service. Starts an event loop and dispatches IO requests.
/// 'Message' is a notification message type /// 'Message' is a notification message type
pub struct IoService<Message> where Message: Send + 'static { pub struct IoService<Message> where Message: Send + Sync + Clone + 'static {
thread: Option<JoinHandle<()>>, thread: Option<JoinHandle<()>>,
host_channel: Sender<IoMessage<Message>> host_channel: Sender<IoMessage<Message>>,
} }
impl<Message> IoService<Message> where Message: Send + 'static { impl<Message> IoService<Message> where Message: Send + Sync + Clone + 'static {
/// Starts IO event loop /// Starts IO event loop
pub fn start() -> Result<IoService<Message>, UtilError> { pub fn start() -> Result<IoService<Message>, UtilError> {
let mut event_loop = EventLoop::new().unwrap(); let mut event_loop = EventLoop::new().unwrap();
@ -191,7 +309,7 @@ impl<Message> IoService<Message> where Message: Send + 'static {
} }
/// Regiter a IO hadnler with the event loop. /// Regiter a IO hadnler with the event loop.
pub fn register_handler(&mut self, handler: Box<IoHandler<Message>+Send>) -> Result<(), IoError> { pub fn register_handler(&mut self, handler: Arc<IoHandler<Message>+Send>) -> Result<(), IoError> {
try!(self.host_channel.send(IoMessage::AddHandler { try!(self.host_channel.send(IoMessage::AddHandler {
handler: handler, handler: handler,
})); }));
@ -210,10 +328,10 @@ impl<Message> IoService<Message> where Message: Send + 'static {
} }
} }
impl<Message> Drop for IoService<Message> where Message: Send { impl<Message> Drop for IoService<Message> where Message: Send + Sync + Clone {
fn drop(&mut self) { fn drop(&mut self) {
self.host_channel.send(IoMessage::Shutdown).unwrap(); self.host_channel.send(IoMessage::Shutdown).unwrap();
self.thread.take().unwrap().join().unwrap(); self.thread.take().unwrap().join().ok();
} }
} }

99
util/src/io/worker.rs Normal file
View File

@ -0,0 +1,99 @@
use std::sync::*;
use std::mem;
use std::thread::{JoinHandle, self};
use std::sync::atomic::{AtomicBool, Ordering as AtomicOrdering};
use crossbeam::sync::chase_lev;
use io::service::{HandlerId, IoChannel, IoContext};
use io::{IoHandler};
pub enum WorkType<Message> {
Readable,
Writable,
Hup,
Timeout,
Message(Message)
}
pub struct Work<Message> {
pub work_type: WorkType<Message>,
pub token: usize,
pub handler_id: HandlerId,
pub handler: Arc<IoHandler<Message>>,
}
/// An IO worker thread
/// Sorts them ready for blockchain insertion.
pub struct Worker {
thread: Option<JoinHandle<()>>,
wait: Arc<Condvar>,
deleting: Arc<AtomicBool>,
}
impl Worker {
/// Creates a new worker instance.
pub fn new<Message>(index: usize,
stealer: chase_lev::Stealer<Work<Message>>,
channel: IoChannel<Message>,
wait: Arc<Condvar>,
wait_mutex: Arc<Mutex<()>>) -> Worker
where Message: Send + Sync + Clone + 'static {
let deleting = Arc::new(AtomicBool::new(false));
let mut worker = Worker {
thread: None,
wait: wait.clone(),
deleting: deleting.clone(),
};
worker.thread = Some(thread::Builder::new().name(format!("IO Worker #{}", index)).spawn(
move || Worker::work_loop(stealer, channel.clone(), wait, wait_mutex.clone(), deleting))
.expect("Error creating worker thread"));
worker
}
fn work_loop<Message>(stealer: chase_lev::Stealer<Work<Message>>,
channel: IoChannel<Message>, wait: Arc<Condvar>,
wait_mutex: Arc<Mutex<()>>,
deleting: Arc<AtomicBool>)
where Message: Send + Sync + Clone + 'static {
while !deleting.load(AtomicOrdering::Relaxed) {
{
let lock = wait_mutex.lock().unwrap();
let _ = wait.wait(lock).unwrap();
if deleting.load(AtomicOrdering::Relaxed) {
return;
}
}
while let chase_lev::Steal::Data(work) = stealer.steal() {
Worker::do_work(work, channel.clone());
}
}
}
fn do_work<Message>(work: Work<Message>, channel: IoChannel<Message>) where Message: Send + Sync + Clone + 'static {
match work.work_type {
WorkType::Readable => {
work.handler.stream_readable(&IoContext::new(channel, work.handler_id), work.token);
},
WorkType::Writable => {
work.handler.stream_writable(&IoContext::new(channel, work.handler_id), work.token);
}
WorkType::Hup => {
work.handler.stream_hup(&IoContext::new(channel, work.handler_id), work.token);
}
WorkType::Timeout => {
work.handler.timeout(&IoContext::new(channel, work.handler_id), work.token);
}
WorkType::Message(message) => {
work.handler.message(&IoContext::new(channel, work.handler_id), &message);
}
}
}
}
impl Drop for Worker {
fn drop(&mut self) {
self.deleting.store(true, AtomicOrdering::Relaxed);
self.wait.notify_all();
let thread = mem::replace(&mut self.thread, None).unwrap();
thread.join().ok();
}
}

View File

@ -34,6 +34,16 @@ impl JournalDB {
} }
} }
/// Create a new instance given a shared `backing` database.
pub fn new_with_arc(backing: Arc<DB>) -> JournalDB {
JournalDB {
forward: OverlayDB::new_with_arc(backing.clone()),
backing: backing,
inserts: vec![],
removes: vec![],
}
}
/// Create a new instance with an anonymous temporary database. /// Create a new instance with an anonymous temporary database.
pub fn new_temp() -> JournalDB { pub fn new_temp() -> JournalDB {
let mut dir = env::temp_dir(); let mut dir = env::temp_dir();
@ -96,7 +106,7 @@ impl JournalDB {
})) { })) {
let rlp = Rlp::new(&rlp_data); let rlp = Rlp::new(&rlp_data);
let to_remove: Vec<H256> = rlp.val_at(if canon_id == rlp.val_at(0) {2} else {1}); let to_remove: Vec<H256> = rlp.val_at(if canon_id == rlp.val_at(0) {2} else {1});
for i in to_remove.iter() { for i in &to_remove {
self.forward.remove(i); self.forward.remove(i);
} }
try!(self.backing.delete(&last)); try!(self.backing.delete(&last));

View File

@ -11,18 +11,18 @@ pub fn clean(s: &str) -> &str {
fn u256_from_str(s: &str) -> U256 { fn u256_from_str(s: &str) -> U256 {
if s.len() >= 2 && &s[0..2] == "0x" { if s.len() >= 2 && &s[0..2] == "0x" {
U256::from_str(&s[2..]).unwrap_or(U256::from(0)) U256::from_str(&s[2..]).unwrap_or_else(|_| U256::zero())
} else { } else {
U256::from_dec_str(s).unwrap_or(U256::from(0)) U256::from_dec_str(s).unwrap_or_else(|_| U256::zero())
} }
} }
impl FromJson for Bytes { impl FromJson for Bytes {
fn from_json(json: &Json) -> Self { fn from_json(json: &Json) -> Self {
match json { match *json {
&Json::String(ref s) => match s.len() % 2 { Json::String(ref s) => match s.len() % 2 {
0 => FromHex::from_hex(clean(s)).unwrap_or(vec![]), 0 => FromHex::from_hex(clean(s)).unwrap_or_else(|_| vec![]),
_ => FromHex::from_hex(&("0".to_string() + &(clean(s).to_string()))[..]).unwrap_or(vec![]), _ => FromHex::from_hex(&("0".to_owned() + &(clean(s).to_owned()))[..]).unwrap_or_else(|_| vec![]),
}, },
_ => vec![], _ => vec![],
} }
@ -31,8 +31,8 @@ impl FromJson for Bytes {
impl FromJson for BTreeMap<H256, H256> { impl FromJson for BTreeMap<H256, H256> {
fn from_json(json: &Json) -> Self { fn from_json(json: &Json) -> Self {
match json { match *json {
&Json::Object(ref o) => o.iter().map(|(key, value)| (x!(&u256_from_str(key)), x!(&U256::from_json(value)))).collect(), Json::Object(ref o) => o.iter().map(|(key, value)| (x!(&u256_from_str(key)), x!(&U256::from_json(value)))).collect(),
_ => BTreeMap::new(), _ => BTreeMap::new(),
} }
} }
@ -40,8 +40,8 @@ impl FromJson for BTreeMap<H256, H256> {
impl<T> FromJson for Vec<T> where T: FromJson { impl<T> FromJson for Vec<T> where T: FromJson {
fn from_json(json: &Json) -> Self { fn from_json(json: &Json) -> Self {
match json { match *json {
&Json::Array(ref o) => o.iter().map(|x|T::from_json(x)).collect(), Json::Array(ref o) => o.iter().map(|x|T::from_json(x)).collect(),
_ => Vec::new(), _ => Vec::new(),
} }
} }
@ -49,9 +49,9 @@ impl<T> FromJson for Vec<T> where T: FromJson {
impl<T> FromJson for Option<T> where T: FromJson { impl<T> FromJson for Option<T> where T: FromJson {
fn from_json(json: &Json) -> Self { fn from_json(json: &Json) -> Self {
match json { match *json {
&Json::String(ref o) if o.is_empty() => None, Json::String(ref o) if o.is_empty() => None,
&Json::Null => None, Json::Null => None,
_ => Some(FromJson::from_json(json)), _ => Some(FromJson::from_json(json)),
} }
} }

View File

@ -2,6 +2,9 @@
#![feature(op_assign_traits)] #![feature(op_assign_traits)]
#![feature(augmented_assignments)] #![feature(augmented_assignments)]
#![feature(associated_consts)] #![feature(associated_consts)]
#![feature(plugin)]
#![plugin(clippy)]
#![allow(needless_range_loop, match_bool)]
//! Ethcore-util library //! Ethcore-util library
//! //!
//! ### Rust version: //! ### Rust version:
@ -51,6 +54,8 @@ extern crate crypto as rcrypto;
extern crate secp256k1; extern crate secp256k1;
extern crate arrayvec; extern crate arrayvec;
extern crate elastic_array; extern crate elastic_array;
extern crate crossbeam;
extern crate serde;
/// TODO [Gav Wood] Please document me /// TODO [Gav Wood] Please document me
pub mod standard; pub mod standard;

View File

@ -18,13 +18,13 @@ impl<T> Diff<T> where T: Eq {
pub fn new(pre: T, post: T) -> Self { if pre == post { Diff::Same } else { Diff::Changed(pre, post) } } pub fn new(pre: T, post: T) -> Self { if pre == post { Diff::Same } else { Diff::Changed(pre, post) } }
/// Get the before value, if there is one. /// Get the before value, if there is one.
pub fn pre(&self) -> Option<&T> { match self { &Diff::Died(ref x) | &Diff::Changed(ref x, _) => Some(x), _ => None } } pub fn pre(&self) -> Option<&T> { match *self { Diff::Died(ref x) | Diff::Changed(ref x, _) => Some(x), _ => None } }
/// Get the after value, if there is one. /// Get the after value, if there is one.
pub fn post(&self) -> Option<&T> { match self { &Diff::Born(ref x) | &Diff::Changed(_, ref x) => Some(x), _ => None } } pub fn post(&self) -> Option<&T> { match *self { Diff::Born(ref x) | Diff::Changed(_, ref x) => Some(x), _ => None } }
/// Determine whether there was a change or not. /// Determine whether there was a change or not.
pub fn is_same(&self) -> bool { match self { &Diff::Same => true, _ => false }} pub fn is_same(&self) -> bool { match *self { Diff::Same => true, _ => false }}
} }
#[derive(PartialEq,Eq,Clone,Copy)] #[derive(PartialEq,Eq,Clone,Copy)]

View File

@ -1,5 +1,6 @@
use std::sync::Arc;
use std::collections::VecDeque; use std::collections::VecDeque;
use mio::{Handler, Token, EventSet, EventLoop, Timeout, PollOpt, TryRead, TryWrite}; use mio::{Handler, Token, EventSet, EventLoop, PollOpt, TryRead, TryWrite};
use mio::tcp::*; use mio::tcp::*;
use hash::*; use hash::*;
use sha3::*; use sha3::*;
@ -7,8 +8,10 @@ use bytes::*;
use rlp::*; use rlp::*;
use std::io::{self, Cursor, Read}; use std::io::{self, Cursor, Read};
use error::*; use error::*;
use io::{IoContext, StreamToken};
use network::error::NetworkError; use network::error::NetworkError;
use network::handshake::Handshake; use network::handshake::Handshake;
use network::stats::NetworkStats;
use crypto; use crypto;
use rcrypto::blockmodes::*; use rcrypto::blockmodes::*;
use rcrypto::aessafe::*; use rcrypto::aessafe::*;
@ -17,11 +20,12 @@ use rcrypto::buffer::*;
use tiny_keccak::Keccak; use tiny_keccak::Keccak;
const ENCRYPTED_HEADER_LEN: usize = 32; const ENCRYPTED_HEADER_LEN: usize = 32;
const RECIEVE_PAYLOAD_TIMEOUT: u64 = 30000;
/// Low level tcp connection /// Low level tcp connection
pub struct Connection { pub struct Connection {
/// Connection id (token) /// Connection id (token)
pub token: Token, pub token: StreamToken,
/// Network socket /// Network socket
pub socket: TcpStream, pub socket: TcpStream,
/// Receive buffer /// Receive buffer
@ -32,6 +36,8 @@ pub struct Connection {
send_queue: VecDeque<Cursor<Bytes>>, send_queue: VecDeque<Cursor<Bytes>>,
/// Event flags this connection expects /// Event flags this connection expects
interest: EventSet, interest: EventSet,
/// Shared network staistics
stats: Arc<NetworkStats>,
} }
/// Connection write status. /// Connection write status.
@ -45,14 +51,15 @@ pub enum WriteStatus {
impl Connection { impl Connection {
/// Create a new connection with given id and socket. /// Create a new connection with given id and socket.
pub fn new(token: Token, socket: TcpStream) -> Connection { pub fn new(token: StreamToken, socket: TcpStream, stats: Arc<NetworkStats>) -> Connection {
Connection { Connection {
token: token, token: token,
socket: socket, socket: socket,
send_queue: VecDeque::new(), send_queue: VecDeque::new(),
rec_buf: Bytes::new(), rec_buf: Bytes::new(),
rec_size: 0, rec_size: 0,
interest: EventSet::hup(), interest: EventSet::hup() | EventSet::readable(),
stats: stats,
} }
} }
@ -66,7 +73,6 @@ impl Connection {
} }
/// Readable IO handler. Called when there is some data to be read. /// Readable IO handler. Called when there is some data to be read.
//TODO: return a slice
pub fn readable(&mut self) -> io::Result<Option<Bytes>> { pub fn readable(&mut self) -> io::Result<Option<Bytes>> {
if self.rec_size == 0 || self.rec_buf.len() >= self.rec_size { if self.rec_size == 0 || self.rec_buf.len() >= self.rec_size {
warn!(target:"net", "Unexpected connection read"); warn!(target:"net", "Unexpected connection read");
@ -75,9 +81,12 @@ impl Connection {
// resolve "multiple applicable items in scope [E0034]" error // resolve "multiple applicable items in scope [E0034]" error
let sock_ref = <TcpStream as Read>::by_ref(&mut self.socket); let sock_ref = <TcpStream as Read>::by_ref(&mut self.socket);
match sock_ref.take(max as u64).try_read_buf(&mut self.rec_buf) { match sock_ref.take(max as u64).try_read_buf(&mut self.rec_buf) {
Ok(Some(_)) if self.rec_buf.len() == self.rec_size => { Ok(Some(size)) if size != 0 => {
self.stats.inc_recv(size);
if self.rec_size != 0 && self.rec_buf.len() == self.rec_size {
self.rec_size = 0; self.rec_size = 0;
Ok(Some(::std::mem::replace(&mut self.rec_buf, Bytes::new()))) Ok(Some(::std::mem::replace(&mut self.rec_buf, Bytes::new())))
} else { Ok(None) }
}, },
Ok(_) => Ok(None), Ok(_) => Ok(None),
Err(e) => Err(e), Err(e) => Err(e),
@ -86,7 +95,7 @@ impl Connection {
/// Add a packet to send queue. /// Add a packet to send queue.
pub fn send(&mut self, data: Bytes) { pub fn send(&mut self, data: Bytes) {
if data.len() != 0 { if !data.is_empty() {
self.send_queue.push_back(Cursor::new(data)); self.send_queue.push_back(Cursor::new(data));
} }
if !self.interest.is_writable() { if !self.interest.is_writable() {
@ -107,14 +116,17 @@ impl Connection {
return Ok(WriteStatus::Complete) return Ok(WriteStatus::Complete)
} }
match self.socket.try_write_buf(buf) { match self.socket.try_write_buf(buf) {
Ok(_) if (buf.position() as usize) < send_size => { Ok(Some(size)) if (buf.position() as usize) < send_size => {
self.interest.insert(EventSet::writable()); self.interest.insert(EventSet::writable());
self.stats.inc_send(size);
Ok(WriteStatus::Ongoing) Ok(WriteStatus::Ongoing)
}, },
Ok(_) if (buf.position() as usize) == send_size => { Ok(Some(size)) if (buf.position() as usize) == send_size => {
self.stats.inc_send(size);
Ok(WriteStatus::Complete) Ok(WriteStatus::Complete)
}, },
Ok(_) => { panic!("Wrote past buffer");}, Ok(Some(_)) => { panic!("Wrote past buffer");},
Ok(None) => Ok(WriteStatus::Ongoing),
Err(e) => Err(e) Err(e) => Err(e)
} }
}.and_then(|r| { }.and_then(|r| {
@ -132,23 +144,29 @@ impl Connection {
} }
/// Register this connection with the IO event loop. /// Register this connection with the IO event loop.
pub fn register<Host: Handler>(&mut self, event_loop: &mut EventLoop<Host>) -> io::Result<()> { pub fn register_socket<Host: Handler>(&self, reg: Token, event_loop: &mut EventLoop<Host>) -> io::Result<()> {
trace!(target: "net", "connection register; token={:?}", self.token); trace!(target: "net", "connection register; token={:?}", reg);
self.interest.insert(EventSet::readable()); event_loop.register(&self.socket, reg, self.interest, PollOpt::edge() | PollOpt::oneshot()).or_else(|e| {
event_loop.register(&self.socket, self.token, self.interest, PollOpt::edge() | PollOpt::oneshot()).or_else(|e| { debug!("Failed to register {:?}, {:?}", reg, e);
error!("Failed to register {:?}, {:?}", self.token, e); Ok(())
Err(e)
}) })
} }
/// Update connection registration. Should be called at the end of the IO handler. /// Update connection registration. Should be called at the end of the IO handler.
pub fn reregister<Host: Handler>(&mut self, event_loop: &mut EventLoop<Host>) -> io::Result<()> { pub fn update_socket<Host: Handler>(&self, reg: Token, event_loop: &mut EventLoop<Host>) -> io::Result<()> {
trace!(target: "net", "connection reregister; token={:?}", self.token); trace!(target: "net", "connection reregister; token={:?}", reg);
event_loop.reregister( &self.socket, self.token, self.interest, PollOpt::edge() | PollOpt::oneshot()).or_else(|e| { event_loop.reregister( &self.socket, reg, self.interest, PollOpt::edge() | PollOpt::oneshot()).or_else(|e| {
error!("Failed to reregister {:?}, {:?}", self.token, e); debug!("Failed to reregister {:?}, {:?}", reg, e);
Err(e) Ok(())
}) })
} }
/// Delete connection registration. Should be called at the end of the IO handler.
pub fn deregister_socket<Host: Handler>(&self, event_loop: &mut EventLoop<Host>) -> io::Result<()> {
trace!(target: "net", "connection deregister; token={:?}", self.token);
event_loop.deregister(&self.socket).ok(); // ignore errors here
Ok(())
}
} }
/// RLPx packet /// RLPx packet
@ -182,8 +200,6 @@ pub struct EncryptedConnection {
ingress_mac: Keccak, ingress_mac: Keccak,
/// Read state /// Read state
read_state: EncryptedConnectionState, read_state: EncryptedConnectionState,
/// Disconnect timeout
idle_timeout: Option<Timeout>,
/// Protocol id for the last received packet /// Protocol id for the last received packet
protocol_id: u16, protocol_id: u16,
/// Payload expected to be received for the last header. /// Payload expected to be received for the last header.
@ -192,7 +208,7 @@ pub struct EncryptedConnection {
impl EncryptedConnection { impl EncryptedConnection {
/// Create an encrypted connection out of the handshake. Consumes a handshake object. /// Create an encrypted connection out of the handshake. Consumes a handshake object.
pub fn new(handshake: Handshake) -> Result<EncryptedConnection, UtilError> { pub fn new(mut handshake: Handshake) -> Result<EncryptedConnection, UtilError> {
let shared = try!(crypto::ecdh::agree(handshake.ecdhe.secret(), &handshake.remote_public)); let shared = try!(crypto::ecdh::agree(handshake.ecdhe.secret(), &handshake.remote_public));
let mut nonce_material = H512::new(); let mut nonce_material = H512::new();
if handshake.originated { if handshake.originated {
@ -227,6 +243,7 @@ impl EncryptedConnection {
ingress_mac.update(&mac_material); ingress_mac.update(&mac_material);
ingress_mac.update(if handshake.originated { &handshake.ack_cipher } else { &handshake.auth_cipher }); ingress_mac.update(if handshake.originated { &handshake.ack_cipher } else { &handshake.auth_cipher });
handshake.connection.expect(ENCRYPTED_HEADER_LEN);
Ok(EncryptedConnection { Ok(EncryptedConnection {
connection: handshake.connection, connection: handshake.connection,
encoder: encoder, encoder: encoder,
@ -235,7 +252,6 @@ impl EncryptedConnection {
egress_mac: egress_mac, egress_mac: egress_mac,
ingress_mac: ingress_mac, ingress_mac: ingress_mac,
read_state: EncryptedConnectionState::Header, read_state: EncryptedConnectionState::Header,
idle_timeout: None,
protocol_id: 0, protocol_id: 0,
payload_len: 0 payload_len: 0
}) })
@ -337,16 +353,14 @@ impl EncryptedConnection {
} }
/// Readable IO handler. Tracker receive status and returns decoded packet if avaialable. /// Readable IO handler. Tracker receive status and returns decoded packet if avaialable.
pub fn readable<Host:Handler>(&mut self, event_loop: &mut EventLoop<Host>) -> Result<Option<Packet>, UtilError> { pub fn readable<Message>(&mut self, io: &IoContext<Message>) -> Result<Option<Packet>, UtilError> where Message: Send + Clone{
self.idle_timeout.map(|t| event_loop.clear_timeout(t)); io.clear_timer(self.connection.token).unwrap();
match self.read_state { match self.read_state {
EncryptedConnectionState::Header => { EncryptedConnectionState::Header => {
match try!(self.connection.readable()) { if let Some(data) = try!(self.connection.readable()) {
Some(data) => {
try!(self.read_header(&data)); try!(self.read_header(&data));
}, try!(io.register_timer(self.connection.token, RECIEVE_PAYLOAD_TIMEOUT));
None => {} }
};
Ok(None) Ok(None)
}, },
EncryptedConnectionState::Payload => { EncryptedConnectionState::Payload => {
@ -363,24 +377,21 @@ impl EncryptedConnection {
} }
/// Writable IO handler. Processes send queeue. /// Writable IO handler. Processes send queeue.
pub fn writable<Host:Handler>(&mut self, event_loop: &mut EventLoop<Host>) -> Result<(), UtilError> { pub fn writable<Message>(&mut self, io: &IoContext<Message>) -> Result<(), UtilError> where Message: Send + Clone {
self.idle_timeout.map(|t| event_loop.clear_timeout(t)); io.clear_timer(self.connection.token).unwrap();
try!(self.connection.writable()); try!(self.connection.writable());
Ok(()) Ok(())
} }
/// Register this connection with the event handler. /// Update connection registration. This should be called at the end of the event loop.
pub fn register<Host:Handler<Timeout=Token>>(&mut self, event_loop: &mut EventLoop<Host>) -> Result<(), UtilError> { pub fn update_socket<Host:Handler>(&self, reg: Token, event_loop: &mut EventLoop<Host>) -> Result<(), UtilError> {
self.connection.expect(ENCRYPTED_HEADER_LEN); try!(self.connection.update_socket(reg, event_loop));
self.idle_timeout.map(|t| event_loop.clear_timeout(t));
self.idle_timeout = event_loop.timeout_ms(self.connection.token, 1800).ok();
try!(self.connection.reregister(event_loop));
Ok(()) Ok(())
} }
/// Update connection registration. This should be called at the end of the event loop. /// Delete connection registration. This should be called at the end of the event loop.
pub fn reregister<Host:Handler>(&mut self, event_loop: &mut EventLoop<Host>) -> Result<(), UtilError> { pub fn deregister_socket<Host:Handler>(&self, event_loop: &mut EventLoop<Host>) -> Result<(), UtilError> {
try!(self.connection.reregister(event_loop)); try!(self.connection.deregister_socket(event_loop));
Ok(()) Ok(())
} }
} }

View File

@ -62,7 +62,7 @@ impl Discovery {
discovery_round: 0, discovery_round: 0,
discovery_id: NodeId::new(), discovery_id: NodeId::new(),
discovery_nodes: HashSet::new(), discovery_nodes: HashSet::new(),
node_buckets: (0..NODE_BINS).map(|x| NodeBucket::new(x)).collect(), node_buckets: (0..NODE_BINS).map(NodeBucket::new).collect(),
} }
} }
@ -122,7 +122,8 @@ impl Discovery {
ret ret
} }
fn nearest_node_entries<'b>(source: &NodeId, target: &NodeId, buckets: &'b Vec<NodeBucket>) -> Vec<&'b NodeId> #[allow(cyclomatic_complexity)]
fn nearest_node_entries<'b>(source: &NodeId, target: &NodeId, buckets: &'b [NodeBucket]) -> Vec<&'b NodeId>
{ {
// send ALPHA FindNode packets to nodes we know, closest to target // send ALPHA FindNode packets to nodes we know, closest to target
const LAST_BIN: u32 = NODE_BINS - 1; const LAST_BIN: u32 = NODE_BINS - 1;
@ -136,21 +137,21 @@ impl Discovery {
if head > 1 && tail != LAST_BIN { if head > 1 && tail != LAST_BIN {
while head != tail && head < NODE_BINS && count < BUCKET_SIZE while head != tail && head < NODE_BINS && count < BUCKET_SIZE
{ {
for n in buckets[head as usize].nodes.iter() for n in &buckets[head as usize].nodes
{ {
if count < BUCKET_SIZE { if count < BUCKET_SIZE {
count += 1; count += 1;
found.entry(Discovery::distance(target, &n)).or_insert(Vec::new()).push(n); found.entry(Discovery::distance(target, &n)).or_insert_with(Vec::new).push(n);
} }
else { else {
break; break;
} }
} }
if count < BUCKET_SIZE && tail != 0 { if count < BUCKET_SIZE && tail != 0 {
for n in buckets[tail as usize].nodes.iter() { for n in &buckets[tail as usize].nodes {
if count < BUCKET_SIZE { if count < BUCKET_SIZE {
count += 1; count += 1;
found.entry(Discovery::distance(target, &n)).or_insert(Vec::new()).push(n); found.entry(Discovery::distance(target, &n)).or_insert_with(Vec::new).push(n);
} }
else { else {
break; break;
@ -166,10 +167,10 @@ impl Discovery {
} }
else if head < 2 { else if head < 2 {
while head < NODE_BINS && count < BUCKET_SIZE { while head < NODE_BINS && count < BUCKET_SIZE {
for n in buckets[head as usize].nodes.iter() { for n in &buckets[head as usize].nodes {
if count < BUCKET_SIZE { if count < BUCKET_SIZE {
count += 1; count += 1;
found.entry(Discovery::distance(target, &n)).or_insert(Vec::new()).push(n); found.entry(Discovery::distance(target, &n)).or_insert_with(Vec::new).push(n);
} }
else { else {
break; break;
@ -180,10 +181,10 @@ impl Discovery {
} }
else { else {
while tail > 0 && count < BUCKET_SIZE { while tail > 0 && count < BUCKET_SIZE {
for n in buckets[tail as usize].nodes.iter() { for n in &buckets[tail as usize].nodes {
if count < BUCKET_SIZE { if count < BUCKET_SIZE {
count += 1; count += 1;
found.entry(Discovery::distance(target, &n)).or_insert(Vec::new()).push(n); found.entry(Discovery::distance(target, &n)).or_insert_with(Vec::new).push(n);
} }
else { else {
break; break;

View File

@ -19,11 +19,17 @@ pub enum DisconnectReason
} }
#[derive(Debug)] #[derive(Debug)]
/// Network error.
pub enum NetworkError { pub enum NetworkError {
/// Authentication error.
Auth, Auth,
/// Unrecognised protocol.
BadProtocol, BadProtocol,
/// Peer not found.
PeerNotFound, PeerNotFound,
/// Peer is diconnected.
Disconnect(DisconnectReason), Disconnect(DisconnectReason),
/// Socket IO error.
Io(IoError), Io(IoError),
} }

View File

@ -1,3 +1,4 @@
use std::sync::Arc;
use mio::*; use mio::*;
use mio::tcp::*; use mio::tcp::*;
use hash::*; use hash::*;
@ -10,6 +11,8 @@ use network::host::{HostInfo};
use network::node::NodeId; use network::node::NodeId;
use error::*; use error::*;
use network::error::NetworkError; use network::error::NetworkError;
use network::stats::NetworkStats;
use io::{IoContext, StreamToken};
#[derive(PartialEq, Eq, Debug)] #[derive(PartialEq, Eq, Debug)]
enum HandshakeState { enum HandshakeState {
@ -33,8 +36,6 @@ pub struct Handshake {
state: HandshakeState, state: HandshakeState,
/// Outgoing or incoming connection /// Outgoing or incoming connection
pub originated: bool, pub originated: bool,
/// Disconnect timeout
idle_timeout: Option<Timeout>,
/// ECDH ephemeral /// ECDH ephemeral
pub ecdhe: KeyPair, pub ecdhe: KeyPair,
/// Connection nonce /// Connection nonce
@ -51,16 +52,16 @@ pub struct Handshake {
const AUTH_PACKET_SIZE: usize = 307; const AUTH_PACKET_SIZE: usize = 307;
const ACK_PACKET_SIZE: usize = 210; const ACK_PACKET_SIZE: usize = 210;
const HANDSHAKE_TIMEOUT: u64 = 30000;
impl Handshake { impl Handshake {
/// Create a new handshake object /// Create a new handshake object
pub fn new(token: Token, id: &NodeId, socket: TcpStream, nonce: &H256) -> Result<Handshake, UtilError> { pub fn new(token: StreamToken, id: Option<&NodeId>, socket: TcpStream, nonce: &H256, stats: Arc<NetworkStats>) -> Result<Handshake, UtilError> {
Ok(Handshake { Ok(Handshake {
id: id.clone(), id: if let Some(id) = id { id.clone()} else { NodeId::new() },
connection: Connection::new(token, socket), connection: Connection::new(token, socket, stats),
originated: false, originated: false,
state: HandshakeState::New, state: HandshakeState::New,
idle_timeout: None,
ecdhe: try!(KeyPair::create()), ecdhe: try!(KeyPair::create()),
nonce: nonce.clone(), nonce: nonce.clone(),
remote_public: Public::new(), remote_public: Public::new(),
@ -71,8 +72,9 @@ impl Handshake {
} }
/// Start a handhsake /// Start a handhsake
pub fn start(&mut self, host: &HostInfo, originated: bool) -> Result<(), UtilError> { pub fn start<Message>(&mut self, io: &IoContext<Message>, host: &HostInfo, originated: bool) -> Result<(), UtilError> where Message: Send + Clone{
self.originated = originated; self.originated = originated;
io.register_timer(self.connection.token, HANDSHAKE_TIMEOUT).ok();
if originated { if originated {
try!(self.write_auth(host)); try!(self.write_auth(host));
} }
@ -89,79 +91,90 @@ impl Handshake {
} }
/// Readable IO handler. Drives the state change. /// Readable IO handler. Drives the state change.
pub fn readable<Host:Handler>(&mut self, event_loop: &mut EventLoop<Host>, host: &HostInfo) -> Result<(), UtilError> { pub fn readable<Message>(&mut self, io: &IoContext<Message>, host: &HostInfo) -> Result<(), UtilError> where Message: Send + Clone {
self.idle_timeout.map(|t| event_loop.clear_timeout(t)); io.clear_timer(self.connection.token).unwrap();
match self.state { match self.state {
HandshakeState::ReadingAuth => { HandshakeState::ReadingAuth => {
match try!(self.connection.readable()) { if let Some(data) = try!(self.connection.readable()) {
Some(data) => {
try!(self.read_auth(host, &data)); try!(self.read_auth(host, &data));
try!(self.write_ack()); try!(self.write_ack());
},
None => {}
}; };
}, },
HandshakeState::ReadingAck => { HandshakeState::ReadingAck => {
match try!(self.connection.readable()) { if let Some(data) = try!(self.connection.readable()) {
Some(data) => {
try!(self.read_ack(host, &data)); try!(self.read_ack(host, &data));
self.state = HandshakeState::StartSession; self.state = HandshakeState::StartSession;
},
None => {}
}; };
}, },
HandshakeState::StartSession => {},
_ => { panic!("Unexpected state"); } _ => { panic!("Unexpected state"); }
} }
if self.state != HandshakeState::StartSession { if self.state != HandshakeState::StartSession {
try!(self.connection.reregister(event_loop)); try!(io.update_registration(self.connection.token));
} }
Ok(()) Ok(())
} }
/// Writabe IO handler. /// Writabe IO handler.
pub fn writable<Host:Handler>(&mut self, event_loop: &mut EventLoop<Host>, _host: &HostInfo) -> Result<(), UtilError> { pub fn writable<Message>(&mut self, io: &IoContext<Message>, _host: &HostInfo) -> Result<(), UtilError> where Message: Send + Clone {
self.idle_timeout.map(|t| event_loop.clear_timeout(t)); io.clear_timer(self.connection.token).unwrap();
try!(self.connection.writable()); try!(self.connection.writable());
if self.state != HandshakeState::StartSession { if self.state != HandshakeState::StartSession {
try!(self.connection.reregister(event_loop)); io.update_registration(self.connection.token).unwrap();
} }
Ok(()) Ok(())
} }
/// Register the IO handler with the event loop /// Register the socket with the event loop
pub fn register<Host:Handler<Timeout=Token>>(&mut self, event_loop: &mut EventLoop<Host>) -> Result<(), UtilError> { pub fn register_socket<Host:Handler<Timeout=Token>>(&self, reg: Token, event_loop: &mut EventLoop<Host>) -> Result<(), UtilError> {
self.idle_timeout.map(|t| event_loop.clear_timeout(t)); try!(self.connection.register_socket(reg, event_loop));
self.idle_timeout = event_loop.timeout_ms(self.connection.token, 1800).ok(); Ok(())
try!(self.connection.register(event_loop)); }
pub fn update_socket<Host:Handler<Timeout=Token>>(&self, reg: Token, event_loop: &mut EventLoop<Host>) -> Result<(), UtilError> {
try!(self.connection.update_socket(reg, event_loop));
Ok(())
}
/// Delete registration
pub fn deregister_socket<Host:Handler>(&self, event_loop: &mut EventLoop<Host>) -> Result<(), UtilError> {
try!(self.connection.deregister_socket(event_loop));
Ok(()) Ok(())
} }
/// Parse, validate and confirm auth message /// Parse, validate and confirm auth message
fn read_auth(&mut self, host: &HostInfo, data: &[u8]) -> Result<(), UtilError> { fn read_auth(&mut self, host: &HostInfo, data: &[u8]) -> Result<(), UtilError> {
trace!(target:"net", "Received handshake auth to {:?}", self.connection.socket.peer_addr()); trace!(target:"net", "Received handshake auth to {:?}", self.connection.socket.peer_addr());
assert!(data.len() == AUTH_PACKET_SIZE); if data.len() != AUTH_PACKET_SIZE {
debug!(target:"net", "Wrong auth packet size");
return Err(From::from(NetworkError::BadProtocol));
}
self.auth_cipher = data.to_vec(); self.auth_cipher = data.to_vec();
let auth = try!(ecies::decrypt(host.secret(), data)); let auth = try!(ecies::decrypt(host.secret(), data));
let (sig, rest) = auth.split_at(65); let (sig, rest) = auth.split_at(65);
let (hepubk, rest) = rest.split_at(32); let (hepubk, rest) = rest.split_at(32);
let (pubk, rest) = rest.split_at(64); let (pubk, rest) = rest.split_at(64);
let (nonce, _) = rest.split_at(32); let (nonce, _) = rest.split_at(32);
self.remote_public.clone_from_slice(pubk); self.id.clone_from_slice(pubk);
self.remote_nonce.clone_from_slice(nonce); self.remote_nonce.clone_from_slice(nonce);
let shared = try!(ecdh::agree(host.secret(), &self.remote_public)); let shared = try!(ecdh::agree(host.secret(), &self.id));
let signature = Signature::from_slice(sig); let signature = Signature::from_slice(sig);
let spub = try!(ec::recover(&signature, &(&shared ^ &self.remote_nonce))); let spub = try!(ec::recover(&signature, &(&shared ^ &self.remote_nonce)));
self.remote_public = spub.clone();
if &spub.sha3()[..] != hepubk { if &spub.sha3()[..] != hepubk {
trace!(target:"net", "Handshake hash mismath with {:?}", self.connection.socket.peer_addr()); trace!(target:"net", "Handshake hash mismath with {:?}", self.connection.socket.peer_addr());
return Err(From::from(NetworkError::Auth)); return Err(From::from(NetworkError::Auth));
}; };
self.write_ack() Ok(())
} }
/// Parse and validate ack message /// Parse and validate ack message
fn read_ack(&mut self, host: &HostInfo, data: &[u8]) -> Result<(), UtilError> { fn read_ack(&mut self, host: &HostInfo, data: &[u8]) -> Result<(), UtilError> {
trace!(target:"net", "Received handshake auth to {:?}", self.connection.socket.peer_addr()); trace!(target:"net", "Received handshake auth to {:?}", self.connection.socket.peer_addr());
assert!(data.len() == ACK_PACKET_SIZE); if data.len() != ACK_PACKET_SIZE {
debug!(target:"net", "Wrong ack packet size");
return Err(From::from(NetworkError::BadProtocol));
}
self.ack_cipher = data.to_vec(); self.ack_cipher = data.to_vec();
let ack = try!(ecies::decrypt(host.secret(), data)); let ack = try!(ecies::decrypt(host.secret(), data));
self.remote_public.clone_from_slice(&ack[0..64]); self.remote_public.clone_from_slice(&ack[0..64]);

View File

@ -1,8 +1,9 @@
use std::mem;
use std::net::{SocketAddr}; use std::net::{SocketAddr};
use std::collections::{HashMap}; use std::collections::{HashMap};
use std::hash::{Hasher}; use std::hash::{Hasher};
use std::str::{FromStr}; use std::str::{FromStr};
use std::sync::*;
use std::ops::*;
use mio::*; use mio::*;
use mio::tcp::*; use mio::tcp::*;
use mio::udp::*; use mio::udp::*;
@ -16,6 +17,7 @@ use error::*;
use io::*; use io::*;
use network::NetworkProtocolHandler; use network::NetworkProtocolHandler;
use network::node::*; use network::node::*;
use network::stats::NetworkStats;
type Slab<T> = ::slab::Slab<T, usize>; type Slab<T> = ::slab::Slab<T, usize>;
@ -27,24 +29,45 @@ const IDEAL_PEERS: u32 = 10;
const MAINTENANCE_TIMEOUT: u64 = 1000; const MAINTENANCE_TIMEOUT: u64 = 1000;
#[derive(Debug)] #[derive(Debug)]
struct NetworkConfiguration { /// Network service configuration
listen_address: SocketAddr, pub struct NetworkConfiguration {
public_address: SocketAddr, /// IP address to listen for incoming connections
nat_enabled: bool, pub listen_address: SocketAddr,
discovery_enabled: bool, /// IP address to advertise
pin: bool, pub public_address: SocketAddr,
/// Enable NAT configuration
pub nat_enabled: bool,
/// Enable discovery
pub discovery_enabled: bool,
/// Pin to boot nodes only
pub pin: bool,
/// List of initial node addresses
pub boot_nodes: Vec<String>,
/// Use provided node key instead of default
pub use_secret: Option<Secret>,
} }
impl NetworkConfiguration { impl NetworkConfiguration {
fn new() -> NetworkConfiguration { /// Create a new instance of default settings.
pub fn new() -> NetworkConfiguration {
NetworkConfiguration { NetworkConfiguration {
listen_address: SocketAddr::from_str("0.0.0.0:30304").unwrap(), listen_address: SocketAddr::from_str("0.0.0.0:30304").unwrap(),
public_address: SocketAddr::from_str("0.0.0.0:30304").unwrap(), public_address: SocketAddr::from_str("0.0.0.0:30304").unwrap(),
nat_enabled: true, nat_enabled: true,
discovery_enabled: true, discovery_enabled: true,
pin: false, pin: false,
boot_nodes: Vec::new(),
use_secret: None,
} }
} }
/// Create new default configuration with sepcified listen port.
pub fn new_with_port(port: u16) -> NetworkConfiguration {
let mut config = NetworkConfiguration::new();
config.listen_address = SocketAddr::from_str(&format!("0.0.0.0:{}", port)).unwrap();
config.public_address = SocketAddr::from_str(&format!("0.0.0.0:{}", port)).unwrap();
config
}
} }
// Tokens // Tokens
@ -64,19 +87,25 @@ pub type PacketId = u8;
pub type ProtocolId = &'static str; pub type ProtocolId = &'static str;
/// Messages used to communitate with the event loop from other threads. /// Messages used to communitate with the event loop from other threads.
pub enum NetworkIoMessage<Message> where Message: Send { #[derive(Clone)]
pub enum NetworkIoMessage<Message> where Message: Send + Sync + Clone {
/// Register a new protocol handler. /// Register a new protocol handler.
AddHandler { AddHandler {
handler: Option<Box<NetworkProtocolHandler<Message>+Send>>, /// Handler shared instance.
handler: Arc<NetworkProtocolHandler<Message> + Sync>,
/// Protocol Id.
protocol: ProtocolId, protocol: ProtocolId,
/// Supported protocol versions.
versions: Vec<u8>, versions: Vec<u8>,
}, },
/// Send data over the network. /// Register a new protocol timer
Send { AddTimer {
peer: PeerId, /// Protocol Id.
packet_id: PacketId,
protocol: ProtocolId, protocol: ProtocolId,
data: Vec<u8>, /// Timer token.
token: TimerToken,
/// Timer delay in milliseconds.
delay: u64,
}, },
/// User message /// User message
User(Message), User(Message),
@ -104,46 +133,45 @@ impl Encodable for CapabilityInfo {
} }
/// IO access point. This is passed to all IO handlers and provides an interface to the IO subsystem. /// IO access point. This is passed to all IO handlers and provides an interface to the IO subsystem.
pub struct NetworkContext<'s, 'io, Message> where Message: Send + 'static, 'io: 's { pub struct NetworkContext<'s, Message> where Message: Send + Sync + Clone + 'static, 's {
io: &'s mut IoContext<'io, NetworkIoMessage<Message>>, io: &'s IoContext<NetworkIoMessage<Message>>,
protocol: ProtocolId, protocol: ProtocolId,
connections: &'s mut Slab<ConnectionEntry>, connections: Arc<RwLock<Slab<SharedConnectionEntry>>>,
timers: &'s mut HashMap<TimerToken, ProtocolId>,
session: Option<StreamToken>, session: Option<StreamToken>,
} }
impl<'s, 'io, Message> NetworkContext<'s, 'io, Message> where Message: Send + 'static, { impl<'s, Message> NetworkContext<'s, Message> where Message: Send + Sync + Clone + 'static, {
/// Create a new network IO access point. Takes references to all the data that can be updated within the IO handler. /// Create a new network IO access point. Takes references to all the data that can be updated within the IO handler.
fn new(io: &'s mut IoContext<'io, NetworkIoMessage<Message>>, fn new(io: &'s IoContext<NetworkIoMessage<Message>>,
protocol: ProtocolId, protocol: ProtocolId,
session: Option<StreamToken>, connections: &'s mut Slab<ConnectionEntry>, session: Option<StreamToken>, connections: Arc<RwLock<Slab<SharedConnectionEntry>>>) -> NetworkContext<'s, Message> {
timers: &'s mut HashMap<TimerToken, ProtocolId>) -> NetworkContext<'s, 'io, Message> {
NetworkContext { NetworkContext {
io: io, io: io,
protocol: protocol, protocol: protocol,
session: session, session: session,
connections: connections, connections: connections,
timers: timers,
} }
} }
/// Send a packet over the network to another peer. /// Send a packet over the network to another peer.
pub fn send(&mut self, peer: PeerId, packet_id: PacketId, data: Vec<u8>) -> Result<(), UtilError> { pub fn send(&self, peer: PeerId, packet_id: PacketId, data: Vec<u8>) -> Result<(), UtilError> {
match self.connections.get_mut(peer) { if let Some(connection) = self.connections.read().unwrap().get(peer).cloned() {
Some(&mut ConnectionEntry::Session(ref mut s)) => { match *connection.lock().unwrap().deref_mut() {
ConnectionEntry::Session(ref mut s) => {
s.send_packet(self.protocol, packet_id as u8, &data).unwrap_or_else(|e| { s.send_packet(self.protocol, packet_id as u8, &data).unwrap_or_else(|e| {
warn!(target: "net", "Send error: {:?}", e); warn!(target: "net", "Send error: {:?}", e);
}); //TODO: don't copy vector data }); //TODO: don't copy vector data
}, },
_ => { _ => warn!(target: "net", "Send: Peer is not connected yet")
warn!(target: "net", "Send: Peer does not exist");
} }
} else {
warn!(target: "net", "Send: Peer does not exist")
} }
Ok(()) Ok(())
} }
/// Respond to a current network message. Panics if no there is no packet in the context. /// Respond to a current network message. Panics if no there is no packet in the context.
pub fn respond(&mut self, packet_id: PacketId, data: Vec<u8>) -> Result<(), UtilError> { pub fn respond(&self, packet_id: PacketId, data: Vec<u8>) -> Result<(), UtilError> {
match self.session { match self.session {
Some(session) => self.send(session, packet_id, data), Some(session) => self.send(session, packet_id, data),
None => { None => {
@ -153,31 +181,28 @@ impl<'s, 'io, Message> NetworkContext<'s, 'io, Message> where Message: Send + 's
} }
/// Disable current protocol capability for given peer. If no capabilities left peer gets disconnected. /// Disable current protocol capability for given peer. If no capabilities left peer gets disconnected.
pub fn disable_peer(&mut self, _peer: PeerId) { pub fn disable_peer(&self, _peer: PeerId) {
//TODO: remove capability, disconnect if no capabilities left //TODO: remove capability, disconnect if no capabilities left
} }
/// Register a new IO timer. Returns a new timer token. 'NetworkProtocolHandler::timeout' will be called with the token. /// Register a new IO timer. 'IoHandler::timeout' will be called with the token.
pub fn register_timer(&mut self, ms: u64) -> Result<TimerToken, UtilError>{ pub fn register_timer(&self, token: TimerToken, ms: u64) -> Result<(), UtilError> {
match self.io.register_timer(ms) { self.io.message(NetworkIoMessage::AddTimer {
Ok(token) => { token: token,
self.timers.insert(token, self.protocol); delay: ms,
Ok(token) protocol: self.protocol,
}, });
e => e, Ok(())
}
} }
/// Returns peer identification string /// Returns peer identification string
pub fn peer_info(&self, peer: PeerId) -> String { pub fn peer_info(&self, peer: PeerId) -> String {
match self.connections.get(peer) { if let Some(connection) = self.connections.read().unwrap().get(peer).cloned() {
Some(&ConnectionEntry::Session(ref s)) => { if let ConnectionEntry::Session(ref s) = *connection.lock().unwrap().deref() {
s.info.client_version.clone() return s.info.client_version.clone()
},
_ => {
"unknown".to_string()
} }
} }
"unknown".to_owned()
} }
} }
@ -213,7 +238,7 @@ impl HostInfo {
/// Increments and returns connection nonce. /// Increments and returns connection nonce.
pub fn next_nonce(&mut self) -> H256 { pub fn next_nonce(&mut self) -> H256 {
self.nonce = self.nonce.sha3(); self.nonce = self.nonce.sha3();
return self.nonce.clone(); self.nonce.clone()
} }
} }
@ -222,66 +247,103 @@ enum ConnectionEntry {
Session(Session) Session(Session)
} }
/// Root IO handler. Manages protocol handlers, IO timers and network connections. type SharedConnectionEntry = Arc<Mutex<ConnectionEntry>>;
pub struct Host<Message> where Message: Send {
pub info: HostInfo, #[derive(Copy, Clone)]
udp_socket: UdpSocket, struct ProtocolTimer {
listener: TcpListener, pub protocol: ProtocolId,
connections: Slab<ConnectionEntry>, pub token: TimerToken, // Handler level token
timers: HashMap<TimerToken, ProtocolId>,
nodes: HashMap<NodeId, Node>,
handlers: HashMap<ProtocolId, Box<NetworkProtocolHandler<Message>>>,
} }
impl<Message> Host<Message> where Message: Send { /// Root IO handler. Manages protocol handlers, IO timers and network connections.
pub fn new() -> Host<Message> { pub struct Host<Message> where Message: Send + Sync + Clone {
let config = NetworkConfiguration::new(); pub info: RwLock<HostInfo>,
udp_socket: Mutex<UdpSocket>,
tcp_listener: Mutex<TcpListener>,
connections: Arc<RwLock<Slab<SharedConnectionEntry>>>,
nodes: RwLock<HashMap<NodeId, Node>>,
handlers: RwLock<HashMap<ProtocolId, Arc<NetworkProtocolHandler<Message>>>>,
timers: RwLock<HashMap<TimerToken, ProtocolTimer>>,
timer_counter: RwLock<usize>,
stats: Arc<NetworkStats>,
}
impl<Message> Host<Message> where Message: Send + Sync + Clone {
/// Create a new instance
pub fn new(config: NetworkConfiguration) -> Host<Message> {
let addr = config.listen_address; let addr = config.listen_address;
// Setup the server socket // Setup the server socket
let listener = TcpListener::bind(&addr).unwrap(); let tcp_listener = TcpListener::bind(&addr).unwrap();
let udp_socket = UdpSocket::bound(&addr).unwrap(); let udp_socket = UdpSocket::bound(&addr).unwrap();
Host::<Message> { let mut host = Host::<Message> {
info: HostInfo { info: RwLock::new(HostInfo {
keys: KeyPair::create().unwrap(), keys: if let Some(ref secret) = config.use_secret { KeyPair::from_secret(secret.clone()).unwrap() } else { KeyPair::create().unwrap() },
config: config, config: config,
nonce: H256::random(), nonce: H256::random(),
protocol_version: 4, protocol_version: 4,
client_version: "parity".to_string(), client_version: "parity".to_owned(),
listen_port: 0, listen_port: 0,
capabilities: Vec::new(), capabilities: Vec::new(),
}, }),
udp_socket: udp_socket, udp_socket: Mutex::new(udp_socket),
listener: listener, tcp_listener: Mutex::new(tcp_listener),
connections: Slab::new_starting_at(FIRST_CONNECTION, MAX_CONNECTIONS), connections: Arc::new(RwLock::new(Slab::new_starting_at(FIRST_CONNECTION, MAX_CONNECTIONS))),
timers: HashMap::new(), nodes: RwLock::new(HashMap::new()),
nodes: HashMap::new(), handlers: RwLock::new(HashMap::new()),
handlers: HashMap::new(), timers: RwLock::new(HashMap::new()),
timer_counter: RwLock::new(LAST_CONNECTION + 1),
stats: Arc::new(NetworkStats::default()),
};
let port = host.info.read().unwrap().config.listen_address.port();
host.info.write().unwrap().deref_mut().listen_port = port;
/*
match ::ifaces::Interface::get_all().unwrap().into_iter().filter(|x| x.kind == ::ifaces::Kind::Packet && x.addr.is_some()).next() {
Some(iface) => config.public_address = iface.addr.unwrap(),
None => warn!("No public network interface"),
*/
let boot_nodes = host.info.read().unwrap().config.boot_nodes.clone();
for n in boot_nodes {
host.add_node(&n);
} }
host
} }
fn add_node(&mut self, id: &str) { pub fn stats(&self) -> Arc<NetworkStats> {
self.stats.clone()
}
pub fn add_node(&mut self, id: &str) {
match Node::from_str(id) { match Node::from_str(id) {
Err(e) => { warn!("Could not add node: {:?}", e); }, Err(e) => { warn!("Could not add node: {:?}", e); },
Ok(n) => { Ok(n) => {
self.nodes.insert(n.id.clone(), n); self.nodes.write().unwrap().insert(n.id.clone(), n);
} }
} }
} }
fn maintain_network(&mut self, io: &mut IoContext<NetworkIoMessage<Message>>) { pub fn client_version(&self) -> String {
self.info.read().unwrap().client_version.clone()
}
pub fn client_id(&self) -> NodeId {
self.info.read().unwrap().id().clone()
}
fn maintain_network(&self, io: &IoContext<NetworkIoMessage<Message>>) {
self.connect_peers(io); self.connect_peers(io);
io.event_loop.timeout_ms(Token(IDLE), MAINTENANCE_TIMEOUT).unwrap();
} }
fn have_session(&self, id: &NodeId) -> bool { fn have_session(&self, id: &NodeId) -> bool {
self.connections.iter().any(|e| match e { &ConnectionEntry::Session(ref s) => s.info.id.eq(&id), _ => false }) self.connections.read().unwrap().iter().any(|e| match *e.lock().unwrap().deref() { ConnectionEntry::Session(ref s) => s.info.id.eq(&id), _ => false })
} }
fn connecting_to(&self, id: &NodeId) -> bool { fn connecting_to(&self, id: &NodeId) -> bool {
self.connections.iter().any(|e| match e { &ConnectionEntry::Handshake(ref h) => h.id.eq(&id), _ => false }) self.connections.read().unwrap().iter().any(|e| match *e.lock().unwrap().deref() { ConnectionEntry::Handshake(ref h) => h.id.eq(&id), _ => false })
} }
fn connect_peers(&mut self, io: &mut IoContext<NetworkIoMessage<Message>>) { fn connect_peers(&self, io: &IoContext<NetworkIoMessage<Message>>) {
struct NodeInfo { struct NodeInfo {
id: NodeId, id: NodeId,
peer_type: PeerType peer_type: PeerType
@ -292,18 +354,19 @@ impl<Message> Host<Message> where Message: Send {
let mut req_conn = 0; let mut req_conn = 0;
//TODO: use nodes from discovery here //TODO: use nodes from discovery here
//for n in self.node_buckets.iter().flat_map(|n| &n.nodes).map(|id| NodeInfo { id: id.clone(), peer_type: self.nodes.get(id).unwrap().peer_type}) { //for n in self.node_buckets.iter().flat_map(|n| &n.nodes).map(|id| NodeInfo { id: id.clone(), peer_type: self.nodes.get(id).unwrap().peer_type}) {
for n in self.nodes.values().map(|n| NodeInfo { id: n.id.clone(), peer_type: n.peer_type }) { let pin = self.info.read().unwrap().deref().config.pin;
for n in self.nodes.read().unwrap().values().map(|n| NodeInfo { id: n.id.clone(), peer_type: n.peer_type }) {
let connected = self.have_session(&n.id) || self.connecting_to(&n.id); let connected = self.have_session(&n.id) || self.connecting_to(&n.id);
let required = n.peer_type == PeerType::Required; let required = n.peer_type == PeerType::Required;
if connected && required { if connected && required {
req_conn += 1; req_conn += 1;
} }
else if !connected && (!self.info.config.pin || required) { else if !connected && (!pin || required) {
to_connect.push(n); to_connect.push(n);
} }
} }
for n in to_connect.iter() { for n in &to_connect {
if n.peer_type == PeerType::Required { if n.peer_type == PeerType::Required {
if req_conn < IDEAL_PEERS { if req_conn < IDEAL_PEERS {
self.connect_peer(&n.id, io); self.connect_peer(&n.id, io);
@ -312,13 +375,12 @@ impl<Message> Host<Message> where Message: Send {
} }
} }
if !self.info.config.pin if !pin {
{
let pending_count = 0; //TODO: let pending_count = 0; //TODO:
let peer_count = 0; let peer_count = 0;
let mut open_slots = IDEAL_PEERS - peer_count - pending_count + req_conn; let mut open_slots = IDEAL_PEERS - peer_count - pending_count + req_conn;
if open_slots > 0 { if open_slots > 0 {
for n in to_connect.iter() { for n in &to_connect {
if n.peer_type == PeerType::Optional && open_slots > 0 { if n.peer_type == PeerType::Optional && open_slots > 0 {
open_slots -= 1; open_slots -= 1;
self.connect_peer(&n.id, io); self.connect_peer(&n.id, io);
@ -328,23 +390,26 @@ impl<Message> Host<Message> where Message: Send {
} }
} }
fn connect_peer(&mut self, id: &NodeId, io: &mut IoContext<NetworkIoMessage<Message>>) { #[allow(single_match)]
fn connect_peer(&self, id: &NodeId, io: &IoContext<NetworkIoMessage<Message>>) {
if self.have_session(id) if self.have_session(id)
{ {
warn!("Aborted connect. Node already connected."); warn!("Aborted connect. Node already connected.");
return; return;
} }
if self.connecting_to(id) if self.connecting_to(id) {
{
warn!("Aborted connect. Node already connecting."); warn!("Aborted connect. Node already connecting.");
return; return;
} }
let socket = { let socket = {
let node = self.nodes.get_mut(id).unwrap(); let address = {
let mut nodes = self.nodes.write().unwrap();
let node = nodes.get_mut(id).unwrap();
node.last_attempted = Some(::time::now()); node.last_attempted = Some(::time::now());
node.endpoint.address
match TcpStream::connect(&node.endpoint.address) { };
match TcpStream::connect(&address) {
Ok(socket) => socket, Ok(socket) => socket,
Err(_) => { Err(_) => {
warn!("Cannot connect to node"); warn!("Cannot connect to node");
@ -352,225 +417,203 @@ impl<Message> Host<Message> where Message: Send {
} }
} }
}; };
self.create_connection(socket, Some(id), io);
}
let nonce = self.info.next_nonce(); #[allow(block_in_if_condition_stmt)]
match self.connections.insert_with(|token| ConnectionEntry::Handshake(Handshake::new(Token(token), id, socket, &nonce).expect("Can't create handshake"))) { fn create_connection(&self, socket: TcpStream, id: Option<&NodeId>, io: &IoContext<NetworkIoMessage<Message>>) {
Some(token) => { let nonce = self.info.write().unwrap().next_nonce();
match self.connections.get_mut(token) { let mut connections = self.connections.write().unwrap();
Some(&mut ConnectionEntry::Handshake(ref mut h)) => { if connections.insert_with(|token| {
h.start(&self.info, true) let mut handshake = Handshake::new(token, id, socket, &nonce, self.stats.clone()).expect("Can't create handshake");
.and_then(|_| h.register(io.event_loop)) handshake.start(io, &self.info.read().unwrap(), id.is_some()).and_then(|_| io.register_stream(token)).unwrap_or_else (|e| {
.unwrap_or_else (|e| {
debug!(target: "net", "Handshake create error: {:?}", e); debug!(target: "net", "Handshake create error: {:?}", e);
}); });
}, Arc::new(Mutex::new(ConnectionEntry::Handshake(handshake)))
_ => {} }).is_none() {
} warn!("Max connections reached");
},
None => { warn!("Max connections reached") }
} }
} }
fn accept(&self, io: &IoContext<NetworkIoMessage<Message>>) {
fn accept(&mut self, _io: &mut IoContext<NetworkIoMessage<Message>>) {
trace!(target: "net", "accept"); trace!(target: "net", "accept");
loop {
let socket = match self.tcp_listener.lock().unwrap().accept() {
Ok(None) => break,
Ok(Some((sock, _addr))) => sock,
Err(e) => {
warn!("Error accepting connection: {:?}", e);
break
},
};
self.create_connection(socket, None, io);
}
io.update_registration(TCP_ACCEPT).expect("Error registering TCP listener");
} }
fn connection_writable<'s>(&'s mut self, token: StreamToken, io: &mut IoContext<'s, NetworkIoMessage<Message>>) { #[allow(single_match)]
let mut kill = false; fn connection_writable(&self, token: StreamToken, io: &IoContext<NetworkIoMessage<Message>>) {
let mut create_session = false; let mut create_session = false;
match self.connections.get_mut(token) { let mut kill = false;
Some(&mut ConnectionEntry::Handshake(ref mut h)) => { if let Some(connection) = self.connections.read().unwrap().get(token).cloned() {
h.writable(io.event_loop, &self.info).unwrap_or_else(|e| { match *connection.lock().unwrap().deref_mut() {
ConnectionEntry::Handshake(ref mut h) => {
match h.writable(io, &self.info.read().unwrap()) {
Err(e) => {
debug!(target: "net", "Handshake write error: {:?}", e); debug!(target: "net", "Handshake write error: {:?}", e);
kill = true; kill = true;
});
create_session = h.done();
}, },
Some(&mut ConnectionEntry::Session(ref mut s)) => { Ok(_) => ()
s.writable(io.event_loop, &self.info).unwrap_or_else(|e| { }
if h.done() {
create_session = true;
}
},
ConnectionEntry::Session(ref mut s) => {
match s.writable(io, &self.info.read().unwrap()) {
Err(e) => {
debug!(target: "net", "Session write error: {:?}", e); debug!(target: "net", "Session write error: {:?}", e);
kill = true; kill = true;
}); },
Ok(_) => ()
}
io.update_registration(token).unwrap_or_else(|e| debug!(target: "net", "Session registration error: {:?}", e));
} }
_ => {
warn!(target: "net", "Received event for unknown connection");
} }
} }
if kill { if kill {
self.kill_connection(token, io); self.kill_connection(token, io); //TODO: mark connection as dead an check in kill_connection
return; return;
} else if create_session { } else if create_session {
self.start_session(token, io); self.start_session(token, io);
} io.update_registration(token).unwrap_or_else(|e| debug!(target: "net", "Session registration error: {:?}", e));
match self.connections.get_mut(token) {
Some(&mut ConnectionEntry::Session(ref mut s)) => {
s.reregister(io.event_loop).unwrap_or_else(|e| debug!(target: "net", "Session registration error: {:?}", e));
},
_ => (),
} }
} }
fn connection_closed<'s>(&'s mut self, token: TimerToken, io: &mut IoContext<'s, NetworkIoMessage<Message>>) { fn connection_closed(&self, token: TimerToken, io: &IoContext<NetworkIoMessage<Message>>) {
self.kill_connection(token, io); self.kill_connection(token, io);
} }
fn connection_readable<'s>(&'s mut self, token: StreamToken, io: &mut IoContext<'s, NetworkIoMessage<Message>>) { fn connection_readable(&self, token: StreamToken, io: &IoContext<NetworkIoMessage<Message>>) {
let mut kill = false;
let mut create_session = false;
let mut ready_data: Vec<ProtocolId> = Vec::new(); let mut ready_data: Vec<ProtocolId> = Vec::new();
let mut packet_data: Option<(ProtocolId, PacketId, Vec<u8>)> = None; let mut packet_data: Option<(ProtocolId, PacketId, Vec<u8>)> = None;
match self.connections.get_mut(token) { let mut create_session = false;
Some(&mut ConnectionEntry::Handshake(ref mut h)) => { let mut kill = false;
h.readable(io.event_loop, &self.info).unwrap_or_else(|e| { if let Some(connection) = self.connections.read().unwrap().get(token).cloned() {
match *connection.lock().unwrap().deref_mut() {
ConnectionEntry::Handshake(ref mut h) => {
if let Err(e) = h.readable(io, &self.info.read().unwrap()) {
debug!(target: "net", "Handshake read error: {:?}", e); debug!(target: "net", "Handshake read error: {:?}", e);
kill = true; kill = true;
}); }
create_session = h.done(); if h.done() {
create_session = true;
}
}, },
Some(&mut ConnectionEntry::Session(ref mut s)) => { ConnectionEntry::Session(ref mut s) => {
let sd = { s.readable(io.event_loop, &self.info).unwrap_or_else(|e| { match s.readable(io, &self.info.read().unwrap()) {
debug!(target: "net", "Session read error: {:?}", e); Err(e) => {
debug!(target: "net", "Handshake read error: {:?}", e);
kill = true; kill = true;
SessionData::None },
}) }; Ok(SessionData::Ready) => {
match sd { for (p, _) in self.handlers.read().unwrap().iter() {
SessionData::Ready => {
for (p, _) in self.handlers.iter_mut() {
if s.have_capability(p) { if s.have_capability(p) {
ready_data.push(p); ready_data.push(p);
} }
} }
}, },
SessionData::Packet { Ok(SessionData::Packet {
data, data,
protocol, protocol,
packet_id, packet_id,
} => { }) => {
match self.handlers.get_mut(protocol) { match self.handlers.read().unwrap().get(protocol) {
None => { warn!(target: "net", "No handler found for protocol: {:?}", protocol) }, None => { warn!(target: "net", "No handler found for protocol: {:?}", protocol) },
Some(_) => packet_data = Some((protocol, packet_id, data)), Some(_) => packet_data = Some((protocol, packet_id, data)),
} }
}, },
SessionData::None => {}, Ok(SessionData::None) => {},
} }
} }
_ => {
warn!(target: "net", "Received event for unknown connection");
} }
} }
if kill { if kill {
self.kill_connection(token, io); self.kill_connection(token, io); //TODO: mark connection as dead an check in kill_connection
return; return;
} } else if create_session {
if create_session {
self.start_session(token, io); self.start_session(token, io);
io.update_registration(token).unwrap_or_else(|e| debug!(target: "net", "Session registration error: {:?}", e));
} }
for p in ready_data { for p in ready_data {
let mut h = self.handlers.get_mut(p).unwrap(); let h = self.handlers.read().unwrap().get(p).unwrap().clone();
h.connected(&mut NetworkContext::new(io, p, Some(token), &mut self.connections, &mut self.timers), &token); h.connected(&NetworkContext::new(io, p, Some(token), self.connections.clone()), &token);
} }
if let Some((p, packet_id, data)) = packet_data { if let Some((p, packet_id, data)) = packet_data {
let mut h = self.handlers.get_mut(p).unwrap(); let h = self.handlers.read().unwrap().get(p).unwrap().clone();
h.read(&mut NetworkContext::new(io, p, Some(token), &mut self.connections, &mut self.timers), &token, packet_id, &data[1..]); h.read(&NetworkContext::new(io, p, Some(token), self.connections.clone()), &token, packet_id, &data[1..]);
}
io.update_registration(token).unwrap_or_else(|e| debug!(target: "net", "Token registration error: {:?}", e));
} }
match self.connections.get_mut(token) { fn start_session(&self, token: StreamToken, io: &IoContext<NetworkIoMessage<Message>>) {
Some(&mut ConnectionEntry::Session(ref mut s)) => { let mut connections = self.connections.write().unwrap();
s.reregister(io.event_loop).unwrap_or_else(|e| debug!(target: "net", "Session registration error: {:?}", e)); connections.replace_with(token, |c| {
match Arc::try_unwrap(c).ok().unwrap().into_inner().unwrap() {
ConnectionEntry::Handshake(h) => {
let session = Session::new(h, io, &self.info.read().unwrap()).expect("Session creation error");
io.update_registration(token).expect("Error updating session registration");
self.stats.inc_sessions();
Some(Arc::new(Mutex::new(ConnectionEntry::Session(session))))
}, },
_ => (), _ => { None } // handshake expired
} }
}).ok();
} }
fn start_session(&mut self, token: StreamToken, io: &mut IoContext<NetworkIoMessage<Message>>) { fn connection_timeout(&self, token: StreamToken, io: &IoContext<NetworkIoMessage<Message>>) {
let info = &self.info;
// TODO: use slab::replace_with (currently broken)
/*
match self.connections.remove(token) {
Some(ConnectionEntry::Handshake(h)) => {
match Session::new(h, io.event_loop, info) {
Ok(session) => {
assert!(token == self.connections.insert(ConnectionEntry::Session(session)).ok().unwrap());
},
Err(e) => {
debug!(target: "net", "Session construction error: {:?}", e);
}
}
},
_ => panic!("Error updating slab with session")
}*/
self.connections.replace_with(token, |c| {
match c {
ConnectionEntry::Handshake(h) => Session::new(h, io.event_loop, info)
.map(|s| Some(ConnectionEntry::Session(s)))
.unwrap_or_else(|e| {
debug!(target: "net", "Session construction error: {:?}", e);
None
}),
_ => { panic!("No handshake to create a session from"); }
}
}).expect("Error updating slab with session");
}
fn connection_timeout<'s>(&'s mut self, token: StreamToken, io: &mut IoContext<'s, NetworkIoMessage<Message>>) {
self.kill_connection(token, io) self.kill_connection(token, io)
} }
fn kill_connection<'s>(&'s mut self, token: StreamToken, io: &mut IoContext<'s, NetworkIoMessage<Message>>) { fn kill_connection(&self, token: StreamToken, io: &IoContext<NetworkIoMessage<Message>>) {
let mut to_disconnect: Vec<ProtocolId> = Vec::new(); let mut to_disconnect: Vec<ProtocolId> = Vec::new();
let mut remove = true; {
match self.connections.get_mut(token) { let mut connections = self.connections.write().unwrap();
Some(&mut ConnectionEntry::Handshake(_)) => (), // just abandon handshake if let Some(connection) = connections.get(token).cloned() {
Some(&mut ConnectionEntry::Session(ref mut s)) if s.is_ready() => { match *connection.lock().unwrap().deref_mut() {
for (p, _) in self.handlers.iter_mut() { ConnectionEntry::Handshake(_) => {
connections.remove(token);
},
ConnectionEntry::Session(ref mut s) if s.is_ready() => {
for (p, _) in self.handlers.read().unwrap().iter() {
if s.have_capability(p) { if s.have_capability(p) {
to_disconnect.push(p); to_disconnect.push(p);
} }
} }
connections.remove(token);
}, },
_ => { _ => {},
remove = false; }
}, }
io.deregister_stream(token).expect("Error deregistering stream");
} }
for p in to_disconnect { for p in to_disconnect {
let mut h = self.handlers.get_mut(p).unwrap(); let h = self.handlers.read().unwrap().get(p).unwrap().clone();
h.disconnected(&mut NetworkContext::new(io, p, Some(token), &mut self.connections, &mut self.timers), &token); h.disconnected(&NetworkContext::new(io, p, Some(token), self.connections.clone()), &token);
}
if remove {
self.connections.remove(token);
} }
} }
} }
impl<Message> IoHandler<NetworkIoMessage<Message>> for Host<Message> where Message: Send + 'static { impl<Message> IoHandler<NetworkIoMessage<Message>> for Host<Message> where Message: Send + Sync + Clone + 'static {
/// Initialize networking /// Initialize networking
fn initialize(&mut self, io: &mut IoContext<NetworkIoMessage<Message>>) { fn initialize(&self, io: &IoContext<NetworkIoMessage<Message>>) {
/* io.register_stream(TCP_ACCEPT).expect("Error registering TCP listener");
match ::ifaces::Interface::get_all().unwrap().into_iter().filter(|x| x.kind == ::ifaces::Kind::Packet && x.addr.is_some()).next() { io.register_stream(NODETABLE_RECEIVE).expect("Error registering UDP listener");
Some(iface) => config.public_address = iface.addr.unwrap(), io.register_timer(IDLE, MAINTENANCE_TIMEOUT).expect("Error registering Network idle timer");
None => warn!("No public network interface"), //io.register_timer(NODETABLE_MAINTAIN, 7200);
*/
// Start listening for incoming connections
io.event_loop.register(&self.listener, Token(TCP_ACCEPT), EventSet::readable(), PollOpt::edge()).unwrap();
io.event_loop.timeout_ms(Token(IDLE), MAINTENANCE_TIMEOUT).unwrap();
// open the udp socket
io.event_loop.register(&self.udp_socket, Token(NODETABLE_RECEIVE), EventSet::readable(), PollOpt::edge()).unwrap();
io.event_loop.timeout_ms(Token(NODETABLE_MAINTAIN), 7200).unwrap();
let port = self.info.config.listen_address.port();
self.info.listen_port = port;
self.add_node("enode://a9a921de2ff09a9a4d38b623c67b2d6b477a8e654ae95d874750cbbcb31b33296496a7b4421934e2629269e180823e52c15c2b19fc59592ec51ffe4f2de76ed7@127.0.0.1:30303");
/* // GO bootnodes
self.add_node("enode://a979fb575495b8d6db44f750317d0f4622bf4c2aa3365d6af7c284339968eef29b69ad0dce72a4d8db5ebb4968de0e3bec910127f134779fbcb0cb6d3331163c@52.16.188.185:30303"); // IE
self.add_node("enode://de471bccee3d042261d52e9bff31458daecc406142b401d4cd848f677479f73104b9fdeb090af9583d3391b7f10cb2ba9e26865dd5fca4fcdc0fb1e3b723c786@54.94.239.50:30303"); // BR
self.add_node("enode://1118980bf48b0a3640bdba04e0fe78b1add18e1cd99bf22d53daac1fd9972ad650df52176e7c7d89d1114cfef2bc23a2959aa54998a46afcf7d91809f0855082@52.74.57.123:30303"); // SG
// ETH/DEV cpp-ethereum (poc-9.ethdev.com)
self.add_node("enode://979b7fa28feeb35a4741660a16076f1943202cb72b6af70d327f053e248bab9ba81760f39d0701ef1d8f89cc1fbd2cacba0710a12cd5314d5e0c9021aa3637f9@5.1.83.226:30303");*/
} }
fn stream_hup<'s>(&'s mut self, io: &mut IoContext<'s, NetworkIoMessage<Message>>, stream: StreamToken) { fn stream_hup(&self, io: &IoContext<NetworkIoMessage<Message>>, stream: StreamToken) {
trace!(target: "net", "Hup: {}", stream); trace!(target: "net", "Hup: {}", stream);
match stream { match stream {
FIRST_CONNECTION ... LAST_CONNECTION => self.connection_closed(stream, io), FIRST_CONNECTION ... LAST_CONNECTION => self.connection_closed(stream, io),
@ -578,7 +621,7 @@ impl<Message> IoHandler<NetworkIoMessage<Message>> for Host<Message> where Messa
}; };
} }
fn stream_readable<'s>(&'s mut self, io: &mut IoContext<'s, NetworkIoMessage<Message>>, stream: StreamToken) { fn stream_readable(&self, io: &IoContext<NetworkIoMessage<Message>>, stream: StreamToken) {
match stream { match stream {
FIRST_CONNECTION ... LAST_CONNECTION => self.connection_readable(stream, io), FIRST_CONNECTION ... LAST_CONNECTION => self.connection_readable(stream, io),
NODETABLE_RECEIVE => {}, NODETABLE_RECEIVE => {},
@ -587,65 +630,115 @@ impl<Message> IoHandler<NetworkIoMessage<Message>> for Host<Message> where Messa
} }
} }
fn stream_writable<'s>(&'s mut self, io: &mut IoContext<'s, NetworkIoMessage<Message>>, stream: StreamToken) { fn stream_writable(&self, io: &IoContext<NetworkIoMessage<Message>>, stream: StreamToken) {
match stream { match stream {
FIRST_CONNECTION ... LAST_CONNECTION => self.connection_writable(stream, io), FIRST_CONNECTION ... LAST_CONNECTION => self.connection_writable(stream, io),
NODETABLE_RECEIVE => {},
_ => panic!("Received unknown writable token"), _ => panic!("Received unknown writable token"),
} }
} }
fn timeout<'s>(&'s mut self, io: &mut IoContext<'s, NetworkIoMessage<Message>>, token: TimerToken) { fn timeout(&self, io: &IoContext<NetworkIoMessage<Message>>, token: TimerToken) {
match token { match token {
IDLE => self.maintain_network(io), IDLE => self.maintain_network(io),
FIRST_CONNECTION ... LAST_CONNECTION => self.connection_timeout(token, io), FIRST_CONNECTION ... LAST_CONNECTION => self.connection_timeout(token, io),
NODETABLE_DISCOVERY => {}, NODETABLE_DISCOVERY => {},
NODETABLE_MAINTAIN => {}, NODETABLE_MAINTAIN => {},
_ => match self.timers.get_mut(&token).map(|p| *p) { _ => match self.timers.read().unwrap().get(&token).cloned() {
Some(protocol) => match self.handlers.get_mut(protocol) { Some(timer) => match self.handlers.read().unwrap().get(timer.protocol).cloned() {
None => { warn!(target: "net", "No handler found for protocol: {:?}", protocol) }, None => { warn!(target: "net", "No handler found for protocol: {:?}", timer.protocol) },
Some(h) => { h.timeout(&mut NetworkContext::new(io, protocol, Some(token), &mut self.connections, &mut self.timers), token); } Some(h) => { h.timeout(&NetworkContext::new(io, timer.protocol, None, self.connections.clone()), timer.token); }
}, },
None => {} // time not registerd through us None => { warn!("Unknown timer token: {}", token); } // timer is not registerd through us
} }
} }
} }
fn message<'s>(&'s mut self, io: &mut IoContext<'s, NetworkIoMessage<Message>>, message: &'s mut NetworkIoMessage<Message>) { fn message(&self, io: &IoContext<NetworkIoMessage<Message>>, message: &NetworkIoMessage<Message>) {
match message { match *message {
&mut NetworkIoMessage::AddHandler { NetworkIoMessage::AddHandler {
ref mut handler, ref handler,
ref protocol, ref protocol,
ref versions ref versions
} => { } => {
let mut h = mem::replace(handler, None).unwrap(); let h = handler.clone();
h.initialize(&mut NetworkContext::new(io, protocol, None, &mut self.connections, &mut self.timers)); h.initialize(&NetworkContext::new(io, protocol, None, self.connections.clone()));
self.handlers.insert(protocol, h); self.handlers.write().unwrap().insert(protocol, h);
let mut info = self.info.write().unwrap();
for v in versions { for v in versions {
self.info.capabilities.push(CapabilityInfo { protocol: protocol, version: *v, packet_count:0 }); info.capabilities.push(CapabilityInfo { protocol: protocol, version: *v, packet_count:0 });
} }
}, },
&mut NetworkIoMessage::Send { NetworkIoMessage::AddTimer {
ref peer,
ref packet_id,
ref protocol, ref protocol,
ref data, ref delay,
ref token,
} => { } => {
match self.connections.get_mut(*peer as usize) { let handler_token = {
Some(&mut ConnectionEntry::Session(ref mut s)) => { let mut timer_counter = self.timer_counter.write().unwrap();
s.send_packet(protocol, *packet_id as u8, &data).unwrap_or_else(|e| { let counter = timer_counter.deref_mut();
warn!(target: "net", "Send error: {:?}", e); let handler_token = *counter;
}); //TODO: don't copy vector data *counter += 1;
handler_token
};
self.timers.write().unwrap().insert(handler_token, ProtocolTimer { protocol: protocol, token: *token });
io.register_timer(handler_token, *delay).expect("Error registering timer");
}, },
_ => { NetworkIoMessage::User(ref message) => {
warn!(target: "net", "Send: Peer does not exist"); for (p, h) in self.handlers.read().unwrap().iter() {
h.message(&NetworkContext::new(io, p, None, self.connections.clone()), &message);
} }
} }
}
}
fn register_stream(&self, stream: StreamToken, reg: Token, event_loop: &mut EventLoop<IoManager<NetworkIoMessage<Message>>>) {
match stream {
FIRST_CONNECTION ... LAST_CONNECTION => {
if let Some(connection) = self.connections.read().unwrap().get(stream).cloned() {
match *connection.lock().unwrap().deref() {
ConnectionEntry::Handshake(ref h) => h.register_socket(reg, event_loop).expect("Error registering socket"),
ConnectionEntry::Session(_) => warn!("Unexpected session stream registration")
}
} else {} // expired
}
NODETABLE_RECEIVE => event_loop.register(self.udp_socket.lock().unwrap().deref(), Token(NODETABLE_RECEIVE), EventSet::all(), PollOpt::edge()).expect("Error registering stream"),
TCP_ACCEPT => event_loop.register(self.tcp_listener.lock().unwrap().deref(), Token(TCP_ACCEPT), EventSet::all(), PollOpt::edge()).expect("Error registering stream"),
_ => warn!("Unexpected stream registration")
}
}
fn deregister_stream(&self, stream: StreamToken, event_loop: &mut EventLoop<IoManager<NetworkIoMessage<Message>>>) {
match stream {
FIRST_CONNECTION ... LAST_CONNECTION => {
let mut connections = self.connections.write().unwrap();
if let Some(connection) = connections.get(stream).cloned() {
match *connection.lock().unwrap().deref() {
ConnectionEntry::Handshake(ref h) => h.deregister_socket(event_loop).expect("Error deregistering socket"),
ConnectionEntry::Session(ref s) => s.deregister_socket(event_loop).expect("Error deregistering session socket"),
}
connections.remove(stream);
}
}, },
&mut NetworkIoMessage::User(ref message) => { NODETABLE_RECEIVE => event_loop.deregister(self.udp_socket.lock().unwrap().deref()).unwrap(),
for (p, h) in self.handlers.iter_mut() { TCP_ACCEPT => event_loop.deregister(self.tcp_listener.lock().unwrap().deref()).unwrap(),
h.message(&mut NetworkContext::new(io, p, None, &mut self.connections, &mut self.timers), &message); _ => warn!("Unexpected stream deregistration")
} }
} }
fn update_stream(&self, stream: StreamToken, reg: Token, event_loop: &mut EventLoop<IoManager<NetworkIoMessage<Message>>>) {
match stream {
FIRST_CONNECTION ... LAST_CONNECTION => {
if let Some(connection) = self.connections.read().unwrap().get(stream).cloned() {
match *connection.lock().unwrap().deref() {
ConnectionEntry::Handshake(ref h) => h.update_socket(reg, event_loop).expect("Error updating socket"),
ConnectionEntry::Session(ref s) => s.update_socket(reg, event_loop).expect("Error updating socket"),
}
} else {} // expired
}
NODETABLE_RECEIVE => event_loop.reregister(self.udp_socket.lock().unwrap().deref(), Token(NODETABLE_RECEIVE), EventSet::all(), PollOpt::edge()).expect("Error reregistering stream"),
TCP_ACCEPT => event_loop.reregister(self.tcp_listener.lock().unwrap().deref(), Token(TCP_ACCEPT), EventSet::all(), PollOpt::edge()).expect("Error reregistering stream"),
_ => warn!("Unexpected stream update")
} }
} }
} }

View File

@ -1,5 +1,4 @@
/// Network and general IO module. /// Network and general IO module.
///
/// Example usage for craeting a network service and adding an IO handler: /// Example usage for craeting a network service and adding an IO handler:
/// ///
/// ```rust /// ```rust
@ -8,39 +7,40 @@
/// ///
/// struct MyHandler; /// struct MyHandler;
/// ///
/// #[derive(Clone)]
/// struct MyMessage { /// struct MyMessage {
/// data: u32 /// data: u32
/// } /// }
/// ///
/// impl NetworkProtocolHandler<MyMessage> for MyHandler { /// impl NetworkProtocolHandler<MyMessage> for MyHandler {
/// fn initialize(&mut self, io: &mut NetworkContext<MyMessage>) { /// fn initialize(&self, io: &NetworkContext<MyMessage>) {
/// io.register_timer(1000); /// io.register_timer(0, 1000);
/// } /// }
/// ///
/// fn read(&mut self, io: &mut NetworkContext<MyMessage>, peer: &PeerId, packet_id: u8, data: &[u8]) { /// fn read(&self, io: &NetworkContext<MyMessage>, peer: &PeerId, packet_id: u8, data: &[u8]) {
/// println!("Received {} ({} bytes) from {}", packet_id, data.len(), peer); /// println!("Received {} ({} bytes) from {}", packet_id, data.len(), peer);
/// } /// }
/// ///
/// fn connected(&mut self, io: &mut NetworkContext<MyMessage>, peer: &PeerId) { /// fn connected(&self, io: &NetworkContext<MyMessage>, peer: &PeerId) {
/// println!("Connected {}", peer); /// println!("Connected {}", peer);
/// } /// }
/// ///
/// fn disconnected(&mut self, io: &mut NetworkContext<MyMessage>, peer: &PeerId) { /// fn disconnected(&self, io: &NetworkContext<MyMessage>, peer: &PeerId) {
/// println!("Disconnected {}", peer); /// println!("Disconnected {}", peer);
/// } /// }
/// ///
/// fn timeout(&mut self, io: &mut NetworkContext<MyMessage>, timer: TimerToken) { /// fn timeout(&self, io: &NetworkContext<MyMessage>, timer: TimerToken) {
/// println!("Timeout {}", timer); /// println!("Timeout {}", timer);
/// } /// }
/// ///
/// fn message(&mut self, io: &mut NetworkContext<MyMessage>, message: &MyMessage) { /// fn message(&self, io: &NetworkContext<MyMessage>, message: &MyMessage) {
/// println!("Message {}", message.data); /// println!("Message {}", message.data);
/// } /// }
/// } /// }
/// ///
/// fn main () { /// fn main () {
/// let mut service = NetworkService::<MyMessage>::start().expect("Error creating network service"); /// let mut service = NetworkService::<MyMessage>::start(NetworkConfiguration::new()).expect("Error creating network service");
/// service.register_protocol(Box::new(MyHandler), "myproto", &[1u8]); /// service.register_protocol(Arc::new(MyHandler), "myproto", &[1u8]);
/// ///
/// // Wait for quit condition /// // Wait for quit condition
/// // ... /// // ...
@ -55,38 +55,38 @@ mod discovery;
mod service; mod service;
mod error; mod error;
mod node; mod node;
mod stats;
/// TODO [arkpar] Please document me #[cfg(test)]
pub type PeerId = host::PeerId; mod tests;
/// TODO [arkpar] Please document me
pub type PacketId = host::PacketId; pub use network::host::PeerId;
/// TODO [arkpar] Please document me pub use network::host::PacketId;
pub type NetworkContext<'s,'io, Message> = host::NetworkContext<'s, 'io, Message>; pub use network::host::NetworkContext;
/// TODO [arkpar] Please document me pub use network::service::NetworkService;
pub type NetworkService<Message> = service::NetworkService<Message>; pub use network::host::NetworkIoMessage;
/// TODO [arkpar] Please document me
pub type NetworkIoMessage<Message> = host::NetworkIoMessage<Message>;
pub use network::host::NetworkIoMessage::User as UserMessage; pub use network::host::NetworkIoMessage::User as UserMessage;
/// TODO [arkpar] Please document me pub use network::error::NetworkError;
pub type NetworkError = error::NetworkError; pub use network::host::NetworkConfiguration;
pub use network::stats::NetworkStats;
use io::*; use io::TimerToken;
/// Network IO protocol handler. This needs to be implemented for each new subprotocol. /// Network IO protocol handler. This needs to be implemented for each new subprotocol.
/// All the handler function are called from within IO event loop. /// All the handler function are called from within IO event loop.
/// `Message` is the type for message data. /// `Message` is the type for message data.
pub trait NetworkProtocolHandler<Message>: Send where Message: Send { pub trait NetworkProtocolHandler<Message>: Sync + Send where Message: Send + Sync + Clone {
/// Initialize the handler /// Initialize the handler
fn initialize(&mut self, _io: &mut NetworkContext<Message>) {} fn initialize(&self, _io: &NetworkContext<Message>) {}
/// Called when new network packet received. /// Called when new network packet received.
fn read(&mut self, io: &mut NetworkContext<Message>, peer: &PeerId, packet_id: u8, data: &[u8]); fn read(&self, io: &NetworkContext<Message>, peer: &PeerId, packet_id: u8, data: &[u8]);
/// Called when new peer is connected. Only called when peer supports the same protocol. /// Called when new peer is connected. Only called when peer supports the same protocol.
fn connected(&mut self, io: &mut NetworkContext<Message>, peer: &PeerId); fn connected(&self, io: &NetworkContext<Message>, peer: &PeerId);
/// Called when a previously connected peer disconnects. /// Called when a previously connected peer disconnects.
fn disconnected(&mut self, io: &mut NetworkContext<Message>, peer: &PeerId); fn disconnected(&self, io: &NetworkContext<Message>, peer: &PeerId);
/// Timer function called after a timeout created with `NetworkContext::timeout`. /// Timer function called after a timeout created with `NetworkContext::timeout`.
fn timeout(&mut self, _io: &mut NetworkContext<Message>, _timer: TimerToken) {} fn timeout(&self, _io: &NetworkContext<Message>, _timer: TimerToken) {}
/// Called when a broadcasted message is received. The message can only be sent from a different IO handler. /// Called when a broadcasted message is received. The message can only be sent from a different IO handler.
fn message(&mut self, _io: &mut NetworkContext<Message>, _message: &Message) {} fn message(&self, _io: &NetworkContext<Message>, _message: &Message) {}
} }

View File

@ -20,14 +20,16 @@ pub struct NodeEndpoint {
pub udp_port: u16 pub udp_port: u16
} }
impl NodeEndpoint { impl FromStr for NodeEndpoint {
type Err = UtilError;
/// Create endpoint from string. Performs name resolution if given a host name. /// Create endpoint from string. Performs name resolution if given a host name.
fn from_str(s: &str) -> Result<NodeEndpoint, UtilError> { fn from_str(s: &str) -> Result<NodeEndpoint, UtilError> {
let address = s.to_socket_addrs().map(|mut i| i.next()); let address = s.to_socket_addrs().map(|mut i| i.next());
match address { match address {
Ok(Some(a)) => Ok(NodeEndpoint { Ok(Some(a)) => Ok(NodeEndpoint {
address: a, address: a,
address_str: s.to_string(), address_str: s.to_owned(),
udp_port: a.port() udp_port: a.port()
}), }),
Ok(_) => Err(UtilError::AddressResolve(None)), Ok(_) => Err(UtilError::AddressResolve(None)),

View File

@ -1,45 +1,39 @@
use std::sync::*;
use error::*; use error::*;
use network::{NetworkProtocolHandler}; use network::{NetworkProtocolHandler, NetworkConfiguration};
use network::error::{NetworkError}; use network::error::{NetworkError};
use network::host::{Host, NetworkIoMessage, PeerId, PacketId, ProtocolId}; use network::host::{Host, NetworkIoMessage, ProtocolId};
use network::stats::{NetworkStats};
use io::*; use io::*;
/// IO Service with networking /// IO Service with networking
/// `Message` defines a notification data type. /// `Message` defines a notification data type.
pub struct NetworkService<Message> where Message: Send + 'static { pub struct NetworkService<Message> where Message: Send + Sync + Clone + 'static {
io_service: IoService<NetworkIoMessage<Message>>, io_service: IoService<NetworkIoMessage<Message>>,
host_info: String, host_info: String,
stats: Arc<NetworkStats>
} }
impl<Message> NetworkService<Message> where Message: Send + 'static { impl<Message> NetworkService<Message> where Message: Send + Sync + Clone + 'static {
/// Starts IO event loop /// Starts IO event loop
pub fn start() -> Result<NetworkService<Message>, UtilError> { pub fn start(config: NetworkConfiguration) -> Result<NetworkService<Message>, UtilError> {
let mut io_service = try!(IoService::<NetworkIoMessage<Message>>::start()); let mut io_service = try!(IoService::<NetworkIoMessage<Message>>::start());
let host = Box::new(Host::new()); let host = Arc::new(Host::new(config));
let host_info = host.info.client_version.clone(); let stats = host.stats().clone();
info!("NetworkService::start(): id={:?}", host.info.id()); let host_info = host.client_version();
info!("NetworkService::start(): id={:?}", host.client_id());
try!(io_service.register_handler(host)); try!(io_service.register_handler(host));
Ok(NetworkService { Ok(NetworkService {
io_service: io_service, io_service: io_service,
host_info: host_info, host_info: host_info,
stats: stats,
}) })
} }
/// Send a message over the network. Normaly `HostIo::send` should be used. This can be used from non-io threads.
pub fn send(&mut self, peer: &PeerId, packet_id: PacketId, protocol: ProtocolId, data: &[u8]) -> Result<(), NetworkError> {
try!(self.io_service.send_message(NetworkIoMessage::Send {
peer: *peer,
packet_id: packet_id,
protocol: protocol,
data: data.to_vec()
}));
Ok(())
}
/// Regiter a new protocol handler with the event loop. /// Regiter a new protocol handler with the event loop.
pub fn register_protocol(&mut self, handler: Box<NetworkProtocolHandler<Message>+Send>, protocol: ProtocolId, versions: &[u8]) -> Result<(), NetworkError> { pub fn register_protocol(&mut self, handler: Arc<NetworkProtocolHandler<Message>+Send + Sync>, protocol: ProtocolId, versions: &[u8]) -> Result<(), NetworkError> {
try!(self.io_service.send_message(NetworkIoMessage::AddHandler { try!(self.io_service.send_message(NetworkIoMessage::AddHandler {
handler: Some(handler), handler: handler,
protocol: protocol, protocol: protocol,
versions: versions.to_vec(), versions: versions.to_vec(),
})); }));
@ -56,6 +50,9 @@ impl<Message> NetworkService<Message> where Message: Send + 'static {
&mut self.io_service &mut self.io_service
} }
/// Returns underlying io service.
pub fn stats(&self) -> &NetworkStats {
&self.stats
}
} }

View File

@ -4,6 +4,7 @@ use rlp::*;
use network::connection::{EncryptedConnection, Packet}; use network::connection::{EncryptedConnection, Packet};
use network::handshake::Handshake; use network::handshake::Handshake;
use error::*; use error::*;
use io::{IoContext};
use network::error::{NetworkError, DisconnectReason}; use network::error::{NetworkError, DisconnectReason};
use network::host::*; use network::host::*;
use network::node::NodeId; use network::node::NodeId;
@ -84,7 +85,7 @@ const PACKET_LAST: u8 = 0x7f;
impl Session { impl Session {
/// Create a new session out of comepleted handshake. Consumes handshake object. /// Create a new session out of comepleted handshake. Consumes handshake object.
pub fn new<Host:Handler<Timeout=Token>>(h: Handshake, event_loop: &mut EventLoop<Host>, host: &HostInfo) -> Result<Session, UtilError> { pub fn new<Message>(h: Handshake, _io: &IoContext<Message>, host: &HostInfo) -> Result<Session, UtilError> where Message: Send + Sync + Clone {
let id = h.id.clone(); let id = h.id.clone();
let connection = try!(EncryptedConnection::new(h)); let connection = try!(EncryptedConnection::new(h));
let mut session = Session { let mut session = Session {
@ -99,7 +100,6 @@ impl Session {
}; };
try!(session.write_hello(host)); try!(session.write_hello(host));
try!(session.write_ping()); try!(session.write_ping());
try!(session.connection.register(event_loop));
Ok(session) Ok(session)
} }
@ -109,16 +109,16 @@ impl Session {
} }
/// Readable IO handler. Returns packet data if available. /// Readable IO handler. Returns packet data if available.
pub fn readable<Host:Handler>(&mut self, event_loop: &mut EventLoop<Host>, host: &HostInfo) -> Result<SessionData, UtilError> { pub fn readable<Message>(&mut self, io: &IoContext<Message>, host: &HostInfo) -> Result<SessionData, UtilError> where Message: Send + Sync + Clone {
match try!(self.connection.readable(event_loop)) { match try!(self.connection.readable(io)) {
Some(data) => Ok(try!(self.read_packet(data, host))), Some(data) => Ok(try!(self.read_packet(data, host))),
None => Ok(SessionData::None) None => Ok(SessionData::None)
} }
} }
/// Writable IO handler. Sends pending packets. /// Writable IO handler. Sends pending packets.
pub fn writable<Host:Handler>(&mut self, event_loop: &mut EventLoop<Host>, _host: &HostInfo) -> Result<(), UtilError> { pub fn writable<Message>(&mut self, io: &IoContext<Message>, _host: &HostInfo) -> Result<(), UtilError> where Message: Send + Sync + Clone {
self.connection.writable(event_loop) self.connection.writable(io)
} }
/// Checks if peer supports given capability /// Checks if peer supports given capability
@ -127,8 +127,13 @@ impl Session {
} }
/// Update registration with the event loop. Should be called at the end of the IO handler. /// Update registration with the event loop. Should be called at the end of the IO handler.
pub fn reregister<Host:Handler>(&mut self, event_loop: &mut EventLoop<Host>) -> Result<(), UtilError> { pub fn update_socket<Host:Handler>(&self, reg:Token, event_loop: &mut EventLoop<Host>) -> Result<(), UtilError> {
self.connection.reregister(event_loop) self.connection.update_socket(reg, event_loop)
}
/// Delete registration
pub fn deregister_socket<Host:Handler>(&self, event_loop: &mut EventLoop<Host>) -> Result<(), UtilError> {
self.connection.deregister_socket(event_loop)
} }
/// Send a protocol packet to peer. /// Send a protocol packet to peer.
@ -182,7 +187,7 @@ impl Session {
// map to protocol // map to protocol
let protocol = self.info.capabilities[i].protocol; let protocol = self.info.capabilities[i].protocol;
let pid = packet_id - self.info.capabilities[i].id_offset; let pid = packet_id - self.info.capabilities[i].id_offset;
return Ok(SessionData::Packet { data: packet.data, protocol: protocol, packet_id: pid } ) Ok(SessionData::Packet { data: packet.data, protocol: protocol, packet_id: pid } )
}, },
_ => { _ => {
debug!(target: "net", "Unkown packet: {:?}", packet_id); debug!(target: "net", "Unkown packet: {:?}", packet_id);
@ -212,7 +217,7 @@ impl Session {
// Intersect with host capabilities // Intersect with host capabilities
// Leave only highset mutually supported capability version // Leave only highset mutually supported capability version
let mut caps: Vec<SessionCapabilityInfo> = Vec::new(); let mut caps: Vec<SessionCapabilityInfo> = Vec::new();
for hc in host.capabilities.iter() { for hc in &host.capabilities {
if peer_caps.iter().any(|c| c.protocol == hc.protocol && c.version == hc.version) { if peer_caps.iter().any(|c| c.protocol == hc.protocol && c.version == hc.version) {
caps.push(SessionCapabilityInfo { caps.push(SessionCapabilityInfo {
protocol: hc.protocol, protocol: hc.protocol,

51
util/src/network/stats.rs Normal file
View File

@ -0,0 +1,51 @@
//! Network Statistics
use std::sync::atomic::*;
/// Network statistics structure
#[derive(Default, Debug)]
pub struct NetworkStats {
/// Bytes received
recv: AtomicUsize,
/// Bytes sent
send: AtomicUsize,
/// Total number of sessions created
sessions: AtomicUsize,
}
impl NetworkStats {
/// Increase bytes received.
#[inline]
pub fn inc_recv(&self, size: usize) {
self.recv.fetch_add(size, Ordering::Relaxed);
}
/// Increase bytes sent.
#[inline]
pub fn inc_send(&self, size: usize) {
self.send.fetch_add(size, Ordering::Relaxed);
}
/// Increase number of sessions.
#[inline]
pub fn inc_sessions(&self) {
self.sessions.fetch_add(1, Ordering::Relaxed);
}
/// Get bytes sent.
#[inline]
pub fn send(&self) -> usize {
self.send.load(Ordering::Relaxed)
}
/// Get bytes received.
#[inline]
pub fn recv(&self) -> usize {
self.recv.load(Ordering::Relaxed)
}
/// Get total number of sessions created.
#[inline]
pub fn sessions(&self) -> usize {
self.sessions.load(Ordering::Relaxed)
}
}

103
util/src/network/tests.rs Normal file
View File

@ -0,0 +1,103 @@
use std::sync::atomic::{AtomicBool, Ordering as AtomicOrdering};
use std::thread;
use std::time::*;
use common::*;
use network::*;
use io::TimerToken;
use crypto::KeyPair;
pub struct TestProtocol {
pub packet: Mutex<Bytes>,
pub got_timeout: AtomicBool,
}
impl Default for TestProtocol {
fn default() -> Self {
TestProtocol {
packet: Mutex::new(Vec::new()),
got_timeout: AtomicBool::new(false),
}
}
}
#[derive(Clone)]
pub struct TestProtocolMessage {
payload: u32,
}
impl TestProtocol {
/// Creates and register protocol with the network service
pub fn register(service: &mut NetworkService<TestProtocolMessage>) -> Arc<TestProtocol> {
let handler = Arc::new(TestProtocol::default());
service.register_protocol(handler.clone(), "test", &[42u8, 43u8]).expect("Error registering test protocol handler");
handler
}
pub fn got_packet(&self) -> bool {
self.packet.lock().unwrap().deref()[..] == b"hello"[..]
}
pub fn got_timeout(&self) -> bool {
self.got_timeout.load(AtomicOrdering::Relaxed)
}
}
impl NetworkProtocolHandler<TestProtocolMessage> for TestProtocol {
fn initialize(&self, io: &NetworkContext<TestProtocolMessage>) {
io.register_timer(0, 10).unwrap();
}
fn read(&self, _io: &NetworkContext<TestProtocolMessage>, _peer: &PeerId, packet_id: u8, data: &[u8]) {
assert_eq!(packet_id, 33);
self.packet.lock().unwrap().extend(data);
}
fn connected(&self, io: &NetworkContext<TestProtocolMessage>, _peer: &PeerId) {
io.respond(33, "hello".to_owned().into_bytes()).unwrap();
}
fn disconnected(&self, _io: &NetworkContext<TestProtocolMessage>, _peer: &PeerId) {
}
/// Timer function called after a timeout created with `NetworkContext::timeout`.
fn timeout(&self, _io: &NetworkContext<TestProtocolMessage>, timer: TimerToken) {
assert_eq!(timer, 0);
self.got_timeout.store(true, AtomicOrdering::Relaxed);
}
}
#[test]
fn test_net_service() {
let mut service = NetworkService::<TestProtocolMessage>::start(NetworkConfiguration::new()).expect("Error creating network service");
service.register_protocol(Arc::new(TestProtocol::default()), "myproto", &[1u8]).unwrap();
}
#[test]
fn test_net_connect() {
let key1 = KeyPair::create().unwrap();
let mut config1 = NetworkConfiguration::new_with_port(30344);
config1.use_secret = Some(key1.secret().clone());
config1.boot_nodes = vec![ ];
let mut config2 = NetworkConfiguration::new_with_port(30345);
config2.boot_nodes = vec![ format!("enode://{}@127.0.0.1:30344", key1.public().hex()) ];
let mut service1 = NetworkService::<TestProtocolMessage>::start(config1).unwrap();
let mut service2 = NetworkService::<TestProtocolMessage>::start(config2).unwrap();
let handler1 = TestProtocol::register(&mut service1);
let handler2 = TestProtocol::register(&mut service2);
while !handler1.got_packet() && !handler2.got_packet() {
thread::sleep(Duration::from_millis(50));
}
assert!(service1.stats().sessions() >= 1);
assert!(service2.stats().sessions() >= 1);
}
#[test]
fn test_net_timeout() {
let config = NetworkConfiguration::new_with_port(30346);
let mut service = NetworkService::<TestProtocolMessage>::start(config).unwrap();
let handler = TestProtocol::register(&mut service);
while !handler.got_timeout() {
thread::sleep(Duration::from_millis(50));
}
}

View File

@ -169,7 +169,7 @@ impl HashDB for OverlayDB {
match k { match k {
Some(&(ref d, rc)) if rc > 0 => Some(d), Some(&(ref d, rc)) if rc > 0 => Some(d),
_ => { _ => {
let memrc = k.map(|&(_, rc)| rc).unwrap_or(0); let memrc = k.map_or(0, |&(_, rc)| rc);
match self.payload(key) { match self.payload(key) {
Some(x) => { Some(x) => {
let (d, rc) = x; let (d, rc) = x;
@ -194,16 +194,11 @@ impl HashDB for OverlayDB {
match k { match k {
Some(&(_, rc)) if rc > 0 => true, Some(&(_, rc)) if rc > 0 => true,
_ => { _ => {
let memrc = k.map(|&(_, rc)| rc).unwrap_or(0); let memrc = k.map_or(0, |&(_, rc)| rc);
match self.payload(key) { match self.payload(key) {
Some(x) => { Some(x) => {
let (_, rc) = x; let (_, rc) = x;
if rc as i32 + memrc > 0 { rc as i32 + memrc > 0
true
}
else {
false
}
} }
// Replace above match arm with this once https://github.com/rust-lang/rust/issues/15287 is done. // Replace above match arm with this once https://github.com/rust-lang/rust/issues/15287 is done.
//Some((d, rc)) if rc + memrc > 0 => true, //Some((d, rc)) if rc + memrc > 0 => true,

View File

@ -7,6 +7,8 @@ use bytes::FromBytesError;
pub enum DecoderError { pub enum DecoderError {
/// TODO [debris] Please document me /// TODO [debris] Please document me
FromBytesError(FromBytesError), FromBytesError(FromBytesError),
/// Given data has additional bytes at the end of the valid RLP fragment.
RlpIsTooBig,
/// TODO [debris] Please document me /// TODO [debris] Please document me
RlpIsTooShort, RlpIsTooShort,
/// TODO [debris] Please document me /// TODO [debris] Please document me
@ -21,6 +23,8 @@ pub enum DecoderError {
RlpListLenWithZeroPrefix, RlpListLenWithZeroPrefix,
/// TODO [debris] Please document me /// TODO [debris] Please document me
RlpInvalidIndirection, RlpInvalidIndirection,
/// Returned when declared length is inconsistent with data specified after
RlpInconsistentLengthAndData
} }
impl StdError for DecoderError { impl StdError for DecoderError {

Some files were not shown because too many files have changed in this diff Show More