Skip to content

Commit

Permalink
Remove duplicated storage item (#1568)
Browse files Browse the repository at this point in the history
* Remove duplicated storage item

* Remove debug item

* Eliminate cargo warnings

* Fix

Signed-off-by: Xavier Lau <[email protected]>

* Fix

Signed-off-by: Xavier Lau <[email protected]>

* Eliminate warning

---------

Signed-off-by: Xavier Lau <[email protected]>
  • Loading branch information
AurevoirXavier authored Aug 20, 2024
1 parent 4b1a1fb commit 4d9bf13
Show file tree
Hide file tree
Showing 19 changed files with 68 additions and 93 deletions.
2 changes: 1 addition & 1 deletion pallet/account-migration/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ version.workspace = true
[dependencies]
# crates.io
array-bytes = { workspace = true }
codec = { workspace = true, package = "parity-scale-codec" }
codec = { workspace = true }
scale-info = { workspace = true }

# darwinia
Expand Down
2 changes: 1 addition & 1 deletion pallet/asset-limit/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ version.workspace = true

[dependencies]
# crates.io
codec = { workspace = true, package = "parity-scale-codec" }
codec = { workspace = true }
scale-info = { workspace = true }

# moonbeam
Expand Down
2 changes: 1 addition & 1 deletion pallet/deposit/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ version.workspace = true

[dependencies]
# crates.io
codec = { workspace = true, package = "parity-scale-codec" }
codec = { workspace = true }
ethabi = { workspace = true }
log = { workspace = true }
scale-info = { workspace = true }
Expand Down
2 changes: 1 addition & 1 deletion pallet/ethtx-forwarder/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ version.workspace = true

[dependencies]
# crates.io
codec = { workspace = true, package = "parity-scale-codec" }
codec = { workspace = true }
ethabi = { workspace = true }
ethereum = { workspace = true }
log = { workspace = true }
Expand Down
2 changes: 1 addition & 1 deletion pallet/staking/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ version.workspace = true

[dependencies]
# crates.io
codec = { workspace = true, package = "parity-scale-codec" }
codec = { workspace = true }
ethabi = { workspace = true }
log = { workspace = true }
scale-info = { workspace = true }
Expand Down
66 changes: 10 additions & 56 deletions pallet/staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ macro_rules! call_on_exposure {
}
}};
(<$s:ident<$t:ident>>$($f:tt)*) => {{
let s = <$crate::ExposureCacheStates<$t>>::get();
let s = <$crate::CacheStates<$t>>::get();

$crate::call_on_exposure!(s, <$s<$t>>$($f)*)
}};
Expand All @@ -103,7 +103,7 @@ macro_rules! call_on_cache {
}
}};
(<$s:ident<$t:ident>>$($f:tt)*) => {{
let s = <$crate::CollatorsCacheState<$t>>::get();
let s = <$crate::CacheStates<$t>>::get();

$crate::call_on_cache!(s, <$s<$t>>$($f)*)
}};
Expand Down Expand Up @@ -215,7 +215,7 @@ pub mod pallet {
#[pallet::getter(fn collator_of)]
pub type Collators<T: Config> = StorageMap<_, Twox64Concat, T::AccountId, Perbill>;

/// Exposure cache states.
/// Collator/Exposure cache states.
///
/// To avoid extra DB RWs during new session, such as:
/// ```nocompile
Expand All @@ -239,15 +239,11 @@ pub mod pallet {
/// ```
#[pallet::storage]
#[pallet::getter(fn exposure_cache_states)]
pub type ExposureCacheStates<T: Config> = StorageValue<
_,
(CacheState, CacheState, CacheState),
ValueQuery,
ExposureCacheStatesDefault<T>,
>;
/// Default value for [`ExposureCacheStates`].
pub type CacheStates<T: Config> =
StorageValue<_, (CacheState, CacheState, CacheState), ValueQuery, CacheStatesDefault<T>>;
/// Default value for [`CacheStates`].
#[pallet::type_value]
pub fn ExposureCacheStatesDefault<T: Config>() -> (CacheState, CacheState, CacheState) {
pub fn CacheStatesDefault<T: Config>() -> (CacheState, CacheState, CacheState) {
(CacheState::Previous, CacheState::Current, CacheState::Next)
}

Expand Down Expand Up @@ -334,42 +330,6 @@ pub mod pallet {
#[pallet::getter(fn migration_start_point)]
pub type MigrationStartPoint<T: Config> = StorageValue<_, Moment, ValueQuery>;

/// Collator cache state.
///
/// To avoid extra DB RWs during new session, such as:
/// ```nocompile
/// previous = current;
/// current = next;
/// next = elect();
/// ```
///
/// Now, with data:
/// ```nocompile
/// cache1 == previous;
/// cache2 == current;
/// cache3 == next;
/// ```
/// Just need to shift the marker and write the storage map once:
/// ```nocompile
/// mark(cache3, current);
/// mark(cache2, previous);
/// mark(cache1, next);
/// cache1 = elect();
/// ```
#[pallet::storage]
#[pallet::getter(fn collator_cache_state)]
pub type CollatorsCacheState<T: Config> = StorageValue<
_,
(CacheState, CacheState, CacheState),
ValueQuery,
CollatorCacheStateDefault<T>,
>;
/// Default value for [`CollatorsCacheState`].
#[pallet::type_value]
pub fn CollatorCacheStateDefault<T: Config>() -> (CacheState, CacheState, CacheState) {
(CacheState::Previous, CacheState::Current, CacheState::Next)
}

// TODO: use `BoundedVec`.
/// Exposure cache 0.
#[pallet::storage]
Expand Down Expand Up @@ -789,8 +749,6 @@ pub mod pallet {
for n_exposure in c_exposure.nominators {
let n_payout = Perbill::from_rational(n_exposure.vote, c_exposure.vote) * n_payout;

dbg!(n_payout);

if collator == n_exposure.who {
// If the collator nominated themselves.

Expand Down Expand Up @@ -873,13 +831,9 @@ pub mod pallet {
/// loop { mutate(2, 0, 1) }
/// ```
pub fn shift_cache_states() {
let (s0, s1, s2) = <ExposureCacheStates<T>>::get();

<ExposureCacheStates<T>>::put((s2, s0, s1));

let (s0, s1, s2) = <CollatorsCacheState<T>>::get();
let (s0, s1, s2) = <CacheStates<T>>::get();

<CollatorsCacheState<T>>::put((s2, s0, s1));
<CacheStates<T>>::put((s2, s0, s1));
}

/// Elect the new collators.
Expand Down Expand Up @@ -912,7 +866,7 @@ pub mod pallet {

collators.sort_by(|(_, a), (_, b)| b.cmp(a));

let cache_states = <ExposureCacheStates<T>>::get();
let cache_states = <CacheStates<T>>::get();

collators
.into_iter()
Expand Down
24 changes: 6 additions & 18 deletions pallet/staking/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ fn exposure_cache_states_should_work() {
ExtBuilder::default().build().execute_with(|| {
#[allow(deprecated)]
{
<ExposureCacheStates<Runtime>>::kill();
<CacheStates<Runtime>>::kill();
<ExposureCache0<Runtime>>::remove_all(None);
<ExposureCache1<Runtime>>::remove_all(None);
<ExposureCache2<Runtime>>::remove_all(None);
Expand All @@ -56,7 +56,7 @@ fn exposure_cache_states_should_work() {
assert!(call_on_exposure!(<Next<Runtime>>::get(AccountId(1)).is_none()).unwrap());
assert!(call_on_exposure!(<Next<Runtime>>::get(AccountId(2)).is_some()).unwrap());
assert_eq!(
<ExposureCacheStates<Runtime>>::get(),
<CacheStates<Runtime>>::get(),
(CacheState::Previous, CacheState::Current, CacheState::Next)
);

Expand All @@ -72,7 +72,7 @@ fn exposure_cache_states_should_work() {
assert!(call_on_exposure!(<Next<Runtime>>::get(AccountId(1)).is_none()).unwrap());
assert!(call_on_exposure!(<Next<Runtime>>::get(AccountId(2)).is_none()).unwrap());
assert_eq!(
<ExposureCacheStates<Runtime>>::get(),
<CacheStates<Runtime>>::get(),
(CacheState::Next, CacheState::Previous, CacheState::Current)
);

Expand All @@ -88,7 +88,7 @@ fn exposure_cache_states_should_work() {
assert!(call_on_exposure!(<Next<Runtime>>::get(AccountId(1)).is_some()).unwrap());
assert!(call_on_exposure!(<Next<Runtime>>::get(AccountId(2)).is_none()).unwrap());
assert_eq!(
<ExposureCacheStates<Runtime>>::get(),
<CacheStates<Runtime>>::get(),
(CacheState::Current, CacheState::Next, CacheState::Previous)
);

Expand All @@ -104,7 +104,7 @@ fn exposure_cache_states_should_work() {
assert!(call_on_exposure!(<Next<Runtime>>::get(AccountId(1)).is_none()).unwrap());
assert!(call_on_exposure!(<Next<Runtime>>::get(AccountId(2)).is_some()).unwrap());
assert_eq!(
<ExposureCacheStates<Runtime>>::get(),
<CacheStates<Runtime>>::get(),
(CacheState::Previous, CacheState::Current, CacheState::Next)
);
});
Expand Down Expand Up @@ -864,19 +864,7 @@ fn hybrid_payout_should_work() {
payout();

assert_eq!(
[
1_000 * UNIT,
1_000 * UNIT,
1_000 * UNIT,
1_000 * UNIT,
1_000 * UNIT,
1_000 * UNIT,
1_000 * UNIT,
1_000 * UNIT,
1_000 * UNIT,
1_000 * UNIT,
]
.as_slice(),
[1_000 * UNIT; 10].as_slice(),
collators
.into_iter()
.map(Balances::free_balance)
Expand Down
2 changes: 1 addition & 1 deletion pallet/staking/traits/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ version.workspace = true

[dependencies]
# crates.io
codec = { workspace = true, package = "parity-scale-codec" }
codec = { workspace = true }
scale-info = { workspace = true }

# polkadot-sdk
Expand Down
2 changes: 1 addition & 1 deletion precompile/assets/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ sp-std = { workspace = true }

[dev-dependencies]
# crates.io
codec = { workspace = true, package = "parity-scale-codec" }
codec = { workspace = true }
scale-info = { workspace = true, features = ["std"] }
sha3 = { workspace = true }

Expand Down
2 changes: 1 addition & 1 deletion precompile/deposit/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ sp-std = { workspace = true }

[dev-dependencies]
# crates.io
codec = { workspace = true, package = "parity-scale-codec", features = ["std"] }
codec = { workspace = true, features = ["std"] }
scale-info = { workspace = true, features = ["std"] }

# moonbeam
Expand Down
2 changes: 1 addition & 1 deletion precompile/staking/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ sp-std = { workspace = true }

[dev-dependencies]
# crates.io
codec = { workspace = true, package = "parity-scale-codec", features = ["std"] }
codec = { workspace = true, features = ["std"] }
scale-info = { workspace = true, features = ["std"] }

# darwinia
Expand Down
2 changes: 1 addition & 1 deletion precompile/state-storage/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ sp-std = { workspace = true }

[dev-dependencies]
# crates.io
codec = { workspace = true, package = "parity-scale-codec", features = ["std"] }
codec = { workspace = true, features = ["std"] }
scale-info = { workspace = true, features = ["std"] }

# moonbeam
Expand Down
2 changes: 1 addition & 1 deletion runtime/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ version.workspace = true

[dependencies]
# crates.io
codec = { workspace = true, package = "parity-scale-codec" }
codec = { workspace = true }
log = { workspace = true }
scale-info = { workspace = true }
smallvec = { version = "1.10" }
Expand Down
2 changes: 1 addition & 1 deletion runtime/crab/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ substrate-wasm-builder = { workspace = true, optional = true }

[dependencies]
# crates.io
codec = { workspace = true, package = "parity-scale-codec" }
codec = { workspace = true }
log = { workspace = true }
scale-info = { workspace = true }
strum = { workspace = true, features = ["derive"] }
Expand Down
14 changes: 12 additions & 2 deletions runtime/crab/src/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,16 @@ impl frame_support::traits::OnRuntimeUpgrade for CustomOnRuntimeUpgrade {
}

fn migrate() -> frame_support::weights::Weight {
frame_support::weights::Weight::zero()
// <Runtime as frame_system::Config>::DbWeight::get().reads_writes(10, 10)
// dawinia
use darwinia_staking::CacheState;
if let Some(s) = migration::get_storage_value::<(CacheState, CacheState, CacheState)>(
b"DarwinaStaking",
b"ExposureCacheStates",
&[],
) {
let _ = migration::put_storage_value(b"DarwinaStaking", b"CacheStates", &[], s);
}

// frame_support::weights::Weight::zero()
<Runtime as frame_system::Config>::DbWeight::get().reads_writes(1, 1)
}
2 changes: 1 addition & 1 deletion runtime/darwinia/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ substrate-wasm-builder = { workspace = true, optional = true }

[dependencies]
# crates.io
codec = { workspace = true, package = "parity-scale-codec" }
codec = { workspace = true }
log = { workspace = true }
scale-info = { workspace = true }
strum = { workspace = true, features = ["derive"] }
Expand Down
12 changes: 11 additions & 1 deletion runtime/darwinia/src/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,16 @@ fn migrate() -> frame_support::weights::Weight {
None,
);

// dawinia
use darwinia_staking::CacheState;
if let Some(s) = migration::get_storage_value::<(CacheState, CacheState, CacheState)>(
b"DarwinaStaking",
b"ExposureCacheStates",
&[],
) {
let _ = migration::put_storage_value(b"DarwinaStaking", b"CacheStates", &[], s);
}

// frame_support::weights::Weight::zero()
<Runtime as frame_system::Config>::DbWeight::get().reads_writes(0, 100)
<Runtime as frame_system::Config>::DbWeight::get().reads_writes(1, 101)
}
2 changes: 1 addition & 1 deletion runtime/koi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ substrate-wasm-builder = { workspace = true, optional = true }
[dependencies]
# crates.io
array-bytes = { workspace = true }
codec = { workspace = true, package = "parity-scale-codec" }
codec = { workspace = true }
log = { workspace = true }
scale-info = { workspace = true }
strum = { workspace = true, features = ["derive"] }
Expand Down
17 changes: 15 additions & 2 deletions runtime/koi/src/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,19 @@ impl frame_support::traits::OnRuntimeUpgrade for CustomOnRuntimeUpgrade {
}

fn migrate() -> frame_support::weights::Weight {
frame_support::weights::Weight::zero()
// <Runtime as frame_system::Config>::DbWeight::get().reads_writes(0, 1)
let _ =
migration::clear_storage_prefix(b"DarwinaStaking", b"CollatorCacheState", &[], None, None);

// dawinia
use darwinia_staking::CacheState;
if let Some(s) = migration::get_storage_value::<(CacheState, CacheState, CacheState)>(
b"DarwinaStaking",
b"ExposureCacheStates",
&[],
) {
let _ = migration::put_storage_value(b"DarwinaStaking", b"CacheStates", &[], s);
}

// frame_support::weights::Weight::zero()
<Runtime as frame_system::Config>::DbWeight::get().reads_writes(1, 2)
}

0 comments on commit 4d9bf13

Please sign in to comment.