mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-05-30 15:11:02 +00:00
Contracts: Refactor test builder (#4158)
- Moved `substrate/frame/contracts/src/tests/builder.rs` into a pub test_utils module, so we can use that in the `pallet-contracts-mock-network` tests - Refactor xcm tests to use XCM builders, and simplify the use case for xcm-send
This commit is contained in:
@@ -23,6 +23,7 @@ pub mod relay_chain;
|
||||
mod tests;
|
||||
|
||||
use crate::primitives::{AccountId, UNITS};
|
||||
pub use pallet_contracts::test_utils::{ALICE, BOB};
|
||||
use sp_runtime::BuildStorage;
|
||||
use xcm::latest::prelude::*;
|
||||
use xcm_executor::traits::ConvertLocation;
|
||||
@@ -31,8 +32,6 @@ use xcm_simulator::{decl_test_network, decl_test_parachain, decl_test_relay_chai
|
||||
|
||||
// Accounts
|
||||
pub const ADMIN: sp_runtime::AccountId32 = sp_runtime::AccountId32::new([0u8; 32]);
|
||||
pub const ALICE: sp_runtime::AccountId32 = sp_runtime::AccountId32::new([1u8; 32]);
|
||||
pub const BOB: sp_runtime::AccountId32 = sp_runtime::AccountId32::new([2u8; 32]);
|
||||
|
||||
// Balances
|
||||
pub const INITIAL_BALANCE: u128 = 1_000_000_000 * UNITS;
|
||||
|
||||
@@ -22,45 +22,31 @@ use crate::{
|
||||
relay_chain, MockNet, ParaA, ParachainBalances, Relay, ALICE, BOB, INITIAL_BALANCE,
|
||||
};
|
||||
use codec::{Decode, Encode};
|
||||
use frame_support::{
|
||||
pallet_prelude::Weight,
|
||||
traits::{fungibles::Mutate, Currency},
|
||||
};
|
||||
use pallet_balances::{BalanceLock, Reasons};
|
||||
use pallet_contracts::{Code, CollectEvents, DebugInfo, Determinism};
|
||||
use frame_support::traits::{fungibles::Mutate, Currency};
|
||||
use pallet_contracts::{test_utils::builder::*, Code};
|
||||
use pallet_contracts_fixtures::compile_module;
|
||||
use pallet_contracts_uapi::ReturnErrorCode;
|
||||
use xcm::{v4::prelude::*, VersionedLocation, VersionedXcm};
|
||||
use xcm_simulator::TestExt;
|
||||
|
||||
type ParachainContracts = pallet_contracts::Pallet<parachain::Runtime>;
|
||||
|
||||
macro_rules! assert_return_code {
|
||||
( $x:expr , $y:expr $(,)? ) => {{
|
||||
assert_eq!(u32::from_le_bytes($x.data[..].try_into().unwrap()), $y as u32);
|
||||
}};
|
||||
}
|
||||
|
||||
fn bare_call(dest: sp_runtime::AccountId32) -> BareCallBuilder<parachain::Runtime> {
|
||||
BareCallBuilder::<parachain::Runtime>::bare_call(ALICE, dest)
|
||||
}
|
||||
|
||||
/// Instantiate the tests contract, and fund it with some balance and assets.
|
||||
fn instantiate_test_contract(name: &str) -> AccountId {
|
||||
let (wasm, _) = compile_module::<Runtime>(name).unwrap();
|
||||
|
||||
// Instantiate contract.
|
||||
let contract_addr = ParaA::execute_with(|| {
|
||||
ParachainContracts::bare_instantiate(
|
||||
ALICE,
|
||||
0,
|
||||
Weight::MAX,
|
||||
None,
|
||||
Code::Upload(wasm),
|
||||
vec![],
|
||||
vec![],
|
||||
DebugInfo::UnsafeDebug,
|
||||
CollectEvents::Skip,
|
||||
)
|
||||
.result
|
||||
.unwrap()
|
||||
.account_id
|
||||
BareInstantiateBuilder::<parachain::Runtime>::bare_instantiate(ALICE, Code::Upload(wasm))
|
||||
.build_and_unwrap_account_id()
|
||||
});
|
||||
|
||||
// Funds contract account with some balance and assets.
|
||||
@@ -85,27 +71,18 @@ fn test_xcm_execute() {
|
||||
// Execute XCM instructions through the contract.
|
||||
ParaA::execute_with(|| {
|
||||
let amount: u128 = 10 * CENTS;
|
||||
let assets: Asset = (Here, amount).into();
|
||||
let beneficiary = AccountId32 { network: None, id: BOB.clone().into() };
|
||||
|
||||
// The XCM used to transfer funds to Bob.
|
||||
let message: Xcm<()> = Xcm(vec![
|
||||
WithdrawAsset(vec![(Here, amount).into()].into()),
|
||||
DepositAsset {
|
||||
assets: All.into(),
|
||||
beneficiary: AccountId32 { network: None, id: BOB.clone().into() }.into(),
|
||||
},
|
||||
]);
|
||||
let message: Xcm<()> = Xcm::builder_unsafe()
|
||||
.withdraw_asset(assets.clone())
|
||||
.deposit_asset(assets, beneficiary)
|
||||
.build();
|
||||
|
||||
let result = ParachainContracts::bare_call(
|
||||
ALICE,
|
||||
contract_addr.clone(),
|
||||
0,
|
||||
Weight::MAX,
|
||||
None,
|
||||
VersionedXcm::V4(message).encode().encode(),
|
||||
DebugInfo::UnsafeDebug,
|
||||
CollectEvents::UnsafeCollect,
|
||||
Determinism::Enforced,
|
||||
);
|
||||
let result = bare_call(contract_addr.clone())
|
||||
.data(VersionedXcm::V4(message).encode().encode())
|
||||
.build();
|
||||
|
||||
assert_eq!(result.gas_consumed, result.gas_required);
|
||||
assert_return_code!(&result.result.unwrap(), ReturnErrorCode::Success);
|
||||
@@ -127,29 +104,22 @@ fn test_xcm_execute_incomplete() {
|
||||
|
||||
// Execute XCM instructions through the contract.
|
||||
ParaA::execute_with(|| {
|
||||
let assets: Asset = (Here, amount).into();
|
||||
let beneficiary = AccountId32 { network: None, id: BOB.clone().into() };
|
||||
|
||||
// The XCM used to transfer funds to Bob.
|
||||
let message: Xcm<()> = Xcm(vec![
|
||||
WithdrawAsset(vec![(Here, amount).into()].into()),
|
||||
let message: Xcm<()> = Xcm::builder_unsafe()
|
||||
.withdraw_asset(assets.clone())
|
||||
// This will fail as the contract does not have enough balance to complete both
|
||||
// withdrawals.
|
||||
WithdrawAsset(vec![(Here, INITIAL_BALANCE).into()].into()),
|
||||
DepositAsset {
|
||||
assets: All.into(),
|
||||
beneficiary: AccountId32 { network: None, id: BOB.clone().into() }.into(),
|
||||
},
|
||||
]);
|
||||
.withdraw_asset((Here, INITIAL_BALANCE))
|
||||
.buy_execution(assets.clone(), Unlimited)
|
||||
.deposit_asset(assets, beneficiary)
|
||||
.build();
|
||||
|
||||
let result = ParachainContracts::bare_call(
|
||||
ALICE,
|
||||
contract_addr.clone(),
|
||||
0,
|
||||
Weight::MAX,
|
||||
None,
|
||||
VersionedXcm::V4(message).encode().encode(),
|
||||
DebugInfo::UnsafeDebug,
|
||||
CollectEvents::UnsafeCollect,
|
||||
Determinism::Enforced,
|
||||
);
|
||||
let result = bare_call(contract_addr.clone())
|
||||
.data(VersionedXcm::V4(message).encode().encode())
|
||||
.build();
|
||||
|
||||
assert_eq!(result.gas_consumed, result.gas_required);
|
||||
assert_return_code!(&result.result.unwrap(), ReturnErrorCode::XcmExecutionFailed);
|
||||
@@ -175,28 +145,16 @@ fn test_xcm_execute_reentrant_call() {
|
||||
});
|
||||
|
||||
// The XCM used to transfer funds to Bob.
|
||||
let message: Xcm<parachain::RuntimeCall> = Xcm(vec![
|
||||
Transact {
|
||||
origin_kind: OriginKind::Native,
|
||||
require_weight_at_most: 1_000_000_000.into(),
|
||||
call: transact_call.encode().into(),
|
||||
},
|
||||
ExpectTransactStatus(MaybeErrorCode::Success),
|
||||
]);
|
||||
let message: Xcm<parachain::RuntimeCall> = Xcm::builder_unsafe()
|
||||
.transact(OriginKind::Native, 1_000_000_000, transact_call.encode())
|
||||
.expect_transact_status(MaybeErrorCode::Success)
|
||||
.build();
|
||||
|
||||
let result = ParachainContracts::bare_call(
|
||||
ALICE,
|
||||
contract_addr.clone(),
|
||||
0,
|
||||
Weight::MAX,
|
||||
None,
|
||||
VersionedXcm::V4(message).encode().encode(),
|
||||
DebugInfo::UnsafeDebug,
|
||||
CollectEvents::UnsafeCollect,
|
||||
Determinism::Enforced,
|
||||
);
|
||||
let result = bare_call(contract_addr.clone())
|
||||
.data(VersionedXcm::V4(message).encode().encode())
|
||||
.build_and_unwrap_result();
|
||||
|
||||
assert_return_code!(&result.result.unwrap(), ReturnErrorCode::XcmExecutionFailed);
|
||||
assert_return_code!(&result, ReturnErrorCode::XcmExecutionFailed);
|
||||
|
||||
// Funds should not change hands as the XCM transact failed.
|
||||
assert_eq!(ParachainBalances::free_balance(BOB), INITIAL_BALANCE);
|
||||
@@ -207,40 +165,36 @@ fn test_xcm_execute_reentrant_call() {
|
||||
fn test_xcm_send() {
|
||||
MockNet::reset();
|
||||
let contract_addr = instantiate_test_contract("xcm_send");
|
||||
let amount = 1_000 * CENTS;
|
||||
let fee = parachain::estimate_message_fee(4); // Accounts for the `DescendOrigin` instruction added by `send_xcm`
|
||||
|
||||
// Send XCM instructions through the contract, to lock some funds on the relay chain.
|
||||
// Send XCM instructions through the contract, to transfer some funds from the contract
|
||||
// derivative account to Alice on the relay chain.
|
||||
ParaA::execute_with(|| {
|
||||
let dest = Location::from(Parent);
|
||||
let dest = VersionedLocation::V4(dest);
|
||||
let dest = VersionedLocation::V4(Parent.into());
|
||||
let assets: Asset = (Here, amount).into();
|
||||
let beneficiary = AccountId32 { network: None, id: ALICE.clone().into() };
|
||||
|
||||
let message: Xcm<()> = Xcm(vec![
|
||||
WithdrawAsset((Here, fee).into()),
|
||||
BuyExecution { fees: (Here, fee).into(), weight_limit: WeightLimit::Unlimited },
|
||||
LockAsset { asset: (Here, 5 * CENTS).into(), unlocker: (Parachain(1)).into() },
|
||||
]);
|
||||
let message = VersionedXcm::V4(message);
|
||||
let exec = ParachainContracts::bare_call(
|
||||
ALICE,
|
||||
contract_addr.clone(),
|
||||
0,
|
||||
Weight::MAX,
|
||||
None,
|
||||
(dest, message.encode()).encode(),
|
||||
DebugInfo::UnsafeDebug,
|
||||
CollectEvents::UnsafeCollect,
|
||||
Determinism::Enforced,
|
||||
);
|
||||
let message: Xcm<()> = Xcm::builder()
|
||||
.withdraw_asset(assets.clone())
|
||||
.buy_execution((Here, fee), Unlimited)
|
||||
.deposit_asset(assets, beneficiary)
|
||||
.build();
|
||||
|
||||
let mut data = &exec.result.unwrap().data[..];
|
||||
let result = bare_call(contract_addr.clone())
|
||||
.data((dest, VersionedXcm::V4(message).encode()).encode())
|
||||
.build_and_unwrap_result();
|
||||
|
||||
let mut data = &result.data[..];
|
||||
XcmHash::decode(&mut data).expect("Failed to decode xcm_send message_id");
|
||||
});
|
||||
|
||||
Relay::execute_with(|| {
|
||||
// Check if the funds are locked on the relay chain.
|
||||
let derived_contract_addr = ¶chain_account_sovereign_account_id(1, contract_addr);
|
||||
assert_eq!(
|
||||
relay_chain::Balances::locks(¶chain_account_sovereign_account_id(1, contract_addr)),
|
||||
vec![BalanceLock { id: *b"py/xcmlk", amount: 5 * CENTS, reasons: Reasons::All }]
|
||||
INITIAL_BALANCE - amount,
|
||||
relay_chain::Balances::free_balance(derived_contract_addr)
|
||||
);
|
||||
assert_eq!(INITIAL_BALANCE + amount - fee, relay_chain::Balances::free_balance(ALICE));
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user