Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
91 commits
Select commit Hold shift + click to select a range
719efe0
Intoduce XCM v2
gavofyork Aug 10, 2021
b2dae98
Minor version cleanup
gavofyork Aug 10, 2021
5ab1b8b
Minor version cleanup
gavofyork Aug 10, 2021
c854e94
Introduce SendError for XcmSend trait to avoid cycles with having Out…
gavofyork Aug 10, 2021
1e287f3
comment
gavofyork Aug 10, 2021
c279e44
Corrent type
gavofyork Aug 10, 2021
632a992
Docs
gavofyork Aug 10, 2021
18ef569
Fix build
gavofyork Aug 10, 2021
ef3bc2e
Fixes
gavofyork Aug 10, 2021
eaea484
Build
gavofyork Aug 10, 2021
9820197
Introduce the basic impl
gavofyork Aug 10, 2021
82a67f7
Docs
gavofyork Aug 12, 2021
ace53dc
Add function
gavofyork Aug 12, 2021
49f1895
Merge remote-tracking branch 'origin/master' into gav-xcm-v2
gavofyork Aug 12, 2021
c6835ae
Basic implementation
gavofyork Aug 12, 2021
19617eb
Weighed responses and on_report
gavofyork Aug 12, 2021
0831192
Make XCM more script-like
gavofyork Aug 14, 2021
62d76b4
Remove BuyExecution::orders
gavofyork Aug 14, 2021
2bf963c
Fixes
gavofyork Aug 15, 2021
d58848f
Fixes
gavofyork Aug 15, 2021
e97f993
Fixes
gavofyork Aug 15, 2021
1a0e0ce
Formatting
gavofyork Aug 15, 2021
89debf5
Merge branch 'gav-rm-buy-orders' into gav-xcm-v2
gavofyork Aug 15, 2021
4516074
Initial draft and make pallet-xcm build
gavofyork Aug 16, 2021
329360f
fix XCM tests
gavofyork Aug 16, 2021
0ca87bd
Formatting
gavofyork Aug 16, 2021
15b9371
Merge remote-tracking branch 'origin/master' into gav-xcm-v2
gavofyork Aug 16, 2021
40efb2e
Fixes
gavofyork Aug 16, 2021
36f7a4e
Formatting
gavofyork Aug 16, 2021
33594b3
spelling
gavofyork Aug 16, 2021
8fd21f2
Fixes
gavofyork Aug 16, 2021
30c225d
Fixes
gavofyork Aug 17, 2021
b0f884e
spelling
gavofyork Aug 17, 2021
732ec50
tests for translation
gavofyork Aug 17, 2021
fbd84e4
extra fields to XCM pallet
gavofyork Aug 17, 2021
236f275
Formatting
gavofyork Aug 17, 2021
0188a9d
Fixes
gavofyork Aug 17, 2021
dcdf01d
spelling
gavofyork Aug 17, 2021
3d20770
first integration test
gavofyork Aug 17, 2021
0b27f43
Another integration test
gavofyork Aug 18, 2021
11b8a80
Formatting
gavofyork Aug 20, 2021
df76b24
Merge remote-tracking branch 'origin/master' into gav-xcm-v2
gavofyork Aug 20, 2021
170a20d
fix tests
gavofyork Aug 20, 2021
26330fa
all tests
gavofyork Aug 21, 2021
5d03978
Fixes
gavofyork Aug 21, 2021
7e7528d
Fixes
gavofyork Aug 22, 2021
b462eae
Formatting
gavofyork Aug 22, 2021
e1f36c4
Fixes
gavofyork Aug 22, 2021
a0db17a
Fixes
gavofyork Aug 22, 2021
653a2a2
Formatting
gavofyork Aug 22, 2021
f1ce4eb
Bump
gavofyork Aug 22, 2021
07ba2d0
Remove unneeded structuring
gavofyork Aug 22, 2021
48baab6
add instruction
gavofyork Aug 22, 2021
f317a64
Fixes
gavofyork Aug 22, 2021
dd80ebc
spelling
gavofyork Aug 22, 2021
de5e76c
Fixes
gavofyork Aug 22, 2021
e798658
Fixes
gavofyork Aug 22, 2021
df8f941
Formatting
gavofyork Aug 22, 2021
e39b1e6
Fixes
gavofyork Aug 22, 2021
bea12fb
Fixes
gavofyork Aug 23, 2021
2b47c3c
Formatting
gavofyork Aug 23, 2021
ca21160
Introduce and use VersionedResponse
gavofyork Aug 23, 2021
a062919
Introduce versioning to dispatchables' params
gavofyork Aug 23, 2021
26ca165
Fixes
gavofyork Aug 23, 2021
aeba4a8
Formatting
gavofyork Aug 23, 2021
1862f69
Merge branch 'gav-versioned-params' into gav-xcm-v2
gavofyork Aug 23, 2021
d92b7a7
Rest of merge
gavofyork Aug 23, 2021
c4c45d5
Merge branch 'gav-xcm-v2' into gav-xcm-trycatch
gavofyork Aug 23, 2021
7d7f2ac
more work
gavofyork Aug 23, 2021
604414c
Formatting
gavofyork Aug 23, 2021
8130a36
Basic logic
gavofyork Aug 23, 2021
fc58298
Fixes
gavofyork Aug 24, 2021
93dc645
Fixes
gavofyork Aug 24, 2021
6b5045f
Merge branch 'gav-xcm-v2' into gav-xcm-trycatch
gavofyork Aug 24, 2021
22fcbf2
Fixes
gavofyork Aug 24, 2021
7f5add7
Add test
gavofyork Aug 24, 2021
8c231a0
Fixes
gavofyork Aug 24, 2021
745835f
Formatting
gavofyork Aug 24, 2021
bb046cc
Fixes
gavofyork Aug 24, 2021
34dae85
Fixes
gavofyork Aug 24, 2021
ffe8513
Fixes
gavofyork Aug 24, 2021
c0f1875
Nits
gavofyork Aug 25, 2021
7b243cc
Simplify
gavofyork Aug 25, 2021
9f7e88c
Spelling
gavofyork Aug 25, 2021
8f07df6
Formatting
gavofyork Aug 25, 2021
41d8435
Return weight of unexecuted instructions in case of error as surplus
gavofyork Aug 25, 2021
53fc8dd
Formatting
gavofyork Aug 25, 2021
944b597
Fixes
gavofyork Aug 25, 2021
bcbacce
Test for instruction count limiting
gavofyork Aug 25, 2021
8c8d434
Formatting
gavofyork Aug 25, 2021
feb79ce
Docs
gavofyork Aug 25, 2021
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
Prev Previous commit
Next Next commit
Formatting
  • Loading branch information
gavofyork committed Aug 24, 2021
commit 745835f69510f590434e3a4071f67331d9e6d25f
8 changes: 6 additions & 2 deletions runtime/kusama/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1254,6 +1254,9 @@ type LocalOriginConverter = (
parameter_types! {
/// The amount of weight an XCM operation takes. This is a safe overestimate.
pub const BaseXcmWeight: Weight = 1_000_000_000;
/// Maximum number of instructions in a single XCM fragment. A sanity check against weight
/// calculations getting too crazy.
pub const MaxInstructions: u32 = 100;
}

/// The XCM router. When we want to send an XCM message, we use this type. It amalgamates all of our
Expand Down Expand Up @@ -1289,14 +1292,15 @@ impl xcm_executor::Config for XcmConfig {
type IsTeleporter = TrustedTeleporters;
type LocationInverter = LocationInverter<Ancestry>;
type Barrier = Barrier;
type Weigher = FixedWeightBounds<BaseXcmWeight, Call>;
type Weigher = FixedWeightBounds<BaseXcmWeight, Call, MaxInstructions>;
// The weight trader piggybacks on the existing transaction-fee conversion logic.
type Trader = UsingComponents<WeightToFee, KsmLocation, AccountId, Balances, ToAuthor<Runtime>>;
type ResponseHandler = ();
}

parameter_types! {
pub const CouncilBodyId: BodyId = BodyId::Executive;
pub const MaxInstructions: u32 = 100;
}

/// Type to convert an `Origin` type value into a `MultiLocation` value which represents an interior location
Expand Down Expand Up @@ -1324,7 +1328,7 @@ impl pallet_xcm::Config for Runtime {
type XcmExecutor = XcmExecutor<XcmConfig>;
type XcmTeleportFilter = Everything;
type XcmReserveTransferFilter = Everything;
type Weigher = FixedWeightBounds<BaseXcmWeight, Call>;
type Weigher = FixedWeightBounds<BaseXcmWeight, Call, MaxInstructions>;
type LocationInverter = LocationInverter<Ancestry>;
type Origin = Origin;
type Call = Call;
Expand Down
16 changes: 10 additions & 6 deletions xcm/pallet-xcm/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,11 @@ fn report_outcome_notify_works() {
assert_eq!(
message,
Xcm(vec![
SetAppendix(Xcm(vec![
ReportError { query_id: 0, dest: Parent.into(), max_response_weight: 1_000_000 },
])),
SetAppendix(Xcm(vec![ReportError {
query_id: 0,
dest: Parent.into(),
max_response_weight: 1_000_000
},])),
TransferAsset { assets: (Here, SEND_AMOUNT).into(), beneficiary: sender.clone() },
])
);
Expand Down Expand Up @@ -95,9 +97,11 @@ fn report_outcome_works() {
assert_eq!(
message,
Xcm(vec![
SetAppendix(Xcm(vec![
ReportError { query_id: 0, dest: Parent.into(), max_response_weight: 0 },
])),
SetAppendix(Xcm(vec![ReportError {
query_id: 0,
dest: Parent.into(),
max_response_weight: 0
},])),
TransferAsset { assets: (Here, SEND_AMOUNT).into(), beneficiary: sender.clone() },
])
);
Expand Down
12 changes: 6 additions & 6 deletions xcm/src/v2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -441,22 +441,22 @@ pub enum Instruction<Call> {
RefundSurplus,

/// Set code that should be called in the case of an error happening.
///
/// An error occuring within execution of this code will _NOT_ result in the error register
///
/// An error occurring within execution of this code will _NOT_ result in the error register
/// being set, nor will an error handler be called due to it. The error handler and appendix
/// may each still be set.
///
///
/// The apparent weight of this instruction is inclusive of the inner `Xcm`; the executing
/// weight however includes only the difference between the previous handler and the new
/// handler, which can reasonably be negative, which would result in a surplus.
SetErrorHandler(Xcm<Call>),

/// Set code that should be called after code execution (including the error handler if any)
/// is finished. This will be called regardless of whether an error occurred.
///
/// Any error occuring due to execution of this code will result in the error register being
///
/// Any error occurring due to execution of this code will result in the error register being
/// set, and the error handler (if set) firing.
///
///
/// The apparent weight of this instruction is inclusive of the inner `Xcm`; the executing
/// weight however includes only the difference between the previous appendix and the new
/// appendix, which can reasonably be negative, which would result in a surplus.
Expand Down
3 changes: 2 additions & 1 deletion xcm/xcm-builder/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ parameter_types! {
pub static AllowPaidFrom: Vec<MultiLocation> = vec![];
// 1_000_000_000_000 => 1 unit of asset for 1 unit of Weight.
pub static WeightPrice: (AssetId, u128) = (From::from(Here), 1_000_000_000_000);
pub static MaxInstructions: u32 = 100;
}

pub type TestBarrier = (
Expand All @@ -278,7 +279,7 @@ impl Config for TestConfig {
type IsTeleporter = TestIsTeleporter;
type LocationInverter = LocationInverter<TestAncestry>;
type Barrier = TestBarrier;
type Weigher = FixedWeightBounds<UnitWeightCost, TestCall>;
type Weigher = FixedWeightBounds<UnitWeightCost, TestCall, MaxInstructions>;
type Trader = FixedRateOfFungible<WeightPrice, ()>;
type ResponseHandler = TestResponseHandler;
}
14 changes: 6 additions & 8 deletions xcm/xcm-builder/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,12 +219,10 @@ fn code_registers_should_work() {
ClearError,
])),
// Set the appendix - this will always fire.
SetAppendix(Xcm(vec![
TransferAsset {
assets: (Here, 4).into(),
beneficiary: X1(AccountIndex64 { index: 3, network: Any }).into(),
},
])),
SetAppendix(Xcm(vec![TransferAsset {
assets: (Here, 4).into(),
beneficiary: X1(AccountIndex64 { index: 3, network: Any }).into(),
}])),
// First xfer always works ok
TransferAsset {
assets: (Here, 1).into(),
Expand All @@ -241,13 +239,13 @@ fn code_registers_should_work() {
assert_eq!(limit, 70);

let r = XcmExecutor::<TestConfig>::execute_xcm(Here.into(), message.clone(), limit);
assert_eq!(r, Outcome::Complete(50)); // We don't pay the 20 weight for the error handler.
assert_eq!(r, Outcome::Complete(50)); // We don't pay the 20 weight for the error handler.
assert_eq!(assets(3), vec![(Here, 13).into()]);
assert_eq!(assets(3000), vec![(Here, 8).into()]);
assert_eq!(sent_xcm(), vec![]);

let r = XcmExecutor::<TestConfig>::execute_xcm(Here.into(), message, limit);
assert_eq!(r, Outcome::Complete(70)); // We pay the full weight here.
assert_eq!(r, Outcome::Complete(70)); // We pay the full weight here.
assert_eq!(assets(3), vec![(Here, 20).into()]);
assert_eq!(assets(3000), vec![(Here, 1).into()]);
assert_eq!(sent_xcm(), vec![]);
Expand Down
23 changes: 15 additions & 8 deletions xcm/xcm-builder/src/weight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,29 @@ use xcm_executor::{
Assets,
};

pub struct FixedWeightBounds<T, C>(PhantomData<(T, C)>);
impl<T: Get<Weight>, C: Decode + GetDispatchInfo> WeightBounds<C> for FixedWeightBounds<T, C> {
pub struct FixedWeightBounds<T, C, M>(PhantomData<(T, C, M)>);
impl<T: Get<Weight>, C: Decode + GetDispatchInfo, M: Get<u32>> WeightBounds<C>
for FixedWeightBounds<T, C, M>
{
fn weight(message: &mut Xcm<C>) -> Result<Weight, ()> {
let mut instructions_left = M::get();
Self::weight_with_limit(message, &mut instructions_left)
}
}

impl<T: Get<Weight>, C: Decode + GetDispatchInfo> FixedWeightBounds<T, C> {
fn weight_with_limit(message: &mut Xcm<C>, instrs_limit: &mut u32) -> Result<Weight, ()> {
let mut r = 0;
*instrs_limit = instrs_limit.checked_sub(message.0.len() as u32).ok_or(())?;
for m in message.0.iter_mut() {
r += Self::instr_weight(m)?;
r += Self::instr_weight(m, instrs_limit)?;
}
Ok(r)
}
}

impl<T: Get<Weight>, C: Decode + GetDispatchInfo> FixedWeightBounds<T, C> {
fn instr_weight(message: &mut Instruction<C>) -> Result<Weight, ()> {
fn instr_weight(message: &mut Instruction<C>, instrs_limit: &mut u32) -> Result<Weight, ()> {
Ok(T::get().saturating_add(match message {
Transact { require_weight_at_most, .. } => *require_weight_at_most,
SetErrorHandler(xcm) | SetAppendix(xcm) => Self::weight(xcm)?,
SetErrorHandler(xcm) | SetAppendix(xcm) => Self::weight_with_limit(xcm, instrs_limit)?,
_ => 0,
}))
}
Expand Down
47 changes: 23 additions & 24 deletions xcm/xcm-executor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,17 +86,19 @@ impl<Config: config::Config> ExecuteXcm<Config::Call> for XcmExecutor<Config> {
return Outcome::Error(XcmError::WeightLimitReached(xcm_weight))
}
let origin = Some(origin);

if Config::Barrier::should_execute(
&origin,
true,
&mut message,
xcm_weight,
&origin,
true,
&mut message,
xcm_weight,
&mut weight_credit,
).is_err() {
)
.is_err()
{
return Outcome::Error(XcmError::Barrier)
}

let mut vm = Self::new(origin);

while !message.0.is_empty() {
Expand Down Expand Up @@ -147,10 +149,7 @@ impl<Config: config::Config> XcmExecutor<Config> {

/// Execute the XCM program fragment and report back the error and which instruction caused it,
/// or `Ok` if there was no error.
fn execute(
&mut self,
xcm: Xcm<Config::Call>,
) -> Result<(), (u32, XcmError)> {
fn execute(&mut self, xcm: Xcm<Config::Call>) -> Result<(), (u32, XcmError)> {
log::trace!(
target: "xcm::execute",
"origin: {:?}, total_surplus/refunded: {:?}/{:?}, error_handler_weight: {:?}",
Expand All @@ -161,20 +160,20 @@ impl<Config: config::Config> XcmExecutor<Config> {
);
for (i, instr) in xcm.0.into_iter().enumerate() {
if let Err(e) = self.process_instruction(instr) {
return Err((i as u32, e));
return Err((i as u32, e))
}
}
return Ok(())
}

/// Remove the registered error handler and return it. Do not refund its weight.
fn take_error_handler(&mut self) -> Xcm::<Config::Call> {
fn take_error_handler(&mut self) -> Xcm<Config::Call> {
let mut r = Xcm::<Config::Call>(vec![]);
sp_std::mem::swap(&mut self.error_handler, &mut r);
self.error_handler_weight = 0;
r
}

/// Drop the registered error handler and refund its weight.
fn drop_error_handler(&mut self) {
self.error_handler = Xcm::<Config::Call>(vec![]);
Expand All @@ -183,7 +182,7 @@ impl<Config: config::Config> XcmExecutor<Config> {
}

/// Remove the registered appendix and return it.
fn take_appendix(&mut self) -> Xcm::<Config::Call> {
fn take_appendix(&mut self) -> Xcm<Config::Call> {
let mut r = Xcm::<Config::Call>(vec![]);
sp_std::mem::swap(&mut self.appendix, &mut r);
self.appendix_weight = 0;
Expand All @@ -201,7 +200,7 @@ impl<Config: config::Config> XcmExecutor<Config> {
}
}

/// Process a single XCM instruction, mutating the state of the XCM VM.
/// Process a single XCM instruction, mutating the state of the XCM virtual machine.
fn process_instruction(&mut self, instr: Instruction<Config::Call>) -> Result<(), XcmError> {
match instr {
WithdrawAsset(assets) => {
Expand Down Expand Up @@ -303,7 +302,8 @@ impl<Config: config::Config> XcmExecutor<Config> {
Config::ResponseHandler::on_response(origin, query_id, response, max_weight);
Ok(())
},
DescendOrigin(who) => self.origin
DescendOrigin(who) => self
.origin
.as_mut()
.ok_or(XcmError::BadOrigin)?
.append_with(who)
Expand Down Expand Up @@ -371,9 +371,8 @@ impl<Config: config::Config> XcmExecutor<Config> {
// should be executed.
if let Some(weight) = Option::<u64>::from(weight_limit) {
// pay for `weight` using up to `fees` of the holding register.
let max_fee = self.holding
.try_take(fees.into())
.map_err(|_| XcmError::NotHoldingFees)?;
let max_fee =
self.holding.try_take(fees.into()).map_err(|_| XcmError::NotHoldingFees)?;
let unspent = self.trader.buy_weight(weight, max_fee)?;
self.holding.subsume_assets(unspent);
}
Expand All @@ -389,18 +388,18 @@ impl<Config: config::Config> XcmExecutor<Config> {
self.error_handler = handler;
self.error_handler_weight = handler_weight;
Ok(())
}
},
SetAppendix(mut appendix) => {
let appendix_weight = Config::Weigher::weight(&mut appendix)?;
self.total_surplus = self.total_surplus.saturating_add(self.appendix_weight);
self.appendix = appendix;
self.appendix_weight = appendix_weight;
Ok(())
}
},
ClearError => {
self.error = None;
Ok(())
}
},
ExchangeAsset { .. } => Err(XcmError::Unimplemented),
HrmpNewChannelOpenRequest { .. } => Err(XcmError::Unimplemented),
HrmpChannelAccepted { .. } => Err(XcmError::Unimplemented),
Expand Down