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 and bkontur committed Jun 14, 2024
1 parent d106d9a commit faed229
Show file tree
Hide file tree
Showing 11 changed files with 126 additions and 67 deletions.
1 change: 1 addition & 0 deletions bridges/bin/runtime-common/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ impl pallet_bridge_messages::Config for TestRuntime {
TestRuntime,
(),
ConstU64<100_000>,
ConstU64<100_000>,
>;
type OnMessagesDelivered = ();

Expand Down
10 changes: 5 additions & 5 deletions bridges/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 bridges/modules/messages/src/call_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ mod tests {
messages: DeliveredMessages {
begin: n + 1,
end: n + 1,
relayer_reward_per_message: 0,
relayer_reward_per_message: None,
},
});
}
Expand All @@ -277,7 +277,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 bridges/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>,
) -> ReceptionResult<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),
),
ReceptionResult::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),
),
ReceptionResult::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),
),
ReceptionResult::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),
),
ReceptionResult::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),
),
ReceptionResult::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),
),
ReceptionResult::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),
),
ReceptionResult::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),
),
ReceptionResult::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),
),
ReceptionResult::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),
),
ReceptionResult::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),
),
ReceptionResult::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),
),
ReceptionResult::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),
),
ReceptionResult::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),
),
ReceptionResult::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),
),
ReceptionResult::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),
),
ReceptionResult::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),
),
ReceptionResult::Dispatched(dispatch_result(1))
);
Expand Down
13 changes: 9 additions & 4 deletions bridges/modules/messages/src/tests/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,8 +296,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 @@ -337,7 +340,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 @@ -463,7 +468,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 bridges/modules/messages/src/tests/pallet_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,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 @@ -266,7 +266,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 @@ -859,7 +859,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 @@ -892,7 +892,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 @@ -1006,7 +1006,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 @@ -1069,7 +1069,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 @@ -1165,7 +1169,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 bridges/modules/relayers/src/extension/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2325,7 +2325,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 bridges/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 @@ -259,6 +261,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 faed229

Please sign in to comment.