Skip to content
This repository was archived by the owner on Jul 4, 2022. It is now read-only.

Commit b442214

Browse files
bkchrchemeshawntabrizishamb0xlc
authored
Add proper commit_all to TestExternalities (#7808)
* Add proper `commit_all` to `TestExternalities` This pr adds a propoer `commit_all` function to `TestExternalities` to commit all changes from the overlay to the internal backend. Besides that it fixes some bugs with handling empty dbs when calculating a delta storage root. It also changes the way data is added to the in memory backend. * Update primitives/state-machine/src/testing.rs Co-authored-by: cheme <emericchevalier.pro@gmail.com> * Don't allow self proxies (#7803) * Allow council to slash treasury tip (#7753) * wk2051 | D4 |Allow council to slash treasury tip | p1 * Update frame/tips/src/lib.rs Co-authored-by: Xiliang Chen <xlchen1291@gmail.com> * wk2051 | D5 |Allow council to slash treasury tip | p2 * wk2051 | D5 |Allow council to slash treasury tip | p3 * wk2051 | D5 |Allow council to slash treasury tip | p4 * wk2051 | D5 |Allow council to slash treasury tip | p5 * random change * cargo run --release --features=runtime-benchmarks --manifest-path=bin/node/cli/Cargo.toml -- benchmark --chain=dev --steps=50 --repeat=20 --pallet=pallet_tips --extrinsic=* --execution=wasm --wasm-execution=compiled --heap-pages=4096 --output=./frame/tips/src/weights.rs --template=./.maintain/frame-weight-template.hbs * fix typo * Update frame/tips/src/lib.rs Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com> * Update frame/tips/src/lib.rs Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com> * Update frame/tips/src/lib.rs Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com> * Update frame/tips/src/lib.rs Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com> * Update frame/tips/src/tests.rs Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com> * wk2052 | D1 | Allow council to slash treasury tip | p6 Co-authored-by: Xiliang Chen <xlchen1291@gmail.com> Co-authored-by: Shawn Tabrizi <shawntabrizi@gmail.com> Co-authored-by: Parity Benchmarking Bot <admin@parity.io> Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com> * Review feedback * Review feedback * Update docs * More docs * Make it private * Use `None` * Use apply transaction * Update primitives/state-machine/src/testing.rs Co-authored-by: cheme <emericchevalier.pro@gmail.com> Co-authored-by: Shawn Tabrizi <shawntabrizi@gmail.com> Co-authored-by: RK <r.raajey@gmail.com> Co-authored-by: Xiliang Chen <xlchen1291@gmail.com> Co-authored-by: Parity Benchmarking Bot <admin@parity.io> Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com> Co-authored-by: Guillaume Thiolliere <gui.thiolliere@gmail.com>
1 parent 67c8cad commit b442214

File tree

3 files changed

+106
-72
lines changed

3 files changed

+106
-72
lines changed

primitives/state-machine/src/in_memory_backend.rs

Lines changed: 34 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -17,56 +17,21 @@
1717
//! State machine in memory backend.
1818
1919
use crate::{
20-
StorageKey, StorageValue, StorageCollection,
21-
trie_backend::TrieBackend,
20+
StorageKey, StorageValue, StorageCollection, trie_backend::TrieBackend, backend::Backend,
2221
};
23-
use std::{collections::{BTreeMap, HashMap}};
22+
use std::collections::{BTreeMap, HashMap};
2423
use hash_db::Hasher;
25-
use sp_trie::{
26-
MemoryDB, TrieMut,
27-
trie_types::TrieDBMut,
28-
};
24+
use sp_trie::{MemoryDB, empty_trie_root, Layout};
2925
use codec::Codec;
3026
use sp_core::storage::{ChildInfo, Storage};
3127

32-
/// Insert input pairs into memory db.
33-
fn insert_into_memory_db<H, I>(mut root: H::Out, mdb: &mut MemoryDB<H>, input: I) -> H::Out
34-
where
35-
H: Hasher,
36-
I: IntoIterator<Item=(StorageKey, Option<StorageValue>)>,
37-
{
38-
{
39-
let mut trie = if root == Default::default() {
40-
TrieDBMut::<H>::new(mdb, &mut root)
41-
} else {
42-
TrieDBMut::<H>::from_existing(mdb, &mut root).unwrap()
43-
};
44-
for (key, value) in input {
45-
if let Err(e) = match value {
46-
Some(value) => {
47-
trie.insert(&key, &value)
48-
},
49-
None => {
50-
trie.remove(&key)
51-
},
52-
} {
53-
panic!("Failed to write to trie: {}", e);
54-
}
55-
}
56-
trie.commit();
57-
}
58-
root
59-
}
60-
6128
/// Create a new empty instance of in-memory backend.
6229
pub fn new_in_mem<H: Hasher>() -> TrieBackend<MemoryDB<H>, H>
6330
where
6431
H::Out: Codec + Ord,
6532
{
6633
let db = MemoryDB::default();
67-
let mut backend = TrieBackend::new(db, Default::default());
68-
backend.insert(std::iter::empty());
69-
backend
34+
TrieBackend::new(db, empty_trie_root::<Layout<H>>())
7035
}
7136

7237
impl<H: Hasher> TrieBackend<MemoryDB<H>, H>
@@ -92,32 +57,16 @@ where
9257
&mut self,
9358
changes: T,
9459
) {
95-
let mut new_child_roots = Vec::new();
96-
let mut root_map = None;
97-
let root = self.root().clone();
98-
for (child_info, map) in changes {
99-
if let Some(child_info) = child_info.as_ref() {
100-
let prefix_storage_key = child_info.prefixed_storage_key();
101-
let ch = insert_into_memory_db::<H, _>(root, self.backend_storage_mut(), map.clone().into_iter());
102-
new_child_roots.push((prefix_storage_key.into_inner(), Some(ch.as_ref().into())));
103-
} else {
104-
root_map = Some(map);
105-
}
106-
}
60+
let (top, child) = changes.into_iter().partition::<Vec<_>, _>(|v| v.0.is_none());
61+
let (root, transaction) = self.full_storage_root(
62+
top.iter().map(|(_, v)| v).flatten().map(|(k, v)| (&k[..], v.as_deref())),
63+
child.iter()
64+
.filter_map(|v|
65+
v.0.as_ref().map(|c| (c, v.1.iter().map(|(k, v)| (&k[..], v.as_deref()))))
66+
),
67+
);
10768

108-
let root = match root_map {
109-
Some(map) => insert_into_memory_db::<H, _>(
110-
root,
111-
self.backend_storage_mut(),
112-
map.into_iter().chain(new_child_roots.into_iter()),
113-
),
114-
None => insert_into_memory_db::<H, _>(
115-
root,
116-
self.backend_storage_mut(),
117-
new_child_roots.into_iter(),
118-
),
119-
};
120-
self.essence.set_root(root);
69+
self.apply_transaction(root, transaction);
12170
}
12271

12372
/// Merge trie nodes into this backend.
@@ -127,6 +76,12 @@ where
12776
Self::new(clone, root)
12877
}
12978

79+
/// Apply the given transaction to this backend and set the root to the given value.
80+
pub fn apply_transaction(&mut self, root: H::Out, transaction: MemoryDB<H>) {
81+
self.backend_storage_mut().consolidate(transaction);
82+
self.essence.set_root(root);
83+
}
84+
13085
/// Compare with another in-memory backend.
13186
pub fn eq(&self, other: &Self) -> bool {
13287
self.root() == other.root()
@@ -158,7 +113,9 @@ where
158113
{
159114
fn from(inner: HashMap<Option<ChildInfo>, BTreeMap<StorageKey, StorageValue>>) -> Self {
160115
let mut backend = new_in_mem();
161-
backend.insert(inner.into_iter().map(|(k, m)| (k, m.into_iter().map(|(k, v)| (k, Some(v))).collect())));
116+
backend.insert(
117+
inner.into_iter().map(|(k, m)| (k, m.into_iter().map(|(k, v)| (k, Some(v))).collect())),
118+
);
162119
backend
163120
}
164121
}
@@ -232,4 +189,16 @@ mod tests {
232189
let storage_key = child_info.prefixed_storage_key();
233190
assert!(trie_backend.storage(storage_key.as_slice()).unwrap().is_some());
234191
}
192+
193+
#[test]
194+
fn insert_multiple_times_child_data_works() {
195+
let mut storage = new_in_mem::<BlakeTwo256>();
196+
let child_info = ChildInfo::new_default(b"1");
197+
198+
storage.insert(vec![(Some(child_info.clone()), vec![(b"2".to_vec(), Some(b"3".to_vec()))])]);
199+
storage.insert(vec![(Some(child_info.clone()), vec![(b"1".to_vec(), Some(b"3".to_vec()))])]);
200+
201+
assert_eq!(storage.child_storage(&child_info, &b"2"[..]), Ok(Some(b"3".to_vec())));
202+
assert_eq!(storage.child_storage(&child_info, &b"1"[..]), Ok(Some(b"3".to_vec())));
203+
}
235204
}

primitives/state-machine/src/testing.rs

Lines changed: 65 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,11 @@ impl<H: Hasher, N: ChangesTrieBlockNumber> TestExternalities<H, N>
153153
&mut self.changes_trie_storage
154154
}
155155

156-
/// Return a new backend with all pending value.
157-
pub fn commit_all(&self) -> InMemoryBackend<H> {
156+
/// Return a new backend with all pending changes.
157+
///
158+
/// In contrast to [`commit_all`](Self::commit_all) this will not panic if there are open
159+
/// transactions.
160+
fn as_backend(&self) -> InMemoryBackend<H> {
158161
let top: Vec<_> = self.overlay.changes()
159162
.map(|(k, v)| (k.clone(), v.value().cloned()))
160163
.collect();
@@ -172,6 +175,23 @@ impl<H: Hasher, N: ChangesTrieBlockNumber> TestExternalities<H, N>
172175
self.backend.update(transaction)
173176
}
174177

178+
/// Commit all pending changes to the underlying backend.
179+
///
180+
/// # Panic
181+
///
182+
/// This will panic if there are still open transactions.
183+
pub fn commit_all(&mut self) -> Result<(), String> {
184+
let changes = self.overlay.drain_storage_changes::<_, _, N>(
185+
&self.backend,
186+
None,
187+
Default::default(),
188+
&mut Default::default(),
189+
)?;
190+
191+
self.backend.apply_transaction(changes.transaction_storage_root, changes.transaction);
192+
Ok(())
193+
}
194+
175195
/// Execute the given closure while `self` is set as externalities.
176196
///
177197
/// Returns the result of the given closure.
@@ -209,7 +229,7 @@ impl<H: Hasher, N: ChangesTrieBlockNumber> PartialEq for TestExternalities<H, N>
209229
/// This doesn't test if they are in the same state, only if they contains the
210230
/// same data at this state
211231
fn eq(&self, other: &TestExternalities<H, N>) -> bool {
212-
self.commit_all().eq(&other.commit_all())
232+
self.as_backend().eq(&other.as_backend())
213233
}
214234
}
215235

@@ -258,7 +278,7 @@ impl<H, N> sp_externalities::ExtensionStore for TestExternalities<H, N> where
258278
#[cfg(test)]
259279
mod tests {
260280
use super::*;
261-
use sp_core::{H256, traits::Externalities};
281+
use sp_core::{H256, traits::Externalities, storage::ChildInfo};
262282
use sp_runtime::traits::BlakeTwo256;
263283
use hex_literal::hex;
264284

@@ -289,4 +309,45 @@ mod tests {
289309
fn assert_send<T: Send>() {}
290310
assert_send::<TestExternalities::<BlakeTwo256, u64>>();
291311
}
312+
313+
#[test]
314+
fn commit_all_and_kill_child_storage() {
315+
let mut ext = TestExternalities::<BlakeTwo256, u64>::default();
316+
let child_info = ChildInfo::new_default(&b"test_child"[..]);
317+
318+
{
319+
let mut ext = ext.ext();
320+
ext.place_child_storage(&child_info, b"doe".to_vec(), Some(b"reindeer".to_vec()));
321+
ext.place_child_storage(&child_info, b"dog".to_vec(), Some(b"puppy".to_vec()));
322+
ext.place_child_storage(&child_info, b"dog2".to_vec(), Some(b"puppy2".to_vec()));
323+
}
324+
325+
ext.commit_all().unwrap();
326+
327+
{
328+
let mut ext = ext.ext();
329+
330+
assert!(!ext.kill_child_storage(&child_info, Some(2)), "Should not delete all keys");
331+
332+
assert!(ext.child_storage(&child_info, &b"doe"[..]).is_none());
333+
assert!(ext.child_storage(&child_info, &b"dog"[..]).is_none());
334+
assert!(ext.child_storage(&child_info, &b"dog2"[..]).is_some());
335+
}
336+
}
337+
338+
#[test]
339+
fn as_backend_generates_same_backend_as_commit_all() {
340+
let mut ext = TestExternalities::<BlakeTwo256, u64>::default();
341+
{
342+
let mut ext = ext.ext();
343+
ext.set_storage(b"doe".to_vec(), b"reindeer".to_vec());
344+
ext.set_storage(b"dog".to_vec(), b"puppy".to_vec());
345+
ext.set_storage(b"dogglesworth".to_vec(), b"cat".to_vec());
346+
}
347+
348+
let backend = ext.as_backend();
349+
350+
ext.commit_all().unwrap();
351+
assert!(ext.backend.eq(&backend), "Both backend should be equal.");
352+
}
292353
}

primitives/trie/src/lib.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ pub fn delta_trie_root<L: TrieConfiguration, I, A, B, DB, V>(
184184
DB: hash_db::HashDB<L::Hash, trie_db::DBValue>,
185185
{
186186
{
187-
let mut trie = TrieDBMut::<L>::from_existing(&mut *db, &mut root)?;
187+
let mut trie = TrieDBMut::<L>::from_existing(db, &mut root)?;
188188

189189
let mut delta = delta.into_iter().collect::<Vec<_>>();
190190
delta.sort_by(|l, r| l.0.borrow().cmp(r.0.borrow()));
@@ -223,9 +223,13 @@ pub fn read_trie_value_with<
223223
Ok(TrieDB::<L>::new(&*db, root)?.get_with(key, query).map(|x| x.map(|val| val.to_vec()))?)
224224
}
225225

226+
/// Determine the empty trie root.
227+
pub fn empty_trie_root<L: TrieConfiguration>() -> <L::Hash as Hasher>::Out {
228+
L::trie_root::<_, Vec<u8>, Vec<u8>>(core::iter::empty())
229+
}
230+
226231
/// Determine the empty child trie root.
227-
pub fn empty_child_trie_root<L: TrieConfiguration>(
228-
) -> <L::Hash as Hasher>::Out {
232+
pub fn empty_child_trie_root<L: TrieConfiguration>() -> <L::Hash as Hasher>::Out {
229233
L::trie_root::<_, Vec<u8>, Vec<u8>>(core::iter::empty())
230234
}
231235

0 commit comments

Comments
 (0)