Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Remove ChangeTrieOverlay.
  • Loading branch information
cheme committed Aug 24, 2020
commit 11305d1f7d8da6fd765bf335045c6942c4a24232
19 changes: 8 additions & 11 deletions primitives/state-machine/src/changes_trie/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use num_traits::One;
use crate::{
StorageKey,
backend::Backend,
overlayed_changes::{OverlayedChanges, OverlayedValue, ChangeTrieOverlay},
overlayed_changes::{OverlayedChanges, OverlayedValue},
trie_backend_essence::TrieBackendEssence,
changes_trie::{
AnchorBlockId, ConfigurationRange, Storage, BlockNumber,
Expand All @@ -39,11 +39,11 @@ use sp_core::storage::{ChildInfo, PrefixedStorageKey};
///
/// Returns Err if storage error has occurred OR if storage haven't returned
/// required data.
pub(crate) fn prepare_input<'a, B, H, Number, CT>(
pub(crate) fn prepare_input<'a, B, H, Number>(
backend: &'a B,
storage: &'a dyn Storage<H, Number>,
config: ConfigurationRange<'a, Number>,
overlay: &'a OverlayedChanges<CT>,
overlay: &'a OverlayedChanges,
parent: &'a AnchorBlockId<H::Out, Number>,
) -> Result<(
impl Iterator<Item=InputPair<Number>> + 'a,
Expand All @@ -55,7 +55,6 @@ pub(crate) fn prepare_input<'a, B, H, Number, CT>(
H: Hasher + 'a,
H::Out: Encode,
Number: BlockNumber,
CT: ChangeTrieOverlay,
{
let number = parent.number.clone() + One::one();
let (extrinsics_input, children_extrinsics_input) = prepare_extrinsics_input(
Expand Down Expand Up @@ -94,10 +93,10 @@ pub(crate) fn prepare_input<'a, B, H, Number, CT>(
))
}
/// Prepare ExtrinsicIndex input pairs.
fn prepare_extrinsics_input<'a, B, H, Number, CT>(
fn prepare_extrinsics_input<'a, B, H, Number>(
backend: &'a B,
block: &Number,
overlay: &'a OverlayedChanges<CT>,
overlay: &'a OverlayedChanges,
) -> Result<(
impl Iterator<Item=InputPair<Number>> + 'a,
BTreeMap<ChildIndex<Number>, impl Iterator<Item=InputPair<Number>> + 'a>,
Expand All @@ -106,7 +105,6 @@ fn prepare_extrinsics_input<'a, B, H, Number, CT>(
B: Backend<H>,
H: Hasher + 'a,
Number: BlockNumber,
CT: ChangeTrieOverlay,
{
let mut children_result = BTreeMap::new();

Expand All @@ -129,18 +127,17 @@ fn prepare_extrinsics_input<'a, B, H, Number, CT>(
Ok((top, children_result))
}

fn prepare_extrinsics_input_inner<'a, B, H, Number, CT>(
fn prepare_extrinsics_input_inner<'a, B, H, Number>(
backend: &'a B,
block: &Number,
overlay: &'a OverlayedChanges<CT>,
overlay: &'a OverlayedChanges,
child_info: Option<ChildInfo>,
mut changes: impl Iterator<Item=(&'a StorageKey, &'a OverlayedValue<CT>)>
mut changes: impl Iterator<Item=(&'a StorageKey, &'a OverlayedValue)>
) -> Result<impl Iterator<Item=InputPair<Number>> + 'a, String>
where
B: Backend<H>,
H: Hasher,
Number: BlockNumber,
CT: ChangeTrieOverlay,
{
changes
.try_fold(BTreeMap::new(), |mut map: BTreeMap<&[u8], (ExtrinsicIndex<Number>, Vec<u32>)>, (k, v)| {
Expand Down
11 changes: 4 additions & 7 deletions primitives/state-machine/src/changes_trie/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ use sp_trie::trie_types::TrieDBMut;
use crate::{
StorageKey,
backend::Backend,
overlayed_changes::{OverlayedChanges, ChangeTrieOverlay},
overlayed_changes::OverlayedChanges,
changes_trie::{
build::prepare_input,
build_cache::{IncompleteCachedBuildData, IncompleteCacheAction},
Expand Down Expand Up @@ -220,19 +220,16 @@ pub fn disabled_state<'a, H, Number>() -> Option<State<'a, H, Number>> {
/// Returns Err(()) if unknown `parent_hash` has been passed.
/// Returns Ok(None) if there's no data to perform computation.
/// Panics if background storage returns an error OR if insert to MemoryDB fails.
pub fn build_changes_trie<'a, B: Backend<H>, H: Hasher, Number: BlockNumber, CT: ChangeTrieOverlay>(
pub fn build_changes_trie<'a, B: Backend<H>, H: Hasher, Number: BlockNumber>(
backend: &B,
state: Option<&'a State<'a, H, Number>>,
changes: &OverlayedChanges<CT>,
changes: &OverlayedChanges,
parent_hash: H::Out,
panic_on_storage_error: bool,
) -> Result<Option<(MemoryDB<H>, H::Out, CacheAction<H::Out, Number>)>, ()>
where
H::Out: Ord + 'static + Encode,
{
if !CT::CHANGE_TRIE_CAPABLE {
return Ok(None);
}
/// Panics when `res.is_err() && panic`, otherwise it returns `Err(())` on an error.
fn maybe_panic<R, E: std::fmt::Debug>(
res: std::result::Result<R, E>,
Expand Down Expand Up @@ -271,7 +268,7 @@ pub fn build_changes_trie<'a, B: Backend<H>, H: Hasher, Number: BlockNumber, CT:

// storage errors are considered fatal (similar to situations when runtime fetches values from storage)
let (input_pairs, child_input_pairs, digest_input_blocks) = maybe_panic(
prepare_input::<B, H, Number, CT>(
prepare_input::<B, H, Number>(
backend,
state.storage,
config_range.clone(),
Expand Down
17 changes: 7 additions & 10 deletions primitives/state-machine/src/ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

use crate::{
StorageKey, StorageValue, OverlayedChanges,
overlayed_changes::{ChangeTrieOverlay, Extrinsics},
backend::Backend,
};
use hash_db::Hasher;
Expand Down Expand Up @@ -114,13 +113,13 @@ pub struct Ext<'a, H, N, B>
}

/// Basis implementation for `Externalities` trait.
pub struct ExtInnerMut<'a, H, B, CT: ChangeTrieOverlay = Extrinsics>
pub struct ExtInnerMut<'a, H, B>
where
H: Hasher,
B: Backend<H>,
{
/// The overlayed changes to write to.
pub overlay: &'a mut OverlayedChanges<CT>,
pub overlay: &'a mut OverlayedChanges,
/// The storage backend to read from.
pub backend: &'a B,
/// Pseudo-unique id used for tracing.
Expand All @@ -130,13 +129,13 @@ pub struct ExtInnerMut<'a, H, B, CT: ChangeTrieOverlay = Extrinsics>
}

/// Basis implementation for `Externalities` trait.
pub struct ExtInner<'a, H, B, CT: ChangeTrieOverlay = Extrinsics>
pub struct ExtInner<'a, H, B>
where
H: Hasher,
B: Backend<H>,
{
/// The overlayed changes to write to.
pub overlay: &'a OverlayedChanges<CT>,
pub overlay: &'a OverlayedChanges,
/// The storage backend to read from.
pub backend: &'a B,
/// Pseudo-unique id used for tracing.
Expand Down Expand Up @@ -217,12 +216,11 @@ where
}
}

impl<'a, H, B, CT> ExtInner<'a, H, B, CT>
impl<'a, H, B> ExtInner<'a, H, B>
where
H: Hasher,
H::Out: Ord + 'static + codec::Codec,
B: Backend<H>,
CT: ChangeTrieOverlay,
{
pub(crate) fn storage(&self, key: &[u8]) -> Option<StorageValue> {
let _guard = guard();
Expand Down Expand Up @@ -385,14 +383,13 @@ where
}
}

impl<'a, H, B, CT> ExtInnerMut<'a, H, B, CT>
impl<'a, H, B> ExtInnerMut<'a, H, B>
where
H: Hasher,
H::Out: Ord + 'static + codec::Codec,
B: Backend<H>,
CT: ChangeTrieOverlay,
{
pub(crate) fn inner(&'a self) -> ExtInner<'a, H, B, CT> {
pub(crate) fn inner(&'a self) -> ExtInner<'a, H, B> {
ExtInner {
overlay: self.overlay,
backend: self.backend,
Expand Down
44 changes: 19 additions & 25 deletions primitives/state-machine/src/overlayed_changes/changeset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

//! Houses the code that implements the transactional overlay storage.

use super::{StorageKey, StorageValue, ChangeTrieOverlay};
use super::{StorageKey, StorageValue, Extrinsics};

#[cfg(feature = "std")]
use std::collections::HashSet as Set;
Expand All @@ -34,7 +34,7 @@ const PROOF_OVERLAY_NON_EMPTY: &str = "\
as the last transaction is removed; qed";

type DirtyKeysSets = SmallVec<[Set<StorageKey>; 5]>;
type Transactions<CT> = SmallVec<[InnerValue<CT>; 5]>;
type Transactions = SmallVec<[InnerValue; 5]>;

/// Error returned when trying to commit or rollback while no transaction is open or
/// when the runtime is trying to close a transaction started by the client.
Expand Down Expand Up @@ -63,28 +63,28 @@ pub enum ExecutionMode {

#[derive(Debug, Default, Clone)]
#[cfg_attr(test, derive(PartialEq))]
struct InnerValue<CT: ChangeTrieOverlay> {
struct InnerValue {
/// Current value. None if value has been deleted.
value: Option<StorageValue>,
/// The set of extrinsic indices where the values has been changed.
/// Is filled only if runtime has announced changes trie support.
extrinsics: CT,
extrinsics: Extrinsics,
}

/// An overlay that contains all versions of a value for a specific key.
#[derive(Debug, Default, Clone)]
#[cfg_attr(test, derive(PartialEq))]
pub struct OverlayedValue<CT: ChangeTrieOverlay> {
pub struct OverlayedValue {
/// The individual versions of that value.
/// One entry per transactions during that the value was actually written.
transactions: Transactions<CT>,
transactions: Transactions,
}

/// Holds a set of changes with the ability modify them using nested transactions.
#[derive(Debug, Default, Clone)]
pub struct OverlayedChangeSet<CT: ChangeTrieOverlay> {
pub struct OverlayedChangeSet {
/// Stores the changes that this overlay constitutes.
changes: BTreeMap<StorageKey, OverlayedValue<CT>>,
changes: BTreeMap<StorageKey, OverlayedValue>,
/// Stores which keys are dirty per transaction. Needed in order to determine which
/// values to merge into the parent transaction on commit. The length of this vector
/// therefore determines how many nested transactions are currently open (depth).
Expand All @@ -103,7 +103,7 @@ impl Default for ExecutionMode {
}
}

impl<CT: ChangeTrieOverlay> OverlayedValue<CT> {
impl OverlayedValue {
/// The value as seen by the current transaction.
pub fn value(&self) -> Option<&StorageValue> {
self.transactions.last().expect(PROOF_OVERLAY_NON_EMPTY).value.as_ref()
Expand All @@ -122,12 +122,12 @@ impl<CT: ChangeTrieOverlay> OverlayedValue<CT> {
}

/// Remove the last version and return it.
fn pop_transaction(&mut self) -> InnerValue<CT> {
fn pop_transaction(&mut self) -> InnerValue {
self.transactions.pop().expect(PROOF_OVERLAY_NON_EMPTY)
}

/// Mutable reference to the set which holds the indices for the **current transaction only**.
fn transaction_extrinsics_mut(&mut self) -> &mut CT {
fn transaction_extrinsics_mut(&mut self) -> &mut Extrinsics {
&mut self.transactions.last_mut().expect(PROOF_OVERLAY_NON_EMPTY).extrinsics
}

Expand All @@ -150,10 +150,8 @@ impl<CT: ChangeTrieOverlay> OverlayedValue<CT> {
*self.value_mut() = value;
}

if CT::CHANGE_TRIE_CAPABLE {
if let Some(extrinsic) = at_extrinsic {
self.transaction_extrinsics_mut().insert(extrinsic);
}
if let Some(extrinsic) = at_extrinsic {
self.transaction_extrinsics_mut().insert(extrinsic);
}
}
}
Expand All @@ -166,7 +164,7 @@ fn insert_dirty(set: &mut DirtyKeysSets, key: StorageKey) -> bool {
set.last_mut().map(|dk| dk.insert(key)).unwrap_or_default()
}

impl<CT: ChangeTrieOverlay> OverlayedChangeSet<CT> {
impl OverlayedChangeSet {
/// Create a new changeset at the same transaction state but without any contents.
///
/// This changeset might be created when there are already open transactions.
Expand All @@ -187,7 +185,7 @@ impl<CT: ChangeTrieOverlay> OverlayedChangeSet<CT> {
}

/// Get an optional reference to the value stored for the specified key.
pub fn get(&self, key: &[u8]) -> Option<&OverlayedValue<CT>> {
pub fn get(&self, key: &[u8]) -> Option<&OverlayedValue> {
self.changes.get(key)
}

Expand Down Expand Up @@ -237,7 +235,7 @@ impl<CT: ChangeTrieOverlay> OverlayedChangeSet<CT> {
/// Can be rolled back or committed when called inside a transaction.
pub fn clear_where(
&mut self,
predicate: impl Fn(&[u8], &OverlayedValue<CT>) -> bool,
predicate: impl Fn(&[u8], &OverlayedValue) -> bool,
at_extrinsic: Option<u32>,
) {
for (key, val) in self.changes.iter_mut().filter(|(k, v)| predicate(k, v)) {
Expand All @@ -246,12 +244,12 @@ impl<CT: ChangeTrieOverlay> OverlayedChangeSet<CT> {
}

/// Get a list of all changes as seen by current transaction.
pub fn changes(&self) -> impl Iterator<Item=(&StorageKey, &OverlayedValue<CT>)> {
pub fn changes(&self) -> impl Iterator<Item=(&StorageKey, &OverlayedValue)> {
self.changes.iter()
}

/// Get the change that is next to the supplied key.
pub fn next_change(&self, key: &[u8]) -> Option<(&[u8], &OverlayedValue<CT>)> {
pub fn next_change(&self, key: &[u8]) -> Option<(&[u8], &OverlayedValue)> {
use sp_std::ops::Bound;
let range = (Bound::Excluded(key), Bound::Unbounded);
self.changes.range::<[u8], _>(range).next().map(|(k, v)| (&k[..], v))
Expand Down Expand Up @@ -374,9 +372,7 @@ impl<CT: ChangeTrieOverlay> OverlayedChangeSet<CT> {
if has_predecessor {
let dropped_tx = overlayed.pop_transaction();
*overlayed.value_mut() = dropped_tx.value;
if CT::CHANGE_TRIE_CAPABLE {
overlayed.transaction_extrinsics_mut().merge(dropped_tx.extrinsics);
}
overlayed.transaction_extrinsics_mut().merge(dropped_tx.extrinsics);
}
}
}
Expand All @@ -393,9 +389,7 @@ impl<CT: ChangeTrieOverlay> OverlayedChangeSet<CT> {
mod test {
use super::*;
use pretty_assertions::assert_eq;
use crate::overlayed_changes::Extrinsics;

type OverlayedChangeSet = super::OverlayedChangeSet<Extrinsics>;
type Changes<'a> = Vec<(&'a [u8], (Option<&'a [u8]>, Vec<u32>))>;
type Drained<'a> = Vec<(&'a [u8], Option<&'a [u8]>)>;

Expand Down
Loading