Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
12 changes: 9 additions & 3 deletions bridges/bin/runtime-common/src/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ pub mod source {
payload: &FromThisChainMessagePayload<B>,
) -> Result<(), &'static str> {
let weight_limits = BridgedChain::<B>::message_weight_limits(&payload.call);
if !weight_limits.contains(&payload.weight.into()) {
if !weight_limits.contains(&Weight::from_computation(payload.weight).into()) {
return Err("Incorrect message weight declared")
}

Expand Down Expand Up @@ -407,7 +407,11 @@ pub mod source {
let delivery_transaction = BridgedChain::<B>::estimate_delivery_transaction(
&payload.encode(),
pay_dispatch_fee_at_target_chain,
if pay_dispatch_fee_at_target_chain { 0.into() } else { payload.weight.into() },
if pay_dispatch_fee_at_target_chain {
Weight::zero().into()
} else {
Weight::from_computation(payload.weight).into()
},
);
let delivery_transaction_fee = BridgedChain::<B>::transaction_payment(delivery_transaction);

Expand Down Expand Up @@ -584,7 +588,9 @@ pub mod target {
fn dispatch_weight(
message: &DispatchMessage<Self::DispatchPayload, BalanceOf<BridgedChain<B>>>,
) -> frame_support::weights::Weight {
message.data.payload.as_ref().map(|payload| payload.weight).unwrap_or(0)
Weight::from_computation(
message.data.payload.as_ref().map(|payload| payload.weight).unwrap_or(0),
)
}

fn dispatch(
Expand Down
3 changes: 2 additions & 1 deletion bridges/bin/runtime-common/src/messages_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use crate::messages::{source::FromThisChainMessagePayload, MessageBridge};

use bp_messages::{LaneId, MessageDetails, MessageNonce};
use codec::Decode;
use frame_support::weights::Weight;
use sp_std::vec::Vec;

/// Implementation of the `To*OutboundLaneApi::message_details`.
Expand All @@ -41,7 +42,7 @@ where
FromThisChainMessagePayload::<BridgeConfig>::decode(&mut &message_data.payload[..]).ok()?;
Some(MessageDetails {
nonce,
dispatch_weight: decoded_payload.weight,
dispatch_weight: Weight::from_computation(decoded_payload.weight),
size: message_data.payload.len() as _,
delivery_and_dispatch_fee: message_data.fee,
dispatch_fee_payment: decoded_payload.dispatch_fee_payment,
Expand Down
6 changes: 4 additions & 2 deletions bridges/modules/grandpa/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,11 @@ pub mod pallet {
fn on_initialize(_n: T::BlockNumber) -> frame_support::weights::Weight {
<RequestCount<T, I>>::mutate(|count| *count = count.saturating_sub(1));

(0_u64)
let computation_weight = (0_u64)
.saturating_add(T::DbWeight::get().reads(1))
.saturating_add(T::DbWeight::get().writes(1))
.saturating_add(T::DbWeight::get().writes(1));

Weight::from_computation(computation_weight)
}
}

Expand Down
2 changes: 1 addition & 1 deletion bridges/modules/grandpa/src/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@

use frame_support::{
traits::Get,
weights::{constants::RocksDbWeight, Weight},
weights::{constants::RocksDbWeight, ComputationWeight as Weight},
};
use sp_std::marker::PhantomData;

Expand Down
54 changes: 33 additions & 21 deletions bridges/modules/messages/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ use codec::{Decode, Encode};
use frame_support::{
fail,
traits::Get,
weights::{Pays, PostDispatchInfo},
weights::{Pays, PostDispatchInfo, Weight},
};
use frame_system::RawOrigin;
use num_traits::{SaturatingAdd, Zero};
Expand Down Expand Up @@ -349,15 +349,18 @@ pub mod pallet {
T::WeightInfo::increase_message_fee(message_size as _),
);

Ok(PostDispatchInfo { actual_weight: Some(actual_weight), pays_fee: Pays::Yes })
Ok(PostDispatchInfo {
actual_weight: Some(Weight::from_computation(actual_weight)),
pays_fee: Pays::Yes,
})
}

/// Receive messages proof from bridged chain.
///
/// The weight of the call assumes that the transaction always brings outbound lane
/// state update. Because of that, the submitter (relayer) has no benefit of not including
/// this data in the transaction, so reward confirmations lags should be minimal.
#[pallet::weight(T::WeightInfo::receive_messages_proof_weight(proof, *messages_count, *dispatch_weight))]
#[pallet::weight(T::WeightInfo::receive_messages_proof_weight(proof, *messages_count, dispatch_weight.computation()))]
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is surely wrong. the dispatch will also have a PoV component which should be added in, no?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is handled by the receive_messages_proof_weight. dispatch_weight argument means is a pure dispatch weight - e.g. for call it needs to be call.get_dispatch_info().weight.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i see. is there some reason they're not being handled together?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dispatch weight is declared by the message sender when message is sent from the source (here: bridged) chain. Relay computes this argument by summing weights of all delivered messages. Why: originally we've been delivering encoded calls with prepaid (at the source chain) dispatch. Since we can't call call.get_dispatch_info().weight at the bridged chain (because it don't know the call type + it don't know weights of the other chain), it was provided by the message sender.

Other weight components (like cost of proof verification and others) depend on this chain (here: target) and are computed using regular auto benchmarks.

There's an open issue, related to this dispatch_weight argument and ongoing XCM integration. But overall, scheme will stay the same (separate argument for pure dispatch weight and auto-benchmarks for everything else).

pub fn receive_messages_proof(
origin: OriginFor<T>,
relayer_id_at_bridged_chain: T::InboundRelayer,
Expand Down Expand Up @@ -387,7 +390,7 @@ pub mod pallet {
let declared_weight = T::WeightInfo::receive_messages_proof_weight(
&proof,
messages_count,
dispatch_weight,
dispatch_weight.computation(),
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same

);
let mut actual_weight = declared_weight;

Expand Down Expand Up @@ -433,7 +436,7 @@ pub mod pallet {
// on this lane. We can't dispatch lane messages out-of-order, so if declared
// weight is not enough, let's move to next lane
let dispatch_weight = T::MessageDispatch::dispatch_weight(&message);
if dispatch_weight > dispatch_weight_left {
if dispatch_weight.is_strictly_greater_than(&dispatch_weight_left) {
log::trace!(
target: "runtime::bridge-messages",
"Cannot dispatch any more messages on lane {:?}. Weight: declared={}, left={}",
Expand Down Expand Up @@ -471,18 +474,19 @@ pub mod pallet {
ReceivalResult::TooManyUnconfirmedMessages => (dispatch_weight, true),
};

let unspent_weight = sp_std::cmp::min(unspent_weight, dispatch_weight);
let unspent_weight = unspent_weight.min(dispatch_weight);
dispatch_weight_left -= 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 {
0
},
);
actual_weight =
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also here. we seem to be disregarding PoV weight.

actual_weight.saturating_sub(unspent_weight.computation()).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 {
0
},
);
}
}

Expand All @@ -495,7 +499,10 @@ pub mod pallet {
declared_weight,
);

Ok(PostDispatchInfo { actual_weight: Some(actual_weight), pays_fee: Pays::Yes })
Ok(PostDispatchInfo {
actual_weight: Some(Weight::from_computation(actual_weight)),
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also here?

pays_fee: Pays::Yes,
})
}

/// Receive messages delivery proof from bridged chain.
Expand Down Expand Up @@ -592,7 +599,9 @@ pub mod pallet {
relayers_state.total_messages.saturating_mul(single_message_callback_overhead);
let actual_callback_weight =
T::OnDeliveryConfirmed::on_messages_delivered(&lane_id, &confirmed_messages);
match preliminary_callback_overhead.checked_sub(actual_callback_weight) {
match preliminary_callback_overhead
.checked_sub(actual_callback_weight.computation())
{
Some(difference) if difference == 0 => (),
Some(difference) => {
log::trace!(
Expand Down Expand Up @@ -646,7 +655,10 @@ pub mod pallet {
lane_id,
);

Ok(PostDispatchInfo { actual_weight: Some(actual_weight), pays_fee: Pays::Yes })
Ok(PostDispatchInfo {
actual_weight: Some(Weight::from_computation(actual_weight)),
pays_fee: Pays::Yes,
})
}
}

Expand Down Expand Up @@ -877,7 +889,7 @@ fn send_message<T: Config<I>, I: 'static>(
let single_message_callback_overhead =
T::WeightInfo::single_message_callback_overhead(T::DbWeight::get());
let actual_callback_weight = T::OnMessageAccepted::on_messages_accepted(&lane_id, &nonce);
match single_message_callback_overhead.checked_sub(actual_callback_weight) {
match single_message_callback_overhead.checked_sub(actual_callback_weight.computation()) {
Some(difference) if difference == 0 => (),
Some(difference) => {
log::trace!(
Expand Down Expand Up @@ -921,7 +933,7 @@ fn send_message<T: Config<I>, I: 'static>(

Pallet::<T, I>::deposit_event(Event::MessageAccepted { lane_id, nonce });

Ok(SendMessageArtifacts { nonce, weight: actual_weight })
Ok(SendMessageArtifacts { nonce, weight: Weight::from_computation(actual_weight) })
}

/// Ensure that the origin is either root, or `PalletOwner`.
Expand Down
2 changes: 1 addition & 1 deletion bridges/modules/messages/src/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@

use frame_support::{
traits::Get,
weights::{constants::RocksDbWeight, Weight},
weights::{constants::RocksDbWeight, ComputationWeight as Weight},
};
use sp_std::marker::PhantomData;

Expand Down
2 changes: 1 addition & 1 deletion bridges/modules/messages/src/weights_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use crate::weights::WeightInfo;

use bp_messages::{MessageNonce, UnrewardedRelayersState};
use bp_runtime::{PreComputedSize, Size};
use frame_support::weights::{RuntimeDbWeight, Weight};
use frame_support::weights::{ComputationWeight as Weight, RuntimeDbWeight};

/// Size of the message being delivered in benchmarks.
pub const EXPECTED_DEFAULT_MESSAGE_LENGTH: u32 = 128;
Expand Down
2 changes: 1 addition & 1 deletion bridges/modules/token-swap/src/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@

use frame_support::{
traits::Get,
weights::{constants::RocksDbWeight, Weight},
weights::{constants::RocksDbWeight, ComputationWeight as Weight},
};
use sp_std::marker::PhantomData;

Expand Down
4 changes: 2 additions & 2 deletions bridges/primitives/chain-rococo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ impl WeightToFeePolynomial for WeightToFee {
fn polynomial() -> WeightToFeeCoefficients<Balance> {
const CENTS: Balance = 1_000_000_000_000 / 100;
let p = CENTS;
let q = 10 * Balance::from(ExtrinsicBaseWeight::get());
let q = 10 * Balance::from(ExtrinsicBaseWeight::get().computation());
smallvec::smallvec![WeightToFeeCoefficient {
degree: 1,
negative: false,
Expand Down Expand Up @@ -99,7 +99,7 @@ pub const EXISTENTIAL_DEPOSIT: Balance = 1_000_000_000_000 / 100;
/// chain. Don't put too much reserve there, because it is used to **decrease**
/// `DEFAULT_MESSAGE_DELIVERY_TX_WEIGHT` cost. So putting large reserve would make delivery
/// transactions cheaper.
pub const PAY_INBOUND_DISPATCH_FEE_WEIGHT: Weight = 600_000_000;
pub const PAY_INBOUND_DISPATCH_FEE_WEIGHT: Weight = Weight::new().set_computation(600_000_000);

sp_api::decl_runtime_apis! {
/// API for querying information about the finalized Rococo headers.
Expand Down
10 changes: 6 additions & 4 deletions bridges/primitives/messages/src/source_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ use crate::{DeliveredMessages, InboundLaneData, LaneId, MessageNonce, OutboundLa

use crate::UnrewardedRelayer;
use bp_runtime::Size;
use frame_support::{weights::Weight, Parameter, RuntimeDebug};
use frame_support::{
dispatch::Zero, pallet_prelude::Saturating, weights::Weight, Parameter, RuntimeDebug,
};
use sp_std::{
collections::{btree_map::BTreeMap, vec_deque::VecDeque},
fmt::Debug,
Expand Down Expand Up @@ -189,7 +191,7 @@ impl<SenderOrigin, AccountId, Balance, Payload>
_message: Payload,
_delivery_and_dispatch_fee: Balance,
) -> Result<SendMessageArtifacts, Self::Error> {
Ok(SendMessageArtifacts { nonce: 0, weight: 0 })
Ok(SendMessageArtifacts { nonce: 0, weight: Weight::zero() })
}
}

Expand All @@ -213,7 +215,7 @@ pub trait OnDeliveryConfirmed {
#[impl_trait_for_tuples::impl_for_tuples(30)]
impl OnDeliveryConfirmed for Tuple {
fn on_messages_delivered(lane: &LaneId, messages: &DeliveredMessages) -> Weight {
let mut total_weight: Weight = 0;
let mut total_weight = Weight::zero();
for_tuples!(
#(
total_weight = total_weight.saturating_add(Tuple::on_messages_delivered(lane, messages));
Expand All @@ -231,7 +233,7 @@ pub trait OnMessageAccepted {

impl OnMessageAccepted for () {
fn on_messages_accepted(_lane: &LaneId, _message: &MessageNonce) -> Weight {
0
Weight::zero()
}
}

Expand Down
4 changes: 2 additions & 2 deletions bridges/primitives/messages/src/target_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use crate::{LaneId, Message, MessageData, MessageKey, OutboundLaneData};

use bp_runtime::{messages::MessageDispatchResult, Size};
use codec::{Decode, Encode, Error as CodecError};
use frame_support::{weights::Weight, Parameter, RuntimeDebug};
use frame_support::{dispatch::Zero, weights::Weight, Parameter, RuntimeDebug};
use scale_info::TypeInfo;
use sp_std::{collections::btree_map::BTreeMap, fmt::Debug, prelude::*};

Expand Down Expand Up @@ -166,7 +166,7 @@ impl<AccountId, Fee> MessageDispatch<AccountId, Fee> for ForbidInboundMessages {
) -> MessageDispatchResult {
MessageDispatchResult {
dispatch_result: false,
unspent_weight: 0,
unspent_weight: Weight::zero(),
dispatch_fee_paid_during_dispatch: false,
}
}
Expand Down
12 changes: 7 additions & 5 deletions bridges/primitives/polkadot-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ const NORMAL_DISPATCH_RATIO: Perbill = Perbill::from_percent(75);
/// All Polkadot-like chains allow 2 seconds of compute with a 6-second average block time.
///
/// This is a copy-paste from the Polkadot repo's `polkadot-runtime-common` crate.
pub const MAXIMUM_BLOCK_WEIGHT: Weight = 2 * WEIGHT_PER_SECOND;
pub const MAXIMUM_BLOCK_WEIGHT: Weight =
Weight::new().set_computation(2 * WEIGHT_PER_SECOND).set_bandwidth(u64::MAX); // No limit to bandwidth at the moment.

/// All Polkadot-like chains assume that an on-initialize consumes 1 percent of the weight on
/// average, hence a single extrinsic will not be allowed to consume more than
Expand Down Expand Up @@ -137,15 +138,16 @@ pub const MAX_UNCONFIRMED_MESSAGES_IN_CONFIRMATION_TX: MessageNonce = 8192;
/// This value is a result of `pallet_bridge_messages::Pallet::receive_messages_delivery_proof`
/// weight formula computation for the case when single message is confirmed. The result then must
/// be rounded up to account possible future runtime upgrades.
pub const MAX_SINGLE_MESSAGE_DELIVERY_CONFIRMATION_TX_WEIGHT: Weight = 2_000_000_000;
pub const MAX_SINGLE_MESSAGE_DELIVERY_CONFIRMATION_TX_WEIGHT: Weight =
Weight::new().set_computation(2_000_000_000).set_bandwidth(u64::MAX); // No bandwidth limit at the moment

/// Increase of delivery transaction weight on Polkadot-like chain with every additional message
/// byte.
///
/// This value is a result of
/// `pallet_bridge_messages::WeightInfoExt::storage_proof_size_overhead(1)` call. The result then
/// must be rounded up to account possible future runtime upgrades.
pub const ADDITIONAL_MESSAGE_BYTE_DELIVERY_WEIGHT: Weight = 25_000;
pub const ADDITIONAL_MESSAGE_BYTE_DELIVERY_WEIGHT: Weight = Weight::new().set_computation(25_000);

/// Maximal number of bytes, included in the signed Polkadot-like transaction apart from the encoded
/// call itself.
Expand All @@ -159,7 +161,7 @@ pub const TX_EXTRA_BYTES: u32 = 256;
/// for the case when single message of `pallet_bridge_messages::EXPECTED_DEFAULT_MESSAGE_LENGTH`
/// bytes is delivered. The message must have dispatch weight set to zero. The result then must be
/// rounded up to account possible future runtime upgrades.
pub const DEFAULT_MESSAGE_DELIVERY_TX_WEIGHT: Weight = 1_500_000_000;
pub const DEFAULT_MESSAGE_DELIVERY_TX_WEIGHT: Weight = Weight::new().set_computation(1_500_000_000);

/// Weight of pay-dispatch-fee operation for inbound messages at Polkadot-like chain.
///
Expand All @@ -168,7 +170,7 @@ pub const DEFAULT_MESSAGE_DELIVERY_TX_WEIGHT: Weight = 1_500_000_000;
/// chain. Don't put too much reserve there, because it is used to **decrease**
/// `DEFAULT_MESSAGE_DELIVERY_TX_WEIGHT` cost. So putting large reserve would make delivery
/// transactions cheaper.
pub const PAY_INBOUND_DISPATCH_FEE_WEIGHT: Weight = 600_000_000;
pub const PAY_INBOUND_DISPATCH_FEE_WEIGHT: Weight = Weight::new().set_computation(600_000_000);

/// Re-export `time_units` to make usage easier.
pub use time_units::*;
Expand Down
6 changes: 3 additions & 3 deletions parachain/src/primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

use sp_std::vec::Vec;

use frame_support::weights::Weight;
use frame_support::{dispatch::Zero, weights::Weight};
use parity_scale_codec::{CompactAs, Decode, Encode, MaxEncodedLen};
use scale_info::TypeInfo;
use sp_core::{RuntimeDebug, TypeId};
Expand Down Expand Up @@ -316,7 +316,7 @@ impl DmpMessageHandler for () {
_max_weight: Weight,
) -> Weight {
iter.for_each(drop);
0
Weight::zero()
}
}

Expand Down Expand Up @@ -349,7 +349,7 @@ impl XcmpMessageHandler for () {
_max_weight: Weight,
) -> Weight {
for _ in iter {}
0
Weight::zero()
}
}

Expand Down
6 changes: 4 additions & 2 deletions runtime/parachains/src/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,9 @@ impl<BlockNumber: Default + From<u32>> Default for HostConfiguration<BlockNumber
hrmp_max_parachain_outbound_channels: Default::default(),
hrmp_max_parathread_outbound_channels: Default::default(),
hrmp_max_message_num_per_candidate: Default::default(),
ump_max_individual_weight: 20 * WEIGHT_PER_MILLIS,
ump_max_individual_weight: Weight::new()
.set_computation(20 * WEIGHT_PER_MILLIS)
.set_bandwidth(5 * 1024 * 1024),
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

20 ms is ~1% of the 2s expected maximum. 50KB should be about the same proportion of the maximum PoV size.

Suggested change
.set_bandwidth(5 * 1024 * 1024),
.set_bandwidth(50 * 1024),

pvf_checking_enabled: false,
pvf_voting_ttl: 2u32.into(),
minimum_validation_upgrade_delay: 2.into(),
Expand Down Expand Up @@ -1136,7 +1138,7 @@ pub struct SessionChangeOutcome<BlockNumber> {
impl<T: Config> Pallet<T> {
/// Called by the initializer to initialize the configuration pallet.
pub(crate) fn initializer_initialize(_now: T::BlockNumber) -> Weight {
0
Weight::zero()
}

/// Called by the initializer to finalize the configuration pallet.
Expand Down
Loading