Refactor ethcore::client::TransactResult to use it inside std::result::Result (#10366)

* Refactor TransactResult

* Adapt evmbin and tests
This commit is contained in:
elferdo 2019-02-26 13:49:33 +01:00 committed by Marek Kotewicz
parent afc1b72611
commit 1871275ecd
4 changed files with 53 additions and 51 deletions

View File

@ -241,16 +241,17 @@ impl<'a> EvmTestClient<'a> {
transaction: transaction::SignedTransaction, transaction: transaction::SignedTransaction,
tracer: T, tracer: T,
vm_tracer: V, vm_tracer: V,
) -> TransactResult<T::Output, V::Output> { ) -> std::result::Result<TransactSuccess<T::Output, V::Output>, TransactErr> {
let initial_gas = transaction.gas; let initial_gas = transaction.gas;
// Verify transaction // Verify transaction
let is_ok = transaction.verify_basic(true, None, false); let is_ok = transaction.verify_basic(true, None, false);
if let Err(error) = is_ok { if let Err(error) = is_ok {
return TransactResult::Err { return Err(
state_root: *self.state.root(), TransactErr{
error: error.into(), state_root: *self.state.root(),
end_state: (self.dump_state)(&self.state), error: error.into(),
}; end_state: (self.dump_state)(&self.state),
});
} }
// Apply transaction // Apply transaction
@ -283,7 +284,7 @@ impl<'a> EvmTestClient<'a> {
match result { match result {
Ok(result) => { Ok(result) => {
TransactResult::Ok { Ok(TransactSuccess {
state_root, state_root,
gas_left: initial_gas - result.receipt.gas_used, gas_left: initial_gas - result.receipt.gas_used,
outcome: result.receipt.outcome, outcome: result.receipt.outcome,
@ -298,47 +299,48 @@ impl<'a> EvmTestClient<'a> {
}, },
end_state, end_state,
} }
}, )},
Err(error) => TransactResult::Err { Err(error) => Err(TransactErr {
state_root, state_root,
error, error,
end_state, end_state,
}, }),
} }
} }
} }
/// A result of applying transaction to the state. /// To be returned inside a std::result::Result::Ok after a successful
#[derive(Debug)] /// transaction completed.
pub enum TransactResult<T, V> { #[allow(dead_code)]
/// Successful execution pub struct TransactSuccess<T, V> {
Ok { /// State root
/// State root pub state_root: H256,
state_root: H256, /// Amount of gas left
/// Amount of gas left pub gas_left: U256,
gas_left: U256, /// Output
/// Output pub output: Vec<u8>,
output: Vec<u8>, /// Traces
/// Traces pub trace: Vec<T>,
trace: Vec<T>, /// VM Traces
/// VM Traces pub vm_trace: Option<V>,
vm_trace: Option<V>, /// Created contract address (if any)
/// Created contract address (if any) pub contract_address: Option<H160>,
contract_address: Option<H160>, /// Generated logs
/// Generated logs pub logs: Vec<log_entry::LogEntry>,
logs: Vec<log_entry::LogEntry>, /// outcome
/// outcome pub outcome: receipt::TransactionOutcome,
outcome: receipt::TransactionOutcome, /// end state if needed
/// end state if needed pub end_state: Option<pod_state::PodState>,
end_state: Option<pod_state::PodState>, }
},
/// Transaction failed to run /// To be returned inside a std::result::Result::Err after a failed
Err { /// transaction.
/// State root #[allow(dead_code)]
state_root: H256, pub struct TransactErr {
/// Execution error /// State root
error: ::error::Error, pub state_root: H256,
/// end state if needed /// Execution error
end_state: Option<pod_state::PodState>, pub error: ::error::Error,
}, /// end state if needed
pub end_state: Option<pod_state::PodState>,
} }

View File

@ -30,7 +30,7 @@ mod trace;
pub use self::client::*; pub use self::client::*;
pub use self::config::{Mode, ClientConfig, DatabaseCompactionProfile, BlockChainConfig, VMType}; pub use self::config::{Mode, ClientConfig, DatabaseCompactionProfile, BlockChainConfig, VMType};
#[cfg(any(test, feature = "test-helpers"))] #[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; pub use self::io_message::ClientIoMessage;
#[cfg(any(test, feature = "test-helpers"))] #[cfg(any(test, feature = "test-helpers"))]
pub use self::test_client::{TestBlockChainClient, EachBlockWith}; pub use self::test_client::{TestBlockChainClient, EachBlockWith};

View File

@ -18,7 +18,7 @@ use std::path::Path;
use super::test_common::*; use super::test_common::*;
use pod_state::PodState; use pod_state::PodState;
use trace; use trace;
use client::{EvmTestClient, EvmTestError, TransactResult}; use client::{EvmTestClient, EvmTestError, TransactErr, TransactSuccess};
use ethjson; use ethjson;
use types::transaction::SignedTransaction; use types::transaction::SignedTransaction;
use vm::EnvInfo; use vm::EnvInfo;
@ -90,18 +90,18 @@ pub fn json_chain_test<H: FnMut(&str, HookType)>(json_data: &[u8], start_stop_ho
flushln!("{} fail", info); flushln!("{} fail", info);
failed.push(name.clone()); 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); println!("{} !!! State mismatch (got: {}, expect: {}", info, state_root, post_root);
flushln!("{} fail", info); flushln!("{} fail", info);
failed.push(name.clone()); 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!("{} !!! State mismatch (got: {}, expect: {}", info, state_root, post_root);
println!("{} !!! Execution error: {:?}", info, error); println!("{} !!! Execution error: {:?}", info, error);
flushln!("{} fail", info); flushln!("{} fail", info);
failed.push(name.clone()); failed.push(name.clone());
}, },
Ok(TransactResult::Err { error, .. }) => { Ok(Err(TransactErr { error, .. })) => {
flushln!("{} ok ({:?})", info, error); flushln!("{} ok ({:?})", info, error);
}, },
Ok(_) => { Ok(_) => {

View File

@ -18,7 +18,7 @@
use std::time::{Instant, Duration}; use std::time::{Instant, Duration};
use ethereum_types::{H256, U256}; 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 ethcore::{state, state_db, trace, spec, pod_state, TrieSpec};
use ethjson; use ethjson;
use types::transaction; use types::transaction;
@ -130,7 +130,7 @@ pub fn run_transaction<T: Informant>(
let result = run(&spec, trie_spec, transaction.gas, pre_state, |mut client| { let result = run(&spec, trie_spec, transaction.gas, pre_state, |mut client| {
let result = client.transact(env_info, transaction, trace::NoopTracer, informant); let result = client.transact(env_info, transaction, trace::NoopTracer, informant);
match result { 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 { if state_root != post_root {
(Err(EvmTestError::PostCondition(format!( (Err(EvmTestError::PostCondition(format!(
"State root mismatch (got: {:#x}, expected: {:#x})", "State root mismatch (got: {:#x}, expected: {:#x})",
@ -141,7 +141,7 @@ pub fn run_transaction<T: Informant>(
(Ok(output), state_root, end_state, Some(gas_left), vm_trace) (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!( (Err(EvmTestError::PostCondition(format!(
"Unexpected execution error: {:?}", error "Unexpected execution error: {:?}", error
))), state_root, end_state, None, None) ))), state_root, end_state, None, None)