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();
}
/// Iterator that lists `first` and then all of `first`'s ancestors, by hash.
pub fn ancestry_iter(&self, first: H256) -> Option<AncestryIter> {
AncestryIter {
current: first,
chain: &self,
if self.is_known(&first) {
Some(AncestryIter {
current: first,
chain: &self,
})
} else {
None
}
}
/// 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;
/*
{
@ -524,13 +529,15 @@ impl BlockChain {
}
}
*/
let _excluded = self
.ancestry_iter(parent.clone())
.take(uncle_generations)
.flat_map(|h| self.uncle_hashes(&h).iter().chain(&[h]))
.collect::<HashSet<_>>();
Vec::new()
if !self.is_known(parent) { return None; }
let mut _excluded = HashSet::new();
for a in self.ancestry_iter(parent.clone()).unwrap().take(uncle_generations) {
for u in self.uncle_hashes(&a).unwrap().into_iter() {
_excluded.insert(u);
}
_excluded.insert(a);
}
None
}
/// Get inserted block info which is critical to preapre extras updates.
@ -882,7 +889,7 @@ mod tests {
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]

View File

@ -200,7 +200,7 @@ pub struct Client {
extra_data: RwLock<Bytes>,
}
const HISTORY: u64 = 1000;
const HISTORY: u64 = 2000000;
const CLIENT_DB_VER_STR: &'static str = "4.0";
impl Client {
@ -457,7 +457,7 @@ impl Client {
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.