Skip to content

Commit

Permalink
Refactor move DAME precompile to pallet module (#637)
Browse files Browse the repository at this point in the history
* create precompile folder under pallet-asset-metadata-extender

* test works

* remove legacy folder

* remove old use of legacy folder

* fix clippy

* fmt
  • Loading branch information
magecnion committed Jun 27, 2024
1 parent 113dab7 commit 6931d75
Show file tree
Hide file tree
Showing 24 changed files with 1,504 additions and 1,844 deletions.
33 changes: 7 additions & 26 deletions Cargo.lock

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

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,6 @@ pallet-evm-evolution-collection = { path = "./precompile/evolution-collection",
pallet-evm-erc721 = { path = "./precompile/erc721", default-features = false }
pallet-asset-metadata-extender = { path = "./pallets/asset-metadata-extender", default-features = false }
pallet-evm-asset-metadata-extender = { path = "./precompile/asset-metadata-extender", default-features = false }
pallet-evm-asset-metadata-extender-legacy = { path = "./precompile/asset-metadata-extender-legacy", default-features = false }
pallet-parachain-staking = { path = "./pallets/parachain-staking", default-features = false }

# Precompiles
Expand Down
12 changes: 11 additions & 1 deletion pallets/asset-metadata-extender/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,17 @@ sp-runtime = { workspace = true }
sp-core = { workspace = true }
frame-benchmarking = { optional = true, workspace = true }
sp-std = { workspace = true }
fp-evm = { workspace = true }
precompile-utils = { workspace = true, default-features = false }
sp-io = { workspace = true }
pallet-evm = { workspace = true }

[dev-dependencies]
sp-io = { workspace = true }
sp-runtime = { workspace = true }
precompile-utils = { workspace = true, features = ["testing"] }
pallet-balances = { workspace = true, features = ["std", "insecure_zero_ed"] }
pallet-timestamp = { workspace = true }
hex = { workspace = true }

[features]
default = ["std"]
Expand All @@ -34,6 +41,9 @@ std = [
"sp-runtime/std",
"frame-benchmarking/std",
"sp-std/std",
"fp-evm/std",
"precompile-utils/std",
"pallet-evm/std",
]

runtime-benchmarks = [
Expand Down
5 changes: 5 additions & 0 deletions pallets/asset-metadata-extender/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@
#![cfg_attr(not(feature = "std"), no_std)]

mod benchmarking;
pub mod precompiles;
pub mod traits;
pub mod types;
pub mod weights;

use frame_support::pallet_prelude::*;
pub use pallet::*;
use pallet_evm::GasWeightMapping;
use sp_core::H160;
use sp_runtime::{
traits::{Convert, One},
Expand Down Expand Up @@ -59,6 +61,9 @@ pub mod pallet {

/// Converts `Self::AccountId` to `H160`
type AccountIdToH160: Convert<Self::AccountId, H160>;

/// Gas weight mapping
type GasWeightMapping: GasWeightMapping;
}

/// Extensions counter for a given location
Expand Down
13 changes: 12 additions & 1 deletion pallets/asset-metadata-extender/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// along with LAOS. If not, see <http://www.gnu.org/licenses/>.

use crate as pallet_asset_metadata_extender;
use frame_support::{derive_impl, parameter_types};
use frame_support::{derive_impl, pallet_prelude::Weight, parameter_types};
use sp_core::H160;
use sp_runtime::{traits::IdentityLookup, BuildStorage};

Expand Down Expand Up @@ -49,6 +49,7 @@ impl pallet_asset_metadata_extender::Config for Test {
type MaxUniversalLocationLength = MaxUniversalLocationLength;
type MaxTokenUriLength = MaxTokenUriLength;
type AccountIdToH160 = AccountIdToH160;
type GasWeightMapping = MockGasWeightMapping;
}

pub struct AccountIdToH160;
Expand All @@ -59,6 +60,16 @@ impl sp_runtime::traits::Convert<AccountId, H160> for AccountIdToH160 {
}
}

pub struct MockGasWeightMapping;
impl pallet_evm::GasWeightMapping for MockGasWeightMapping {
fn gas_to_weight(gas: u64, _without_base_weight: bool) -> Weight {
Weight::from_parts(gas, 0)
}
fn weight_to_gas(weight: Weight) -> u64 {
weight.ref_time()
}
}

// Build genesis storage according to the mock runtime.
pub fn new_test_ext() -> sp_io::TestExternalities {
frame_system::GenesisConfig::<Test>::default().build_storage().unwrap().into()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@
// along with LAOS. If not, see <http://www.gnu.org/licenses/>.

use core::str::FromStr;
use fp_evm::{Precompile, PrecompileHandle};
use frame_support::{
derive_impl, parameter_types, traits::FindAuthor, weights::constants::RocksDbWeight,
};
use precompile_utils::precompile_set::{AddressU64, PrecompileAt, PrecompileSetBuilder};
use sp_core::{H160, U256};
use sp_runtime::{traits::IdentityLookup, BuildStorage, ConsensusEngineId};

use crate::AssetMetadataExtenderPrecompile;
use super::{AssetMetadataExtenderPrecompile, AssetMetadataExtenderPrecompileCall};
use crate as pallet_asset_metadata_extender;

// Configure a mock runtime to test the pallet.
frame_support::construct_runtime!(
Expand Down Expand Up @@ -69,6 +70,7 @@ impl pallet_asset_metadata_extender::Config for Test {
type MaxUniversalLocationLength = MaxUniversalLocationLength;
type MaxTokenUriLength = MaxTokenUriLength;
type AccountIdToH160 = AccountIdToH160;
type GasWeightMapping = pallet_evm::FixedGasWeightMapping<Self>;
}

pub struct AccountIdToH160;
Expand All @@ -95,8 +97,8 @@ impl pallet_evm::Config for Test {
type AddressMapping = pallet_evm::IdentityAddressMapping;
type Currency = Balances;
type RuntimeEvent = RuntimeEvent;
type PrecompilesType = MockPrecompileSet<Self>;
type PrecompilesValue = MockPrecompiles;
type PrecompilesType = LaosPrecompiles<Self>;
type PrecompilesValue = PrecompilesInstance;
type ChainId = ();
type BlockGasLimit = BlockGasLimit;
type Runner = pallet_evm::runner::stack::Runner<Self>;
Expand All @@ -111,12 +113,19 @@ impl pallet_evm::Config for Test {
pub const BLOCK_GAS_LIMIT: u64 = 15_000_000;
pub const MAX_POV_SIZE: u64 = 5 * 1024 * 1024;

pub type PrecompileCall = AssetMetadataExtenderPrecompileCall<Test>;

pub type LaosPrecompiles<Test> = PrecompileSetBuilder<
Test,
(PrecompileAt<AddressU64<1>, AssetMetadataExtenderPrecompile<Test>>,),
>;

frame_support::parameter_types! {
pub BlockGasLimit: U256 = U256::from(crate::mock::BLOCK_GAS_LIMIT);
pub const GasLimitPovSizeRatio: u64 = crate::mock::BLOCK_GAS_LIMIT.saturating_div(crate::mock::MAX_POV_SIZE);
pub BlockGasLimit: U256 = U256::from(BLOCK_GAS_LIMIT);
pub const GasLimitPovSizeRatio: u64 = BLOCK_GAS_LIMIT.saturating_div(MAX_POV_SIZE);
/// 1 weight to 1 gas, for testing purposes
pub WeightPerGas: frame_support::weights::Weight = frame_support::weights::Weight::from_parts(1, 0);
pub MockPrecompiles: MockPrecompileSet<Test> = MockPrecompileSet::<_>::new();
pub PrecompilesInstance: LaosPrecompiles<Test> = LaosPrecompiles::new();
}

pub struct FindAuthorTruncated;
Expand All @@ -129,33 +138,6 @@ impl FindAuthor<H160> for FindAuthorTruncated {
}
}

#[derive(Default)]
pub struct MockPrecompileSet<Test>(sp_std::marker::PhantomData<Test>);

pub type MockAssetMetadataExtenderPrecompile = AssetMetadataExtenderPrecompile<Test>;

impl<Test> MockPrecompileSet<Test>
where
Test: pallet_evm::Config,
{
pub fn new() -> Self {
Self(Default::default())
}
}

impl<Test> fp_evm::PrecompileSet for MockPrecompileSet<Test>
where
Test: pallet_evm::Config + pallet_asset_metadata_extender::Config,
{
fn execute(&self, handle: &mut impl PrecompileHandle) -> Option<fp_evm::PrecompileResult> {
Some(MockAssetMetadataExtenderPrecompile::execute(handle))
}

fn is_precompile(&self, _address: H160, _gas: u64) -> fp_evm::IsPrecompileResult {
fp_evm::IsPrecompileResult::Answer { is_precompile: true, extra_cost: 0 }
}
}

// Pallet Timestamp
parameter_types! {
pub const MinimumPeriod: u64 = 1000;
Expand Down
Loading

0 comments on commit 6931d75

Please sign in to comment.