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

Commit

Permalink
Use serde_json to export hardcoded sync
Browse files Browse the repository at this point in the history
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
  • Loading branch information
marktoda committed Apr 5, 2020
1 parent e047bb4 commit 9448819
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 8 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions ethcore/spec/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
33 changes: 25 additions & 8 deletions ethcore/spec/src/spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -244,34 +245,50 @@ 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<H256>,
}

fn serialize_total_difficulty<S>(total_difficulty: &U256, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let total_difficulty_str = format!("{:?}", total_difficulty);
serializer.serialize_str(&total_difficulty_str)
}

fn serialize_header<S>(header: &encoded::Header, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let header_str = format!("{:x}", header);
serializer.serialize_str(&header_str)
}

impl From<ethjson::spec::HardcodedSync> for SpecHardcodedSync {
fn from(sync: ethjson::spec::HardcodedSync) -> Self {
SpecHardcodedSync {
header: encoded::Header::new(sync.header.into()),
total_difficulty: sync.total_difficulty.into(),
chts: sync.chts.into_iter().map(Into::into).collect(),
}
}
}
}
}

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::<Vec<_>>())?;
writeln!(f, "}}")
let serialized = serde_json::to_string_pretty(&self).unwrap();
writeln!(f, "{}", serialized)
}
}

Expand Down

0 comments on commit 9448819

Please sign in to comment.