From 3aac42d2cab44b7ca7a8f26f5737436fd7e8035f Mon Sep 17 00:00:00 2001 From: Alejandro Martinez Andres <11448715+al3mart@users.noreply.github.com> Date: Sat, 9 Mar 2024 12:05:19 +0100 Subject: [PATCH 1/5] fix(pop-api): read_state uses RelayChainDataProvider --- runtime/src/extensions.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/runtime/src/extensions.rs b/runtime/src/extensions.rs index 84f359c1..8b6a3734 100644 --- a/runtime/src/extensions.rs +++ b/runtime/src/extensions.rs @@ -14,6 +14,8 @@ use pop_api_primitives::{ use sp_core::crypto::UncheckedFrom; use sp_runtime::{traits::Dispatchable, DispatchError}; use sp_std::vec::Vec; +use cumulus_pallet_parachain_system::RelaychainDataProvider; +use sp_runtime::traits::BlockNumberProvider; const LOG_TARGET: &str = "pop-api::extension"; @@ -172,8 +174,7 @@ where RuntimeStateKeys::ParachainSystem(key) => match key { ParachainSystemKeys::LastRelayChainBlockNumber => { env.charge_weight(T::DbWeight::get().reads(1_u64))?; - let relay_block_num: BlockNumber = - cumulus_pallet_parachain_system::Pallet::::last_relay_block_number(); + let relay_block_num: BlockNumber = RelaychainDataProvider::::current_block_number(); log::debug!( target:LOG_TARGET, "{} last relay chain block number is: {:?}.", LOG_PREFIX, relay_block_num @@ -520,7 +521,7 @@ mod tests { new_test_ext().execute_with(|| { let _ = env_logger::try_init(); - let (wasm_binary, _) = load_wasm_module::("../contracts/pop-api-examples/read-runtime-state/target/ink/pop_api_extension_demo.wasm").unwrap(); + let (wasm_binary, _) = load_wasm_module::("../contracts/pop-api-examples/read-runtime-state/target/ink/pop_api_read_state_example.wasm").unwrap(); let init_value = 100; From 50d09a1062717a29dfac68bed8e0f82d6d7c67c9 Mon Sep 17 00:00:00 2001 From: Alejandro Martinez Andres <11448715+al3mart@users.noreply.github.com> Date: Mon, 11 Mar 2024 12:37:40 +0100 Subject: [PATCH 2/5] feat(pop-api): cleaner read state ext --- .../read-runtime-state/lib.rs | 2 +- pop-api/primitives/src/storage_keys.rs | 1 + runtime/src/extensions.rs | 29 +++++++++++-------- 3 files changed, 19 insertions(+), 13 deletions(-) diff --git a/contracts/pop-api-examples/read-runtime-state/lib.rs b/contracts/pop-api-examples/read-runtime-state/lib.rs index 9fc5e91b..8d0004f9 100755 --- a/contracts/pop-api-examples/read-runtime-state/lib.rs +++ b/contracts/pop-api-examples/read-runtime-state/lib.rs @@ -26,7 +26,7 @@ mod pop_api_extension_demo { pub fn read_relay_block_number(&self) { let result = pop_api::state::read::(ParachainSystem(LastRelayChainBlockNumber)); - ink::env::debug_println!("{:?}", result); + ink::env::debug_println!("Last relay block number read by contract: {:?}", result); self.env().emit_event(RelayBlockNumberRead { value: result.expect("Failed to read relay block number."), }); diff --git a/pop-api/primitives/src/storage_keys.rs b/pop-api/primitives/src/storage_keys.rs index dbb2b3c0..2bcb41ec 100644 --- a/pop-api/primitives/src/storage_keys.rs +++ b/pop-api/primitives/src/storage_keys.rs @@ -9,6 +9,7 @@ pub enum RuntimeStateKeys { #[derive(Encode, Decode, Debug, MaxEncodedLen)] pub enum ParachainSystemKeys { + /// Get the last relay chain block number seen by the parachain. LastRelayChainBlockNumber, } diff --git a/runtime/src/extensions.rs b/runtime/src/extensions.rs index 8b6a3734..551dc358 100644 --- a/runtime/src/extensions.rs +++ b/runtime/src/extensions.rs @@ -1,4 +1,3 @@ -use cumulus_primitives_core::relay_chain::BlockNumber; use frame_support::{ dispatch::{GetDispatchInfo, PostDispatchInfo, RawOrigin}, pallet_prelude::*, @@ -171,17 +170,7 @@ where let result = match key { RuntimeStateKeys::Nfts(key) => read_nfts_state::(key, &mut env), - RuntimeStateKeys::ParachainSystem(key) => match key { - ParachainSystemKeys::LastRelayChainBlockNumber => { - env.charge_weight(T::DbWeight::get().reads(1_u64))?; - let relay_block_num: BlockNumber = RelaychainDataProvider::::current_block_number(); - log::debug!( - target:LOG_TARGET, - "{} last relay chain block number is: {:?}.", LOG_PREFIX, relay_block_num - ); - Ok(relay_block_num.encode()) - }, - }, + RuntimeStateKeys::ParachainSystem(key) => read_parachain_system_state::(key, &mut env), }? .encode(); @@ -195,6 +184,22 @@ where }) } +fn read_parachain_system_state( + key: ParachainSystemKeys, + env: &mut Environment, +) -> Result, DispatchError> +where + T: pallet_contracts::Config + cumulus_pallet_parachain_system::Config, + E: Ext, +{ + match key { + ParachainSystemKeys::LastRelayChainBlockNumber => { + env.charge_weight(T::DbWeight::get().reads(1_u64))?; + Ok(RelaychainDataProvider::::current_block_number().encode()) + }, + } +} + fn read_nfts_state( key: NftsKeys, env: &mut Environment, From 4efbc3fa35ce19b4c9e0e5b130db3c0766692cc4 Mon Sep 17 00:00:00 2001 From: Alejandro Martinez Andres <11448715+al3mart@users.noreply.github.com> Date: Mon, 11 Mar 2024 12:40:15 +0100 Subject: [PATCH 3/5] chore: fmt --- pop-api/src/v0/nfts.rs | 4 ++-- runtime/src/extensions.rs | 14 +++++++++----- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/pop-api/src/v0/nfts.rs b/pop-api/src/v0/nfts.rs index f4e0d4e4..93f0a0b2 100644 --- a/pop-api/src/v0/nfts.rs +++ b/pop-api/src/v0/nfts.rs @@ -756,8 +756,8 @@ mod types { pub owner: AccountId, /// The approved transferrer of this item, if one is set. pub approvals: BoundedBTreeMap, ApprovalsLimit>, - /// The amount held in the pallet's default account for this item. Free-hold items will have - /// this as zero. + /// The amount held in the pallet's default account for this item. Free-hold items will + /// have this as zero. pub deposit: Balance, } diff --git a/runtime/src/extensions.rs b/runtime/src/extensions.rs index 551dc358..1f228c39 100644 --- a/runtime/src/extensions.rs +++ b/runtime/src/extensions.rs @@ -1,3 +1,4 @@ +use cumulus_pallet_parachain_system::RelaychainDataProvider; use frame_support::{ dispatch::{GetDispatchInfo, PostDispatchInfo, RawOrigin}, pallet_prelude::*, @@ -11,10 +12,11 @@ use pop_api_primitives::{ CollectionId, ItemId, }; use sp_core::crypto::UncheckedFrom; -use sp_runtime::{traits::Dispatchable, DispatchError}; +use sp_runtime::{ + traits::{BlockNumberProvider, Dispatchable}, + DispatchError, +}; use sp_std::vec::Vec; -use cumulus_pallet_parachain_system::RelaychainDataProvider; -use sp_runtime::traits::BlockNumberProvider; const LOG_TARGET: &str = "pop-api::extension"; @@ -46,7 +48,8 @@ where match dispatch::(env) { Ok(()) => Ok(RetVal::Converging(0)), Err(DispatchError::Module(error)) => { - // encode status code = pallet index in runtime + error index, allowing for 999 errors + // encode status code = pallet index in runtime + error index, allowing for + // 999 errors Ok(RetVal::Converging( (error.index as u32 * 1_000) + u32::from_le_bytes(error.error), )) @@ -170,7 +173,8 @@ where let result = match key { RuntimeStateKeys::Nfts(key) => read_nfts_state::(key, &mut env), - RuntimeStateKeys::ParachainSystem(key) => read_parachain_system_state::(key, &mut env), + RuntimeStateKeys::ParachainSystem(key) => + read_parachain_system_state::(key, &mut env), }? .encode(); From ef67da4bd4783cf18859aa45c175e37701a3011a Mon Sep 17 00:00:00 2001 From: Alejandro Martinez Andres <11448715+al3mart@users.noreply.github.com> Date: Mon, 11 Mar 2024 17:55:25 +0100 Subject: [PATCH 4/5] chrome: fmt --- integration-tests/src/lib.rs | 9 +++++---- runtime/src/xcm_config.rs | 7 +++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/integration-tests/src/lib.rs b/integration-tests/src/lib.rs index ac607a3f..2a9e9793 100644 --- a/integration-tests/src/lib.rs +++ b/integration-tests/src/lib.rs @@ -10,10 +10,10 @@ use asset_test_utils::xcm_helpers; use chains::{asset_hub_rococo::AssetHubRococo, pop_network::PopNetwork, rococo::Rococo}; use emulated_integration_tests_common::{ accounts::{ALICE, BOB}, - xcm_emulator::decl_test_networks, xcm_emulator::{ - assert_expected_events, bx, decl_test_sender_receiver_accounts_parameter_types, Chain, - Parachain as Para, RelayChain as Relay, Test, TestArgs, TestContext, TestExt, + assert_expected_events, bx, decl_test_networks, + decl_test_sender_receiver_accounts_parameter_types, Chain, Parachain as Para, + RelayChain as Relay, Test, TestArgs, TestContext, TestExt, }, }; use frame_support::{pallet_prelude::Weight, sp_runtime::DispatchResult}; @@ -278,7 +278,8 @@ fn reserve_transfer_native_asset_from_relay_to_para() { /// Reserve Transfers of native asset from Parachain to Relay should work #[test] fn reserve_transfer_native_asset_from_para_to_relay() { - // Setup: reserve transfer from relay to Pop, so that sovereign account accurate for return transfer + // Setup: reserve transfer from relay to Pop, so that sovereign account accurate for return + // transfer let amount_to_send: Balance = ROCOCO_ED * 1000; { let destination = RococoRelay::child_location_of(PopNetworkPara::para_id()); diff --git a/runtime/src/xcm_config.rs b/runtime/src/xcm_config.rs index 35e45216..aa9afd22 100644 --- a/runtime/src/xcm_config.rs +++ b/runtime/src/xcm_config.rs @@ -5,8 +5,7 @@ use super::{ use core::marker::PhantomData; use frame_support::{ parameter_types, - traits::{ConstU32, Contains, Everything, Nothing}, - traits::{ContainsPair, Get}, + traits::{ConstU32, Contains, ContainsPair, Everything, Get, Nothing}, weights::Weight, }; use frame_system::EnsureRoot; @@ -114,8 +113,8 @@ pub struct NativeAssetFrom(PhantomData); impl> ContainsPair for NativeAssetFrom { fn contains(asset: &Asset, origin: &Location) -> bool { let loc = T::get(); - &loc == origin - && matches!(asset, Asset { id: AssetId(asset_loc), fun: Fungible(_a) } + &loc == origin && + matches!(asset, Asset { id: AssetId(asset_loc), fun: Fungible(_a) } if *asset_loc == Location::from(Parent)) } } From 55d4ed64b65e857de4b8b2d9336ebd09f4b64fca Mon Sep 17 00:00:00 2001 From: Alejandro Martinez Andres <11448715+al3mart@users.noreply.github.com> Date: Mon, 11 Mar 2024 18:01:19 +0100 Subject: [PATCH 5/5] fix: stable fmt --- runtime/src/extensions.rs | 5 +++-- runtime/src/xcm_config.rs | 4 ++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/runtime/src/extensions.rs b/runtime/src/extensions.rs index 4b896b56..48af0815 100644 --- a/runtime/src/extensions.rs +++ b/runtime/src/extensions.rs @@ -210,8 +210,9 @@ where let result = match key { RuntimeStateKeys::Nfts(key) => read_nfts_state::(key, &mut env), - RuntimeStateKeys::ParachainSystem(key) => - read_parachain_system_state::(key, &mut env), + RuntimeStateKeys::ParachainSystem(key) => { + read_parachain_system_state::(key, &mut env) + }, }? .encode(); diff --git a/runtime/src/xcm_config.rs b/runtime/src/xcm_config.rs index aa9afd22..8e6c5bff 100644 --- a/runtime/src/xcm_config.rs +++ b/runtime/src/xcm_config.rs @@ -113,8 +113,8 @@ pub struct NativeAssetFrom(PhantomData); impl> ContainsPair for NativeAssetFrom { fn contains(asset: &Asset, origin: &Location) -> bool { let loc = T::get(); - &loc == origin && - matches!(asset, Asset { id: AssetId(asset_loc), fun: Fungible(_a) } + &loc == origin + && matches!(asset, Asset { id: AssetId(asset_loc), fun: Fungible(_a) } if *asset_loc == Location::from(Parent)) } }