mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-12 13:31:10 +00:00
Introduce notion of finality to substrate (#760)
* finalization for in_mem * fetch last finalized block * pruning: use canonical term instead of final * finalize blocks in full node * begin to port light client DB * add tree-route * keep number index consistent in full nodes * fix tests * disable cache and finish porting light client * add AsMut to system module * final leaf is always best * fix all tests * Fix comment and trace * removed unused Into call * add comment on behavior of `finalize_block`
This commit is contained in:
committed by
Gav Wood
parent
28cc4d0fd6
commit
b7d095a2e0
@@ -15,19 +15,19 @@
|
||||
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
// tag::description[]
|
||||
//! State database maintenance. Handles finalization and pruning in the database. The input to
|
||||
//! State database maintenance. Handles canonicalization and pruning in the database. The input to
|
||||
//! this module is a `ChangeSet` which is basically a list of key-value pairs (trie nodes) that
|
||||
//! were added or deleted during block execution.
|
||||
//!
|
||||
//! # Finalization.
|
||||
//! Finalization window tracks a tree of blocks identified by header hash. The in-memory
|
||||
//! # Canonicalization.
|
||||
//! Canonicalization window tracks a tree of blocks identified by header hash. The in-memory
|
||||
//! overlay allows to get any node that was was inserted in any any of the blocks within the window.
|
||||
//! The tree is journaled to the backing database and rebuilt on startup.
|
||||
//! Finalization function select one root from the top of the tree and discards all other roots and
|
||||
//! Canonicalization function select one root from the top of the tree and discards all other roots and
|
||||
//! their subtrees.
|
||||
//!
|
||||
//! # Pruning.
|
||||
//! See `RefWindow` for pruning algorithm details. `StateDb` prunes on each finalization until pruning
|
||||
//! See `RefWindow` for pruning algorithm details. `StateDb` prunes on each canonicalization until pruning
|
||||
//! constraints are satisfied.
|
||||
//!
|
||||
// end::description[]
|
||||
@@ -38,7 +38,7 @@ extern crate parking_lot;
|
||||
extern crate parity_codec as codec;
|
||||
extern crate substrate_primitives as primitives;
|
||||
|
||||
mod unfinalized;
|
||||
mod noncanonical;
|
||||
mod pruning;
|
||||
#[cfg(test)] mod test;
|
||||
|
||||
@@ -46,7 +46,7 @@ use std::fmt;
|
||||
use parking_lot::RwLock;
|
||||
use codec::Codec;
|
||||
use std::collections::HashSet;
|
||||
use unfinalized::UnfinalizedOverlay;
|
||||
use noncanonical::NonCanonicalOverlay;
|
||||
use pruning::RefWindow;
|
||||
|
||||
/// Database value type.
|
||||
@@ -113,7 +113,7 @@ pub struct CommitSet<H: Hash> {
|
||||
/// Pruning constraints. If none are specified pruning is
|
||||
#[derive(Default, Debug, Clone)]
|
||||
pub struct Constraints {
|
||||
/// Maximum blocks. Defaults to 0 when unspecified, effectively keeping only unfinalized states.
|
||||
/// Maximum blocks. Defaults to 0 when unspecified, effectively keeping only non-canonical states.
|
||||
pub max_blocks: Option<u32>,
|
||||
/// Maximum memory in the pruning overlay.
|
||||
pub max_mem: Option<usize>,
|
||||
@@ -124,9 +124,9 @@ pub struct Constraints {
|
||||
pub enum PruningMode {
|
||||
/// Maintain a pruning window.
|
||||
Constrained(Constraints),
|
||||
/// No pruning. Finalization is a no-op.
|
||||
/// No pruning. Canonicalization is a no-op.
|
||||
ArchiveAll,
|
||||
/// Finalization discards unfinalized nodes. All the finalized nodes are kept in the DB.
|
||||
/// Canonicalization discards non-canonical nodes. All the canonical nodes are kept in the DB.
|
||||
ArchiveCanonical,
|
||||
}
|
||||
|
||||
@@ -154,7 +154,7 @@ fn to_meta_key<S: Codec>(suffix: &[u8], data: &S) -> Vec<u8> {
|
||||
|
||||
struct StateDbSync<BlockHash: Hash, Key: Hash> {
|
||||
mode: PruningMode,
|
||||
unfinalized: UnfinalizedOverlay<BlockHash, Key>,
|
||||
non_canonical: NonCanonicalOverlay<BlockHash, Key>,
|
||||
pruning: Option<RefWindow<BlockHash, Key>>,
|
||||
pinned: HashSet<BlockHash>,
|
||||
}
|
||||
@@ -162,7 +162,7 @@ struct StateDbSync<BlockHash: Hash, Key: Hash> {
|
||||
impl<BlockHash: Hash, Key: Hash> StateDbSync<BlockHash, Key> {
|
||||
pub fn new<D: MetaDb>(mode: PruningMode, db: &D) -> Result<StateDbSync<BlockHash, Key>, Error<D::Error>> {
|
||||
trace!("StateDb settings: {:?}", mode);
|
||||
let unfinalized: UnfinalizedOverlay<BlockHash, Key> = UnfinalizedOverlay::new(db)?;
|
||||
let non_canonical: NonCanonicalOverlay<BlockHash, Key> = NonCanonicalOverlay::new(db)?;
|
||||
let pruning: Option<RefWindow<BlockHash, Key>> = match mode {
|
||||
PruningMode::Constrained(Constraints {
|
||||
max_mem: Some(_),
|
||||
@@ -173,7 +173,7 @@ impl<BlockHash: Hash, Key: Hash> StateDbSync<BlockHash, Key> {
|
||||
};
|
||||
Ok(StateDbSync {
|
||||
mode,
|
||||
unfinalized,
|
||||
non_canonical,
|
||||
pruning: pruning,
|
||||
pinned: Default::default(),
|
||||
})
|
||||
@@ -196,36 +196,36 @@ impl<BlockHash: Hash, Key: Hash> StateDbSync<BlockHash, Key> {
|
||||
}
|
||||
},
|
||||
PruningMode::Constrained(_) | PruningMode::ArchiveCanonical => {
|
||||
self.unfinalized.insert(hash, number, parent_hash, changeset)
|
||||
self.non_canonical.insert(hash, number, parent_hash, changeset)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn finalize_block(&mut self, hash: &BlockHash) -> CommitSet<Key> {
|
||||
// clear the temporary overlay from the previous finalization.
|
||||
self.unfinalized.clear_overlay();
|
||||
pub fn canonicalize_block(&mut self, hash: &BlockHash) -> CommitSet<Key> {
|
||||
// clear the temporary overlay from the previous canonicalization.
|
||||
self.non_canonical.clear_overlay();
|
||||
let mut commit = match self.mode {
|
||||
PruningMode::ArchiveAll => {
|
||||
CommitSet::default()
|
||||
},
|
||||
PruningMode::ArchiveCanonical => {
|
||||
let mut commit = self.unfinalized.finalize(hash);
|
||||
let mut commit = self.non_canonical.canonicalize(hash);
|
||||
commit.data.deleted.clear();
|
||||
commit
|
||||
},
|
||||
PruningMode::Constrained(_) => {
|
||||
self.unfinalized.finalize(hash)
|
||||
self.non_canonical.canonicalize(hash)
|
||||
},
|
||||
};
|
||||
if let Some(ref mut pruning) = self.pruning {
|
||||
pruning.note_finalized(hash, &mut commit);
|
||||
pruning.note_canonical(hash, &mut commit);
|
||||
}
|
||||
self.prune(&mut commit);
|
||||
commit
|
||||
}
|
||||
|
||||
pub fn best_finalized(&self) -> u64 {
|
||||
return self.unfinalized.last_finalized_block_number()
|
||||
pub fn best_canonical(&self) -> u64 {
|
||||
return self.non_canonical.last_canonicalized_block_number()
|
||||
}
|
||||
|
||||
pub fn is_pruned(&self, number: u64) -> bool {
|
||||
@@ -252,7 +252,7 @@ impl<BlockHash: Hash, Key: Hash> StateDbSync<BlockHash, Key> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Revert all unfinalized blocks with the best block number.
|
||||
/// Revert all non-canonical blocks with the best block number.
|
||||
/// Returns a database commit or `None` if not possible.
|
||||
/// For archive an empty commit set is returned.
|
||||
pub fn revert_one(&mut self) -> Option<CommitSet<Key>> {
|
||||
@@ -261,7 +261,7 @@ impl<BlockHash: Hash, Key: Hash> StateDbSync<BlockHash, Key> {
|
||||
Some(CommitSet::default())
|
||||
},
|
||||
PruningMode::ArchiveCanonical | PruningMode::Constrained(_) => {
|
||||
self.unfinalized.revert_one()
|
||||
self.non_canonical.revert_one()
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -275,7 +275,7 @@ impl<BlockHash: Hash, Key: Hash> StateDbSync<BlockHash, Key> {
|
||||
}
|
||||
|
||||
pub fn get<D: HashDb<Hash=Key>>(&self, key: &Key, db: &D) -> Result<Option<DBValue>, Error<D::Error>> {
|
||||
if let Some(value) = self.unfinalized.get(key) {
|
||||
if let Some(value) = self.non_canonical.get(key) {
|
||||
return Ok(Some(value));
|
||||
}
|
||||
db.get(key).map_err(|e| Error::Db(e))
|
||||
@@ -296,14 +296,14 @@ impl<BlockHash: Hash, Key: Hash> StateDb<BlockHash, Key> {
|
||||
})
|
||||
}
|
||||
|
||||
/// Add a new unfinalized block.
|
||||
/// Add a new non-canonical block.
|
||||
pub fn insert_block(&self, hash: &BlockHash, number: u64, parent_hash: &BlockHash, changeset: ChangeSet<Key>) -> CommitSet<Key> {
|
||||
self.db.write().insert_block(hash, number, parent_hash, changeset)
|
||||
}
|
||||
|
||||
/// Finalize a previously inserted block.
|
||||
pub fn finalize_block(&self, hash: &BlockHash) -> CommitSet<Key> {
|
||||
self.db.write().finalize_block(hash)
|
||||
pub fn canonicalize_block(&self, hash: &BlockHash) -> CommitSet<Key> {
|
||||
self.db.write().canonicalize_block(hash)
|
||||
}
|
||||
|
||||
/// Prevents pruning of specified block and its descendants.
|
||||
@@ -316,12 +316,12 @@ impl<BlockHash: Hash, Key: Hash> StateDb<BlockHash, Key> {
|
||||
self.db.write().unpin(hash)
|
||||
}
|
||||
|
||||
/// Get a value from unfinalized/pruning overlay or the backing DB.
|
||||
/// Get a value from non-canonical/pruning overlay or the backing DB.
|
||||
pub fn get<D: HashDb<Hash=Key>>(&self, key: &Key, db: &D) -> Result<Option<DBValue>, Error<D::Error>> {
|
||||
self.db.read().get(key, db)
|
||||
}
|
||||
|
||||
/// Revert all unfinalized blocks with the best block number.
|
||||
/// Revert all non-canonical blocks with the best block number.
|
||||
/// Returns a database commit or `None` if not possible.
|
||||
/// For archive an empty commit set is returned.
|
||||
pub fn revert_one(&self) -> Option<CommitSet<Key>> {
|
||||
@@ -329,8 +329,8 @@ impl<BlockHash: Hash, Key: Hash> StateDb<BlockHash, Key> {
|
||||
}
|
||||
|
||||
/// Returns last finalized block number.
|
||||
pub fn best_finalized(&self) -> u64 {
|
||||
return self.db.read().best_finalized()
|
||||
pub fn best_canonical(&self) -> u64 {
|
||||
return self.db.read().best_canonical()
|
||||
}
|
||||
|
||||
/// Check if block is pruned away.
|
||||
@@ -353,10 +353,10 @@ mod tests {
|
||||
db.commit(&state_db.insert_block(&H256::from(21), 2, &H256::from(1), make_changeset(&[21], &[921, 1])));
|
||||
db.commit(&state_db.insert_block(&H256::from(22), 2, &H256::from(1), make_changeset(&[22], &[922])));
|
||||
db.commit(&state_db.insert_block(&H256::from(3), 3, &H256::from(21), make_changeset(&[3], &[93])));
|
||||
db.commit(&state_db.finalize_block(&H256::from(1)));
|
||||
db.commit(&state_db.canonicalize_block(&H256::from(1)));
|
||||
db.commit(&state_db.insert_block(&H256::from(4), 4, &H256::from(3), make_changeset(&[4], &[94])));
|
||||
db.commit(&state_db.finalize_block(&H256::from(21)));
|
||||
db.commit(&state_db.finalize_block(&H256::from(3)));
|
||||
db.commit(&state_db.canonicalize_block(&H256::from(21)));
|
||||
db.commit(&state_db.canonicalize_block(&H256::from(3)));
|
||||
|
||||
(db, state_db)
|
||||
}
|
||||
|
||||
+84
-84
@@ -14,25 +14,25 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! Finalization window.
|
||||
//! Canonicalization window.
|
||||
//! Maintains trees of block overlays and allows discarding trees/roots
|
||||
//! The overlays are added in `insert` and removed in `finalize`.
|
||||
//! Last finalized overlay is kept in memory until next call to `finalize` or
|
||||
//! The overlays are added in `insert` and removed in `canonicalize`.
|
||||
//! Last canonicalized overlay is kept in memory until next call to `canonicalize` or
|
||||
//! `clear_overlay`
|
||||
|
||||
use std::collections::{HashMap, VecDeque};
|
||||
use super::{Error, DBValue, ChangeSet, CommitSet, MetaDb, Hash, to_meta_key};
|
||||
use codec::{Decode, Encode};
|
||||
|
||||
const UNFINALIZED_JOURNAL: &[u8] = b"unfinalized_journal";
|
||||
const LAST_FINALIZED: &[u8] = b"last_finalized";
|
||||
const NON_CANONICAL_JOURNAL: &[u8] = b"noncanonical_journal";
|
||||
const LAST_CANONICAL: &[u8] = b"last_canonical";
|
||||
|
||||
/// See module documentation.
|
||||
pub struct UnfinalizedOverlay<BlockHash: Hash, Key: Hash> {
|
||||
last_finalized: Option<(BlockHash, u64)>,
|
||||
pub struct NonCanonicalOverlay<BlockHash: Hash, Key: Hash> {
|
||||
last_canonicalized: Option<(BlockHash, u64)>,
|
||||
levels: VecDeque<Vec<BlockOverlay<BlockHash, Key>>>,
|
||||
parents: HashMap<BlockHash, BlockHash>,
|
||||
last_finalized_overlay: HashMap<Key, DBValue>,
|
||||
last_canonicalized_overlay: HashMap<Key, DBValue>,
|
||||
}
|
||||
|
||||
#[derive(Encode, Decode)]
|
||||
@@ -44,7 +44,7 @@ struct JournalRecord<BlockHash: Hash, Key: Hash> {
|
||||
}
|
||||
|
||||
fn to_journal_key(block: u64, index: u64) -> Vec<u8> {
|
||||
to_meta_key(UNFINALIZED_JOURNAL, &(block, index))
|
||||
to_meta_key(NON_CANONICAL_JOURNAL, &(block, index))
|
||||
}
|
||||
|
||||
#[cfg_attr(test, derive(PartialEq, Debug))]
|
||||
@@ -55,20 +55,20 @@ struct BlockOverlay<BlockHash: Hash, Key: Hash> {
|
||||
deleted: Vec<Key>,
|
||||
}
|
||||
|
||||
impl<BlockHash: Hash, Key: Hash> UnfinalizedOverlay<BlockHash, Key> {
|
||||
impl<BlockHash: Hash, Key: Hash> NonCanonicalOverlay<BlockHash, Key> {
|
||||
/// Creates a new instance. Does not expect any metadata to be present in the DB.
|
||||
pub fn new<D: MetaDb>(db: &D) -> Result<UnfinalizedOverlay<BlockHash, Key>, Error<D::Error>> {
|
||||
let last_finalized = db.get_meta(&to_meta_key(LAST_FINALIZED, &()))
|
||||
pub fn new<D: MetaDb>(db: &D) -> Result<NonCanonicalOverlay<BlockHash, Key>, Error<D::Error>> {
|
||||
let last_canonicalized = db.get_meta(&to_meta_key(LAST_CANONICAL, &()))
|
||||
.map_err(|e| Error::Db(e))?;
|
||||
let last_finalized = match last_finalized {
|
||||
let last_canonicalized = match last_canonicalized {
|
||||
Some(buffer) => Some(<(BlockHash, u64)>::decode(&mut buffer.as_slice()).ok_or(Error::Decoding)?),
|
||||
None => None,
|
||||
};
|
||||
let mut levels = VecDeque::new();
|
||||
let mut parents = HashMap::new();
|
||||
if let Some((ref hash, mut block)) = last_finalized {
|
||||
if let Some((ref hash, mut block)) = last_canonicalized {
|
||||
// read the journal
|
||||
trace!(target: "state-db", "Reading unfinalized journal. Last finalized #{} ({:?})", block, hash);
|
||||
trace!(target: "state-db", "Reading uncanonicalized journal. Last canonicalized #{} ({:?})", block, hash);
|
||||
let mut total: u64 = 0;
|
||||
block += 1;
|
||||
loop {
|
||||
@@ -85,7 +85,7 @@ impl<BlockHash: Hash, Key: Hash> UnfinalizedOverlay<BlockHash, Key> {
|
||||
values: record.inserted.into_iter().collect(),
|
||||
deleted: record.deleted,
|
||||
};
|
||||
trace!(target: "state-db", "Unfinalized journal entry {}.{} ({} inserted, {} deleted)", block, index, overlay.values.len(), overlay.deleted.len());
|
||||
trace!(target: "state-db", "Uncanonicalized journal entry {}.{} ({} inserted, {} deleted)", block, index, overlay.values.len(), overlay.deleted.len());
|
||||
level.push(overlay);
|
||||
parents.insert(record.hash, record.parent_hash);
|
||||
index += 1;
|
||||
@@ -100,29 +100,29 @@ impl<BlockHash: Hash, Key: Hash> UnfinalizedOverlay<BlockHash, Key> {
|
||||
levels.push_back(level);
|
||||
block += 1;
|
||||
}
|
||||
trace!(target: "state-db", "Finished reading unfinalized journal, {} entries", total);
|
||||
trace!(target: "state-db", "Finished reading uncanonicalized journal, {} entries", total);
|
||||
}
|
||||
Ok(UnfinalizedOverlay {
|
||||
last_finalized: last_finalized,
|
||||
Ok(NonCanonicalOverlay {
|
||||
last_canonicalized: last_canonicalized,
|
||||
levels,
|
||||
parents,
|
||||
last_finalized_overlay: Default::default(),
|
||||
last_canonicalized_overlay: Default::default(),
|
||||
})
|
||||
}
|
||||
|
||||
/// Insert a new block into the overlay. If inserted on the second level or lover expects parent to be present in the window.
|
||||
pub fn insert(&mut self, hash: &BlockHash, number: u64, parent_hash: &BlockHash, changeset: ChangeSet<Key>) -> CommitSet<Key> {
|
||||
let mut commit = CommitSet::default();
|
||||
if self.levels.is_empty() && self.last_finalized.is_none() {
|
||||
// assume that parent was finalized
|
||||
let last_finalized = (parent_hash.clone(), number - 1);
|
||||
commit.meta.inserted.push((to_meta_key(LAST_FINALIZED, &()), last_finalized.encode()));
|
||||
self.last_finalized = Some(last_finalized);
|
||||
} else if self.last_finalized.is_some() {
|
||||
if self.levels.is_empty() && self.last_canonicalized.is_none() {
|
||||
// assume that parent was canonicalized
|
||||
let last_canonicalized = (parent_hash.clone(), number - 1);
|
||||
commit.meta.inserted.push((to_meta_key(LAST_CANONICAL, &()), last_canonicalized.encode()));
|
||||
self.last_canonicalized = Some(last_canonicalized);
|
||||
} else if self.last_canonicalized.is_some() {
|
||||
assert!(number >= self.front_block_number() && number < (self.front_block_number() + self.levels.len() as u64 + 1));
|
||||
// check for valid parent if inserting on second level or higher
|
||||
if number == self.front_block_number() {
|
||||
assert!(self.last_finalized.as_ref().map_or(false, |&(ref h, n)| h == parent_hash && n == number - 1));
|
||||
assert!(self.last_canonicalized.as_ref().map_or(false, |&(ref h, n)| h == parent_hash && n == number - 1));
|
||||
} else {
|
||||
assert!(self.parents.contains_key(&parent_hash));
|
||||
}
|
||||
@@ -153,7 +153,7 @@ impl<BlockHash: Hash, Key: Hash> UnfinalizedOverlay<BlockHash, Key> {
|
||||
inserted: changeset.inserted,
|
||||
deleted: changeset.deleted,
|
||||
};
|
||||
trace!(target: "state-db", "Inserted unfinalized changeset {}.{} ({} inserted, {} deleted)", number, index, journal_record.inserted.len(), journal_record.deleted.len());
|
||||
trace!(target: "state-db", "Inserted uncanonicalized changeset {}.{} ({} inserted, {} deleted)", number, index, journal_record.inserted.len(), journal_record.deleted.len());
|
||||
let journal_record = journal_record.encode();
|
||||
commit.meta.inserted.push((journal_key, journal_record));
|
||||
commit
|
||||
@@ -182,34 +182,34 @@ impl<BlockHash: Hash, Key: Hash> UnfinalizedOverlay<BlockHash, Key> {
|
||||
}
|
||||
|
||||
fn front_block_number(&self) -> u64 {
|
||||
self.last_finalized.as_ref().map(|&(_, n)| n + 1).unwrap_or(0)
|
||||
self.last_canonicalized.as_ref().map(|&(_, n)| n + 1).unwrap_or(0)
|
||||
}
|
||||
|
||||
pub fn last_finalized_block_number(&self) -> u64 {
|
||||
self.last_finalized.as_ref().map(|&(_, n)| n).unwrap_or(0)
|
||||
pub fn last_canonicalized_block_number(&self) -> u64 {
|
||||
self.last_canonicalized.as_ref().map(|&(_, n)| n).unwrap_or(0)
|
||||
}
|
||||
|
||||
/// This may be called when the last finalization commit was applied to the database.
|
||||
pub fn clear_overlay(&mut self) {
|
||||
self.last_finalized_overlay.clear();
|
||||
self.last_canonicalized_overlay.clear();
|
||||
}
|
||||
|
||||
/// Select a top-level root and finalized it. Discards all sibling subtrees and the root.
|
||||
/// Select a top-level root and canonicalized it. Discards all sibling subtrees and the root.
|
||||
/// Returns a set of changes that need to be added to the DB.
|
||||
pub fn finalize(&mut self, hash: &BlockHash) -> CommitSet<Key> {
|
||||
trace!(target: "state-db", "Finalizing {:?}", hash);
|
||||
let level = self.levels.pop_front().expect("no blocks to finalize");
|
||||
pub fn canonicalize(&mut self, hash: &BlockHash) -> CommitSet<Key> {
|
||||
trace!(target: "state-db", "Canonicalizing {:?}", hash);
|
||||
let level = self.levels.pop_front().expect("no blocks to canonicalize");
|
||||
let index = level.iter().position(|overlay| overlay.hash == *hash)
|
||||
.expect("attempting to finalize unknown block");
|
||||
.expect("attempting to canonicalize unknown block");
|
||||
|
||||
let mut commit = CommitSet::default();
|
||||
let mut discarded_journals = Vec::new();
|
||||
for (i, overlay) in level.into_iter().enumerate() {
|
||||
self.parents.remove(&overlay.hash);
|
||||
if i == index {
|
||||
self.last_finalized_overlay = overlay.values;
|
||||
// that's the one we need to finalize
|
||||
commit.data.inserted = self.last_finalized_overlay.iter().map(|(k, v)| (k.clone(), v.clone())).collect();
|
||||
self.last_canonicalized_overlay = overlay.values;
|
||||
// that's the one we need to canonicalize
|
||||
commit.data.inserted = self.last_canonicalized_overlay.iter().map(|(k, v)| (k.clone(), v.clone())).collect();
|
||||
commit.data.deleted = overlay.deleted;
|
||||
} else {
|
||||
// TODO: borrow checker won't allow us to split out mutable refernces
|
||||
@@ -223,16 +223,16 @@ impl<BlockHash: Hash, Key: Hash> UnfinalizedOverlay<BlockHash, Key> {
|
||||
discarded_journals.push(overlay.journal_key);
|
||||
}
|
||||
commit.meta.deleted.append(&mut discarded_journals);
|
||||
let last_finalized = (hash.clone(), self.front_block_number());
|
||||
commit.meta.inserted.push((to_meta_key(LAST_FINALIZED, &()), last_finalized.encode()));
|
||||
self.last_finalized = Some(last_finalized);
|
||||
let last_canonicalized = (hash.clone(), self.front_block_number());
|
||||
commit.meta.inserted.push((to_meta_key(LAST_CANONICAL, &()), last_canonicalized.encode()));
|
||||
self.last_canonicalized = Some(last_canonicalized);
|
||||
trace!(target: "state-db", "Discarded {} records", commit.meta.deleted.len());
|
||||
commit
|
||||
}
|
||||
|
||||
/// Get a value from the node overlay. This searches in every existing changeset.
|
||||
pub fn get(&self, key: &Key) -> Option<DBValue> {
|
||||
if let Some(value) = self.last_finalized_overlay.get(&key) {
|
||||
if let Some(value) = self.last_canonicalized_overlay.get(&key) {
|
||||
return Some(value.clone());
|
||||
}
|
||||
for level in self.levels.iter() {
|
||||
@@ -260,30 +260,30 @@ impl<BlockHash: Hash, Key: Hash> UnfinalizedOverlay<BlockHash, Key> {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::UnfinalizedOverlay;
|
||||
use super::NonCanonicalOverlay;
|
||||
use {ChangeSet};
|
||||
use primitives::H256;
|
||||
use test::{make_db, make_changeset};
|
||||
|
||||
fn contains(overlay: &UnfinalizedOverlay<H256, H256>, key: u64) -> bool {
|
||||
fn contains(overlay: &NonCanonicalOverlay<H256, H256>, key: u64) -> bool {
|
||||
overlay.get(&H256::from(key)) == Some(H256::from(key).to_vec())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn created_from_empty_db() {
|
||||
let db = make_db(&[]);
|
||||
let overlay: UnfinalizedOverlay<H256, H256> = UnfinalizedOverlay::new(&db).unwrap();
|
||||
assert_eq!(overlay.last_finalized, None);
|
||||
let overlay: NonCanonicalOverlay<H256, H256> = NonCanonicalOverlay::new(&db).unwrap();
|
||||
assert_eq!(overlay.last_canonicalized, None);
|
||||
assert!(overlay.levels.is_empty());
|
||||
assert!(overlay.parents.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic]
|
||||
fn finalize_empty_panics() {
|
||||
fn canonicalize_empty_panics() {
|
||||
let db = make_db(&[]);
|
||||
let mut overlay = UnfinalizedOverlay::<H256, H256>::new(&db).unwrap();
|
||||
overlay.finalize(&H256::default());
|
||||
let mut overlay = NonCanonicalOverlay::<H256, H256>::new(&db).unwrap();
|
||||
overlay.canonicalize(&H256::default());
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -292,7 +292,7 @@ mod tests {
|
||||
let db = make_db(&[]);
|
||||
let h1 = H256::random();
|
||||
let h2 = H256::random();
|
||||
let mut overlay = UnfinalizedOverlay::<H256, H256>::new(&db).unwrap();
|
||||
let mut overlay = NonCanonicalOverlay::<H256, H256>::new(&db).unwrap();
|
||||
overlay.insert(&h1, 2, &H256::default(), ChangeSet::default());
|
||||
overlay.insert(&h2, 1, &h1, ChangeSet::default());
|
||||
}
|
||||
@@ -303,7 +303,7 @@ mod tests {
|
||||
let h1 = H256::random();
|
||||
let h2 = H256::random();
|
||||
let db = make_db(&[]);
|
||||
let mut overlay = UnfinalizedOverlay::<H256, H256>::new(&db).unwrap();
|
||||
let mut overlay = NonCanonicalOverlay::<H256, H256>::new(&db).unwrap();
|
||||
overlay.insert(&h1, 1, &H256::default(), ChangeSet::default());
|
||||
overlay.insert(&h2, 3, &h1, ChangeSet::default());
|
||||
}
|
||||
@@ -314,27 +314,27 @@ mod tests {
|
||||
let db = make_db(&[]);
|
||||
let h1 = H256::random();
|
||||
let h2 = H256::random();
|
||||
let mut overlay = UnfinalizedOverlay::<H256, H256>::new(&db).unwrap();
|
||||
let mut overlay = NonCanonicalOverlay::<H256, H256>::new(&db).unwrap();
|
||||
overlay.insert(&h1, 1, &H256::default(), ChangeSet::default());
|
||||
overlay.insert(&h2, 2, &H256::default(), ChangeSet::default());
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic]
|
||||
fn finalize_unknown_panics() {
|
||||
fn canonicalize_unknown_panics() {
|
||||
let h1 = H256::random();
|
||||
let h2 = H256::random();
|
||||
let db = make_db(&[]);
|
||||
let mut overlay = UnfinalizedOverlay::<H256, H256>::new(&db).unwrap();
|
||||
let mut overlay = NonCanonicalOverlay::<H256, H256>::new(&db).unwrap();
|
||||
overlay.insert(&h1, 1, &H256::default(), ChangeSet::default());
|
||||
overlay.finalize(&h2);
|
||||
overlay.canonicalize(&h2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn insert_finalize_one() {
|
||||
fn insert_canonicalize_one() {
|
||||
let h1 = H256::random();
|
||||
let mut db = make_db(&[1, 2]);
|
||||
let mut overlay = UnfinalizedOverlay::<H256, H256>::new(&db).unwrap();
|
||||
let mut overlay = NonCanonicalOverlay::<H256, H256>::new(&db).unwrap();
|
||||
let changeset = make_changeset(&[3, 4], &[2]);
|
||||
let insertion = overlay.insert(&h1, 1, &H256::default(), changeset.clone());
|
||||
assert_eq!(insertion.data.inserted.len(), 0);
|
||||
@@ -342,7 +342,7 @@ mod tests {
|
||||
assert_eq!(insertion.meta.inserted.len(), 2);
|
||||
assert_eq!(insertion.meta.deleted.len(), 0);
|
||||
db.commit(&insertion);
|
||||
let finalization = overlay.finalize(&h1);
|
||||
let finalization = overlay.canonicalize(&h1);
|
||||
assert_eq!(finalization.data.inserted.len(), changeset.inserted.len());
|
||||
assert_eq!(finalization.data.deleted.len(), changeset.deleted.len());
|
||||
assert_eq!(finalization.meta.inserted.len(), 1);
|
||||
@@ -356,40 +356,40 @@ mod tests {
|
||||
let h1 = H256::random();
|
||||
let h2 = H256::random();
|
||||
let mut db = make_db(&[1, 2]);
|
||||
let mut overlay = UnfinalizedOverlay::<H256, H256>::new(&db).unwrap();
|
||||
let mut overlay = NonCanonicalOverlay::<H256, H256>::new(&db).unwrap();
|
||||
db.commit(&overlay.insert(&h1, 10, &H256::default(), make_changeset(&[3, 4], &[2])));
|
||||
db.commit(&overlay.insert(&h2, 11, &h1, make_changeset(&[5], &[3])));
|
||||
assert_eq!(db.meta.len(), 3);
|
||||
|
||||
let overlay2 = UnfinalizedOverlay::<H256, H256>::new(&db).unwrap();
|
||||
let overlay2 = NonCanonicalOverlay::<H256, H256>::new(&db).unwrap();
|
||||
assert_eq!(overlay.levels, overlay2.levels);
|
||||
assert_eq!(overlay.parents, overlay2.parents);
|
||||
assert_eq!(overlay.last_finalized, overlay2.last_finalized);
|
||||
assert_eq!(overlay.last_canonicalized, overlay2.last_canonicalized);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn restore_from_journal_after_finalize() {
|
||||
fn restore_from_journal_after_canonicalize() {
|
||||
let h1 = H256::random();
|
||||
let h2 = H256::random();
|
||||
let mut db = make_db(&[1, 2]);
|
||||
let mut overlay = UnfinalizedOverlay::<H256, H256>::new(&db).unwrap();
|
||||
let mut overlay = NonCanonicalOverlay::<H256, H256>::new(&db).unwrap();
|
||||
db.commit(&overlay.insert(&h1, 10, &H256::default(), make_changeset(&[3, 4], &[2])));
|
||||
db.commit(&overlay.insert(&h2, 11, &h1, make_changeset(&[5], &[3])));
|
||||
db.commit(&overlay.finalize(&h1));
|
||||
db.commit(&overlay.canonicalize(&h1));
|
||||
assert_eq!(overlay.levels.len(), 1);
|
||||
|
||||
let overlay2 = UnfinalizedOverlay::<H256, H256>::new(&db).unwrap();
|
||||
let overlay2 = NonCanonicalOverlay::<H256, H256>::new(&db).unwrap();
|
||||
assert_eq!(overlay.levels, overlay2.levels);
|
||||
assert_eq!(overlay.parents, overlay2.parents);
|
||||
assert_eq!(overlay.last_finalized, overlay2.last_finalized);
|
||||
assert_eq!(overlay.last_canonicalized, overlay2.last_canonicalized);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn insert_finalize_two() {
|
||||
fn insert_canonicalize_two() {
|
||||
let h1 = H256::random();
|
||||
let h2 = H256::random();
|
||||
let mut db = make_db(&[1, 2, 3, 4]);
|
||||
let mut overlay = UnfinalizedOverlay::<H256, H256>::new(&db).unwrap();
|
||||
let mut overlay = NonCanonicalOverlay::<H256, H256>::new(&db).unwrap();
|
||||
let changeset1 = make_changeset(&[5, 6], &[2]);
|
||||
let changeset2 = make_changeset(&[7, 8], &[5, 3]);
|
||||
db.commit(&overlay.insert(&h1, 1, &H256::default(), changeset1));
|
||||
@@ -399,14 +399,14 @@ mod tests {
|
||||
assert!(contains(&overlay, 5));
|
||||
assert_eq!(overlay.levels.len(), 2);
|
||||
assert_eq!(overlay.parents.len(), 2);
|
||||
db.commit(&overlay.finalize(&h1));
|
||||
db.commit(&overlay.canonicalize(&h1));
|
||||
assert_eq!(overlay.levels.len(), 1);
|
||||
assert_eq!(overlay.parents.len(), 1);
|
||||
assert!(contains(&overlay, 5));
|
||||
overlay.clear_overlay();
|
||||
assert!(!contains(&overlay, 5));
|
||||
assert!(contains(&overlay, 7));
|
||||
db.commit(&overlay.finalize(&h2));
|
||||
db.commit(&overlay.canonicalize(&h2));
|
||||
overlay.clear_overlay();
|
||||
assert_eq!(overlay.levels.len(), 0);
|
||||
assert_eq!(overlay.parents.len(), 0);
|
||||
@@ -442,7 +442,7 @@ mod tests {
|
||||
let (h_1_2_3, c_1_2_3) = (H256::random(), make_changeset(&[123], &[]));
|
||||
let (h_2_1_1, c_2_1_1) = (H256::random(), make_changeset(&[211], &[]));
|
||||
|
||||
let mut overlay = UnfinalizedOverlay::<H256, H256>::new(&db).unwrap();
|
||||
let mut overlay = NonCanonicalOverlay::<H256, H256>::new(&db).unwrap();
|
||||
db.commit(&overlay.insert(&h_1, 1, &H256::default(), c_1));
|
||||
|
||||
db.commit(&overlay.insert(&h_1_1, 2, &h_1, c_1_1));
|
||||
@@ -467,16 +467,16 @@ mod tests {
|
||||
assert!(contains(&overlay, 211));
|
||||
assert_eq!(overlay.levels.len(), 3);
|
||||
assert_eq!(overlay.parents.len(), 11);
|
||||
assert_eq!(overlay.last_finalized, Some((H256::default(), 0)));
|
||||
assert_eq!(overlay.last_canonicalized, Some((H256::default(), 0)));
|
||||
|
||||
// check if restoration from journal results in the same tree
|
||||
let overlay2 = UnfinalizedOverlay::<H256, H256>::new(&db).unwrap();
|
||||
let overlay2 = NonCanonicalOverlay::<H256, H256>::new(&db).unwrap();
|
||||
assert_eq!(overlay.levels, overlay2.levels);
|
||||
assert_eq!(overlay.parents, overlay2.parents);
|
||||
assert_eq!(overlay.last_finalized, overlay2.last_finalized);
|
||||
assert_eq!(overlay.last_canonicalized, overlay2.last_canonicalized);
|
||||
|
||||
// finalize 1. 2 and all its children should be discarded
|
||||
db.commit(&overlay.finalize(&h_1));
|
||||
// canonicalize 1. 2 and all its children should be discarded
|
||||
db.commit(&overlay.canonicalize(&h_1));
|
||||
overlay.clear_overlay();
|
||||
assert_eq!(overlay.levels.len(), 2);
|
||||
assert_eq!(overlay.parents.len(), 6);
|
||||
@@ -487,8 +487,8 @@ mod tests {
|
||||
assert!(!contains(&overlay, 211));
|
||||
assert!(contains(&overlay, 111));
|
||||
|
||||
// finalize 1_2. 1_1 and all its children should be discarded
|
||||
db.commit(&overlay.finalize(&h_1_2));
|
||||
// canonicalize 1_2. 1_1 and all its children should be discarded
|
||||
db.commit(&overlay.canonicalize(&h_1_2));
|
||||
overlay.clear_overlay();
|
||||
assert_eq!(overlay.levels.len(), 1);
|
||||
assert_eq!(overlay.parents.len(), 3);
|
||||
@@ -498,13 +498,13 @@ mod tests {
|
||||
assert!(contains(&overlay, 122));
|
||||
assert!(contains(&overlay, 123));
|
||||
|
||||
// finalize 1_2_2
|
||||
db.commit(&overlay.finalize(&h_1_2_2));
|
||||
// canonicalize 1_2_2
|
||||
db.commit(&overlay.canonicalize(&h_1_2_2));
|
||||
overlay.clear_overlay();
|
||||
assert_eq!(overlay.levels.len(), 0);
|
||||
assert_eq!(overlay.parents.len(), 0);
|
||||
assert!(db.data_eq(&make_db(&[1, 12, 122])));
|
||||
assert_eq!(overlay.last_finalized, Some((h_1_2_2, 3)));
|
||||
assert_eq!(overlay.last_canonicalized, Some((h_1_2_2, 3)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -512,7 +512,7 @@ mod tests {
|
||||
let h1 = H256::random();
|
||||
let h2 = H256::random();
|
||||
let mut db = make_db(&[1, 2, 3, 4]);
|
||||
let mut overlay = UnfinalizedOverlay::<H256, H256>::new(&db).unwrap();
|
||||
let mut overlay = NonCanonicalOverlay::<H256, H256>::new(&db).unwrap();
|
||||
assert!(overlay.revert_one().is_none());
|
||||
let changeset1 = make_changeset(&[5, 6], &[2]);
|
||||
let changeset2 = make_changeset(&[7, 8], &[5, 3]);
|
||||
@@ -137,7 +137,7 @@ impl<BlockHash: Hash, Key: Hash> RefWindow<BlockHash, Key> {
|
||||
}
|
||||
|
||||
/// Add a change set to the window. Creates a journal record and pushes it to `commit`
|
||||
pub fn note_finalized(&mut self, hash: &BlockHash, commit: &mut CommitSet<Key>) {
|
||||
pub fn note_canonical(&mut self, hash: &BlockHash, commit: &mut CommitSet<Key>) {
|
||||
trace!(target: "state-db", "Adding to pruning window: {:?} ({} inserted, {} deleted)", hash, commit.data.inserted.len(), commit.data.deleted.len());
|
||||
let inserted = commit.data.inserted.iter().map(|(k, _)| k.clone()).collect();
|
||||
let deleted = ::std::mem::replace(&mut commit.data.deleted, Vec::new());
|
||||
@@ -192,7 +192,7 @@ mod tests {
|
||||
let mut pruning: RefWindow<H256, H256> = RefWindow::new(&db).unwrap();
|
||||
let mut commit = make_commit(&[4, 5], &[1, 3]);
|
||||
let h = H256::random();
|
||||
pruning.note_finalized(&h, &mut commit);
|
||||
pruning.note_canonical(&h, &mut commit);
|
||||
db.commit(&commit);
|
||||
assert!(commit.data.deleted.is_empty());
|
||||
assert_eq!(pruning.death_rows.len(), 1);
|
||||
@@ -214,10 +214,10 @@ mod tests {
|
||||
let mut db = make_db(&[1, 2, 3]);
|
||||
let mut pruning: RefWindow<H256, H256> = RefWindow::new(&db).unwrap();
|
||||
let mut commit = make_commit(&[4], &[1]);
|
||||
pruning.note_finalized(&H256::random(), &mut commit);
|
||||
pruning.note_canonical(&H256::random(), &mut commit);
|
||||
db.commit(&commit);
|
||||
let mut commit = make_commit(&[5], &[2]);
|
||||
pruning.note_finalized(&H256::random(), &mut commit);
|
||||
pruning.note_canonical(&H256::random(), &mut commit);
|
||||
db.commit(&commit);
|
||||
assert!(db.data_eq(&make_db(&[1, 2, 3, 4, 5])));
|
||||
|
||||
@@ -239,13 +239,13 @@ mod tests {
|
||||
let mut db = make_db(&[1, 2, 3]);
|
||||
let mut pruning: RefWindow<H256, H256> = RefWindow::new(&db).unwrap();
|
||||
let mut commit = make_commit(&[], &[2]);
|
||||
pruning.note_finalized(&H256::random(), &mut commit);
|
||||
pruning.note_canonical(&H256::random(), &mut commit);
|
||||
db.commit(&commit);
|
||||
let mut commit = make_commit(&[2], &[]);
|
||||
pruning.note_finalized(&H256::random(), &mut commit);
|
||||
pruning.note_canonical(&H256::random(), &mut commit);
|
||||
db.commit(&commit);
|
||||
let mut commit = make_commit(&[], &[2]);
|
||||
pruning.note_finalized(&H256::random(), &mut commit);
|
||||
pruning.note_canonical(&H256::random(), &mut commit);
|
||||
db.commit(&commit);
|
||||
assert!(db.data_eq(&make_db(&[1, 2, 3])));
|
||||
|
||||
|
||||
Reference in New Issue
Block a user