diff --git a/Cargo.lock b/Cargo.lock index 38569b271..15d841103 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -302,6 +302,7 @@ dependencies = [ "ethcore-util 1.8.0", "ethjson 0.1.0", "hash 0.1.0", + "heapsize 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "rlp 0.2.0", "rlp_derive 0.1.0", "rustc-hex 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -518,6 +519,7 @@ dependencies = [ "futures 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)", "hardware-wallet 1.8.0", "hash 0.1.0", + "heapsize 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "hyper 0.10.0-a.0 (git+https://github.com/paritytech/hyper)", "itertools 0.5.9 (registry+https://github.com/rust-lang/crates.io-index)", "lazy_static 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", @@ -655,6 +657,7 @@ dependencies = [ "evm 0.1.0", "futures 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)", "hash 0.1.0", + "heapsize 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "itertools 0.5.9 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)", @@ -928,6 +931,7 @@ dependencies = [ "ethjson 0.1.0", "evmjit 1.8.0", "hash 0.1.0", + "heapsize 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "lazy_static 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", "parity-wasm 0.12.1 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/ethcore/Cargo.toml b/ethcore/Cargo.toml index 4af0f315d..6ee332c99 100644 --- a/ethcore/Cargo.toml +++ b/ethcore/Cargo.toml @@ -35,6 +35,7 @@ ethstore = { path = "../ethstore" } evm = { path = "evm" } futures = "0.1" hardware-wallet = { path = "../hw" } +heapsize = "0.4" hyper = { git = "https://github.com/paritytech/hyper", default-features = false } itertools = "0.5" lazy_static = "0.2" diff --git a/ethcore/evm/Cargo.toml b/ethcore/evm/Cargo.toml index 4dc239e19..7d323d622 100644 --- a/ethcore/evm/Cargo.toml +++ b/ethcore/evm/Cargo.toml @@ -10,6 +10,7 @@ common-types = { path = "../types" } ethcore-util = { path = "../../util" } evmjit = { path = "../../evmjit", optional = true } ethjson = { path = "../../json" } +heapsize = "0.4" lazy_static = "0.2" log = "0.3" rlp = { path = "../../util/rlp" } diff --git a/ethcore/evm/src/interpreter/shared_cache.rs b/ethcore/evm/src/interpreter/shared_cache.rs index 6f6620a3b..4f70044dd 100644 --- a/ethcore/evm/src/interpreter/shared_cache.rs +++ b/ethcore/evm/src/interpreter/shared_cache.rs @@ -16,7 +16,8 @@ use std::sync::Arc; use hash::KECCAK_EMPTY; -use util::{H256, HeapSizeOf, Mutex}; +use heapsize::HeapSizeOf; +use util::{H256, Mutex}; use util::cache::MemoryLruCache; use bit_set::BitSet; use super::super::instructions; diff --git a/ethcore/evm/src/lib.rs b/ethcore/evm/src/lib.rs index 5cde3a131..d5b08c8d8 100644 --- a/ethcore/evm/src/lib.rs +++ b/ethcore/evm/src/lib.rs @@ -25,6 +25,7 @@ extern crate rlp; extern crate parity_wasm; extern crate wasm_utils; extern crate ethcore_logger; +extern crate heapsize; extern crate vm; extern crate hash; diff --git a/ethcore/light/Cargo.toml b/ethcore/light/Cargo.toml index f0c8590e3..4e7b3523d 100644 --- a/ethcore/light/Cargo.toml +++ b/ethcore/light/Cargo.toml @@ -19,6 +19,7 @@ ethcore-io = { path = "../../util/io" } ethcore-ipc = { path = "../../ipc/rpc", optional = true } ethcore-devtools = { path = "../../devtools" } evm = { path = "../evm" } +heapsize = "0.4" vm = { path = "../vm" } rlp = { path = "../../util/rlp" } rlp_derive = { path = "../../util/rlp_derive" } diff --git a/ethcore/light/src/cache.rs b/ethcore/light/src/cache.rs index 9a5a3638f..ab416e9b3 100644 --- a/ethcore/light/src/cache.rs +++ b/ethcore/light/src/cache.rs @@ -26,7 +26,8 @@ use ethcore::receipt::Receipt; use stats::Corpus; use time::{SteadyTime, Duration}; -use util::{U256, H256, HeapSizeOf}; +use heapsize::HeapSizeOf; +use util::{U256, H256}; use util::cache::MemoryLruCache; /// Configuration for how much data to cache. diff --git a/ethcore/light/src/client/header_chain.rs b/ethcore/light/src/client/header_chain.rs index e99e89c37..a16cb0c85 100644 --- a/ethcore/light/src/client/header_chain.rs +++ b/ethcore/light/src/client/header_chain.rs @@ -36,7 +36,8 @@ use ethcore::header::Header; use ethcore::ids::BlockId; use rlp::{Encodable, Decodable, DecoderError, RlpStream, Rlp, UntrustedRlp}; -use util::{H256, U256, HeapSizeOf, RwLock}; +use heapsize::HeapSizeOf; +use util::{H256, U256, RwLock}; use util::kvdb::{DBTransaction, KeyValueDB}; use cache::Cache; diff --git a/ethcore/light/src/client/mod.rs b/ethcore/light/src/client/mod.rs index d1cdfe9aa..ee270c977 100644 --- a/ethcore/light/src/client/mod.rs +++ b/ethcore/light/src/client/mod.rs @@ -330,7 +330,7 @@ impl Client { /// Get blockchain mem usage in bytes. pub fn chain_mem_used(&self) -> usize { - use util::HeapSizeOf; + use heapsize::HeapSizeOf; self.chain.heap_size_of_children() } diff --git a/ethcore/light/src/lib.rs b/ethcore/light/src/lib.rs index 0487d5d23..37de03205 100644 --- a/ethcore/light/src/lib.rs +++ b/ethcore/light/src/lib.rs @@ -72,6 +72,7 @@ extern crate ethcore_network as network; extern crate ethcore_util as util; extern crate ethcore; extern crate evm; +extern crate heapsize; extern crate futures; extern crate itertools; extern crate rand; diff --git a/ethcore/res/null_morden_with_reward.json b/ethcore/res/null_morden_with_reward.json new file mode 100644 index 000000000..b7b1c9a0d --- /dev/null +++ b/ethcore/res/null_morden_with_reward.json @@ -0,0 +1,35 @@ +{ + "name": "Morden", + "engine": { + "null": null + }, + "params": { + "gasLimitBoundDivisor": "0x0400", + "accountStartNonce": "0x0", + "maximumExtraDataSize": "0x20", + "minGasLimit": "0x1388", + "networkID" : "0x2", + "blockReward": "0x4563918244F40000" + }, + "genesis": { + "seal": { + "ethereum": { + "nonce": "0x00006d6f7264656e", + "mixHash": "0x00000000000000000000000000000000000000647572616c65787365646c6578" + } + }, + "difficulty": "0x20000", + "author": "0x0000000000000000000000000000000000000000", + "timestamp": "0x00", + "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "extraData": "0x", + "gasLimit": "0x2fefd8" + }, + "accounts": { + "0000000000000000000000000000000000000001": { "balance": "1", "nonce": "1048576", "builtin": { "name": "ecrecover", "pricing": { "linear": { "base": 3000, "word": 0 } } } }, + "0000000000000000000000000000000000000002": { "balance": "1", "nonce": "1048576", "builtin": { "name": "sha256", "pricing": { "linear": { "base": 60, "word": 12 } } } }, + "0000000000000000000000000000000000000003": { "balance": "1", "nonce": "1048576", "builtin": { "name": "ripemd160", "pricing": { "linear": { "base": 600, "word": 120 } } } }, + "0000000000000000000000000000000000000004": { "balance": "1", "nonce": "1048576", "builtin": { "name": "identity", "pricing": { "linear": { "base": 15, "word": 3 } } } }, + "102e61f5d8f9bc71d0ad4a084df4e65e05ce0e1c": { "balance": "1606938044258990275541962092341162602522202993782792835301376", "nonce": "1048576" } + } +} diff --git a/ethcore/src/block.rs b/ethcore/src/block.rs index 35eb41a83..5181dff1f 100644 --- a/ethcore/src/block.rs +++ b/ethcore/src/block.rs @@ -19,7 +19,7 @@ use std::cmp; use std::sync::Arc; use std::collections::HashSet; -use hash::{keccak, KECCAK_NULL_RLP}; +use hash::{keccak, KECCAK_NULL_RLP, KECCAK_EMPTY_LIST_RLP}; use rlp::{UntrustedRlp, RlpStream, Encodable, Decodable, DecoderError}; use util::{Bytes, Address, U256, H256, ordered_trie_root}; @@ -108,7 +108,7 @@ pub struct BlockRefMut<'a> { /// State. pub state: &'a mut State, /// Traces. - pub traces: &'a Option>>, + pub traces: &'a mut Option>>, } /// A set of immutable references to `ExecutedBlock` fields that are publicly accessible. @@ -149,7 +149,7 @@ impl ExecutedBlock { uncles: &self.uncles, state: &mut self.state, receipts: &self.receipts, - traces: &self.traces, + traces: &mut self.traces, } } @@ -197,6 +197,9 @@ pub trait IsBlock { /// Get all uncles in this block. fn uncles(&self) -> &[Header] { &self.block().uncles } + + /// Get tracing enabled flag for this block. + fn tracing_enabled(&self) -> bool { self.block().traces.is_some() } } /// Trait for a object that has a state database. @@ -396,6 +399,7 @@ impl<'x> OpenBlock<'x> { if let Err(e) = s.engine.on_close_block(&mut s.block) { warn!("Encountered error on closing the block: {}", e); } + if let Err(e) = s.block.state.commit() { warn!("Encountered error on state commit: {}", e); } @@ -430,7 +434,7 @@ impl<'x> OpenBlock<'x> { s.block.header.set_transactions_root(ordered_trie_root(s.block.transactions.iter().map(|e| e.rlp_bytes().into_vec()))); } let uncle_bytes = s.block.uncles.iter().fold(RlpStream::new_list(s.block.uncles.len()), |mut s, u| {s.append_raw(&u.rlp(Seal::With), 1); s} ).out(); - if s.block.header.uncles_hash().is_zero() { + if s.block.header.uncles_hash().is_zero() || s.block.header.uncles_hash() == &KECCAK_EMPTY_LIST_RLP { s.block.header.set_uncles_hash(keccak(&uncle_bytes)); } if s.block.header.receipts_root().is_zero() || s.block.header.receipts_root() == &KECCAK_NULL_RLP { diff --git a/ethcore/src/blockchain/blockchain.rs b/ethcore/src/blockchain/blockchain.rs index a39417321..022c93b8f 100644 --- a/ethcore/src/blockchain/blockchain.rs +++ b/ethcore/src/blockchain/blockchain.rs @@ -21,6 +21,7 @@ use std::sync::Arc; use std::mem; use itertools::Itertools; use bloomchain as bc; +use heapsize::HeapSizeOf; use util::*; use rlp::*; use header::*; diff --git a/ethcore/src/blockchain/extras.rs b/ethcore/src/blockchain/extras.rs index 0e2396f8c..69e623a1f 100644 --- a/ethcore/src/blockchain/extras.rs +++ b/ethcore/src/blockchain/extras.rs @@ -25,7 +25,8 @@ use engines::epoch::{Transition as EpochTransition}; use header::BlockNumber; use receipt::Receipt; -use util::{HeapSizeOf, H256, H264, U256}; +use heapsize::HeapSizeOf; +use util::{H256, H264, U256}; use util::kvdb::PREFIX_LEN as DB_PREFIX_LEN; /// Represents index of extra data in database diff --git a/ethcore/src/blooms/bloom.rs b/ethcore/src/blooms/bloom.rs index c79091cb4..f32fc5359 100644 --- a/ethcore/src/blooms/bloom.rs +++ b/ethcore/src/blooms/bloom.rs @@ -15,7 +15,7 @@ // along with Parity. If not, see . use bloomchain as bc; -use util::HeapSizeOf; +use heapsize::HeapSizeOf; use basic_types::LogBloom; /// Helper structure representing bloom of the trace. diff --git a/ethcore/src/blooms/bloom_group.rs b/ethcore/src/blooms/bloom_group.rs index 087f20b6f..1867b7ecc 100644 --- a/ethcore/src/blooms/bloom_group.rs +++ b/ethcore/src/blooms/bloom_group.rs @@ -16,7 +16,7 @@ use bloomchain::group as bc; use rlp::*; -use util::HeapSizeOf; +use heapsize::HeapSizeOf; use super::Bloom; /// Represents group of X consecutive blooms. diff --git a/ethcore/src/blooms/group_position.rs b/ethcore/src/blooms/group_position.rs index bb28b3c1c..b1ea82792 100644 --- a/ethcore/src/blooms/group_position.rs +++ b/ethcore/src/blooms/group_position.rs @@ -15,7 +15,7 @@ // along with Parity. If not, see . use bloomchain::group as bc; -use util::HeapSizeOf; +use heapsize::HeapSizeOf; /// Represents `BloomGroup` position in database. #[derive(PartialEq, Eq, Hash, Clone, Debug)] diff --git a/ethcore/src/encoded.rs b/ethcore/src/encoded.rs index e6eb025e9..e1601d965 100644 --- a/ethcore/src/encoded.rs +++ b/ethcore/src/encoded.rs @@ -28,8 +28,9 @@ use header::{BlockNumber, Header as FullHeader}; use transaction::UnverifiedTransaction; use views; -use util::{Address, H256, H2048, U256, HeapSizeOf}; use hash::keccak; +use heapsize::HeapSizeOf; +use util::{Address, H256, H2048, U256}; use rlp::Rlp; /// Owning header view. diff --git a/ethcore/src/engines/authority_round/mod.rs b/ethcore/src/engines/authority_round/mod.rs index acc1a3834..776aca320 100644 --- a/ethcore/src/engines/authority_round/mod.rs +++ b/ethcore/src/engines/authority_round/mod.rs @@ -31,7 +31,6 @@ use error::{Error, TransactionError, BlockError}; use ethjson; use header::{Header, BlockNumber}; use spec::CommonParams; -use state::CleanupMode; use transaction::UnverifiedTransaction; use super::signer::EngineSigner; @@ -548,17 +547,7 @@ impl Engine for AuthorityRound { /// Apply the block reward on finalisation of the block. fn on_close_block(&self, block: &mut ExecutedBlock) -> Result<(), Error> { - let fields = block.fields_mut(); - // Bestow block reward - let reward = self.params().block_reward; - let res = fields.state.add_balance(fields.header.author(), &reward, CleanupMode::NoEmpty) - .map_err(::error::Error::from) - .and_then(|_| fields.state.commit()); - // Commit state so that we can actually figure out the state root. - if let Err(ref e) = res { - warn!("Encountered error on closing block: {}", e); - } - res + ::engines::common::bestow_block_reward(block, self) } /// Check the number of seal fields. diff --git a/ethcore/src/engines/mod.rs b/ethcore/src/engines/mod.rs index 00f322318..cb13c984a 100644 --- a/ethcore/src/engines/mod.rs +++ b/ethcore/src/engines/mod.rs @@ -399,8 +399,9 @@ pub mod common { use transaction::SYSTEM_ADDRESS; use executive::Executive; use vm::{CallType, ActionParams, ActionValue, EnvInfo, LastHashes}; - use trace::{NoopTracer, NoopVMTracer}; + use trace::{NoopTracer, NoopVMTracer, Tracer, ExecutiveTracer, RewardType}; use state::Substate; + use state::CleanupMode; use util::*; use super::Engine; @@ -469,4 +470,27 @@ pub mod common { } Ok(()) } + + /// Trace rewards on closing block + pub fn bestow_block_reward(block: &mut ExecutedBlock, engine: &E) -> Result<(), Error> { + let fields = block.fields_mut(); + // Bestow block reward + let reward = engine.params().block_reward; + let res = fields.state.add_balance(fields.header.author(), &reward, CleanupMode::NoEmpty) + .map_err(::error::Error::from) + .and_then(|_| fields.state.commit()); + + let block_author = fields.header.author().clone(); + fields.traces.as_mut().map(|mut traces| { + let mut tracer = ExecutiveTracer::default(); + tracer.trace_reward(block_author, engine.params().block_reward, RewardType::Block); + traces.push(tracer.drain()) + }); + + // Commit state so that we can actually figure out the state root. + if let Err(ref e) = res { + warn!("Encountered error on bestowing reward: {}", e); + } + res + } } diff --git a/ethcore/src/engines/null_engine.rs b/ethcore/src/engines/null_engine.rs index 552154580..a52342c59 100644 --- a/ethcore/src/engines/null_engine.rs +++ b/ethcore/src/engines/null_engine.rs @@ -17,10 +17,15 @@ use std::collections::BTreeMap; use util::Address; use builtin::Builtin; +use block::{ExecutedBlock, IsBlock}; +use util::U256; use engines::Engine; use spec::CommonParams; use evm::Schedule; use header::BlockNumber; +use error::Error; +use state::CleanupMode; +use trace::{Tracer, ExecutiveTracer, RewardType}; /// An engine which does not provide any consensus mechanism and does not seal blocks. pub struct NullEngine { @@ -64,4 +69,48 @@ impl Engine for NullEngine { fn snapshot_components(&self) -> Option> { Some(Box::new(::snapshot::PowSnapshot::new(10000, 10000))) } + + fn on_close_block(&self, block: &mut ExecutedBlock) -> Result<(), Error> { + if self.params.block_reward == U256::zero() { + // we don't have to apply reward in this case + return Ok(()) + } + + /// Block reward + let tracing_enabled = block.tracing_enabled(); + let fields = block.fields_mut(); + let mut tracer = ExecutiveTracer::default(); + + let result_block_reward = U256::from(1000000000); + fields.state.add_balance( + fields.header.author(), + &result_block_reward, + CleanupMode::NoEmpty + )?; + + if tracing_enabled { + let block_author = fields.header.author().clone(); + tracer.trace_reward(block_author, result_block_reward, RewardType::Block); + } + + /// Uncle rewards + let result_uncle_reward = U256::from(10000000); + for u in fields.uncles.iter() { + let uncle_author = u.author().clone(); + fields.state.add_balance( + u.author(), + &(result_uncle_reward), + CleanupMode::NoEmpty + )?; + if tracing_enabled { + tracer.trace_reward(uncle_author, result_uncle_reward, RewardType::Uncle); + } + } + + fields.state.commit()?; + if tracing_enabled { + fields.traces.as_mut().map(|mut traces| traces.push(tracer.drain())); + } + Ok(()) + } } diff --git a/ethcore/src/engines/tendermint/mod.rs b/ethcore/src/engines/tendermint/mod.rs index 0f63bac17..f6981c210 100644 --- a/ethcore/src/engines/tendermint/mod.rs +++ b/ethcore/src/engines/tendermint/mod.rs @@ -41,7 +41,6 @@ use account_provider::AccountProvider; use block::*; use spec::CommonParams; use engines::{Engine, Seal, EngineError, ConstructedVerifier}; -use state::CleanupMode; use io::IoService; use super::signer::EngineSigner; use super::validator_set::{ValidatorSet, SimpleList}; @@ -542,17 +541,7 @@ impl Engine for Tendermint { /// Apply the block reward on finalisation of the block. fn on_close_block(&self, block: &mut ExecutedBlock) -> Result<(), Error>{ - let fields = block.fields_mut(); - // Bestow block reward - let reward = self.params().block_reward; - let res = fields.state.add_balance(fields.header.author(), &reward, CleanupMode::NoEmpty) - .map_err(::error::Error::from) - .and_then(|_| fields.state.commit()); - // Commit state so that we can actually figure out the state root. - if let Err(ref e) = res { - warn!("Encountered error on closing block: {}", e); - } - res + ::engines::common::bestow_block_reward(block, self) } fn verify_block_basic(&self, header: &Header, _block: Option<&[u8]>) -> Result<(), Error> { diff --git a/ethcore/src/engines/validator_set/simple_list.rs b/ethcore/src/engines/validator_set/simple_list.rs index 8b8d1942d..40cbe4e93 100644 --- a/ethcore/src/engines/validator_set/simple_list.rs +++ b/ethcore/src/engines/validator_set/simple_list.rs @@ -16,7 +16,8 @@ /// Preconfigured validator list. -use util::{H256, Address, HeapSizeOf}; +use heapsize::HeapSizeOf; +use util::{H256, Address}; use engines::{Call, Engine}; use header::{BlockNumber, Header}; diff --git a/ethcore/src/engines/validator_set/test.rs b/ethcore/src/engines/validator_set/test.rs index 25eeff66e..92472d743 100644 --- a/ethcore/src/engines/validator_set/test.rs +++ b/ethcore/src/engines/validator_set/test.rs @@ -19,7 +19,8 @@ use std::str::FromStr; use std::sync::Arc; use std::sync::atomic::{AtomicUsize, Ordering as AtomicOrdering}; -use util::{Bytes, H256, Address, HeapSizeOf}; +use heapsize::HeapSizeOf; +use util::{Bytes, H256, Address}; use engines::{Call, Engine}; use header::{Header, BlockNumber}; diff --git a/ethcore/src/ethereum/ethash.rs b/ethcore/src/ethereum/ethash.rs index cab05b9ca..abb77aefc 100644 --- a/ethcore/src/ethereum/ethash.rs +++ b/ethcore/src/ethereum/ethash.rs @@ -25,6 +25,7 @@ use block::*; use builtin::Builtin; use vm::EnvInfo; use error::{BlockError, Error, TransactionError}; +use trace::{Tracer, ExecutiveTracer, RewardType}; use header::{Header, BlockNumber}; use state::CleanupMode; use spec::CommonParams; @@ -286,38 +287,60 @@ impl Engine for Arc { /// 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). fn on_close_block(&self, block: &mut ExecutedBlock) -> Result<(), Error> { + use std::ops::Shr; let reward = self.params().block_reward; + let tracing_enabled = block.tracing_enabled(); let fields = block.fields_mut(); let eras_rounds = self.ethash_params.ecip1017_era_rounds; let (eras, reward) = ecip1017_eras_block_reward(eras_rounds, reward, fields.header.number()); + let mut tracer = ExecutiveTracer::default(); // Bestow block reward + let result_block_reward = reward + reward.shr(5) * U256::from(fields.uncles.len()); fields.state.add_balance( fields.header.author(), - &(reward + reward / U256::from(32) * U256::from(fields.uncles.len())), + &result_block_reward, CleanupMode::NoEmpty )?; + if tracing_enabled { + let block_author = fields.header.author().clone(); + tracer.trace_reward(block_author, result_block_reward, RewardType::Block); + } + // Bestow uncle rewards let current_number = fields.header.number(); for u in fields.uncles.iter() { + let uncle_author = u.author().clone(); + let result_uncle_reward: U256; + if eras == 0 { + result_uncle_reward = (reward * U256::from(8 + u.number() - current_number)).shr(3); fields.state.add_balance( u.author(), - &(reward * U256::from(8 + u.number() - current_number) / U256::from(8)), + &result_uncle_reward, CleanupMode::NoEmpty ) } else { + result_uncle_reward = reward.shr(5); fields.state.add_balance( u.author(), - &(reward / U256::from(32)), + &result_uncle_reward, CleanupMode::NoEmpty ) }?; + + // Trace uncle rewards + if tracing_enabled { + tracer.trace_reward(uncle_author, result_uncle_reward, RewardType::Uncle); + } } // Commit state so that we can actually figure out the state root. fields.state.commit()?; + if tracing_enabled { + fields.traces.as_mut().map(|mut traces| traces.push(tracer.drain())); + } Ok(()) } diff --git a/ethcore/src/header.rs b/ethcore/src/header.rs index 14bbe5d85..0a804a6af 100644 --- a/ethcore/src/header.rs +++ b/ethcore/src/header.rs @@ -19,6 +19,7 @@ use std::cmp; use std::cell::RefCell; use hash::{KECCAK_NULL_RLP, KECCAK_EMPTY_LIST_RLP, keccak}; +use heapsize::HeapSizeOf; use util::*; use basic_types::{LogBloom, ZERO_LOGBLOOM}; use time::get_time; diff --git a/ethcore/src/lib.rs b/ethcore/src/lib.rs index 5b5c0e5f6..daab42acf 100644 --- a/ethcore/src/lib.rs +++ b/ethcore/src/lib.rs @@ -102,6 +102,7 @@ extern crate price_info; extern crate rand; extern crate rlp; extern crate hash; +extern crate heapsize; #[macro_use] extern crate rlp_derive; diff --git a/ethcore/src/miner/transaction_queue.rs b/ethcore/src/miner/transaction_queue.rs index 00029e70f..5b29a1607 100644 --- a/ethcore/src/miner/transaction_queue.rs +++ b/ethcore/src/miner/transaction_queue.rs @@ -105,7 +105,8 @@ use std::cmp::Ordering; use std::cmp; use std::collections::{HashSet, HashMap, BTreeSet, BTreeMap}; use linked_hash_map::LinkedHashMap; -use util::{Address, H256, U256, HeapSizeOf}; +use heapsize::HeapSizeOf; +use util::{Address, H256, U256}; use table::Table; use transaction::*; use error::{Error, TransactionError}; diff --git a/ethcore/src/spec/spec.rs b/ethcore/src/spec/spec.rs index 02f16b0fa..e93703871 100644 --- a/ethcore/src/spec/spec.rs +++ b/ethcore/src/spec/spec.rs @@ -484,6 +484,9 @@ impl Spec { /// Create a new Spec which conforms to the Frontier-era Morden chain except that it's a NullEngine consensus. pub fn new_test() -> Spec { load_bundled!("null_morden") } + /// Create a new Spec which conforms to the Frontier-era Morden chain except that it's a NullEngine consensus with applying reward on block close. + pub fn new_test_with_reward() -> Spec { load_bundled!("null_morden_with_reward") } + /// Create a new Spec which is a NullEngine consensus with a premine of address whose secret is keccak(''). pub fn new_null() -> Spec { load_bundled!("null") } diff --git a/ethcore/src/tests/mod.rs b/ethcore/src/tests/mod.rs index 31f195725..c86240f33 100644 --- a/ethcore/src/tests/mod.rs +++ b/ethcore/src/tests/mod.rs @@ -17,6 +17,7 @@ pub mod helpers; mod client; mod evm; +mod trace; #[cfg(feature="ipc")] mod rpc; diff --git a/ethcore/src/tests/trace.rs b/ethcore/src/tests/trace.rs new file mode 100644 index 000000000..0429054fb --- /dev/null +++ b/ethcore/src/tests/trace.rs @@ -0,0 +1,207 @@ +// Copyright 2015-2017 Parity Technologies (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +//! Client tests of tracing + +use ethkey::KeyPair; +use hash::keccak; +use block::*; +use util::*; +use io::*; +use spec::*; +use client::*; +use tests::helpers::*; +use devtools::RandomTempPath; +use client::{BlockChainClient, Client, ClientConfig}; +use util::kvdb::{Database, DatabaseConfig}; +use std::sync::Arc; +use header::Header; +use miner::Miner; +use transaction::{Action, Transaction}; +use views::BlockView; +use trace::{RewardType, LocalizedTrace}; +use trace::trace::Action::Reward; + +#[test] +fn can_trace_block_and_uncle_reward() { + let dir = RandomTempPath::new(); + let spec = Spec::new_test_with_reward(); + let engine = &*spec.engine; + + // Create client + let db_config = DatabaseConfig::with_columns(::db::NUM_COLUMNS); + let mut client_config = ClientConfig::default(); + client_config.tracing.enabled = true; + let client_db = Arc::new(Database::open(&db_config, dir.as_path().to_str().unwrap()).unwrap()); + let client = Client::new( + client_config, + &spec, + client_db, + Arc::new(Miner::with_spec(&spec)), + IoChannel::disconnected(), + ).unwrap(); + + // Create test data: + // genesis + // | + // root_block + // | + // parent_block + // | + // block with transaction and uncle + + let genesis_header = spec.genesis_header(); + let mut db = spec.ensure_db_good(get_temp_state_db(), &Default::default()).unwrap(); + let mut rolling_timestamp = 40; + let mut last_hashes = vec![]; + let mut last_header = genesis_header.clone(); + last_hashes.push(last_header.hash()); + + let kp = KeyPair::from_secret_slice(&keccak("")).unwrap(); + let author = kp.address(); + + // Add root block first + let mut root_block = OpenBlock::new( + engine, + Default::default(), + false, + db, + &last_header, + Arc::new(last_hashes.clone()), + author.clone(), + (3141562.into(), 31415620.into()), + vec![], + false, + ).unwrap(); + root_block.set_difficulty(U256::from(0x20000)); + rolling_timestamp += 10; + root_block.set_timestamp(rolling_timestamp); + + let root_block = root_block.close_and_lock().seal(engine, vec![]).unwrap(); + + if let Err(e) = client.import_block(root_block.rlp_bytes()) { + panic!("error importing block which is valid by definition: {:?}", e); + } + + last_header = BlockView::new(&root_block.rlp_bytes()).header(); + let root_header = last_header.clone(); + db = root_block.drain(); + + last_hashes.push(last_header.hash()); + + // Add parent block + let mut parent_block = OpenBlock::new( + engine, + Default::default(), + false, + db, + &last_header, + Arc::new(last_hashes.clone()), + author.clone(), + (3141562.into(), 31415620.into()), + vec![], + false, + ).unwrap(); + parent_block.set_difficulty(U256::from(0x20000)); + rolling_timestamp += 10; + parent_block.set_timestamp(rolling_timestamp); + + let parent_block = parent_block.close_and_lock().seal(engine, vec![]).unwrap(); + + if let Err(e) = client.import_block(parent_block.rlp_bytes()) { + panic!("error importing block which is valid by definition: {:?}", e); + } + + last_header = BlockView::new(&parent_block.rlp_bytes()).header(); + db = parent_block.drain(); + + last_hashes.push(last_header.hash()); + + // Add testing block with transaction and uncle + let mut block = OpenBlock::new( + engine, + Default::default(), + true, + db, + &last_header, + Arc::new(last_hashes.clone()), + author.clone(), + (3141562.into(), 31415620.into()), + vec![], + false + ).unwrap(); + block.set_difficulty(U256::from(0x20000)); + rolling_timestamp += 10; + block.set_timestamp(rolling_timestamp); + + let mut n = 0; + for _ in 0..1 { + block.push_transaction(Transaction { + nonce: n.into(), + gas_price: 10000.into(), + gas: 100000.into(), + action: Action::Create, + data: vec![], + value: U256::zero(), + }.sign(kp.secret(), Some(spec.network_id())), None).unwrap(); + n += 1; + } + + let mut uncle = Header::new(); + let uncle_author: Address = "ef2d6d194084c2de36e0dabfce45d046b37d1106".into(); + uncle.set_author(uncle_author); + uncle.set_parent_hash(root_header.hash()); + uncle.set_gas_limit(U256::from(50_000)); + uncle.set_number(root_header.number() + 1); + uncle.set_timestamp(rolling_timestamp); + block.push_uncle(uncle).unwrap(); + + let block = block.close_and_lock().seal(engine, vec![]).unwrap(); + + let res = client.import_block(block.rlp_bytes()); + if res.is_err() { + panic!("error importing block: {:#?}", res.err().unwrap()); + } + + block.drain(); + client.flush_queue(); + client.import_verified_blocks(); + + // Test0. Check overall filter + let filter = TraceFilter { + range: (BlockId::Number(1)..BlockId::Number(3)), + from_address: vec![], + to_address: vec![], + }; + + let traces = client.filter_traces(filter); + assert!(traces.is_some(), "Filtered traces should be present"); + let traces_vec = traces.unwrap(); + let block_reward_traces: Vec = traces_vec.clone().into_iter().filter(|trace| match (trace).action { + Reward(ref a) => a.reward_type == RewardType::Block, + _ => false, + }).collect(); + assert_eq!(block_reward_traces.len(), 3); + let uncle_reward_traces: Vec = traces_vec.clone().into_iter().filter(|trace| match (trace).action { + Reward(ref a) => a.reward_type == RewardType::Uncle, + _ => false, + }).collect(); + assert_eq!(uncle_reward_traces.len(), 1); + + // Test1. Check block filter + let traces = client.block_traces(BlockId::Number(3)); + assert_eq!(traces.unwrap().len(), 3); +} diff --git a/ethcore/src/trace/db.rs b/ethcore/src/trace/db.rs index fe90ffe41..0fe057a64 100644 --- a/ethcore/src/trace/db.rs +++ b/ethcore/src/trace/db.rs @@ -20,7 +20,8 @@ use std::collections::{HashMap, VecDeque}; use std::sync::Arc; use bloomchain::{Number, Config as BloomConfig}; use bloomchain::group::{BloomGroupDatabase, BloomGroupChain, GroupPosition, BloomGroup}; -use util::{H256, H264, KeyValueDB, DBTransaction, RwLock, HeapSizeOf}; +use heapsize::HeapSizeOf; +use util::{H256, H264, KeyValueDB, DBTransaction, RwLock}; use header::BlockNumber; use trace::{LocalizedTrace, Config, Filter, Database as TraceDatabase, ImportRequest, DatabaseExtras}; use db::{self, Key, Writable, Readable, CacheUpdatePolicy}; @@ -215,8 +216,11 @@ impl TraceDB where T: DatabaseExtras { block_number: BlockNumber, tx_number: usize ) -> Vec { - let tx_hash = self.extras.transaction_hash(block_number, tx_number) - .expect("Expected to find transaction hash. Database is probably corrupted"); + let (trace_tx_number, trace_tx_hash) = match self.extras.transaction_hash(block_number, tx_number) { + Some(hash) => (Some(tx_number), Some(hash.clone())), + //None means trace without transaction (reward) + None => (None, None), + }; let flat_traces: Vec = traces.into(); flat_traces.into_iter() @@ -227,8 +231,8 @@ impl TraceDB where T: DatabaseExtras { result: trace.result, subtraces: trace.subtraces, trace_address: trace.trace_address.into_iter().collect(), - transaction_number: tx_number, - transaction_hash: tx_hash.clone(), + transaction_number: trace_tx_number, + transaction_hash: trace_tx_hash, block_number: block_number, block_hash: block_hash }), @@ -321,8 +325,8 @@ impl TraceDatabase for TraceDB where T: DatabaseExtras { result: trace.result, subtraces: trace.subtraces, trace_address: trace.trace_address.into_iter().collect(), - transaction_number: tx_position, - transaction_hash: tx_hash, + transaction_number: Some(tx_position), + transaction_hash: Some(tx_hash), block_number: block_number, block_hash: block_hash, } @@ -345,8 +349,8 @@ impl TraceDatabase for TraceDB where T: DatabaseExtras { result: trace.result, subtraces: trace.subtraces, trace_address: trace.trace_address.into_iter().collect(), - transaction_number: tx_position, - transaction_hash: tx_hash.clone(), + transaction_number: Some(tx_position), + transaction_hash: Some(tx_hash.clone()), block_number: block_number, block_hash: block_hash }) @@ -363,8 +367,11 @@ impl TraceDatabase for TraceDB where T: DatabaseExtras { .map(Into::>::into) .enumerate() .flat_map(|(tx_position, traces)| { - let tx_hash = self.extras.transaction_hash(block_number, tx_position) - .expect("Expected to find transaction hash. Database is probably corrupted"); + let (trace_tx_number, trace_tx_hash) = match self.extras.transaction_hash(block_number, tx_position) { + Some(hash) => (Some(tx_position), Some(hash.clone())), + //None means trace without transaction (reward) + None => (None, None), + }; traces.into_iter() .map(|trace| LocalizedTrace { @@ -372,8 +379,8 @@ impl TraceDatabase for TraceDB where T: DatabaseExtras { result: trace.result, subtraces: trace.subtraces, trace_address: trace.trace_address.into_iter().collect(), - transaction_number: tx_position, - transaction_hash: tx_hash.clone(), + transaction_number: trace_tx_number, + transaction_hash: trace_tx_hash, block_number: block_number, block_hash: block_hash, }) @@ -543,8 +550,8 @@ mod tests { result: Res::FailedCall(TraceError::OutOfGas), trace_address: vec![], subtraces: 0, - transaction_number: 0, - transaction_hash: tx_hash, + transaction_number: Some(0), + transaction_hash: Some(tx_hash), block_number: block_number, block_hash: block_hash, } diff --git a/ethcore/src/trace/executive_tracer.rs b/ethcore/src/trace/executive_tracer.rs index ba4d0eff9..57e8eabb8 100644 --- a/ethcore/src/trace/executive_tracer.rs +++ b/ethcore/src/trace/executive_tracer.rs @@ -18,7 +18,7 @@ use util::{Bytes, Address, U256}; use vm::ActionParams; -use trace::trace::{Call, Create, Action, Res, CreateResult, CallResult, VMTrace, VMOperation, VMExecutedOperation, MemoryDiff, StorageDiff, Suicide}; +use trace::trace::{Call, Create, Action, Res, CreateResult, CallResult, VMTrace, VMOperation, VMExecutedOperation, MemoryDiff, StorageDiff, Suicide, Reward, RewardType}; use trace::{Tracer, VMTracer, FlatTrace, TraceError}; /// Simple executive tracer. Traces all calls and creates. Ignores delegatecalls. @@ -151,15 +151,22 @@ impl Tracer for ExecutiveTracer { fn trace_suicide(&mut self, address: Address, balance: U256, refund_address: Address) { let trace = FlatTrace { subtraces: 0, - action: Action::Suicide(Suicide { - address: address, - refund_address: refund_address, - balance: balance, - }), + action: Action::Suicide(Suicide { address, refund_address, balance } ), result: Res::None, trace_address: Default::default(), }; - debug!(target: "trace", "Traced failed suicide {:?}", trace); + debug!(target: "trace", "Traced suicide {:?}", trace); + self.traces.push(trace); + } + + fn trace_reward(&mut self, author: Address, value: U256, reward_type: RewardType) { + let trace = FlatTrace { + subtraces: 0, + action: Action::Reward(Reward { author, value, reward_type } ), + result: Res::None, + trace_address: Default::default(), + }; + debug!(target: "trace", "Traced reward {:?}", trace); self.traces.push(trace); } diff --git a/ethcore/src/trace/mod.rs b/ethcore/src/trace/mod.rs index c749bfd82..cbec8a149 100644 --- a/ethcore/src/trace/mod.rs +++ b/ethcore/src/trace/mod.rs @@ -33,7 +33,7 @@ pub use self::localized::LocalizedTrace; pub use self::types::{filter, flat, localized, trace}; pub use self::types::error::Error as TraceError; -pub use self::types::trace::{VMTrace, VMOperation, VMExecutedOperation, MemoryDiff, StorageDiff}; +pub use self::types::trace::{VMTrace, VMOperation, VMExecutedOperation, MemoryDiff, StorageDiff, RewardType}; pub use self::types::flat::{FlatTrace, FlatTransactionTraces, FlatBlockTraces}; pub use self::types::filter::{Filter, AddressesFilter}; @@ -81,6 +81,9 @@ pub trait Tracer: Send { /// Stores suicide info. fn trace_suicide(&mut self, address: Address, balance: U256, refund_address: Address); + /// Stores reward info. + fn trace_reward(&mut self, author: Address, value: U256, reward_type: RewardType); + /// Spawn subtracer which will be used to trace deeper levels of execution. fn subtracer(&self) -> Self where Self: Sized; diff --git a/ethcore/src/trace/noop_tracer.rs b/ethcore/src/trace/noop_tracer.rs index 03d6f57a0..c9bd5f2e8 100644 --- a/ethcore/src/trace/noop_tracer.rs +++ b/ethcore/src/trace/noop_tracer.rs @@ -19,7 +19,7 @@ use util::{Bytes, Address, U256}; use vm::ActionParams; use trace::{Tracer, VMTracer, FlatTrace, TraceError}; -use trace::trace::{Call, Create, VMTrace}; +use trace::trace::{Call, Create, VMTrace, RewardType}; /// Nonoperative tracer. Does not trace anything. pub struct NoopTracer; @@ -58,6 +58,9 @@ impl Tracer for NoopTracer { fn trace_suicide(&mut self, _address: Address, _balance: U256, _refund_address: Address) { } + fn trace_reward(&mut self, _: Address, _: U256, _: RewardType) { + } + fn subtracer(&self) -> Self { NoopTracer } diff --git a/ethcore/src/trace/types/filter.rs b/ethcore/src/trace/types/filter.rs index 5c92dcedc..f7e2d2140 100644 --- a/ethcore/src/trace/types/filter.rs +++ b/ethcore/src/trace/types/filter.rs @@ -113,7 +113,7 @@ impl Filter { let from_matches = self.from_address.matches(&call.from); let to_matches = self.to_address.matches(&call.to); from_matches && to_matches - } + }, Action::Create(ref create) => { let from_matches = self.from_address.matches(&create.from); @@ -128,7 +128,10 @@ impl Filter { let from_matches = self.from_address.matches(&suicide.address); let to_matches = self.to_address.matches(&suicide.refund_address); from_matches && to_matches - } + }, + Action::Reward(ref reward) => { + self.to_address.matches(&reward.author) + }, } } } @@ -138,9 +141,9 @@ mod tests { use util::Address; use hash::keccak; use bloomable::Bloomable; - use trace::trace::{Action, Call, Res, Create, CreateResult, Suicide}; + use trace::trace::{Action, Call, Res, Create, CreateResult, Suicide, Reward}; use trace::flat::FlatTrace; - use trace::{Filter, AddressesFilter, TraceError}; + use trace::{Filter, AddressesFilter, TraceError, RewardType}; use evm::CallType; #[test] @@ -341,5 +344,24 @@ mod tests { assert!(f4.matches(&trace)); assert!(f5.matches(&trace)); assert!(!f6.matches(&trace)); + + let trace = FlatTrace { + action: Action::Reward(Reward { + author: 2.into(), + value: 100.into(), + reward_type: RewardType::Block, + }), + result: Res::None, + trace_address: vec![].into_iter().collect(), + subtraces: 0 + }; + + assert!(f0.matches(&trace)); + assert!(f1.matches(&trace)); + assert!(f2.matches(&trace)); + assert!(f3.matches(&trace)); + assert!(f4.matches(&trace)); + assert!(f5.matches(&trace)); + assert!(!f6.matches(&trace)); } } diff --git a/ethcore/src/trace/types/flat.rs b/ethcore/src/trace/types/flat.rs index 8b65f1f4c..0ea8ede27 100644 --- a/ethcore/src/trace/types/flat.rs +++ b/ethcore/src/trace/types/flat.rs @@ -18,7 +18,7 @@ use std::collections::VecDeque; use rlp::*; -use util::HeapSizeOf; +use heapsize::HeapSizeOf; use basic_types::LogBloom; use super::trace::{Action, Res}; @@ -138,8 +138,9 @@ impl Into> for FlatBlockTraces { mod tests { use rlp::*; use super::{FlatBlockTraces, FlatTransactionTraces, FlatTrace}; - use trace::trace::{Action, Res, CallResult, Call, Suicide}; + use trace::trace::{Action, Res, CallResult, Call, Suicide, Reward}; use evm::CallType; + use trace::RewardType; #[test] fn encode_flat_transaction_traces() { @@ -214,9 +215,32 @@ mod tests { subtraces: 0, }; + let flat_trace3 = FlatTrace { + action: Action::Reward(Reward { + author: "412fda7643b37d436cb40628f6dbbb80a07267ed".parse().unwrap(), + value: 10.into(), + reward_type: RewardType::Uncle, + }), + result: Res::None, + trace_address: vec![0].into_iter().collect(), + subtraces: 0, + }; + + let flat_trace4 = FlatTrace { + action: Action::Reward(Reward { + author: "412fda7643b37d436cb40628f6dbbb80a07267ed".parse().unwrap(), + value: 10.into(), + reward_type: RewardType::Block, + }), + result: Res::None, + trace_address: vec![0].into_iter().collect(), + subtraces: 0, + }; + let block_traces = FlatBlockTraces(vec![ FlatTransactionTraces(vec![flat_trace]), - FlatTransactionTraces(vec![flat_trace1, flat_trace2]) + FlatTransactionTraces(vec![flat_trace1, flat_trace2]), + FlatTransactionTraces(vec![flat_trace3, flat_trace4]) ]); let encoded = ::rlp::encode(&block_traces); diff --git a/ethcore/src/trace/types/localized.rs b/ethcore/src/trace/types/localized.rs index 39a4b08cc..2d4850a8a 100644 --- a/ethcore/src/trace/types/localized.rs +++ b/ethcore/src/trace/types/localized.rs @@ -34,9 +34,9 @@ pub struct LocalizedTrace { /// [index in root, index in first CALL, index in second CALL, ...] pub trace_address: Vec, /// Transaction number within the block. - pub transaction_number: usize, + pub transaction_number: Option, /// Signed transaction hash. - pub transaction_hash: H256, + pub transaction_hash: Option, /// Block number. pub block_number: BlockNumber, /// Block hash. diff --git a/ethcore/src/trace/types/trace.rs b/ethcore/src/trace/types/trace.rs index af0fa5f6e..6a4345adb 100644 --- a/ethcore/src/trace/types/trace.rs +++ b/ethcore/src/trace/types/trace.rs @@ -128,6 +128,77 @@ impl Create { } } +/// Reward type. +#[derive(Debug, PartialEq, Clone)] +#[cfg_attr(feature = "ipc", binary)] +pub enum RewardType { + /// Block + Block, + /// Uncle + Uncle, +} + +impl Encodable for RewardType { + fn rlp_append(&self, s: &mut RlpStream) { + let v = match *self { + RewardType::Block => 0u32, + RewardType::Uncle => 1, + }; + Encodable::rlp_append(&v, s); + } +} + +impl Decodable for RewardType { + fn decode(rlp: &UntrustedRlp) -> Result { + rlp.as_val().and_then(|v| Ok(match v { + 0u32 => RewardType::Block, + 1 => RewardType::Uncle, + _ => return Err(DecoderError::Custom("Invalid value of RewardType item")), + })) + } +} + +/// Reward action +#[derive(Debug, Clone, PartialEq)] +#[cfg_attr(feature = "ipc", binary)] +pub struct Reward { + /// Author's address. + pub author: Address, + /// Reward amount. + pub value: U256, + /// Reward type. + pub reward_type: RewardType, +} + +impl Reward { + /// Return reward action bloom. + pub fn bloom(&self) -> LogBloom { + LogBloom::from_bloomed(&keccak(&self.author)) + } +} + +impl Encodable for Reward { + fn rlp_append(&self, s: &mut RlpStream) { + s.begin_list(3); + s.append(&self.author); + s.append(&self.value); + s.append(&self.reward_type); + } +} + +impl Decodable for Reward { + fn decode(rlp: &UntrustedRlp) -> Result { + let res = Reward { + author: rlp.val_at(0)?, + value: rlp.val_at(1)?, + reward_type: rlp.val_at(2)?, + }; + + Ok(res) + } +} + + /// Suicide action. #[derive(Debug, Clone, PartialEq, RlpEncodable, RlpDecodable)] #[cfg_attr(feature = "ipc", binary)] @@ -158,6 +229,8 @@ pub enum Action { Create(Create), /// Suicide. Suicide(Suicide), + /// Reward + Reward(Reward), } impl Encodable for Action { @@ -175,7 +248,12 @@ impl Encodable for Action { Action::Suicide(ref suicide) => { s.append(&2u8); s.append(suicide); + }, + Action::Reward(ref reward) => { + s.append(&3u8); + s.append(reward); } + } } } @@ -187,6 +265,7 @@ impl Decodable for Action { 0 => rlp.val_at(1).map(Action::Call), 1 => rlp.val_at(1).map(Action::Create), 2 => rlp.val_at(1).map(Action::Suicide), + 3 => rlp.val_at(1).map(Action::Reward), _ => Err(DecoderError::Custom("Invalid action type.")), } } @@ -199,6 +278,7 @@ impl Action { Action::Call(ref call) => call.bloom(), Action::Create(ref create) => create.bloom(), Action::Suicide(ref suicide) => suicide.bloom(), + Action::Reward(ref reward) => reward.bloom(), } } } diff --git a/ethcore/src/transaction.rs b/ethcore/src/transaction.rs index e68021768..6e3302f0a 100644 --- a/ethcore/src/transaction.rs +++ b/ethcore/src/transaction.rs @@ -19,7 +19,8 @@ use std::ops::Deref; use rlp::*; use hash::keccak; -use util::{H256, Address, U256, Bytes, HeapSizeOf}; +use heapsize::HeapSizeOf; +use util::{H256, Address, U256, Bytes}; use ethkey::{Signature, Secret, Public, recover, public_to_address, Error as EthkeyError}; use error::*; use evm::Schedule; diff --git a/ethcore/src/verification/queue/kind.rs b/ethcore/src/verification/queue/kind.rs index 6ab6d692e..4b547c1b7 100644 --- a/ethcore/src/verification/queue/kind.rs +++ b/ethcore/src/verification/queue/kind.rs @@ -19,7 +19,8 @@ use engines::Engine; use error::Error; -use util::{HeapSizeOf, H256, U256}; +use heapsize::HeapSizeOf; +use util::{H256, U256}; pub use self::blocks::Blocks; pub use self::headers::Headers; @@ -72,7 +73,8 @@ pub mod blocks { use header::Header; use verification::{PreverifiedBlock, verify_block_basic, verify_block_unordered}; - use util::{Bytes, HeapSizeOf, H256, U256}; + use heapsize::HeapSizeOf; + use util::{Bytes, H256, U256}; /// A mode for verifying blocks. pub struct Blocks; diff --git a/ethcore/src/verification/queue/mod.rs b/ethcore/src/verification/queue/mod.rs index ce0cb4179..8fc4a5919 100644 --- a/ethcore/src/verification/queue/mod.rs +++ b/ethcore/src/verification/queue/mod.rs @@ -22,6 +22,7 @@ use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering as AtomicOrdering}; use std::sync::{Condvar as SCondvar, Mutex as SMutex, Arc}; use std::cmp; use std::collections::{VecDeque, HashSet, HashMap}; +use heapsize::HeapSizeOf; use util::*; use io::*; use error::*; diff --git a/ethcore/src/verification/verification.rs b/ethcore/src/verification/verification.rs index 11fd01d7b..f9290309c 100644 --- a/ethcore/src/verification/verification.rs +++ b/ethcore/src/verification/verification.rs @@ -23,6 +23,7 @@ use std::collections::HashSet; use hash::keccak; +use heapsize::HeapSizeOf; use util::*; use engines::Engine; use error::{BlockError, Error}; diff --git a/ethcore/types/Cargo.toml b/ethcore/types/Cargo.toml index bb43d0d49..458d4d54e 100644 --- a/ethcore/types/Cargo.toml +++ b/ethcore/types/Cargo.toml @@ -11,6 +11,7 @@ ethcore-util = { path = "../../util" } ethjson = { path = "../../json" } bloomable = { path = "../../util/bloomable" } hash = { path = "../../util/hash" } +heapsize = "0.4" [dev-dependencies] rustc-hex= "1.0" diff --git a/ethcore/types/src/lib.rs b/ethcore/types/src/lib.rs index 4050bf737..62ff153a0 100644 --- a/ethcore/types/src/lib.rs +++ b/ethcore/types/src/lib.rs @@ -23,6 +23,7 @@ extern crate rlp; extern crate rlp_derive; extern crate bloomable; extern crate hash; +extern crate heapsize; #[cfg(test)] extern crate rustc_hex; diff --git a/ethcore/types/src/log_entry.rs b/ethcore/types/src/log_entry.rs index 40b0f3ea9..f2f01c2dc 100644 --- a/ethcore/types/src/log_entry.rs +++ b/ethcore/types/src/log_entry.rs @@ -18,7 +18,8 @@ use std::ops::Deref; use hash::keccak; -use util::{H256, Address, Bytes, HeapSizeOf}; +use heapsize::HeapSizeOf; +use util::{H256, Address, Bytes}; use bloomable::Bloomable; use {BlockNumber}; diff --git a/ethcore/types/src/receipt.rs b/ethcore/types/src/receipt.rs index 81439ecdf..63a122179 100644 --- a/ethcore/types/src/receipt.rs +++ b/ethcore/types/src/receipt.rs @@ -17,7 +17,7 @@ //! Receipt use util::{H256, U256, Address}; -use util::HeapSizeOf; +use heapsize::HeapSizeOf; use rlp::*; use {BlockNumber}; diff --git a/json/src/spec/params.rs b/json/src/spec/params.rs index de33039c4..b90eef5ea 100644 --- a/json/src/spec/params.rs +++ b/json/src/spec/params.rs @@ -102,6 +102,9 @@ pub struct Params { pub block_reward: Option, /// See `CommonParams` docs. pub registrar: Option
, + /// Apply reward flag + #[serde(rename="applyReward")] + pub apply_reward: Option, /// Node permission contract address. #[serde(rename="nodePermissionContract")] pub node_permission_contract: Option
, diff --git a/rpc/src/v1/tests/mocked/traces.rs b/rpc/src/v1/tests/mocked/traces.rs index cd3ae3666..71b0ea3bb 100644 --- a/rpc/src/v1/tests/mocked/traces.rs +++ b/rpc/src/v1/tests/mocked/traces.rs @@ -47,8 +47,8 @@ fn io() -> Tester { result: Res::None, subtraces: 0, trace_address: vec![0], - transaction_number: 0, - transaction_hash: 5.into(), + transaction_number: Some(0), + transaction_hash: Some(5.into()), block_number: 10, block_hash: 10.into(), }]); diff --git a/rpc/src/v1/types/trace.rs b/rpc/src/v1/types/trace.rs index f9cf0bb40..e09d95932 100644 --- a/rpc/src/v1/types/trace.rs +++ b/rpc/src/v1/types/trace.rs @@ -299,6 +299,49 @@ impl From for Call { } } +/// Reward type. +#[derive(Debug, Serialize)] +pub enum RewardType { + /// Block + #[serde(rename="block")] + Block, + /// Uncle + #[serde(rename="uncle")] + Uncle, +} + +impl From for RewardType { + fn from(c: trace::RewardType) -> Self { + match c { + trace::RewardType::Block => RewardType::Block, + trace::RewardType::Uncle => RewardType::Uncle, + } + } +} + + +/// Reward action +#[derive(Debug, Serialize)] +pub struct Reward { + /// Author's address. + pub author: H160, + /// Reward amount. + pub value: U256, + /// Reward type. + #[serde(rename="rewardType")] + pub reward_type: RewardType, +} + +impl From for Reward { + fn from(r: trace::Reward) -> Self { + Reward { + author: r.author.into(), + value: r.value.into(), + reward_type: r.reward_type.into(), + } + } +} + /// Suicide #[derive(Debug, Serialize)] pub struct Suicide { @@ -330,6 +373,8 @@ pub enum Action { Create(Create), /// Suicide Suicide(Suicide), + /// Reward + Reward(Reward), } impl From for Action { @@ -338,6 +383,7 @@ impl From for Action { trace::Action::Call(call) => Action::Call(call.into()), trace::Action::Create(create) => Action::Create(create.into()), trace::Action::Suicide(suicide) => Action::Suicide(suicide.into()), + trace::Action::Reward(reward) => Action::Reward(reward.into()), } } } @@ -422,9 +468,9 @@ pub struct LocalizedTrace { /// Subtraces subtraces: usize, /// Transaction position - transaction_position: usize, + transaction_position: Option, /// Transaction hash - transaction_hash: H256, + transaction_hash: Option, /// Block Number block_number: u64, /// Block Hash @@ -449,6 +495,10 @@ impl Serialize for LocalizedTrace { struc.serialize_field("type", "suicide")?; struc.serialize_field("action", suicide)?; }, + Action::Reward(ref reward) => { + struc.serialize_field("type", "reward")?; + struc.serialize_field("action", reward)?; + }, } match self.result { @@ -477,8 +527,8 @@ impl From for LocalizedTrace { result: t.result.into(), trace_address: t.trace_address.into_iter().map(Into::into).collect(), subtraces: t.subtraces.into(), - transaction_position: t.transaction_number.into(), - transaction_hash: t.transaction_hash.into(), + transaction_position: t.transaction_number.map(Into::into), + transaction_hash: t.transaction_hash.map(Into::into), block_number: t.block_number.into(), block_hash: t.block_hash.into(), } @@ -516,6 +566,10 @@ impl Serialize for Trace { struc.serialize_field("type", "suicide")?; struc.serialize_field("action", suicide)?; }, + Action::Reward(ref reward) => { + struc.serialize_field("type", "reward")?; + struc.serialize_field("action", reward)?; + }, } match self.result { @@ -607,8 +661,8 @@ mod tests { }), trace_address: vec![10], subtraces: 1, - transaction_position: 11, - transaction_hash: 12.into(), + transaction_position: Some(11), + transaction_hash: Some(12.into()), block_number: 13, block_hash: 14.into(), }; @@ -630,8 +684,8 @@ mod tests { result: Res::FailedCall(TraceError::OutOfGas), trace_address: vec![10], subtraces: 1, - transaction_position: 11, - transaction_hash: 12.into(), + transaction_position: Some(11), + transaction_hash: Some(12.into()), block_number: 13, block_hash: 14.into(), }; @@ -655,8 +709,8 @@ mod tests { }), trace_address: vec![10], subtraces: 1, - transaction_position: 11, - transaction_hash: 12.into(), + transaction_position: Some(11), + transaction_hash: Some(12.into()), block_number: 13, block_hash: 14.into(), }; @@ -676,8 +730,8 @@ mod tests { result: Res::FailedCreate(TraceError::OutOfGas), trace_address: vec![10], subtraces: 1, - transaction_position: 11, - transaction_hash: 12.into(), + transaction_position: Some(11), + transaction_hash: Some(12.into()), block_number: 13, block_hash: 14.into(), }; @@ -696,8 +750,8 @@ mod tests { result: Res::None, trace_address: vec![10], subtraces: 1, - transaction_position: 11, - transaction_hash: 12.into(), + transaction_position: Some(11), + transaction_hash: Some(12.into()), block_number: 13, block_hash: 14.into(), }; @@ -705,6 +759,26 @@ mod tests { assert_eq!(serialized, r#"{"type":"suicide","action":{"address":"0x0000000000000000000000000000000000000004","refundAddress":"0x0000000000000000000000000000000000000006","balance":"0x7"},"result":null,"traceAddress":[10],"subtraces":1,"transactionPosition":11,"transactionHash":"0x000000000000000000000000000000000000000000000000000000000000000c","blockNumber":13,"blockHash":"0x000000000000000000000000000000000000000000000000000000000000000e"}"#); } + #[test] + fn test_trace_reward_serialize() { + let t = LocalizedTrace { + action: Action::Reward(Reward { + author: 4.into(), + value: 6.into(), + reward_type: RewardType::Block, + }), + result: Res::None, + trace_address: vec![10], + subtraces: 1, + transaction_position: None, + transaction_hash: None, + block_number: 13, + block_hash: 14.into(), + }; + let serialized = serde_json::to_string(&t).unwrap(); + assert_eq!(serialized, r#"{"type":"reward","action":{"author":"0x0000000000000000000000000000000000000004","value":"0x6","rewardType":"block"},"result":null,"traceAddress":[10],"subtraces":1,"transactionPosition":null,"transactionHash":null,"blockNumber":13,"blockHash":"0x000000000000000000000000000000000000000000000000000000000000000e"}"#); + } + #[test] fn test_vmtrace_serialize() { let t = VMTrace { diff --git a/sync/src/block_sync.rs b/sync/src/block_sync.rs index 2cf8f974c..9f341d6de 100644 --- a/sync/src/block_sync.rs +++ b/sync/src/block_sync.rs @@ -20,6 +20,7 @@ use std::collections::{HashSet, VecDeque}; use std::cmp; +use heapsize::HeapSizeOf; use util::*; use rlp::*; use ethcore::views::{BlockView}; diff --git a/sync/src/blocks.rs b/sync/src/blocks.rs index b953db09a..77cb206c8 100644 --- a/sync/src/blocks.rs +++ b/sync/src/blocks.rs @@ -18,6 +18,7 @@ use std::collections::{HashSet, HashMap}; use std::collections::hash_map::Entry; use smallvec::SmallVec; use hash::{keccak, KECCAK_NULL_RLP, KECCAK_EMPTY_LIST_RLP}; +use heapsize::HeapSizeOf; use util::*; use rlp::*; use network::NetworkError; diff --git a/sync/src/chain.rs b/sync/src/chain.rs index 9c62a2a61..432a0d0f5 100644 --- a/sync/src/chain.rs +++ b/sync/src/chain.rs @@ -92,6 +92,7 @@ use std::collections::{HashSet, HashMap}; use std::cmp; use hash::keccak; +use heapsize::HeapSizeOf; use util::*; use rlp::*; use network::*; diff --git a/util/bloom/src/lib.rs b/util/bloom/src/lib.rs index 91897ed44..4fdb61d40 100644 --- a/util/bloom/src/lib.rs +++ b/util/bloom/src/lib.rs @@ -24,9 +24,6 @@ use std::hash::{Hash, Hasher}; use std::collections::HashSet; use siphasher::sip::SipHasher; -// TODO [ToDr] Both hashers are exactly the same - no point to keep two. -const NUMBER_OF_HASHERS: usize = 2; - /// BitVec structure with journalling /// Every time any of the blocks is getting set it's index is tracked /// and can be then drained by `drain` method @@ -80,8 +77,6 @@ pub struct Bloom { bitmap: BitVecJournal, bitmap_bits: u64, k_num: u32, - // TODO [ToDr] Both hashers are exactly the same - no point to keep two. - sips: [SipHasher; NUMBER_OF_HASHERS], } impl Bloom { @@ -93,12 +88,10 @@ impl Bloom { let bitmap_bits = (bitmap_size as u64) * 8u64; let k_num = Bloom::optimal_k_num(bitmap_bits, items_count); let bitmap = BitVecJournal::new(bitmap_bits as usize); - let sips = [SipHasher::new(), SipHasher::new()]; Bloom { bitmap: bitmap, bitmap_bits: bitmap_bits, k_num: k_num, - sips: sips, } } @@ -107,12 +100,10 @@ impl Bloom { let bitmap_size = parts.len() * 8; let bitmap_bits = (bitmap_size as u64) * 8u64; let bitmap = BitVecJournal::from_parts(parts); - let sips = [SipHasher::new(), SipHasher::new()]; Bloom { bitmap: bitmap, bitmap_bits: bitmap_bits, k_num: k_num, - sips: sips, } } @@ -139,9 +130,9 @@ impl Bloom { pub fn set(&mut self, item: T) where T: Hash { - let mut hashes = [0u64, 0u64]; + let base_hash = Bloom::sip_hash(&item); for k_i in 0..self.k_num { - let bit_offset = (self.bloom_hash(&mut hashes, &item, k_i) % self.bitmap_bits) as usize; + let bit_offset = (Bloom::bloom_hash(base_hash, k_i) % self.bitmap_bits) as usize; self.bitmap.set(bit_offset); } } @@ -151,9 +142,9 @@ impl Bloom { pub fn check(&self, item: T) -> bool where T: Hash { - let mut hashes = [0u64, 0u64]; + let base_hash = Bloom::sip_hash(&item); for k_i in 0..self.k_num { - let bit_offset = (self.bloom_hash(&mut hashes, &item, k_i) % self.bitmap_bits) as usize; + let bit_offset = (Bloom::bloom_hash(base_hash, k_i) % self.bitmap_bits) as usize; if !self.bitmap.get(bit_offset) { return false; } @@ -178,17 +169,20 @@ impl Bloom { cmp::max(k_num, 1) } - fn bloom_hash(&self, hashes: &mut [u64; NUMBER_OF_HASHERS], item: &T, k_i: u32) -> u64 + fn sip_hash(item: &T) -> u64 where T: Hash { - if k_i < NUMBER_OF_HASHERS as u32 { - let mut sip = self.sips[k_i as usize].clone(); - item.hash(&mut sip); - let hash = sip.finish(); - hashes[k_i as usize] = hash; - hash + let mut sip = SipHasher::new(); + item.hash(&mut sip); + let hash = sip.finish(); + hash + } + + fn bloom_hash(base_hash: u64, k_i: u32) -> u64 { + if k_i < 2 { + base_hash } else { - hashes[0].wrapping_add((k_i as u64).wrapping_mul(hashes[1]) % 0xffffffffffffffc5) + base_hash.wrapping_add((k_i as u64).wrapping_mul(base_hash) % 0xffffffffffffffc5) } } @@ -218,6 +212,7 @@ pub struct BloomJournal { #[cfg(test)] mod tests { use super::Bloom; + use std::collections::HashSet; #[test] fn get_set() { @@ -248,4 +243,16 @@ mod tests { // 2/8/64 = 0.00390625 assert!(full >= 0.0039f64 && full <= 0.004f64); } + + #[test] + fn hash_backward_compatibility() { + let ss = vec!["you", "should", "not", "break", "hash", "backward", "compatibility"]; + let mut bloom = Bloom::new(16, 8); + for s in ss.iter() { + bloom.set(&s); + } + let drained_elems: HashSet = bloom.drain_journal().entries.into_iter().map(|t| t.1).collect(); + let expected: HashSet = [2094615114573771027u64, 244675582389208413u64].iter().cloned().collect(); + assert_eq!(drained_elems, expected); + } } diff --git a/util/src/lib.rs b/util/src/lib.rs index 911df1cc6..0f16609c7 100644 --- a/util/src/lib.rs +++ b/util/src/lib.rs @@ -150,7 +150,6 @@ pub use bigint::prelude::*; pub use bigint::hash; pub use ansi_term::{Colour, Style}; -pub use heapsize::HeapSizeOf; pub use parking_lot::{Condvar, Mutex, MutexGuard, RwLock, RwLockReadGuard, RwLockWriteGuard}; /// 160-bit integer representing account address