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

Contracts: XCM queries & response integration #2355

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion polkadot/xcm/xcm-executor/src/traits/on_response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ impl VersionChangeNotifier for () {
}

/// The possible state of an XCM query response.
#[derive(Debug, PartialEq, Eq, Encode, Decode)]
#[derive(Debug, PartialEq, Eq, Encode, Decode, TypeInfo)]
pub enum QueryResponseStatus<BlockNumber> {
/// The response has arrived, and includes the inner Response and the block number it arrived
/// at.
Expand Down
40 changes: 40 additions & 0 deletions substrate/frame/contracts/fixtures/contracts/xcm_query.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// This file is part of Substrate.

// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#![no_std]
#![no_main]

use common::input;
use uapi::{
HostFn,
HostFnImpl as api,
};

#[no_mangle]
#[polkavm_derive::polkavm_export]
pub extern "C" fn deploy() {}

#[no_mangle]
#[polkavm_derive::polkavm_export]
pub extern "C" fn call() {
input!(512, timeout: [u8; 4], match_querier: [u8],);
let mut query_id = [0u8; 8];

#[allow(deprecated)]
api::xcm_query(timeout, match_querier, &mut query_id).unwrap();
api::return_value(uapi::ReturnFlags::empty(), &query_id);
}
37 changes: 37 additions & 0 deletions substrate/frame/contracts/fixtures/contracts/xcm_take_response.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// This file is part of Substrate.

// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#![no_std]
#![no_main]

use common::input;
use uapi::{HostFn, HostFnImpl as api};

#[no_mangle]
#[polkavm_derive::polkavm_export]
pub extern "C" fn deploy() {}

#[no_mangle]
#[polkavm_derive::polkavm_export]
pub extern "C" fn call() {
input!(query_id: [u8; 8],);
let mut response_status = [0u8; 100];

#[allow(deprecated)]
api::xcm_take_response(query_id, &mut response_status).unwrap();
api::return_value(uapi::ReturnFlags::empty(), &response_status);
}
202 changes: 114 additions & 88 deletions substrate/frame/contracts/mock-network/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,20 @@ pub mod relay_chain;
#[cfg(test)]
mod tests;

use crate::primitives::{AccountId, UNITS};
use crate::primitives::{
AccountId,
UNITS,
};
use sp_runtime::BuildStorage;
use xcm::latest::prelude::*;
pub use xcm_executor;
use xcm_executor::traits::ConvertLocation;
pub use xcm_simulator::TestExt;
use xcm_simulator::{decl_test_network, decl_test_parachain, decl_test_relay_chain};
use xcm_simulator::{
decl_test_network,
decl_test_parachain,
decl_test_relay_chain,
};

// Accounts
pub const ADMIN: sp_runtime::AccountId32 = sp_runtime::AccountId32::new([0u8; 32]);
Expand All @@ -38,114 +46,132 @@ pub const BOB: sp_runtime::AccountId32 = sp_runtime::AccountId32::new([2u8; 32])
pub const INITIAL_BALANCE: u128 = 1_000_000_000 * UNITS;

decl_test_parachain! {
pub struct ParaA {
Runtime = parachain::Runtime,
XcmpMessageHandler = parachain::MsgQueue,
DmpMessageHandler = parachain::MsgQueue,
new_ext = para_ext(1),
}
pub struct ParaA {
Runtime = parachain::Runtime,
XcmpMessageHandler = parachain::MsgQueue,
DmpMessageHandler = parachain::MsgQueue,
new_ext = para_ext(1),
}
}

decl_test_relay_chain! {
pub struct Relay {
Runtime = relay_chain::Runtime,
RuntimeCall = relay_chain::RuntimeCall,
RuntimeEvent = relay_chain::RuntimeEvent,
XcmConfig = relay_chain::XcmConfig,
MessageQueue = relay_chain::MessageQueue,
System = relay_chain::System,
new_ext = relay_ext(),
}
pub struct Relay {
Runtime = relay_chain::Runtime,
RuntimeCall = relay_chain::RuntimeCall,
RuntimeEvent = relay_chain::RuntimeEvent,
XcmConfig = relay_chain::XcmConfig,
MessageQueue = relay_chain::MessageQueue,
System = relay_chain::System,
new_ext = relay_ext(),
}
}

decl_test_network! {
pub struct MockNet {
relay_chain = Relay,
parachains = vec![
(1, ParaA),
],
}
pub struct MockNet {
relay_chain = Relay,
parachains = vec![
(1, ParaA),
],
}
}

pub fn relay_sovereign_account_id() -> AccountId {
let location: Location = (Parent,).into();
parachain::SovereignAccountOf::convert_location(&location).unwrap()
let location: Location = (Parent,).into();
parachain::SovereignAccountOf::convert_location(&location).unwrap()
}

pub fn parachain_sovereign_account_id(para: u32) -> AccountId {
let location: Location = (Parachain(para),).into();
relay_chain::SovereignAccountOf::convert_location(&location).unwrap()
let location: Location = (Parachain(para),).into();
relay_chain::SovereignAccountOf::convert_location(&location).unwrap()
}

pub fn parachain_account_sovereign_account_id(
para: u32,
who: sp_runtime::AccountId32,
para: u32,
who: sp_runtime::AccountId32,
) -> AccountId {
let location: Location = (
Parachain(para),
AccountId32 { network: Some(relay_chain::RelayNetwork::get()), id: who.into() },
)
.into();
relay_chain::SovereignAccountOf::convert_location(&location).unwrap()
let location: Location = (
Parachain(para),
AccountId32 {
network: Some(relay_chain::RelayNetwork::get()),
id: who.into(),
},
)
.into();
relay_chain::SovereignAccountOf::convert_location(&location).unwrap()
}

pub fn para_ext(para_id: u32) -> sp_io::TestExternalities {
use parachain::{MsgQueue, Runtime, System};

let mut t = frame_system::GenesisConfig::<Runtime>::default().build_storage().unwrap();

pallet_balances::GenesisConfig::<Runtime> {
balances: vec![
(ALICE, INITIAL_BALANCE),
(relay_sovereign_account_id(), INITIAL_BALANCE),
(BOB, INITIAL_BALANCE),
],
}
.assimilate_storage(&mut t)
.unwrap();

pallet_assets::GenesisConfig::<Runtime> {
assets: vec![
(0u128, ADMIN, false, 1u128), // Create derivative asset for relay's native token
],
metadata: Default::default(),
accounts: vec![
(0u128, ALICE, INITIAL_BALANCE),
(0u128, relay_sovereign_account_id(), INITIAL_BALANCE),
],
}
.assimilate_storage(&mut t)
.unwrap();

let mut ext = sp_io::TestExternalities::new(t);
ext.execute_with(|| {
sp_tracing::try_init_simple();
System::set_block_number(1);
MsgQueue::set_para_id(para_id.into());
});
ext
use parachain::{
MsgQueue,
Runtime,
System,
};

let mut t = frame_system::GenesisConfig::<Runtime>::default()
.build_storage()
.unwrap();

pallet_balances::GenesisConfig::<Runtime> {
balances: vec![
(ALICE, INITIAL_BALANCE),
(relay_sovereign_account_id(), INITIAL_BALANCE),
(BOB, INITIAL_BALANCE),
],
}
.assimilate_storage(&mut t)
.unwrap();

pallet_assets::GenesisConfig::<Runtime> {
assets: vec![
(0u128, ADMIN, false, 1u128), /* Create derivative asset for relay's
* native token */
],
metadata: Default::default(),
accounts: vec![
(0u128, ALICE, INITIAL_BALANCE),
(0u128, relay_sovereign_account_id(), INITIAL_BALANCE),
],
}
.assimilate_storage(&mut t)
.unwrap();

let mut ext = sp_io::TestExternalities::new(t);
ext.execute_with(|| {
sp_tracing::try_init_simple();
System::set_block_number(1);
MsgQueue::set_para_id(para_id.into());
});
ext
}

pub fn relay_ext() -> sp_io::TestExternalities {
use relay_chain::{Runtime, System};

let mut t = frame_system::GenesisConfig::<Runtime>::default().build_storage().unwrap();

pallet_balances::GenesisConfig::<Runtime> {
balances: vec![
(ALICE, INITIAL_BALANCE),
(parachain_sovereign_account_id(1), INITIAL_BALANCE),
(parachain_account_sovereign_account_id(1, ALICE), INITIAL_BALANCE),
],
}
.assimilate_storage(&mut t)
.unwrap();

let mut ext = sp_io::TestExternalities::new(t);
ext.execute_with(|| {
System::set_block_number(1);
});
ext
use relay_chain::{
Runtime,
System,
};

let mut t = frame_system::GenesisConfig::<Runtime>::default()
.build_storage()
.unwrap();

pallet_balances::GenesisConfig::<Runtime> {
balances: vec![
(ALICE, INITIAL_BALANCE),
(parachain_sovereign_account_id(1), INITIAL_BALANCE),
(
parachain_account_sovereign_account_id(1, ALICE),
INITIAL_BALANCE,
),
],
}
.assimilate_storage(&mut t)
.unwrap();

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

pub type ParachainPalletXcm = pallet_xcm::Pallet<parachain::Runtime>;
Expand Down
4 changes: 2 additions & 2 deletions substrate/frame/contracts/mock-network/src/parachain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ pub type SovereignAccountOf =
(AccountId32Aliases<RelayNetwork, AccountId>, ParentIsPreset<AccountId>);

parameter_types! {
pub const BlockHashCount: u64 = 250;
pub const BlockHashCount: u32 = 250;
}

#[derive_impl(frame_system::config_preludes::TestDefaultConfig)]
Expand Down Expand Up @@ -328,7 +328,7 @@ impl pallet_xcm::Config for Runtime {
type AdminOrigin = EnsureRoot<AccountId>;
}

type Block = frame_system::mocking::MockBlock<Runtime>;
type Block = frame_system::mocking::MockBlockU32<Runtime>;

impl pallet_timestamp::Config for Runtime {
type Moment = u64;
Expand Down
Loading
Loading