Skip to content

Commit

Permalink
refund_pay_dispatch_fee removed (paritytech#1695)
Browse files Browse the repository at this point in the history
  • Loading branch information
svyatonik authored and serban300 committed Apr 9, 2024
1 parent 63a16a9 commit a065a35
Show file tree
Hide file tree
Showing 11 changed files with 14 additions and 105 deletions.
1 change: 0 additions & 1 deletion bridges/bin/millau/runtime/src/xcm_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,6 @@ mod tests {
dispatch_result,
MessageDispatchResult {
unspent_weight: frame_support::weights::Weight::from_ref_time(0),
dispatch_fee_paid_during_dispatch: false,
dispatch_level_result: (),
}
);
Expand Down
1 change: 0 additions & 1 deletion bridges/bin/rialto-parachain/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -892,7 +892,6 @@ mod tests {
dispatch_result,
MessageDispatchResult {
unspent_weight: frame_support::weights::Weight::from_ref_time(0),
dispatch_fee_paid_during_dispatch: false,
dispatch_level_result: (),
}
);
Expand Down
1 change: 0 additions & 1 deletion bridges/bin/rialto/runtime/src/xcm_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,6 @@ mod tests {
dispatch_result,
MessageDispatchResult {
unspent_weight: frame_support::weights::Weight::from_ref_time(0),
dispatch_fee_paid_during_dispatch: false,
dispatch_level_result: (),
}
);
Expand Down
6 changes: 1 addition & 5 deletions bridges/bin/runtime-common/src/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -542,11 +542,7 @@ pub mod target {
},
}

MessageDispatchResult {
unspent_weight: Weight::zero(),
dispatch_fee_paid_during_dispatch: false,
dispatch_level_result: (),
}
MessageDispatchResult { unspent_weight: Weight::zero(), dispatch_level_result: () }
}
}

Expand Down
33 changes: 0 additions & 33 deletions bridges/modules/messages/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,39 +256,6 @@ benchmarks_instance_pallet! {
assert!(T::is_message_dispatched(21));
}

// Benchmark `receive_messages_proof` extrinsic with single minimal-weight message and following conditions:
// * proof does not include outbound lane state proof;
// * inbound lane already has state, so it needs to be read and decoded;
// * message is successfully dispatched;
// * message requires all heavy checks done by dispatcher;
// * message dispatch fee is paid at source (bridged) chain.
//
// This benchmark is used to compute extra weight spent at target chain when fee is paid there. Then we use
// this information in two places: (1) to reduce weight of delivery tx if sender pays fee at the source chain
// and (2) to refund relayer with this weight if fee has been paid at the source chain.
receive_single_prepaid_message_proof {
let relayer_id_on_source = T::bridged_relayer_id();
let relayer_id_on_target = account("relayer", 0, SEED);
T::endow_account(&relayer_id_on_target);

// mark messages 1..=20 as delivered
receive_messages::<T, I>(20);

let (proof, dispatch_weight) = T::prepare_message_proof(MessageProofParams {
lane: T::bench_lane_id(),
message_nonces: 21..=21,
outbound_lane_data: None,
size: StorageProofSize::Minimal(EXPECTED_DEFAULT_MESSAGE_LENGTH),
});
}: receive_messages_proof(RawOrigin::Signed(relayer_id_on_target), relayer_id_on_source, proof, 1, dispatch_weight)
verify {
assert_eq!(
crate::InboundLanes::<T, I>::get(&T::bench_lane_id()).last_delivered_nonce(),
21,
);
assert!(T::is_message_dispatched(21));
}

// Benchmark `receive_messages_delivery_proof` extrinsic with following conditions:
// * single relayer is rewarded for relaying single message;
// * relayer account does not exist (in practice it needs to exist in production environment).
Expand Down
46 changes: 12 additions & 34 deletions bridges/modules/messages/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -361,32 +361,20 @@ pub mod pallet {
// losing funds for messages dispatch. But keep in mind that relayer pays base
// delivery transaction cost anyway. And base cost covers everything except
// dispatch, so we have a balance here.
let (unspent_weight, refund_pay_dispatch_fee) = match &receival_result {
let unspent_weight = match &receival_result {
ReceivalResult::Dispatched(dispatch_result) => {
valid_messages += 1;
(
dispatch_result.unspent_weight,
!dispatch_result.dispatch_fee_paid_during_dispatch,
)
dispatch_result.unspent_weight
},
ReceivalResult::InvalidNonce |
ReceivalResult::TooManyUnrewardedRelayers |
ReceivalResult::TooManyUnconfirmedMessages => (message_dispatch_weight, true),
ReceivalResult::TooManyUnconfirmedMessages => message_dispatch_weight,
};
lane_messages_received_status.push(message.key.nonce, receival_result);

let unspent_weight = unspent_weight.min(message_dispatch_weight);
dispatch_weight_left -= message_dispatch_weight - unspent_weight;
actual_weight = actual_weight.saturating_sub(unspent_weight).saturating_sub(
// delivery call weight formula assumes that the fee is paid at
// this (target) chain. If the message is prepaid at the source
// chain, let's refund relayer with this extra cost.
if refund_pay_dispatch_fee {
T::WeightInfo::pay_inbound_dispatch_fee_overhead()
} else {
Weight::zero()
},
);
actual_weight = actual_weight.saturating_sub(unspent_weight);
}

messages_received_status.push(lane_messages_received_status);
Expand Down Expand Up @@ -1554,11 +1542,9 @@ mod tests {
fn submit_with_unspent_weight(
nonce: MessageNonce,
unspent_weight: u64,
is_prepaid: bool,
) -> (Weight, Weight) {
let mut payload = REGULAR_PAYLOAD;
*payload.dispatch_result.unspent_weight.ref_time_mut() = unspent_weight;
payload.dispatch_result.dispatch_fee_paid_during_dispatch = !is_prepaid;
let proof = Ok(vec![message(nonce, payload)]).into();
let messages_count = 1;
let pre_dispatch_weight =
Expand All @@ -1582,40 +1568,32 @@ mod tests {
}

// when dispatch is returning `unspent_weight < declared_weight`
let (pre, post) = submit_with_unspent_weight(1, 1, false);
let (pre, post) = submit_with_unspent_weight(1, 1);
assert_eq!(post.ref_time(), pre.ref_time() - 1);

// when dispatch is returning `unspent_weight = declared_weight`
let (pre, post) =
submit_with_unspent_weight(2, REGULAR_PAYLOAD.declared_weight.ref_time(), false);
submit_with_unspent_weight(2, REGULAR_PAYLOAD.declared_weight.ref_time());
assert_eq!(
post.ref_time(),
pre.ref_time() - REGULAR_PAYLOAD.declared_weight.ref_time()
);

// when dispatch is returning `unspent_weight > declared_weight`
let (pre, post) = submit_with_unspent_weight(
3,
REGULAR_PAYLOAD.declared_weight.ref_time() + 1,
false,
);
let (pre, post) =
submit_with_unspent_weight(3, REGULAR_PAYLOAD.declared_weight.ref_time() + 1);
assert_eq!(
post.ref_time(),
pre.ref_time() - REGULAR_PAYLOAD.declared_weight.ref_time()
);

// when there's no unspent weight
let (pre, post) = submit_with_unspent_weight(4, 0, false);
let (pre, post) = submit_with_unspent_weight(4, 0);
assert_eq!(post, pre);

// when dispatch is returning `unspent_weight < declared_weight` AND message is prepaid
let (pre, post) = submit_with_unspent_weight(5, 1, true);
assert_eq!(
post.ref_time(),
pre.ref_time() -
1 - <TestRuntime as Config>::WeightInfo::pay_inbound_dispatch_fee_overhead()
.ref_time()
);
// when dispatch is returning `unspent_weight < declared_weight`
let (pre, post) = submit_with_unspent_weight(5, 1);
assert_eq!(post.ref_time(), pre.ref_time() - 1);
});
}

Expand Down
1 change: 0 additions & 1 deletion bridges/modules/messages/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,6 @@ pub const fn dispatch_result(
) -> MessageDispatchResult<TestDispatchLevelResult> {
MessageDispatchResult {
unspent_weight: Weight::from_ref_time(unspent_weight),
dispatch_fee_paid_during_dispatch: true,
dispatch_level_result: (),
}
}
Expand Down
11 changes: 0 additions & 11 deletions bridges/modules/messages/src/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ pub trait WeightInfo {
fn receive_single_message_proof_with_outbound_lane_state() -> Weight;
fn receive_single_message_proof_1_kb() -> Weight;
fn receive_single_message_proof_16_kb() -> Weight;
fn receive_single_prepaid_message_proof() -> Weight;
fn receive_delivery_proof_for_single_message() -> Weight;
fn receive_delivery_proof_for_two_messages_by_single_relayer() -> Weight;
fn receive_delivery_proof_for_two_messages_by_two_relayers() -> Weight;
Expand Down Expand Up @@ -91,11 +90,6 @@ impl<T: frame_system::Config> WeightInfo for BridgeWeight<T> {
.saturating_add(T::DbWeight::get().reads(3 as u64))
.saturating_add(T::DbWeight::get().writes(1 as u64))
}
fn receive_single_prepaid_message_proof() -> Weight {
Weight::from_ref_time(49_646_000 as u64)
.saturating_add(T::DbWeight::get().reads(4 as u64))
.saturating_add(T::DbWeight::get().writes(2 as u64))
}
fn receive_delivery_proof_for_single_message() -> Weight {
Weight::from_ref_time(55_108_000 as u64)
.saturating_add(T::DbWeight::get().reads(4 as u64))
Expand Down Expand Up @@ -140,11 +134,6 @@ impl WeightInfo for () {
.saturating_add(RocksDbWeight::get().reads(3 as u64))
.saturating_add(RocksDbWeight::get().writes(1 as u64))
}
fn receive_single_prepaid_message_proof() -> Weight {
Weight::from_ref_time(49_646_000 as u64)
.saturating_add(RocksDbWeight::get().reads(4 as u64))
.saturating_add(RocksDbWeight::get().writes(2 as u64))
}
fn receive_delivery_proof_for_single_message() -> Weight {
Weight::from_ref_time(55_108_000 as u64)
.saturating_add(RocksDbWeight::get().reads(4 as u64))
Expand Down
9 changes: 0 additions & 9 deletions bridges/modules/messages/src/weights_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,15 +265,6 @@ pub trait WeightInfoExt: WeightInfo {
(15 * 1024);
proof_size_in_bytes * byte_weight
}

/// Returns weight of the pay-dispatch-fee operation for inbound messages.
///
/// This function may return zero if runtime doesn't support pay-dispatch-fee-at-target-chain
/// option.
fn pay_inbound_dispatch_fee_overhead() -> Weight {
Self::receive_single_message_proof()
.saturating_sub(Self::receive_single_prepaid_message_proof())
}
}

impl WeightInfoExt for () {
Expand Down
6 changes: 1 addition & 5 deletions bridges/primitives/messages/src/target_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,6 @@ impl<AccountId> MessageDispatch<AccountId> for ForbidInboundMessages {
_: &AccountId,
_: DispatchMessage<Self::DispatchPayload>,
) -> MessageDispatchResult<Self::DispatchLevelResult> {
MessageDispatchResult {
unspent_weight: Weight::zero(),
dispatch_fee_paid_during_dispatch: false,
dispatch_level_result: (),
}
MessageDispatchResult { unspent_weight: Weight::zero(), dispatch_level_result: () }
}
}
4 changes: 0 additions & 4 deletions bridges/primitives/runtime/src/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ pub struct MessageDispatchResult<DispatchLevelResult> {
/// the weight, declared by the message sender;
/// 2) if message has not been dispatched at all.
pub unspent_weight: Weight,
/// Whether the message dispatch fee has been paid during dispatch. This will be true if your
/// configuration supports pay-dispatch-fee-at-target-chain option and message sender has
/// enabled this option.
pub dispatch_fee_paid_during_dispatch: bool,
/// Fine-grained result of single message dispatch (for better diagnostic purposes)
pub dispatch_level_result: DispatchLevelResult,
}

0 comments on commit a065a35

Please sign in to comment.