Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

[FRAME] Paginated Storage Primitives #14747

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
13 changes: 11 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,9 @@ members = [
"frame/nomination-pools/benchmarking",
"frame/nomination-pools/test-staking",
"frame/nomination-pools/runtime-api",
"frame/insecure-randomness-collective-flip",
"frame/paged-list",
"frame/paged-list/fuzzer",
"frame/insecure-randomness-collective-flip",
"frame/ranked-collective",
"frame/recovery",
"frame/referenda",
Expand All @@ -209,6 +209,7 @@ members = [
"frame/support/test",
"frame/support/test/compile_pass",
"frame/support/test/pallet",
"frame/support/fuzzer",
"frame/system",
"frame/system/benchmarking",
"frame/system/rpc/runtime-api",
Expand Down
24 changes: 3 additions & 21 deletions frame/benchmarking/pov/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,6 @@ sp-std = { version = "8.0.0", default-features = false, path = "../../../primiti

[features]
default = ["std"]
std = [
"codec/std",
"frame-benchmarking/std",
"frame-support/std",
"frame-system/std",
"scale-info/std",
"sp-io/std",
"sp-runtime/std",
"sp-std/std",
]
runtime-benchmarks = [
"frame-system/runtime-benchmarks",
"frame-benchmarking/runtime-benchmarks",
"frame-support/runtime-benchmarks",
"sp-runtime/runtime-benchmarks"
]
try-runtime = [
"frame-support/try-runtime",
"frame-system/try-runtime",
"sp-runtime/try-runtime"
]
std = ["codec/std", "frame-benchmarking/std", "frame-support/std", "frame-system/std", "scale-info/std", "sp-io/std", "sp-runtime/std", "sp-std/std"]
runtime-benchmarks = ["frame-system/runtime-benchmarks", "frame-benchmarking/runtime-benchmarks", "frame-support/runtime-benchmarks", "sp-runtime/runtime-benchmarks"]
try-runtime = ["frame-support/try-runtime", "frame-system/try-runtime", "sp-runtime/try-runtime"]
12 changes: 11 additions & 1 deletion frame/benchmarking/pov/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

use super::*;

use frame_support::traits::UnfilteredDispatchable;
use frame_support::{pallet_prelude::*, traits::UnfilteredDispatchable};
use frame_system::{Pallet as System, RawOrigin};
use sp_runtime::traits::Hash;

Expand Down Expand Up @@ -330,6 +330,16 @@ frame_benchmarking::benchmarks! {
}
}

paged_list_iter_pages {
let n in 0 .. 20;
PagedList64k::<T>::append_many(0u32..((1<<14) * 20));
}: {
let mut iter = PagedList64k::<T>::iter();
for i in 0 .. ((1<<14) * n) {
assert_eq!(iter.next(), Some(i));
}
}

impl_benchmark_test_suite!(
Pallet,
mock::new_test_ext(),
Expand Down
3 changes: 3 additions & 0 deletions frame/benchmarking/pov/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ pub mod pallet {
pub(crate) type UnboundedMapTwox<T: Config> =
StorageMap<Hasher = Twox64Concat, Key = u32, Value = Vec<u32>, QueryKind = OptionQuery>;

#[pallet::storage]
pub(super) type PagedList64k<T: Config> = StoragePagedList<_, u32>;

#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
pub enum Event<T: Config> {
Expand Down
20 changes: 20 additions & 0 deletions frame/examples/basic/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use crate::*;
use frame_support::{
assert_ok,
dispatch::{DispatchInfo, GetDispatchInfo},
pallet_prelude::*,
traits::{ConstU64, OnInitialize},
};
use sp_core::H256;
Expand Down Expand Up @@ -195,3 +196,22 @@ fn weights_work() {
// TODO: account for proof size weight
assert!(info1.weight.ref_time() > info2.weight.ref_time());
}

#[test]
fn paged_nmap_works() {
new_test_ext().execute_with(|| {
use frame_support::storage::StorageKeyedList;

for x in 0..10 {
for y in 0..10 {
PagedNMap::<Test>::append_many((x, y), 0..3);
}
}

for x in 0..10 {
for y in 0..10 {
assert_eq!(PagedNMap::<Test>::iter((x, y)).collect::<Vec<_>>(), vec![0, 1, 2]);
}
}
});
}
2 changes: 1 addition & 1 deletion frame/paged-list/fuzzer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ description = "Fuzz storage types of pallet-paged-list"
publish = false

[[bin]]
name = "pallet-paged-list"
name = "pallet-paged-list-fuzzer"
path = "src/paged_list.rs"

[dependencies]
Expand Down
8 changes: 5 additions & 3 deletions frame/paged-list/fuzzer/src/paged_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
//!
//! # Debugging a panic
//! Once a panic is found, it can be debugged with
//! `cargo hfuzz run-debug pallet-paged-list hfuzz_workspace/pallet-paged-list/*.fuzz`.
//! `cargo hfuzz run-debug pallet-paged-list-fuzzer
//! hfuzz_workspace/pallet-paged-list-fuzzer/*.fuzz`.
//!
//! # More information
//! More information about `honggfuzz` can be found
Expand All @@ -47,7 +48,7 @@ fn main() {
///
/// It also changes the maximal number of elements per page dynamically, hence the `page_size`.
fn drain_append_work(ops: Vec<Op>, page_size: u8) {
if page_size == 0 {
if page_size < 4 {
return
}

Expand All @@ -61,11 +62,12 @@ fn drain_append_work(ops: Vec<Op>, page_size: u8) {

assert!(total >= 0);
assert_eq!(List::iter().count(), total as usize);
assert_eq!(total as u64, List::len());

// We have the assumption that the queue removes the metadata when empty.
if total == 0 {
assert_eq!(List::drain().count(), 0);
assert_eq!(Meta::from_storage().unwrap_or_default(), Default::default());
assert_eq!(Meta::from_storage(((),)).unwrap_or_default(), Default::default());
}
}

Expand Down
7 changes: 5 additions & 2 deletions frame/paged-list/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,14 @@
pub use pallet::*;

pub mod mock;
mod paged_list;
mod tests;

use codec::FullCodec;
use frame_support::{
pallet_prelude::StorageList,
storage::types::StoragePagedList,
traits::{PalletInfoAccess, StorageInstance},
};
pub use paged_list::StoragePagedList;

#[frame_support::pallet]
pub mod pallet {
Expand Down Expand Up @@ -111,6 +110,10 @@ impl<T: Config<I>, I: 'static> StorageList<T::Value> for Pallet<T, I> {
type Iterator = <List<T, I> as StorageList<T::Value>>::Iterator;
type Appender = <List<T, I> as StorageList<T::Value>>::Appender;

fn len() -> u64 {
List::<T, I>::len()
}

fn iter() -> Self::Iterator {
List::<T, I>::iter()
}
Expand Down
7 changes: 5 additions & 2 deletions frame/paged-list/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@

#![cfg(feature = "std")]

use crate::{paged_list::StoragePagedListMeta, Config, ListPrefix};
use frame_support::traits::{ConstU16, ConstU64};
use crate::{Config, ListPrefix};
use frame_support::{
storage::types::StoragePagedListMeta,
traits::{ConstU16, ConstU64},
};
use sp_core::H256;
use sp_runtime::{
traits::{BlakeTwo256, IdentityLookup},
Expand Down
Loading
Loading