Rework storage iterators (#13284)

* Rework storage iterators

* Make sure storage iteration is also accounted for when benchmarking

* Use `trie-db` from crates.io

* Appease clippy

* Bump `trie-bench` to 0.35.0

* Fix tests' compilation

* Update comment to clarify how `IterArgs::start_at` works

* Add extra tests

* Fix iterators on `Client` so that they behave as before

* Add extra `unwrap`s in tests

* More clippy fixes

* Come on clippy, give me a break already

* Rename `allow_missing` to `stop_on_incomplete_database`

* Add `#[inline]` to `with_recorder_and_cache`

* Use `with_recorder_and_cache` in `with_trie_db`; add doc comment

* Simplify code: use `with_trie_db` in `next_storage_key_from_root`

* Remove `expect`s in the benchmarking CLI

* Add extra doc comments

* Move `RawIter` before `TrieBackendEssence` (no code changes; just cut-paste)

* Remove a TODO in tests

* Update comment for `StorageIterator::was_complete`

* Update `trie-db` to 0.25.1
This commit is contained in:
Koute
2023-02-22 16:49:25 +09:00
committed by GitHub
parent 236bbbd5ef
commit f8e3bdad3d
27 changed files with 1097 additions and 742 deletions
+84 -18
View File
@@ -341,7 +341,20 @@ fn block_builder_works_with_transactions() {
.expect("block 1 was just imported. qed");
assert_eq!(client.chain_info().best_number, 1);
assert_ne!(client.state_at(hash1).unwrap().pairs(), client.state_at(hash0).unwrap().pairs());
assert_ne!(
client
.state_at(hash1)
.unwrap()
.pairs(Default::default())
.unwrap()
.collect::<Vec<_>>(),
client
.state_at(hash0)
.unwrap()
.pairs(Default::default())
.unwrap()
.collect::<Vec<_>>()
);
assert_eq!(
client
.runtime_api()
@@ -394,8 +407,18 @@ fn block_builder_does_not_include_invalid() {
assert_eq!(client.chain_info().best_number, 1);
assert_ne!(
client.state_at(hashof1).unwrap().pairs(),
client.state_at(hashof0).unwrap().pairs()
client
.state_at(hashof1)
.unwrap()
.pairs(Default::default())
.unwrap()
.collect::<Vec<_>>(),
client
.state_at(hashof0)
.unwrap()
.pairs(Default::default())
.unwrap()
.collect::<Vec<_>>()
);
assert_eq!(client.body(hashof1).unwrap().unwrap().len(), 1)
}
@@ -1688,7 +1711,7 @@ fn returns_status_for_pruned_blocks() {
}
#[test]
fn storage_keys_iter_prefix_and_start_key_works() {
fn storage_keys_prefix_and_start_key_works() {
let child_info = ChildInfo::new_default(b"child");
let client = TestClientBuilder::new()
.add_extra_child_storage(&child_info, b"first".to_vec(), vec![0u8; 32])
@@ -1703,7 +1726,7 @@ fn storage_keys_iter_prefix_and_start_key_works() {
let child_prefix = StorageKey(b"sec".to_vec());
let res: Vec<_> = client
.storage_keys_iter(block_hash, Some(&prefix), None)
.storage_keys(block_hash, Some(&prefix), None)
.unwrap()
.map(|x| x.0)
.collect();
@@ -1717,7 +1740,7 @@ fn storage_keys_iter_prefix_and_start_key_works() {
);
let res: Vec<_> = client
.storage_keys_iter(
.storage_keys(
block_hash,
Some(&prefix),
Some(&StorageKey(array_bytes::hex2bytes_unchecked("3a636f6465"))),
@@ -1728,7 +1751,7 @@ fn storage_keys_iter_prefix_and_start_key_works() {
assert_eq!(res, [array_bytes::hex2bytes_unchecked("3a686561707061676573")]);
let res: Vec<_> = client
.storage_keys_iter(
.storage_keys(
block_hash,
Some(&prefix),
Some(&StorageKey(array_bytes::hex2bytes_unchecked("3a686561707061676573"))),
@@ -1739,19 +1762,14 @@ fn storage_keys_iter_prefix_and_start_key_works() {
assert_eq!(res, Vec::<Vec<u8>>::new());
let res: Vec<_> = client
.child_storage_keys_iter(block_hash, child_info.clone(), Some(&child_prefix), None)
.child_storage_keys(block_hash, child_info.clone(), Some(&child_prefix), None)
.unwrap()
.map(|x| x.0)
.collect();
assert_eq!(res, [b"second".to_vec()]);
let res: Vec<_> = client
.child_storage_keys_iter(
block_hash,
child_info,
None,
Some(&StorageKey(b"second".to_vec())),
)
.child_storage_keys(block_hash, child_info, None, Some(&StorageKey(b"second".to_vec())))
.unwrap()
.map(|x| x.0)
.collect();
@@ -1759,7 +1777,7 @@ fn storage_keys_iter_prefix_and_start_key_works() {
}
#[test]
fn storage_keys_iter_works() {
fn storage_keys_works() {
let client = substrate_test_runtime_client::new();
let block_hash = client.info().best_hash;
@@ -1767,7 +1785,7 @@ fn storage_keys_iter_works() {
let prefix = StorageKey(array_bytes::hex2bytes_unchecked(""));
let res: Vec<_> = client
.storage_keys_iter(block_hash, Some(&prefix), None)
.storage_keys(block_hash, Some(&prefix), None)
.unwrap()
.take(9)
.map(|x| array_bytes::bytes2hex("", &x.0))
@@ -1787,8 +1805,56 @@ fn storage_keys_iter_works() {
]
);
// Starting at an empty key nothing gets skipped.
let res: Vec<_> = client
.storage_keys_iter(
.storage_keys(block_hash, Some(&prefix), Some(&StorageKey("".into())))
.unwrap()
.take(9)
.map(|x| array_bytes::bytes2hex("", &x.0))
.collect();
assert_eq!(
res,
[
"00c232cf4e70a5e343317016dc805bf80a6a8cd8ad39958d56f99891b07851e0",
"085b2407916e53a86efeb8b72dbe338c4b341dab135252f96b6ed8022209b6cb",
"0befda6e1ca4ef40219d588a727f1271",
"1a560ecfd2a62c2b8521ef149d0804eb621050e3988ed97dca55f0d7c3e6aa34",
"1d66850d32002979d67dd29dc583af5b2ae2a1f71c1f35ad90fff122be7a3824",
"237498b98d8803334286e9f0483ef513098dd3c1c22ca21c4dc155b4ef6cc204",
"26aa394eea5630e07c48ae0c9558cef75e0621c4869aa60c02be9adcc98a0d1d",
"29b9db10ec5bf7907d8f74b5e60aa8140c4fbdd8127a1ee5600cb98e5ec01729",
"3a636f6465",
]
);
// Starting at an incomplete key nothing gets skipped.
let res: Vec<_> = client
.storage_keys(
block_hash,
Some(&prefix),
Some(&StorageKey(array_bytes::hex2bytes_unchecked("3a636f64"))),
)
.unwrap()
.take(8)
.map(|x| array_bytes::bytes2hex("", &x.0))
.collect();
assert_eq!(
res,
[
"3a636f6465",
"3a686561707061676573",
"52008686cc27f6e5ed83a216929942f8bcd32a396f09664a5698f81371934b56",
"5348d72ac6cc66e5d8cbecc27b0e0677503b845fe2382d819f83001781788fd5",
"5c2d5fda66373dabf970e4fb13d277ce91c5233473321129d32b5a8085fa8133",
"6644b9b8bc315888ac8e41a7968dc2b4141a5403c58acdf70b7e8f7e07bf5081",
"66484000ed3f75c95fc7b03f39c20ca1e1011e5999278247d3b2f5e3c3273808",
"7d5007603a7f5dd729d51d93cf695d6465789443bb967c0d1fe270e388c96eaa",
]
);
// Starting at a complete key the first key is skipped.
let res: Vec<_> = client
.storage_keys(
block_hash,
Some(&prefix),
Some(&StorageKey(array_bytes::hex2bytes_unchecked("3a636f6465"))),
@@ -1811,7 +1877,7 @@ fn storage_keys_iter_works() {
);
let res: Vec<_> = client
.storage_keys_iter(
.storage_keys(
block_hash,
Some(&prefix),
Some(&StorageKey(array_bytes::hex2bytes_unchecked(