Skip to content

Commit

Permalink
Additional priority boost for relayers that have explicitly registere…
Browse files Browse the repository at this point in the history
…d at lane (#2597)

* compute_per_lane_priority_boost to discuss new approach

* clippy

* fix incorrect merge
  • Loading branch information
svyatonik authored and bkontur committed Jun 20, 2024
1 parent c078b5b commit 682ca11
Show file tree
Hide file tree
Showing 9 changed files with 214 additions and 58 deletions.
7 changes: 7 additions & 0 deletions bridges/bin/runtime-common/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ use pallet_transaction_payment::Multiplier;
use sp_runtime::{
testing::H256,
traits::{BlakeTwo256, ConstU32, ConstU64, ConstU8},
transaction_validity::TransactionPriority,
FixedPointNumber, Perquintill, StateVersion,
};

Expand Down Expand Up @@ -124,6 +125,8 @@ parameter_types! {
pub MinimumMultiplier: Multiplier = Multiplier::saturating_from_rational(1, 1_000_000u128);
pub MaximumMultiplier: Multiplier = sp_runtime::traits::Bounded::max_value();
pub const ReserveId: [u8; 8] = *b"brdgrlrs";
pub SlotLength: u32 = 16;
pub PriorityBoostForActiveLaneRelayer: TransactionPriority = 1;
}

#[derive_impl(frame_system::config_preludes::TestDefaultConfig)]
Expand Down Expand Up @@ -212,6 +215,10 @@ impl pallet_bridge_relayers::Config for TestRuntime {
type Reward = ThisChainBalance;
type PaymentProcedure = TestPaymentProcedure;
type StakeAndSlash = TestStakeAndSlash;
type MaxRelayersPerLane = ConstU32<16>;
type SlotLength = SlotLength;
type PriorityBoostPerItem = ConstU64<1>;
type PriorityBoostForActiveLaneRelayer = PriorityBoostForActiveLaneRelayer;
type WeightInfo = ();
}

Expand Down
12 changes: 2 additions & 10 deletions bridges/modules/relayers/src/extension/grandpa_adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ use pallet_bridge_messages::{
CallSubType as BridgeMessagesCallSubType, Config as BridgeMessagesConfig,
};
use sp_runtime::{
traits::{Dispatchable, Get},
transaction_validity::{TransactionPriority, TransactionValidityError},
Saturating,
traits::Dispatchable, transaction_validity::TransactionValidityError, Saturating,
};
use sp_std::marker::PhantomData;

Expand All @@ -54,21 +52,17 @@ pub struct WithGrandpaChainExtensionConfig<
BridgeGrandpaPalletInstance,
// instance of BridgedChain `pallet-bridge-messages`, tracked by this extension
BridgeMessagesPalletInstance,
// message delivery transaction priority boost for every additional message
PriorityBoostPerMessage,
>(
PhantomData<(
IdProvider,
Runtime,
BatchCallUnpacker,
BridgeGrandpaPalletInstance,
BridgeMessagesPalletInstance,
PriorityBoostPerMessage,
)>,
);

impl<ID, R, BCU, GI, MI, P> ExtensionConfig
for WithGrandpaChainExtensionConfig<ID, R, BCU, GI, MI, P>
impl<ID, R, BCU, GI, MI> ExtensionConfig for WithGrandpaChainExtensionConfig<ID, R, BCU, GI, MI>
where
ID: StaticStrProvider,
R: BridgeRelayersConfig
Expand All @@ -77,15 +71,13 @@ where
BCU: BatchCallUnpacker<R>,
GI: 'static,
MI: 'static,
P: Get<TransactionPriority>,
R::RuntimeCall: Dispatchable<Info = DispatchInfo, PostInfo = PostDispatchInfo>
+ BridgeGrandpaCallSubtype<R, GI>
+ BridgeMessagesCallSubType<R, MI>,
{
type IdProvider = ID;
type Runtime = R;
type BridgeMessagesPalletInstance = MI;
type PriorityBoostPerMessage = P;
type Reward = R::Reward;
type RemoteGrandpaChainBlockNumber = pallet_bridge_grandpa::BridgedBlockNumber<R, GI>;

Expand Down
1 change: 0 additions & 1 deletion bridges/modules/relayers/src/extension/messages_adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ where
type IdProvider = ID;
type Runtime = R;
type BridgeMessagesPalletInstance = MI;
type PriorityBoostPerMessage = P;
type Reward = R::Reward;
type RemoteGrandpaChainBlockNumber = ();

Expand Down
39 changes: 21 additions & 18 deletions bridges/modules/relayers/src/extension/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ use frame_support::{
dispatch::{DispatchInfo, PostDispatchInfo},
CloneNoBound, DefaultNoBound, EqNoBound, PartialEqNoBound, RuntimeDebugNoBound,
};
use frame_system::Config as SystemConfig;
use frame_system::{pallet_prelude::BlockNumberFor, Config as SystemConfig};
use pallet_bridge_messages::{CallHelper as MessagesCallHelper, Config as BridgeMessagesConfig};
use pallet_transaction_payment::{
Config as TransactionPaymentConfig, OnChargeTransaction, Pallet as TransactionPaymentPallet,
Expand Down Expand Up @@ -125,6 +125,7 @@ where
R::RuntimeCall: Dispatchable<Info = DispatchInfo, PostInfo = PostDispatchInfo>,
<R as TransactionPaymentConfig>::OnChargeTransaction:
OnChargeTransaction<R, Balance = R::Reward>,
usize: TryFrom<BlockNumberFor<R>>,
{
/// Returns number of bundled messages `Some(_)`, if the given call info is a:
///
Expand All @@ -136,16 +137,15 @@ where
/// virtually boosted. The relayer registration (we only boost priority for registered
/// relayer transactions) must be checked outside.
fn bundled_messages_for_priority_boost(
call_info: Option<&ExtensionCallInfo<C::RemoteGrandpaChainBlockNumber>>,
call_info: &ExtensionCallInfo<C::RemoteGrandpaChainBlockNumber>,
) -> Option<MessageNonce> {
// we only boost priority of message delivery transactions
let parsed_call = match call_info {
Some(parsed_call) if parsed_call.is_receive_messages_proof_call() => parsed_call,
_ => return None,
};
if !call_info.is_receive_messages_proof_call() {
return None
}

// compute total number of messages in transaction
let bundled_messages = parsed_call.messages_call_info().bundled_messages().saturating_len();
let bundled_messages = call_info.messages_call_info().bundled_messages().saturating_len();

// a quick check to avoid invalid high-priority transactions
let max_unconfirmed_messages_in_confirmation_tx = <R as BridgeMessagesConfig<C::BridgeMessagesPalletInstance>>::BridgedChain
Expand Down Expand Up @@ -197,8 +197,7 @@ where
//
// - when relayer is registered after `validate` is called and priority is not boosted:
// relayer should be ready for slashing after registration.
let may_slash_relayer =
Self::bundled_messages_for_priority_boost(Some(&call_info)).is_some();
let may_slash_relayer = Self::bundled_messages_for_priority_boost(&call_info).is_some();
let slash_relayer_if_delivery_result = may_slash_relayer
.then(|| RelayerAccountAction::Slash(relayer.clone(), reward_account_params))
.unwrap_or(RelayerAccountAction::None);
Expand Down Expand Up @@ -273,6 +272,7 @@ where
R::RuntimeCall: Dispatchable<Info = DispatchInfo, PostInfo = PostDispatchInfo>,
<R as TransactionPaymentConfig>::OnChargeTransaction:
OnChargeTransaction<R, Balance = R::Reward>,
usize: TryFrom<BlockNumberFor<R>>,
{
const IDENTIFIER: &'static str = C::IdProvider::STR;
type AccountId = R::AccountId;
Expand All @@ -296,13 +296,15 @@ where
// we're not calling `validate` from `pre_dispatch` directly because of performance
// reasons, so if you're adding some code that may fail here, please check if it needs
// to be added to the `pre_dispatch` as well
let parsed_call = C::parse_and_check_for_obsolete_call(call)?;
let parsed_call = match C::parse_and_check_for_obsolete_call(call)? {
Some(parsed_call) => parsed_call,
None => return Ok(Default::default()),
};

// the following code just plays with transaction priority and never returns an error

// we only boost priority of presumably correct message delivery transactions
let bundled_messages = match Self::bundled_messages_for_priority_boost(parsed_call.as_ref())
{
let bundled_messages = match Self::bundled_messages_for_priority_boost(&parsed_call) {
Some(bundled_messages) => bundled_messages,
None => return Ok(Default::default()),
};
Expand All @@ -313,16 +315,19 @@ where
}

// compute priority boost
let priority_boost =
priority::compute_priority_boost::<C::PriorityBoostPerMessage>(bundled_messages);
let priority_boost = priority::compute_priority_boost::<R>(
parsed_call.messages_call_info().lane_id(),
bundled_messages,
who,
);
let valid_transaction = ValidTransactionBuilder::default().priority(priority_boost);

log::trace!(
target: LOG_TARGET,
"{}.{:?}: has boosted priority of message delivery transaction \
of relayer {:?}: {} messages -> {} priority",
Self::IDENTIFIER,
parsed_call.as_ref().map(|p| p.messages_call_info().lane_id()),
parsed_call.messages_call_info().lane_id(),
who,
bundled_messages,
priority_boost,
Expand Down Expand Up @@ -447,7 +452,7 @@ mod tests {
use pallet_bridge_parachains::{Call as ParachainsCall, Pallet as ParachainsPallet};
use pallet_utility::Call as UtilityCall;
use sp_runtime::{
traits::{ConstU64, Header as HeaderT},
traits::Header as HeaderT,
transaction_validity::{InvalidTransaction, ValidTransaction},
DispatchError,
};
Expand Down Expand Up @@ -475,7 +480,6 @@ mod tests {
RuntimeWithUtilityPallet<TestRuntime>,
(),
(),
ConstU64<1>,
>;
type TestGrandpaExtension =
BridgeRelayersSignedExtension<TestRuntime, TestGrandpaExtensionConfig>;
Expand All @@ -485,7 +489,6 @@ mod tests {
RuntimeWithUtilityPallet<TestRuntime>,
(),
(),
ConstU64<1>,
>;
type TestExtension = BridgeRelayersSignedExtension<TestRuntime, TestExtensionConfig>;

Expand Down
12 changes: 2 additions & 10 deletions bridges/modules/relayers/src/extension/parachain_adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,7 @@ use pallet_bridge_parachains::{
CallSubType as BridgeParachainsCallSubtype, Config as BridgeParachainsConfig,
SubmitParachainHeadsHelper,
};
use sp_runtime::{
traits::{Dispatchable, Get},
transaction_validity::{TransactionPriority, TransactionValidityError},
};
use sp_runtime::{traits::Dispatchable, transaction_validity::TransactionValidityError};
use sp_std::marker::PhantomData;

/// Adapter to be used in signed extension configuration, when bridging with remote parachains.
Expand All @@ -58,20 +55,17 @@ pub struct WithParachainExtensionConfig<
BridgeParachainsPalletInstance,
// instance of BridgedChain `pallet-bridge-messages`, tracked by this extension
BridgeMessagesPalletInstance,
// message delivery transaction priority boost for every additional message
PriorityBoostPerMessage,
>(
PhantomData<(
IdProvider,
Runtime,
BatchCallUnpacker,
BridgeParachainsPalletInstance,
BridgeMessagesPalletInstance,
PriorityBoostPerMessage,
)>,
);

impl<ID, R, BCU, PI, MI, P> ExtensionConfig for WithParachainExtensionConfig<ID, R, BCU, PI, MI, P>
impl<ID, R, BCU, PI, MI> ExtensionConfig for WithParachainExtensionConfig<ID, R, BCU, PI, MI>
where
ID: StaticStrProvider,
R: BridgeRelayersConfig
Expand All @@ -81,7 +75,6 @@ where
BCU: BatchCallUnpacker<R>,
PI: 'static,
MI: 'static,
P: Get<TransactionPriority>,
R::RuntimeCall: Dispatchable<Info = DispatchInfo, PostInfo = PostDispatchInfo>
+ BridgeGrandpaCallSubtype<R, R::BridgesGrandpaPalletInstance>
+ BridgeParachainsCallSubtype<R, PI>
Expand All @@ -91,7 +84,6 @@ where
type IdProvider = ID;
type Runtime = R;
type BridgeMessagesPalletInstance = MI;
type PriorityBoostPerMessage = P;
type Reward = R::Reward;
type RemoteGrandpaChainBlockNumber =
pallet_bridge_grandpa::BridgedBlockNumber<R, R::BridgesGrandpaPalletInstance>;
Expand Down
Loading

0 comments on commit 682ca11

Please sign in to comment.