From fc952a6293e6a3f391f571306fda20189b31297d Mon Sep 17 00:00:00 2001 From: Xavier Lau Date: Tue, 3 Sep 2024 16:50:45 +0800 Subject: [PATCH] Allow performing migration for others (#1588) * Allow performing migration for others * Update contract addrs * Update abi * Update sol * Fix tests * Fix * Fix Signed-off-by: Xavier Lau --------- Signed-off-by: Xavier Lau --- pallet/deposit/src/benchmarking.rs | 3 ++- pallet/deposit/src/lib.rs | 7 +++--- pallet/deposit/src/tests.rs | 4 +-- precompile/deposit/src/lib.rs | 12 +++++---- precompile/deposit/src/tests.rs | 2 +- precompile/metadata/abi-creator.sh | 9 +++++-- precompile/metadata/abi/asset.json | 2 +- precompile/metadata/abi/bls12-381.json | 2 +- .../metadata/abi/conviction-voting.json | 2 +- precompile/metadata/abi/deposit.json | 15 ++++++++--- precompile/metadata/abi/staking.json | 2 +- precompile/metadata/abi/state-storage.json | 2 +- precompile/metadata/sol/deposit.sol | 2 +- runtime/crab/src/migration.rs | 14 +++++++++-- runtime/darwinia/src/migration.rs | 14 +++++++++-- runtime/koi/src/migration.rs | 25 +------------------ 16 files changed, 65 insertions(+), 52 deletions(-) diff --git a/pallet/deposit/src/benchmarking.rs b/pallet/deposit/src/benchmarking.rs index 6cebd619a..34fb0165b 100644 --- a/pallet/deposit/src/benchmarking.rs +++ b/pallet/deposit/src/benchmarking.rs @@ -117,6 +117,7 @@ mod benchmarks { #[benchmark] fn migrate() { let a = frame_benchmarking::whitelisted_caller::(); + let a_ = a.clone(); let max_deposits = T::MaxDeposits::get(); >::set_deposit_contract(RawOrigin::Root.into(), a.clone()).unwrap(); @@ -133,7 +134,7 @@ mod benchmarks { }); #[extrinsic_call] - _(RawOrigin::Signed(a)); + _(RawOrigin::Signed(a), a_); } #[benchmark] diff --git a/pallet/deposit/src/lib.rs b/pallet/deposit/src/lib.rs index 9278475df..006542232 100644 --- a/pallet/deposit/src/lib.rs +++ b/pallet/deposit/src/lib.rs @@ -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(::WeightInfo::migrate())] - pub fn migrate(origin: OriginFor) -> DispatchResult { - let who = ensure_signed(origin)?; + pub fn migrate(origin: OriginFor, who: T::AccountId) -> DispatchResult { + ensure_signed(origin)?; + let Some(ds) = >::take(&who) else { return Ok(()) }; let now = Self::now(); let mut ds = ds.into_iter(); diff --git a/pallet/deposit/src/tests.rs b/pallet/deposit/src/tests.rs index f6e71714c..765073e23 100644 --- a/pallet/deposit/src/tests.rs +++ b/pallet/deposit/src/tests.rs @@ -365,11 +365,11 @@ fn migrate_should_work() { mock::set_in_use(1, true); - assert_noop!(Deposit::migrate(RuntimeOrigin::signed(1)), >::DepositInUse); + assert_noop!(Deposit::migrate(RuntimeOrigin::signed(1), 1), >::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()); }); } diff --git a/precompile/deposit/src/lib.rs b/precompile/deposit/src/lib.rs index 8906e0837..f4af92b66 100644 --- a/precompile/deposit/src/lib.rs +++ b/precompile/deposit/src/lib.rs @@ -44,8 +44,10 @@ pub struct Deposit(PhantomData); impl Deposit where Runtime: darwinia_deposit::Config + pallet_evm::Config, - Runtime::RuntimeCall: Dispatchable + GetDispatchInfo, - Runtime::RuntimeCall: From>, + Runtime::RuntimeCall: From> + + Dispatchable + + GetDispatchInfo, + ::AccountId: From, <::RuntimeCall as Dispatchable>::RuntimeOrigin: OriginTrait, ::RuntimeOrigin: From>, AccountIdOf: From, @@ -89,13 +91,13 @@ where Ok(true) } - #[precompile::public("migrate()")] - fn migrate(handle: &mut impl PrecompileHandle) -> EvmResult { + #[precompile::public("migrate(address)")] + fn migrate(handle: &mut impl PrecompileHandle, who: Address) -> EvmResult { let origin: AccountIdOf = handle.context().caller.into(); RuntimeHelper::::try_dispatch( handle, Some(origin).into(), - darwinia_deposit::Call::::migrate {}, + darwinia_deposit::Call::::migrate { who: H160::from(who).into() }, )?; Ok(true) } diff --git a/precompile/deposit/src/tests.rs b/precompile/deposit/src/tests.rs index 0ee1c4dfd..2e79a2858 100644 --- a/precompile/deposit/src/tests.rs +++ b/precompile/deposit/src/tests.rs @@ -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] diff --git a/precompile/metadata/abi-creator.sh b/precompile/metadata/abi-creator.sh index 142b73587..f8ce41e25 100755 --- a/precompile/metadata/abi-creator.sh +++ b/precompile/metadata/abi-creator.sh @@ -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" diff --git a/precompile/metadata/abi/asset.json b/precompile/metadata/abi/asset.json index c5dcbb5ad..38583bb3f 100644 --- a/precompile/metadata/abi/asset.json +++ b/precompile/metadata/abi/asset.json @@ -371,5 +371,5 @@ } } }, - "version": "0.8.17+commit.8df45f5f.Linux.g++" + "version": "0.8.25+commit.b61c2a91.Darwin.appleclang" } diff --git a/precompile/metadata/abi/bls12-381.json b/precompile/metadata/abi/bls12-381.json index 7ffb88e96..c5e2f225a 100644 --- a/precompile/metadata/abi/bls12-381.json +++ b/precompile/metadata/abi/bls12-381.json @@ -66,5 +66,5 @@ } } }, - "version": "0.8.17+commit.8df45f5f.Linux.g++" + "version": "0.8.25+commit.b61c2a91.Darwin.appleclang" } diff --git a/precompile/metadata/abi/conviction-voting.json b/precompile/metadata/abi/conviction-voting.json index 8154dbee7..337c14625 100644 --- a/precompile/metadata/abi/conviction-voting.json +++ b/precompile/metadata/abi/conviction-voting.json @@ -1021,5 +1021,5 @@ } } }, - "version": "0.8.17+commit.8df45f5f.Linux.g++" + "version": "0.8.25+commit.b61c2a91.Darwin.appleclang" } diff --git a/precompile/metadata/abi/deposit.json b/precompile/metadata/abi/deposit.json index 011ed1a80..686f1b80f 100644 --- a/precompile/metadata/abi/deposit.json +++ b/precompile/metadata/abi/deposit.json @@ -67,7 +67,14 @@ "type": "function" }, { - "inputs": [], + "inputs": + [ + { + "internalType": "address", + "name": "who", + "type": "address" + } + ], "name": "migrate", "outputs": [ @@ -119,7 +126,7 @@ "_0": "returns true on success, false otherwise." } }, - "migrate()": + "migrate(address)": { "details": "Migrate data to deposit contract" } @@ -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" } diff --git a/precompile/metadata/abi/staking.json b/precompile/metadata/abi/staking.json index 47febcf44..9844573f3 100644 --- a/precompile/metadata/abi/staking.json +++ b/precompile/metadata/abi/staking.json @@ -225,5 +225,5 @@ } } }, - "version": "0.8.17+commit.8df45f5f.Linux.g++" + "version": "0.8.25+commit.b61c2a91.Darwin.appleclang" } diff --git a/precompile/metadata/abi/state-storage.json b/precompile/metadata/abi/state-storage.json index e3a59bd54..45bfc441f 100644 --- a/precompile/metadata/abi/state-storage.json +++ b/precompile/metadata/abi/state-storage.json @@ -54,5 +54,5 @@ } } }, - "version": "0.8.17+commit.8df45f5f.Linux.g++" + "version": "0.8.25+commit.b61c2a91.Darwin.appleclang" } diff --git a/precompile/metadata/sol/deposit.sol b/precompile/metadata/sol/deposit.sol index 59e44a498..1e508f992 100644 --- a/precompile/metadata/sol/deposit.sol +++ b/precompile/metadata/sol/deposit.sol @@ -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); } diff --git a/runtime/crab/src/migration.rs b/runtime/crab/src/migration.rs index 34335bb0e..1c89863ce 100644 --- a/runtime/crab/src/migration.rs +++ b/runtime/crab/src/migration.rs @@ -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 _ = >::transfer_ownership( RuntimeOrigin::signed(ROOT), @@ -65,7 +65,7 @@ fn migrate() -> frame_support::weights::Weight { ); if let Ok(deposit) = array_bytes::hex_n_into::<_, AccountId, 20>( - "0x08837De0Ae21C270383D9F2de4DB03c7b1314632", + "0xDeC9cD45e921F2AedE72f694743265af37d47Fa7", ) { let _ = >::set_team( RuntimeOrigin::signed(dao), @@ -76,6 +76,16 @@ fn migrate() -> frame_support::weights::Weight { ); } } + if let Ok(who) = + array_bytes::hex_n_into::<_, AccountId, 20>("0xDeC9cD45e921F2AedE72f694743265af37d47Fa7") + { + >::put(who); + } + if let Ok(who) = + array_bytes::hex_n_into::<_, AccountId, 20>("0xb037E75fE2BFA42DdDC17BB90963Dafe10A5Dd11") + { + >::put(who); + } // frame_support::weights::Weight::zero() ::DbWeight::get().reads_writes(7, 7) diff --git a/runtime/darwinia/src/migration.rs b/runtime/darwinia/src/migration.rs index 0bda7b7cc..640b455fa 100644 --- a/runtime/darwinia/src/migration.rs +++ b/runtime/darwinia/src/migration.rs @@ -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 _ = >::transfer_ownership( RuntimeOrigin::signed(ROOT), @@ -73,7 +73,7 @@ fn migrate() -> frame_support::weights::Weight { ); if let Ok(deposit) = array_bytes::hex_n_into::<_, AccountId, 20>( - "0x08837De0Ae21C270383D9F2de4DB03c7b1314632", + "0xDeC9cD45e921F2AedE72f694743265af37d47Fa7", ) { let _ = >::set_team( RuntimeOrigin::signed(dao), @@ -84,6 +84,16 @@ fn migrate() -> frame_support::weights::Weight { ); } } + if let Ok(who) = + array_bytes::hex_n_into::<_, AccountId, 20>("0xDeC9cD45e921F2AedE72f694743265af37d47Fa7") + { + >::put(who); + } + if let Ok(who) = + array_bytes::hex_n_into::<_, AccountId, 20>("0xb037E75fE2BFA42DdDC17BB90963Dafe10A5Dd11") + { + >::put(who); + } // frame_support::weights::Weight::zero() ::DbWeight::get().reads_writes(7, 107) diff --git a/runtime/koi/src/migration.rs b/runtime/koi/src/migration.rs index 15e9e442f..d6045cb0e 100644 --- a/runtime/koi/src/migration.rs +++ b/runtime/koi/src/migration.rs @@ -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 _ = >::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 _ = >::set_team( - RuntimeOrigin::signed(dao), - codec::Compact(AssetIds::KKton as AssetId), - deposit, - deposit, - dao, - ); - } - } - - // frame_support::weights::Weight::zero() - ::DbWeight::get().reads_writes(5, 5) + frame_support::weights::Weight::zero() }