Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

Commit

Permalink
refactor: Related #9459 - evmbin: replace untyped json! macro with fu…
Browse files Browse the repository at this point in the history
…lly typed serde serialization using Rust structs (#10657)

* fix: Replace multirust with rustup wince multirust is deprecated

* docs: Update evmbin Rust docs and code comments

* WIP: Add Response struct. Initial step using serde to serialize instead of hardcoding with JSON

* fix: Update Response struct types to be string after formatting

* fix: Fix move out of borrowed content error by cloning informant

* refactor: Change from camelcase to snake case to fix linting errors

* restore: Restore some code since now covered in separate PR #10658

* restore: Restore original Rustdocs of evmbin

* WIP

* add Clone type

* add newlines to end of json files

* remove uml file that was unintentionally commited

* rename chain spec to state test JSON fle

* remove log. fix indentation

* revert: Restore indentation now handled by separate PR #10740

* remove state test json files as moved to PR #10742

* revert changes in info.rs since covered in PR #10742

* revert changes to main.rs since covered in PR #10742

* revert newlines back to master

* revert newlines back to master2

* refactor: Rename Response to TraceData

* fix: Remove Clone and replace with lifetimes. Update tests since not ordered

* refactor: Change all json! to typed serde

* docs: Update rustdocs. Remove fixme

* fix: Add missing semicolons from printf

* fix: Change style from unwrap to expect in evmbin/src/display/json.rs

Co-Authored-By: Andronik Ordian <write@reusable.software>

* fix: Change style from unwrap to expect in evmbin/src/display/std_json.rs

Co-Authored-By: Andronik Ordian <write@reusable.software>

* revert updating module comments as will be done in separate PR #10742 instead

* review-fix: Remove useless reference

* Remove unncessary use of format macro

* Update evmbin/src/display/json.rs

Co-Authored-By: Andronik Ordian <write@reusable.software>

* refactor: Update evmbin/src/display/json.rs with serialization in set_gas success

Co-Authored-By: Andronik Ordian <write@reusable.software>

* refactor: Update evmbin/src/display/json.rs with serialization in set_gas failure

Co-Authored-By: Andronik Ordian <write@reusable.software>

* refactor: Update evmbin/src/display/std_json.rs with serialization in finish for state root

Co-Authored-By: Andronik Ordian <write@reusable.software>

* refactor: Update evmbin/src/display/std_json.rs with serialization in before_test

Co-Authored-By: Andronik Ordian <write@reusable.software>

* refactor: Update evmbin/src/display/std_json.rs with serialization for state root

Co-Authored-By: Andronik Ordian <write@reusable.software>

* refactor: Update evmbin/src/display/std_json.rs with serialization for finish success

Co-Authored-By: Andronik Ordian <write@reusable.software>

* refactor: Update evmbin/src/display/std_json.rs with serialization for finish failure

Co-Authored-By: Andronik Ordian <write@reusable.software>

* refactor: Rename structs and variables. Remove space. Simplify MessageInitial struct

* refactor: Captialize expect message

* revert to previous struct name TraceDataStateRoot

* refactor: Simplify variable for consistency

* Update accounts/ethstore/src/json/crypto.rs

Co-Authored-By: David <dvdplm@gmail.com>
  • Loading branch information
ltfschoen and dvdplm committed Jul 3, 2019
1 parent 8d24b4e commit 02e33c4
Show file tree
Hide file tree
Showing 4 changed files with 225 additions and 100 deletions.
2 changes: 1 addition & 1 deletion accounts/ethstore/src/json/crypto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl str::FromStr for Crypto {

impl From<Crypto> for String {
fn from(c: Crypto) -> Self {
serde_json::to_string(&c).expect("serialization cannot fail, cause all crypto keys are strings")
serde_json::to_string(&c).expect("Serialization cannot fail, because all crypto keys are strings")
}
}

Expand Down
102 changes: 78 additions & 24 deletions evmbin/src/display/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,42 @@ pub struct Informant {
unmatched: bool,
}

#[derive(Serialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct TraceData<'a> {
pc: usize,
op: u8,
op_name: &'a str,
gas: &'a str,
gas_cost: &'a str,
memory: &'a str,
stack: &'a [U256],
storage: &'a HashMap<H256, H256>,
depth: usize,
}

#[derive(Serialize, Debug)]
pub struct MessageInitial<'a> {
action: &'a str,
test: &'a str,
}

#[derive(Serialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct MessageSuccess<'a> {
output: &'a str,
gas_used: &'a str,
time: &'a u64,
}

#[derive(Serialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct MessageFailure<'a> {
error: &'a str,
gas_used: &'a str,
time: &'a u64,
}

impl Informant {
fn with_informant_in_depth<F: Fn(&mut Informant)>(informant: &mut Informant, depth: usize, f: F) {
if depth == 0 {
Expand All @@ -59,25 +95,37 @@ impl Informant {
fn informant_trace(informant: &Informant, gas_used: U256) -> String {
let info = ::evm::Instruction::from_u8(informant.instruction).map(|i| i.info());

json!({
"pc": informant.pc,
"op": informant.instruction,
"opName": info.map(|i| i.name).unwrap_or(""),
"gas": format!("{:#x}", gas_used.saturating_add(informant.gas_cost)),
"gasCost": format!("{:#x}", informant.gas_cost),
"memory": format!("0x{}", informant.memory.to_hex()),
"stack": informant.stack,
"storage": informant.storage,
"depth": informant.depth,
}).to_string()
let trace_data =
TraceData {
pc: informant.pc,
op: informant.instruction,
op_name: info.map(|i| i.name).unwrap_or(""),
gas: &format!("{:#x}", gas_used.saturating_add(informant.gas_cost)),
gas_cost: &format!("{:#x}", informant.gas_cost),
memory: &format!("0x{}", informant.memory.to_hex()),
stack: &informant.stack,
storage: &informant.storage,
depth: informant.depth,
}
;

serde_json::to_string(&trace_data).expect("Serialization cannot fail; qed")
}
}

impl vm::Informant for Informant {
type Sink = ();

fn before_test(&mut self, name: &str, action: &str) {
println!("{}", json!({"action": action, "test": name}));
let message_init =
MessageInitial {
action,
test: &name,
}
;

let s = serde_json::to_string(&message_init).expect("Serialization cannot fail; qed");
println!("{}", s);
}

fn set_gas(&mut self, gas: U256) {
Expand All @@ -93,26 +141,32 @@ impl vm::Informant for Informant {
println!("{}", trace);
}

let success_msg = json!({
"output": format!("0x{}", success.output.to_hex()),
"gasUsed": format!("{:#x}", success.gas_used),
"time": display::as_micros(&success.time),
});
let message_success =
MessageSuccess {
output: &format!("0x{}", success.output.to_hex()),
gas_used: &format!("{:#x}", success.gas_used),
time: &display::as_micros(&success.time),
}
;

println!("{}", success_msg)
let s = serde_json::to_string(&message_success).expect("Serialization cannot fail; qed");
println!("{}", s);
},
Err(failure) => {
for trace in failure.traces.unwrap_or_else(Vec::new) {
println!("{}", trace);
}

let failure_msg = json!({
"error": &failure.error.to_string(),
"gasUsed": format!("{:#x}", failure.gas_used),
"time": display::as_micros(&failure.time),
});
let message_failure =
MessageFailure {
error: &failure.error.to_string(),
gas_used: &format!("{:#x}", failure.gas_used),
time: &display::as_micros(&failure.time),
}
;

println!("{}", failure_msg)
let s = serde_json::to_string(&message_failure).expect("Serialization cannot fail; qed");
println!("{}", s);
},
}
}
Expand Down
Loading

0 comments on commit 02e33c4

Please sign in to comment.