Skip to content

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 openethereum#11415
  • Loading branch information
marktoda committed Apr 4, 2020
1 parent d4b5720 commit ef478ce
Show file tree
Hide file tree
Showing 3 changed files with 28 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
32 changes: 24 additions & 8 deletions ethcore/spec/src/spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ use maplit::btreeset;
use null_engine::NullEngine;
use pod::PodState;
use rlp::{Rlp, RlpStream};
use serde::ser::{Serialize, Serializer, SerializeStruct};
use serde_json;
use trace::{NoopTracer, NoopVMTracer};
use trie_vm_factories::Factories;
use vm::{EnvInfo, ActionType, ActionValue, ActionParams, ParamsType};
Expand Down Expand Up @@ -264,15 +266,29 @@ impl From<ethjson::spec::HardcodedSync> for SpecHardcodedSync {
}
}

impl Serialize for SpecHardcodedSync {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let mut struc = serializer.serialize_struct("SpecHardcodedSync", 3)?;

let header_str = format!("{:x}", self.header);
struc.serialize_field("header", &header_str)?;

let total_difficulty_str = format!("{:?}", self.total_difficulty);
struc.serialize_field("totalDifficulty", &total_difficulty_str)?;

struc.serialize_field("CHTs", &self.chts)?;
struc.end()
}
}

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, "}}")
}
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let serialized = serde_json::to_string_pretty(&self).unwrap();
writeln!(f, "{}", serialized)
}
}

fn convert_json_to_spec(
Expand Down

0 comments on commit ef478ce

Please sign in to comment.