Run cargo fmt on the whole code base (#9394)

* Run cargo fmt on the whole code base

* Second run

* Add CI check

* Fix compilation

* More unnecessary braces

* Handle weights

* Use --all

* Use correct attributes...

* Fix UI tests

* AHHHHHHHHH

* 🤦

* Docs

* Fix compilation

* 🤷

* Please stop

* 🤦 x 2

* More

* make rustfmt.toml consistent with polkadot

Co-authored-by: André Silva <andrerfosilva@gmail.com>
This commit is contained in:
Bastian Köcher
2021-07-21 16:32:32 +02:00
committed by GitHub
parent d451c38c1c
commit 7b56ab15b4
1010 changed files with 53339 additions and 51208 deletions
@@ -17,17 +17,19 @@
//! Houses the code that implements the transactional overlay storage.
use super::{StorageKey, StorageValue, Extrinsics};
use super::{Extrinsics, StorageKey, StorageValue};
#[cfg(feature = "std")]
use std::collections::HashSet as Set;
#[cfg(not(feature = "std"))]
use sp_std::collections::btree_set::BTreeSet as Set;
#[cfg(feature = "std")]
use std::collections::HashSet as Set;
use sp_std::collections::{btree_map::BTreeMap, btree_set::BTreeSet};
use sp_std::hash::Hash;
use smallvec::SmallVec;
use crate::warn;
use smallvec::SmallVec;
use sp_std::{
collections::{btree_map::BTreeMap, btree_set::BTreeSet},
hash::Hash,
};
const PROOF_OVERLAY_NON_EMPTY: &str = "\
An OverlayValue is always created with at least one transaction and dropped as soon
@@ -82,9 +84,7 @@ pub struct OverlayedEntry<V> {
impl<V> Default for OverlayedEntry<V> {
fn default() -> Self {
Self {
transactions: SmallVec::new(),
}
Self { transactions: SmallVec::new() }
}
}
@@ -142,7 +142,9 @@ impl<V> OverlayedEntry<V> {
/// Unique list of extrinsic indices which modified the value.
pub fn extrinsics(&self) -> BTreeSet<u32> {
let mut set = BTreeSet::new();
self.transactions.iter().for_each(|t| t.extrinsics.copy_extrinsics_into(&mut set));
self.transactions
.iter()
.for_each(|t| t.extrinsics.copy_extrinsics_into(&mut set));
set
}
@@ -165,17 +167,9 @@ impl<V> OverlayedEntry<V> {
///
/// This makes sure that the old version is not overwritten and can be properly
/// rolled back when required.
fn set(
&mut self,
value: V,
first_write_in_tx: bool,
at_extrinsic: Option<u32>,
) {
fn set(&mut self, value: V, first_write_in_tx: bool, at_extrinsic: Option<u32>) {
if first_write_in_tx || self.transactions.is_empty() {
self.transactions.push(InnerValue {
value,
extrinsics: Default::default(),
});
self.transactions.push(InnerValue { value, extrinsics: Default::default() });
} else {
*self.value_mut() = value;
}
@@ -223,9 +217,9 @@ impl<K: Ord + Hash + Clone, V> OverlayedMap<K, V> {
/// Get an optional reference to the value stored for the specified key.
pub fn get<Q>(&self, key: &Q) -> Option<&OverlayedEntry<V>>
where
K: sp_std::borrow::Borrow<Q>,
Q: Ord + ?Sized,
where
K: sp_std::borrow::Borrow<Q>,
Q: Ord + ?Sized,
{
self.changes.get(key)
}
@@ -233,24 +227,19 @@ impl<K: Ord + Hash + Clone, V> OverlayedMap<K, V> {
/// Set a new value for the specified key.
///
/// Can be rolled back or committed when called inside a transaction.
pub fn set(
&mut self,
key: K,
value: V,
at_extrinsic: Option<u32>,
) {
pub fn set(&mut self, key: K, value: V, at_extrinsic: Option<u32>) {
let overlayed = self.changes.entry(key.clone()).or_default();
overlayed.set(value, insert_dirty(&mut self.dirty_keys, key), at_extrinsic);
}
/// Get a list of all changes as seen by current transaction.
pub fn changes(&self) -> impl Iterator<Item=(&K, &OverlayedEntry<V>)> {
pub fn changes(&self) -> impl Iterator<Item = (&K, &OverlayedEntry<V>)> {
self.changes.iter()
}
/// Get a list of all changes as seen by current transaction, consumes
/// the overlay.
pub fn into_changes(self) -> impl Iterator<Item=(K, OverlayedEntry<V>)> {
pub fn into_changes(self) -> impl Iterator<Item = (K, OverlayedEntry<V>)> {
self.changes.into_iter()
}
@@ -258,7 +247,7 @@ impl<K: Ord + Hash + Clone, V> OverlayedMap<K, V> {
///
/// Panics:
/// Panics if there are open transactions: `transaction_depth() > 0`
pub fn drain_commited(self) -> impl Iterator<Item=(K, V)> {
pub fn drain_commited(self) -> impl Iterator<Item = (K, V)> {
assert!(self.transaction_depth() == 0, "Drain is not allowed with open transactions.");
self.changes.into_iter().map(|(k, mut v)| (k, v.pop_transaction().value))
}
@@ -276,7 +265,7 @@ impl<K: Ord + Hash + Clone, V> OverlayedMap<K, V> {
/// Calling this while already inside the runtime will return an error.
pub fn enter_runtime(&mut self) -> Result<(), AlreadyInRuntime> {
if let ExecutionMode::Runtime = self.execution_mode {
return Err(AlreadyInRuntime);
return Err(AlreadyInRuntime)
}
self.execution_mode = ExecutionMode::Runtime;
self.num_client_transactions = self.transaction_depth();
@@ -289,7 +278,7 @@ impl<K: Ord + Hash + Clone, V> OverlayedMap<K, V> {
/// Calling this while already outside the runtime will return an error.
pub fn exit_runtime(&mut self) -> Result<(), NotInRuntime> {
if let ExecutionMode::Client = self.execution_mode {
return Err(NotInRuntime);
return Err(NotInRuntime)
}
self.execution_mode = ExecutionMode::Client;
if self.has_open_runtime_transactions() {
@@ -341,11 +330,13 @@ impl<K: Ord + Hash + Clone, V> OverlayedMap<K, V> {
}
for key in self.dirty_keys.pop().ok_or(NoOpenTransaction)? {
let overlayed = self.changes.get_mut(&key).expect("\
let overlayed = self.changes.get_mut(&key).expect(
"\
A write to an OverlayedValue is recorded in the dirty key set. Before an
OverlayedValue is removed, its containing dirty set is removed. This
function is only called for keys that are in the dirty set. qed\
");
",
);
if rollback {
overlayed.pop_transaction();
@@ -443,9 +434,12 @@ mod test {
type Drained<'a> = Vec<(&'a [u8], Option<&'a [u8]>)>;
fn assert_changes(is: &OverlayedChangeSet, expected: &Changes) {
let is: Changes = is.changes().map(|(k, v)| {
(k.as_ref(), (v.value().map(AsRef::as_ref), v.extrinsics().into_iter().collect()))
}).collect();
let is: Changes = is
.changes()
.map(|(k, v)| {
(k.as_ref(), (v.value().map(AsRef::as_ref), v.extrinsics().into_iter().collect()))
})
.collect();
assert_eq!(&is, expected);
}
@@ -453,7 +447,8 @@ mod test {
let is = is.drain_commited().collect::<Vec<_>>();
let expected = expected
.iter()
.map(|(k, v)| (k.to_vec(), v.0.map(From::from))).collect::<Vec<_>>();
.map(|(k, v)| (k.to_vec(), v.0.map(From::from)))
.collect::<Vec<_>>();
assert_eq!(is, expected);
}
@@ -461,7 +456,8 @@ mod test {
let is = is.drain_commited().collect::<Vec<_>>();
let expected = expected
.iter()
.map(|(k, v)| (k.to_vec(), v.map(From::from))).collect::<Vec<_>>();
.map(|(k, v)| (k.to_vec(), v.map(From::from)))
.collect::<Vec<_>>();
assert_eq!(is, expected);
}
@@ -474,10 +470,7 @@ mod test {
changeset.set(b"key1".to_vec(), Some(b"val1".to_vec()), Some(2));
changeset.set(b"key0".to_vec(), Some(b"val0-1".to_vec()), Some(9));
assert_drained(changeset, vec![
(b"key0", Some(b"val0-1")),
(b"key1", Some(b"val1")),
]);
assert_drained(changeset, vec![(b"key0", Some(b"val0-1")), (b"key1", Some(b"val1"))]);
}
#[test]
@@ -599,10 +592,8 @@ mod test {
changeset.rollback_transaction().unwrap();
assert_eq!(changeset.transaction_depth(), 0);
let rolled_back: Changes = vec![
(b"key0", (Some(b"val0-1"), vec![1, 10])),
(b"key1", (Some(b"val1"), vec![1])),
];
let rolled_back: Changes =
vec![(b"key0", (Some(b"val0-1"), vec![1, 10])), (b"key1", (Some(b"val1"), vec![1]))];
assert_changes(&changeset, &rolled_back);
assert_drained_changes(changeset, rolled_back);
@@ -676,21 +667,27 @@ mod test {
changeset.clear_where(|k, _| k.starts_with(b"del"), Some(5));
assert_changes(&changeset, &vec![
(b"del1", (None, vec![3, 5])),
(b"del2", (None, vec![4, 5])),
(b"key0", (Some(b"val0"), vec![1])),
(b"key1", (Some(b"val1"), vec![2])),
]);
assert_changes(
&changeset,
&vec![
(b"del1", (None, vec![3, 5])),
(b"del2", (None, vec![4, 5])),
(b"key0", (Some(b"val0"), vec![1])),
(b"key1", (Some(b"val1"), vec![2])),
],
);
changeset.rollback_transaction().unwrap();
assert_changes(&changeset, &vec![
(b"del1", (Some(b"delval1"), vec![3])),
(b"del2", (Some(b"delval2"), vec![4])),
(b"key0", (Some(b"val0"), vec![1])),
(b"key1", (Some(b"val1"), vec![2])),
]);
assert_changes(
&changeset,
&vec![
(b"del1", (Some(b"delval1"), vec![3])),
(b"del2", (Some(b"delval2"), vec![4])),
(b"key0", (Some(b"val0"), vec![1])),
(b"key1", (Some(b"val1"), vec![2])),
],
);
}
#[test]
@@ -708,29 +705,52 @@ mod test {
changeset.set(b"key11".to_vec(), Some(b"val11".to_vec()), Some(11));
assert_eq!(changeset.changes_after(b"key0").next().unwrap().0, b"key1");
assert_eq!(changeset.changes_after(b"key0").next().unwrap().1.value(), Some(&b"val1".to_vec()));
assert_eq!(
changeset.changes_after(b"key0").next().unwrap().1.value(),
Some(&b"val1".to_vec())
);
assert_eq!(changeset.changes_after(b"key1").next().unwrap().0, b"key11");
assert_eq!(changeset.changes_after(b"key1").next().unwrap().1.value(), Some(&b"val11".to_vec()));
assert_eq!(
changeset.changes_after(b"key1").next().unwrap().1.value(),
Some(&b"val11".to_vec())
);
assert_eq!(changeset.changes_after(b"key11").next().unwrap().0, b"key2");
assert_eq!(changeset.changes_after(b"key11").next().unwrap().1.value(), Some(&b"val2".to_vec()));
assert_eq!(
changeset.changes_after(b"key11").next().unwrap().1.value(),
Some(&b"val2".to_vec())
);
assert_eq!(changeset.changes_after(b"key2").next().unwrap().0, b"key3");
assert_eq!(changeset.changes_after(b"key2").next().unwrap().1.value(), Some(&b"val3".to_vec()));
assert_eq!(
changeset.changes_after(b"key2").next().unwrap().1.value(),
Some(&b"val3".to_vec())
);
assert_eq!(changeset.changes_after(b"key3").next().unwrap().0, b"key4");
assert_eq!(changeset.changes_after(b"key3").next().unwrap().1.value(), Some(&b"val4".to_vec()));
assert_eq!(
changeset.changes_after(b"key3").next().unwrap().1.value(),
Some(&b"val4".to_vec())
);
assert_eq!(changeset.changes_after(b"key4").next(), None);
changeset.rollback_transaction().unwrap();
assert_eq!(changeset.changes_after(b"key0").next().unwrap().0, b"key1");
assert_eq!(changeset.changes_after(b"key0").next().unwrap().1.value(), Some(&b"val1".to_vec()));
assert_eq!(
changeset.changes_after(b"key0").next().unwrap().1.value(),
Some(&b"val1".to_vec())
);
assert_eq!(changeset.changes_after(b"key1").next().unwrap().0, b"key2");
assert_eq!(changeset.changes_after(b"key1").next().unwrap().1.value(), Some(&b"val2".to_vec()));
assert_eq!(
changeset.changes_after(b"key1").next().unwrap().1.value(),
Some(&b"val2".to_vec())
);
assert_eq!(changeset.changes_after(b"key11").next().unwrap().0, b"key2");
assert_eq!(changeset.changes_after(b"key11").next().unwrap().1.value(), Some(&b"val2".to_vec()));
assert_eq!(
changeset.changes_after(b"key11").next().unwrap().1.value(),
Some(&b"val2".to_vec())
);
assert_eq!(changeset.changes_after(b"key2").next(), None);
assert_eq!(changeset.changes_after(b"key3").next(), None);
assert_eq!(changeset.changes_after(b"key4").next(), None);
}
#[test]
@@ -790,9 +810,7 @@ mod test {
changeset.commit_transaction().unwrap();
assert_eq!(changeset.transaction_depth(), 0);
assert_drained(changeset, vec![
(b"key0", Some(b"val0")),
]);
assert_drained(changeset, vec![(b"key0", Some(b"val0"))]);
}
#[test]
@@ -20,36 +20,35 @@
mod changeset;
mod offchain;
pub use offchain::OffchainOverlayedChanges;
use crate::{
backend::Backend,
stats::StateMachineStats,
};
use sp_std::{vec::Vec, any::{TypeId, Any}, boxed::Box};
use self::changeset::OverlayedChangeSet;
use crate::{backend::Backend, stats::StateMachineStats};
pub use offchain::OffchainOverlayedChanges;
use sp_std::{
any::{Any, TypeId},
boxed::Box,
vec::Vec,
};
use crate::{changes_trie::BlockNumber, DefaultError};
#[cfg(feature = "std")]
use crate::{
changes_trie::{build_changes_trie, State as ChangesTrieState},
ChangesTrieTransaction,
changes_trie::{
build_changes_trie,
State as ChangesTrieState,
},
};
use crate::changes_trie::BlockNumber;
#[cfg(feature = "std")]
use std::collections::{HashMap as Map, hash_map::Entry as MapEntry};
use codec::{Decode, Encode};
use hash_db::Hasher;
use sp_core::{
offchain::OffchainOverlayedChange,
storage::{well_known_keys::EXTRINSIC_INDEX, ChildInfo},
};
use sp_externalities::{Extension, Extensions};
#[cfg(not(feature = "std"))]
use sp_std::collections::btree_map::{BTreeMap as Map, Entry as MapEntry};
use sp_std::collections::btree_set::BTreeSet;
use codec::{Decode, Encode};
use sp_core::storage::{well_known_keys::EXTRINSIC_INDEX, ChildInfo};
use sp_core::offchain::OffchainOverlayedChange;
use hash_db::Hasher;
use crate::DefaultError;
use sp_externalities::{Extensions, Extension};
#[cfg(feature = "std")]
use std::collections::{hash_map::Entry as MapEntry, HashMap as Map};
pub use self::changeset::{OverlayedValue, NoOpenTransaction, AlreadyInRuntime, NotInRuntime};
pub use self::changeset::{AlreadyInRuntime, NoOpenTransaction, NotInRuntime, OverlayedValue};
/// Changes that are made outside of extrinsics are marked with this index;
pub const NO_EXTRINSIC_INDEX: u32 = 0xffffffff;
@@ -129,7 +128,7 @@ pub enum IndexOperation {
extrinsic: u32,
/// Referenced index hash.
hash: Vec<u8>,
}
},
}
/// A storage changes structure that can be generated by the data collected in [`OverlayedChanges`].
@@ -169,7 +168,9 @@ pub struct StorageChanges<Transaction, H: Hasher, N: BlockNumber> {
#[cfg(feature = "std")]
impl<Transaction, H: Hasher, N: BlockNumber> StorageChanges<Transaction, H, N> {
/// Deconstruct into the inner values
pub fn into_inner(self) -> (
pub fn into_inner(
self,
) -> (
StorageCollection,
ChildStorageCollection,
OffchainChangesCollection,
@@ -216,7 +217,9 @@ impl<Transaction, H: Hasher, N: BlockNumber> StorageTransactionCache<Transaction
}
}
impl<Transaction, H: Hasher, N: BlockNumber> Default for StorageTransactionCache<Transaction, H, N> {
impl<Transaction, H: Hasher, N: BlockNumber> Default
for StorageTransactionCache<Transaction, H, N>
{
fn default() -> Self {
Self {
transaction: None,
@@ -231,7 +234,9 @@ impl<Transaction, H: Hasher, N: BlockNumber> Default for StorageTransactionCache
}
}
impl<Transaction: Default, H: Hasher, N: BlockNumber> Default for StorageChanges<Transaction, H, N> {
impl<Transaction: Default, H: Hasher, N: BlockNumber> Default
for StorageChanges<Transaction, H, N>
{
fn default() -> Self {
Self {
main_storage_changes: Default::default(),
@@ -325,12 +330,10 @@ impl OverlayedChanges {
self.stats.tally_write_overlay(size_write);
let storage_key = child_info.storage_key().to_vec();
let top = &self.top;
let (changeset, info) = self.children.entry(storage_key).or_insert_with(||
(
top.spawn_child(),
child_info.clone()
)
);
let (changeset, info) = self
.children
.entry(storage_key)
.or_insert_with(|| (top.spawn_child(), child_info.clone()));
let updatable = info.try_update(child_info);
debug_assert!(updatable);
changeset.set(key, val, extrinsic_index);
@@ -339,19 +342,14 @@ impl OverlayedChanges {
/// Clear child storage of given storage key.
///
/// Can be rolled back or committed when called inside a transaction.
pub(crate) fn clear_child_storage(
&mut self,
child_info: &ChildInfo,
) {
pub(crate) fn clear_child_storage(&mut self, child_info: &ChildInfo) {
let extrinsic_index = self.extrinsic_index();
let storage_key = child_info.storage_key().to_vec();
let top = &self.top;
let (changeset, info) = self.children.entry(storage_key).or_insert_with(||
(
top.spawn_child(),
child_info.clone()
)
);
let (changeset, info) = self
.children
.entry(storage_key)
.or_insert_with(|| (top.spawn_child(), child_info.clone()));
let updatable = info.try_update(child_info);
debug_assert!(updatable);
changeset.clear_where(|_, _| true, extrinsic_index);
@@ -367,20 +365,14 @@ impl OverlayedChanges {
/// Removes all key-value pairs which keys share the given prefix.
///
/// Can be rolled back or committed when called inside a transaction
pub(crate) fn clear_child_prefix(
&mut self,
child_info: &ChildInfo,
prefix: &[u8],
) {
pub(crate) fn clear_child_prefix(&mut self, child_info: &ChildInfo, prefix: &[u8]) {
let extrinsic_index = self.extrinsic_index();
let storage_key = child_info.storage_key().to_vec();
let top = &self.top;
let (changeset, info) = self.children.entry(storage_key).or_insert_with(||
(
top.spawn_child(),
child_info.clone()
)
);
let (changeset, info) = self
.children
.entry(storage_key)
.or_insert_with(|| (top.spawn_child(), child_info.clone()));
let updatable = info.try_update(child_info);
debug_assert!(updatable);
changeset.clear_where(|key, _| key.starts_with(prefix), extrinsic_index);
@@ -417,11 +409,14 @@ impl OverlayedChanges {
pub fn rollback_transaction(&mut self) -> Result<(), NoOpenTransaction> {
self.top.rollback_transaction()?;
retain_map(&mut self.children, |_, (changeset, _)| {
changeset.rollback_transaction()
changeset
.rollback_transaction()
.expect("Top and children changesets are started in lockstep; qed");
!changeset.is_empty()
});
self.offchain.overlay_mut().rollback_transaction()
self.offchain
.overlay_mut()
.rollback_transaction()
.expect("Top and offchain changesets are started in lockstep; qed");
Ok(())
}
@@ -433,10 +428,13 @@ impl OverlayedChanges {
pub fn commit_transaction(&mut self) -> Result<(), NoOpenTransaction> {
self.top.commit_transaction()?;
for (_, (changeset, _)) in self.children.iter_mut() {
changeset.commit_transaction()
changeset
.commit_transaction()
.expect("Top and children changesets are started in lockstep; qed");
}
self.offchain.overlay_mut().commit_transaction()
self.offchain
.overlay_mut()
.commit_transaction()
.expect("Top and offchain changesets are started in lockstep; qed");
Ok(())
}
@@ -448,10 +446,13 @@ impl OverlayedChanges {
pub fn enter_runtime(&mut self) -> Result<(), AlreadyInRuntime> {
self.top.enter_runtime()?;
for (_, (changeset, _)) in self.children.iter_mut() {
changeset.enter_runtime()
changeset
.enter_runtime()
.expect("Top and children changesets are entering runtime in lockstep; qed")
}
self.offchain.overlay_mut().enter_runtime()
self.offchain
.overlay_mut()
.enter_runtime()
.expect("Top and offchain changesets are started in lockstep; qed");
Ok(())
}
@@ -463,10 +464,13 @@ impl OverlayedChanges {
pub fn exit_runtime(&mut self) -> Result<(), NotInRuntime> {
self.top.exit_runtime()?;
for (_, (changeset, _)) in self.children.iter_mut() {
changeset.exit_runtime()
changeset
.exit_runtime()
.expect("Top and children changesets are entering runtime in lockstep; qed");
}
self.offchain.overlay_mut().exit_runtime()
self.offchain
.overlay_mut()
.exit_runtime()
.expect("Top and offchain changesets are started in lockstep; qed");
Ok(())
}
@@ -477,19 +481,23 @@ impl OverlayedChanges {
///
/// Panics:
/// Panics if `transaction_depth() > 0`
fn drain_committed(&mut self) -> (
impl Iterator<Item=(StorageKey, Option<StorageValue>)>,
impl Iterator<Item=(StorageKey, (impl Iterator<Item=(StorageKey, Option<StorageValue>)>, ChildInfo))>,
fn drain_committed(
&mut self,
) -> (
impl Iterator<Item = (StorageKey, Option<StorageValue>)>,
impl Iterator<
Item = (
StorageKey,
(impl Iterator<Item = (StorageKey, Option<StorageValue>)>, ChildInfo),
),
>,
) {
use sp_std::mem::take;
(
take(&mut self.top).drain_commited(),
take(&mut self.children).into_iter()
.map(|(key, (val, info))| (
key,
(val.drain_commited(), info)
)
),
take(&mut self.children)
.into_iter()
.map(|(key, (val, info))| (key, (val.drain_commited(), info))),
)
}
@@ -499,24 +507,29 @@ impl OverlayedChanges {
///
/// Panics:
/// Panics if `transaction_depth() > 0`
pub fn offchain_drain_committed(&mut self) -> impl Iterator<Item=((StorageKey, StorageKey), OffchainOverlayedChange)> {
pub fn offchain_drain_committed(
&mut self,
) -> impl Iterator<Item = ((StorageKey, StorageKey), OffchainOverlayedChange)> {
self.offchain.drain()
}
/// Get an iterator over all child changes as seen by the current transaction.
pub fn children(&self)
-> impl Iterator<Item=(impl Iterator<Item=(&StorageKey, &OverlayedValue)>, &ChildInfo)> {
pub fn children(
&self,
) -> impl Iterator<Item = (impl Iterator<Item = (&StorageKey, &OverlayedValue)>, &ChildInfo)> {
self.children.iter().map(|(_, v)| (v.0.changes(), &v.1))
}
/// Get an iterator over all top changes as been by the current transaction.
pub fn changes(&self) -> impl Iterator<Item=(&StorageKey, &OverlayedValue)> {
pub fn changes(&self) -> impl Iterator<Item = (&StorageKey, &OverlayedValue)> {
self.top.changes()
}
/// Get an optional iterator over all child changes stored under the supplied key.
pub fn child_changes(&self, key: &[u8])
-> Option<(impl Iterator<Item=(&StorageKey, &OverlayedValue)>, &ChildInfo)> {
pub fn child_changes(
&self,
key: &[u8],
) -> Option<(impl Iterator<Item = (&StorageKey, &OverlayedValue)>, &ChildInfo)> {
self.children.get(key).map(|(overlay, info)| (overlay.changes(), info))
}
@@ -527,16 +540,16 @@ impl OverlayedChanges {
/// Convert this instance with all changes into a [`StorageChanges`] instance.
#[cfg(feature = "std")]
pub fn into_storage_changes<
B: Backend<H>, H: Hasher, N: BlockNumber
>(
pub fn into_storage_changes<B: Backend<H>, H: Hasher, N: BlockNumber>(
mut self,
backend: &B,
changes_trie_state: Option<&ChangesTrieState<H, N>>,
parent_hash: H::Out,
mut cache: StorageTransactionCache<B::Transaction, H, N>,
) -> Result<StorageChanges<B::Transaction, H, N>, DefaultError>
where H::Out: Ord + Encode + 'static {
where
H::Out: Ord + Encode + 'static,
{
self.drain_storage_changes(backend, changes_trie_state, parent_hash, &mut cache)
}
@@ -544,35 +557,34 @@ impl OverlayedChanges {
pub fn drain_storage_changes<B: Backend<H>, H: Hasher, N: BlockNumber>(
&mut self,
backend: &B,
#[cfg(feature = "std")]
changes_trie_state: Option<&ChangesTrieState<H, N>>,
#[cfg(feature = "std")] changes_trie_state: Option<&ChangesTrieState<H, N>>,
parent_hash: H::Out,
mut cache: &mut StorageTransactionCache<B::Transaction, H, N>,
) -> Result<StorageChanges<B::Transaction, H, N>, DefaultError>
where H::Out: Ord + Encode + 'static {
where
H::Out: Ord + Encode + 'static,
{
// If the transaction does not exist, we generate it.
if cache.transaction.is_none() {
self.storage_root(backend, &mut cache);
}
let (transaction, transaction_storage_root) = cache.transaction.take()
let (transaction, transaction_storage_root) = cache
.transaction
.take()
.and_then(|t| cache.transaction_storage_root.take().map(|tr| (t, tr)))
.expect("Transaction was be generated as part of `storage_root`; qed");
// If the transaction does not exist, we generate it.
#[cfg(feature = "std")]
if cache.changes_trie_transaction.is_none() {
self.changes_trie_root(
backend,
changes_trie_state,
parent_hash,
false,
&mut cache,
).map_err(|_| "Failed to generate changes trie transaction")?;
self.changes_trie_root(backend, changes_trie_state, parent_hash, false, &mut cache)
.map_err(|_| "Failed to generate changes trie transaction")?;
}
#[cfg(feature = "std")]
let changes_trie_transaction = cache.changes_trie_transaction
let changes_trie_transaction = cache
.changes_trie_transaction
.take()
.expect("Changes trie transaction was generated by `changes_trie_root`; qed");
@@ -584,7 +596,9 @@ impl OverlayedChanges {
Ok(StorageChanges {
main_storage_changes: main_storage_changes.collect(),
child_storage_changes: child_storage_changes.map(|(sk, it)| (sk, it.0.collect())).collect(),
child_storage_changes: child_storage_changes
.map(|(sk, it)| (sk, it.0.collect()))
.collect(),
offchain_storage_changes,
transaction,
transaction_storage_root,
@@ -614,7 +628,8 @@ impl OverlayedChanges {
true => Some(
self.storage(EXTRINSIC_INDEX)
.and_then(|idx| idx.and_then(|idx| Decode::decode(&mut &*idx).ok()))
.unwrap_or(NO_EXTRINSIC_INDEX)),
.unwrap_or(NO_EXTRINSIC_INDEX),
),
false => None,
}
}
@@ -628,13 +643,13 @@ impl OverlayedChanges {
backend: &B,
cache: &mut StorageTransactionCache<B::Transaction, H, N>,
) -> H::Out
where H::Out: Ord + Encode,
where
H::Out: Ord + Encode,
{
let delta = self.changes().map(|(k, v)| (&k[..], v.value().map(|v| &v[..])));
let child_delta = self.children()
.map(|(changes, info)| (info, changes.map(
|(k, v)| (&k[..], v.value().map(|v| &v[..]))
)));
let child_delta = self.children().map(|(changes, info)| {
(info, changes.map(|(k, v)| (&k[..], v.value().map(|v| &v[..]))))
});
let (root, transaction) = backend.full_storage_root(delta, child_delta);
@@ -659,14 +674,18 @@ impl OverlayedChanges {
parent_hash: H::Out,
panic_on_storage_error: bool,
cache: &mut StorageTransactionCache<B::Transaction, H, N>,
) -> Result<Option<H::Out>, ()> where H::Out: Ord + Encode + 'static {
) -> Result<Option<H::Out>, ()>
where
H::Out: Ord + Encode + 'static,
{
build_changes_trie::<_, H, N>(
backend,
changes_trie_state,
self,
parent_hash,
panic_on_storage_error,
).map(|r| {
)
.map(|r| {
let root = r.as_ref().map(|r| r.1).clone();
cache.changes_trie_transaction = Some(r.map(|(db, _, cache)| (db, cache)));
cache.changes_trie_transaction_storage_root = Some(root);
@@ -685,7 +704,7 @@ impl OverlayedChanges {
pub fn child_iter_after(
&self,
storage_key: &[u8],
key: &[u8]
key: &[u8],
) -> impl Iterator<Item = (&[u8], &OverlayedValue)> {
self.children
.get(storage_key)
@@ -716,18 +735,18 @@ impl OverlayedChanges {
#[cfg(feature = "std")]
fn retain_map<K, V, F>(map: &mut Map<K, V>, f: F)
where
K: std::cmp::Eq + std::hash::Hash,
F: FnMut(&K, &mut V) -> bool,
where
K: std::cmp::Eq + std::hash::Hash,
F: FnMut(&K, &mut V) -> bool,
{
map.retain(f);
}
#[cfg(not(feature = "std"))]
fn retain_map<K, V, F>(map: &mut Map<K, V>, mut f: F)
where
K: Ord,
F: FnMut(&K, &mut V) -> bool,
where
K: Ord,
F: FnMut(&K, &mut V) -> bool,
{
let old = sp_std::mem::replace(map, Map::default());
for (k, mut v) in old.into_iter() {
@@ -799,18 +818,13 @@ impl<'a> OverlayedExtensions<'a> {
#[cfg(test)]
mod tests {
use hex_literal::hex;
use sp_core::{Blake2Hasher, traits::Externalities};
use crate::InMemoryBackend;
use crate::ext::Ext;
use super::*;
use crate::{ext::Ext, InMemoryBackend};
use hex_literal::hex;
use sp_core::{traits::Externalities, Blake2Hasher};
use std::collections::BTreeMap;
fn assert_extrinsics(
overlay: &OverlayedChangeSet,
key: impl AsRef<[u8]>,
expected: Vec<u32>,
) {
fn assert_extrinsics(overlay: &OverlayedChangeSet, key: impl AsRef<[u8]>, expected: Vec<u32>) {
assert_eq!(
overlay.get(key.as_ref()).unwrap().extrinsics().into_iter().collect::<Vec<_>>(),
expected
@@ -863,13 +877,16 @@ mod tests {
state.commit_transaction().unwrap();
}
let offchain_data: Vec<_> = state.offchain_drain_committed().collect();
let expected: Vec<_> = expected.into_iter().map(|(key, value)| {
let change = match value {
Some(value) => OffchainOverlayedChange::SetValue(value),
None => OffchainOverlayedChange::Remove,
};
((STORAGE_PREFIX.to_vec(), key), change)
}).collect();
let expected: Vec<_> = expected
.into_iter()
.map(|(key, value)| {
let change = match value {
Some(value) => OffchainOverlayedChange::SetValue(value),
None => OffchainOverlayedChange::Remove,
};
((STORAGE_PREFIX.to_vec(), key), change)
})
.collect();
assert_eq!(offchain_data, expected);
}
@@ -904,7 +921,6 @@ mod tests {
check_offchain_content(&overlayed, 0, vec![(key.clone(), None)]);
}
#[test]
fn overlayed_storage_root_works() {
let initial: BTreeMap<_, _> = vec![
@@ -912,7 +928,9 @@ mod tests {
(b"dog".to_vec(), b"puppyXXX".to_vec()),
(b"dogglesworth".to_vec(), b"catXXX".to_vec()),
(b"doug".to_vec(), b"notadog".to_vec()),
].into_iter().collect();
]
.into_iter()
.collect();
let backend = InMemoryBackend::<Blake2Hasher>::from(initial);
let mut overlay = OverlayedChanges::default();
overlay.set_collect_extrinsics(false);
@@ -935,7 +953,8 @@ mod tests {
crate::changes_trie::disabled_state::<_, u64>(),
None,
);
const ROOT: [u8; 32] = hex!("39245109cef3758c2eed2ccba8d9b370a917850af3824bc8348d505df2c298fa");
const ROOT: [u8; 32] =
hex!("39245109cef3758c2eed2ccba8d9b370a917850af3824bc8348d505df2c298fa");
assert_eq!(&ext.storage_root()[..], &ROOT);
}
@@ -17,9 +17,9 @@
//! Overlayed changes for offchain indexing.
use super::changeset::OverlayedMap;
use sp_core::offchain::OffchainOverlayedChange;
use sp_std::prelude::Vec;
use super::changeset::OverlayedMap;
/// In-memory storage for offchain workers recoding changes for the actual offchain storage implementation.
#[derive(Debug, Clone, Default)]
@@ -52,11 +52,9 @@ impl OffchainOverlayedChanges {
/// Remove a key and its associated value from the offchain database.
pub fn remove(&mut self, prefix: &[u8], key: &[u8]) {
let _ = self.0.set(
(prefix.to_vec(), key.to_vec()),
OffchainOverlayedChange::Remove,
None,
);
let _ = self
.0
.set((prefix.to_vec(), key.to_vec()), OffchainOverlayedChange::Remove, None);
}
/// Set the value associated with a key under a prefix to the value provided.
@@ -80,7 +78,9 @@ impl OffchainOverlayedChanges {
}
/// Mutable reference to inner change set.
pub fn overlay_mut(&mut self) -> &mut OverlayedMap<(Vec<u8>, Vec<u8>), OffchainOverlayedChange> {
pub fn overlay_mut(
&mut self,
) -> &mut OverlayedMap<(Vec<u8>, Vec<u8>), OffchainOverlayedChange> {
&mut self.0
}
}
@@ -120,10 +120,10 @@ mod test {
let mut iter = ooc.into_iter();
assert_eq!(
iter.next(),
Some(
((STORAGE_PREFIX.to_vec(), b"ppp".to_vec()),
OffchainOverlayedChange::SetValue(b"rrr".to_vec()))
)
Some((
(STORAGE_PREFIX.to_vec(), b"ppp".to_vec()),
OffchainOverlayedChange::SetValue(b"rrr".to_vec())
))
);
assert_eq!(iter.next(), None);
}