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

Migrate 10 deposits everytime #1585

Merged
merged 2 commits into from
Aug 28, 2024
Merged
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
64 changes: 36 additions & 28 deletions pallet/deposit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,38 +319,44 @@ pub mod pallet {
let who = ensure_signed(origin)?;
let Some(ds) = <Deposits<T>>::take(&who) else { return Ok(()) };
let now = Self::now();
let mut ds = ds.into_iter();
let mut to_claim = (0, Vec::new());
let mut to_migrate = (0, Vec::new(), Vec::new());

for c in ds.chunks(50) {
let mut to_claim = (0, Vec::new());
let mut to_migrate = (0, Vec::new(), Vec::new());

for d in c {
if d.in_use {
Err(<Error<T>>::DepositInUse)?;
}
// Take 0~10 deposits to migrate.
for d in ds.by_ref().take(10) {
if d.in_use {
Err(<Error<T>>::DepositInUse)?;
}

if d.expired_time <= now {
to_claim.0 += d.value;
to_claim.1.push(d.id);
} else {
to_migrate.0 += d.value;
to_migrate.1.push(d.id);
to_migrate.2.push((d.value, d.start_time / 1_000, d.expired_time / 1_000));
}
if d.expired_time <= now {
to_claim.0 += d.value;
to_claim.1.push(d.id);
} else {
to_migrate.0 += d.value;
to_migrate.1.push(d.id);
to_migrate.2.push((d.value, d.start_time / 1_000, d.expired_time / 1_000));
}
}

T::Ring::transfer(&account_id(), &who, to_claim.0, AllowDeath)?;
T::Ring::transfer(&account_id(), &T::Treasury::get(), to_migrate.0, AllowDeath)?;
T::DepositMigrator::migrate(who.clone(), to_migrate.0, to_migrate.2)?;
T::Ring::transfer(&account_id(), &who, to_claim.0, AllowDeath)?;
T::Ring::transfer(&account_id(), &T::Treasury::get(), to_migrate.0, AllowDeath)?;
T::DepositMigrator::migrate(who.clone(), to_migrate.0, to_migrate.2)?;

Self::deposit_event(Event::DepositsClaimed {
owner: who.clone(),
deposits: to_claim.1,
});
Self::deposit_event(Event::DepositsMigrated {
owner: who.clone(),
deposits: to_migrate.1,
});
Self::deposit_event(Event::DepositsClaimed {
owner: who.clone(),
deposits: to_claim.1,
});
Self::deposit_event(Event::DepositsMigrated {
owner: who.clone(),
deposits: to_migrate.1,
});

let ds = ds.collect::<Vec<_>>();

if !ds.is_empty() {
// Put the rest deposits back.
<Deposits<T>>::insert(&who, BoundedVec::truncate_from(ds));
}

Ok(())
Expand Down Expand Up @@ -497,6 +503,7 @@ where
total: Balance,
deposits: Vec<(Balance, Moment, Moment)>,
) -> DispatchResult {
let cnt = deposits.len();
let dc = <DepositContract<T>>::get().ok_or(<Error<T>>::InvalidDepositContract)?.into();
#[allow(deprecated)]
let exit_reason = match darwinia_ethtx_forwarder::quick_forward_transact::<T>(
Expand Down Expand Up @@ -540,7 +547,8 @@ where
],
dc,
total.into(),
1_000_000.into(),
// Approximately consume 160,000 gas per deposit on Koi testnet.
(200_000 * cnt as u64).into(),
)?
.1
{
Expand Down
3 changes: 1 addition & 2 deletions pallet/ethtx-forwarder/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ fn foraward_call_works() {
request,
));
let pallet_ethereum::Event::Executed { to, .. } =
System::read_events_for_pallet().into_iter().nth(0).expect("events expected");
System::read_events_for_pallet().into_iter().next().expect("events expected");

use ethabi::{Function, Param, ParamType, Token};
#[allow(deprecated)]
Expand All @@ -153,7 +153,6 @@ fn foraward_call_works() {
Default::default(),
U256::from(10_000_000u64),
)
.ok()
.expect("call should succeed");
let ExecutionInfoV2 { value, .. } = result;
assert_eq!(U256::from_big_endian(&value), U256::from(1000000));
Expand Down