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,
tracer: T,
vm_tracer: V,
) -> TransactResult<T::Output, V::Output> {
) -> std::result::Result<TransactSuccess<T::Output, V::Output>, 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<T, V> {
/// Successful execution
Ok {
/// State root
state_root: H256,
/// Amount of gas left
gas_left: U256,
/// Output
output: Vec<u8>,
/// Traces
trace: Vec<T>,
/// VM Traces
vm_trace: Option<V>,
/// Created contract address (if any)
contract_address: Option<H160>,
/// Generated logs
logs: Vec<log_entry::LogEntry>,
/// outcome
outcome: receipt::TransactionOutcome,
/// end state if needed
end_state: Option<pod_state::PodState>,
},
/// Transaction failed to run
Err {
/// State root
state_root: H256,
/// Execution error
error: ::error::Error,
/// end state if needed
end_state: Option<pod_state::PodState>,
},
/// To be returned inside a std::result::Result::Ok after a successful
/// transaction completed.
#[allow(dead_code)]
pub struct TransactSuccess<T, V> {
/// State root
pub state_root: H256,
/// Amount of gas left
pub gas_left: U256,
/// Output
pub output: Vec<u8>,
/// Traces
pub trace: Vec<T>,
/// VM Traces
pub vm_trace: Option<V>,
/// Created contract address (if any)
pub contract_address: Option<H160>,
/// Generated logs
pub logs: Vec<log_entry::LogEntry>,
/// outcome
pub outcome: receipt::TransactionOutcome,
/// end state if needed
pub end_state: Option<pod_state::PodState>,
}
/// 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<pod_state::PodState>,
}

View File

@@ -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};