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

refactor: Related #9459 - evmbin: replace untyped json! macro with fully typed serde serialization using Rust structs #10657

Merged
merged 44 commits into from
Jul 3, 2019
Merged
Show file tree
Hide file tree
Changes from 26 commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
11b1a75
fix: Replace multirust with rustup wince multirust is deprecated
ltfschoen May 13, 2019
39965bf
docs: Update evmbin Rust docs and code comments
ltfschoen May 13, 2019
946597c
WIP: Add Response struct. Initial step using serde to serialize inste…
ltfschoen May 14, 2019
369846b
fix: Update Response struct types to be string after formatting
ltfschoen May 14, 2019
52571cd
fix: Fix move out of borrowed content error by cloning informant
ltfschoen May 14, 2019
98b83c0
refactor: Change from camelcase to snake case to fix linting errors
ltfschoen May 14, 2019
a81a1b8
restore: Restore some code since now covered in separate PR #10658
ltfschoen May 14, 2019
b65215f
restore: Restore original Rustdocs of evmbin
ltfschoen May 14, 2019
53e6404
WIP
ltfschoen Jun 11, 2019
42ebcf7
add Clone type
ltfschoen Jun 11, 2019
0008569
add newlines to end of json files
ltfschoen Jun 11, 2019
a39bd56
remove uml file that was unintentionally commited
ltfschoen Jun 11, 2019
b8b0adb
rename chain spec to state test JSON fle
ltfschoen Jun 11, 2019
2e60ccf
remove log. fix indentation
ltfschoen Jun 12, 2019
794bac5
revert: Restore indentation now handled by separate PR #10740
ltfschoen Jun 12, 2019
f037f05
remove state test json files as moved to PR #10742
ltfschoen Jun 12, 2019
a951287
revert changes in info.rs since covered in PR #10742
ltfschoen Jun 12, 2019
1c36280
revert changes to main.rs since covered in PR #10742
ltfschoen Jun 12, 2019
669996e
Merge branch 'master' into luke-9459-evmbin-serde
ltfschoen Jun 12, 2019
5aeb6c6
revert newlines back to master
ltfschoen Jun 12, 2019
6abccdf
revert newlines back to master2
ltfschoen Jun 12, 2019
fce1163
refactor: Rename Response to TraceData
ltfschoen Jun 12, 2019
3f15c0d
fix: Remove Clone and replace with lifetimes. Update tests since not …
ltfschoen Jun 12, 2019
c8cc9f7
refactor: Change all json! to typed serde
ltfschoen Jun 13, 2019
429c919
docs: Update rustdocs. Remove fixme
ltfschoen Jun 13, 2019
cefb91a
fix: Add missing semicolons from printf
ltfschoen Jun 13, 2019
024a759
fix: Change style from unwrap to expect in evmbin/src/display/json.rs
ltfschoen Jun 14, 2019
a7c6171
fix: Change style from unwrap to expect in evmbin/src/display/std_jso…
ltfschoen Jun 14, 2019
ab4e9b3
revert updating module comments as will be done in separate PR #10742…
ltfschoen Jun 14, 2019
8ab7a78
review-fix: Remove useless reference
ltfschoen Jun 17, 2019
c743d33
Remove unncessary use of format macro
ltfschoen Jun 18, 2019
0efc9b6
Update evmbin/src/display/json.rs
ltfschoen Jun 18, 2019
12952b6
refactor: Update evmbin/src/display/json.rs with serialization in set…
ltfschoen Jun 19, 2019
dc5c332
refactor: Update evmbin/src/display/json.rs with serialization in set…
ltfschoen Jun 19, 2019
b879cf9
refactor: Update evmbin/src/display/std_json.rs with serialization in…
ltfschoen Jun 19, 2019
5b8d39f
refactor: Update evmbin/src/display/std_json.rs with serialization in…
ltfschoen Jun 19, 2019
93291b1
refactor: Update evmbin/src/display/std_json.rs with serialization fo…
ltfschoen Jun 19, 2019
15d6dbd
refactor: Update evmbin/src/display/std_json.rs with serialization fo…
ltfschoen Jun 19, 2019
23e2c80
refactor: Update evmbin/src/display/std_json.rs with serialization fo…
ltfschoen Jun 19, 2019
d71fa06
refactor: Rename structs and variables. Remove space. Simplify Messag…
ltfschoen Jun 19, 2019
58297de
refactor: Captialize expect message
ltfschoen Jun 19, 2019
e7d25d9
revert to previous struct name TraceDataStateRoot
ltfschoen Jun 19, 2019
3dd615c
refactor: Simplify variable for consistency
ltfschoen Jun 20, 2019
95e5d56
Update accounts/ethstore/src/json/crypto.rs
ltfschoen Jul 3, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 77 additions & 25 deletions evmbin/src/display/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with Parity Ethereum. If not, see <http://www.gnu.org/licenses/>.

//! JSON VM output.
//! Log VM instruction output data traces from a JSON formatting informant.

use std::collections::HashMap;
use std::mem;
Expand Down 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 InitMessage<'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()
// Reference: https://serde.rs/attr-skip-serializing.html
dvdplm marked this conversation as resolved.
Show resolved Hide resolved
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).unwrap()
ltfschoen marked this conversation as resolved.
Show resolved Hide resolved
}
}

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

fn before_test(&mut self, name: &str, action: &str) {
println!("{}", json!({"action": action, "test": name}));
let init_message =
InitMessage {
action: &format!("{}", action),
dvdplm marked this conversation as resolved.
Show resolved Hide resolved
test: &format!("{}", name),
}
;

println!("{:?}", init_message);
ltfschoen marked this conversation as resolved.
Show resolved Hide resolved
}

fn set_gas(&mut self, gas: U256) {
Expand All @@ -93,26 +141,30 @@ 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()),
dvdplm marked this conversation as resolved.
Show resolved Hide resolved
gas_used: &format!("{:#x}", success.gas_used),
time: &display::as_micros(&success.time),
}
;

println!("{}", success_msg)
println!("{:?}", message_success);
dvdplm marked this conversation as resolved.
Show resolved Hide resolved
ltfschoen marked this conversation as resolved.
Show resolved Hide resolved
},
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)
println!("{:?}", message_failure);
ltfschoen marked this conversation as resolved.
Show resolved Hide resolved
},
}
}
Expand Down
2 changes: 1 addition & 1 deletion evmbin/src/display/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with Parity Ethereum. If not, see <http://www.gnu.org/licenses/>.

//! Simple VM output.
//! Log VM instruction output data traces from a simple formatting informant.

use ethcore::trace;
use bytes::ToPretty;
Expand Down
Loading