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:
parent
e047bb4bb5
commit
b683c22c98
2
Cargo.lock
generated
2
Cargo.lock
generated
@ -4820,6 +4820,8 @@ dependencies = [
|
|||||||
"parity-bytes",
|
"parity-bytes",
|
||||||
"pod",
|
"pod",
|
||||||
"rlp",
|
"rlp",
|
||||||
|
"serde",
|
||||||
|
"serde_json",
|
||||||
"tempfile",
|
"tempfile",
|
||||||
"trace",
|
"trace",
|
||||||
"trie-vm-factories",
|
"trie-vm-factories",
|
||||||
|
@ -32,6 +32,8 @@ maplit = "1"
|
|||||||
null-engine = { path = "../engines/null-engine" }
|
null-engine = { path = "../engines/null-engine" }
|
||||||
pod = { path = "../pod" }
|
pod = { path = "../pod" }
|
||||||
rlp = "0.4.2"
|
rlp = "0.4.2"
|
||||||
|
serde = "1.0"
|
||||||
|
serde_json = "1.0"
|
||||||
trace = { path = "../trace" }
|
trace = { path = "../trace" }
|
||||||
trie-vm-factories = { path = "../trie-vm-factories" }
|
trie-vm-factories = { path = "../trie-vm-factories" }
|
||||||
vm = { path = "../vm" }
|
vm = { path = "../vm" }
|
||||||
|
@ -51,6 +51,7 @@ use maplit::btreeset;
|
|||||||
use null_engine::NullEngine;
|
use null_engine::NullEngine;
|
||||||
use pod::PodState;
|
use pod::PodState;
|
||||||
use rlp::{Rlp, RlpStream};
|
use rlp::{Rlp, RlpStream};
|
||||||
|
use serde::{Serialize, Serializer};
|
||||||
use trace::{NoopTracer, NoopVMTracer};
|
use trace::{NoopTracer, NoopVMTracer};
|
||||||
use trie_vm_factories::Factories;
|
use trie_vm_factories::Factories;
|
||||||
use vm::{EnvInfo, ActionType, ActionValue, ActionParams, ParamsType};
|
use vm::{EnvInfo, ActionType, ActionValue, ActionParams, ParamsType};
|
||||||
@ -244,16 +245,36 @@ pub struct Spec {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Part of `Spec`. Describes the hardcoded synchronization parameters.
|
/// Part of `Spec`. Describes the hardcoded synchronization parameters.
|
||||||
|
#[derive(Serialize)]
|
||||||
pub struct SpecHardcodedSync {
|
pub struct SpecHardcodedSync {
|
||||||
/// Header of the block to jump to for hardcoded sync, and total difficulty.
|
/// Header of the block to jump to for hardcoded sync, and total difficulty.
|
||||||
|
#[serde(serialize_with = "serialize_header")]
|
||||||
pub header: encoded::Header,
|
pub header: encoded::Header,
|
||||||
/// Total difficulty of the block to jump to.
|
/// Total difficulty of the block to jump to.
|
||||||
|
#[serde(rename = "totalDifficulty", serialize_with = "serialize_total_difficulty")]
|
||||||
pub total_difficulty: U256,
|
pub total_difficulty: U256,
|
||||||
/// List of hardcoded CHTs, in order. If `hardcoded_sync` is set, the CHTs should include the
|
/// List of hardcoded CHTs, in order. If `hardcoded_sync` is set, the CHTs should include the
|
||||||
/// header of `hardcoded_sync`.
|
/// header of `hardcoded_sync`.
|
||||||
|
#[serde(rename = "CHTs")]
|
||||||
pub chts: Vec<H256>,
|
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 {
|
impl From<ethjson::spec::HardcodedSync> for SpecHardcodedSync {
|
||||||
fn from(sync: ethjson::spec::HardcodedSync) -> Self {
|
fn from(sync: ethjson::spec::HardcodedSync) -> Self {
|
||||||
SpecHardcodedSync {
|
SpecHardcodedSync {
|
||||||
@ -266,12 +287,8 @@ impl From<ethjson::spec::HardcodedSync> for SpecHardcodedSync {
|
|||||||
|
|
||||||
impl fmt::Display for SpecHardcodedSync {
|
impl fmt::Display for SpecHardcodedSync {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
writeln!(f, "{{")?;
|
let serialized = serde_json::to_string_pretty(&self).unwrap();
|
||||||
writeln!(f, r#""header": "{:x}","#, self.header)?;
|
writeln!(f, "{}", serialized)
|
||||||
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, "}}")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user