From d6284dc6f4ac678ed6da43ec9d3528ccd0ce2c25 Mon Sep 17 00:00:00 2001 From: Mark Toda Date: Sat, 4 Apr 2020 13:00:13 -0700 Subject: [PATCH] Use serde_json to export hardcoded sync The exported hardcoded sync was previously generating invalid JSON, with the CHT list ending in a trailing comma. In order to remedy this, this commit uses serde_json to serialize the SpecHardcodedSync struct into valid JSON. Fixes #11415 --- Cargo.lock | 2 ++ ethcore/spec/Cargo.toml | 2 ++ ethcore/spec/src/spec.rs | 32 ++++++++++++++++++++++++-------- 3 files changed, 28 insertions(+), 8 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6ac24a5d0e0..f9f97558e3c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4782,6 +4782,8 @@ dependencies = [ "parity-bytes", "pod", "rlp", + "serde", + "serde_json", "tempfile", "trace", "trie-vm-factories", diff --git a/ethcore/spec/Cargo.toml b/ethcore/spec/Cargo.toml index 1c17d0cc0e2..e99d48f8238 100644 --- a/ethcore/spec/Cargo.toml +++ b/ethcore/spec/Cargo.toml @@ -32,6 +32,8 @@ maplit = "1" null-engine = { path = "../engines/null-engine" } pod = { path = "../pod" } rlp = "0.4.2" +serde = "1.0" +serde_json = "1.0" trace = { path = "../trace" } trie-vm-factories = { path = "../trie-vm-factories" } vm = { path = "../vm" } diff --git a/ethcore/spec/src/spec.rs b/ethcore/spec/src/spec.rs index 507082cb06f..5a1938b4353 100644 --- a/ethcore/spec/src/spec.rs +++ b/ethcore/spec/src/spec.rs @@ -51,6 +51,8 @@ use maplit::btreeset; use null_engine::NullEngine; use pod::PodState; use rlp::{Rlp, RlpStream}; +use serde::ser::{Serialize, Serializer, SerializeStruct}; +use serde_json; use trace::{NoopTracer, NoopVMTracer}; use trie_vm_factories::Factories; use vm::{EnvInfo, ActionType, ActionValue, ActionParams, ParamsType}; @@ -264,15 +266,29 @@ impl From for SpecHardcodedSync { } } +impl Serialize for SpecHardcodedSync { + fn serialize(&self, serializer: S) -> Result + where + S: Serializer, + { + let mut serialized = serializer.serialize_struct("SpecHardcodedSync", 3)?; + + let header_str = format!("{:x}", self.header); + serialized.serialize_field("header", &header_str)?; + + let total_difficulty_str = format!("{:?}", self.total_difficulty); + serialized.serialize_field("totalDifficulty", &total_difficulty_str)?; + + serialized.serialize_field("CHTs", &self.chts)?; + serialized.end() + } +} + impl fmt::Display for SpecHardcodedSync { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - writeln!(f, "{{")?; - writeln!(f, r#""header": "{:x}","#, self.header)?; - writeln!(f, r#""totalDifficulty": "{:?}""#, self.total_difficulty)?; - // TODO: #11415 - fix trailing comma for CHTs - writeln!(f, r#""CHTs": {:#?}"#, self.chts.iter().map(|x| format!("{:?}", x)).collect::>())?; - writeln!(f, "}}") - } + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + let serialized = serde_json::to_string_pretty(&self).unwrap(); + writeln!(f, "{}", serialized) + } } fn convert_json_to_spec(