Skip to content

Commit

Permalink
Make relayer_reward_per_message field an option (#2632)
Browse files Browse the repository at this point in the history
* relayer_reward_per_message is now Option<RelayerRewardAtSource>

* fixed benchmarks compilation
  • Loading branch information
svyatonik authored Oct 19, 2023
1 parent 349d6a3 commit 469a5a0
Show file tree
Hide file tree
Showing 13 changed files with 129 additions and 67 deletions.
2 changes: 2 additions & 0 deletions bin/millau/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,7 @@ impl pallet_bridge_messages::Config<WithRialtoMessagesInstance> for Runtime {
type DeliveryConfirmationPayments = pallet_bridge_relayers::DeliveryConfirmationPaymentsAdapter<
Runtime,
WithRialtoMessagesInstance,
frame_support::traits::ConstU64<0>,
frame_support::traits::ConstU64<100_000>,
>;
type OnMessagesDelivered = XcmRialtoBridgeHub;
Expand Down Expand Up @@ -511,6 +512,7 @@ impl pallet_bridge_messages::Config<WithRialtoParachainMessagesInstance> for Run
type DeliveryConfirmationPayments = pallet_bridge_relayers::DeliveryConfirmationPaymentsAdapter<
Runtime,
WithRialtoParachainMessagesInstance,
frame_support::traits::ConstU64<0>,
frame_support::traits::ConstU64<100_000>,
>;
type OnMessagesDelivered = XcmRialtoParachainBridgeHub;
Expand Down
1 change: 1 addition & 0 deletions bin/rialto-parachain/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,7 @@ impl pallet_bridge_messages::Config<WithMillauMessagesInstance> for Runtime {
type DeliveryConfirmationPayments = pallet_bridge_relayers::DeliveryConfirmationPaymentsAdapter<
Runtime,
WithMillauMessagesInstance,
frame_support::traits::ConstU128<0>,
frame_support::traits::ConstU128<100_000>,
>;
type OnMessagesDelivered = XcmMillauBridgeHub;
Expand Down
1 change: 1 addition & 0 deletions bin/rialto/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,7 @@ impl pallet_bridge_messages::Config<WithMillauMessagesInstance> for Runtime {
type DeliveryConfirmationPayments = pallet_bridge_relayers::DeliveryConfirmationPaymentsAdapter<
Runtime,
WithMillauMessagesInstance,
frame_support::traits::ConstU128<0>,
frame_support::traits::ConstU128<100_000>,
>;
type OnMessagesDelivered = XcmMillauBridgeHub;
Expand Down
10 changes: 5 additions & 5 deletions modules/messages/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ fn receive_messages<T: Config<I>, I: 'static>(nonce: MessageNonce) {
state: LaneState::Opened,
relayers: vec![UnrewardedRelayer {
relayer: T::bridged_relayer_id(),
messages: DeliveredMessages::new(nonce, 1),
messages: DeliveredMessages::new(nonce, Some(1)),
}]
.into(),
last_confirmed_nonce: 0,
Expand Down Expand Up @@ -368,7 +368,7 @@ mod benchmarks {
state: LaneState::Opened,
relayers: vec![UnrewardedRelayer {
relayer: relayer_id.clone(),
messages: DeliveredMessages::new(1, 1),
messages: DeliveredMessages::new(1, Some(1)),
}]
.into_iter()
.collect(),
Expand Down Expand Up @@ -412,7 +412,7 @@ mod benchmarks {
total_messages: 2,
last_delivered_nonce: 2,
};
let mut delivered_messages = DeliveredMessages::new(1, 1);
let mut delivered_messages = DeliveredMessages::new(1, Some(1));
delivered_messages.note_dispatched_message();
let proof = T::prepare_message_delivery_proof(MessageDeliveryProofParams {
lane: T::bench_lane_id(),
Expand Down Expand Up @@ -472,11 +472,11 @@ mod benchmarks {
relayers: vec![
UnrewardedRelayer {
relayer: relayer1_id.clone(),
messages: DeliveredMessages::new(1, 1),
messages: DeliveredMessages::new(1, Some(1)),
},
UnrewardedRelayer {
relayer: relayer2_id.clone(),
messages: DeliveredMessages::new(2, 1),
messages: DeliveredMessages::new(2, Some(1)),
},
]
.into_iter()
Expand Down
4 changes: 2 additions & 2 deletions modules/messages/src/call_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ mod tests {
messages: DeliveredMessages {
begin: n + 1,
end: n + 1,
relayer_reward_per_message: 0,
relayer_reward_per_message: None,
},
});
}
Expand All @@ -278,7 +278,7 @@ mod tests {
messages: DeliveredMessages {
begin: 1,
end: BridgedChain::MAX_UNCONFIRMED_MESSAGES_IN_CONFIRMATION_TX,
relayer_reward_per_message: 0,
relayer_reward_per_message: None,
},
});
InboundLanes::<TestRuntime>::insert(test_lane_id(), inbound_lane_state);
Expand Down
38 changes: 19 additions & 19 deletions modules/messages/src/inbound_lane.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ impl<S: InboundLaneStorage> InboundLane<S> {
relayer_at_bridged_chain: &S::Relayer,
nonce: MessageNonce,
message_data: DispatchMessageData<Dispatch::DispatchPayload>,
relayer_reward_per_message: RelayerRewardAtSource,
relayer_reward_per_message: Option<RelayerRewardAtSource>,
) -> ReceivalResult<Dispatch::DispatchLevelResult> {
let mut data = self.storage.data();
if Some(nonce) != data.last_delivered_nonce().checked_add(1) {
Expand Down Expand Up @@ -251,7 +251,7 @@ mod tests {
&TEST_RELAYER_A,
nonce,
inbound_message_data(REGULAR_PAYLOAD),
RELAYER_REWARD_PER_MESSAGE,
Some(RELAYER_REWARD_PER_MESSAGE),
),
ReceivalResult::Dispatched(dispatch_result(0))
);
Expand Down Expand Up @@ -379,7 +379,7 @@ mod tests {
&TEST_RELAYER_A,
10,
inbound_message_data(REGULAR_PAYLOAD),
RELAYER_REWARD_PER_MESSAGE,
Some(RELAYER_REWARD_PER_MESSAGE),
),
ReceivalResult::InvalidNonce
);
Expand All @@ -398,7 +398,7 @@ mod tests {
&(TEST_RELAYER_A + current_nonce),
current_nonce,
inbound_message_data(REGULAR_PAYLOAD),
RELAYER_REWARD_PER_MESSAGE,
Some(RELAYER_REWARD_PER_MESSAGE),
),
ReceivalResult::Dispatched(dispatch_result(0))
);
Expand All @@ -409,7 +409,7 @@ mod tests {
&(TEST_RELAYER_A + max_nonce + 1),
max_nonce + 1,
inbound_message_data(REGULAR_PAYLOAD),
RELAYER_REWARD_PER_MESSAGE,
Some(RELAYER_REWARD_PER_MESSAGE),
),
ReceivalResult::TooManyUnrewardedRelayers,
);
Expand All @@ -419,7 +419,7 @@ mod tests {
&(TEST_RELAYER_A + max_nonce),
max_nonce + 1,
inbound_message_data(REGULAR_PAYLOAD),
RELAYER_REWARD_PER_MESSAGE,
Some(RELAYER_REWARD_PER_MESSAGE),
),
ReceivalResult::TooManyUnrewardedRelayers,
);
Expand All @@ -437,7 +437,7 @@ mod tests {
&TEST_RELAYER_A,
current_nonce,
inbound_message_data(REGULAR_PAYLOAD),
RELAYER_REWARD_PER_MESSAGE,
Some(RELAYER_REWARD_PER_MESSAGE),
),
ReceivalResult::Dispatched(dispatch_result(0))
);
Expand All @@ -448,7 +448,7 @@ mod tests {
&TEST_RELAYER_B,
max_nonce + 1,
inbound_message_data(REGULAR_PAYLOAD),
RELAYER_REWARD_PER_MESSAGE,
Some(RELAYER_REWARD_PER_MESSAGE),
),
ReceivalResult::TooManyUnconfirmedMessages,
);
Expand All @@ -458,7 +458,7 @@ mod tests {
&TEST_RELAYER_A,
max_nonce + 1,
inbound_message_data(REGULAR_PAYLOAD),
RELAYER_REWARD_PER_MESSAGE,
Some(RELAYER_REWARD_PER_MESSAGE),
),
ReceivalResult::TooManyUnconfirmedMessages,
);
Expand All @@ -474,7 +474,7 @@ mod tests {
&TEST_RELAYER_A,
1,
inbound_message_data(REGULAR_PAYLOAD),
RELAYER_REWARD_PER_MESSAGE,
Some(RELAYER_REWARD_PER_MESSAGE),
),
ReceivalResult::Dispatched(dispatch_result(0))
);
Expand All @@ -483,7 +483,7 @@ mod tests {
&TEST_RELAYER_B,
2,
inbound_message_data(REGULAR_PAYLOAD),
RELAYER_REWARD_PER_MESSAGE,
Some(RELAYER_REWARD_PER_MESSAGE),
),
ReceivalResult::Dispatched(dispatch_result(0))
);
Expand All @@ -492,7 +492,7 @@ mod tests {
&TEST_RELAYER_A,
3,
inbound_message_data(REGULAR_PAYLOAD),
RELAYER_REWARD_PER_MESSAGE,
Some(RELAYER_REWARD_PER_MESSAGE),
),
ReceivalResult::Dispatched(dispatch_result(0))
);
Expand All @@ -516,7 +516,7 @@ mod tests {
&TEST_RELAYER_A,
1,
inbound_message_data(REGULAR_PAYLOAD),
RELAYER_REWARD_PER_MESSAGE,
Some(RELAYER_REWARD_PER_MESSAGE),
),
ReceivalResult::Dispatched(dispatch_result(0))
);
Expand All @@ -525,7 +525,7 @@ mod tests {
&TEST_RELAYER_A,
2,
inbound_message_data(REGULAR_PAYLOAD),
RELAYER_REWARD_PER_MESSAGE + 1,
Some(RELAYER_REWARD_PER_MESSAGE + 1),
),
ReceivalResult::Dispatched(dispatch_result(0))
);
Expand All @@ -534,15 +534,15 @@ mod tests {
&TEST_RELAYER_A,
3,
inbound_message_data(REGULAR_PAYLOAD),
RELAYER_REWARD_PER_MESSAGE + 1,
Some(RELAYER_REWARD_PER_MESSAGE + 1),
),
ReceivalResult::Dispatched(dispatch_result(0))
);

let mut unrewarded_relayer_with_larger_reward =
unrewarded_relayer(2, 3, TEST_RELAYER_A);
unrewarded_relayer_with_larger_reward.messages.relayer_reward_per_message =
RELAYER_REWARD_PER_MESSAGE + 1;
Some(RELAYER_REWARD_PER_MESSAGE + 1);
assert_eq!(
lane.storage.data().relayers,
vec![
Expand All @@ -562,7 +562,7 @@ mod tests {
&TEST_RELAYER_A,
1,
inbound_message_data(REGULAR_PAYLOAD),
RELAYER_REWARD_PER_MESSAGE,
Some(RELAYER_REWARD_PER_MESSAGE),
),
ReceivalResult::Dispatched(dispatch_result(0))
);
Expand All @@ -571,7 +571,7 @@ mod tests {
&TEST_RELAYER_B,
1,
inbound_message_data(REGULAR_PAYLOAD),
RELAYER_REWARD_PER_MESSAGE,
Some(RELAYER_REWARD_PER_MESSAGE),
),
ReceivalResult::InvalidNonce,
);
Expand All @@ -598,7 +598,7 @@ mod tests {
&TEST_RELAYER_A,
1,
inbound_message_data(payload),
RELAYER_REWARD_PER_MESSAGE,
Some(RELAYER_REWARD_PER_MESSAGE),
),
ReceivalResult::Dispatched(dispatch_result(1))
);
Expand Down
13 changes: 9 additions & 4 deletions modules/messages/src/tests/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,8 +332,11 @@ impl TestDeliveryPayments {
impl DeliveryPayments<AccountId> for TestDeliveryPayments {
type Error = &'static str;

fn relayer_reward_per_message(_lane_id: LaneId, _relayer: &AccountId) -> RelayerRewardAtSource {
RELAYER_REWARD_PER_MESSAGE
fn relayer_reward_per_message(
_lane_id: LaneId,
_relayer: &AccountId,
) -> Option<RelayerRewardAtSource> {
Some(RELAYER_REWARD_PER_MESSAGE)
}

fn pay_reward(
Expand Down Expand Up @@ -373,7 +376,9 @@ impl DeliveryConfirmationPayments<AccountId> for TestDeliveryConfirmationPayment
let relayers_rewards = calc_relayers_rewards_at_source::<AccountId, Balance>(
messages_relayers,
received_range,
|messages, relayer_reward_per_message| messages * relayer_reward_per_message,
|messages, relayer_reward_per_message| {
messages * relayer_reward_per_message.unwrap_or(0)
},
);
let rewarded_relayers = relayers_rewards.len();
for (relayer, reward) in &relayers_rewards {
Expand Down Expand Up @@ -481,7 +486,7 @@ pub fn unrewarded_relayer(
messages: DeliveredMessages {
begin,
end,
relayer_reward_per_message: RELAYER_REWARD_PER_MESSAGE,
relayer_reward_per_message: Some(RELAYER_REWARD_PER_MESSAGE),
},
}
}
Expand Down
18 changes: 11 additions & 7 deletions modules/messages/src/tests/pallet_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ fn receive_messages_delivery_proof() {
last_confirmed_nonce: 1,
relayers: vec![UnrewardedRelayer {
relayer: 0,
messages: DeliveredMessages::new(1, 0),
messages: DeliveredMessages::new(1, None),
}]
.into(),
},
Expand Down Expand Up @@ -263,7 +263,7 @@ fn receive_messages_proof_works() {
.0
.relayers
.front()
.map(|r| r.messages.relayer_reward_per_message),
.and_then(|r| r.messages.relayer_reward_per_message),
Some(RELAYER_REWARD_PER_MESSAGE),
);

Expand Down Expand Up @@ -855,7 +855,7 @@ fn proof_size_refund_from_receive_messages_proof_works() {
messages: DeliveredMessages {
begin: 0,
end: 100,
relayer_reward_per_message: 0
relayer_reward_per_message: None,
}
};
max_entries
Expand Down Expand Up @@ -888,7 +888,7 @@ fn proof_size_refund_from_receive_messages_proof_works() {
messages: DeliveredMessages {
begin: 0,
end: 100,
relayer_reward_per_message: 0
relayer_reward_per_message: None,
}
};
max_entries - 1
Expand Down Expand Up @@ -1002,7 +1002,7 @@ fn test_bridge_messages_call_is_correctly_defined() {
last_confirmed_nonce: 1,
relayers: vec![UnrewardedRelayer {
relayer: 0,
messages: DeliveredMessages::new(1, 0),
messages: DeliveredMessages::new(1, None),
}]
.into(),
},
Expand Down Expand Up @@ -1065,7 +1065,11 @@ fn inbound_storage_extra_proof_size_bytes_works() {
fn relayer_entry() -> UnrewardedRelayer<TestRelayer> {
UnrewardedRelayer {
relayer: 42u64,
messages: DeliveredMessages { begin: 0, end: 100, relayer_reward_per_message: 0 },
messages: DeliveredMessages {
begin: 0,
end: 100,
relayer_reward_per_message: Some(42),
},
}
}

Expand Down Expand Up @@ -1161,7 +1165,7 @@ fn receive_messages_delivery_proof_fails_if_outbound_lane_is_unknown() {
last_confirmed_nonce: 1,
relayers: vec![UnrewardedRelayer {
relayer: 0,
messages: DeliveredMessages::new(1, 0),
messages: DeliveredMessages::new(1, None),
}]
.into(),
},
Expand Down
2 changes: 1 addition & 1 deletion modules/relayers/src/extension/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1900,7 +1900,7 @@ mod tests {
messages: DeliveredMessages {
begin: 1,
end: best_delivered_message,
relayer_reward_per_message: 0,
relayer_reward_per_message: None,
},
}]
.into(),
Expand Down
3 changes: 3 additions & 0 deletions modules/relayers/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ pub const TEST_BRIDGED_CHAIN_ID: ChainId = *b"brdg";
/// Maximal extrinsic size at the `BridgedChain`.
pub const BRIDGED_CHAIN_MAX_EXTRINSIC_SIZE: u32 = 1024;

/// Default reward that is paid to relayer for delivering a single message.
pub const DEFAULT_REWARD_PER_MESSAGE: ThisChainBalance = 100_000;
/// Maximal reward that may be paid to relayer for delivering a single message.
pub const MAX_REWARD_PER_MESSAGE: ThisChainBalance = 100_000;

Expand Down Expand Up @@ -284,6 +286,7 @@ pub type TestDeliveryConfirmationPaymentsAdapter =
pallet_bridge_relayers::DeliveryConfirmationPaymentsAdapter<
TestRuntime,
(),
ConstU64<DEFAULT_REWARD_PER_MESSAGE>,
ConstU64<MAX_REWARD_PER_MESSAGE>,
>;

Expand Down
Loading

0 comments on commit 469a5a0

Please sign in to comment.