Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: cleaner read_state extension #37

Merged
merged 6 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions integration-tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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());
Expand Down
2 changes: 1 addition & 1 deletion pop-api/examples/read-runtime-state/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ mod pop_api_extension_demo {
pub fn read_relay_block_number(&self) {
let result =
pop_api::state::read::<BlockNumber>(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."),
});
Expand Down
4 changes: 2 additions & 2 deletions pop-api/src/v0/nfts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -756,8 +756,8 @@ mod types {
pub owner: AccountId,
/// The approved transferrer of this item, if one is set.
pub approvals: BoundedBTreeMap<AccountId, Option<BlockNumber>, 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,
}

Expand Down
1 change: 1 addition & 0 deletions primitives/src/storage_keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}

Expand Down
33 changes: 20 additions & 13 deletions runtime/src/extensions.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use cumulus_pallet_parachain_system::RelaychainDataProvider;
use cumulus_primitives_core::relay_chain::BlockNumber;
use frame_support::{
dispatch::{GetDispatchInfo, RawOrigin},
pallet_prelude::*,
Expand Down Expand Up @@ -57,7 +56,8 @@
match dispatch::<T, E>(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),
))
Expand Down Expand Up @@ -210,17 +210,8 @@

let result = match key {
RuntimeStateKeys::Nfts(key) => read_nfts_state::<T, E>(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::<T>::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::<T, E>(key, &mut env)
},
}?
.encode();
Expand All @@ -235,6 +226,22 @@
})
}

fn read_parachain_system_state<T, E>(
key: ParachainSystemKeys,
env: &mut Environment<E, BufInBufOutState>,
) -> Result<Vec<u8>, DispatchError>
where
T: pallet_contracts::Config + cumulus_pallet_parachain_system::Config,
E: Ext<T = T>,
{
match key {
ParachainSystemKeys::LastRelayChainBlockNumber => {
env.charge_weight(T::DbWeight::get().reads(1_u64))?;
Ok(RelaychainDataProvider::<T>::current_block_number().encode())
},
}
}

fn read_nfts_state<T, E>(
key: NftsKeys,
env: &mut Environment<E, BufInBufOutState>,
Expand Down Expand Up @@ -310,14 +317,14 @@
AccountId32 { id: (env.ext().address().clone()).into(), network: None }.into();
let message = Xcm::builder()
.withdraw_asset(assets.clone().into())
.buy_execution(assets.clone().into(), Unlimited)

Check warning on line 320 in runtime/src/extensions.rs

View workflow job for this annotation

GitHub Actions / clippy

useless conversion to the same type: `cumulus_primitives_core::Asset`

warning: useless conversion to the same type: `cumulus_primitives_core::Asset` --> runtime/src/extensions.rs:320:20 | 320 | .buy_execution(assets.clone().into(), Unlimited) | ^^^^^^^^^^^^^^^^^^^^^ help: consider removing `.into()`: `assets.clone()` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#useless_conversion = note: `#[warn(clippy::useless_conversion)]` on by default
.transact(
SovereignAccount,
Weight::from_parts(25_000_000, 10_000),
message.encode().into(),
)
.refund_surplus()
.deposit_asset(assets.into(), beneficiary.into())

Check warning on line 327 in runtime/src/extensions.rs

View workflow job for this annotation

GitHub Actions / clippy

useless conversion to the same type: `cumulus_primitives_core::Location`

warning: useless conversion to the same type: `cumulus_primitives_core::Location` --> runtime/src/extensions.rs:327:35 | 327 | .deposit_asset(assets.into(), beneficiary.into()) | ^^^^^^^^^^^^^^^^^^ help: consider removing `.into()`: `beneficiary` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#useless_conversion
.build();
(dest, message)
},
Expand Down
3 changes: 1 addition & 2 deletions runtime/src/xcm_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading