Fixing JSON WiP. Refactor & Write tests.

This commit is contained in:
Tomasz Drwięga 2017-10-17 14:54:56 +02:00
parent b0c15497e1
commit 2d10cd0ecc
No known key found for this signature in database
GPG Key ID: D066F497E62CAF66
3 changed files with 31 additions and 5 deletions

View File

@ -219,7 +219,7 @@ impl<'a> EvmTestClient<'a> {
TransactResult::Ok { TransactResult::Ok {
state_root: *self.state.root(), state_root: *self.state.root(),
gas_left: initial_gas - result.receipt.gas_used, gas_left: initial_gas - result.receipt.gas_used,
output: result.output output: result.output,
} }
}, },
Err(error) => TransactResult::Err { Err(error) => TransactResult::Err {

View File

@ -37,6 +37,8 @@ pub struct Informant {
stack: Vec<U256>, stack: Vec<U256>,
memory: Vec<u8>, memory: Vec<u8>,
storage: HashMap<H256, H256>, storage: HashMap<H256, H256>,
traces: Vec<String>,
subtraces: Vec<String>,
} }
impl Informant { impl Informant {
@ -57,6 +59,14 @@ impl Informant {
} }
} }
impl Drop for Informant {
fn drop(&mut self) {
for trace in &self.traces {
println!("{}", trace);
}
}
}
impl vm::Informant for Informant { impl vm::Informant for Informant {
fn before_test(&self, name: &str, action: &str) { fn before_test(&self, name: &str, action: &str) {
println!( println!(
@ -104,18 +114,19 @@ impl trace::VMTracer for Informant {
fn trace_executed(&mut self, gas_used: U256, stack_push: &[U256], mem_diff: Option<(usize, &[u8])>, store_diff: Option<(U256, U256)>) { fn trace_executed(&mut self, gas_used: U256, stack_push: &[U256], mem_diff: Option<(usize, &[u8])>, store_diff: Option<(U256, U256)>) {
let info = ::evm::INSTRUCTIONS[self.instruction as usize]; let info = ::evm::INSTRUCTIONS[self.instruction as usize];
println!( let trace = format!(
"{{\"pc\":{pc},\"op\":{op},\"opName\":\"{name}\",\"gas\":{gas},\"gasCost\":{gas_cost},\"memory\":{memory},\"stack\":{stack},\"storage\":{storage},\"depth\":{depth}}}", "{{\"pc\":{pc},\"op\":{op},\"opName\":\"{name}\",\"gas\":{gas},\"gasCost\":{gas_cost},\"memory\":{memory},\"stack\":{stack},\"storage\":{storage},\"depth\":{depth}}}",
pc = self.pc, pc = self.pc,
op = self.instruction, op = self.instruction,
name = info.name, name = info.name,
gas = display::u256_as_str(&(gas_used + self.gas_cost)), gas = display::u256_as_str(&(gas_used.saturating_add(self.gas_cost))),
gas_cost = display::u256_as_str(&self.gas_cost), gas_cost = display::u256_as_str(&self.gas_cost),
memory = self.memory(), memory = self.memory(),
stack = self.stack(), stack = self.stack(),
storage = self.storage(), storage = self.storage(),
depth = self.depth, depth = self.depth,
); );
self.traces.push(trace);
self.gas_used = gas_used; self.gas_used = gas_used;
@ -133,6 +144,11 @@ impl trace::VMTracer for Informant {
if let Some((pos, val)) = store_diff { if let Some((pos, val)) = store_diff {
self.storage.insert(pos.into(), val.into()); self.storage.insert(pos.into(), val.into());
} }
if !self.subtraces.is_empty() {
self.traces.extend(::std::mem::replace(&mut self.subtraces, vec![]));
}
} }
fn prepare_subtrace(&self, code: &[u8]) -> Self where Self: Sized { fn prepare_subtrace(&self, code: &[u8]) -> Self where Self: Sized {
@ -144,13 +160,23 @@ impl trace::VMTracer for Informant {
} }
fn done_subtrace(&mut self, mut sub: Self) { fn done_subtrace(&mut self, mut sub: Self) {
let subtraces = ::std::mem::replace(&mut sub.traces, vec![]);
if sub.depth == 1 { if sub.depth == 1 {
self.traces.extend(subtraces);
// print last line with final state: // print last line with final state:
sub.gas_cost = 0.into(); sub.gas_cost = 0.into();
let gas_used = sub.gas_used; let gas_used = sub.gas_used;
trace::VMTracer::trace_executed(&mut sub, gas_used, &[], None, None); trace::VMTracer::trace_executed(&mut sub, gas_used, &[], None, None);
} else {
self.subtraces = subtraces;
} }
} }
fn drain(self) -> Option<trace::VMTrace> { None } fn drain(self) -> Option<trace::VMTrace> {
println!("Drain called.");
for trace in &self.traces {
println!("{}", trace);
}
None
}
} }

View File

@ -187,7 +187,7 @@ impl Args {
pub fn gas(&self) -> Result<U256, String> { pub fn gas(&self) -> Result<U256, String> {
match self.flag_gas { match self.flag_gas {
Some(ref gas) => gas.parse().map_err(to_string), Some(ref gas) => gas.parse().map_err(to_string),
None => Ok(!U256::zero()), None => Ok(U256::from(u64::max_value())),
} }
} }