This commit is contained in:
Gav Wood 2016-03-02 18:32:54 +01:00
parent 42df98450c
commit 877270c35f
2 changed files with 21 additions and 14 deletions

View File

@ -491,15 +491,20 @@ impl BlockChain {
self.extras_db.write(batch).unwrap(); self.extras_db.write(batch).unwrap();
} }
/// Iterator that lists `first` and then all of `first`'s ancestors, by hash.
pub fn ancestry_iter(&self, first: H256) -> Option<AncestryIter> { pub fn ancestry_iter(&self, first: H256) -> Option<AncestryIter> {
AncestryIter { if self.is_known(&first) {
Some(AncestryIter {
current: first, current: first,
chain: &self, chain: &self,
})
} else {
None
} }
} }
/// Given a block's `parent`, find every block header which represents a valid uncle. /// Given a block's `parent`, find every block header which represents a valid uncle.
pub fn find_uncle_headers(&self, parent: &H256) -> Vec<Header> { pub fn find_uncle_headers(&self, parent: &H256) -> Option<Vec<Header>> {
let uncle_generations = 6usize; let uncle_generations = 6usize;
/* /*
{ {
@ -524,13 +529,15 @@ impl BlockChain {
} }
} }
*/ */
let _excluded = self if !self.is_known(parent) { return None; }
.ancestry_iter(parent.clone()) let mut _excluded = HashSet::new();
.take(uncle_generations) for a in self.ancestry_iter(parent.clone()).unwrap().take(uncle_generations) {
.flat_map(|h| self.uncle_hashes(&h).iter().chain(&[h])) for u in self.uncle_hashes(&a).unwrap().into_iter() {
.collect::<HashSet<_>>(); _excluded.insert(u);
}
Vec::new() _excluded.insert(a);
}
None
} }
/// Get inserted block info which is critical to preapre extras updates. /// Get inserted block info which is critical to preapre extras updates.
@ -882,7 +889,7 @@ mod tests {
block_hashes.reverse(); block_hashes.reverse();
assert_eq!(bc.ancestry_iter(block_hashes[0].clone()).collect::<Vec<_>>(), block_hashes) assert_eq!(bc.ancestry_iter(block_hashes[0].clone()).unwrap().collect::<Vec<_>>(), block_hashes)
} }
#[test] #[test]

View File

@ -200,7 +200,7 @@ pub struct Client {
extra_data: RwLock<Bytes>, extra_data: RwLock<Bytes>,
} }
const HISTORY: u64 = 1000; const HISTORY: u64 = 2000000;
const CLIENT_DB_VER_STR: &'static str = "4.0"; const CLIENT_DB_VER_STR: &'static str = "4.0";
impl Client { impl Client {
@ -457,7 +457,7 @@ impl Client {
self.extra_data() self.extra_data()
); );
self.chain.read().unwrap().find_uncle_headers(&h).into_iter().foreach(|h| { b.push_uncle(h).unwrap(); }); self.chain.read().unwrap().find_uncle_headers(&h).unwrap().into_iter().foreach(|h| { b.push_uncle(h).unwrap(); });
// TODO: push transactions. // TODO: push transactions.