Hard cutoff for non-finalized block state pruning (#798)

* state pruning after window even when nothing is finalized

* rename and alter canonicalization delay

* fix constant

* address grumbles

* add comment on canonicality vs finality
This commit is contained in:
Robert Habermeier
2018-09-25 23:24:11 +01:00
committed by Arkadiy Paronyan
parent 98e0a3a55a
commit abf799f78f
2 changed files with 73 additions and 167 deletions
-131
View File
@@ -220,134 +220,3 @@ pub fn read_meta<Block>(db: &KeyValueDB, col_header: Option<u32>) -> Result<
genesis_hash,
})
}
/// An entry in a tree route.
#[derive(Debug)]
pub struct RouteEntry<Block: BlockT> {
/// The number of the block.
pub number: <Block::Header as HeaderT>::Number,
/// The hash of the block.
pub hash: Block::Hash,
}
/// A tree-route from one block to another in the chain.
///
/// All blocks prior to the pivot in the deque is the reverse-order unique ancestry
/// of the first block, the block at the pivot index is the common ancestor,
/// and all blocks after the pivot is the ancestry of the second block, in
/// order.
///
/// The ancestry sets will include the given blocks, and thus the tree-route is
/// never empty.
///
/// ```ignore
/// Tree route from R1 to E2. Retracted is [R1, R2, R3], Common is C, enacted [E1, E2]
/// <- R3 <- R2 <- R1
/// /
/// C
/// \-> E1 -> E2
/// ```
///
/// ```ignore
/// Tree route from C to E2. Retracted empty. Common is C, enacted [E1, E2]
/// C -> E1 -> E2
/// ```
#[derive(Debug)]
pub struct TreeRoute<Block: BlockT> {
route: Vec<RouteEntry<Block>>,
pivot: usize,
}
impl<Block: BlockT> TreeRoute<Block> {
/// Get an iterator of all retracted blocks in reverse order (towards common ancestor)
pub fn retracted(&self) -> impl Iterator<Item=&RouteEntry<Block>> {
self.route.iter().take(self.pivot)
}
/// Get the common ancestor block. This might be one of the two blocks of the
/// route.
#[allow(unused)]
pub fn common_block(&self) -> &RouteEntry<Block> {
self.route.get(self.pivot).expect("tree-routes are computed between blocks; \
which are included in the route; \
thus it is never empty; qed")
}
/// Get an iterator of enacted blocks (descendents of the common ancestor)
pub fn enacted(&self) -> impl Iterator<Item=&RouteEntry<Block>> {
self.route.iter().skip(self.pivot + 1)
}
}
/// Compute a tree-route between two blocks. See tree-route docs for more details.
pub fn tree_route<Block: BlockT>(
db: &KeyValueDB,
col_header: Option<u32>,
from: Block::Hash,
to: Block::Hash,
) -> Result<TreeRoute<Block>, client::error::Error> {
use runtime_primitives::traits::Header;
let load_header = |hash: &Block::Hash| {
match db.get(col_header, hash.as_ref()).map_err(db_err) {
Ok(Some(b)) => match <Block::Header>::decode(&mut &b[..]) {
Some(hdr) => Ok(hdr),
None => Err(client::error::ErrorKind::Backend("Error decoding header".into()).into()),
}
Ok(None) => Err(client::error::ErrorKind::UnknownBlock(format!("Unknown block {:?}", hash)).into()),
Err(e) => Err(e),
}
};
let mut from = load_header(&from)?;
let mut to = load_header(&to)?;
let mut from_branch = Vec::new();
let mut to_branch = Vec::new();
while to.number() > from.number() {
to_branch.push(RouteEntry {
number: to.number().clone(),
hash: to.hash(),
});
to = load_header(to.parent_hash())?;
}
while from.number() > to.number() {
from_branch.push(RouteEntry {
number: from.number().clone(),
hash: from.hash(),
});
from = load_header(from.parent_hash())?;
}
// numbers are equal now. walk backwards until the block is the same
while to != from {
to_branch.push(RouteEntry {
number: to.number().clone(),
hash: to.hash(),
});
to = load_header(to.parent_hash())?;
from_branch.push(RouteEntry {
number: from.number().clone(),
hash: from.hash(),
});
from = load_header(from.parent_hash())?;
}
// add the pivot block. and append the reversed to-branch (note that it's reverse order originalls)
let pivot = from_branch.len();
from_branch.push(RouteEntry {
number: to.number().clone(),
hash: to.hash(),
});
from_branch.extend(to_branch.into_iter().rev());
Ok(TreeRoute {
route: from_branch,
pivot,
})
}