From 10d6ca7df5d9b9e2fcd28c9c5c271c918675423e 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 | 29 +++++++++++++++++++++++------ 3 files changed, 27 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index cc5000b79d7..a806a2689ef 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4820,6 +4820,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..b9a3e9a818a 100644 --- a/ethcore/spec/src/spec.rs +++ b/ethcore/spec/src/spec.rs @@ -51,6 +51,7 @@ use maplit::btreeset; use null_engine::NullEngine; use pod::PodState; use rlp::{Rlp, RlpStream}; +use serde::{Serialize, Serializer}; use trace::{NoopTracer, NoopVMTracer}; use trie_vm_factories::Factories; use vm::{EnvInfo, ActionType, ActionValue, ActionParams, ParamsType}; @@ -244,16 +245,36 @@ pub struct Spec { } /// Part of `Spec`. Describes the hardcoded synchronization parameters. +#[derive(Serialize)] pub struct SpecHardcodedSync { /// Header of the block to jump to for hardcoded sync, and total difficulty. + #[serde(serialize_with = "serialize_header")] pub header: encoded::Header, /// Total difficulty of the block to jump to. + #[serde(rename = "totalDifficulty", serialize_with = "serialize_total_difficulty")] pub total_difficulty: U256, /// List of hardcoded CHTs, in order. If `hardcoded_sync` is set, the CHTs should include the /// header of `hardcoded_sync`. + #[serde(rename = "CHTs")] pub chts: Vec, } +fn serialize_total_difficulty(total_difficulty: &U256, serializer: S) -> Result + where + S: Serializer, +{ + let total_difficulty_str = format!("{:?}", total_difficulty); + serializer.serialize_str(&total_difficulty_str) +} + +fn serialize_header(header: &encoded::Header, serializer: S) -> Result +where + S: Serializer, +{ + let header_str = format!("{:x}", header); + serializer.serialize_str(&header_str) +} + impl From for SpecHardcodedSync { fn from(sync: ethjson::spec::HardcodedSync) -> Self { SpecHardcodedSync { @@ -266,12 +287,8 @@ impl From for SpecHardcodedSync { 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, "}}") + let serialized = serde_json::to_string_pretty(&self).unwrap(); + writeln!(f, "{}", serialized) } }