Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add reward field to unrewarded relayers #2613

Merged
merged 6 commits into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion modules/messages/src/call_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ mod tests {
for n in 0..BridgedChain::MAX_UNREWARDED_RELAYERS_IN_CONFIRMATION_TX {
inbound_lane_state.relayers.push_back(UnrewardedRelayer {
relayer: Default::default(),
messages: DeliveredMessages { begin: n + 1, end: n + 1 },
messages: DeliveredMessages { begin: n + 1, end: n + 1, reward_per_message: 0 },
});
}
InboundLanes::<TestRuntime>::insert(test_lane_id(), inbound_lane_state);
Expand All @@ -274,6 +274,7 @@ mod tests {
messages: DeliveredMessages {
begin: 1,
end: BridgedChain::MAX_UNCONFIRMED_MESSAGES_IN_CONFIRMATION_TX,
reward_per_message: 0,
},
});
InboundLanes::<TestRuntime>::insert(test_lane_id(), inbound_lane_state);
Expand Down
52 changes: 35 additions & 17 deletions modules/messages/src/inbound_lane.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use crate::{BridgedChainOf, Config};
use bp_messages::{
target_chain::{DispatchMessage, DispatchMessageData, MessageDispatch},
ChainWithMessages, DeliveredMessages, InboundLaneData, LaneId, LaneState, MessageKey,
MessageNonce, OutboundLaneData, ReceivalResult, UnrewardedRelayer,
MessageNonce, OutboundLaneData, ReceivalResult, RewardAtSource, UnrewardedRelayer,
};
use bp_runtime::AccountIdOf;
use codec::{Decode, Encode, EncodeLike, MaxEncodedLen};
Expand Down Expand Up @@ -186,6 +186,7 @@ impl<S: InboundLaneStorage> InboundLane<S> {
relayer_at_bridged_chain: &S::Relayer,
nonce: MessageNonce,
message_data: DispatchMessageData<Dispatch::DispatchPayload>,
delivery_reward_per_message: RewardAtSource,
) -> ReceivalResult<Dispatch::DispatchLevelResult> {
let mut data = self.storage.data();
if Some(nonce) != data.last_delivered_nonce().checked_add(1) {
Expand All @@ -211,13 +212,16 @@ impl<S: InboundLaneStorage> InboundLane<S> {

// now let's update inbound lane storage
match data.relayers.back_mut() {
Some(entry) if entry.relayer == *relayer_at_bridged_chain => {
Some(entry)
if entry.relayer == *relayer_at_bridged_chain &&
entry.messages.reward_per_message == delivery_reward_per_message =>
{
entry.messages.note_dispatched_message();
},
_ => {
data.relayers.push_back(UnrewardedRelayer {
relayer: relayer_at_bridged_chain.clone(),
messages: DeliveredMessages::new(nonce),
messages: DeliveredMessages::new(nonce, delivery_reward_per_message),
});
},
};
Expand Down Expand Up @@ -246,7 +250,8 @@ mod tests {
lane.receive_message::<TestMessageDispatch>(
&TEST_RELAYER_A,
nonce,
inbound_message_data(REGULAR_PAYLOAD)
inbound_message_data(REGULAR_PAYLOAD),
DELIVERY_REWARD_PER_MESSAGE,
),
ReceivalResult::Dispatched(dispatch_result(0))
);
Expand Down Expand Up @@ -373,7 +378,8 @@ mod tests {
lane.receive_message::<TestMessageDispatch>(
&TEST_RELAYER_A,
10,
inbound_message_data(REGULAR_PAYLOAD)
inbound_message_data(REGULAR_PAYLOAD),
DELIVERY_REWARD_PER_MESSAGE,
),
ReceivalResult::InvalidNonce
);
Expand All @@ -391,7 +397,8 @@ mod tests {
lane.receive_message::<TestMessageDispatch>(
&(TEST_RELAYER_A + current_nonce),
current_nonce,
inbound_message_data(REGULAR_PAYLOAD)
inbound_message_data(REGULAR_PAYLOAD),
DELIVERY_REWARD_PER_MESSAGE,
),
ReceivalResult::Dispatched(dispatch_result(0))
);
Expand All @@ -401,7 +408,8 @@ mod tests {
lane.receive_message::<TestMessageDispatch>(
&(TEST_RELAYER_A + max_nonce + 1),
max_nonce + 1,
inbound_message_data(REGULAR_PAYLOAD)
inbound_message_data(REGULAR_PAYLOAD),
DELIVERY_REWARD_PER_MESSAGE,
),
ReceivalResult::TooManyUnrewardedRelayers,
);
Expand All @@ -410,7 +418,8 @@ mod tests {
lane.receive_message::<TestMessageDispatch>(
&(TEST_RELAYER_A + max_nonce),
max_nonce + 1,
inbound_message_data(REGULAR_PAYLOAD)
inbound_message_data(REGULAR_PAYLOAD),
DELIVERY_REWARD_PER_MESSAGE,
),
ReceivalResult::TooManyUnrewardedRelayers,
);
Expand All @@ -427,7 +436,8 @@ mod tests {
lane.receive_message::<TestMessageDispatch>(
&TEST_RELAYER_A,
current_nonce,
inbound_message_data(REGULAR_PAYLOAD)
inbound_message_data(REGULAR_PAYLOAD),
DELIVERY_REWARD_PER_MESSAGE,
),
ReceivalResult::Dispatched(dispatch_result(0))
);
Expand All @@ -437,7 +447,8 @@ mod tests {
lane.receive_message::<TestMessageDispatch>(
&TEST_RELAYER_B,
max_nonce + 1,
inbound_message_data(REGULAR_PAYLOAD)
inbound_message_data(REGULAR_PAYLOAD),
DELIVERY_REWARD_PER_MESSAGE,
),
ReceivalResult::TooManyUnconfirmedMessages,
);
Expand All @@ -446,7 +457,8 @@ mod tests {
lane.receive_message::<TestMessageDispatch>(
&TEST_RELAYER_A,
max_nonce + 1,
inbound_message_data(REGULAR_PAYLOAD)
inbound_message_data(REGULAR_PAYLOAD),
DELIVERY_REWARD_PER_MESSAGE,
),
ReceivalResult::TooManyUnconfirmedMessages,
);
Expand All @@ -461,23 +473,26 @@ mod tests {
lane.receive_message::<TestMessageDispatch>(
&TEST_RELAYER_A,
1,
inbound_message_data(REGULAR_PAYLOAD)
inbound_message_data(REGULAR_PAYLOAD),
DELIVERY_REWARD_PER_MESSAGE,
),
ReceivalResult::Dispatched(dispatch_result(0))
);
assert_eq!(
lane.receive_message::<TestMessageDispatch>(
&TEST_RELAYER_B,
2,
inbound_message_data(REGULAR_PAYLOAD)
inbound_message_data(REGULAR_PAYLOAD),
DELIVERY_REWARD_PER_MESSAGE,
),
ReceivalResult::Dispatched(dispatch_result(0))
);
assert_eq!(
lane.receive_message::<TestMessageDispatch>(
&TEST_RELAYER_A,
3,
inbound_message_data(REGULAR_PAYLOAD)
inbound_message_data(REGULAR_PAYLOAD),
DELIVERY_REWARD_PER_MESSAGE,
),
ReceivalResult::Dispatched(dispatch_result(0))
);
Expand All @@ -500,15 +515,17 @@ mod tests {
lane.receive_message::<TestMessageDispatch>(
&TEST_RELAYER_A,
1,
inbound_message_data(REGULAR_PAYLOAD)
inbound_message_data(REGULAR_PAYLOAD),
DELIVERY_REWARD_PER_MESSAGE,
),
ReceivalResult::Dispatched(dispatch_result(0))
);
assert_eq!(
lane.receive_message::<TestMessageDispatch>(
&TEST_RELAYER_B,
1,
inbound_message_data(REGULAR_PAYLOAD)
inbound_message_data(REGULAR_PAYLOAD),
DELIVERY_REWARD_PER_MESSAGE,
),
ReceivalResult::InvalidNonce,
);
Expand All @@ -534,7 +551,8 @@ mod tests {
lane.receive_message::<TestMessageDispatch>(
&TEST_RELAYER_A,
1,
inbound_message_data(payload)
inbound_message_data(payload),
DELIVERY_REWARD_PER_MESSAGE,
),
ReceivalResult::Dispatched(dispatch_result(1))
);
Expand Down
27 changes: 19 additions & 8 deletions modules/messages/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ use bp_messages::{
DeliveryPayments, DispatchMessage, FromBridgedChainMessagesProof, MessageDispatch,
ProvedLaneMessages, ProvedMessages,
},
ChainWithMessages, DeliveredMessages, InboundLaneData, InboundMessageDetails, LaneId,
MessageKey, MessageNonce, MessagePayload, MessagesOperatingMode, OutboundLaneData,
OutboundMessageDetails, UnrewardedRelayersState, VerificationError,
ChainWithMessages, InboundLaneData, InboundMessageDetails, LaneId, MessageKey, MessageNonce,
MessagePayload, MessagesOperatingMode, OutboundLaneData, OutboundMessageDetails,
UnrewardedRelayersState, VerificationError,
};
use bp_runtime::{
AccountIdOf, BasicOperatingMode, HashOf, OwnedBridgeModule, PreComputedSize, RangeInclusiveExt,
Expand Down Expand Up @@ -268,6 +268,13 @@ pub mod pallet {
}
}

// compute the per-message reward that is paid at the bridged (source) chain to relayer
// that has delivered message
let delivery_reward_per_message = T::DeliveryPayments::delivery_reward_per_message(
lane_id,
&relayer_id_at_this_chain,
);

let mut messages_received_status =
ReceivedMessages::new(lane_id, Vec::with_capacity(lane_data.messages.len()));
for mut message in lane_data.messages {
Expand All @@ -294,6 +301,7 @@ pub mod pallet {
&relayer_id_at_bridged_chain,
message.key.nonce,
message.data,
delivery_reward_per_message,
);

// note that we're returning unspent weight to relayer even if message has been
Expand All @@ -320,6 +328,7 @@ pub mod pallet {

// let's now deal with relayer payments
T::DeliveryPayments::pay_reward(
lane_id,
relayer_id_at_this_chain,
total_messages,
valid_messages,
Expand Down Expand Up @@ -381,12 +390,12 @@ pub mod pallet {
)
.map_err(Error::<T, I>::ReceivalConfirmation)?;

if let Some(confirmed_messages) = confirmed_messages {
if let Some(received_range) = confirmed_messages {
// emit 'delivered' event
let received_range = confirmed_messages.begin..=confirmed_messages.end;
Self::deposit_event(Event::MessagesDelivered {
lane_id,
messages: confirmed_messages,
messages_begin: *received_range.start(),
messages_end: *received_range.end(),
});

// if some new messages have been confirmed, reward relayers
Expand Down Expand Up @@ -452,8 +461,10 @@ pub mod pallet {
MessagesDelivered {
/// Lane for which the delivery has been confirmed.
lane_id: LaneId,
/// Delivered messages.
messages: DeliveredMessages,
/// Nonce of the first delivered message.
messages_begin: MessageNonce,
/// Nonce of the last delivered message.
messages_end: MessageNonce,
},
}

Expand Down
Loading