Skip to content

Commit

Permalink
Allow performing migration for others (#1588)
Browse files Browse the repository at this point in the history
* Allow performing migration for others

* Update contract addrs

* Update abi

* Update sol

* Fix tests

* Fix

* Fix

Signed-off-by: Xavier Lau <xavier@inv.cafe>

---------

Signed-off-by: Xavier Lau <xavier@inv.cafe>
  • Loading branch information
AurevoirXavier committed Sep 3, 2024
1 parent bdcfc09 commit fc952a6
Show file tree
Hide file tree
Showing 16 changed files with 65 additions and 52 deletions.
3 changes: 2 additions & 1 deletion pallet/deposit/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ mod benchmarks {
#[benchmark]
fn migrate() {
let a = frame_benchmarking::whitelisted_caller::<T::AccountId>();
let a_ = a.clone();
let max_deposits = T::MaxDeposits::get();

<Pallet<T>>::set_deposit_contract(RawOrigin::Root.into(), a.clone()).unwrap();
Expand All @@ -133,7 +134,7 @@ mod benchmarks {
});

#[extrinsic_call]
_(RawOrigin::Signed(a));
_(RawOrigin::Signed(a), a_);
}

#[benchmark]
Expand Down
7 changes: 4 additions & 3 deletions pallet/deposit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,11 +312,12 @@ pub mod pallet {
Ok(())
}

/// Migration data to deposit contract.
/// Migrate the specified account's data to deposit contract.
#[pallet::call_index(3)]
#[pallet::weight(<T as Config>::WeightInfo::migrate())]
pub fn migrate(origin: OriginFor<T>) -> DispatchResult {
let who = ensure_signed(origin)?;
pub fn migrate(origin: OriginFor<T>, who: T::AccountId) -> DispatchResult {
ensure_signed(origin)?;

let Some(ds) = <Deposits<T>>::take(&who) else { return Ok(()) };
let now = Self::now();
let mut ds = ds.into_iter();
Expand Down
4 changes: 2 additions & 2 deletions pallet/deposit/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,11 +365,11 @@ fn migrate_should_work() {

mock::set_in_use(1, true);

assert_noop!(Deposit::migrate(RuntimeOrigin::signed(1)), <Error<Runtime>>::DepositInUse);
assert_noop!(Deposit::migrate(RuntimeOrigin::signed(1), 1), <Error<Runtime>>::DepositInUse);

mock::set_in_use(1, false);

assert_ok!(Deposit::migrate(RuntimeOrigin::signed(1)));
assert_ok!(Deposit::migrate(RuntimeOrigin::signed(1), 1));
assert!(Deposit::deposit_of(1).is_none());
});
}
12 changes: 7 additions & 5 deletions precompile/deposit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,10 @@ pub struct Deposit<Runtime>(PhantomData<Runtime>);
impl<Runtime> Deposit<Runtime>
where
Runtime: darwinia_deposit::Config + pallet_evm::Config,
Runtime::RuntimeCall: Dispatchable<PostInfo = PostDispatchInfo> + GetDispatchInfo,
Runtime::RuntimeCall: From<darwinia_deposit::Call<Runtime>>,
Runtime::RuntimeCall: From<darwinia_deposit::Call<Runtime>>
+ Dispatchable<PostInfo = PostDispatchInfo>
+ GetDispatchInfo,
<Runtime as frame_system::Config>::AccountId: From<H160>,
<<Runtime as frame_system::Config>::RuntimeCall as Dispatchable>::RuntimeOrigin: OriginTrait,
<Runtime::RuntimeCall as Dispatchable>::RuntimeOrigin: From<Option<Runtime::AccountId>>,
AccountIdOf<Runtime>: From<H160>,
Expand Down Expand Up @@ -89,13 +91,13 @@ where
Ok(true)
}

#[precompile::public("migrate()")]
fn migrate(handle: &mut impl PrecompileHandle) -> EvmResult<bool> {
#[precompile::public("migrate(address)")]
fn migrate(handle: &mut impl PrecompileHandle, who: Address) -> EvmResult<bool> {
let origin: AccountIdOf<Runtime> = handle.context().caller.into();
RuntimeHelper::<Runtime>::try_dispatch(
handle,
Some(origin).into(),
darwinia_deposit::Call::<Runtime>::migrate {},
darwinia_deposit::Call::<Runtime>::migrate { who: H160::from(who).into() },
)?;
Ok(true)
}
Expand Down
2 changes: 1 addition & 1 deletion precompile/deposit/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ fn selectors() {
assert!(PCall::lock_selectors().contains(&0x998e4242));
assert!(PCall::claim_selectors().contains(&0x4e71d92d));
assert!(PCall::claim_with_penalty_selectors().contains(&0xfa04a9bf));
assert!(PCall::migrate_selectors().contains(&0x8fd3ab80));
assert!(PCall::migrate_selectors().contains(&0xce5494bb));
}

#[test]
Expand Down
9 changes: 7 additions & 2 deletions precompile/metadata/abi-creator.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
#!/bin/bash
#!/bin/sh

# Usage: Run `bash abi-creator.sh` in the root of the metadata folder.
# ## Usage
#
# ```sh
# brew install solidity # macOS only
# cd precompile/metadata && ./abi-generator.sh
# ```

sol_directory="sol"
abi_directory="abi"
Expand Down
2 changes: 1 addition & 1 deletion precompile/metadata/abi/asset.json
Original file line number Diff line number Diff line change
Expand Up @@ -371,5 +371,5 @@
}
}
},
"version": "0.8.17+commit.8df45f5f.Linux.g++"
"version": "0.8.25+commit.b61c2a91.Darwin.appleclang"
}
2 changes: 1 addition & 1 deletion precompile/metadata/abi/bls12-381.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,5 @@
}
}
},
"version": "0.8.17+commit.8df45f5f.Linux.g++"
"version": "0.8.25+commit.b61c2a91.Darwin.appleclang"
}
2 changes: 1 addition & 1 deletion precompile/metadata/abi/conviction-voting.json
Original file line number Diff line number Diff line change
Expand Up @@ -1021,5 +1021,5 @@
}
}
},
"version": "0.8.17+commit.8df45f5f.Linux.g++"
"version": "0.8.25+commit.b61c2a91.Darwin.appleclang"
}
15 changes: 11 additions & 4 deletions precompile/metadata/abi/deposit.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,14 @@
"type": "function"
},
{
"inputs": [],
"inputs":
[
{
"internalType": "address",
"name": "who",
"type": "address"
}
],
"name": "migrate",
"outputs":
[
Expand Down Expand Up @@ -119,7 +126,7 @@
"_0": "returns true on success, false otherwise."
}
},
"migrate()":
"migrate(address)":
{
"details": "Migrate data to deposit contract"
}
Expand All @@ -132,9 +139,9 @@
"claim()": "4e71d92d",
"claim_with_penalty(uint8)": "fa04a9bf",
"lock(uint256,uint8)": "998e4242",
"migrate()": "8fd3ab80"
"migrate(address)": "ce5494bb"
}
}
},
"version": "0.8.17+commit.8df45f5f.Linux.g++"
"version": "0.8.25+commit.b61c2a91.Darwin.appleclang"
}
2 changes: 1 addition & 1 deletion precompile/metadata/abi/staking.json
Original file line number Diff line number Diff line change
Expand Up @@ -225,5 +225,5 @@
}
}
},
"version": "0.8.17+commit.8df45f5f.Linux.g++"
"version": "0.8.25+commit.b61c2a91.Darwin.appleclang"
}
2 changes: 1 addition & 1 deletion precompile/metadata/abi/state-storage.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,5 @@
}
}
},
"version": "0.8.17+commit.8df45f5f.Linux.g++"
"version": "0.8.25+commit.b61c2a91.Darwin.appleclang"
}
2 changes: 1 addition & 1 deletion precompile/metadata/sol/deposit.sol
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,5 @@ interface Deposit {
function claim_with_penalty(uint8 depositId) external returns (bool);

/// @dev Migrate data to deposit contract
function migrate() external returns (bool);
function migrate(address who) external returns (bool);
}
14 changes: 12 additions & 2 deletions runtime/crab/src/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ fn migrate() -> frame_support::weights::Weight {
}

if let Ok(dao) =
array_bytes::hex_n_into::<_, AccountId, 20>("0x7FAcDaFB282028E4B3264fB08cd633A9142514df")
array_bytes::hex_n_into::<_, AccountId, 20>("0x08837De0Ae21C270383D9F2de4DB03c7b1314632")
{
let _ = <pallet_assets::Pallet<Runtime>>::transfer_ownership(
RuntimeOrigin::signed(ROOT),
Expand All @@ -65,7 +65,7 @@ fn migrate() -> frame_support::weights::Weight {
);

if let Ok(deposit) = array_bytes::hex_n_into::<_, AccountId, 20>(
"0x08837De0Ae21C270383D9F2de4DB03c7b1314632",
"0xDeC9cD45e921F2AedE72f694743265af37d47Fa7",
) {
let _ = <pallet_assets::Pallet<Runtime>>::set_team(
RuntimeOrigin::signed(dao),
Expand All @@ -76,6 +76,16 @@ fn migrate() -> frame_support::weights::Weight {
);
}
}
if let Ok(who) =
array_bytes::hex_n_into::<_, AccountId, 20>("0xDeC9cD45e921F2AedE72f694743265af37d47Fa7")
{
<darwinia_deposit::DepositContract<Runtime>>::put(who);
}
if let Ok(who) =
array_bytes::hex_n_into::<_, AccountId, 20>("0xb037E75fE2BFA42DdDC17BB90963Dafe10A5Dd11")
{
<darwinia_staking::RingStakingContract<Runtime>>::put(who);
}

// frame_support::weights::Weight::zero()
<Runtime as frame_system::Config>::DbWeight::get().reads_writes(7, 7)
Expand Down
14 changes: 12 additions & 2 deletions runtime/darwinia/src/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ fn migrate() -> frame_support::weights::Weight {
}

if let Ok(dao) =
array_bytes::hex_n_into::<_, AccountId, 20>("0x7FAcDaFB282028E4B3264fB08cd633A9142514df")
array_bytes::hex_n_into::<_, AccountId, 20>("0x08837De0Ae21C270383D9F2de4DB03c7b1314632")
{
let _ = <pallet_assets::Pallet<Runtime>>::transfer_ownership(
RuntimeOrigin::signed(ROOT),
Expand All @@ -73,7 +73,7 @@ fn migrate() -> frame_support::weights::Weight {
);

if let Ok(deposit) = array_bytes::hex_n_into::<_, AccountId, 20>(
"0x08837De0Ae21C270383D9F2de4DB03c7b1314632",
"0xDeC9cD45e921F2AedE72f694743265af37d47Fa7",
) {
let _ = <pallet_assets::Pallet<Runtime>>::set_team(
RuntimeOrigin::signed(dao),
Expand All @@ -84,6 +84,16 @@ fn migrate() -> frame_support::weights::Weight {
);
}
}
if let Ok(who) =
array_bytes::hex_n_into::<_, AccountId, 20>("0xDeC9cD45e921F2AedE72f694743265af37d47Fa7")
{
<darwinia_deposit::DepositContract<Runtime>>::put(who);
}
if let Ok(who) =
array_bytes::hex_n_into::<_, AccountId, 20>("0xb037E75fE2BFA42DdDC17BB90963Dafe10A5Dd11")
{
<darwinia_staking::RingStakingContract<Runtime>>::put(who);
}

// frame_support::weights::Weight::zero()
<Runtime as frame_system::Config>::DbWeight::get().reads_writes(7, 107)
Expand Down
25 changes: 1 addition & 24 deletions runtime/koi/src/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,28 +45,5 @@ impl frame_support::traits::OnRuntimeUpgrade for CustomOnRuntimeUpgrade {
}

fn migrate() -> frame_support::weights::Weight {
if let Ok(dao) =
array_bytes::hex_n_into::<_, AccountId, 20>("0x7FAcDaFB282028E4B3264fB08cd633A9142514df")
{
let _ = <pallet_assets::Pallet<Runtime>>::transfer_ownership(
RuntimeOrigin::signed(ROOT),
codec::Compact(AssetIds::KKton as AssetId),
dao,
);

if let Ok(deposit) = array_bytes::hex_n_into::<_, AccountId, 20>(
"0x08837De0Ae21C270383D9F2de4DB03c7b1314632",
) {
let _ = <pallet_assets::Pallet<Runtime>>::set_team(
RuntimeOrigin::signed(dao),
codec::Compact(AssetIds::KKton as AssetId),
deposit,
deposit,
dao,
);
}
}

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

0 comments on commit fc952a6

Please sign in to comment.