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

Commit 10d6ca7

Browse files
committed
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
1 parent e047bb4 commit 10d6ca7

File tree

3 files changed

+27
-6
lines changed

3 files changed

+27
-6
lines changed

Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ethcore/spec/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ maplit = "1"
3232
null-engine = { path = "../engines/null-engine" }
3333
pod = { path = "../pod" }
3434
rlp = "0.4.2"
35+
serde = "1.0"
36+
serde_json = "1.0"
3537
trace = { path = "../trace" }
3638
trie-vm-factories = { path = "../trie-vm-factories" }
3739
vm = { path = "../vm" }

ethcore/spec/src/spec.rs

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ use maplit::btreeset;
5151
use null_engine::NullEngine;
5252
use pod::PodState;
5353
use rlp::{Rlp, RlpStream};
54+
use serde::{Serialize, Serializer};
5455
use trace::{NoopTracer, NoopVMTracer};
5556
use trie_vm_factories::Factories;
5657
use vm::{EnvInfo, ActionType, ActionValue, ActionParams, ParamsType};
@@ -244,16 +245,36 @@ pub struct Spec {
244245
}
245246

246247
/// Part of `Spec`. Describes the hardcoded synchronization parameters.
248+
#[derive(Serialize)]
247249
pub struct SpecHardcodedSync {
248250
/// Header of the block to jump to for hardcoded sync, and total difficulty.
251+
#[serde(serialize_with = "serialize_header")]
249252
pub header: encoded::Header,
250253
/// Total difficulty of the block to jump to.
254+
#[serde(rename = "totalDifficulty", serialize_with = "serialize_total_difficulty")]
251255
pub total_difficulty: U256,
252256
/// List of hardcoded CHTs, in order. If `hardcoded_sync` is set, the CHTs should include the
253257
/// header of `hardcoded_sync`.
258+
#[serde(rename = "CHTs")]
254259
pub chts: Vec<H256>,
255260
}
256261

262+
fn serialize_total_difficulty<S>(total_difficulty: &U256, serializer: S) -> Result<S::Ok, S::Error>
263+
where
264+
S: Serializer,
265+
{
266+
let total_difficulty_str = format!("{:?}", total_difficulty);
267+
serializer.serialize_str(&total_difficulty_str)
268+
}
269+
270+
fn serialize_header<S>(header: &encoded::Header, serializer: S) -> Result<S::Ok, S::Error>
271+
where
272+
S: Serializer,
273+
{
274+
let header_str = format!("{:x}", header);
275+
serializer.serialize_str(&header_str)
276+
}
277+
257278
impl From<ethjson::spec::HardcodedSync> for SpecHardcodedSync {
258279
fn from(sync: ethjson::spec::HardcodedSync) -> Self {
259280
SpecHardcodedSync {
@@ -266,12 +287,8 @@ impl From<ethjson::spec::HardcodedSync> for SpecHardcodedSync {
266287

267288
impl fmt::Display for SpecHardcodedSync {
268289
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
269-
writeln!(f, "{{")?;
270-
writeln!(f, r#""header": "{:x}","#, self.header)?;
271-
writeln!(f, r#""totalDifficulty": "{:?}""#, self.total_difficulty)?;
272-
// TODO: #11415 - fix trailing comma for CHTs
273-
writeln!(f, r#""CHTs": {:#?}"#, self.chts.iter().map(|x| format!("{:?}", x)).collect::<Vec<_>>())?;
274-
writeln!(f, "}}")
290+
let serialized = serde_json::to_string_pretty(&self).unwrap();
291+
writeln!(f, "{}", serialized)
275292
}
276293
}
277294

0 commit comments

Comments
 (0)