Remove panickers from trie iterators (#2209)

* port trie iterators to use error handling

* use new trie iterators in snapshot

allows proper recovery from a premature periodic snapshot
This commit is contained in:
Robert Habermeier
2016-09-21 12:56:13 +02:00
committed by Gav Wood
parent 8c111da70b
commit a100b9d09e
9 changed files with 62 additions and 37 deletions

View File

@@ -144,7 +144,9 @@ pub struct Client {
factories: Factories,
}
const HISTORY: u64 = 1200;
/// The pruning constant -- how old blocks must be before we
/// assume finality of a given candidate.
pub const HISTORY: u64 = 1200;
/// Append a path element to the given path and return the string.
pub fn append_path<P>(path: P, item: &str) -> String where P: AsRef<Path> {

View File

@@ -92,7 +92,8 @@ impl Account {
let mut pairs = Vec::new();
for (k, v) in db.iter() {
for item in try!(db.iter()) {
let (k, v) = try!(item);
pairs.push((k, v));
}

View File

@@ -358,7 +358,8 @@ pub fn chunk_state<'a>(db: &HashDB, root: &H256, writer: &Mutex<SnapshotWriter +
let mut used_code = HashSet::new();
// account_key here is the address' hash.
for (account_key, account_data) in account_trie.iter() {
for item in try!(account_trie.iter()) {
let (account_key, account_data) = try!(item);
let account = Account::from_thin_rlp(account_data);
let account_key_hash = H256::from_slice(&account_key);

View File

@@ -27,7 +27,7 @@ use super::{ManifestData, StateRebuilder, BlockRebuilder, RestorationStatus, Sna
use super::io::{SnapshotReader, LooseReader, SnapshotWriter, LooseWriter};
use blockchain::BlockChain;
use client::Client;
use client::{BlockChainClient, Client};
use engines::Engine;
use error::Error;
use ids::BlockID;
@@ -345,7 +345,17 @@ impl Service {
let res = client.take_snapshot(writer, BlockID::Number(num), &self.progress);
self.taking_snapshot.store(false, Ordering::SeqCst);
try!(res);
if let Err(e) = res {
if client.chain_info().best_block_number >= num + ::client::HISTORY {
// "Cancelled" is mincing words a bit -- what really happened
// is that the state we were snapshotting got pruned out
// before we could finish.
info!("Cancelled prematurely-started periodic snapshot.");
return Ok(())
} else {
return Err(e);
}
}
info!("Finished taking snapshot at #{}", num);

View File

@@ -52,8 +52,9 @@ impl StateProducer {
// modify existing accounts.
let mut accounts_to_modify: Vec<_> = {
let trie = TrieDB::new(&*db, &self.state_root).unwrap();
let temp = trie.iter() // binding required due to complicated lifetime stuff
let temp = trie.iter().unwrap() // binding required due to complicated lifetime stuff
.filter(|_| rng.gen::<f32>() < ACCOUNT_CHURN)
.map(Result::unwrap)
.map(|(k, v)| (H256::from_slice(&k), v.to_owned()))
.collect();