Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate pallet-paged-list to umbrella crate #6931

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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
7 changes: 1 addition & 6 deletions Cargo.lock

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

25 changes: 4 additions & 21 deletions substrate/frame/paged-list/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,41 +19,24 @@ codec = { features = ["derive"], workspace = true }
docify = { workspace = true }
scale-info = { features = ["derive"], workspace = true }

frame-benchmarking = { optional = true, workspace = true }
frame-support = { workspace = true }
frame-system = { workspace = true }

sp-runtime = { workspace = true }
sp-core = { workspace = true }
sp-io = { workspace = true }
frame = { workspace = true, features = ["experimental", "runtime"] }
sp-metadata-ir = { optional = true, workspace = true }

[features]
default = ["std"]

std = [
"codec/std",
"frame-benchmarking?/std",
"frame-support/std",
"frame-system/std",
"frame/std",
"scale-info/std",
"sp-core/std",
"sp-io/std",
"sp-metadata-ir/std",
"sp-runtime/std",
]

runtime-benchmarks = [
"frame-benchmarking/runtime-benchmarks",
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
"sp-runtime/runtime-benchmarks",
"frame/runtime-benchmarks",
]

try-runtime = [
"frame-support/try-runtime",
"frame-system/try-runtime",
"sp-runtime/try-runtime",
"frame/try-runtime",
]

frame-metadata = ["sp-metadata-ir"]
7 changes: 3 additions & 4 deletions substrate/frame/paged-list/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,15 @@ mod tests;
extern crate alloc;

use codec::FullCodec;
use frame_support::{
pallet_prelude::StorageList,
use frame::{
prelude::*,
traits::{PalletInfoAccess, StorageInstance},
};
pub use paged_list::StoragePagedList;

#[frame_support::pallet]
#[frame::pallet]
pub mod pallet {
use super::*;
use frame_support::pallet_prelude::*;

#[pallet::pallet]
pub struct Pallet<T, I = ()>(_);
Expand Down
9 changes: 4 additions & 5 deletions substrate/frame/paged-list/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,12 @@
#![cfg(feature = "std")]

use crate::{paged_list::StoragePagedListMeta, Config, ListPrefix};
use frame_support::derive_impl;
use sp_runtime::{traits::IdentityLookup, BuildStorage};
use frame::{testing_prelude::*, traits::IdentityLookup};

type Block = frame_system::mocking::MockBlock<Test>;

// Configure a mock runtime to test the pallet.
frame_support::construct_runtime!(
construct_runtime!(
pub enum Test {
System: frame_system,
PagedList: crate,
Expand All @@ -43,7 +42,7 @@ impl frame_system::Config for Test {
type RuntimeEvent = RuntimeEvent;
}

frame_support::parameter_types! {
parameter_types! {
pub storage ValuesPerNewPage: u32 = 5;
pub const MaxPages: Option<u32> = Some(20);
}
Expand All @@ -62,7 +61,7 @@ pub type MetaOf<T, I> =
StoragePagedListMeta<ListPrefix<T, I>, <T as Config>::Value, <T as Config>::ValuesPerNewPage>;

/// Build genesis storage according to the mock runtime.
pub fn new_test_ext() -> sp_io::TestExternalities {
pub fn new_test_ext() -> TestExternalities {
frame_system::GenesisConfig::<Test>::default().build_storage().unwrap().into()
}

Expand Down
56 changes: 25 additions & 31 deletions substrate/frame/paged-list/src/paged_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@
use alloc::vec::Vec;
use codec::{Decode, Encode, EncodeLike, FullCodec};
use core::marker::PhantomData;
use frame_support::{
defensive,
storage::StoragePrefixedContainer,
traits::{Get, StorageInstance},
CloneNoBound, DebugNoBound, DefaultNoBound, EqNoBound, PartialEqNoBound,

use frame::{
deps::{frame_support},
runtime::prelude::storage::StoragePrefixedContainer,
testing_prelude::*,
traits::{Saturating, StorageInstance},
};
use sp_runtime::traits::Saturating;

pub type PageIndex = u32;
pub type ValueIndex = u32;
Expand Down Expand Up @@ -60,10 +60,10 @@ pub type ValueIndex = u32;
/// as long as there are elements in the page and there are pages in storage. All elements of a page
/// are loaded once a page is read from storage. Iteration then happens on the cached elements. This
/// reduces the number of storage `read` calls on the overlay. **Appending** to the list happens by
/// appending to the last page by utilizing [`sp_io::storage::append`]. It allows to directly extend
/// appending to the last page by utilizing [`append`]. It allows to directly extend
/// the elements of `values` vector of the page without loading the whole vector from storage. A new
/// page is instantiated once [`Page::next`] overflows `ValuesPerNewPage`. Its vector will also be
/// created through [`sp_io::storage::append`]. **Draining** advances the internal indices identical
/// created through [`append`]. **Draining** advances the internal indices identical
/// to Iteration. It additionally persists the increments to storage and thereby 'drains' elements.
/// Completely drained pages are deleted from storage.
///
Expand Down Expand Up @@ -111,7 +111,7 @@ pub struct StoragePagedListMeta<Prefix, Value, ValuesPerNewPage> {
_phantom: PhantomData<(Prefix, Value, ValuesPerNewPage)>,
}

impl<Prefix, Value, ValuesPerNewPage> frame_support::storage::StorageAppender<Value>
impl<Prefix, Value, ValuesPerNewPage> storage::StorageAppender<Value>
for StoragePagedListMeta<Prefix, Value, ValuesPerNewPage>
where
Prefix: StorageInstance,
Expand All @@ -135,7 +135,7 @@ where
pub fn from_storage() -> Option<Self> {
let key = Self::key();

sp_io::storage::get(&key).and_then(|raw| Self::decode(&mut &raw[..]).ok())
get(&key).and_then(|raw| Self::decode(&mut &raw[..]).ok())
}

pub fn key() -> Vec<u8> {
Expand All @@ -153,13 +153,13 @@ where
}
let key = page_key::<Prefix>(self.last_page);
self.last_page_len.saturating_inc();
sp_io::storage::append(&key, item.encode());
append(&key, item.encode());
self.store();
}

pub fn store(&self) {
let key = Self::key();
self.using_encoded(|enc| sp_io::storage::set(&key, enc));
self.using_encoded(|enc| set(&key, enc));
}

pub fn reset(&mut self) {
Expand All @@ -168,7 +168,7 @@ where
}

pub fn delete() {
sp_io::storage::clear(&Self::key());
clear(&Self::key());
}
}

Expand All @@ -187,7 +187,7 @@ impl<V: FullCodec> Page<V> {
value_index: ValueIndex,
) -> Option<Self> {
let key = page_key::<Prefix>(index);
let values = sp_io::storage::get(&key)
let values = get(&key)
.and_then(|raw| alloc::vec::Vec::<V>::decode(&mut &raw[..]).ok())?;
if values.is_empty() {
// Don't create empty pages.
Expand All @@ -213,7 +213,7 @@ impl<V: FullCodec> Page<V> {
// Does not live under `Page` since it does not require the `Value` generic.
pub(crate) fn delete_page<Prefix: StorageInstance>(index: PageIndex) {
let key = page_key::<Prefix>(index);
sp_io::storage::clear(&key);
clear(&key);
}

/// Storage key of a page with `index`.
Expand Down Expand Up @@ -311,7 +311,7 @@ where
}
}

impl<Prefix, Value, ValuesPerNewPage> frame_support::storage::StorageList<Value>
impl<Prefix, Value, ValuesPerNewPage> storage::StorageList<Value>
for StoragePagedList<Prefix, Value, ValuesPerNewPage>
where
Prefix: StorageInstance,
Expand Down Expand Up @@ -355,13 +355,13 @@ where
/// Return the elements of the list.
#[cfg(test)]
fn as_vec() -> Vec<Value> {
<Self as frame_support::storage::StorageList<_>>::iter().collect()
<Self as storage::StorageList<_>>::iter().collect()
}

/// Return and remove the elements of the list.
#[cfg(test)]
fn as_drained_vec() -> Vec<Value> {
<Self as frame_support::storage::StorageList<_>>::drain().collect()
<Self as storage::StorageList<_>>::drain().collect()
}
}

Expand Down Expand Up @@ -407,11 +407,6 @@ where
#[allow(dead_code)]
pub(crate) mod mock {
pub use super::*;
pub use frame_support::parameter_types;
#[cfg(test)]
pub use frame_support::{storage::StorageList as _, StorageNoopGuard};
#[cfg(test)]
pub use sp_io::TestExternalities;

parameter_types! {
pub const ValuesPerNewPage: u32 = 5;
Expand Down Expand Up @@ -445,7 +440,6 @@ mod tests {
#[test]
fn simple_drain_works() {
TestExternalities::default().execute_with(|| {
let _g = StorageNoopGuard::default(); // All in all a No-Op
List::append_many(0..1000);

assert_eq!(List::as_drained_vec(), (0..1000).collect::<Vec<_>>());
Expand Down Expand Up @@ -499,13 +493,13 @@ mod tests {
TestExternalities::default().execute_with(|| {
List::append_many(0..9);

assert!(sp_io::storage::exists(&page_key::<Prefix>(0)));
assert!(sp_io::storage::exists(&page_key::<Prefix>(1)));
assert!(exists(&page_key::<Prefix>(0)));
assert!(exists(&page_key::<Prefix>(1)));

assert_eq!(List::drain().take(5).count(), 5);
// Page 0 is eagerly removed.
assert!(!sp_io::storage::exists(&page_key::<Prefix>(0)));
assert!(sp_io::storage::exists(&page_key::<Prefix>(1)));
assert!(!exists(&page_key::<Prefix>(0)));
assert!(exists(&page_key::<Prefix>(1)));
});
}

Expand All @@ -516,16 +510,16 @@ mod tests {
List::append_many(0..9);

let key = page_key::<Prefix>(0);
let raw = sp_io::storage::get(&key).expect("Page should be present");
let raw = get(&key).expect("Page should be present");
let as_vec = Vec::<u32>::decode(&mut &raw[..]).unwrap();
assert_eq!(as_vec.len(), 5, "First page contains 5");

let key = page_key::<Prefix>(1);
let raw = sp_io::storage::get(&key).expect("Page should be present");
let raw = get(&key).expect("Page should be present");
let as_vec = Vec::<u32>::decode(&mut &raw[..]).unwrap();
assert_eq!(as_vec.len(), 4, "Second page contains 4");

let meta = sp_io::storage::get(&meta_key::<Prefix>()).expect("Meta should be present");
let meta = get(&meta_key::<Prefix>()).expect("Meta should be present");
let meta: StoragePagedListMeta<Prefix, u32, ValuesPerNewPage> =
Decode::decode(&mut &meta[..]).unwrap();
assert_eq!(meta.first_page, 0);
Expand Down
6 changes: 4 additions & 2 deletions substrate/frame/paged-list/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@
#![cfg(test)]

use crate::{mock::*, *};
use frame_support::storage::{StorageList, StoragePrefixedContainer};
use frame::{
prelude::storage::{StorageAppender, StoragePrefixedContainer},
testing_prelude::*,
};

#[docify::export]
#[test]
Expand All @@ -46,7 +49,6 @@ fn append_many_works() {
#[docify::export]
#[test]
fn appender_works() {
use frame_support::storage::StorageAppender;
test_closure(|| {
let mut appender = PagedList::appender();

Expand Down
4 changes: 3 additions & 1 deletion substrate/frame/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ pub mod testing_prelude {
/// Other helper macros from `frame_support` that help with asserting in tests.
pub use frame_support::{
assert_err, assert_err_ignore_postinfo, assert_error_encoded_size, assert_noop, assert_ok,
assert_storage_noop, storage_alias,
assert_storage_noop, defensive, storage_alias,
};

pub use frame_system::{self, mocking::*};
Expand All @@ -317,6 +317,8 @@ pub mod testing_prelude {
pub use sp_io::TestExternalities;

pub use sp_io::TestExternalities as TestState;

pub use sp_io::storage::*;
}

/// All of the types and tools needed to build FRAME-based runtimes.
Expand Down
Loading