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

Parachain staking ledger migration #1077

Merged
merged 5 commits into from
Nov 14, 2023
Merged
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
4 changes: 2 additions & 2 deletions pallets/slp/src/agents/common.rs
Original file line number Diff line number Diff line change
@@ -474,7 +474,7 @@ impl<T: Config> Pallet<T> {
_ => None,
};

let entry = LedgerUpdateEntry::Moonbeam(ParachainStakingLedgerUpdateEntry {
let entry = LedgerUpdateEntry::ParachainStaking(ParachainStakingLedgerUpdateEntry {
currency_id,
delegator_id: *who,
validator_id: validator,
@@ -513,7 +513,7 @@ impl<T: Config> Pallet<T> {
let mut all_occupied = true;

for (_, ledger) in DelegatorLedgers::<T>::iter_prefix(currency_id) {
if let Ledger::Moonbeam(moonbeam_ledger) = ledger {
if let Ledger::ParachainStaking(moonbeam_ledger) = ledger {
if moonbeam_ledger.delegations.len() > moonbeam_ledger.request_briefs.len() {
all_occupied = false;
break;
47 changes: 24 additions & 23 deletions pallets/slp/src/agents/parachain_staking_agent/agent.rs
Original file line number Diff line number Diff line change
@@ -120,7 +120,7 @@ impl<T: Config>
.ok_or(Error::<T>::ValidatorNotExist)?;

let ledger_option = DelegatorLedgers::<T>::get(currency_id, who);
if let Some(Ledger::Moonbeam(ledger)) = ledger_option {
if let Some(Ledger::ParachainStaking(ledger)) = ledger_option {
ensure!(
ledger.status == OneToManyDelegatorStatus::Active,
Error::<T>::DelegatorLeaving
@@ -171,7 +171,7 @@ impl<T: Config>
request_briefs: request_briefs_set,
status: OneToManyDelegatorStatus::Active,
};
let moonbeam_ledger = Ledger::<BalanceOf<T>>::Moonbeam(new_ledger);
let moonbeam_ledger = Ledger::<BalanceOf<T>>::ParachainStaking(new_ledger);

DelegatorLedgers::<T>::insert(currency_id, who, moonbeam_ledger);
}
@@ -206,7 +206,7 @@ impl<T: Config>
currency_id,
who,
|old_ledger_opt| -> Result<(), Error<T>> {
if let Some(Ledger::Moonbeam(ref mut old_ledger)) = old_ledger_opt {
if let Some(Ledger::ParachainStaking(ref mut old_ledger)) = old_ledger_opt {
// first bond and bond more operations
// If this is a bonding operation.
// Increase the total amount and add the delegation relationship.
@@ -336,7 +336,7 @@ impl<T: Config>
ensure!(validators.contains(&collator), Error::<T>::ValidatorError);

let ledger_option = DelegatorLedgers::<T>::get(currency_id, who);
if let Some(Ledger::Moonbeam(ledger)) = ledger_option {
if let Some(Ledger::ParachainStaking(ledger)) = ledger_option {
ensure!(
ledger.status == OneToManyDelegatorStatus::Active,
Error::<T>::DelegatorLeaving
@@ -374,7 +374,7 @@ impl<T: Config>
currency_id,
who,
|old_ledger_opt| -> Result<(), Error<T>> {
if let Some(Ledger::Moonbeam(ref mut old_ledger)) = old_ledger_opt {
if let Some(Ledger::ParachainStaking(ref mut old_ledger)) = old_ledger_opt {
// first bond and bond more operations
// If this is a bonding operation.
// Increase the total amount and add the delegation relationship.
@@ -476,7 +476,7 @@ impl<T: Config>
let collator = (*validator).ok_or(Error::<T>::ValidatorNotProvided)?;

let ledger_option = DelegatorLedgers::<T>::get(currency_id, who);
if let Some(Ledger::Moonbeam(ledger)) = ledger_option {
if let Some(Ledger::ParachainStaking(ledger)) = ledger_option {
ensure!(
ledger.status == OneToManyDelegatorStatus::Active,
Error::<T>::DelegatorLeaving
@@ -526,7 +526,7 @@ impl<T: Config>
currency_id,
who,
|old_ledger_opt| -> Result<(), Error<T>> {
if let Some(Ledger::Moonbeam(ref mut old_ledger)) = old_ledger_opt {
if let Some(Ledger::ParachainStaking(ref mut old_ledger)) = old_ledger_opt {
ensure!(
old_ledger.status == OneToManyDelegatorStatus::Active,
Error::<T>::DelegatorLeaving
@@ -634,7 +634,7 @@ impl<T: Config>
// check if the delegator exists.
let ledger_option = DelegatorLedgers::<T>::get(currency_id, who);

if let Some(Ledger::Moonbeam(ledger)) = ledger_option {
if let Some(Ledger::ParachainStaking(ledger)) = ledger_option {
// check if the delegator is in the state of leaving.
ensure!(ledger.status == OneToManyDelegatorStatus::Active, Error::<T>::AlreadyLeaving);
} else {
@@ -650,7 +650,7 @@ impl<T: Config>
currency_id,
who,
|old_ledger_opt| -> Result<(), Error<T>> {
if let Some(Ledger::Moonbeam(ref mut old_ledger)) = old_ledger_opt {
if let Some(Ledger::ParachainStaking(ref mut old_ledger)) = old_ledger_opt {
ensure!(
old_ledger.status == OneToManyDelegatorStatus::Active,
Error::<T>::DelegatorAlreadyLeaving
@@ -702,7 +702,7 @@ impl<T: Config>
let collator = (*validator).ok_or(Error::<T>::ValidatorNotProvided)?;

let ledger_option = DelegatorLedgers::<T>::get(currency_id, who);
if let Some(Ledger::Moonbeam(ledger)) = ledger_option {
if let Some(Ledger::ParachainStaking(ledger)) = ledger_option {
ensure!(
ledger.status == OneToManyDelegatorStatus::Active,
Error::<T>::DelegatorLeaving
@@ -744,7 +744,7 @@ impl<T: Config>
currency_id,
who,
|old_ledger_opt| -> Result<(), Error<T>> {
if let Some(Ledger::Moonbeam(ref mut old_ledger)) = old_ledger_opt {
if let Some(Ledger::ParachainStaking(ref mut old_ledger)) = old_ledger_opt {
ensure!(
old_ledger.status == OneToManyDelegatorStatus::Active,
Error::<T>::DelegatorLeaving
@@ -856,7 +856,7 @@ impl<T: Config>

// First, check if the delegator exists.
let ledger_option = DelegatorLedgers::<T>::get(currency_id, who);
if let Some(Ledger::Moonbeam(ledger)) = ledger_option {
if let Some(Ledger::ParachainStaking(ledger)) = ledger_option {
ensure!(
ledger.status == OneToManyDelegatorStatus::Active,
Error::<T>::DelegatorLeaving
@@ -883,7 +883,7 @@ impl<T: Config>
currency_id,
who,
|old_ledger_opt| -> Result<(), Error<T>> {
if let Some(Ledger::Moonbeam(ref mut old_ledger)) = old_ledger_opt {
if let Some(Ledger::ParachainStaking(ref mut old_ledger)) = old_ledger_opt {
ensure!(
old_ledger.status == OneToManyDelegatorStatus::Active,
Error::<T>::DelegatorLeaving
@@ -991,7 +991,7 @@ impl<T: Config>

// first check if the delegator exists.
let ledger_option = DelegatorLedgers::<T>::get(currency_id, who);
if let Some(Ledger::Moonbeam(ledger)) = ledger_option {
if let Some(Ledger::ParachainStaking(ledger)) = ledger_option {
// check if the delegator is in the state of leaving.
match ledger.status {
OneToManyDelegatorStatus::Leaving(_) => Ok(()),
@@ -1010,7 +1010,7 @@ impl<T: Config>
currency_id,
who,
|old_ledger_opt| -> Result<(), Error<T>> {
if let Some(Ledger::Moonbeam(ref mut old_ledger)) = old_ledger_opt {
if let Some(Ledger::ParachainStaking(ref mut old_ledger)) = old_ledger_opt {
let leaving = matches!(old_ledger.status, OneToManyDelegatorStatus::Leaving(_));
ensure!(leaving, Error::<T>::DelegatorNotLeaving);

@@ -1059,7 +1059,7 @@ impl<T: Config>

let ledger_option = DelegatorLedgers::<T>::get(currency_id, who);
let mut due_amount = Zero::zero();
if let Some(Ledger::Moonbeam(ledger)) = ledger_option {
if let Some(Ledger::ParachainStaking(ledger)) = ledger_option {
// check if the delegator is in the state of leaving. If yes, execute leaving.
if let OneToManyDelegatorStatus::Leaving(leaving_time) = ledger.status {
ensure!(now >= leaving_time, Error::<T>::LeavingNotDue);
@@ -1097,7 +1097,7 @@ impl<T: Config>
currency_id,
who,
|old_ledger_opt| -> Result<(), Error<T>> {
if let Some(Ledger::Moonbeam(ref mut old_ledger)) = old_ledger_opt {
if let Some(Ledger::ParachainStaking(ref mut old_ledger)) = old_ledger_opt {
// make sure leaving time is less than or equal to current time.
let scheduled_time =
if let OneToManyDelegatorStatus::Leaving(scheduled_time_unit) =
@@ -1141,7 +1141,7 @@ impl<T: Config>
status: OneToManyDelegatorStatus::Active,
};
let parachain_staking_ledger =
Ledger::<BalanceOf<T>>::Moonbeam(new_ledger);
Ledger::<BalanceOf<T>>::ParachainStaking(new_ledger);

*old_ledger_opt = Some(parachain_staking_ledger);
Ok(())
@@ -1162,7 +1162,7 @@ impl<T: Config>
currency_id,
who,
|old_ledger_opt| -> Result<(), Error<T>> {
if let Some(Ledger::Moonbeam(ref mut old_ledger)) = old_ledger_opt {
if let Some(Ledger::ParachainStaking(ref mut old_ledger)) = old_ledger_opt {
ensure!(
old_ledger.status == OneToManyDelegatorStatus::Active,
Error::<T>::DelegatorLeaving
@@ -1464,7 +1464,7 @@ impl<T: Config>
let ledger =
DelegatorLedgers::<T>::get(currency_id, who).ok_or(Error::<T>::DelegatorNotBonded)?;

let total = if let Ledger::Moonbeam(moonbeam_ledger) = ledger {
let total = if let Ledger::ParachainStaking(moonbeam_ledger) = ledger {
moonbeam_ledger.total
} else {
Err(Error::<T>::Unexpected)?
@@ -1586,7 +1586,7 @@ impl<T: Config> ParachainStakingAgent<T> {
LeaveDelegator, Revoke,
};
// update DelegatorLedgers<T> storage
if let LedgerUpdateEntry::Moonbeam(ParachainStakingLedgerUpdateEntry {
if let LedgerUpdateEntry::ParachainStaking(ParachainStakingLedgerUpdateEntry {
currency_id: _,
delegator_id,
validator_id: validator_id_op,
@@ -1599,7 +1599,7 @@ impl<T: Config> ParachainStakingAgent<T> {
currency_id,
delegator_id,
|old_ledger_opt| -> Result<(), Error<T>> {
if let Some(Ledger::Moonbeam(ref mut old_ledger)) = old_ledger_opt {
if let Some(Ledger::ParachainStaking(ref mut old_ledger)) = old_ledger_opt {
match update_operation {
// first bond and bond more operations
Bond => {
@@ -1815,7 +1815,8 @@ impl<T: Config> ParachainStakingAgent<T> {
request_briefs: request_briefs_set,
status: OneToManyDelegatorStatus::Active,
};
let moonbeam_ledger = Ledger::<BalanceOf<T>>::Moonbeam(new_ledger);
let moonbeam_ledger =
Ledger::<BalanceOf<T>>::ParachainStaking(new_ledger);

*old_ledger_opt = Some(moonbeam_ledger);
// execute request
7 changes: 5 additions & 2 deletions pallets/slp/src/agents/utils.rs
Original file line number Diff line number Diff line change
@@ -226,7 +226,9 @@ impl<T: Config> Pallet<T> {
if now <= timeout {
let currency_id = match entry.clone() {
LedgerUpdateEntry::Substrate(substrate_entry) => Some(substrate_entry.currency_id),
LedgerUpdateEntry::Moonbeam(moonbeam_entry) => Some(moonbeam_entry.currency_id),
LedgerUpdateEntry::ParachainStaking(moonbeam_entry) =>
Some(moonbeam_entry.currency_id),
_ => None,
}
.ok_or(Error::<T>::NotSupportedCurrencyId)?;

@@ -283,7 +285,8 @@ impl<T: Config> Pallet<T> {
Self::get_delegator_ledger_update_entry(query_id).ok_or(Error::<T>::QueryNotExist)?;
let currency_id = match entry {
LedgerUpdateEntry::Substrate(substrate_entry) => Some(substrate_entry.currency_id),
LedgerUpdateEntry::Moonbeam(moonbeam_entry) => Some(moonbeam_entry.currency_id),
LedgerUpdateEntry::ParachainStaking(moonbeam_entry) => Some(moonbeam_entry.currency_id),
_ => None,
}
.ok_or(Error::<T>::NotSupportedCurrencyId)?;

1 change: 1 addition & 0 deletions pallets/slp/src/migrations/mod.rs
Original file line number Diff line number Diff line change
@@ -18,3 +18,4 @@

/// Version 2.
pub mod v2;
pub mod v3;
103 changes: 103 additions & 0 deletions pallets/slp/src/migrations/v3.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
// This file is part of Bifrost.

// Copyright (C) 2019-2022 Liebi Technologies (UK) Ltd.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

use crate::*;
use frame_support::traits::OnRuntimeUpgrade;

const LOG_TARGET: &str = "SLP::migration";

pub struct SlpMigration3<T>(sp_std::marker::PhantomData<T>);
impl<T: Config> OnRuntimeUpgrade for SlpMigration3<T> {
fn on_runtime_upgrade() -> frame_support::weights::Weight {
// Check the storage version
let onchain_version = Pallet::<T>::on_chain_storage_version();
if onchain_version < 3 {
log::info!(target: LOG_TARGET, "Start to migrate DelegatorLedgers storage...");

let mut write_count = 0;
//migrate the value type of DelegatorLedgers
DelegatorLedgers::<T>::translate(|key1, key2, value: Ledger<BalanceOf<T>>| {
// Only migrate the Ledger::ParachainStaking
if key1 == MOVR || key1 == GLMR {
write_count = write_count + 1;
// change Ledger::Moonbeam to Ledger::ParachainStaking
if let Ledger::Moonbeam(ledger) = value {
let new_ledger = Ledger::ParachainStaking(ledger);

log::info!(
target: LOG_TARGET,
"Migrated to Ledger for {:?} - {:?}... into {:?}",
key1,
key2,
new_ledger.clone()
);
Some(new_ledger)
} else {
Some(value)
}
} else {
Some(value)
}
});

// Update the storage version
StorageVersion::new(3).put::<Pallet<T>>();

// Return the consumed weight
let read_count = DelegatorLedgers::<T>::iter().count();
Weight::from(T::DbWeight::get().reads_writes(read_count as u64, write_count as u64))
} else {
// We don't do anything here.
Weight::zero()
}
}

#[cfg(feature = "try-runtime")]
fn pre_upgrade() -> Result<Vec<u8>, &'static str> {
let delegator_Ledgers_cnt = DelegatorLedgers::<T>::iter().count();
// print out the pre-migrate storage count
log::info!(
target: LOG_TARGET,
"DelegatorLedgers pre-migrate storage count: {:?}",
delegator_Ledgers_cnt
);

let cnt = (delegator_Ledgers_cnt as u32);
Ok(cnt.encode())
}

#[cfg(feature = "try-runtime")]
fn post_upgrade(cnt: Vec<u8>) -> Result<(), &'static str> {
let (delegator_Ledgers_old): (u32) = Decode::decode(&mut cnt.as_slice())
.expect("the state parameter should be something that was generated by pre_upgrade");

let delegator_Ledgers_new = DelegatorLedgers::<T>::iter().count();
// print out the post-migrate storage count
log::info!(
target: LOG_TARGET,
"DelegatorLedgers post-migrate storage count: {:?}",
delegator_Ledgers_new
);
ensure!(
delegator_Ledgers_new as u32 == delegator_Ledgers_old,
"DelegatorLedgers post-migrate storage count not match"
);

Ok(())
}
}
2 changes: 2 additions & 0 deletions pallets/slp/src/primitives/mod.rs
Original file line number Diff line number Diff line change
@@ -39,6 +39,7 @@ pub const BASE_WEIGHT: u64 = 1000;
pub enum Ledger<Balance> {
Substrate(SubstrateLedger<Balance>),
Moonbeam(OneToManyLedger<Balance>),
ParachainStaking(OneToManyLedger<Balance>),
Filecoin(FilecoinLedger<Balance>),
Phala(PhalaLedger<Balance>),
}
@@ -49,6 +50,7 @@ pub enum LedgerUpdateEntry<Balance> {
/// A type for substrate ledger updating entries
Substrate(SubstrateLedgerUpdateEntry<Balance>),
Moonbeam(ParachainStakingLedgerUpdateEntry<Balance>),
ParachainStaking(ParachainStakingLedgerUpdateEntry<Balance>),
}

/// A type for accommodating validators by delegator update entries for different kinds of
Loading