Skip to content

Commit

Permalink
Account proof size in weight formula (paritytech#679)
Browse files Browse the repository at this point in the history
* fix broken message lane benchmarks

* proof-size related benchmarks

* impl Size for proof parameters

* include proof weight into weight formula

* left TODO

* fixed proof size

* WeightInfoExt::receive_messages_proof_weight

* charge for extra message bytes delivery in send_message

* removed default impl of WeightsInfoExt

* moved weight formulas to WeightInfoExt

* receive_messages_proof_outbound_lane_state_overhead is included twice in weight

* typo

* typo

* fixed TODO

* more asserts

* started wotk on message-lane documentation

* expected_extra_storage_proof_size() is actually expected in delivery confirmation tx

* update README.md

* ensure_able_to_receive_confirmation

* test rialto message lane weights

* removed TODO

* removed unnecessary trait requirements

* fixed arguments

* fix compilation

* decreased basic delivery tx weight

* fmt

* clippy

* Update modules/message-lane/src/benchmarking.rs

Co-authored-by: Hernando Castano <[email protected]>

* structs

* Update primitives/millau/src/lib.rs

Co-authored-by: Hernando Castano <[email protected]>

* removed readme.md

* removed obsolete trait bounds

* Revert "removed readme.md"

This reverts commit 50b7376a41687a94c27bf77565434be153f87ca1.

* Update bin/runtime-common/src/messages.rs

Co-authored-by: Tomasz Drwięga <[email protected]>

* Update bin/runtime-common/src/messages.rs

Co-authored-by: Tomasz Drwięga <[email protected]>

* Update bin/runtime-common/src/messages.rs

Co-authored-by: Tomasz Drwięga <[email protected]>

* Update bin/runtime-common/src/messages.rs

Co-authored-by: Tomasz Drwięga <[email protected]>

* Update bin/runtime-common/src/messages.rs

Co-authored-by: Tomasz Drwięga <[email protected]>

* Update bin/runtime-common/src/messages.rs

Co-authored-by: Tomasz Drwięga <[email protected]>

* Update bin/runtime-common/src/messages.rs

Co-authored-by: Tomasz Drwięga <[email protected]>

* PreComputedSize

Co-authored-by: Hernando Castano <[email protected]>
Co-authored-by: Tomasz Drwięga <[email protected]>
  • Loading branch information
3 people authored and serban300 committed Apr 9, 2024
1 parent 7f83f39 commit 56a951d
Show file tree
Hide file tree
Showing 25 changed files with 905 additions and 290 deletions.
38 changes: 37 additions & 1 deletion bridges/bin/millau/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -623,13 +623,49 @@ where
#[cfg(test)]
mod tests {
use super::*;
use bridge_runtime_common::messages;

#[test]
fn ensure_millau_message_lane_weights_are_correct() {
// TODO: https://github.com/paritytech/parity-bridges-common/issues/390
pallet_message_lane::ensure_weights_are_correct::<pallet_message_lane::weights::RialtoWeight<Runtime>>(
type Weights = pallet_message_lane::weights::RialtoWeight<Runtime>;

pallet_message_lane::ensure_weights_are_correct::<Weights>(
bp_millau::MAX_SINGLE_MESSAGE_DELIVERY_TX_WEIGHT,
bp_millau::MAX_SINGLE_MESSAGE_DELIVERY_CONFIRMATION_TX_WEIGHT,
);

let max_incoming_message_proof_size = bp_rialto::EXTRA_STORAGE_PROOF_SIZE.saturating_add(
messages::target::maximal_incoming_message_size(bp_millau::max_extrinsic_size()),
);
pallet_message_lane::ensure_able_to_receive_message::<Weights>(
bp_millau::max_extrinsic_size(),
bp_millau::max_extrinsic_weight(),
max_incoming_message_proof_size,
bridge_runtime_common::messages::transaction_weight_without_multiplier(
bp_millau::BlockWeights::get().get(DispatchClass::Normal).base_extrinsic,
max_incoming_message_proof_size as _,
0,
),
messages::target::maximal_incoming_message_dispatch_weight(bp_millau::max_extrinsic_weight()),
);

let max_incoming_inbound_lane_data_proof_size = bp_message_lane::InboundLaneData::<()>::encoded_size_hint(
bp_millau::MAXIMAL_ENCODED_ACCOUNT_ID_SIZE,
bp_rialto::MAX_UNREWARDED_RELAYER_ENTRIES_AT_INBOUND_LANE as _,
)
.unwrap_or(u32::MAX);
pallet_message_lane::ensure_able_to_receive_confirmation::<Weights>(
bp_millau::max_extrinsic_size(),
bp_millau::max_extrinsic_weight(),
max_incoming_inbound_lane_data_proof_size,
bp_rialto::MAX_UNREWARDED_RELAYER_ENTRIES_AT_INBOUND_LANE,
bp_rialto::MAX_UNCONFIRMED_MESSAGES_AT_INBOUND_LANE,
bridge_runtime_common::messages::transaction_weight_without_multiplier(
bp_millau::BlockWeights::get().get(DispatchClass::Normal).base_extrinsic,
max_incoming_inbound_lane_data_proof_size as _,
0,
),
);
}
}
22 changes: 13 additions & 9 deletions bridges/bin/millau/runtime/src/rialto_messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@ pub type FromRialtoMessagePayload = messages::target::FromBridgedChainMessagePay
pub type FromRialtoEncodedCall = messages::target::FromBridgedChainEncodedMessageCall<WithRialtoMessageBridge>;

/// Messages proof for Rialto -> Millau messages.
type FromRialtoMessagesProof = messages::target::FromBridgedChainMessagesProof<WithRialtoMessageBridge>;
type FromRialtoMessagesProof = messages::target::FromBridgedChainMessagesProof<bp_rialto::Hash>;

/// Messages delivery proof for Millau -> Rialto messages.
type ToRialtoMessagesDeliveryProof = messages::source::FromBridgedChainMessagesDeliveryProof<WithRialtoMessageBridge>;
type ToRialtoMessagesDeliveryProof = messages::source::FromBridgedChainMessagesDeliveryProof<bp_rialto::Hash>;

/// Call-dispatch based message dispatch for Rialto -> Millau messages.
pub type FromRialtoMessageDispatch = messages::target::FromBridgedChainMessageDispatch<
Expand All @@ -97,7 +97,7 @@ impl MessageBridge for WithRialtoMessageBridge {

fn weight_limits_of_message_on_bridged_chain(message_payload: &[u8]) -> RangeInclusive<Weight> {
// we don't want to relay too large messages + keep reserve for future upgrades
let upper_limit = bp_rialto::max_extrinsic_weight() / 2;
let upper_limit = messages::target::maximal_incoming_message_dispatch_weight(bp_rialto::max_extrinsic_weight());

// given Rialto chain parameters (`TransactionByteFee`, `WeightToFee`, `FeeMultiplierUpdate`),
// the minimal weight of the message may be computed as message.length()
Expand All @@ -109,13 +109,17 @@ impl MessageBridge for WithRialtoMessageBridge {
}

fn weight_of_delivery_transaction(message_payload: &[u8]) -> Weight {
let message_payload_len = u32::try_from(message_payload.len())
.map(Into::into)
.unwrap_or(Weight::MAX);
let extra_bytes_in_payload =
message_payload_len.saturating_sub(pallet_message_lane::EXPECTED_DEFAULT_MESSAGE_LENGTH.into());
messages::transaction_weight_without_multiplier(
bp_millau::BlockWeights::get().get(DispatchClass::Normal).base_extrinsic,
u32::try_from(message_payload.len())
.map(Into::into)
.unwrap_or(Weight::MAX)
.saturating_add(bp_millau::EXTRA_STORAGE_PROOF_SIZE as _),
bp_rialto::MAX_SINGLE_MESSAGE_DELIVERY_TX_WEIGHT,
bp_rialto::BlockWeights::get().get(DispatchClass::Normal).base_extrinsic,
message_payload_len.saturating_add(bp_millau::EXTRA_STORAGE_PROOF_SIZE as _),
extra_bytes_in_payload
.saturating_mul(bp_rialto::ADDITIONAL_MESSAGE_BYTE_DELIVERY_WEIGHT)
.saturating_add(bp_rialto::MAX_SINGLE_MESSAGE_DELIVERY_TX_WEIGHT),
)
}

Expand Down
47 changes: 44 additions & 3 deletions bridges/bin/rialto/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -829,6 +829,7 @@ impl_runtime_apis! {
MessageDeliveryProofParams as MessageLaneMessageDeliveryProofParams,
MessageParams as MessageLaneMessageParams,
MessageProofParams as MessageLaneMessageProofParams,
ProofSize as MessageLaneProofSize,
};

impl MessageLaneConfig<WithMillauMessageLaneInstance> for Runtime {
Expand Down Expand Up @@ -882,7 +883,11 @@ impl_runtime_apis! {
use pallet_message_lane::storage_keys;
use sp_runtime::traits::Header;

let call = Call::System(SystemCall::remark(vec![]));
let remark = match params.size {
MessageLaneProofSize::Minimal(ref size) => vec![0u8; *size as _],
_ => vec![],
};
let call = Call::System(SystemCall::remark(remark));
let call_weight = call.get_dispatch_info().weight;

let millau_account_id: bp_millau::AccountId = Default::default();
Expand Down Expand Up @@ -939,7 +944,7 @@ impl_runtime_apis! {

fn prepare_message_delivery_proof(
params: MessageLaneMessageDeliveryProofParams<Self::AccountId>,
) -> millau_messages::FromMillauMessagesDeliveryProof {
) -> millau_messages::ToMillauMessagesDeliveryProof {
use crate::millau_messages::{Millau, WithMillauMessageBridge};
use bridge_runtime_common::{
messages::ChainWithMessageLanes,
Expand Down Expand Up @@ -1013,6 +1018,7 @@ where
mod tests {
use super::*;
use bp_currency_exchange::DepositInto;
use bridge_runtime_common::messages;

fn run_deposit_into_test(test: impl Fn(AccountId) -> Balance) {
let mut ext: sp_io::TestExternalities = SystemConfig::default().build_storage::<Runtime>().unwrap().into();
Expand Down Expand Up @@ -1051,10 +1057,45 @@ mod tests {

#[test]
fn ensure_rialto_message_lane_weights_are_correct() {
pallet_message_lane::ensure_weights_are_correct::<pallet_message_lane::weights::RialtoWeight<Runtime>>(
type Weights = pallet_message_lane::weights::RialtoWeight<Runtime>;

pallet_message_lane::ensure_weights_are_correct::<Weights>(
bp_rialto::MAX_SINGLE_MESSAGE_DELIVERY_TX_WEIGHT,
bp_rialto::MAX_SINGLE_MESSAGE_DELIVERY_CONFIRMATION_TX_WEIGHT,
);

let max_incoming_message_proof_size = bp_millau::EXTRA_STORAGE_PROOF_SIZE.saturating_add(
messages::target::maximal_incoming_message_size(bp_rialto::max_extrinsic_size()),
);
pallet_message_lane::ensure_able_to_receive_message::<Weights>(
bp_rialto::max_extrinsic_size(),
bp_rialto::max_extrinsic_weight(),
max_incoming_message_proof_size,
bridge_runtime_common::messages::transaction_weight_without_multiplier(
bp_rialto::BlockWeights::get().get(DispatchClass::Normal).base_extrinsic,
max_incoming_message_proof_size as _,
0,
),
messages::target::maximal_incoming_message_dispatch_weight(bp_rialto::max_extrinsic_weight()),
);

let max_incoming_inbound_lane_data_proof_size = bp_message_lane::InboundLaneData::<()>::encoded_size_hint(
bp_rialto::MAXIMAL_ENCODED_ACCOUNT_ID_SIZE,
bp_millau::MAX_UNREWARDED_RELAYER_ENTRIES_AT_INBOUND_LANE as _,
)
.unwrap_or(u32::MAX);
pallet_message_lane::ensure_able_to_receive_confirmation::<Weights>(
bp_rialto::max_extrinsic_size(),
bp_rialto::max_extrinsic_weight(),
max_incoming_inbound_lane_data_proof_size,
bp_millau::MAX_UNREWARDED_RELAYER_ENTRIES_AT_INBOUND_LANE,
bp_millau::MAX_UNCONFIRMED_MESSAGES_AT_INBOUND_LANE,
bridge_runtime_common::messages::transaction_weight_without_multiplier(
bp_rialto::BlockWeights::get().get(DispatchClass::Normal).base_extrinsic,
max_incoming_inbound_lane_data_proof_size as _,
0,
),
);
}

#[test]
Expand Down
23 changes: 13 additions & 10 deletions bridges/bin/rialto/runtime/src/millau_messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,10 @@ pub type FromMillauMessageDispatch = messages::target::FromBridgedChainMessageDi
>;

/// Messages proof for Millau -> Rialto messages.
pub type FromMillauMessagesProof = messages::target::FromBridgedChainMessagesProof<WithMillauMessageBridge>;
pub type FromMillauMessagesProof = messages::target::FromBridgedChainMessagesProof<bp_millau::Hash>;

/// Messages delivery proof for Rialto -> Millau messages.
pub type FromMillauMessagesDeliveryProof =
messages::source::FromBridgedChainMessagesDeliveryProof<WithMillauMessageBridge>;
pub type ToMillauMessagesDeliveryProof = messages::source::FromBridgedChainMessagesDeliveryProof<bp_millau::Hash>;

/// Millau <-> Rialto message bridge.
#[derive(RuntimeDebug, Clone, Copy)]
Expand All @@ -98,7 +97,7 @@ impl MessageBridge for WithMillauMessageBridge {

fn weight_limits_of_message_on_bridged_chain(message_payload: &[u8]) -> RangeInclusive<Weight> {
// we don't want to relay too large messages + keep reserve for future upgrades
let upper_limit = bp_millau::max_extrinsic_weight() / 2;
let upper_limit = messages::target::maximal_incoming_message_dispatch_weight(bp_millau::max_extrinsic_weight());

// given Millau chain parameters (`TransactionByteFee`, `WeightToFee`, `FeeMultiplierUpdate`),
// the minimal weight of the message may be computed as message.length()
Expand All @@ -110,13 +109,17 @@ impl MessageBridge for WithMillauMessageBridge {
}

fn weight_of_delivery_transaction(message_payload: &[u8]) -> Weight {
let message_payload_len = u32::try_from(message_payload.len())
.map(Into::into)
.unwrap_or(Weight::MAX);
let extra_bytes_in_payload =
message_payload_len.saturating_sub(pallet_message_lane::EXPECTED_DEFAULT_MESSAGE_LENGTH.into());
messages::transaction_weight_without_multiplier(
bp_millau::BlockWeights::get().get(DispatchClass::Normal).base_extrinsic,
u32::try_from(message_payload.len())
.map(Into::into)
.unwrap_or(Weight::MAX)
.saturating_add(bp_rialto::EXTRA_STORAGE_PROOF_SIZE as _),
bp_millau::MAX_SINGLE_MESSAGE_DELIVERY_TX_WEIGHT,
message_payload_len.saturating_add(bp_rialto::EXTRA_STORAGE_PROOF_SIZE as _),
extra_bytes_in_payload
.saturating_mul(bp_millau::ADDITIONAL_MESSAGE_BYTE_DELIVERY_WEIGHT)
.saturating_add(bp_millau::MAX_SINGLE_MESSAGE_DELIVERY_TX_WEIGHT),
)
}

Expand Down Expand Up @@ -186,7 +189,7 @@ impl TargetHeaderChain<ToMillauMessagePayload, bp_millau::AccountId> for Millau
// - hash of the header this proof has been created with;
// - the storage proof of one or several keys;
// - id of the lane we prove state of.
type MessagesDeliveryProof = FromMillauMessagesDeliveryProof;
type MessagesDeliveryProof = ToMillauMessagesDeliveryProof;

fn verify_message(payload: &ToMillauMessagePayload) -> Result<(), Self::Error> {
messages::source::verify_chain_message::<WithMillauMessageBridge>(payload)
Expand Down
Loading

0 comments on commit 56a951d

Please sign in to comment.