Refactor ethcore::client::TransactResult to use it inside std::result::Result (#10366)
* Refactor TransactResult * Adapt evmbin and tests
This commit is contained in:
parent
afc1b72611
commit
1871275ecd
@ -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(
|
||||||
|
TransactErr{
|
||||||
state_root: *self.state.root(),
|
state_root: *self.state.root(),
|
||||||
error: error.into(),
|
error: error.into(),
|
||||||
end_state: (self.dump_state)(&self.state),
|
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
|
||||||
state_root: H256,
|
pub state_root: H256,
|
||||||
/// Amount of gas left
|
/// Amount of gas left
|
||||||
gas_left: U256,
|
pub gas_left: U256,
|
||||||
/// Output
|
/// Output
|
||||||
output: Vec<u8>,
|
pub output: Vec<u8>,
|
||||||
/// Traces
|
/// Traces
|
||||||
trace: Vec<T>,
|
pub trace: Vec<T>,
|
||||||
/// VM Traces
|
/// VM Traces
|
||||||
vm_trace: Option<V>,
|
pub vm_trace: Option<V>,
|
||||||
/// Created contract address (if any)
|
/// Created contract address (if any)
|
||||||
contract_address: Option<H160>,
|
pub contract_address: Option<H160>,
|
||||||
/// Generated logs
|
/// Generated logs
|
||||||
logs: Vec<log_entry::LogEntry>,
|
pub logs: Vec<log_entry::LogEntry>,
|
||||||
/// outcome
|
/// outcome
|
||||||
outcome: receipt::TransactionOutcome,
|
pub outcome: receipt::TransactionOutcome,
|
||||||
/// end state if needed
|
/// end state if needed
|
||||||
end_state: Option<pod_state::PodState>,
|
pub end_state: Option<pod_state::PodState>,
|
||||||
},
|
}
|
||||||
/// Transaction failed to run
|
|
||||||
Err {
|
/// To be returned inside a std::result::Result::Err after a failed
|
||||||
/// State root
|
/// transaction.
|
||||||
state_root: H256,
|
#[allow(dead_code)]
|
||||||
/// Execution error
|
pub struct TransactErr {
|
||||||
error: ::error::Error,
|
/// State root
|
||||||
/// end state if needed
|
pub state_root: H256,
|
||||||
end_state: Option<pod_state::PodState>,
|
/// Execution error
|
||||||
},
|
pub error: ::error::Error,
|
||||||
|
/// end state if needed
|
||||||
|
pub end_state: Option<pod_state::PodState>,
|
||||||
}
|
}
|
||||||
|
@ -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};
|
||||||
|
@ -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(_) => {
|
||||||
|
@ -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)
|
||||||
|
Loading…
Reference in New Issue
Block a user