Skip to content

Commit

Permalink
Changed delivery and dispatch fee computation methods (paritytech#795)
Browse files Browse the repository at this point in the history
* removed weight <-> fee mess

* updated documentation

Co-authored-by: Hernando Castano <castano.ha@gmail.com>
  • Loading branch information
2 people authored and serban300 committed Apr 9, 2024
1 parent dec54af commit d687a61
Show file tree
Hide file tree
Showing 19 changed files with 440 additions and 357 deletions.
10 changes: 0 additions & 10 deletions bridges/bin/millau/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -667,11 +667,6 @@ mod tests {
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()),
);

Expand All @@ -686,11 +681,6 @@ mod tests {
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,
),
);
}
}
131 changes: 74 additions & 57 deletions bridges/bin/millau/runtime/src/rialto_messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ use bp_message_lane::{
InboundLaneData, LaneId, Message, MessageNonce, Parameter as MessageLaneParameter,
};
use bp_runtime::{InstanceId, RIALTO_BRIDGE_INSTANCE};
use bridge_runtime_common::messages::{self, ChainWithMessageLanes, MessageBridge};
use bridge_runtime_common::messages::{self, ChainWithMessageLanes, MessageBridge, MessageLaneTransaction};
use codec::{Decode, Encode};
use frame_support::{
parameter_types,
weights::{DispatchClass, Weight, WeightToFeePolynomial},
weights::{DispatchClass, Weight},
RuntimeDebug,
};
use sp_core::storage::StorageKey;
Expand Down Expand Up @@ -99,59 +99,6 @@ impl MessageBridge for WithRialtoMessageBridge {
type ThisChain = Millau;
type BridgedChain = Rialto;

fn maximal_extrinsic_size_on_target_chain() -> u32 {
bp_rialto::max_extrinsic_size()
}

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 = messages::target::maximal_incoming_message_dispatch_weight(bp_rialto::max_extrinsic_weight());

// we're charging for payload bytes in `WithRialtoMessageBridge::weight_of_delivery_transaction` function
//
// this bridge may be used to deliver all kind of messages, so we're not making any assumptions about
// minimal dispatch weight here

0..=upper_limit
}

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_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::DEFAULT_MESSAGE_DELIVERY_TX_WEIGHT),
)
}

fn weight_of_delivery_confirmation_transaction_on_this_chain() -> Weight {
let inbounded_data_size: Weight =
InboundLaneData::<bp_rialto::AccountId>::encoded_size_hint(bp_rialto::MAXIMAL_ENCODED_ACCOUNT_ID_SIZE, 1)
.map(Into::into)
.unwrap_or(Weight::MAX);

messages::transaction_weight_without_multiplier(
bp_millau::BlockWeights::get().get(DispatchClass::Normal).base_extrinsic,
inbounded_data_size.saturating_add(bp_rialto::EXTRA_STORAGE_PROOF_SIZE as _),
bp_millau::MAX_SINGLE_MESSAGE_DELIVERY_CONFIRMATION_TX_WEIGHT,
)
}

fn this_weight_to_this_balance(weight: Weight) -> bp_millau::Balance {
<crate::Runtime as pallet_transaction_payment::Config>::WeightToFee::calc(&weight)
}

fn bridged_weight_to_bridged_balance(weight: Weight) -> bp_rialto::Balance {
// we're using the same weights in both chains now
<crate::Runtime as pallet_transaction_payment::Config>::WeightToFee::calc(&weight) as _
}

fn bridged_balance_to_this_balance(bridged_balance: bp_rialto::Balance) -> bp_millau::Balance {
bp_millau::Balance::try_from(RialtoToMillauConversionRate::get().saturating_mul_int(bridged_balance))
.unwrap_or(bp_millau::Balance::MAX)
Expand All @@ -167,21 +114,45 @@ impl messages::ChainWithMessageLanes for Millau {
type AccountId = bp_millau::AccountId;
type Signer = bp_millau::AccountSigner;
type Signature = bp_millau::Signature;
type Call = crate::Call;
type Weight = Weight;
type Balance = bp_millau::Balance;

type MessageLaneInstance = pallet_message_lane::DefaultInstance;
}

impl messages::ThisChainWithMessageLanes for Millau {
type Call = crate::Call;

fn is_outbound_lane_enabled(lane: &LaneId) -> bool {
*lane == LaneId::default()
}

fn maximal_pending_messages_at_outbound_lane() -> MessageNonce {
MessageNonce::MAX
}

fn estimate_delivery_confirmation_transaction() -> MessageLaneTransaction<Weight> {
let inbound_data_size =
InboundLaneData::<bp_millau::AccountId>::encoded_size_hint(bp_millau::MAXIMAL_ENCODED_ACCOUNT_ID_SIZE, 1)
.unwrap_or(u32::MAX);

MessageLaneTransaction {
dispatch_weight: bp_millau::MAX_SINGLE_MESSAGE_DELIVERY_CONFIRMATION_TX_WEIGHT,
size: inbound_data_size
.saturating_add(bp_rialto::EXTRA_STORAGE_PROOF_SIZE)
.saturating_add(bp_millau::TX_EXTRA_BYTES),
}
}

fn transaction_payment(transaction: MessageLaneTransaction<Weight>) -> bp_millau::Balance {
// in our testnets, both per-byte fee and weight-to-fee are 1:1
messages::transaction_payment_without_multiplier(
bp_millau::BlockWeights::get().get(DispatchClass::Normal).base_extrinsic,
1,
|weight| weight as _,
transaction,
)
}
}

/// Rialto chain from message lane point of view.
Expand All @@ -193,13 +164,59 @@ impl messages::ChainWithMessageLanes for Rialto {
type AccountId = bp_rialto::AccountId;
type Signer = bp_rialto::AccountSigner;
type Signature = bp_rialto::Signature;
type Call = (); // unknown to us
type Weight = Weight;
type Balance = bp_rialto::Balance;

type MessageLaneInstance = pallet_message_lane::DefaultInstance;
}

impl messages::BridgedChainWithMessageLanes for Rialto {
fn maximal_extrinsic_size() -> u32 {
bp_rialto::max_extrinsic_size()
}

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

// we're charging for payload bytes in `WithRialtoMessageBridge::transaction_payment` function
//
// this bridge may be used to deliver all kind of messages, so we're not making any assumptions about
// minimal dispatch weight here

0..=upper_limit
}

fn estimate_delivery_transaction(
message_payload: &[u8],
message_dispatch_weight: Weight,
) -> MessageLaneTransaction<Weight> {
let message_payload_len = u32::try_from(message_payload.len()).unwrap_or(u32::MAX);
let extra_bytes_in_payload = Weight::from(message_payload_len)
.saturating_sub(pallet_message_lane::EXPECTED_DEFAULT_MESSAGE_LENGTH.into());

MessageLaneTransaction {
dispatch_weight: extra_bytes_in_payload
.saturating_mul(bp_rialto::ADDITIONAL_MESSAGE_BYTE_DELIVERY_WEIGHT)
.saturating_add(bp_rialto::DEFAULT_MESSAGE_DELIVERY_TX_WEIGHT)
.saturating_add(message_dispatch_weight),
size: message_payload_len
.saturating_add(bp_millau::EXTRA_STORAGE_PROOF_SIZE)
.saturating_add(bp_rialto::TX_EXTRA_BYTES),
}
}

fn transaction_payment(transaction: MessageLaneTransaction<Weight>) -> bp_rialto::Balance {
// in our testnets, both per-byte fee and weight-to-fee are 1:1
messages::transaction_payment_without_multiplier(
bp_rialto::BlockWeights::get().get(DispatchClass::Normal).base_extrinsic,
1,
|weight| weight as _,
transaction,
)
}
}

impl TargetHeaderChain<ToRialtoMessagePayload, bp_rialto::AccountId> for Rialto {
type Error = &'static str;
// The proof is:
Expand Down
10 changes: 0 additions & 10 deletions bridges/bin/rialto/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1096,11 +1096,6 @@ mod tests {
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()),
);

Expand All @@ -1115,11 +1110,6 @@ mod tests {
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,
),
);
}

Expand Down
Loading

0 comments on commit d687a61

Please sign in to comment.