Use serde_json to export hardcoded sync (#11601)

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
This commit is contained in:
marktoda 2020-04-06 01:30:12 -07:00 committed by GitHub
parent e047bb4bb5
commit b683c22c98
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 6 deletions

2
Cargo.lock generated
View File

@ -4820,6 +4820,8 @@ dependencies = [
"parity-bytes",
"pod",
"rlp",
"serde",
"serde_json",
"tempfile",
"trace",
"trie-vm-factories",

View File

@ -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" }

View File

@ -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<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 {
@ -266,12 +287,8 @@ impl From<ethjson::spec::HardcodedSync> 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::<Vec<_>>())?;
writeln!(f, "}}")
let serialized = serde_json::to_string_pretty(&self).unwrap();
writeln!(f, "{}", serialized)
}
}