From 509fda727bec3ac0c38545492616a449efa8fed5 Mon Sep 17 00:00:00 2001 From: Marek Kotewicz Date: Mon, 12 Aug 2019 16:10:46 +0200 Subject: [PATCH] removed redundant state_root function from spec, improve spec error types (#10955) --- ethcore/src/spec/chain.rs | 4 ++-- ethcore/src/spec/spec.rs | 38 ++++++++++++++------------------------ evmbin/src/main.rs | 4 ++-- parity/params.rs | 2 +- 4 files changed, 19 insertions(+), 29 deletions(-) diff --git a/ethcore/src/spec/chain.rs b/ethcore/src/spec/chain.rs index df9b12e61..7d2ee5366 100644 --- a/ethcore/src/spec/chain.rs +++ b/ethcore/src/spec/chain.rs @@ -149,7 +149,7 @@ mod tests { let tempdir = TempDir::new("").unwrap(); let morden = new_morden(&tempdir.path()); - assert_eq!(morden.state_root(), "f3f4696bbf3b3b07775128eb7a3763279a394e382130f27c21e70233e04946a9".parse().unwrap()); + assert_eq!(morden.state_root, "f3f4696bbf3b3b07775128eb7a3763279a394e382130f27c21e70233e04946a9".parse().unwrap()); let genesis = morden.genesis_block(); assert_eq!(view!(BlockView, &genesis).header_view().hash(), "0cd786a2425d16f152c658316c423e6ce1181e15c3295826d7c9904cba9ce303".parse().unwrap()); } @@ -159,7 +159,7 @@ mod tests { let tempdir = TempDir::new("").unwrap(); let frontier = new_foundation(&tempdir.path()); - assert_eq!(frontier.state_root(), "d7f8974fb5ac78d9ac099b9ad5018bedc2ce0a72dad1827a1709da30580f0544".parse().unwrap()); + assert_eq!(frontier.state_root, "d7f8974fb5ac78d9ac099b9ad5018bedc2ce0a72dad1827a1709da30580f0544".parse().unwrap()); let genesis = frontier.genesis_block(); assert_eq!(view!(BlockView, &genesis).header_view().hash(), "d4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3".parse().unwrap()); } diff --git a/ethcore/src/spec/spec.rs b/ethcore/src/spec/spec.rs index 78f049f56..d4fe46a06 100644 --- a/ethcore/src/spec/spec.rs +++ b/ethcore/src/spec/spec.rs @@ -55,11 +55,6 @@ use trace::{NoopTracer, NoopVMTracer}; pub use ethash::OptimizeFor; -// helper for formatting errors. -fn fmt_err(f: F) -> String { - format!("Spec json is invalid: {}", f) -} - /// Runtime parameters for the spec that are related to how the software should run the chain, /// rather than integral properties of the chain itself. pub struct SpecParams<'a> { @@ -219,7 +214,7 @@ pub struct Spec { /// Contract constructors to be executed on genesis. pub constructors: Vec<(Address, Bytes)>, /// May be prepopulated if we know this in advance. - pub state_root_memo: H256, + pub state_root: H256, /// Genesis state as plain old data. pub genesis_state: PodState, } @@ -279,7 +274,7 @@ fn load_from(spec_params: SpecParams, s: ethjson::spec::Spec) -> Result Result H256 { - self.state_root_memo - } - /// Get common blockchain parameters. pub fn params(&self) -> &CommonParams { &self.engine.params() @@ -391,7 +381,7 @@ impl Spec { header.set_transactions_root(self.transactions_root.clone()); header.set_uncles_hash(keccak(RlpStream::new_list(0).out())); header.set_extra_data(self.extra_data.clone()); - header.set_state_root(self.state_root()); + header.set_state_root(self.state_root); header.set_receipts_root(self.receipts_root.clone()); header.set_log_bloom(Bloom::default()); header.set_gas_used(self.gas_used.clone()); @@ -445,13 +435,13 @@ impl Spec { BasicBackend(journaldb::new_memory_db()), )?; - self.state_root_memo = root; + self.state_root = root; Ok(()) } /// Ensure that the given state DB has the trie nodes in for the genesis state. pub fn ensure_db_good(&self, db: T, factories: &Factories) -> Result { - if db.as_hash_db().contains(&self.state_root(), hash_db::EMPTY_PREFIX) { + if db.as_hash_db().contains(&self.state_root, hash_db::EMPTY_PREFIX) { return Ok(db); } @@ -468,14 +458,14 @@ impl Spec { db )?; - assert_eq!(root, self.state_root(), "Spec's state root has not been precomputed correctly."); + assert_eq!(root, self.state_root, "Spec's state root has not been precomputed correctly."); Ok(db) } /// Loads just the state machine from a json file. - pub fn load_machine(reader: R) -> Result { + pub fn load_machine(reader: R) -> Result { ethjson::spec::Spec::load(reader) - .map_err(fmt_err) + .map_err(|e| Error::Msg(e.to_string())) .map(|s| { let builtins = s.accounts.builtins().into_iter().map(|p| (p.0.into(), From::from(p.1))).collect(); let params = CommonParams::from(s.params); @@ -485,10 +475,10 @@ impl Spec { /// Loads spec from json file. Provide factories for executing contracts and ensuring /// storage goes to the right place. - pub fn load<'a, T: Into>, R: Read>(params: T, reader: R) -> Result { + pub fn load<'a, T: Into>, R: Read>(params: T, reader: R) -> Result { ethjson::spec::Spec::load(reader) - .map_err(fmt_err) - .and_then(|x| load_from(params.into(), x).map_err(fmt_err)) + .map_err(|e| Error::Msg(e.to_string())) + .and_then(|x| load_from(params.into(), x)) } /// initialize genesis epoch data, using in-memory database for @@ -570,7 +560,7 @@ mod tests { let test_spec = spec::new_test(); assert_eq!( - test_spec.state_root(), + test_spec.state_root, H256::from_str("f3f4696bbf3b3b07775128eb7a3763279a394e382130f27c21e70233e04946a9").unwrap() ); let genesis = test_spec.genesis_block(); @@ -588,7 +578,7 @@ mod tests { .unwrap(); let state = State::from_existing( db.boxed_clone(), - spec.state_root(), + spec.state_root, spec.engine.account_start_nonce(0), Default::default(), ).unwrap(); diff --git a/evmbin/src/main.rs b/evmbin/src/main.rs index 159683ec2..7491d3c18 100644 --- a/evmbin/src/main.rs +++ b/evmbin/src/main.rs @@ -439,8 +439,8 @@ impl Args { pub fn spec(&self) -> Result { Ok(match self.flag_chain { Some(ref filename) => { - let file = fs::File::open(filename).map_err(|e| format!("{}", e))?; - spec::Spec::load(&::std::env::temp_dir(), file)? + let file = fs::File::open(filename).map_err(|e| e.to_string())?; + spec::Spec::load(&::std::env::temp_dir(), file).map_err(|e| e.to_string())? }, None => { spec::new_foundation(&::std::env::temp_dir()) diff --git a/parity/params.rs b/parity/params.rs index dbbb6f8be..f7fd9a37c 100644 --- a/parity/params.rs +++ b/parity/params.rs @@ -137,7 +137,7 @@ impl SpecType { SpecType::Dev => Ok(spec::new_instant()), SpecType::Custom(ref filename) => { let file = fs::File::open(filename).map_err(|e| format!("Could not load specification file at {}: {}", filename, e))?; - Spec::load(params, file) + Spec::load(params, file).map_err(|e| e.to_string()) } } }