From 1871275ecdf02431bf67d09a1b25be8ff8916e3a Mon Sep 17 00:00:00 2001 From: elferdo Date: Tue, 26 Feb 2019 13:49:33 +0100 Subject: [PATCH] Refactor ethcore::client::TransactResult to use it inside std::result::Result (#10366) * Refactor TransactResult * Adapt evmbin and tests --- ethcore/src/client/evm_test_client.rs | 88 ++++++++++++++------------- ethcore/src/client/mod.rs | 2 +- ethcore/src/json_tests/state.rs | 8 +-- evmbin/src/info.rs | 6 +- 4 files changed, 53 insertions(+), 51 deletions(-) diff --git a/ethcore/src/client/evm_test_client.rs b/ethcore/src/client/evm_test_client.rs index a65e2b313..d6c03e65a 100644 --- a/ethcore/src/client/evm_test_client.rs +++ b/ethcore/src/client/evm_test_client.rs @@ -241,16 +241,17 @@ impl<'a> EvmTestClient<'a> { transaction: transaction::SignedTransaction, tracer: T, vm_tracer: V, - ) -> TransactResult { + ) -> std::result::Result, TransactErr> { let initial_gas = transaction.gas; // Verify transaction let is_ok = transaction.verify_basic(true, None, false); if let Err(error) = is_ok { - return TransactResult::Err { - state_root: *self.state.root(), - error: error.into(), - end_state: (self.dump_state)(&self.state), - }; + return Err( + TransactErr{ + state_root: *self.state.root(), + error: error.into(), + end_state: (self.dump_state)(&self.state), + }); } // Apply transaction @@ -283,7 +284,7 @@ impl<'a> EvmTestClient<'a> { match result { Ok(result) => { - TransactResult::Ok { + Ok(TransactSuccess { state_root, gas_left: initial_gas - result.receipt.gas_used, outcome: result.receipt.outcome, @@ -298,47 +299,48 @@ impl<'a> EvmTestClient<'a> { }, end_state, } - }, - Err(error) => TransactResult::Err { + )}, + Err(error) => Err(TransactErr { state_root, error, end_state, - }, + }), } } } -/// A result of applying transaction to the state. -#[derive(Debug)] -pub enum TransactResult { - /// Successful execution - Ok { - /// State root - state_root: H256, - /// Amount of gas left - gas_left: U256, - /// Output - output: Vec, - /// Traces - trace: Vec, - /// VM Traces - vm_trace: Option, - /// Created contract address (if any) - contract_address: Option, - /// Generated logs - logs: Vec, - /// outcome - outcome: receipt::TransactionOutcome, - /// end state if needed - end_state: Option, - }, - /// Transaction failed to run - Err { - /// State root - state_root: H256, - /// Execution error - error: ::error::Error, - /// end state if needed - end_state: Option, - }, +/// To be returned inside a std::result::Result::Ok after a successful +/// transaction completed. +#[allow(dead_code)] +pub struct TransactSuccess { + /// State root + pub state_root: H256, + /// Amount of gas left + pub gas_left: U256, + /// Output + pub output: Vec, + /// Traces + pub trace: Vec, + /// VM Traces + pub vm_trace: Option, + /// Created contract address (if any) + pub contract_address: Option, + /// Generated logs + pub logs: Vec, + /// outcome + pub outcome: receipt::TransactionOutcome, + /// end state if needed + pub end_state: Option, +} + +/// To be returned inside a std::result::Result::Err after a failed +/// transaction. +#[allow(dead_code)] +pub struct TransactErr { + /// State root + pub state_root: H256, + /// Execution error + pub error: ::error::Error, + /// end state if needed + pub end_state: Option, } diff --git a/ethcore/src/client/mod.rs b/ethcore/src/client/mod.rs index d501ba21f..51b4f53db 100644 --- a/ethcore/src/client/mod.rs +++ b/ethcore/src/client/mod.rs @@ -30,7 +30,7 @@ mod trace; pub use self::client::*; pub use self::config::{Mode, ClientConfig, DatabaseCompactionProfile, BlockChainConfig, VMType}; #[cfg(any(test, feature = "test-helpers"))] -pub use self::evm_test_client::{EvmTestClient, EvmTestError, TransactResult}; +pub use self::evm_test_client::{EvmTestClient, EvmTestError, TransactErr, TransactSuccess}; pub use self::io_message::ClientIoMessage; #[cfg(any(test, feature = "test-helpers"))] pub use self::test_client::{TestBlockChainClient, EachBlockWith}; diff --git a/ethcore/src/json_tests/state.rs b/ethcore/src/json_tests/state.rs index 33fc6d522..c51a2c361 100644 --- a/ethcore/src/json_tests/state.rs +++ b/ethcore/src/json_tests/state.rs @@ -18,7 +18,7 @@ use std::path::Path; use super::test_common::*; use pod_state::PodState; use trace; -use client::{EvmTestClient, EvmTestError, TransactResult}; +use client::{EvmTestClient, EvmTestError, TransactErr, TransactSuccess}; use ethjson; use types::transaction::SignedTransaction; use vm::EnvInfo; @@ -90,18 +90,18 @@ pub fn json_chain_test(json_data: &[u8], start_stop_ho flushln!("{} fail", info); failed.push(name.clone()); }, - Ok(TransactResult::Ok { state_root, .. }) if state_root != post_root => { + Ok(Ok(TransactSuccess { state_root, .. })) if state_root != post_root => { println!("{} !!! State mismatch (got: {}, expect: {}", info, state_root, post_root); flushln!("{} fail", info); failed.push(name.clone()); }, - Ok(TransactResult::Err { state_root, ref error, .. }) if state_root != post_root => { + Ok(Err(TransactErr { state_root, ref error, .. })) if state_root != post_root => { println!("{} !!! State mismatch (got: {}, expect: {}", info, state_root, post_root); println!("{} !!! Execution error: {:?}", info, error); flushln!("{} fail", info); failed.push(name.clone()); }, - Ok(TransactResult::Err { error, .. }) => { + Ok(Err(TransactErr { error, .. })) => { flushln!("{} ok ({:?})", info, error); }, Ok(_) => { diff --git a/evmbin/src/info.rs b/evmbin/src/info.rs index a12cdb16b..74ea3175a 100644 --- a/evmbin/src/info.rs +++ b/evmbin/src/info.rs @@ -18,7 +18,7 @@ use std::time::{Instant, Duration}; use ethereum_types::{H256, U256}; -use ethcore::client::{self, EvmTestClient, EvmTestError, TransactResult}; +use ethcore::client::{self, EvmTestClient, EvmTestError, TransactErr, TransactSuccess}; use ethcore::{state, state_db, trace, spec, pod_state, TrieSpec}; use ethjson; use types::transaction; @@ -130,7 +130,7 @@ pub fn run_transaction( let result = run(&spec, trie_spec, transaction.gas, pre_state, |mut client| { let result = client.transact(env_info, transaction, trace::NoopTracer, informant); match result { - TransactResult::Ok { state_root, gas_left, output, vm_trace, end_state, .. } => { + Ok(TransactSuccess { state_root, gas_left, output, vm_trace, end_state, .. }) => { if state_root != post_root { (Err(EvmTestError::PostCondition(format!( "State root mismatch (got: {:#x}, expected: {:#x})", @@ -141,7 +141,7 @@ pub fn run_transaction( (Ok(output), state_root, end_state, Some(gas_left), vm_trace) } }, - TransactResult::Err { state_root, error, end_state } => { + Err(TransactErr { state_root, error, end_state }) => { (Err(EvmTestError::PostCondition(format!( "Unexpected execution error: {:?}", error ))), state_root, end_state, None, None)