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

Commit 509fda7

Browse files
debrisdvdplm
authored andcommitted
removed redundant state_root function from spec, improve spec error types (#10955)
1 parent ffc066e commit 509fda7

File tree

4 files changed

+19
-29
lines changed

4 files changed

+19
-29
lines changed

ethcore/src/spec/chain.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ mod tests {
149149
let tempdir = TempDir::new("").unwrap();
150150
let morden = new_morden(&tempdir.path());
151151

152-
assert_eq!(morden.state_root(), "f3f4696bbf3b3b07775128eb7a3763279a394e382130f27c21e70233e04946a9".parse().unwrap());
152+
assert_eq!(morden.state_root, "f3f4696bbf3b3b07775128eb7a3763279a394e382130f27c21e70233e04946a9".parse().unwrap());
153153
let genesis = morden.genesis_block();
154154
assert_eq!(view!(BlockView, &genesis).header_view().hash(), "0cd786a2425d16f152c658316c423e6ce1181e15c3295826d7c9904cba9ce303".parse().unwrap());
155155
}
@@ -159,7 +159,7 @@ mod tests {
159159
let tempdir = TempDir::new("").unwrap();
160160
let frontier = new_foundation(&tempdir.path());
161161

162-
assert_eq!(frontier.state_root(), "d7f8974fb5ac78d9ac099b9ad5018bedc2ce0a72dad1827a1709da30580f0544".parse().unwrap());
162+
assert_eq!(frontier.state_root, "d7f8974fb5ac78d9ac099b9ad5018bedc2ce0a72dad1827a1709da30580f0544".parse().unwrap());
163163
let genesis = frontier.genesis_block();
164164
assert_eq!(view!(BlockView, &genesis).header_view().hash(), "d4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3".parse().unwrap());
165165
}

ethcore/src/spec/spec.rs

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,6 @@ use trace::{NoopTracer, NoopVMTracer};
5555

5656
pub use ethash::OptimizeFor;
5757

58-
// helper for formatting errors.
59-
fn fmt_err<F: ::std::fmt::Display>(f: F) -> String {
60-
format!("Spec json is invalid: {}", f)
61-
}
62-
6358
/// Runtime parameters for the spec that are related to how the software should run the chain,
6459
/// rather than integral properties of the chain itself.
6560
pub struct SpecParams<'a> {
@@ -219,7 +214,7 @@ pub struct Spec {
219214
/// Contract constructors to be executed on genesis.
220215
pub constructors: Vec<(Address, Bytes)>,
221216
/// May be prepopulated if we know this in advance.
222-
pub state_root_memo: H256,
217+
pub state_root: H256,
223218
/// Genesis state as plain old data.
224219
pub genesis_state: PodState,
225220
}
@@ -279,7 +274,7 @@ fn load_from(spec_params: SpecParams, s: ethjson::spec::Spec) -> Result<Spec, Er
279274
.collect();
280275
let genesis_state: PodState = s.accounts.into();
281276

282-
let (state_root_memo, _) = run_constructors(
277+
let (state_root, _) = run_constructors(
283278
&genesis_state,
284279
&constructors,
285280
&*engine,
@@ -308,7 +303,7 @@ fn load_from(spec_params: SpecParams, s: ethjson::spec::Spec) -> Result<Spec, Er
308303
hardcoded_sync,
309304
constructors,
310305
genesis_state,
311-
state_root_memo,
306+
state_root,
312307
};
313308

314309
Ok(s)
@@ -351,11 +346,6 @@ impl Spec {
351346
}
352347
}
353348

354-
/// Return the state root for the genesis state, memoising accordingly.
355-
pub fn state_root(&self) -> H256 {
356-
self.state_root_memo
357-
}
358-
359349
/// Get common blockchain parameters.
360350
pub fn params(&self) -> &CommonParams {
361351
&self.engine.params()
@@ -391,7 +381,7 @@ impl Spec {
391381
header.set_transactions_root(self.transactions_root.clone());
392382
header.set_uncles_hash(keccak(RlpStream::new_list(0).out()));
393383
header.set_extra_data(self.extra_data.clone());
394-
header.set_state_root(self.state_root());
384+
header.set_state_root(self.state_root);
395385
header.set_receipts_root(self.receipts_root.clone());
396386
header.set_log_bloom(Bloom::default());
397387
header.set_gas_used(self.gas_used.clone());
@@ -445,13 +435,13 @@ impl Spec {
445435
BasicBackend(journaldb::new_memory_db()),
446436
)?;
447437

448-
self.state_root_memo = root;
438+
self.state_root = root;
449439
Ok(())
450440
}
451441

452442
/// Ensure that the given state DB has the trie nodes in for the genesis state.
453443
pub fn ensure_db_good<T: Backend>(&self, db: T, factories: &Factories) -> Result<T, Error> {
454-
if db.as_hash_db().contains(&self.state_root(), hash_db::EMPTY_PREFIX) {
444+
if db.as_hash_db().contains(&self.state_root, hash_db::EMPTY_PREFIX) {
455445
return Ok(db);
456446
}
457447

@@ -468,14 +458,14 @@ impl Spec {
468458
db
469459
)?;
470460

471-
assert_eq!(root, self.state_root(), "Spec's state root has not been precomputed correctly.");
461+
assert_eq!(root, self.state_root, "Spec's state root has not been precomputed correctly.");
472462
Ok(db)
473463
}
474464

475465
/// Loads just the state machine from a json file.
476-
pub fn load_machine<R: Read>(reader: R) -> Result<Machine, String> {
466+
pub fn load_machine<R: Read>(reader: R) -> Result<Machine, Error> {
477467
ethjson::spec::Spec::load(reader)
478-
.map_err(fmt_err)
468+
.map_err(|e| Error::Msg(e.to_string()))
479469
.map(|s| {
480470
let builtins = s.accounts.builtins().into_iter().map(|p| (p.0.into(), From::from(p.1))).collect();
481471
let params = CommonParams::from(s.params);
@@ -485,10 +475,10 @@ impl Spec {
485475

486476
/// Loads spec from json file. Provide factories for executing contracts and ensuring
487477
/// storage goes to the right place.
488-
pub fn load<'a, T: Into<SpecParams<'a>>, R: Read>(params: T, reader: R) -> Result<Self, String> {
478+
pub fn load<'a, T: Into<SpecParams<'a>>, R: Read>(params: T, reader: R) -> Result<Self, Error> {
489479
ethjson::spec::Spec::load(reader)
490-
.map_err(fmt_err)
491-
.and_then(|x| load_from(params.into(), x).map_err(fmt_err))
480+
.map_err(|e| Error::Msg(e.to_string()))
481+
.and_then(|x| load_from(params.into(), x))
492482
}
493483

494484
/// initialize genesis epoch data, using in-memory database for
@@ -570,7 +560,7 @@ mod tests {
570560
let test_spec = spec::new_test();
571561

572562
assert_eq!(
573-
test_spec.state_root(),
563+
test_spec.state_root,
574564
H256::from_str("f3f4696bbf3b3b07775128eb7a3763279a394e382130f27c21e70233e04946a9").unwrap()
575565
);
576566
let genesis = test_spec.genesis_block();
@@ -588,7 +578,7 @@ mod tests {
588578
.unwrap();
589579
let state = State::from_existing(
590580
db.boxed_clone(),
591-
spec.state_root(),
581+
spec.state_root,
592582
spec.engine.account_start_nonce(0),
593583
Default::default(),
594584
).unwrap();

evmbin/src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -439,8 +439,8 @@ impl Args {
439439
pub fn spec(&self) -> Result<spec::Spec, String> {
440440
Ok(match self.flag_chain {
441441
Some(ref filename) => {
442-
let file = fs::File::open(filename).map_err(|e| format!("{}", e))?;
443-
spec::Spec::load(&::std::env::temp_dir(), file)?
442+
let file = fs::File::open(filename).map_err(|e| e.to_string())?;
443+
spec::Spec::load(&::std::env::temp_dir(), file).map_err(|e| e.to_string())?
444444
},
445445
None => {
446446
spec::new_foundation(&::std::env::temp_dir())

parity/params.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ impl SpecType {
137137
SpecType::Dev => Ok(spec::new_instant()),
138138
SpecType::Custom(ref filename) => {
139139
let file = fs::File::open(filename).map_err(|e| format!("Could not load specification file at {}: {}", filename, e))?;
140-
Spec::load(params, file)
140+
Spec::load(params, file).map_err(|e| e.to_string())
141141
}
142142
}
143143
}

0 commit comments

Comments
 (0)