removed redundant state_root function from spec, improve spec error types (#10955)

This commit is contained in:
Marek Kotewicz 2019-08-12 16:10:46 +02:00 committed by David
parent ffc066e5a4
commit 509fda727b
4 changed files with 19 additions and 29 deletions

View File

@ -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());
}

View File

@ -55,11 +55,6 @@ use trace::{NoopTracer, NoopVMTracer};
pub use ethash::OptimizeFor;
// helper for formatting errors.
fn fmt_err<F: ::std::fmt::Display>(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<Spec, Er
.collect();
let genesis_state: PodState = s.accounts.into();
let (state_root_memo, _) = run_constructors(
let (state_root, _) = run_constructors(
&genesis_state,
&constructors,
&*engine,
@ -308,7 +303,7 @@ fn load_from(spec_params: SpecParams, s: ethjson::spec::Spec) -> Result<Spec, Er
hardcoded_sync,
constructors,
genesis_state,
state_root_memo,
state_root,
};
Ok(s)
@ -351,11 +346,6 @@ impl Spec {
}
}
/// Return the state root for the genesis state, memoising accordingly.
pub fn state_root(&self) -> 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<T: Backend>(&self, db: T, factories: &Factories) -> Result<T, Error> {
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<R: Read>(reader: R) -> Result<Machine, String> {
pub fn load_machine<R: Read>(reader: R) -> Result<Machine, Error> {
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<SpecParams<'a>>, R: Read>(params: T, reader: R) -> Result<Self, String> {
pub fn load<'a, T: Into<SpecParams<'a>>, R: Read>(params: T, reader: R) -> Result<Self, Error> {
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();

View File

@ -439,8 +439,8 @@ impl Args {
pub fn spec(&self) -> Result<spec::Spec, String> {
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())

View File

@ -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())
}
}
}