diff --git a/Cargo.lock b/Cargo.lock index 9b0317ed94..a123c86280 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3389,7 +3389,7 @@ dependencies = [ [[package]] name = "fc-api" version = "1.0.0-dev" -source = "git+https://github.com/moonbeam-foundation/frontier?branch=moonbeam-polkadot-v1.11.0#9a91f3d2369c3a9380ef53bdf26fab2679069e52" +source = "git+https://github.com/moonbeam-foundation/frontier?branch=moonbeam-polkadot-v1.11.0#9a74ea7ae476886a6e840196e5603c411444c31c" dependencies = [ "async-trait", "fp-storage", @@ -3401,7 +3401,7 @@ dependencies = [ [[package]] name = "fc-consensus" version = "2.0.0-dev" -source = "git+https://github.com/moonbeam-foundation/frontier?branch=moonbeam-polkadot-v1.11.0#9a91f3d2369c3a9380ef53bdf26fab2679069e52" +source = "git+https://github.com/moonbeam-foundation/frontier?branch=moonbeam-polkadot-v1.11.0#9a74ea7ae476886a6e840196e5603c411444c31c" dependencies = [ "async-trait", "fp-consensus", @@ -3417,7 +3417,7 @@ dependencies = [ [[package]] name = "fc-db" version = "2.0.0-dev" -source = "git+https://github.com/moonbeam-foundation/frontier?branch=moonbeam-polkadot-v1.11.0#9a91f3d2369c3a9380ef53bdf26fab2679069e52" +source = "git+https://github.com/moonbeam-foundation/frontier?branch=moonbeam-polkadot-v1.11.0#9a74ea7ae476886a6e840196e5603c411444c31c" dependencies = [ "async-trait", "ethereum", @@ -3447,7 +3447,7 @@ dependencies = [ [[package]] name = "fc-mapping-sync" version = "2.0.0-dev" -source = "git+https://github.com/moonbeam-foundation/frontier?branch=moonbeam-polkadot-v1.11.0#9a91f3d2369c3a9380ef53bdf26fab2679069e52" +source = "git+https://github.com/moonbeam-foundation/frontier?branch=moonbeam-polkadot-v1.11.0#9a74ea7ae476886a6e840196e5603c411444c31c" dependencies = [ "fc-db", "fc-storage", @@ -3470,7 +3470,7 @@ dependencies = [ [[package]] name = "fc-rpc" version = "2.0.0-dev" -source = "git+https://github.com/moonbeam-foundation/frontier?branch=moonbeam-polkadot-v1.11.0#9a91f3d2369c3a9380ef53bdf26fab2679069e52" +source = "git+https://github.com/moonbeam-foundation/frontier?branch=moonbeam-polkadot-v1.11.0#9a74ea7ae476886a6e840196e5603c411444c31c" dependencies = [ "ethereum", "ethereum-types", @@ -3524,7 +3524,7 @@ dependencies = [ [[package]] name = "fc-rpc-core" version = "1.1.0-dev" -source = "git+https://github.com/moonbeam-foundation/frontier?branch=moonbeam-polkadot-v1.11.0#9a91f3d2369c3a9380ef53bdf26fab2679069e52" +source = "git+https://github.com/moonbeam-foundation/frontier?branch=moonbeam-polkadot-v1.11.0#9a74ea7ae476886a6e840196e5603c411444c31c" dependencies = [ "ethereum", "ethereum-types", @@ -3539,7 +3539,7 @@ dependencies = [ [[package]] name = "fc-storage" version = "1.0.0-dev" -source = "git+https://github.com/moonbeam-foundation/frontier?branch=moonbeam-polkadot-v1.11.0#9a91f3d2369c3a9380ef53bdf26fab2679069e52" +source = "git+https://github.com/moonbeam-foundation/frontier?branch=moonbeam-polkadot-v1.11.0#9a74ea7ae476886a6e840196e5603c411444c31c" dependencies = [ "ethereum", "ethereum-types", @@ -3733,7 +3733,7 @@ dependencies = [ [[package]] name = "fp-account" version = "1.0.0-dev" -source = "git+https://github.com/moonbeam-foundation/frontier?branch=moonbeam-polkadot-v1.11.0#9a91f3d2369c3a9380ef53bdf26fab2679069e52" +source = "git+https://github.com/moonbeam-foundation/frontier?branch=moonbeam-polkadot-v1.11.0#9a74ea7ae476886a6e840196e5603c411444c31c" dependencies = [ "hex", "impl-serde 0.4.0", @@ -3751,7 +3751,7 @@ dependencies = [ [[package]] name = "fp-consensus" version = "2.0.0-dev" -source = "git+https://github.com/moonbeam-foundation/frontier?branch=moonbeam-polkadot-v1.11.0#9a91f3d2369c3a9380ef53bdf26fab2679069e52" +source = "git+https://github.com/moonbeam-foundation/frontier?branch=moonbeam-polkadot-v1.11.0#9a74ea7ae476886a6e840196e5603c411444c31c" dependencies = [ "ethereum", "parity-scale-codec", @@ -3762,7 +3762,7 @@ dependencies = [ [[package]] name = "fp-ethereum" version = "1.0.0-dev" -source = "git+https://github.com/moonbeam-foundation/frontier?branch=moonbeam-polkadot-v1.11.0#9a91f3d2369c3a9380ef53bdf26fab2679069e52" +source = "git+https://github.com/moonbeam-foundation/frontier?branch=moonbeam-polkadot-v1.11.0#9a74ea7ae476886a6e840196e5603c411444c31c" dependencies = [ "ethereum", "ethereum-types", @@ -3774,7 +3774,7 @@ dependencies = [ [[package]] name = "fp-evm" version = "3.0.0-dev" -source = "git+https://github.com/moonbeam-foundation/frontier?branch=moonbeam-polkadot-v1.11.0#9a91f3d2369c3a9380ef53bdf26fab2679069e52" +source = "git+https://github.com/moonbeam-foundation/frontier?branch=moonbeam-polkadot-v1.11.0#9a74ea7ae476886a6e840196e5603c411444c31c" dependencies = [ "evm", "frame-support", @@ -3789,7 +3789,7 @@ dependencies = [ [[package]] name = "fp-rpc" version = "3.0.0-dev" -source = "git+https://github.com/moonbeam-foundation/frontier?branch=moonbeam-polkadot-v1.11.0#9a91f3d2369c3a9380ef53bdf26fab2679069e52" +source = "git+https://github.com/moonbeam-foundation/frontier?branch=moonbeam-polkadot-v1.11.0#9a74ea7ae476886a6e840196e5603c411444c31c" dependencies = [ "ethereum", "ethereum-types", @@ -3805,7 +3805,7 @@ dependencies = [ [[package]] name = "fp-self-contained" version = "1.0.0-dev" -source = "git+https://github.com/moonbeam-foundation/frontier?branch=moonbeam-polkadot-v1.11.0#9a91f3d2369c3a9380ef53bdf26fab2679069e52" +source = "git+https://github.com/moonbeam-foundation/frontier?branch=moonbeam-polkadot-v1.11.0#9a74ea7ae476886a6e840196e5603c411444c31c" dependencies = [ "frame-support", "parity-scale-codec", @@ -3817,7 +3817,7 @@ dependencies = [ [[package]] name = "fp-storage" version = "2.0.0" -source = "git+https://github.com/moonbeam-foundation/frontier?branch=moonbeam-polkadot-v1.11.0#9a91f3d2369c3a9380ef53bdf26fab2679069e52" +source = "git+https://github.com/moonbeam-foundation/frontier?branch=moonbeam-polkadot-v1.11.0#9a74ea7ae476886a6e840196e5603c411444c31c" dependencies = [ "parity-scale-codec", "serde", @@ -8740,7 +8740,7 @@ dependencies = [ [[package]] name = "pallet-ethereum" version = "4.0.0-dev" -source = "git+https://github.com/moonbeam-foundation/frontier?branch=moonbeam-polkadot-v1.11.0#9a91f3d2369c3a9380ef53bdf26fab2679069e52" +source = "git+https://github.com/moonbeam-foundation/frontier?branch=moonbeam-polkadot-v1.11.0#9a74ea7ae476886a6e840196e5603c411444c31c" dependencies = [ "environmental", "ethereum", @@ -8796,7 +8796,7 @@ dependencies = [ [[package]] name = "pallet-evm" version = "6.0.0-dev" -source = "git+https://github.com/moonbeam-foundation/frontier?branch=moonbeam-polkadot-v1.11.0#9a91f3d2369c3a9380ef53bdf26fab2679069e52" +source = "git+https://github.com/moonbeam-foundation/frontier?branch=moonbeam-polkadot-v1.11.0#9a74ea7ae476886a6e840196e5603c411444c31c" dependencies = [ "environmental", "evm", @@ -8821,7 +8821,7 @@ dependencies = [ [[package]] name = "pallet-evm-chain-id" version = "1.0.0-dev" -source = "git+https://github.com/moonbeam-foundation/frontier?branch=moonbeam-polkadot-v1.11.0#9a91f3d2369c3a9380ef53bdf26fab2679069e52" +source = "git+https://github.com/moonbeam-foundation/frontier?branch=moonbeam-polkadot-v1.11.0#9a74ea7ae476886a6e840196e5603c411444c31c" dependencies = [ "frame-support", "frame-system", @@ -8916,7 +8916,7 @@ dependencies = [ [[package]] name = "pallet-evm-precompile-blake2" version = "2.0.0-dev" -source = "git+https://github.com/moonbeam-foundation/frontier?branch=moonbeam-polkadot-v1.11.0#9a91f3d2369c3a9380ef53bdf26fab2679069e52" +source = "git+https://github.com/moonbeam-foundation/frontier?branch=moonbeam-polkadot-v1.11.0#9a74ea7ae476886a6e840196e5603c411444c31c" dependencies = [ "fp-evm", ] @@ -8924,7 +8924,7 @@ dependencies = [ [[package]] name = "pallet-evm-precompile-bn128" version = "2.0.0-dev" -source = "git+https://github.com/moonbeam-foundation/frontier?branch=moonbeam-polkadot-v1.11.0#9a91f3d2369c3a9380ef53bdf26fab2679069e52" +source = "git+https://github.com/moonbeam-foundation/frontier?branch=moonbeam-polkadot-v1.11.0#9a74ea7ae476886a6e840196e5603c411444c31c" dependencies = [ "fp-evm", "sp-core", @@ -9056,7 +9056,7 @@ dependencies = [ [[package]] name = "pallet-evm-precompile-dispatch" version = "2.0.0-dev" -source = "git+https://github.com/moonbeam-foundation/frontier?branch=moonbeam-polkadot-v1.11.0#9a91f3d2369c3a9380ef53bdf26fab2679069e52" +source = "git+https://github.com/moonbeam-foundation/frontier?branch=moonbeam-polkadot-v1.11.0#9a74ea7ae476886a6e840196e5603c411444c31c" dependencies = [ "fp-evm", "frame-support", @@ -9133,7 +9133,7 @@ dependencies = [ [[package]] name = "pallet-evm-precompile-modexp" version = "2.0.0-dev" -source = "git+https://github.com/moonbeam-foundation/frontier?branch=moonbeam-polkadot-v1.11.0#9a91f3d2369c3a9380ef53bdf26fab2679069e52" +source = "git+https://github.com/moonbeam-foundation/frontier?branch=moonbeam-polkadot-v1.11.0#9a74ea7ae476886a6e840196e5603c411444c31c" dependencies = [ "fp-evm", "num", @@ -9379,7 +9379,7 @@ dependencies = [ [[package]] name = "pallet-evm-precompile-sha3fips" version = "2.0.0-dev" -source = "git+https://github.com/moonbeam-foundation/frontier?branch=moonbeam-polkadot-v1.11.0#9a91f3d2369c3a9380ef53bdf26fab2679069e52" +source = "git+https://github.com/moonbeam-foundation/frontier?branch=moonbeam-polkadot-v1.11.0#9a74ea7ae476886a6e840196e5603c411444c31c" dependencies = [ "fp-evm", "tiny-keccak", @@ -9388,7 +9388,7 @@ dependencies = [ [[package]] name = "pallet-evm-precompile-simple" version = "2.0.0-dev" -source = "git+https://github.com/moonbeam-foundation/frontier?branch=moonbeam-polkadot-v1.11.0#9a91f3d2369c3a9380ef53bdf26fab2679069e52" +source = "git+https://github.com/moonbeam-foundation/frontier?branch=moonbeam-polkadot-v1.11.0#9a74ea7ae476886a6e840196e5603c411444c31c" dependencies = [ "fp-evm", "ripemd", @@ -9398,7 +9398,7 @@ dependencies = [ [[package]] name = "pallet-evm-precompile-storage-cleaner" version = "0.1.0" -source = "git+https://github.com/moonbeam-foundation/frontier?branch=moonbeam-polkadot-v1.11.0#9a91f3d2369c3a9380ef53bdf26fab2679069e52" +source = "git+https://github.com/moonbeam-foundation/frontier?branch=moonbeam-polkadot-v1.11.0#9a74ea7ae476886a6e840196e5603c411444c31c" dependencies = [ "fp-evm", "frame-support", @@ -9786,7 +9786,6 @@ dependencies = [ "frame-support", "frame-system", "log", - "pallet-assets", "pallet-balances", "pallet-evm", "pallet-scheduler", @@ -12159,7 +12158,7 @@ dependencies = [ [[package]] name = "precompile-utils" version = "0.1.0" -source = "git+https://github.com/moonbeam-foundation/frontier?branch=moonbeam-polkadot-v1.11.0#9a91f3d2369c3a9380ef53bdf26fab2679069e52" +source = "git+https://github.com/moonbeam-foundation/frontier?branch=moonbeam-polkadot-v1.11.0#9a74ea7ae476886a6e840196e5603c411444c31c" dependencies = [ "derive_more", "environmental", @@ -12188,7 +12187,7 @@ dependencies = [ [[package]] name = "precompile-utils-macro" version = "0.1.0" -source = "git+https://github.com/moonbeam-foundation/frontier?branch=moonbeam-polkadot-v1.11.0#9a91f3d2369c3a9380ef53bdf26fab2679069e52" +source = "git+https://github.com/moonbeam-foundation/frontier?branch=moonbeam-polkadot-v1.11.0#9a74ea7ae476886a6e840196e5603c411444c31c" dependencies = [ "case", "num_enum 0.7.2", diff --git a/pallets/moonbeam-lazy-migrations/Cargo.toml b/pallets/moonbeam-lazy-migrations/Cargo.toml index fd33c9adb7..cefc23f304 100644 --- a/pallets/moonbeam-lazy-migrations/Cargo.toml +++ b/pallets/moonbeam-lazy-migrations/Cargo.toml @@ -12,7 +12,6 @@ log = {workspace = true} frame-support = {workspace = true} frame-system = {workspace = true} pallet-scheduler = {workspace = true} -pallet-assets = {workspace = true} pallet-balances = {workspace = true} parity-scale-codec = {workspace = true} scale-info = {workspace = true, features = ["derive"]} diff --git a/pallets/moonbeam-lazy-migrations/src/lib.rs b/pallets/moonbeam-lazy-migrations/src/lib.rs index 0258b3a6d0..2114608c97 100644 --- a/pallets/moonbeam-lazy-migrations/src/lib.rs +++ b/pallets/moonbeam-lazy-migrations/src/lib.rs @@ -33,6 +33,8 @@ use frame_support::pallet; pub use pallet::*; +const MAX_CONTRACT_CODE_SIZE: u64 = 25 * 1024; + #[pallet] pub mod pallet { use super::*; @@ -65,6 +67,10 @@ pub mod pallet { AddressesLengthCannotBeZero, /// The contract is not corrupted (Still exist or properly suicided) ContractNotCorrupted, + /// The contract already have metadata + ContractMetadataAlreadySet, + /// Contract not exist + ContractNotExist, } #[pallet::call] @@ -124,5 +130,55 @@ pub mod pallet { } Ok(Pays::No.into()) } + #[pallet::call_index(2)] + #[pallet::weight(Pallet::::create_contract_metadata_weight(MAX_CONTRACT_CODE_SIZE))] + pub fn create_contract_metadata( + origin: OriginFor, + address: H160, + ) -> DispatchResultWithPostInfo { + ensure_signed(origin)?; + + ensure!( + pallet_evm::AccountCodesMetadata::::get(address).is_none(), + Error::::ContractMetadataAlreadySet + ); + + // Ensure contract exist + let code = pallet_evm::AccountCodes::::get(address); + ensure!(!code.is_empty(), Error::::ContractNotExist); + + // Construct metadata + let code_size = code.len() as u64; + let code_hash = sp_core::H256::from(sp_io::hashing::keccak_256(&code)); + let meta = pallet_evm::CodeMetadata { + size: code_size, + hash: code_hash, + }; + + // Set metadata + pallet_evm::AccountCodesMetadata::::insert(address, meta); + + Ok(( + Some(Self::create_contract_metadata_weight(code_size)), + Pays::No, + ) + .into()) + } + } + + impl Pallet { + fn create_contract_metadata_weight(code_size: u64) -> Weight { + // max entry size of AccountCodesMetadata (full key + value) + const PROOF_SIZE_CODE_METADATA: u64 = 100; + // intermediates nodes might be up to 3Kb + const PROOF_SIZE_INTERMEDIATES_NODES: u64 = 3 * 1024; + + // Account for 2 reads, 1 write + ::DbWeight::get() + .reads_writes(2, 1) + .set_proof_size( + code_size + (PROOF_SIZE_INTERMEDIATES_NODES * 2) + PROOF_SIZE_CODE_METADATA, + ) + } } } diff --git a/pallets/moonbeam-lazy-migrations/src/tests.rs b/pallets/moonbeam-lazy-migrations/src/tests.rs index a0e1257ea4..6d135c9a88 100644 --- a/pallets/moonbeam-lazy-migrations/src/tests.rs +++ b/pallets/moonbeam-lazy-migrations/src/tests.rs @@ -20,7 +20,7 @@ use { mock::{ExtBuilder, LazyMigrations, RuntimeOrigin, Test}, Error, }, - frame_support::assert_noop, + frame_support::{assert_noop, assert_ok}, rlp::RlpStream, sp_core::{H160, H256}, sp_io::hashing::keccak_256, @@ -64,6 +64,13 @@ fn mock_contract_with_entries(seed: u8, nonce: u64, num_entries: u32) -> H160 { contract_address } +fn create_dummy_contract_without_metadata(seed: u8) -> H160 { + let address = address_build(seed); + let dummy_code = vec![1, 2, 3]; + pallet_evm::AccountCodes::::insert(address, dummy_code); + address +} + #[test] fn test_clear_suicided_contract_succesfull() { ExtBuilder::default().build().execute_with(|| { @@ -263,3 +270,40 @@ fn test_clear_suicided_mixed_suicided_and_non_suicided() { ); }) } + +#[test] +fn test_create_contract_metadata_contract_not_exist() { + ExtBuilder::default().build().execute_with(|| { + assert_noop!( + LazyMigrations::create_contract_metadata( + RuntimeOrigin::signed(AccountId32::from([45; 32])), + address_build(1), + ), + Error::::ContractNotExist + ); + }); +} + +#[test] +fn test_create_contract_metadata_success_path() { + ExtBuilder::default().build().execute_with(|| { + // Setup: create a dummy contract + let address = create_dummy_contract_without_metadata(1); + + assert_ok!(LazyMigrations::create_contract_metadata( + RuntimeOrigin::signed(AccountId32::from([45; 32])), + address, + )); + + assert!(pallet_evm::AccountCodesMetadata::::get(address).is_some()); + + // Should not be able to set metadata again + assert_noop!( + LazyMigrations::create_contract_metadata( + RuntimeOrigin::signed(AccountId32::from([45; 32])), + address, + ), + Error::::ContractMetadataAlreadySet + ); + }); +}