Newer
Older
// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Polkadot.
// Polkadot is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Polkadot is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Polkadot. If not, see <http://www.gnu.org/licenses/>.
Adrian Catangiu
committed
#![cfg(test)]
pub(crate) mod assets_transfer;
Adrian Catangiu
committed
use crate::{
mock::*, AssetTraps, CurrentMigration, Error, LatestVersionedMultiLocation, Queries,
QueryStatus, VersionDiscoveryQueue, VersionMigrationStage, VersionNotifiers,
VersionNotifyTargets,
};
use frame_support::{
assert_noop, assert_ok,
traits::{Currency, Hooks},
use polkadot_parachain_primitives::primitives::Id as ParaId;
use sp_runtime::traits::{AccountIdConversion, BlakeTwo256, Hash};
use xcm_builder::AllowKnownQueryResponses;
traits::{Properties, QueryHandler, QueryResponseStatus, ShouldExecute},
const ALICE: AccountId = AccountId::new([0u8; 32]);
const BOB: AccountId = AccountId::new([1u8; 32]);
const INITIAL_BALANCE: u128 = 100;
const SEND_AMOUNT: u128 = 10;
Adrian Catangiu
committed
const FEE_AMOUNT: u128 = 2;
Gavin Wood
committed
#[test]
fn report_outcome_notify_works() {
let balances = vec![
(ALICE, INITIAL_BALANCE),
Adrian Catangiu
committed
(ParaId::from(OTHER_PARA_ID).into_account_truncating(), INITIAL_BALANCE),
let sender: MultiLocation = AccountId32 { network: None, id: ALICE.into() }.into();
let mut message =
Xcm(vec![TransferAsset { assets: (Here, SEND_AMOUNT).into(), beneficiary: sender }]);
let call = pallet_test_notifier::Call::notification_received {
query_id: 0,
response: Default::default(),
};
let notify = RuntimeCall::TestNotifier(call);
Gavin Wood
committed
new_test_ext_with_balances(balances).execute_with(|| {
Adrian Catangiu
committed
Parachain(OTHER_PARA_ID).into_location(),
Gavin Wood
committed
assert_eq!(
message,
Xcm(vec![
SetAppendix(Xcm(vec![ReportError(QueryResponseInfo {
destination: Parent.into(),
Gavin Wood
committed
query_id: 0,
max_weight: Weight::from_parts(1_000_000, 1_000_000),
})])),
TransferAsset { assets: (Here, SEND_AMOUNT).into(), beneficiary: sender },
Gavin Wood
committed
])
);
Gavin Wood
committed
let status = QueryStatus::Pending {
Adrian Catangiu
committed
responder: MultiLocation::from(Parachain(OTHER_PARA_ID)).into(),
maybe_notify: Some((5, 2)),
Gavin Wood
committed
timeout: 100,
maybe_match_querier: Some(querier.into()),
Gavin Wood
committed
};
assert_eq!(crate::Queries::<Test>::iter().collect::<Vec<_>>(), vec![(0, status)]);
let message = Xcm(vec![QueryResponse {
query_id: 0,
response: Response::ExecutionResult(None),
max_weight: Weight::from_parts(1_000_000, 1_000_000),
querier: Some(querier),
}]);
let hash = fake_message_hash(&message);
Gavin Wood
committed
let r = XcmExecutor::<XcmConfig>::execute_xcm(
Adrian Catangiu
committed
Parachain(OTHER_PARA_ID),
message,
hash,
Weight::from_parts(1_000_000_000, 1_000_000_000),
Gavin Wood
committed
);
assert_eq!(r, Outcome::Complete(Weight::from_parts(1_000, 1_000)));
Gavin Wood
committed
assert_eq!(
last_events(2),
vec![
RuntimeEvent::TestNotifier(pallet_test_notifier::Event::ResponseReceived(
Adrian Catangiu
committed
Parachain(OTHER_PARA_ID).into(),
Gavin Wood
committed
0,
Response::ExecutionResult(None),
Gavin Wood
committed
)),
RuntimeEvent::XcmPallet(crate::Event::Notified {
query_id: 0,
Adrian Catangiu
committed
pallet_index: 5,
Gavin Wood
committed
]
);
assert_eq!(crate::Queries::<Test>::iter().collect::<Vec<_>>(), vec![]);
});
}
#[test]
fn report_outcome_works() {
let balances = vec![
(ALICE, INITIAL_BALANCE),
Adrian Catangiu
committed
(ParaId::from(OTHER_PARA_ID).into_account_truncating(), INITIAL_BALANCE),
let sender: MultiLocation = AccountId32 { network: None, id: ALICE.into() }.into();
let mut message =
Xcm(vec![TransferAsset { assets: (Here, SEND_AMOUNT).into(), beneficiary: sender }]);
Gavin Wood
committed
new_test_ext_with_balances(balances).execute_with(|| {
Adrian Catangiu
committed
XcmPallet::report_outcome(&mut message, Parachain(OTHER_PARA_ID).into_location(), 100)
.unwrap();
Gavin Wood
committed
assert_eq!(
message,
Xcm(vec![
SetAppendix(Xcm(vec![ReportError(QueryResponseInfo {
destination: Parent.into(),
Gavin Wood
committed
query_id: 0,
TransferAsset { assets: (Here, SEND_AMOUNT).into(), beneficiary: sender },
Gavin Wood
committed
])
);
Gavin Wood
committed
let status = QueryStatus::Pending {
Adrian Catangiu
committed
responder: MultiLocation::from(Parachain(OTHER_PARA_ID)).into(),
Gavin Wood
committed
maybe_notify: None,
timeout: 100,
maybe_match_querier: Some(querier.into()),
Gavin Wood
committed
};
assert_eq!(crate::Queries::<Test>::iter().collect::<Vec<_>>(), vec![(0, status)]);
let message = Xcm(vec![QueryResponse {
query_id: 0,
response: Response::ExecutionResult(None),
max_weight: Weight::zero(),
querier: Some(querier),
}]);
let hash = fake_message_hash(&message);
Gavin Wood
committed
let r = XcmExecutor::<XcmConfig>::execute_xcm(
Adrian Catangiu
committed
Parachain(OTHER_PARA_ID),
message,
hash,
Weight::from_parts(1_000_000_000, 1_000_000_000),
Gavin Wood
committed
);
assert_eq!(r, Outcome::Complete(Weight::from_parts(1_000, 1_000)));
assert_eq!(
last_event(),
RuntimeEvent::XcmPallet(crate::Event::ResponseReady {
query_id: 0,
response: Response::ExecutionResult(None),
})
let response =
QueryResponseStatus::Ready { response: Response::ExecutionResult(None), at: 1 };
assert_eq!(XcmPallet::take_response(0), response);
});
}
#[test]
fn custom_querier_works() {
let balances = vec![
(ALICE, INITIAL_BALANCE),
Adrian Catangiu
committed
(ParaId::from(OTHER_PARA_ID).into_account_truncating(), INITIAL_BALANCE),
];
new_test_ext_with_balances(balances).execute_with(|| {
let querier: MultiLocation =
(Parent, AccountId32 { network: None, id: ALICE.into() }).into();
let r = TestNotifier::prepare_new_query(RuntimeOrigin::signed(ALICE), querier);
assert_eq!(r, Ok(()));
let status = QueryStatus::Pending {
responder: MultiLocation::from(AccountId32 { network: None, id: ALICE.into() }).into(),
maybe_notify: None,
timeout: 100,
maybe_match_querier: Some(querier.into()),
};
assert_eq!(crate::Queries::<Test>::iter().collect::<Vec<_>>(), vec![(0, status)]);
Loading full blame...