Skip to content

Commit

Permalink
Remove the LocalAssets pallet-asset instance (#2634)
Browse files Browse the repository at this point in the history
* feat(Deprecate Mintables XC-20s): remove LocalAssets pallet instance from moonbase runtime

* feat(Remove LocalAssets): remove todo!()

* feat(Remove LocalAssets): Remove storage from LocalAssets pallet_assets instance

* feat(Remove LocalAssets): Blacklist all local xc-20s created on moonbase

* feat(Remove LocalAssets): test revert on all local xc-20 addresses

* feat(Remove LocalAssets): update migration to remove account codes of local assets

* feat(Remove LocalAssets): only run LocalAssets storage migration when upgrading runtime 2800

* feat(Remove LocalAssets): remove LocalAssets from moonriver runtime

* feat(Remove LocalAssets): remove LocalAssets from moonbeam\ runtime

* feat(Remove LocalAssets): refactor revert logic for local asset precompile set

* test(Remove LocalAssets): Local assets must be inactive in the precompile registry

* test(Remove LocalAssets): Remove local asset logic from assets-erc20 precompile

* deprecate LocalAssetReserve variant in CurrencyId enum

* Adds a new pallet for removing the LocalAssets pallet

* Support moonbeam dev tests (#2636)

* add PoV size test for LocalAssets storage removal

---------

Co-authored-by: Éloïs <c@elo.tf>
  • Loading branch information
RomarQ and librelois committed Feb 15, 2024
1 parent 7c86c43 commit 7a73691
Show file tree
Hide file tree
Showing 503 changed files with 1,531 additions and 3,681 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/build.yml
Expand Up @@ -353,8 +353,9 @@ jobs:
needs: ["set-tags", "build"]
timeout-minutes: 20
strategy:
fail-fast: false
matrix:
chain: ["moonbase"]
chain: ["moonbase", "moonbeam"]
env:
GH_WORKFLOW_MATRIX_CHAIN: ${{ matrix.chain }}
DEBUG_COLORS: 1
Expand Down
19 changes: 19 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Expand Up @@ -92,6 +92,7 @@ pallet-moonbeam-orbiters = { path = "pallets/moonbeam-orbiters", default-feature
pallet-parachain-staking = { path = "pallets/parachain-staking", default-features = false }
pallet-proxy-genesis-companion = { path = "pallets/proxy-genesis-companion", default-features = false }
pallet-xcm-transactor = { path = "pallets/xcm-transactor", default-features = false }
pallet-moonbeam-lazy-migrations = { path = "pallets/moonbeam-lazy-migrations", default-features = false }
precompile-utils = { path = "precompiles/utils", default-features = false }
xcm-primitives = { path = "primitives/xcm", default-features = false }

Expand Down
40 changes: 40 additions & 0 deletions pallets/moonbeam-lazy-migrations/Cargo.toml
@@ -0,0 +1,40 @@
[package]
name = "pallet-moonbeam-lazy-migrations"
authors = { workspace = true }
description = "A pallet for performing migrations from extrinsics"
edition = "2021"
version = "0.1.0"

[dependencies]
log = { workspace = true }

# Substrate
frame-support = { workspace = true }
frame-system = { workspace = true }
parity-scale-codec = { workspace = true }
scale-info = { workspace = true, features = ["derive"] }
sp-core = { workspace = true }
sp-io = { workspace = true }
sp-runtime = { workspace = true }

# Benchmarks
frame-benchmarking = { workspace = true, optional = true }

[dev-dependencies]
pallet-balances = { workspace = true }
frame-benchmarking = { workspace = true, features = ["std"] }
sp-io = { workspace = true, features = ["std"] }

[features]
default = ["std"]
std = [
"pallet-balances/std",
"frame-support/std",
"frame-system/std",
"scale-info/std",
"sp-core/std",
"sp-io/std",
"sp-runtime/std",
]
runtime-benchmarks = ["frame-benchmarking"]
try-runtime = ["frame-support/try-runtime"]
110 changes: 110 additions & 0 deletions pallets/moonbeam-lazy-migrations/src/lib.rs
@@ -0,0 +1,110 @@
// Copyright 2024 Moonbeam foundation
// This file is part of Moonbeam.

// Moonbeam 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.

// Moonbeam 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 Moonbeam. If not, see <http://www.gnu.org/licenses/>.

//! # Lazy Migration Pallet

#![allow(non_camel_case_types)]
#![cfg_attr(not(feature = "std"), no_std)]

#[cfg(test)]
mod mock;
#[cfg(test)]
mod tests;

use frame_support::pallet;

pub use pallet::*;

#[pallet]
pub mod pallet {
use super::*;
use frame_support::pallet_prelude::*;
use frame_system::pallet_prelude::*;

const INTERMEDIATES_NODES_SIZE: u64 = 4096;
const MAX_LOCAL_ASSETS_STORAGE_ENTRY_SIZE: u64 =
(/* biggest key on moonbeam */136) + (/* biggest value on moonbeam */142);

/// Pallet for multi block migrations
#[pallet::pallet]
pub struct Pallet<T>(PhantomData<T>);

#[pallet::storage]
/// If true, it means that LocalAssets storage has been removed.
pub(crate) type LocalAssetsMigrationCompleted<T: Config> = StorageValue<_, bool, ValueQuery>;

/// Configuration trait of this pallet.
#[pallet::config]
pub trait Config: frame_system::Config {}

#[pallet::error]
pub enum Error<T> {
/// There are no more storage entries to be removed
AllStorageEntriesHaveBeenRemoved,
}

#[pallet::call]
impl<T: Config> Pallet<T> {
// TODO(rodrigo): This extrinsic should be removed once LocalAssets pallet storage is removed
#[pallet::call_index(0)]
#[pallet::weight(Weight::from_parts(0,
INTERMEDIATES_NODES_SIZE + (MAX_LOCAL_ASSETS_STORAGE_ENTRY_SIZE * <u64>::from(*limit)))
.saturating_add(<T as frame_system::Config>::DbWeight::get()
.reads_writes((*limit + 1).into(), (*limit + 1).into()))
)]
pub fn clear_local_assets_storage(
origin: OriginFor<T>,
limit: u32,
) -> DispatchResultWithPostInfo {
ensure_signed(origin)?;
ensure!(limit != 0, "Limit cannot be zero!");

let mut weight = <T as frame_system::Config>::DbWeight::get().reads(1);
ensure!(
!LocalAssetsMigrationCompleted::<T>::get(),
Error::<T>::AllStorageEntriesHaveBeenRemoved
);

let hashed_prefix = sp_io::hashing::twox_128("LocalAssets".as_bytes());

let keys_removed = match sp_io::storage::clear_prefix(&hashed_prefix, Some(limit)) {
sp_io::KillStorageResult::AllRemoved(value) => {
LocalAssetsMigrationCompleted::<T>::set(true);
weight
.saturating_accrue(<T as frame_system::Config>::DbWeight::get().writes(1));
value
}
sp_io::KillStorageResult::SomeRemaining(value) => value,
} as u64;

log::info!("Removed {} keys 🧹", keys_removed);

Ok(Some(
weight
.saturating_add(Weight::from_parts(
0,
INTERMEDIATES_NODES_SIZE
+ MAX_LOCAL_ASSETS_STORAGE_ENTRY_SIZE * keys_removed,
))
.saturating_add(
<T as frame_system::Config>::DbWeight::get()
.reads_writes(keys_removed, keys_removed),
),
)
.into())
}
}
}
120 changes: 120 additions & 0 deletions pallets/moonbeam-lazy-migrations/src/mock.rs
@@ -0,0 +1,120 @@
// Copyright 2024 Moonbeam foundation
// This file is part of Moonbeam.

// Moonbeam 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.

// Moonbeam 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 Moonbeam. If not, see <http://www.gnu.org/licenses/>.

//! A minimal runtime including the multi block migrations pallet

use super::*;
use crate as pallet_moonbeam_lazy_migrations;
use frame_support::{
construct_runtime, parameter_types,
traits::Everything,
weights::{constants::RocksDbWeight, Weight},
};
use sp_core::H256;
use sp_runtime::{
traits::{BlakeTwo256, IdentityLookup},
BuildStorage, Perbill,
};

pub type AccountId = u64;
pub type Balance = u128;
type Block = frame_system::mocking::MockBlock<Runtime>;

// Configure a mock runtime to test the pallet.
construct_runtime!(
pub enum Runtime
{
System: frame_system::{Pallet, Call, Config<T>, Storage, Event<T>},
Balances: pallet_balances::{Pallet, Call, Storage, Config<T>, Event<T>},
LazyMigrations: pallet_moonbeam_lazy_migrations::{Pallet, Call},
}
);

parameter_types! {
pub const BlockHashCount: u32 = 250;
pub const MaximumBlockWeight: Weight = Weight::from_parts(1024, 1);
pub const MaximumBlockLength: u32 = 2 * 1024;
pub const AvailableBlockRatio: Perbill = Perbill::one();
pub const SS58Prefix: u8 = 42;
}
impl frame_system::Config for Runtime {
type BaseCallFilter = Everything;
type DbWeight = RocksDbWeight;
type RuntimeOrigin = RuntimeOrigin;
type Nonce = u64;
type Block = Block;
type RuntimeCall = RuntimeCall;
type Hash = H256;
type Hashing = BlakeTwo256;
type AccountId = AccountId;
type Lookup = IdentityLookup<Self::AccountId>;
type RuntimeEvent = RuntimeEvent;
type BlockHashCount = BlockHashCount;
type Version = ();
type PalletInfo = PalletInfo;
type AccountData = pallet_balances::AccountData<Balance>;
type OnNewAccount = ();
type OnKilledAccount = ();
type SystemWeightInfo = ();
type BlockWeights = ();
type BlockLength = ();
type SS58Prefix = SS58Prefix;
type OnSetCode = ();
type MaxConsumers = frame_support::traits::ConstU32<16>;
}

parameter_types! {
pub const ExistentialDeposit: u128 = 0;
}
impl pallet_balances::Config for Runtime {
type MaxReserves = ();
type ReserveIdentifier = ();
type MaxLocks = ();
type Balance = Balance;
type RuntimeEvent = RuntimeEvent;
type DustRemoval = ();
type ExistentialDeposit = ExistentialDeposit;
type AccountStore = System;
type WeightInfo = ();
type RuntimeHoldReason = ();
type FreezeIdentifier = ();
type MaxHolds = ();
type MaxFreezes = ();
type RuntimeFreezeReason = ();
}

impl Config for Runtime {}

/// Externality builder for pallet migration's mock runtime
pub(crate) struct ExtBuilder;

impl Default for ExtBuilder {
fn default() -> ExtBuilder {
Self
}
}

impl ExtBuilder {
pub(crate) fn build(self) -> sp_io::TestExternalities {
let storage = frame_system::GenesisConfig::<Runtime>::default()
.build_storage()
.expect("Frame system builds valid default genesis config");

let mut ext = sp_io::TestExternalities::new(storage);
ext.execute_with(|| System::set_block_number(1));
ext
}
}

0 comments on commit 7a73691

Please sign in to comment.