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

[AHM] Fast unstake and Bags list #563

Merged
merged 12 commits into from
Jan 31, 2025
Merged
Show file tree
Hide file tree
Changes from 6 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
8 changes: 8 additions & 0 deletions Cargo.lock

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

8 changes: 8 additions & 0 deletions pallets/ah-migrator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ frame-support = { workspace = true }
frame-system = { workspace = true }
log = { workspace = true }
pallet-balances = { workspace = true }
pallet-bags-list = { workspace = true }
pallet-fast-unstake = { workspace = true }
pallet-multisig = { workspace = true }
pallet-nomination-pools = { workspace = true }
pallet-preimage = { workspace = true }
Expand Down Expand Up @@ -45,7 +47,9 @@ std = [
"frame-system/std",
"hex/std",
"log/std",
"pallet-bags-list/std",
"pallet-balances/std",
"pallet-fast-unstake/std",
"pallet-multisig/std",
"pallet-nomination-pools/std",
"pallet-preimage/std",
Expand All @@ -70,7 +74,9 @@ runtime-benchmarks = [
"frame-benchmarking/runtime-benchmarks",
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
"pallet-bags-list/runtime-benchmarks",
"pallet-balances/runtime-benchmarks",
"pallet-fast-unstake/runtime-benchmarks",
"pallet-multisig/runtime-benchmarks",
"pallet-nomination-pools/runtime-benchmarks",
"pallet-preimage/runtime-benchmarks",
Expand All @@ -88,7 +94,9 @@ runtime-benchmarks = [
try-runtime = [
"frame-support/try-runtime",
"frame-system/try-runtime",
"pallet-bags-list/try-runtime",
"pallet-balances/try-runtime",
"pallet-fast-unstake/try-runtime",
"pallet-multisig/try-runtime",
"pallet-nomination-pools/try-runtime",
"pallet-preimage/try-runtime",
Expand Down
55 changes: 52 additions & 3 deletions pallets/ah-migrator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,15 @@ use frame_support::{
use frame_system::pallet_prelude::*;
use pallet_balances::{AccountData, Reasons as LockReasons};
use pallet_rc_migrator::{
accounts::Account as RcAccount, multisig::*, preimage::*, proxy::*, staking::nom_pools::*,
accounts::Account as RcAccount,
multisig::*,
preimage::*,
proxy::*,
staking::{
bags_list::RcBagsListMessage,
fast_unstake::{FastUnstakeMigrator, RcFastUnstakeMessage},
nom_pools::*,
},
};
use pallet_referenda::TrackIdOf;
use referenda::RcReferendumInfoOf;
Expand All @@ -75,6 +83,12 @@ type RcAccountFor<T> = RcAccount<
<T as Config>::RcFreezeReason,
>;

#[derive(Encode, Decode, Clone, PartialEq, Eq, RuntimeDebug, TypeInfo, MaxEncodedLen)]
pub enum PalletEventName {
FastUnstake,
BagsList,
}

#[frame_support::pallet(dev_mode)]
pub mod pallet {
use super::*;
Expand All @@ -90,6 +104,8 @@ pub mod pallet {
+ pallet_preimage::Config<Hash = H256>
+ pallet_referenda::Config<Votes = u128>
+ pallet_nomination_pools::Config
+ pallet_fast_unstake::Config
+ pallet_bags_list::Config<pallet_bags_list::Instance1>
{
/// The overarching event type.
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
Expand Down Expand Up @@ -148,6 +164,8 @@ pub mod pallet {
FailedToUnreserveDeposit,
/// Failed to process an account data from RC.
FailedToProcessAccount,
/// Some item could not be inserted because it already exists.
InsertConflict,
/// Failed to convert RC type to AH type.
FailedToConvertType,
/// Failed to fetch preimage.
Expand Down Expand Up @@ -254,6 +272,17 @@ pub mod pallet {
/// How many nom pools messages failed to integrate.
count_bad: u32,
},
/// We received a batch of messages that will be integrated into a pallet.
BatchReceived {
pallet: PalletEventName,
count: u32,
},
/// We processed a batch of messages for this pallet.
BatchProcessed {
pallet: PalletEventName,
count_good: u32,
count_bad: u32,
},
/// We received a batch of referendums that we are going to integrate.
ReferendumsBatchReceived {
/// How many referendums are in the batch.
Expand Down Expand Up @@ -369,8 +398,18 @@ pub mod pallet {
Self::do_receive_nom_pools_messages(messages).map_err(Into::into)
}

/// Receive referendum counts, deciding counts, votes for the track queue.
#[pallet::call_index(8)]
pub fn receive_fast_unstake_messages(
origin: OriginFor<T>,
messages: Vec<RcFastUnstakeMessage<T>>,
) -> DispatchResult {
ensure_root(origin)?;

Self::do_receive_fast_unstake_messages(messages).map_err(Into::into)
}

/// Receive referendum counts, deciding counts, votes for the track queue.
#[pallet::call_index(9)]
pub fn receive_referenda_values(
origin: OriginFor<T>,
referendum_count: u32,
Expand All @@ -386,7 +425,7 @@ pub mod pallet {
}

/// Receive referendums from the Relay Chain.
#[pallet::call_index(9)]
#[pallet::call_index(10)]
pub fn receive_referendums(
origin: OriginFor<T>,
referendums: Vec<(u32, RcReferendumInfoOf<T, ()>)>,
Expand All @@ -395,6 +434,16 @@ pub mod pallet {

Self::do_receive_referendums(referendums).map_err(Into::into)
}

#[pallet::call_index(11)]
pub fn receive_bags_list_messages(
origin: OriginFor<T>,
messages: Vec<RcBagsListMessage<T>>,
) -> DispatchResult {
ensure_root(origin)?;

Self::do_receive_bags_list_messages(messages).map_err(Into::into)
}
}

#[pallet::hooks]
Expand Down
72 changes: 72 additions & 0 deletions pallets/ah-migrator/src/staking/bags_list.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// This file is part of Substrate.

// Copyright (C) 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.

//! Fast unstake migration logic.

use crate::*;
use pallet_rc_migrator::staking::bags_list::alias;

impl<T: Config> Pallet<T> {
pub fn do_receive_bags_list_messages(
messages: Vec<RcBagsListMessage<T>>,
) -> Result<(), Error<T>> {
let (mut good, mut bad) = (0, 0);
log::info!("Integrating {} BagsListMessages", messages.len());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

log target?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if it adds much. Do you use it often to filter?

Self::deposit_event(Event::BatchReceived {
pallet: PalletEventName::BagsList,
count: messages.len() as u32,
});

for message in messages {
match Self::do_receive_bags_list_message(message) {
Ok(_) => good += 1,
Err(_) => bad += 1,
}
}

Self::deposit_event(Event::BatchProcessed {
pallet: PalletEventName::BagsList,
count_good: good as u32,
count_bad: bad as u32,
});

Ok(())
}

pub fn do_receive_bags_list_message(message: RcBagsListMessage<T>) -> Result<(), Error<T>> {
match message {
RcBagsListMessage::Node { id, node } => {
if alias::ListNodes::<T>::contains_key(&id) {
return Err(Error::<T>::InsertConflict);
}

alias::ListNodes::<T>::insert(&id, &node);
log::debug!("Integrating BagsListNode: {:?}", &id);
},
RcBagsListMessage::Bag { score, bag } => {
if alias::ListBags::<T>::contains_key(&score) {
return Err(Error::<T>::InsertConflict);
}

alias::ListBags::<T>::insert(&score, &bag);
log::debug!("Integrating BagsListBag: {:?}", &score);
},
}

Ok(())
}
}
68 changes: 68 additions & 0 deletions pallets/ah-migrator/src/staking/fast_unstake.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// This file is part of Substrate.

// Copyright (C) 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.

//! Fast unstake migration logic.

use crate::*;

impl<T: Config> Pallet<T> {
pub fn do_receive_fast_unstake_messages(
messages: Vec<RcFastUnstakeMessage<T>>,
) -> Result<(), Error<T>> {
let (mut good, mut bad) = (0, 0);
log::info!("Integrating {} FastUnstakeMessages", messages.len());
Self::deposit_event(Event::BatchReceived {
pallet: PalletEventName::FastUnstake,
count: messages.len() as u32,
});

for message in messages {
match Self::do_receive_fast_unstake_message(message) {
Ok(_) => good += 1,
Err(_) => bad += 1,
}
}

Self::deposit_event(Event::BatchProcessed {
pallet: PalletEventName::FastUnstake,
count_good: good as u32,
count_bad: bad as u32,
});

Ok(())
}

pub fn do_receive_fast_unstake_message(
message: RcFastUnstakeMessage<T>,
) -> Result<(), Error<T>> {
match message {
RcFastUnstakeMessage::StorageValues { values } => {
FastUnstakeMigrator::<T>::put_values(values);
log::debug!("Integrating FastUnstakeStorageValues");
},
RcFastUnstakeMessage::Queue { member } => {
if pallet_fast_unstake::Queue::<T>::contains_key(&member.0) {
return Err(Error::<T>::InsertConflict);
}
log::debug!("Integrating FastUnstakeQueueMember: {:?}", &member.0);
pallet_fast_unstake::Queue::<T>::insert(member.0, member.1);
},
}

Ok(())
}
}
2 changes: 2 additions & 0 deletions pallets/ah-migrator/src/staking/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@

//! Staking migration logic.

pub mod bags_list;
pub mod fast_unstake;
pub mod nom_pools;
8 changes: 8 additions & 0 deletions pallets/rc-migrator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ sp-runtime = { workspace = true }
sp-std = { workspace = true }
sp-io = { workspace = true }
pallet-balances = { workspace = true }
pallet-bags-list = { workspace = true }
pallet-staking = { workspace = true }
pallet-proxy = { workspace = true }
pallet-multisig = { workspace = true }
pallet-preimage = { workspace = true }
pallet-fast-unstake = { workspace = true }
pallet-referenda = { workspace = true }
pallet-nomination-pools = { workspace = true }
polkadot-runtime-common = { workspace = true }
Expand All @@ -41,7 +43,9 @@ std = [
"frame-support/std",
"frame-system/std",
"log/std",
"pallet-bags-list/std",
"pallet-balances/std",
"pallet-fast-unstake/std",
"pallet-multisig/std",
"pallet-nomination-pools/std",
"pallet-preimage/std",
Expand All @@ -64,7 +68,9 @@ runtime-benchmarks = [
"frame-benchmarking/runtime-benchmarks",
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
"pallet-bags-list/runtime-benchmarks",
"pallet-balances/runtime-benchmarks",
"pallet-fast-unstake/runtime-benchmarks",
"pallet-multisig/runtime-benchmarks",
"pallet-nomination-pools/runtime-benchmarks",
"pallet-preimage/runtime-benchmarks",
Expand All @@ -80,7 +86,9 @@ runtime-benchmarks = [
try-runtime = [
"frame-support/try-runtime",
"frame-system/try-runtime",
"pallet-bags-list/try-runtime",
"pallet-balances/try-runtime",
"pallet-fast-unstake/try-runtime",
"pallet-multisig/try-runtime",
"pallet-nomination-pools/try-runtime",
"pallet-preimage/try-runtime",
Expand Down
Loading
Loading