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

Improve deposits migration #1636

Open
wants to merge 8 commits into
base: main
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
2 changes: 1 addition & 1 deletion pallet/account-migration/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ pub mod pallet {
if let Some(ds) = <Deposits<T>>::take(from) {
<pallet_balances::Pallet<T> as Currency<_>>::transfer(
to,
&darwinia_deposit::account_id(),
&<T as darwinia_deposit::Config>::Treasury::get(),
ds.iter().map(|d| d.value).sum(),
AllowDeath,
)?;
Expand Down
2 changes: 1 addition & 1 deletion pallet/deposit/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ frame-support = { workspace = true }
frame-system = { workspace = true }
pallet-timestamp = { workspace = true }
sp-core = { workspace = true }
sp-runtime = { workspace = true }
sp-std = { workspace = true }
# polkadot-sdk optional
frame-benchmarking = { workspace = true, optional = true }
Expand All @@ -34,6 +33,7 @@ frame-benchmarking = { workspace = true, optional = true }
# polkadot-sdk
pallet-balances = { workspace = true, features = ["std"] }
sp-io = { workspace = true, features = ["std"] }
sp-runtime = { workspace = true, features = ["std"] }

[features]
default = ["std"]
Expand Down
1 change: 0 additions & 1 deletion pallet/deposit/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ mod benchmarks {

<Pallet<T>>::set_deposit_contract(RawOrigin::Root.into(), a.clone()).unwrap();

T::Ring::make_free_balance_be(&account_id(), 2 << 126);
T::Ring::make_free_balance_be(&T::Treasury::get(), 2 << 126);

// Worst-case scenario:
Expand Down
32 changes: 17 additions & 15 deletions pallet/deposit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ pub use weights::WeightInfo;
// core
use core::marker::PhantomData;
// crates.io
use codec::FullCodec;
use ethabi::{Function, Param, ParamType, StateMutability, Token};
// darwinia
use dc_types::{Balance, Moment};
Expand All @@ -50,11 +49,9 @@ use fp_evm::{CallOrCreateInfo, ExitReason};
use frame_support::{
pallet_prelude::*,
traits::{Currency, ExistenceRequirement::AllowDeath, UnixTime},
PalletId,
};
use frame_system::pallet_prelude::*;
use sp_core::H160;
use sp_runtime::traits::AccountIdConversion;
use sp_std::prelude::*;

#[frame_support::pallet]
Expand Down Expand Up @@ -89,6 +86,10 @@ pub mod pallet {
DepositsClaimed { owner: T::AccountId, deposits: Vec<DepositId> },
/// Deposits have been migrated.
DepositsMigrated { owner: T::AccountId, deposits: Vec<DepositId> },
/// Migration interaction with deposit contract failed.
MigrationFailedOnContract { exit_reason: ExitReason },
/// Migration interaction with refund failed.
MigrationFailedOnRefund,
}

#[pallet::error]
Expand Down Expand Up @@ -204,7 +205,7 @@ pub mod pallet {
let mut to_migrate = (0, Vec::new(), Vec::new());

// Take 0~10 deposits to migrate.
for d in deposits.by_ref().take(10) {
for d in deposits.by_ref().take(10).filter(|d| d.value != 0) {
hackfisher marked this conversation as resolved.
Show resolved Hide resolved
if d.expired_time <= now {
to_claim.0 += d.value;
to_claim.1.push(d.id);
Expand All @@ -216,14 +217,19 @@ pub mod pallet {
}

if to_claim.0 != 0 {
T::Ring::transfer(&account_id(), who, to_claim.0, AllowDeath)?;
if let Err(e) = T::Ring::transfer(&T::Treasury::get(), who, to_claim.0, AllowDeath)
{
Self::deposit_event(Event::MigrationFailedOnRefund);

Err(e)?;
Copy link
Contributor

Choose a reason for hiding this comment

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

Do not throw errors, otherwise there are still side effects l.

}

Self::deposit_event(Event::DepositsClaimed {
owner: who.clone(),
deposits: to_claim.1,
});
}
if to_migrate.0 != 0 {
T::Ring::transfer(&account_id(), &T::Treasury::get(), to_migrate.0, AllowDeath)?;
T::DepositMigrator::migrate(who.clone(), to_migrate.0, to_migrate.2)?;
hackfisher marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Contributor

Choose a reason for hiding this comment

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

Should not throw errors here right?

Copy link
Member Author

@AurevoirXavier AurevoirXavier Dec 2, 2024

Choose a reason for hiding this comment

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

In FP, we treat any I/O operation as the side effect.

And here, especially we interact with the DB, take has already introduced the side effects. We should keep the error and utilize it to help rollback the state.

Self::deposit_event(Event::DepositsMigrated {
owner: who.clone(),
Expand Down Expand Up @@ -334,15 +340,11 @@ where

match exit_reason {
ExitReason::Succeed(_) => Ok(()),
_ => Err(<Error<T>>::MigrationFailedOnContract)?,
exit_reason => {
<Pallet<T>>::deposit_event(Event::MigrationFailedOnContract { exit_reason });

Err(<Error<T>>::MigrationFailedOnContract)?
},
}
}
}

/// The account of the deposit pot.
pub fn account_id<A>() -> A
where
A: FullCodec,
{
PalletId(*b"dar/depo").into_account_truncating()
}
4 changes: 4 additions & 0 deletions pallet/deposit/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,7 @@ pub fn new_test_ext() -> TestExternalities {

storage.into()
}

pub fn events() -> Vec<Event<Runtime>> {
System::read_events_for_pallet()
}
30 changes: 28 additions & 2 deletions pallet/deposit/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use frame_support::{assert_ok, traits::OnIdle};
#[test]
fn migrate_should_work() {
new_test_ext().execute_with(|| {
let _ = Balances::deposit_creating(&account_id(), 2);
let _ = Balances::deposit_creating(&0, 2);

<Deposits<Runtime>>::insert(
1,
Expand All @@ -52,9 +52,18 @@ fn on_idle_should_work() {
.collect(),
)
}
fn mock_zero_deposits(count: u16) -> BoundedVec<DepositS, ConstU32<512>> {
BoundedVec::truncate_from(
(0..count)
.map(|id| DepositS { id, value: 0, start_time: 0, expired_time: 0, in_use: false })
.collect(),
)
}

new_test_ext().execute_with(|| {
let _ = Balances::deposit_creating(&account_id(), 10_000);
System::set_block_number(1);

let _ = Balances::deposit_creating(&0, 10_000);

<Deposits<Runtime>>::insert(1, mock_deposits(512));
<Deposits<Runtime>>::insert(2, mock_deposits(512));
Expand Down Expand Up @@ -90,11 +99,28 @@ fn on_idle_should_work() {
assert_eq!(Deposit::deposit_of(2).unwrap().len(), 512);
assert!(Deposit::deposit_of(3).is_none());

System::reset_events();
(0..54).for_each(|_| {
<Deposit as OnIdle<_>>::on_idle(0, Weight::MAX);
});
assert_eq!(events().into_iter().count(), 54);
assert!(Deposit::deposit_of(1).is_none());
assert!(Deposit::deposit_of(2).is_none());
assert!(Deposit::deposit_of(3).is_none());
});
new_test_ext().execute_with(|| {
System::set_block_number(1);

let _ = Balances::deposit_creating(&0, 10_000);

<Deposits<Runtime>>::insert(1, mock_zero_deposits(512));
assert_eq!(Deposit::deposit_of(1).unwrap().len(), 512);

System::reset_events();
(0..52).for_each(|_| {
<Deposit as OnIdle<_>>::on_idle(0, Weight::MAX);
});
assert_eq!(events().into_iter().count(), 0);
assert!(Deposit::deposit_of(1).is_none());
});
}
5 changes: 1 addition & 4 deletions runtime/common/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,10 +304,7 @@ macro_rules! impl_account_migration_tests {

assert_ok!(migrate(from, to));
assert_eq!(Balances::free_balance(to), 80);
assert_eq!(
Balances::free_balance(&darwinia_deposit::account_id::<AccountId>()),
20
);
assert_eq!(Balances::free_balance(&Treasury::account_id()), 20);
assert_eq!(Deposit::deposit_of(to).unwrap().len(), 2);
assert_eq!(Assets::maybe_balance(KTON_ID, to).unwrap(), 100);
});
Expand Down
16 changes: 9 additions & 7 deletions runtime/crab/src/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ impl frame_support::traits::OnRuntimeUpgrade for CustomOnRuntimeUpgrade {
fn post_upgrade(_state: Vec<u8>) -> Result<(), sp_runtime::DispatchError> {
log::info!("post");

darwinia_staking::migration::post_check::<Runtime>();

Ok(())
}

Expand All @@ -47,11 +45,15 @@ impl frame_support::traits::OnRuntimeUpgrade for CustomOnRuntimeUpgrade {
}

fn migrate() -> frame_support::weights::Weight {
if let Ok(k) = array_bytes::hex2bytes("0x1da53b775b270400e7e61ed5cbc5a146ab1160471b1418779239ba8e2b847e42d53de13b56da115d3342f0588bc3614108837de0ae21c270383d9f2de4db03c7b1314632314d8c74970d627c9b4f4c42e06688a9f7a2866905a810c4b1a49b8cb0dca3f1bc953905609869b6e9d4fb794cd36c5f") {
let _ = System::kill_storage(RuntimeOrigin::root(), vec![k]);
}
/// polkadot-sdk
use frame_support::PalletId;
use sp_runtime::traits::AccountIdConversion;

Copy link
Contributor

Choose a reason for hiding this comment

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

Please remove previous migration which have been done in last upgrade.

let (r, w) = darwinia_staking::migration::migrate::<Runtime>();
let _ = Balances::transfer_all(
RuntimeOrigin::signed(PalletId(*b"dar/depo").into_account_truncating()),
Treasury::account_id(),
false,
);

<Runtime as frame_system::Config>::DbWeight::get().reads_writes(r, w + 1)
<Runtime as frame_system::Config>::DbWeight::get().reads_writes(0, 6)
}
11 changes: 9 additions & 2 deletions runtime/darwinia/src/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ impl frame_support::traits::OnRuntimeUpgrade for CustomOnRuntimeUpgrade {

fn migrate() -> frame_support::weights::Weight {
// polkadot-sdk
use frame_support::traits::LockableCurrency;
use frame_support::{traits::LockableCurrency, PalletId};
use sp_runtime::traits::AccountIdConversion;

let (r, w) = darwinia_staking::migration::migrate::<Runtime>();
let _ = migration::clear_storage_prefix(b"Vesting", b"Vesting", &[], None, None);
Expand All @@ -86,5 +87,11 @@ fn migrate() -> frame_support::weights::Weight {
]),
);

<Runtime as frame_system::Config>::DbWeight::get().reads_writes(r, w + 5)
let _ = Balances::transfer_all(
RuntimeOrigin::signed(PalletId(*b"dar/depo").into_account_truncating()),
Copy link
Contributor

Choose a reason for hiding this comment

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

The is still some CRAB left in deposit pallet account on Crab chain.

https://crab-scan.darwinia.network/address/0x6d6F646C6461722F6465706F0000000000000000

We also need to do the transfer migration for CRAB to clean it.

Copy link
Member Author

Choose a reason for hiding this comment

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

I haven't noticed anything from a few days back. This might be due to the account migration.

Treasury::account_id(),
false,
);

<Runtime as frame_system::Config>::DbWeight::get().reads_writes(r, w + 10)
}