Skip to content

Commit

Permalink
fixed TODOs for weights v2 (paritytech#1860)
Browse files Browse the repository at this point in the history
  • Loading branch information
svyatonik authored and serban300 committed Apr 10, 2024
1 parent 81c32c2 commit 513bd5e
Show file tree
Hide file tree
Showing 13 changed files with 35 additions and 96 deletions.
4 changes: 2 additions & 2 deletions bridges/bin/millau/runtime/src/rialto_messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ pub const XCM_LANE: LaneId = LaneId([0, 0, 0, 0]);
/// Weight of 2 XCM instructions is for simple `Trap(42)` program, coming through bridge
/// (it is prepended with `UniversalOrigin` instruction). It is used just for simplest manual
/// tests, confirming that we don't break encoding somewhere between.
pub const BASE_XCM_WEIGHT_TWICE: u64 = 2 * crate::xcm_config::BASE_XCM_WEIGHT;
pub const BASE_XCM_WEIGHT_TWICE: Weight = crate::xcm_config::BaseXcmWeight::get().saturating_mul(2);

parameter_types! {
/// Weight credit for our test messages.
///
/// 2 XCM instructions is for simple `Trap(42)` program, coming through bridge
/// (it is prepended with `UniversalOrigin` instruction).
pub const WeightCredit: Weight = Weight::from_ref_time(BASE_XCM_WEIGHT_TWICE);
pub const WeightCredit: Weight = BASE_XCM_WEIGHT_TWICE;
}

/// Message payload for Millau -> Rialto messages.
Expand Down
4 changes: 2 additions & 2 deletions bridges/bin/millau/runtime/src/rialto_parachain_messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ pub const XCM_LANE: LaneId = LaneId([0, 0, 0, 0]);
/// Weight of 2 XCM instructions is for simple `Trap(42)` program, coming through bridge
/// (it is prepended with `UniversalOrigin` instruction). It is used just for simplest manual
/// tests, confirming that we don't break encoding somewhere between.
pub const BASE_XCM_WEIGHT_TWICE: u64 = 2 * crate::xcm_config::BASE_XCM_WEIGHT;
pub const BASE_XCM_WEIGHT_TWICE: Weight = crate::xcm_config::BaseXcmWeight::get().saturating_mul(2);

parameter_types! {
/// Weight credit for our test messages.
///
/// 2 XCM instructions is for simple `Trap(42)` program, coming through bridge
/// (it is prepended with `UniversalOrigin` instruction).
pub const WeightCredit: Weight = Weight::from_ref_time(BASE_XCM_WEIGHT_TWICE);
pub const WeightCredit: Weight = BASE_XCM_WEIGHT_TWICE;
}

/// Message payload for Millau -> RialtoParachain messages.
Expand Down
13 changes: 3 additions & 10 deletions bridges/bin/millau/runtime/src/xcm_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,9 @@ type LocalOriginConverter = (
SignedAccountId32AsNative<ThisNetwork, RuntimeOrigin>,
);

/// The amount of weight an XCM operation takes. This is a safe overestimate.
pub const BASE_XCM_WEIGHT: u64 = 1_000_000_000;

parameter_types! {
/// The amount of weight an XCM operation takes. This is a safe overestimate.
// TODO: https://github.com/paritytech/parity-bridges-common/issues/1543 - check `set_proof_size` 0 or 64*1024 or 1026?
pub const BaseXcmWeight: Weight = Weight::from_parts(BASE_XCM_WEIGHT, 0);
pub const BaseXcmWeight: Weight = Weight::from_parts(1_000_000_000, 64 * 1024);
/// Maximum number of instructions in a single XCM fragment. A sanity check against weight
/// calculations getting too crazy.
pub const MaxInstructions: u32 = 100;
Expand Down Expand Up @@ -314,17 +310,14 @@ mod tests {
};

let dispatch_weight = MessageDispatcher::dispatch_weight(&mut incoming_message);
assert_eq!(
dispatch_weight,
frame_support::weights::Weight::from_ref_time(1_000_000_000)
);
assert_eq!(dispatch_weight, BaseXcmWeight::get());

let dispatch_result =
MessageDispatcher::dispatch(&AccountId::from([0u8; 32]), incoming_message);
assert_eq!(
dispatch_result,
MessageDispatchResult {
unspent_weight: frame_support::weights::Weight::from_ref_time(0),
unspent_weight: frame_support::weights::Weight::zero(),
dispatch_level_result: (),
}
);
Expand Down
13 changes: 3 additions & 10 deletions bridges/bin/rialto-parachain/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,13 +368,9 @@ pub type XcmOriginToTransactDispatchOrigin = (
// TODO: until https://github.com/paritytech/parity-bridges-common/issues/1417 is fixed (in either way),
// the following constant must match the similar constant in the Millau runtime.

/// One XCM operation is `1_000_000_000` weight - almost certainly a conservative estimate.
pub const BASE_XCM_WEIGHT: u64 = 1_000_000_000;

parameter_types! {
/// The amount of weight an XCM operation takes. This is a safe overestimate.
// TODO: https://github.com/paritytech/parity-bridges-common/issues/1543 - check `set_proof_size` 0 or 64*1024 or 1026?
pub UnitWeightCost: Weight = Weight::from_parts(BASE_XCM_WEIGHT, 0);
pub const UnitWeightCost: Weight = Weight::from_parts(1_000_000, 64 * 1024);
// One UNIT buys 1 second of weight.
pub const WeightPrice: (MultiLocation, u128) = (MultiLocation::parent(), UNIT);
pub const MaxInstructions: u32 = 100;
Expand Down Expand Up @@ -902,17 +898,14 @@ mod tests {
};

let dispatch_weight = MessageDispatcher::dispatch_weight(&mut incoming_message);
assert_eq!(
dispatch_weight,
frame_support::weights::Weight::from_ref_time(1_000_000_000)
);
assert_eq!(dispatch_weight, UnitWeightCost::get());

let dispatch_result =
MessageDispatcher::dispatch(&AccountId::from([0u8; 32]), incoming_message);
assert_eq!(
dispatch_result,
MessageDispatchResult {
unspent_weight: frame_support::weights::Weight::from_ref_time(0),
unspent_weight: frame_support::weights::Weight::zero(),
dispatch_level_result: (),
}
);
Expand Down
4 changes: 2 additions & 2 deletions bridges/bin/rialto-parachain/runtime/src/millau_messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ pub const XCM_LANE: LaneId = LaneId([0, 0, 0, 0]);
/// Weight of 2 XCM instructions is for simple `Trap(42)` program, coming through bridge
/// (it is prepended with `UniversalOrigin` instruction). It is used just for simplest manual
/// tests, confirming that we don't break encoding somewhere between.
pub const BASE_XCM_WEIGHT_TWICE: u64 = 2 * crate::BASE_XCM_WEIGHT;
pub const BASE_XCM_WEIGHT_TWICE: Weight = crate::UnitWeightCost::get().saturating_mul(2);

parameter_types! {
/// Weight credit for our test messages.
///
/// 2 XCM instructions is for simple `Trap(42)` program, coming through bridge
/// (it is prepended with `UniversalOrigin` instruction).
pub const WeightCredit: Weight = Weight::from_ref_time(BASE_XCM_WEIGHT_TWICE);
pub const WeightCredit: Weight = BASE_XCM_WEIGHT_TWICE;
}

/// Message payload for RialtoParachain -> Millau messages.
Expand Down
5 changes: 4 additions & 1 deletion bridges/bin/rialto/node/src/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,10 @@ fn testnet_genesis(
max_upward_queue_count: 8,
max_upward_queue_size: 1024 * 1024,
max_downward_message_size: 1024 * 1024,
ump_service_total_weight: Weight::from_ref_time(100_000_000_000),
ump_service_total_weight: Weight::from_parts(
100_000_000_000,
polkadot_primitives::v2::MAX_POV_SIZE as u64,
),
max_upward_message_size: 50 * 1024,
max_upward_message_num_per_candidate: 5,
hrmp_sender_deposit: 0,
Expand Down
4 changes: 2 additions & 2 deletions bridges/bin/rialto/runtime/src/millau_messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ pub const XCM_LANE: LaneId = LaneId([0, 0, 0, 0]);
/// Weight of 2 XCM instructions is for simple `Trap(42)` program, coming through bridge
/// (it is prepended with `UniversalOrigin` instruction). It is used just for simplest manual
/// tests, confirming that we don't break encoding somewhere between.
pub const BASE_XCM_WEIGHT_TWICE: u64 = 2 * crate::xcm_config::BASE_XCM_WEIGHT;
pub const BASE_XCM_WEIGHT_TWICE: Weight = crate::xcm_config::BaseXcmWeight::get().saturating_mul(2);

parameter_types! {
/// Weight credit for our test messages.
///
/// 2 XCM instructions is for simple `Trap(42)` program, coming through bridge
/// (it is prepended with `UniversalOrigin` instruction).
pub const WeightCredit: Weight = Weight::from_ref_time(BASE_XCM_WEIGHT_TWICE);
pub const WeightCredit: Weight = BASE_XCM_WEIGHT_TWICE;
}

/// Message payload for Rialto -> Millau messages.
Expand Down
43 changes: 1 addition & 42 deletions bridges/bin/rialto/runtime/src/parachains.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use crate::{
RuntimeOrigin, ShiftSessionManager, Slots, UncheckedExtrinsic,
};

use frame_support::{parameter_types, traits::KeyOwnerProofSystem, weights::Weight};
use frame_support::{parameter_types, traits::KeyOwnerProofSystem};
use frame_system::EnsureRoot;
use polkadot_primitives::v2::{ValidatorId, ValidatorIndex};
use polkadot_runtime_common::{paras_registrar, paras_sudo_wrapper, slots};
Expand Down Expand Up @@ -163,44 +163,3 @@ impl slots::Config for Runtime {
}

impl paras_sudo_wrapper::Config for Runtime {}

pub struct ZeroWeights;

impl polkadot_runtime_common::paras_registrar::WeightInfo for ZeroWeights {
fn reserve() -> Weight {
Weight::from_ref_time(0)
}
fn register() -> Weight {
Weight::from_ref_time(0)
}
fn force_register() -> Weight {
Weight::from_ref_time(0)
}
fn deregister() -> Weight {
Weight::from_ref_time(0)
}
fn swap() -> Weight {
Weight::from_ref_time(0)
}
fn schedule_code_upgrade(_: u32) -> Weight {
Weight::from_ref_time(0)
}
fn set_current_head(_: u32) -> Weight {
Weight::from_ref_time(0)
}
}

impl polkadot_runtime_common::slots::WeightInfo for ZeroWeights {
fn force_lease() -> Weight {
Weight::from_ref_time(0)
}
fn manage_lease_period_start(_c: u32, _t: u32) -> Weight {
Weight::from_ref_time(0)
}
fn clear_all_leases() -> Weight {
Weight::from_ref_time(0)
}
fn trigger_onboard() -> Weight {
Weight::from_ref_time(0)
}
}
13 changes: 3 additions & 10 deletions bridges/bin/rialto/runtime/src/xcm_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,9 @@ type LocalOriginConverter = (
SignedAccountId32AsNative<ThisNetwork, RuntimeOrigin>,
);

/// The amount of weight an XCM operation takes. This is a safe overestimate.
pub const BASE_XCM_WEIGHT: u64 = 1_000_000_000;

parameter_types! {
/// The amount of weight an XCM operation takes. This is a safe overestimate.
// TODO: https://github.com/paritytech/parity-bridges-common/issues/1543 - check `set_proof_size` 0 or 64*1024 or 1026?
pub const BaseXcmWeight: Weight = Weight::from_parts(BASE_XCM_WEIGHT, 0);
pub const BaseXcmWeight: Weight = Weight::from_parts(1_000_000_000, 64 * 1024);
/// Maximum number of instructions in a single XCM fragment. A sanity check against weight
/// calculations getting too crazy.
pub const MaxInstructions: u32 = 100;
Expand Down Expand Up @@ -273,17 +269,14 @@ mod tests {
};

let dispatch_weight = MessageDispatcher::dispatch_weight(&mut incoming_message);
assert_eq!(
dispatch_weight,
frame_support::weights::Weight::from_ref_time(1_000_000_000)
);
assert_eq!(dispatch_weight, BaseXcmWeight::get());

let dispatch_result =
MessageDispatcher::dispatch(&AccountId::from([0u8; 32]), incoming_message);
assert_eq!(
dispatch_result,
MessageDispatchResult {
unspent_weight: frame_support::weights::Weight::from_ref_time(0),
unspent_weight: frame_support::weights::Weight::zero(),
dispatch_level_result: (),
}
);
Expand Down
14 changes: 5 additions & 9 deletions bridges/modules/messages/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1271,7 +1271,7 @@ mod tests {
TEST_RELAYER_A,
Err(()).into(),
1,
Weight::from_ref_time(0),
Weight::zero(),
),
Error::<TestRuntime, ()>::InvalidMessagesProof,
);
Expand All @@ -1287,7 +1287,7 @@ mod tests {
TEST_RELAYER_A,
Ok(vec![message(1, REGULAR_PAYLOAD)]).into(),
u32::MAX,
Weight::from_ref_time(0),
Weight::zero(),
),
Error::<TestRuntime, ()>::TooManyMessagesInTheProof,
);
Expand Down Expand Up @@ -1479,8 +1479,8 @@ mod tests {
TEST_RELAYER_A,
Ok(vec![invalid_message]).into(),
1,
Weight::from_ref_time(0), /* weight may be zero in this case (all messages are
* improperly encoded) */
Weight::zero(), /* weight may be zero in this case (all messages are
* improperly encoded) */
),);

assert_eq!(InboundLanes::<TestRuntime>::get(TEST_LANE_ID).last_delivered_nonce(), 1,);
Expand Down Expand Up @@ -1710,11 +1710,7 @@ mod tests {
Pallet::<TestRuntime>::inbound_message_data(
TEST_LANE_ID,
REGULAR_PAYLOAD.encode(),
OutboundMessageDetails {
nonce: 0,
dispatch_weight: Weight::from_ref_time(0),
size: 0,
},
OutboundMessageDetails { nonce: 0, dispatch_weight: Weight::zero(), size: 0 },
),
InboundMessageDetails { dispatch_weight: REGULAR_PAYLOAD.declared_weight },
);
Expand Down
2 changes: 1 addition & 1 deletion bridges/modules/messages/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ impl MessageDispatch<AccountId> for TestMessageDispatch {
fn dispatch_weight(message: &mut DispatchMessage<TestPayload>) -> Weight {
match message.data.payload.as_ref() {
Ok(payload) => payload.declared_weight,
Err(_) => Weight::from_ref_time(0),
Err(_) => Weight::zero(),
}
}

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 @@ -48,7 +48,7 @@ pub fn ensure_weights_are_correct<W: WeightInfoExt>() {

// verify `receive_message_proof` weight
let receive_messages_proof_weight =
W::receive_messages_proof_weight(&PreComputedSize(1), 10, Weight::from_ref_time(0));
W::receive_messages_proof_weight(&PreComputedSize(1), 10, Weight::zero());
assert_ne!(receive_messages_proof_weight.ref_time(), 0);
assert_ne!(receive_messages_proof_weight.proof_size(), 0);

Expand Down
10 changes: 6 additions & 4 deletions bridges/primitives/chain-bridge-hub-cumulus/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ pub const NORMAL_DISPATCH_RATIO: Perbill = Perbill::from_percent(75);
/// time.
///
/// This is a copy-paste from the cumulus repo's `parachains-common` crate.
// TODO: https://github.com/paritytech/parity-bridges-common/issues/1543 - remove `set_proof_size`
const MAXIMUM_BLOCK_WEIGHT: Weight = Weight::from_ref_time(constants::WEIGHT_REF_TIME_PER_SECOND)
.saturating_div(2)
.set_proof_size(polkadot_primitives::v2::MAX_POV_SIZE as u64);
Expand All @@ -62,9 +61,12 @@ parameter_types! {
NORMAL_DISPATCH_RATIO,
);

pub const BlockExecutionWeight: Weight = Weight::from_ref_time(constants::WEIGHT_REF_TIME_PER_NANOS).saturating_mul(5_000_000);

pub const ExtrinsicBaseWeight: Weight = Weight::from_ref_time(constants::WEIGHT_REF_TIME_PER_NANOS).saturating_mul(125_000);
/// Importing a block with 0 Extrinsics.
pub const BlockExecutionWeight: Weight = Weight::from_ref_time(constants::WEIGHT_REF_TIME_PER_NANOS)
.saturating_mul(5_000_000);
/// Executing a NO-OP `System::remarks` Extrinsic.
pub const ExtrinsicBaseWeight: Weight = Weight::from_ref_time(constants::WEIGHT_REF_TIME_PER_NANOS)
.saturating_mul(125_000);

pub BlockWeights: limits::BlockWeights = limits::BlockWeights::builder()
.base_block(BlockExecutionWeight::get())
Expand Down

0 comments on commit 513bd5e

Please sign in to comment.