From ab6153853b5d356b913d12b271a90dccf6fa2294 Mon Sep 17 00:00:00 2001 From: Anton Gavrilov Date: Tue, 18 Jul 2017 12:14:06 +0200 Subject: [PATCH 1/9] Tracing for rewards added. Without tests for now --- ethcore/src/block.rs | 22 +++++- ethcore/src/engines/authority_round/mod.rs | 20 ++++- ethcore/src/engines/mod.rs | 12 ++- ethcore/src/engines/tendermint/mod.rs | 21 +++++- ethcore/src/ethereum/ethash.rs | 33 +++++++-- ethcore/src/trace/executive_tracer.rs | 17 ++++- ethcore/src/trace/mod.rs | 5 +- ethcore/src/trace/noop_tracer.rs | 5 +- ethcore/src/trace/types/filter.rs | 4 + ethcore/src/trace/types/trace.rs | 86 +++++++++++++++++++++- rpc/src/v1/types/trace.rs | 58 +++++++++++++++ 11 files changed, 262 insertions(+), 21 deletions(-) diff --git a/ethcore/src/block.rs b/ethcore/src/block.rs index 8cd1e22b6..de04dd8bc 100644 --- a/ethcore/src/block.rs +++ b/ethcore/src/block.rs @@ -92,6 +92,7 @@ pub struct ExecutedBlock { transactions_set: HashSet, state: State, traces: Option>>, + tracing_enabled: bool, } /// A set of references to `ExecutedBlock` fields that are publicly accessible. @@ -108,6 +109,8 @@ pub struct BlockRefMut<'a> { pub state: &'a mut State, /// Traces. pub traces: &'a Option>>, + /// Tracing enabled flag. + pub tracing_enabled: &'a mut bool, } /// A set of immutable references to `ExecutedBlock` fields that are publicly accessible. @@ -124,6 +127,8 @@ pub struct BlockRef<'a> { pub state: &'a State, /// Traces. pub traces: &'a Option>>, + /// Tracing enabled flag. + pub tracing_enabled: &'a bool, } impl ExecutedBlock { @@ -137,6 +142,7 @@ impl ExecutedBlock { transactions_set: Default::default(), state: state, traces: if tracing {Some(Vec::new())} else {None}, + tracing_enabled: tracing, } } @@ -149,6 +155,7 @@ impl ExecutedBlock { state: &mut self.state, receipts: &self.receipts, traces: &self.traces, + tracing_enabled: &mut self.tracing_enabled, } } @@ -161,6 +168,7 @@ impl ExecutedBlock { state: &self.state, receipts: &self.receipts, traces: &self.traces, + tracing_enabled: &self.tracing_enabled, } } } @@ -196,6 +204,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().tracing_enabled } } /// Trait for a object that has a state database. @@ -392,9 +403,16 @@ impl<'x> OpenBlock<'x> { let unclosed_state = s.block.state.clone(); - if let Err(e) = s.engine.on_close_block(&mut s.block) { - warn!("Encountered error on closing the block: {}", e); + match s.engine.on_close_block(&mut s.block) { + Ok(outcome) => { + let t = outcome.trace; + if t.is_some() { + s.block.traces.as_mut().map(|traces| traces.push(t.unwrap())); + } + } + Err(e) => warn!("Encountered error on closing the block: {}", e), } + if let Err(e) = s.block.state.commit() { warn!("Encountered error on state commit: {}", e); } diff --git a/ethcore/src/engines/authority_round/mod.rs b/ethcore/src/engines/authority_round/mod.rs index 3210368db..2f4352b89 100644 --- a/ethcore/src/engines/authority_round/mod.rs +++ b/ethcore/src/engines/authority_round/mod.rs @@ -24,7 +24,9 @@ use account_provider::AccountProvider; use block::*; use builtin::Builtin; use client::{Client, EngineClient}; -use engines::{Call, Engine, Seal, EngineError, ConstructedVerifier}; +use engines::{Call, Engine, Seal, EngineError, ConstructedVerifier, CloseOutcome}; +use trace::{Tracer, ExecutiveTracer}; +use types::trace_types::trace::{RewardType}; use error::{Error, TransactionError, BlockError}; use ethjson; use header::{Header, BlockNumber}; @@ -561,17 +563,29 @@ impl Engine for AuthorityRound { } /// Apply the block reward on finalisation of the block. - fn on_close_block(&self, block: &mut ExecutedBlock) -> Result<(), Error> { + fn on_close_block(&self, block: &mut ExecutedBlock) -> Result { let fields = block.fields_mut(); + let mut tracer = ExecutiveTracer::default(); // Bestow block reward let res = fields.state.add_balance(fields.header.author(), &self.block_reward, CleanupMode::NoEmpty) .map_err(::error::Error::from) .and_then(|_| fields.state.commit()); + + // Trace it + let block_miner = fields.header.author().clone(); + tracer.trace_reward(block_miner, self.block_reward, RewardType::Block); + // 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 + match res { + Ok(res) => match *fields.tracing_enabled { + true => Ok(CloseOutcome{trace: Some(tracer.traces())}), + false => Ok(CloseOutcome{trace: None}) + }, + Err(e) => Err(e) + } } /// Check the number of seal fields. diff --git a/ethcore/src/engines/mod.rs b/ethcore/src/engines/mod.rs index c5fa4818e..116b98054 100644 --- a/ethcore/src/engines/mod.rs +++ b/ethcore/src/engines/mod.rs @@ -48,6 +48,7 @@ use error::Error; use evm::Schedule; use header::{Header, BlockNumber}; use receipt::Receipt; +use trace::FlatTrace; use snapshot::SnapshotComponents; use spec::CommonParams; use transaction::{UnverifiedTransaction, SignedTransaction}; @@ -81,6 +82,13 @@ pub enum EngineError { RequiresClient, } +/// Used to return information about close block operation. +#[derive(Debug)] +pub struct CloseOutcome { + /// The trace for the closing block, if None if tracing is disabled. + pub trace: Option>, +} + impl fmt::Display for EngineError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { use self::EngineError::*; @@ -228,8 +236,8 @@ pub trait Engine : Sync + Send { } /// Block transformation functions, after the transactions. - fn on_close_block(&self, _block: &mut ExecutedBlock) -> Result<(), Error> { - Ok(()) + fn on_close_block(&self, _block: &mut ExecutedBlock) -> Result { + Ok(CloseOutcome{trace: None}) } /// None means that it requires external input (e.g. PoW) to seal a block. diff --git a/ethcore/src/engines/tendermint/mod.rs b/ethcore/src/engines/tendermint/mod.rs index 453e8f9fb..b25b82d76 100644 --- a/ethcore/src/engines/tendermint/mod.rs +++ b/ethcore/src/engines/tendermint/mod.rs @@ -37,7 +37,9 @@ use ethkey::{Message, public_to_address, recover, Signature}; use account_provider::AccountProvider; use block::*; use spec::CommonParams; -use engines::{Engine, Seal, EngineError, ConstructedVerifier}; +use engines::{Engine, Seal, EngineError, CloseOutcome, ConstructedVerifier}; +use trace::{Tracer, ExecutiveTracer}; +use types::trace_types::trace::{RewardType}; use state::CleanupMode; use io::IoService; use super::signer::EngineSigner; @@ -542,17 +544,30 @@ impl Engine for Tendermint { } /// Apply the block reward on finalisation of the block. - fn on_close_block(&self, block: &mut ExecutedBlock) -> Result<(), Error>{ + fn on_close_block(&self, block: &mut ExecutedBlock) -> Result{ let fields = block.fields_mut(); + let mut tracer = ExecutiveTracer::default(); // Bestow block reward let res = fields.state.add_balance(fields.header.author(), &self.block_reward, CleanupMode::NoEmpty) .map_err(::error::Error::from) .and_then(|_| fields.state.commit()); + + // Trace it + let block_miner = fields.header.author().clone(); + tracer.trace_reward(block_miner, self.block_reward, RewardType::Block); + // 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 + match res { + Ok(res) => match *fields.tracing_enabled { + true => Ok(CloseOutcome{trace: Some(tracer.traces())}), + false => Ok(CloseOutcome{trace: None}) + }, + Err(e) => Err(e) + } + } fn verify_block_basic(&self, header: &Header, _block: Option<&[u8]>) -> Result<(), Error> { diff --git a/ethcore/src/ethereum/ethash.rs b/ethcore/src/ethereum/ethash.rs index 1fbd711c9..a38f112fe 100644 --- a/ethcore/src/ethereum/ethash.rs +++ b/ethcore/src/ethereum/ethash.rs @@ -21,11 +21,13 @@ use block::*; use builtin::Builtin; use evm::env_info::EnvInfo; use error::{BlockError, Error, TransactionError}; +use trace::{Tracer, ExecutiveTracer}; +use types::trace_types::trace::{RewardType}; use header::{Header, BlockNumber}; use state::CleanupMode; use spec::CommonParams; use transaction::UnverifiedTransaction; -use engines::{self, Engine}; +use engines::{self, Engine, CloseOutcome}; use evm::Schedule; use ethjson; use rlp::{self, UntrustedRlp}; @@ -283,40 +285,57 @@ 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> { + fn on_close_block(&self, block: &mut ExecutedBlock) -> Result { let reward = self.ethash_params.block_reward; 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 / U256::from(32) * 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 )?; + // Trace it + let block_miner = fields.header.author().clone(); + tracer.trace_reward(block_miner, result_block_reward, RewardType::Block); + // Bestow uncle rewards let current_number = fields.header.number(); for u in fields.uncles.iter() { + let uncle_miner = u.author().clone(); + let mut result_uncle_reward: U256 = U256::from(0); + if eras == 0 { + result_uncle_reward = reward * U256::from(8 + u.number() - current_number) / U256::from(8); 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 / U256::from(32); fields.state.add_balance( u.author(), - &(reward / U256::from(32)), + &(result_uncle_reward), CleanupMode::NoEmpty ) }?; + + // Trace uncle rewards + tracer.trace_reward(uncle_miner, result_uncle_reward, RewardType::Uncle); } // Commit state so that we can actually figure out the state root. fields.state.commit()?; - Ok(()) + match *fields.tracing_enabled { + true => Ok(CloseOutcome{trace: Some(tracer.traces())}), + false => Ok(CloseOutcome{trace: None}) + } } fn verify_block_basic(&self, header: &Header, _block: Option<&[u8]>) -> result::Result<(), Error> { diff --git a/ethcore/src/trace/executive_tracer.rs b/ethcore/src/trace/executive_tracer.rs index cdfe1e004..65d8ebe3e 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 evm::action_params::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. @@ -162,6 +162,21 @@ impl Tracer for ExecutiveTracer { debug!(target: "trace", "Traced failed suicide {:?}", trace); self.traces.push(trace); } + + fn trace_reward(&mut self, miner: Address, value: U256, reward_type: RewardType) { + let trace = FlatTrace { + subtraces: 0, + action: Action::Reward(Reward { + miner: miner, + value: value, + reward_type: reward_type, + }), + result: Res::None, + trace_address: Default::default(), + }; + debug!(target: "trace", "Traced failed reward {:?}", trace); + self.traces.push(trace); + } fn subtracer(&self) -> Self { ExecutiveTracer::default() diff --git a/ethcore/src/trace/mod.rs b/ethcore/src/trace/mod.rs index 39af8a08a..2b318a260 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, miner: 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 5fb8a7c55..091d69547 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 evm::action_params::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, miner: Address, value: U256, reward_type: RewardType) { + } + fn subtracer(&self) -> Self { NoopTracer } diff --git a/ethcore/src/trace/types/filter.rs b/ethcore/src/trace/types/filter.rs index 2dc810442..35d70f84e 100644 --- a/ethcore/src/trace/types/filter.rs +++ b/ethcore/src/trace/types/filter.rs @@ -128,6 +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) => { + let to_matches = self.to_address.matches(&reward.miner); + to_matches } } } diff --git a/ethcore/src/trace/types/trace.rs b/ethcore/src/trace/types/trace.rs index 24250935f..69e351cbb 100644 --- a/ethcore/src/trace/types/trace.rs +++ b/ethcore/src/trace/types/trace.rs @@ -218,6 +218,81 @@ impl Create { } } +/// Reward type. +#[derive(Debug, PartialEq, Clone)] +#[cfg_attr(feature = "ipc", binary)] +pub enum RewardType { + /// None + None, + /// Block + Block, + /// Uncle + Uncle, +} + +impl Encodable for RewardType { + fn rlp_append(&self, s: &mut RlpStream) { + let v = match *self { + RewardType::None => 0u32, + RewardType::Block => 1, + RewardType::Uncle => 2, + }; + 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::None, + 1 => RewardType::Block, + 2 => 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 { + /// Miner's address. + pub miner: 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(&self.miner.sha3()) + } +} + +impl Encodable for Reward { + fn rlp_append(&self, s: &mut RlpStream) { + s.begin_list(2); + s.append(&self.miner); + s.append(&self.value); + s.append(&self.reward_type); + } +} + +impl Decodable for Reward { + fn decode(rlp: &UntrustedRlp) -> Result { + let res = Reward { + miner: rlp.val_at(0)?, + value: rlp.val_at(1)?, + reward_type: rlp.val_at(2)?, + }; + + Ok(res) + } +} + + /// Suicide action. #[derive(Debug, Clone, PartialEq)] #[cfg_attr(feature = "ipc", binary)] @@ -260,7 +335,7 @@ impl Decodable for Suicide { } -/// Description of an action that we trace; will be either a call or a create. +/// Description of an action that we trace. #[derive(Debug, Clone, PartialEq)] #[cfg_attr(feature = "ipc", binary)] pub enum Action { @@ -270,6 +345,8 @@ pub enum Action { Create(Create), /// Suicide. Suicide(Suicide), + /// Reward + Reward(Reward), } impl Encodable for Action { @@ -287,7 +364,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); } + } } } @@ -299,6 +381,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.")), } } @@ -311,6 +394,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/rpc/src/v1/types/trace.rs b/rpc/src/v1/types/trace.rs index b01d04c3f..0fef8ae2b 100644 --- a/rpc/src/v1/types/trace.rs +++ b/rpc/src/v1/types/trace.rs @@ -299,6 +299,53 @@ impl From for Call { } } +/// Reward type. +#[derive(Debug, Serialize)] +pub enum RewardType { + /// None + #[serde(rename="none")] + None, + /// Block + #[serde(rename="block")] + Block, + /// Uncle + #[serde(rename="uncle")] + Uncle, +} + +impl From for RewardType { + fn from(c: trace::RewardType) -> Self { + match c { + trace::RewardType::None => RewardType::None, + trace::RewardType::Block => RewardType::Block, + trace::RewardType::Uncle => RewardType::Uncle, + } + } +} + + +/// Reward action +#[derive(Debug, Serialize)] +pub struct Reward { + /// Miner's address. + pub miner: 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 { + miner: r.miner.into(), + value: r.value.into(), + reward_type: r.reward_type.into(), + } + } +} + /// Suicide #[derive(Debug, Serialize)] pub struct Suicide { @@ -330,6 +377,8 @@ pub enum Action { Create(Create), /// Suicide Suicide(Suicide), + /// Reward + Reward(Reward), } impl From for Action { @@ -338,6 +387,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()), } } } @@ -449,6 +499,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 { @@ -516,6 +570,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 { From 16f39487ba8eadbdf441a8dafa3e4d9ff220cb04 Mon Sep 17 00:00:00 2001 From: Anton Gavrilov Date: Tue, 18 Jul 2017 15:58:14 +0200 Subject: [PATCH 2/9] Warnings removed --- ethcore/src/engines/authority_round/mod.rs | 2 +- ethcore/src/engines/tendermint/mod.rs | 2 +- ethcore/src/ethereum/ethash.rs | 2 +- ethcore/src/trace/noop_tracer.rs | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/ethcore/src/engines/authority_round/mod.rs b/ethcore/src/engines/authority_round/mod.rs index 2f4352b89..237f787f3 100644 --- a/ethcore/src/engines/authority_round/mod.rs +++ b/ethcore/src/engines/authority_round/mod.rs @@ -580,7 +580,7 @@ impl Engine for AuthorityRound { warn!("Encountered error on closing block: {}", e); } match res { - Ok(res) => match *fields.tracing_enabled { + Ok(_) => match *fields.tracing_enabled { true => Ok(CloseOutcome{trace: Some(tracer.traces())}), false => Ok(CloseOutcome{trace: None}) }, diff --git a/ethcore/src/engines/tendermint/mod.rs b/ethcore/src/engines/tendermint/mod.rs index b25b82d76..492a2c7a8 100644 --- a/ethcore/src/engines/tendermint/mod.rs +++ b/ethcore/src/engines/tendermint/mod.rs @@ -561,7 +561,7 @@ impl Engine for Tendermint { warn!("Encountered error on closing block: {}", e); } match res { - Ok(res) => match *fields.tracing_enabled { + Ok(_) => match *fields.tracing_enabled { true => Ok(CloseOutcome{trace: Some(tracer.traces())}), false => Ok(CloseOutcome{trace: None}) }, diff --git a/ethcore/src/ethereum/ethash.rs b/ethcore/src/ethereum/ethash.rs index a38f112fe..bc06fb408 100644 --- a/ethcore/src/ethereum/ethash.rs +++ b/ethcore/src/ethereum/ethash.rs @@ -308,7 +308,7 @@ impl Engine for Arc { let current_number = fields.header.number(); for u in fields.uncles.iter() { let uncle_miner = u.author().clone(); - let mut result_uncle_reward: U256 = U256::from(0); + let result_uncle_reward: U256; if eras == 0 { result_uncle_reward = reward * U256::from(8 + u.number() - current_number) / U256::from(8); diff --git a/ethcore/src/trace/noop_tracer.rs b/ethcore/src/trace/noop_tracer.rs index 091d69547..0d8af7006 100644 --- a/ethcore/src/trace/noop_tracer.rs +++ b/ethcore/src/trace/noop_tracer.rs @@ -58,7 +58,7 @@ impl Tracer for NoopTracer { fn trace_suicide(&mut self, _address: Address, _balance: U256, _refund_address: Address) { } - fn trace_reward(&mut self, miner: Address, value: U256, reward_type: RewardType) { + fn trace_reward(&mut self, _: Address, _: U256, _: RewardType) { } fn subtracer(&self) -> Self { From 73128039a68069055df85b395f3890b4e5347e8f Mon Sep 17 00:00:00 2001 From: Anton Gavrilov Date: Tue, 25 Jul 2017 12:04:37 +0200 Subject: [PATCH 3/9] Working test with block reward added --- ethcore/res/null_morden.json | 3 +- ethcore/res/null_morden_with_reward.json | 34 +++++++++ ethcore/src/block.rs | 22 ++++-- ethcore/src/engines/null_engine.rs | 33 ++++++++- ethcore/src/spec/spec.rs | 6 ++ ethcore/src/tests/mod.rs | 1 + ethcore/src/tests/trace.rs | 93 ++++++++++++++++++++++++ ethcore/src/trace/db.rs | 9 ++- ethcore/src/verification/verification.rs | 1 + json/src/spec/params.rs | 3 + 10 files changed, 192 insertions(+), 13 deletions(-) create mode 100644 ethcore/res/null_morden_with_reward.json create mode 100644 ethcore/src/tests/trace.rs diff --git a/ethcore/res/null_morden.json b/ethcore/res/null_morden.json index 96d804b5b..62b9682dc 100644 --- a/ethcore/res/null_morden.json +++ b/ethcore/res/null_morden.json @@ -7,7 +7,8 @@ "accountStartNonce": "0x0", "maximumExtraDataSize": "0x20", "minGasLimit": "0x1388", - "networkID" : "0x2" + "networkID" : "0x2", + "applyReward": false }, "genesis": { "seal": { diff --git a/ethcore/res/null_morden_with_reward.json b/ethcore/res/null_morden_with_reward.json new file mode 100644 index 000000000..724e58a7a --- /dev/null +++ b/ethcore/res/null_morden_with_reward.json @@ -0,0 +1,34 @@ +{ + "name": "Morden", + "engine": { + "null": null + }, + "params": { + "accountStartNonce": "0x0", + "maximumExtraDataSize": "0x20", + "minGasLimit": "0x1388", + "networkID" : "0x2", + "applyReward": true + }, + "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 de04dd8bc..fa82c33e8 100644 --- a/ethcore/src/block.rs +++ b/ethcore/src/block.rs @@ -404,12 +404,12 @@ impl<'x> OpenBlock<'x> { let unclosed_state = s.block.state.clone(); match s.engine.on_close_block(&mut s.block) { - Ok(outcome) => { - let t = outcome.trace; - if t.is_some() { - s.block.traces.as_mut().map(|traces| traces.push(t.unwrap())); - } - } + Ok(outcome) => match outcome.trace { + Some(t) => { + s.block.traces.as_mut().map(|traces| traces.push(t)); + }, + None => {}, + }, Err(e) => warn!("Encountered error on closing the block: {}", e), } @@ -436,8 +436,14 @@ impl<'x> OpenBlock<'x> { pub fn close_and_lock(self) -> LockedBlock { let mut s = self; - if let Err(e) = s.engine.on_close_block(&mut s.block) { - warn!("Encountered error on closing the block: {}", e); + match s.engine.on_close_block(&mut s.block) { + Ok(outcome) => match outcome.trace { + Some(t) => { + s.block.traces.as_mut().map(|traces| traces.push(t)); + }, + None => {}, + }, + Err(e) => warn!("Encountered error on closing the block: {}", e), } if let Err(e) = s.block.state.commit() { diff --git a/ethcore/src/engines/null_engine.rs b/ethcore/src/engines/null_engine.rs index 3bdef480c..2e752709f 100644 --- a/ethcore/src/engines/null_engine.rs +++ b/ethcore/src/engines/null_engine.rs @@ -17,10 +17,16 @@ use std::collections::BTreeMap; use util::Address; use builtin::Builtin; -use engines::Engine; +use block::*; +use util::*; +use engines::{Engine, CloseOutcome}; use spec::CommonParams; use evm::Schedule; use header::BlockNumber; +use error::Error; +use state::CleanupMode; +use trace::{Tracer, ExecutiveTracer}; +use types::trace_types::trace::{RewardType}; /// An engine which does not provide any consensus mechanism and does not seal blocks. pub struct NullEngine { @@ -64,4 +70,29 @@ impl Engine for NullEngine { fn snapshot_components(&self) -> Option> { Some(Box::new(::snapshot::PowSnapshot(10000))) } + + fn on_close_block(&self, block: &mut ExecutedBlock) -> Result { + if !self.params.apply_reward { + return Ok(CloseOutcome{trace: None}); + } + + 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 + )?; + + let block_miner = fields.header.author().clone(); + tracer.trace_reward(block_miner, result_block_reward, RewardType::Block); + + fields.state.commit()?; + match *fields.tracing_enabled { + true => Ok(CloseOutcome{trace: Some(tracer.traces())}), + false => Ok(CloseOutcome{trace: None}) + } + } } diff --git a/ethcore/src/spec/spec.rs b/ethcore/src/spec/spec.rs index 7d3900f2c..3cb3782d1 100644 --- a/ethcore/src/spec/spec.rs +++ b/ethcore/src/spec/spec.rs @@ -85,6 +85,8 @@ pub struct CommonParams { pub remove_dust_contracts: bool, /// Wasm support pub wasm: bool, + /// Apply reward + pub apply_reward: bool, } impl CommonParams { @@ -139,6 +141,7 @@ impl From for CommonParams { nonce_cap_increment: p.nonce_cap_increment.map_or(64, Into::into), remove_dust_contracts: p.remove_dust_contracts.unwrap_or(false), wasm: p.wasm.unwrap_or(false), + apply_reward: p.apply_reward.unwrap_or(true), } } } @@ -446,6 +449,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 sha3(''). 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..e54b8b5df --- /dev/null +++ b/ethcore/src/tests/trace.rs @@ -0,0 +1,93 @@ +// 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 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}; + +#[test] +fn can_trace_block_and_uncle_reward() { + let dir = RandomTempPath::new(); + let spec = Spec::new_test_with_reward(); + let engine = &*spec.engine; + let genesis_header = spec.genesis_header(); + let db = spec.ensure_db_good(get_temp_state_db(), &Default::default()).unwrap(); + let last_hashes = Arc::new(vec![genesis_header.hash()]); + let mut b = OpenBlock::new(engine, Default::default(), true, db, &genesis_header, last_hashes, Address::zero(), (3141562.into(), 31415620.into()), vec![], false).unwrap(); + + let kp = KeyPair::from_secret_slice(&"".sha3()).unwrap(); + let mut n = 0; + for _ in 0..1 { + b.push_transaction(Transaction { + nonce: n.into(), + gas_price: 0.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); + //b.push_uncle(uncle).unwrap(); + let b = b.close_and_lock().seal(engine, vec![]).unwrap(); + + 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(); + + let res = client.import_block(b.rlp_bytes()); + if res.is_err() { + panic!("error importing block: {:#?}", res.err().unwrap()); + } + + client.flush_queue(); + client.import_verified_blocks(); + + let filter = TraceFilter { + range: (BlockId::Number(1)..BlockId::Number(1)), + from_address: vec![], + to_address: vec![], + }; + + let traces = client.filter_traces(filter); + assert!(traces.is_some(), "Genesis trace should be always present."); + //panic!("Traces size is: {:#?}", traces.unwrap().len()); +} \ No newline at end of file diff --git a/ethcore/src/trace/db.rs b/ethcore/src/trace/db.rs index fe90ffe41..afb0fef49 100644 --- a/ethcore/src/trace/db.rs +++ b/ethcore/src/trace/db.rs @@ -215,8 +215,7 @@ 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 tx_hash = self.extras.transaction_hash(block_number, tx_number); let flat_traces: Vec = traces.into(); flat_traces.into_iter() @@ -228,7 +227,11 @@ impl TraceDB where T: DatabaseExtras { subtraces: trace.subtraces, trace_address: trace.trace_address.into_iter().collect(), transaction_number: tx_number, - transaction_hash: tx_hash.clone(), + transaction_hash: match tx_hash { + Some(hash) => hash.clone(), + /// None tx hash means non transaction's trace + None => 0.into(), + }, block_number: block_number, block_hash: block_hash }), diff --git a/ethcore/src/verification/verification.rs b/ethcore/src/verification/verification.rs index 823d2ef70..6980a21d1 100644 --- a/ethcore/src/verification/verification.rs +++ b/ethcore/src/verification/verification.rs @@ -591,6 +591,7 @@ mod tests { let mut params = CommonParams::default(); params.dust_protection_transition = 0; params.nonce_cap_increment = 2; + params.apply_reward = false; let mut header = Header::default(); header.set_number(1); diff --git a/json/src/spec/params.rs b/json/src/spec/params.rs index 0c0b1b01e..bbb7e7a2d 100644 --- a/json/src/spec/params.rs +++ b/json/src/spec/params.rs @@ -91,6 +91,9 @@ pub struct Params { pub remove_dust_contracts : Option, /// Wasm support flag pub wasm: Option, + /// Apply reward flag + #[serde(rename="applyReward")] + pub apply_reward: Option, } #[cfg(test)] From 53c3b772dd516ec801822b8fe2b2413682b06611 Mon Sep 17 00:00:00 2001 From: Anton Gavrilov Date: Wed, 26 Jul 2017 19:36:09 +0200 Subject: [PATCH 4/9] Complete version of tracing test with reward --- ethcore/src/block.rs | 2 +- ethcore/src/engines/null_engine.rs | 13 +++ ethcore/src/tests/trace.rs | 167 +++++++++++++++++++++++------ 3 files changed, 149 insertions(+), 33 deletions(-) diff --git a/ethcore/src/block.rs b/ethcore/src/block.rs index fa82c33e8..9057155db 100644 --- a/ethcore/src/block.rs +++ b/ethcore/src/block.rs @@ -453,7 +453,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.receipts_root() == &SHA3_NULL_RLP { s.block.header.set_uncles_hash(uncle_bytes.sha3()); } if s.block.header.receipts_root().is_zero() || s.block.header.receipts_root() == &SHA3_NULL_RLP { diff --git a/ethcore/src/engines/null_engine.rs b/ethcore/src/engines/null_engine.rs index 2e752709f..9c90715a4 100644 --- a/ethcore/src/engines/null_engine.rs +++ b/ethcore/src/engines/null_engine.rs @@ -76,6 +76,7 @@ impl Engine for NullEngine { return Ok(CloseOutcome{trace: None}); } + /// Block reward let fields = block.fields_mut(); let mut tracer = ExecutiveTracer::default(); @@ -89,6 +90,18 @@ impl Engine for NullEngine { let block_miner = fields.header.author().clone(); tracer.trace_reward(block_miner, result_block_reward, RewardType::Block); + /// Uncle rewards + let result_uncle_reward = U256::from(10000000); + for u in fields.uncles.iter() { + let uncle_miner = u.author().clone(); + fields.state.add_balance( + u.author(), + &(result_uncle_reward), + CleanupMode::NoEmpty + )?; + tracer.trace_reward(uncle_miner, result_uncle_reward, RewardType::Uncle); + } + fields.state.commit()?; match *fields.tracing_enabled { true => Ok(CloseOutcome{trace: Some(tracer.traces())}), diff --git a/ethcore/src/tests/trace.rs b/ethcore/src/tests/trace.rs index e54b8b5df..4f077af32 100644 --- a/ethcore/src/tests/trace.rs +++ b/ethcore/src/tests/trace.rs @@ -30,38 +30,19 @@ use std::sync::Arc; use header::Header; use miner::Miner; use transaction::{Action, Transaction}; +use views::BlockView; +use types::trace_types::trace::RewardType; +use types::trace_types::trace::Action::Reward; +use types::trace_types::localized::LocalizedTrace; #[test] fn can_trace_block_and_uncle_reward() { let dir = RandomTempPath::new(); let spec = Spec::new_test_with_reward(); let engine = &*spec.engine; - let genesis_header = spec.genesis_header(); - let db = spec.ensure_db_good(get_temp_state_db(), &Default::default()).unwrap(); - let last_hashes = Arc::new(vec![genesis_header.hash()]); - let mut b = OpenBlock::new(engine, Default::default(), true, db, &genesis_header, last_hashes, Address::zero(), (3141562.into(), 31415620.into()), vec![], false).unwrap(); - let kp = KeyPair::from_secret_slice(&"".sha3()).unwrap(); - let mut n = 0; - for _ in 0..1 { - b.push_transaction(Transaction { - nonce: n.into(), - gas_price: 0.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); - //b.push_uncle(uncle).unwrap(); - let b = b.close_and_lock().seal(engine, vec![]).unwrap(); - - let db_config = DatabaseConfig::with_columns(::db::NUM_COLUMNS); + /// 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()); @@ -72,22 +53,144 @@ fn can_trace_block_and_uncle_reward() { Arc::new(Miner::with_spec(&spec)), IoChannel::disconnected(), ).unwrap(); - - let res = client.import_block(b.rlp_bytes()); + + /// Create test data + 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(&"".sha3()).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 transactions and uncles + 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(); - let filter = TraceFilter { - range: (BlockId::Number(1)..BlockId::Number(1)), + /// Filter the resuliting data + 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(), "Genesis trace should be always present."); - //panic!("Traces size is: {:#?}", traces.unwrap().len()); + let traces = client.filter_traces(filter); + assert!(traces.is_some(), "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); } \ No newline at end of file From af1fbb39caffc8b3dfeb2e1b1f3decbbe5c57a95 Mon Sep 17 00:00:00 2001 From: Anton Gavrilov Date: Thu, 27 Jul 2017 17:54:17 +0200 Subject: [PATCH 5/9] Unit tests for tracing reward added --- ethcore/src/trace/types/filter.rs | 22 +++++++++++++++++++++- ethcore/src/trace/types/flat.rs | 28 ++++++++++++++++++++++++++-- ethcore/src/trace/types/trace.rs | 2 +- rpc/src/v1/types/trace.rs | 20 ++++++++++++++++++++ 4 files changed, 68 insertions(+), 4 deletions(-) diff --git a/ethcore/src/trace/types/filter.rs b/ethcore/src/trace/types/filter.rs index 35d70f84e..00fff0014 100644 --- a/ethcore/src/trace/types/filter.rs +++ b/ethcore/src/trace/types/filter.rs @@ -142,7 +142,8 @@ mod tests { use util::Address; use util::sha3::Hashable; use util::bloom::Bloomable; - use trace::trace::{Action, Call, Res, Create, CreateResult, Suicide}; + use trace::trace::{Action, Call, Res, Create, CreateResult, Suicide, Reward}; + use types::trace_types::trace::RewardType; use trace::flat::FlatTrace; use trace::{Filter, AddressesFilter, TraceError}; use evm::CallType; @@ -345,5 +346,24 @@ mod tests { assert!(f4.matches(&trace)); assert!(f5.matches(&trace)); assert!(!f6.matches(&trace)); + + let trace = FlatTrace { + action: Action::Reward(Reward { + miner: 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 da304694d..536ecbef3 100644 --- a/ethcore/src/trace/types/flat.rs +++ b/ethcore/src/trace/types/flat.rs @@ -162,8 +162,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 types::trace_types::trace::RewardType; #[test] fn encode_flat_transaction_traces() { @@ -238,9 +239,32 @@ mod tests { subtraces: 0, }; + let flat_trace3 = FlatTrace { + action: Action::Reward(Reward { + miner: "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 { + miner: "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/trace.rs b/ethcore/src/trace/types/trace.rs index 69e351cbb..faa4ca78f 100644 --- a/ethcore/src/trace/types/trace.rs +++ b/ethcore/src/trace/types/trace.rs @@ -273,7 +273,7 @@ impl Reward { impl Encodable for Reward { fn rlp_append(&self, s: &mut RlpStream) { - s.begin_list(2); + s.begin_list(3); s.append(&self.miner); s.append(&self.value); s.append(&self.reward_type); diff --git a/rpc/src/v1/types/trace.rs b/rpc/src/v1/types/trace.rs index 0fef8ae2b..3de90a75d 100644 --- a/rpc/src/v1/types/trace.rs +++ b/rpc/src/v1/types/trace.rs @@ -763,6 +763,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 { + miner: 4.into(), + value: 6.into(), + reward_type: RewardType::Block, + }), + result: Res::None, + trace_address: vec![10], + subtraces: 1, + transaction_position: 11, + transaction_hash: 12.into(), + block_number: 13, + block_hash: 14.into(), + }; + let serialized = serde_json::to_string(&t).unwrap(); + assert_eq!(serialized, r#"{"type":"reward","action":{"miner":"0x0000000000000000000000000000000000000004","value":"0x0000000000000000000000000000000000000006","rewardType":"block"},"result":null,"traceAddress":[10],"subtraces":1,"transactionPosition":11,"transactionHash":"0x000000000000000000000000000000000000000000000000000000000000000c","blockNumber":13,"blockHash":"0x000000000000000000000000000000000000000000000000000000000000000e"}"#); + } + #[test] fn test_vmtrace_serialize() { let t = VMTrace { From b1935340c8556efe7b691b1ba21a9ac30c263e11 Mon Sep 17 00:00:00 2001 From: Anton Gavrilov Date: Thu, 27 Jul 2017 19:15:25 +0200 Subject: [PATCH 6/9] Fixed errors after merge with master --- ethcore/src/engines/authority_round/mod.rs | 3 +-- ethcore/src/engines/null_engine.rs | 3 +-- ethcore/src/engines/tendermint/mod.rs | 3 +-- ethcore/src/ethereum/ethash.rs | 3 +-- ethcore/src/tests/trace.rs | 5 ++--- ethcore/src/trace/types/filter.rs | 3 +-- ethcore/src/trace/types/flat.rs | 2 +- rpc/src/v1/types/trace.rs | 2 +- 8 files changed, 9 insertions(+), 15 deletions(-) diff --git a/ethcore/src/engines/authority_round/mod.rs b/ethcore/src/engines/authority_round/mod.rs index 237f787f3..a990e3132 100644 --- a/ethcore/src/engines/authority_round/mod.rs +++ b/ethcore/src/engines/authority_round/mod.rs @@ -25,8 +25,7 @@ use block::*; use builtin::Builtin; use client::{Client, EngineClient}; use engines::{Call, Engine, Seal, EngineError, ConstructedVerifier, CloseOutcome}; -use trace::{Tracer, ExecutiveTracer}; -use types::trace_types::trace::{RewardType}; +use trace::{Tracer, ExecutiveTracer, RewardType}; use error::{Error, TransactionError, BlockError}; use ethjson; use header::{Header, BlockNumber}; diff --git a/ethcore/src/engines/null_engine.rs b/ethcore/src/engines/null_engine.rs index 9c90715a4..0a31afc0e 100644 --- a/ethcore/src/engines/null_engine.rs +++ b/ethcore/src/engines/null_engine.rs @@ -25,8 +25,7 @@ use evm::Schedule; use header::BlockNumber; use error::Error; use state::CleanupMode; -use trace::{Tracer, ExecutiveTracer}; -use types::trace_types::trace::{RewardType}; +use trace::{Tracer, ExecutiveTracer, RewardType}; /// An engine which does not provide any consensus mechanism and does not seal blocks. pub struct NullEngine { diff --git a/ethcore/src/engines/tendermint/mod.rs b/ethcore/src/engines/tendermint/mod.rs index 492a2c7a8..70337e4f9 100644 --- a/ethcore/src/engines/tendermint/mod.rs +++ b/ethcore/src/engines/tendermint/mod.rs @@ -38,8 +38,7 @@ use account_provider::AccountProvider; use block::*; use spec::CommonParams; use engines::{Engine, Seal, EngineError, CloseOutcome, ConstructedVerifier}; -use trace::{Tracer, ExecutiveTracer}; -use types::trace_types::trace::{RewardType}; +use trace::{Tracer, ExecutiveTracer, RewardType}; use state::CleanupMode; use io::IoService; use super::signer::EngineSigner; diff --git a/ethcore/src/ethereum/ethash.rs b/ethcore/src/ethereum/ethash.rs index bc06fb408..05777908d 100644 --- a/ethcore/src/ethereum/ethash.rs +++ b/ethcore/src/ethereum/ethash.rs @@ -21,8 +21,7 @@ use block::*; use builtin::Builtin; use evm::env_info::EnvInfo; use error::{BlockError, Error, TransactionError}; -use trace::{Tracer, ExecutiveTracer}; -use types::trace_types::trace::{RewardType}; +use trace::{Tracer, ExecutiveTracer,RewardType}; use header::{Header, BlockNumber}; use state::CleanupMode; use spec::CommonParams; diff --git a/ethcore/src/tests/trace.rs b/ethcore/src/tests/trace.rs index 4f077af32..2389a3c2e 100644 --- a/ethcore/src/tests/trace.rs +++ b/ethcore/src/tests/trace.rs @@ -31,9 +31,8 @@ use header::Header; use miner::Miner; use transaction::{Action, Transaction}; use views::BlockView; -use types::trace_types::trace::RewardType; -use types::trace_types::trace::Action::Reward; -use types::trace_types::localized::LocalizedTrace; +use trace::{RewardType, LocalizedTrace}; +use trace::trace::Action::Reward; #[test] fn can_trace_block_and_uncle_reward() { diff --git a/ethcore/src/trace/types/filter.rs b/ethcore/src/trace/types/filter.rs index 00fff0014..68ab30a31 100644 --- a/ethcore/src/trace/types/filter.rs +++ b/ethcore/src/trace/types/filter.rs @@ -143,9 +143,8 @@ mod tests { use util::sha3::Hashable; use util::bloom::Bloomable; use trace::trace::{Action, Call, Res, Create, CreateResult, Suicide, Reward}; - use types::trace_types::trace::RewardType; use trace::flat::FlatTrace; - use trace::{Filter, AddressesFilter, TraceError}; + use trace::{Filter, AddressesFilter, TraceError, RewardType}; use evm::CallType; #[test] diff --git a/ethcore/src/trace/types/flat.rs b/ethcore/src/trace/types/flat.rs index 536ecbef3..29d0eddb9 100644 --- a/ethcore/src/trace/types/flat.rs +++ b/ethcore/src/trace/types/flat.rs @@ -164,7 +164,7 @@ mod tests { use super::{FlatBlockTraces, FlatTransactionTraces, FlatTrace}; use trace::trace::{Action, Res, CallResult, Call, Suicide, Reward}; use evm::CallType; - use types::trace_types::trace::RewardType; + use trace::RewardType; #[test] fn encode_flat_transaction_traces() { diff --git a/rpc/src/v1/types/trace.rs b/rpc/src/v1/types/trace.rs index 3de90a75d..d64a0347a 100644 --- a/rpc/src/v1/types/trace.rs +++ b/rpc/src/v1/types/trace.rs @@ -780,7 +780,7 @@ mod tests { block_hash: 14.into(), }; let serialized = serde_json::to_string(&t).unwrap(); - assert_eq!(serialized, r#"{"type":"reward","action":{"miner":"0x0000000000000000000000000000000000000004","value":"0x0000000000000000000000000000000000000006","rewardType":"block"},"result":null,"traceAddress":[10],"subtraces":1,"transactionPosition":11,"transactionHash":"0x000000000000000000000000000000000000000000000000000000000000000c","blockNumber":13,"blockHash":"0x000000000000000000000000000000000000000000000000000000000000000e"}"#); + assert_eq!(serialized, r#"{"type":"reward","action":{"miner":"0x0000000000000000000000000000000000000004","value":"0x6","rewardType":"block"},"result":null,"traceAddress":[10],"subtraces":1,"transactionPosition":11,"transactionHash":"0x000000000000000000000000000000000000000000000000000000000000000c","blockNumber":13,"blockHash":"0x000000000000000000000000000000000000000000000000000000000000000e"}"#); } #[test] From 1f3f91136ca9438336e5eb824ce31555ac116fae Mon Sep 17 00:00:00 2001 From: Anton Gavrilov Date: Fri, 28 Jul 2017 13:41:51 +0200 Subject: [PATCH 7/9] Transaction ids made optional in order to reflect not transactional traces --- ethcore/src/tests/trace.rs | 26 +++++++++---- ethcore/src/trace/db.rs | 55 +++++++++++++++++++--------- ethcore/src/trace/types/localized.rs | 4 +- rpc/src/v1/tests/mocked/traces.rs | 4 +- rpc/src/v1/types/trace.rs | 34 ++++++++--------- 5 files changed, 78 insertions(+), 45 deletions(-) diff --git a/ethcore/src/tests/trace.rs b/ethcore/src/tests/trace.rs index 2389a3c2e..c345d9502 100644 --- a/ethcore/src/tests/trace.rs +++ b/ethcore/src/tests/trace.rs @@ -40,7 +40,7 @@ fn can_trace_block_and_uncle_reward() { let spec = Spec::new_test_with_reward(); let engine = &*spec.engine; - /// Create client + // Create client let db_config = DatabaseConfig::with_columns(::db::NUM_COLUMNS); let mut client_config = ClientConfig::default(); client_config.tracing.enabled = true; @@ -53,7 +53,15 @@ fn can_trace_block_and_uncle_reward() { IoChannel::disconnected(), ).unwrap(); - /// Create test data + // 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; @@ -64,7 +72,7 @@ fn can_trace_block_and_uncle_reward() { let kp = KeyPair::from_secret_slice(&"".sha3()).unwrap(); let author = kp.address(); - /// Add root block first + // Add root block first let mut root_block = OpenBlock::new( engine, Default::default(), @@ -93,7 +101,7 @@ fn can_trace_block_and_uncle_reward() { last_hashes.push(last_header.hash()); - /// Add parent block + // Add parent block let mut parent_block = OpenBlock::new( engine, Default::default(), @@ -121,7 +129,7 @@ fn can_trace_block_and_uncle_reward() { last_hashes.push(last_header.hash()); - /// Add testing block with transactions and uncles + // Add testing block with transaction and uncle let mut block = OpenBlock::new( engine, Default::default(), @@ -172,7 +180,7 @@ fn can_trace_block_and_uncle_reward() { client.flush_queue(); client.import_verified_blocks(); - /// Filter the resuliting data + // Test0. Check overall filter let filter = TraceFilter { range: (BlockId::Number(1)..BlockId::Number(3)), from_address: vec![], @@ -180,7 +188,7 @@ fn can_trace_block_and_uncle_reward() { }; let traces = client.filter_traces(filter); - assert!(traces.is_some(), "Traces should be present"); + 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, @@ -192,4 +200,8 @@ fn can_trace_block_and_uncle_reward() { _ => 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); } \ No newline at end of file diff --git a/ethcore/src/trace/db.rs b/ethcore/src/trace/db.rs index afb0fef49..504648d38 100644 --- a/ethcore/src/trace/db.rs +++ b/ethcore/src/trace/db.rs @@ -215,7 +215,20 @@ impl TraceDB where T: DatabaseExtras { block_number: BlockNumber, tx_number: usize ) -> Vec { - let tx_hash = self.extras.transaction_hash(block_number, tx_number); + let trace_tx_number; + let trace_tx_hash; + + match self.extras.transaction_hash(block_number, tx_number) { + Some(hash) => { + trace_tx_hash = Some(hash.clone()); + trace_tx_number = Some(tx_number); + }, + None => { + //None means trace without transaction (reward) + trace_tx_hash = None; + trace_tx_number = None; + } + } let flat_traces: Vec = traces.into(); flat_traces.into_iter() @@ -226,12 +239,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: match tx_hash { - Some(hash) => hash.clone(), - /// None tx hash means non transaction's trace - None => 0.into(), - }, + transaction_number: trace_tx_number, + transaction_hash: trace_tx_hash, block_number: block_number, block_hash: block_hash }), @@ -324,8 +333,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, } @@ -348,8 +357,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 }) @@ -366,8 +375,20 @@ 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; + let trace_tx_hash; + + match self.extras.transaction_hash(block_number, tx_position) { + Some(hash) => { + trace_tx_hash = Some(hash.clone()); + trace_tx_number = Some(tx_position); + }, + None => { + //None means trace without transaction (reward) + trace_tx_hash = None; + trace_tx_number = None; + } + } traces.into_iter() .map(|trace| LocalizedTrace { @@ -375,8 +396,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, }) @@ -546,8 +567,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/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/rpc/src/v1/tests/mocked/traces.rs b/rpc/src/v1/tests/mocked/traces.rs index b1b297eb1..c87595ad1 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 d64a0347a..5c656f4ec 100644 --- a/rpc/src/v1/types/trace.rs +++ b/rpc/src/v1/types/trace.rs @@ -472,9 +472,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 @@ -531,8 +531,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(), } @@ -665,8 +665,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(), }; @@ -688,8 +688,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(), }; @@ -713,8 +713,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(), }; @@ -734,8 +734,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(), }; @@ -754,8 +754,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(), }; @@ -774,13 +774,13 @@ mod tests { result: Res::None, trace_address: vec![10], subtraces: 1, - transaction_position: 11, - transaction_hash: 12.into(), + 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":{"miner":"0x0000000000000000000000000000000000000004","value":"0x6","rewardType":"block"},"result":null,"traceAddress":[10],"subtraces":1,"transactionPosition":11,"transactionHash":"0x000000000000000000000000000000000000000000000000000000000000000c","blockNumber":13,"blockHash":"0x000000000000000000000000000000000000000000000000000000000000000e"}"#); + assert_eq!(serialized, r#"{"type":"reward","action":{"miner":"0x0000000000000000000000000000000000000004","value":"0x6","rewardType":"block"},"result":null,"traceAddress":[10],"subtraces":1,"transactionPosition":null,"transactionHash":null,"blockNumber":13,"blockHash":"0x000000000000000000000000000000000000000000000000000000000000000e"}"#); } #[test] From 94efa3ac193a724aef25895735695cd7b77f50df Mon Sep 17 00:00:00 2001 From: Anton Gavrilov Date: Mon, 31 Jul 2017 12:06:38 +0200 Subject: [PATCH 8/9] Miner field renamed to author --- ethcore/src/trace/executive_tracer.rs | 4 ++-- ethcore/src/trace/mod.rs | 2 +- ethcore/src/trace/types/filter.rs | 4 ++-- ethcore/src/trace/types/flat.rs | 4 ++-- ethcore/src/trace/types/trace.rs | 10 +++++----- rpc/src/v1/types/trace.rs | 10 +++++----- 6 files changed, 17 insertions(+), 17 deletions(-) diff --git a/ethcore/src/trace/executive_tracer.rs b/ethcore/src/trace/executive_tracer.rs index 65d8ebe3e..a6a5a95ea 100644 --- a/ethcore/src/trace/executive_tracer.rs +++ b/ethcore/src/trace/executive_tracer.rs @@ -163,11 +163,11 @@ impl Tracer for ExecutiveTracer { self.traces.push(trace); } - fn trace_reward(&mut self, miner: Address, value: U256, reward_type: RewardType) { + fn trace_reward(&mut self, author: Address, value: U256, reward_type: RewardType) { let trace = FlatTrace { subtraces: 0, action: Action::Reward(Reward { - miner: miner, + author: author, value: value, reward_type: reward_type, }), diff --git a/ethcore/src/trace/mod.rs b/ethcore/src/trace/mod.rs index 2b318a260..e3c8a9bf0 100644 --- a/ethcore/src/trace/mod.rs +++ b/ethcore/src/trace/mod.rs @@ -82,7 +82,7 @@ pub trait Tracer: Send { fn trace_suicide(&mut self, address: Address, balance: U256, refund_address: Address); /// Stores reward info. - fn trace_reward(&mut self, miner: Address, value: U256, reward_type: RewardType); + 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/types/filter.rs b/ethcore/src/trace/types/filter.rs index 68ab30a31..9ab553eca 100644 --- a/ethcore/src/trace/types/filter.rs +++ b/ethcore/src/trace/types/filter.rs @@ -130,7 +130,7 @@ impl Filter { from_matches && to_matches }, Action::Reward(ref reward) => { - let to_matches = self.to_address.matches(&reward.miner); + let to_matches = self.to_address.matches(&reward.author); to_matches } } @@ -348,7 +348,7 @@ mod tests { let trace = FlatTrace { action: Action::Reward(Reward { - miner: 2.into(), + author: 2.into(), value: 100.into(), reward_type: RewardType::Block, }), diff --git a/ethcore/src/trace/types/flat.rs b/ethcore/src/trace/types/flat.rs index 29d0eddb9..329efc94b 100644 --- a/ethcore/src/trace/types/flat.rs +++ b/ethcore/src/trace/types/flat.rs @@ -241,7 +241,7 @@ mod tests { let flat_trace3 = FlatTrace { action: Action::Reward(Reward { - miner: "412fda7643b37d436cb40628f6dbbb80a07267ed".parse().unwrap(), + author: "412fda7643b37d436cb40628f6dbbb80a07267ed".parse().unwrap(), value: 10.into(), reward_type: RewardType::Uncle, }), @@ -252,7 +252,7 @@ mod tests { let flat_trace4 = FlatTrace { action: Action::Reward(Reward { - miner: "412fda7643b37d436cb40628f6dbbb80a07267ed".parse().unwrap(), + author: "412fda7643b37d436cb40628f6dbbb80a07267ed".parse().unwrap(), value: 10.into(), reward_type: RewardType::Block, }), diff --git a/ethcore/src/trace/types/trace.rs b/ethcore/src/trace/types/trace.rs index faa4ca78f..879f51e49 100644 --- a/ethcore/src/trace/types/trace.rs +++ b/ethcore/src/trace/types/trace.rs @@ -256,8 +256,8 @@ impl Decodable for RewardType { #[derive(Debug, Clone, PartialEq)] #[cfg_attr(feature = "ipc", binary)] pub struct Reward { - /// Miner's address. - pub miner: Address, + /// Author's address. + pub author: Address, /// Reward amount. pub value: U256, /// Reward type. @@ -267,14 +267,14 @@ pub struct Reward { impl Reward { /// Return reward action bloom. pub fn bloom(&self) -> LogBloom { - LogBloom::from_bloomed(&self.miner.sha3()) + LogBloom::from_bloomed(&self.author.sha3()) } } impl Encodable for Reward { fn rlp_append(&self, s: &mut RlpStream) { s.begin_list(3); - s.append(&self.miner); + s.append(&self.author); s.append(&self.value); s.append(&self.reward_type); } @@ -283,7 +283,7 @@ impl Encodable for Reward { impl Decodable for Reward { fn decode(rlp: &UntrustedRlp) -> Result { let res = Reward { - miner: rlp.val_at(0)?, + author: rlp.val_at(0)?, value: rlp.val_at(1)?, reward_type: rlp.val_at(2)?, }; diff --git a/rpc/src/v1/types/trace.rs b/rpc/src/v1/types/trace.rs index 5c656f4ec..aafe39f03 100644 --- a/rpc/src/v1/types/trace.rs +++ b/rpc/src/v1/types/trace.rs @@ -327,8 +327,8 @@ impl From for RewardType { /// Reward action #[derive(Debug, Serialize)] pub struct Reward { - /// Miner's address. - pub miner: H160, + /// Author's address. + pub author: H160, /// Reward amount. pub value: U256, /// Reward type. @@ -339,7 +339,7 @@ pub struct Reward { impl From for Reward { fn from(r: trace::Reward) -> Self { Reward { - miner: r.miner.into(), + author: r.author.into(), value: r.value.into(), reward_type: r.reward_type.into(), } @@ -767,7 +767,7 @@ mod tests { fn test_trace_reward_serialize() { let t = LocalizedTrace { action: Action::Reward(Reward { - miner: 4.into(), + author: 4.into(), value: 6.into(), reward_type: RewardType::Block, }), @@ -780,7 +780,7 @@ mod tests { block_hash: 14.into(), }; let serialized = serde_json::to_string(&t).unwrap(); - assert_eq!(serialized, r#"{"type":"reward","action":{"miner":"0x0000000000000000000000000000000000000004","value":"0x6","rewardType":"block"},"result":null,"traceAddress":[10],"subtraces":1,"transactionPosition":null,"transactionHash":null,"blockNumber":13,"blockHash":"0x000000000000000000000000000000000000000000000000000000000000000e"}"#); + 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] From bb043ba2fdcc2b5d049ca8c5f5db233cc5f82aaf Mon Sep 17 00:00:00 2001 From: Anton Gavrilov Date: Mon, 31 Jul 2017 12:23:47 +0200 Subject: [PATCH 9/9] Tabs corrected --- ethcore/res/null_morden.json | 2 +- ethcore/res/null_morden_with_reward.json | 2 +- ethcore/src/tests/trace.rs | 33 ++++++++++++------------ 3 files changed, 18 insertions(+), 19 deletions(-) diff --git a/ethcore/res/null_morden.json b/ethcore/res/null_morden.json index 62b9682dc..0b7ff78c1 100644 --- a/ethcore/res/null_morden.json +++ b/ethcore/res/null_morden.json @@ -8,7 +8,7 @@ "maximumExtraDataSize": "0x20", "minGasLimit": "0x1388", "networkID" : "0x2", - "applyReward": false + "applyReward": false }, "genesis": { "seal": { diff --git a/ethcore/res/null_morden_with_reward.json b/ethcore/res/null_morden_with_reward.json index 724e58a7a..cf22092c4 100644 --- a/ethcore/res/null_morden_with_reward.json +++ b/ethcore/res/null_morden_with_reward.json @@ -8,7 +8,7 @@ "maximumExtraDataSize": "0x20", "minGasLimit": "0x1388", "networkID" : "0x2", - "applyReward": true + "applyReward": true }, "genesis": { "seal": { diff --git a/ethcore/src/tests/trace.rs b/ethcore/src/tests/trace.rs index c345d9502..b27512aa1 100644 --- a/ethcore/src/tests/trace.rs +++ b/ethcore/src/tests/trace.rs @@ -37,15 +37,15 @@ 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; + 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( + 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, @@ -62,8 +62,8 @@ fn can_trace_block_and_uncle_reward() { // | // 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 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(); @@ -130,7 +130,7 @@ fn can_trace_block_and_uncle_reward() { last_hashes.push(last_header.hash()); // Add testing block with transaction and uncle - let mut block = OpenBlock::new( + let mut block = OpenBlock::new( engine, Default::default(), true, @@ -159,35 +159,34 @@ fn can_trace_block_and_uncle_reward() { n += 1; } - let mut uncle = Header::new(); - let uncle_author: Address = "ef2d6d194084c2de36e0dabfce45d046b37d1106".into(); - uncle.set_author(uncle_author); + 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(); + block.push_uncle(uncle).unwrap(); - let block = block.close_and_lock().seal(engine, vec![]).unwrap(); + let block = block.close_and_lock().seal(engine, vec![]).unwrap(); let res = client.import_block(block.rlp_bytes()); - if res.is_err() { + if res.is_err() { panic!("error importing block: {:#?}", res.err().unwrap()); } block.drain(); - client.flush_queue(); client.import_verified_blocks(); - // Test0. Check overall filter + // 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); + 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 { @@ -204,4 +203,4 @@ fn can_trace_block_and_uncle_reward() { // Test1. Check block filter let traces = client.block_traces(BlockId::Number(3)); assert_eq!(traces.unwrap().len(), 3); -} \ No newline at end of file +}