-
Notifications
You must be signed in to change notification settings - Fork 2.6k
use CountedMap in pallet-bags-list #10179
Changes from all commits
37771d1
8df8b8f
6f42e81
8cb4b38
7c218eb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// This file is part of Substrate. | ||
|
||
// Copyright (C) 2021 Parity Technologies (UK) Ltd. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
//! The migrations of this pallet. | ||
|
||
use frame_support::traits::OnRuntimeUpgrade; | ||
|
||
/// A struct that does not migration, but only checks that the counter prefix exists and is correct. | ||
pub struct CheckCounterPrefix<T: crate::Config>(sp_std::marker::PhantomData<T>); | ||
impl<T: crate::Config> OnRuntimeUpgrade for CheckCounterPrefix<T> { | ||
fn on_runtime_upgrade() -> frame_support::weights::Weight { | ||
0 | ||
} | ||
|
||
#[cfg(feature = "try-runtime")] | ||
fn pre_upgrade() -> Result<(), &'static str> { | ||
use frame_support::ensure; | ||
// The old explicit storage item. | ||
frame_support::generate_storage_alias!(BagsList, CounterForListNodes => Value<u32>); | ||
|
||
// ensure that a value exists in the counter struct. | ||
ensure!( | ||
crate::ListNodes::<T>::count() == CounterForListNodes::get().unwrap(), | ||
"wrong list node counter" | ||
); | ||
|
||
crate::log!( | ||
info, | ||
"checked bags-list prefix to be correct and have {} nodes", | ||
crate::ListNodes::<T>::count() | ||
); | ||
|
||
Ok(()) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,6 +31,7 @@ use crate::{ | |
Never, | ||
}; | ||
use codec::{Decode, Encode, EncodeLike, FullCodec, MaxEncodedLen, Ref}; | ||
use sp_arithmetic::traits::Bounded; | ||
use sp_runtime::traits::Saturating; | ||
use sp_std::prelude::*; | ||
|
||
|
@@ -262,9 +263,10 @@ where | |
} | ||
|
||
/// Remove all value of the storage. | ||
pub fn remove_all() { | ||
CounterFor::<Prefix>::set(0u32); | ||
<Self as MapWrapper>::Map::remove_all(None); | ||
pub fn remove_all(maybe_limit: Option<u32>) { | ||
let leftover = Self::count().saturating_sub(maybe_limit.unwrap_or_else(Bounded::max_value)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is the only logical change here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is not correct, the documentation of remove_all for the storage map is indeed missing, but underneath it calls sp-io::storage::clear_prefix Thus we cannot keep an accurate leftover here, we need to change sp-io::storage::clear_prefix to return here the number of key deleted in the overlay. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @kianenigma wat happens nao? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. follow up is #10231 |
||
CounterFor::<Prefix>::set(leftover); | ||
<Self as MapWrapper>::Map::remove_all(maybe_limit); | ||
} | ||
|
||
/// Iter over all value of the storage. | ||
|
@@ -676,7 +678,7 @@ mod test { | |
assert_eq!(A::count(), 2); | ||
|
||
// Remove all. | ||
A::remove_all(); | ||
A::remove_all(None); | ||
|
||
assert_eq!(A::count(), 0); | ||
assert_eq!(A::initialize_counter(), 0); | ||
|
@@ -907,7 +909,7 @@ mod test { | |
assert_eq!(B::count(), 2); | ||
|
||
// Remove all. | ||
B::remove_all(); | ||
B::remove_all(None); | ||
|
||
assert_eq!(B::count(), 0); | ||
assert_eq!(B::initialize_counter(), 0); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this previous code also doesn't seems correct, it is not ensure that
count
items are removed.