Skip to content
This repository was archived by the owner on Feb 29, 2024. It is now read-only.

Commit 20bb9fc

Browse files
Fix tests
1 parent 9b018fe commit 20bb9fc

3 files changed

Lines changed: 24 additions & 212 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bin/runtime-common/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ bp-runtime = { path = "../../primitives/runtime", default-features = false }
2121
pallet-bridge-dispatch = { path = "../../modules/dispatch", default-features = false }
2222
pallet-bridge-grandpa = { path = "../../modules/grandpa", default-features = false }
2323
pallet-bridge-messages = { path = "../../modules/messages", default-features = false }
24+
pallet-fee-market = { path = "../../modules/fee-market", default-features = false }
2425

2526
# Substrate dependencies
2627

@@ -49,6 +50,7 @@ std = [
4950
"pallet-bridge-dispatch/std",
5051
"pallet-bridge-grandpa/std",
5152
"pallet-bridge-messages/std",
53+
"pallet-fee-market/std",
5254
"pallet-transaction-payment/std",
5355
"scale-info/std",
5456
"sp-api/std",

bin/runtime-common/src/messages.rs

Lines changed: 21 additions & 212 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,7 @@ use bp_messages::{
2626
target_chain::{DispatchMessage, MessageDispatch, ProvedLaneMessages, ProvedMessages},
2727
InboundLaneData, LaneId, Message, MessageData, MessageKey, MessageNonce, OutboundLaneData,
2828
};
29-
use bp_runtime::{
30-
messages::{DispatchFeePayment, MessageDispatchResult},
31-
ChainId, Size, StorageProofChecker,
32-
};
29+
use bp_runtime::{messages::MessageDispatchResult, ChainId, Size, StorageProofChecker};
3330
use codec::{Decode, DecodeLimit, Encode};
3431
use frame_support::{
3532
traits::{Currency, ExistenceRequirement},
@@ -260,7 +257,7 @@ pub mod source {
260257
/// dispatch origin;
261258
/// - check that the sender has paid enough funds for both message delivery and dispatch.
262259
#[derive(RuntimeDebug)]
263-
pub struct FromThisChainMessageVerifier<B>(PhantomData<B>);
260+
pub struct FromThisChainMessageVerifier<B, F, I>(PhantomData<(B, F, I)>);
264261

265262
/// The error message returned from LaneMessageVerifier when outbound lane is disabled.
266263
pub const MESSAGE_REJECTED_BY_OUTBOUND_LANE: &str =
@@ -273,19 +270,22 @@ pub mod source {
273270
/// The error message returned from LaneMessageVerifier when the message fee is too low.
274271
pub const TOO_LOW_FEE: &str = "Provided fee is below minimal threshold required by the lane.";
275272

276-
impl<B>
273+
impl<B, F, I>
277274
LaneMessageVerifier<
278275
OriginOf<ThisChain<B>>,
279276
AccountIdOf<ThisChain<B>>,
280277
FromThisChainMessagePayload<B>,
281278
BalanceOf<ThisChain<B>>,
282-
> for FromThisChainMessageVerifier<B>
279+
> for FromThisChainMessageVerifier<B, F, I>
283280
where
284281
B: MessageBridge,
282+
F: pallet_fee_market::Config<I>,
283+
I: 'static,
285284
// matches requirements from the `frame_system::Config::Origin`
286285
OriginOf<ThisChain<B>>: Clone
287286
+ Into<Result<frame_system::RawOrigin<AccountIdOf<ThisChain<B>>>, OriginOf<ThisChain<B>>>>,
288287
AccountIdOf<ThisChain<B>>: PartialEq + Clone,
288+
pallet_fee_market::BalanceOf<F, I>: From<BalanceOf<ThisChain<B>>>,
289289
{
290290
type Error = &'static str;
291291

@@ -329,15 +329,20 @@ pub mod source {
329329
// is valid, or not.
330330
};
331331

332-
let minimal_fee_in_this_tokens = estimate_message_dispatch_and_delivery_fee::<B>(
333-
payload,
334-
B::RELAYER_FEE_PERCENT,
335-
None,
336-
)?;
332+
// Do the delivery_and_dispatch_fee. We assume that the delivery and dispatch fee always
333+
// greater than the fee market provided fee.
334+
if let Some(market_fee) = pallet_fee_market::Pallet::<F, I>::market_fee() {
335+
let message_fee: pallet_fee_market::BalanceOf<F, I> =
336+
(*delivery_and_dispatch_fee).into();
337337

338-
// compare with actual fee paid
339-
if *delivery_and_dispatch_fee < minimal_fee_in_this_tokens {
340-
return Err(TOO_LOW_FEE);
338+
// compare with actual fee paid
339+
if message_fee < market_fee {
340+
return Err(TOO_LOW_FEE);
341+
}
342+
} else {
343+
const NO_MARKET_FEE: &str = "The fee market are not ready for accepting messages.";
344+
345+
return Err(NO_MARKET_FEE);
341346
}
342347

343348
Ok(())
@@ -756,15 +761,14 @@ pub mod target {
756761
#[cfg(test)]
757762
mod tests {
758763
use super::*;
764+
use bp_runtime::messages::DispatchFeePayment;
759765
use codec::{Decode, Encode};
760766
use frame_support::weights::Weight;
761767
use std::ops::RangeInclusive;
762768

763769
const DELIVERY_TRANSACTION_WEIGHT: Weight = 100;
764-
const DELIVERY_CONFIRMATION_TRANSACTION_WEIGHT: Weight = 100;
765770
const THIS_CHAIN_WEIGHT_TO_BALANCE_RATE: Weight = 2;
766771
const BRIDGED_CHAIN_WEIGHT_TO_BALANCE_RATE: Weight = 4;
767-
const BRIDGED_CHAIN_TO_THIS_CHAIN_BALANCE_RATE: u32 = 6;
768772
const BRIDGED_CHAIN_MAX_EXTRINSIC_WEIGHT: Weight = 2048;
769773
const BRIDGED_CHAIN_MAX_EXTRINSIC_SIZE: u32 = 1024;
770774

@@ -1022,10 +1026,6 @@ mod tests {
10221026
}
10231027
}
10241028

1025-
fn test_lane_outbound_data() -> OutboundLaneData {
1026-
OutboundLaneData::default()
1027-
}
1028-
10291029
#[test]
10301030
fn message_from_bridged_chain_is_decoded() {
10311031
// the message is encoded on the bridged chain
@@ -1063,180 +1063,6 @@ mod tests {
10631063
const TEST_LANE_ID: &LaneId = b"test";
10641064
const MAXIMAL_PENDING_MESSAGES_AT_TEST_LANE: MessageNonce = 32;
10651065

1066-
fn regular_outbound_message_payload() -> source::FromThisChainMessagePayload<OnThisChainBridge>
1067-
{
1068-
source::FromThisChainMessagePayload::<OnThisChainBridge> {
1069-
spec_version: 1,
1070-
weight: 100,
1071-
origin: bp_message_dispatch::CallOrigin::SourceRoot,
1072-
dispatch_fee_payment: DispatchFeePayment::AtSourceChain,
1073-
call: vec![42],
1074-
}
1075-
}
1076-
1077-
#[test]
1078-
fn message_fee_is_checked_by_verifier() {
1079-
const EXPECTED_MINIMAL_FEE: u32 = 5500;
1080-
1081-
// payload of the This -> Bridged chain message
1082-
let payload = regular_outbound_message_payload();
1083-
1084-
// let's check if estimation matching hardcoded value
1085-
assert_eq!(
1086-
source::estimate_message_dispatch_and_delivery_fee::<OnThisChainBridge>(
1087-
&payload,
1088-
OnThisChainBridge::RELAYER_FEE_PERCENT,
1089-
None,
1090-
),
1091-
Ok(ThisChainBalance(EXPECTED_MINIMAL_FEE)),
1092-
);
1093-
1094-
// let's check if estimation is less than hardcoded, if dispatch is paid at target chain
1095-
let mut payload_with_pay_on_target = regular_outbound_message_payload();
1096-
payload_with_pay_on_target.dispatch_fee_payment = DispatchFeePayment::AtTargetChain;
1097-
let fee_at_source =
1098-
source::estimate_message_dispatch_and_delivery_fee::<OnThisChainBridge>(
1099-
&payload_with_pay_on_target,
1100-
OnThisChainBridge::RELAYER_FEE_PERCENT,
1101-
None,
1102-
)
1103-
.expect(
1104-
"estimate_message_dispatch_and_delivery_fee failed for pay-at-target-chain message",
1105-
);
1106-
assert!(
1107-
fee_at_source < EXPECTED_MINIMAL_FEE.into(),
1108-
"Computed fee {:?} without prepaid dispatch must be less than the fee with prepaid dispatch {}",
1109-
fee_at_source,
1110-
EXPECTED_MINIMAL_FEE,
1111-
);
1112-
1113-
// and now check that the verifier checks the fee
1114-
assert_eq!(
1115-
source::FromThisChainMessageVerifier::<OnThisChainBridge>::verify_message(
1116-
&ThisChainOrigin(Ok(frame_system::RawOrigin::Root)),
1117-
&ThisChainBalance(1),
1118-
TEST_LANE_ID,
1119-
&test_lane_outbound_data(),
1120-
&payload,
1121-
),
1122-
Err(source::TOO_LOW_FEE)
1123-
);
1124-
assert!(source::FromThisChainMessageVerifier::<OnThisChainBridge>::verify_message(
1125-
&ThisChainOrigin(Ok(frame_system::RawOrigin::Root)),
1126-
&ThisChainBalance(1_000_000),
1127-
TEST_LANE_ID,
1128-
&test_lane_outbound_data(),
1129-
&payload,
1130-
)
1131-
.is_ok(),);
1132-
}
1133-
1134-
#[test]
1135-
fn should_disallow_root_calls_from_regular_accounts() {
1136-
// payload of the This -> Bridged chain message
1137-
let payload = source::FromThisChainMessagePayload::<OnThisChainBridge> {
1138-
spec_version: 1,
1139-
weight: 100,
1140-
origin: bp_message_dispatch::CallOrigin::SourceRoot,
1141-
dispatch_fee_payment: DispatchFeePayment::AtSourceChain,
1142-
call: vec![42],
1143-
};
1144-
1145-
// and now check that the verifier checks the fee
1146-
assert_eq!(
1147-
source::FromThisChainMessageVerifier::<OnThisChainBridge>::verify_message(
1148-
&ThisChainOrigin(Ok(frame_system::RawOrigin::Signed(ThisChainAccountId(0)))),
1149-
&ThisChainBalance(1_000_000),
1150-
TEST_LANE_ID,
1151-
&test_lane_outbound_data(),
1152-
&payload,
1153-
),
1154-
Err(source::BAD_ORIGIN)
1155-
);
1156-
assert_eq!(
1157-
source::FromThisChainMessageVerifier::<OnThisChainBridge>::verify_message(
1158-
&ThisChainOrigin(Ok(frame_system::RawOrigin::None)),
1159-
&ThisChainBalance(1_000_000),
1160-
TEST_LANE_ID,
1161-
&test_lane_outbound_data(),
1162-
&payload,
1163-
),
1164-
Err(source::BAD_ORIGIN)
1165-
);
1166-
assert!(source::FromThisChainMessageVerifier::<OnThisChainBridge>::verify_message(
1167-
&ThisChainOrigin(Ok(frame_system::RawOrigin::Root)),
1168-
&ThisChainBalance(1_000_000),
1169-
TEST_LANE_ID,
1170-
&test_lane_outbound_data(),
1171-
&payload,
1172-
)
1173-
.is_ok(),);
1174-
}
1175-
1176-
#[test]
1177-
fn should_verify_source_and_target_origin_matching() {
1178-
// payload of the This -> Bridged chain message
1179-
let payload = source::FromThisChainMessagePayload::<OnThisChainBridge> {
1180-
spec_version: 1,
1181-
weight: 100,
1182-
origin: bp_message_dispatch::CallOrigin::SourceAccount(ThisChainAccountId(1)),
1183-
dispatch_fee_payment: DispatchFeePayment::AtSourceChain,
1184-
call: vec![42],
1185-
};
1186-
1187-
// and now check that the verifier checks the fee
1188-
assert_eq!(
1189-
source::FromThisChainMessageVerifier::<OnThisChainBridge>::verify_message(
1190-
&ThisChainOrigin(Ok(frame_system::RawOrigin::Signed(ThisChainAccountId(0)))),
1191-
&ThisChainBalance(1_000_000),
1192-
TEST_LANE_ID,
1193-
&test_lane_outbound_data(),
1194-
&payload,
1195-
),
1196-
Err(source::BAD_ORIGIN)
1197-
);
1198-
assert!(source::FromThisChainMessageVerifier::<OnThisChainBridge>::verify_message(
1199-
&ThisChainOrigin(Ok(frame_system::RawOrigin::Signed(ThisChainAccountId(1)))),
1200-
&ThisChainBalance(1_000_000),
1201-
TEST_LANE_ID,
1202-
&test_lane_outbound_data(),
1203-
&payload,
1204-
)
1205-
.is_ok(),);
1206-
}
1207-
1208-
#[test]
1209-
fn message_is_rejected_when_sent_using_disabled_lane() {
1210-
assert_eq!(
1211-
source::FromThisChainMessageVerifier::<OnThisChainBridge>::verify_message(
1212-
&ThisChainOrigin(Ok(frame_system::RawOrigin::Root)),
1213-
&ThisChainBalance(1_000_000),
1214-
b"dsbl",
1215-
&test_lane_outbound_data(),
1216-
&regular_outbound_message_payload(),
1217-
),
1218-
Err(source::MESSAGE_REJECTED_BY_OUTBOUND_LANE)
1219-
);
1220-
}
1221-
1222-
#[test]
1223-
fn message_is_rejected_when_there_are_too_many_pending_messages_at_outbound_lane() {
1224-
assert_eq!(
1225-
source::FromThisChainMessageVerifier::<OnThisChainBridge>::verify_message(
1226-
&ThisChainOrigin(Ok(frame_system::RawOrigin::Root)),
1227-
&ThisChainBalance(1_000_000),
1228-
TEST_LANE_ID,
1229-
&OutboundLaneData {
1230-
latest_received_nonce: 100,
1231-
latest_generated_nonce: 100 + MAXIMAL_PENDING_MESSAGES_AT_TEST_LANE + 1,
1232-
..Default::default()
1233-
},
1234-
&regular_outbound_message_payload(),
1235-
),
1236-
Err(source::TOO_MANY_PENDING_MESSAGES)
1237-
);
1238-
}
1239-
12401066
#[test]
12411067
fn verify_chain_message_rejects_message_with_too_small_declared_weight() {
12421068
assert!(source::verify_chain_message::<OnThisChainBridge>(
@@ -1565,21 +1391,4 @@ mod tests {
15651391
100 + 50 * 10 + 777,
15661392
);
15671393
}
1568-
1569-
#[test]
1570-
fn conversion_rate_override_works() {
1571-
let payload = regular_outbound_message_payload();
1572-
let regular_fee = source::estimate_message_dispatch_and_delivery_fee::<OnThisChainBridge>(
1573-
&payload,
1574-
OnThisChainBridge::RELAYER_FEE_PERCENT,
1575-
None,
1576-
);
1577-
let overrided_fee = source::estimate_message_dispatch_and_delivery_fee::<OnThisChainBridge>(
1578-
&payload,
1579-
OnThisChainBridge::RELAYER_FEE_PERCENT,
1580-
Some(FixedU128::from_float((BRIDGED_CHAIN_TO_THIS_CHAIN_BALANCE_RATE * 2) as f64)),
1581-
);
1582-
1583-
assert!(regular_fee < overrided_fee);
1584-
}
15851394
}

0 commit comments

Comments
 (0)