Reward relayers for dispatching messages (#385)

* reward relayers for dispatching messages

* clippy

* Update modules/message-lane/src/lib.rs

Co-authored-by: Hernando Castano <HCastano@users.noreply.github.com>

* added comment

* Update modules/message-lane/src/inbound_lane.rs

Co-authored-by: Tomasz Drwięga <tomusdrw@users.noreply.github.com>

* Update modules/message-lane/src/inbound_lane.rs

Co-authored-by: Tomasz Drwięga <tomusdrw@users.noreply.github.com>

* SubmitterId + RelayerId -> AccountId

* add confirmation_relayer arg to pay_relayer_reward

* cargo fmt --all

* removed verify_and_decode_messages_proof from SourceHeaderChain

* &mut self -> RefCell

* Optimize max messages at inbound lane (#418)

* Add tests for checking messages above max limit

Signed-off-by: MaciejBaj <macie.baj@gmail.com>

* Extend the relayers entry of inbound lane by additional msg nonce

Signed-off-by: MaciejBaj <macie.baj@gmail.com>

* Support additional message nonce from inbound relayers

Signed-off-by: MaciejBaj <macie.baj@gmail.com>

* Code format

Signed-off-by: MaciejBaj <macie.baj@gmail.com>

* Merge messages range for highest relayers

* Change unwrap() to ensure() while accessing relayers

* Edit rustdocs for relayers deque at inbound lane data

Co-authored-by: Hernando Castano <HCastano@users.noreply.github.com>

* Declare additional relayers A & B and use across tests consistently

Signed-off-by: MaciejBaj <macie.baj@gmail.com>

* Remove duplicates and improve naming for inbound lane tests

* Fix test checking max limit per inbound lane

* Correct relayers rewards loop after a proof is received

* Remove redundant check for messages ahead of received range

* Correct grammar at inbound lane tests rustdocs

Co-authored-by: Hernando Castano <HCastano@users.noreply.github.com>

* Improve code quality of relayers updates 💅

Co-authored-by: Tomasz Drwięga <tomusdrw@users.noreply.github.com>

* Test dispatches above max limit from same relayer

Co-authored-by: Hernando Castano <HCastano@users.noreply.github.com>
Co-authored-by: Tomasz Drwięga <tomusdrw@users.noreply.github.com>

* Fix typo.

Co-authored-by: Hernando Castano <HCastano@users.noreply.github.com>
Co-authored-by: Tomasz Drwięga <tomusdrw@users.noreply.github.com>
Co-authored-by: Maciej Baj <macie.baj@gmail.com>
Co-authored-by: Tomasz Drwięga <tomasz@parity.io>
This commit is contained in:
Svyatoslav Nikolsky
2020-10-20 13:12:35 +03:00
committed by Bastian Köcher
parent c42023269a
commit bff930d01e
6 changed files with 649 additions and 81 deletions
+28 -4
View File
@@ -25,7 +25,7 @@
use codec::{Decode, Encode};
use frame_support::RuntimeDebug;
use sp_api::decl_runtime_apis;
use sp_std::prelude::*;
use sp_std::{collections::vec_deque::VecDeque, prelude::*};
pub mod source_chain;
pub mod target_chain;
@@ -70,14 +70,38 @@ pub struct Message<Fee> {
}
/// Inbound lane data.
#[derive(Default, Encode, Decode, Clone, RuntimeDebug, PartialEq)]
pub struct InboundLaneData {
#[derive(Encode, Decode, Clone, RuntimeDebug, PartialEq, Eq)]
pub struct InboundLaneData<RelayerId> {
/// Identifiers of relayers and messages that they have delivered (ordered by message nonce).
/// It is guaranteed to have at most N entries, where N is configured at module level. If
/// there are N entries in this vec, then:
/// 1) all incoming messages are rejected if they're missing corresponding `proof-of(outbound-lane.state)`;
/// 2) all incoming messages are rejected if `proof-of(outbound-lane.state).latest_received_nonce` is
/// equal to `this.latest_confirmed_nonce`.
/// Given what is said above, all nonces in this queue are in range (latest_confirmed_nonce; latest_received_nonce].
///
/// When a relayer sends a single message, both of MessageNonces are the same.
/// When relayer sends messages in a batch, the first arg is the lowest nonce, second arg the highest nonce.
/// Multiple dispatches from the same relayer one are allowed.
pub relayers: VecDeque<(MessageNonce, MessageNonce, RelayerId)>,
/// Nonce of latest message that we have received from bridged chain.
pub latest_received_nonce: MessageNonce,
/// Nonce of latest message that has been confirmed to the bridged chain.
pub latest_confirmed_nonce: MessageNonce,
}
impl<RelayerId> Default for InboundLaneData<RelayerId> {
fn default() -> Self {
InboundLaneData {
relayers: VecDeque::new(),
latest_received_nonce: 0,
latest_confirmed_nonce: 0,
}
}
}
/// Outbound lane data.
#[derive(Encode, Decode, Clone, RuntimeDebug, PartialEq)]
#[derive(Encode, Decode, Clone, RuntimeDebug, PartialEq, Eq)]
pub struct OutboundLaneData {
/// Nonce of oldest message that we haven't yet pruned. May point to not-yet-generated message if
/// all sent messages are already pruned.
@@ -16,7 +16,7 @@
//! Primitives of message lane module, that are used on the source chain.
use crate::{LaneId, MessageNonce};
use crate::{InboundLaneData, LaneId};
use frame_support::Parameter;
use sp_std::fmt::Debug;
@@ -26,7 +26,7 @@ use sp_std::fmt::Debug;
/// All implementations of this trait should only work with finalized data that
/// can't change. Wrong implementation may lead to invalid lane states (i.e. lane
/// that's stuck) and/or processing messages without paying fees.
pub trait TargetHeaderChain<Payload> {
pub trait TargetHeaderChain<Payload, RelayerId> {
/// Error type.
type Error: Debug + Into<&'static str>;
@@ -50,7 +50,7 @@ pub trait TargetHeaderChain<Payload> {
/// Verify messages delivery proof and return lane && nonce of the latest recevied message.
fn verify_messages_delivery_proof(
proof: Self::MessagesDeliveryProof,
) -> Result<(LaneId, MessageNonce), Self::Error>;
) -> Result<(LaneId, InboundLaneData<RelayerId>), Self::Error>;
}
/// Lane message verifier.
@@ -94,4 +94,7 @@ pub trait MessageDeliveryAndDispatchPayment<AccountId, Balance> {
/// Withhold/write-off delivery_and_dispatch_fee from submitter account to
/// some relayers-fund account.
fn pay_delivery_and_dispatch_fee(submitter: &AccountId, fee: &Balance) -> Result<(), Self::Error>;
/// Pay reward for delivering message to the given relayer account.
fn pay_relayer_reward(confirmation_relayer: &AccountId, relayer: &AccountId, reward: &Balance);
}
@@ -16,11 +16,23 @@
//! Primitives of message lane module, that are used on the target chain.
use crate::{Message, MessageData, MessageKey};
use crate::{LaneId, Message, MessageData, MessageKey, OutboundLaneData};
use codec::{Decode, Error as CodecError};
use codec::{Decode, Encode, Error as CodecError};
use frame_support::{weights::Weight, Parameter, RuntimeDebug};
use sp_std::{fmt::Debug, prelude::*};
use sp_std::{collections::btree_map::BTreeMap, fmt::Debug, prelude::*};
/// Proved messages from the source chain.
pub type ProvedMessages<Message> = BTreeMap<LaneId, ProvedLaneMessages<Message>>;
/// Proved messages from single lane of the source chain.
#[derive(RuntimeDebug, Encode, Decode, Clone, PartialEq, Eq)]
pub struct ProvedLaneMessages<Message> {
/// Optional outbound lane state.
pub lane_state: Option<OutboundLaneData>,
/// Messages sent through this lane.
pub messages: Vec<Message>,
}
/// Message data with decoded dispatch payload.
#[derive(RuntimeDebug)]
@@ -49,14 +61,15 @@ pub trait SourceHeaderChain<Fee> {
/// Error type.
type Error: Debug + Into<&'static str>;
/// Proof that messages are sent from source chain.
/// Proof that messages are sent from source chain. This may also include proof
/// of corresponding outbound lane states.
type MessagesProof: Parameter;
/// Verify messages proof and return proved messages.
///
/// Messages vector is required to be sorted by nonce within each lane. Out-of-order
/// messages will be rejected.
fn verify_messages_proof(proof: Self::MessagesProof) -> Result<Vec<Message<Fee>>, Self::Error>;
fn verify_messages_proof(proof: Self::MessagesProof) -> Result<ProvedMessages<Message<Fee>>, Self::Error>;
}
/// Called when inbound message is received.
@@ -79,6 +92,15 @@ pub trait MessageDispatch<Fee> {
fn dispatch(message: DispatchMessage<Self::DispatchPayload, Fee>);
}
impl<Message> Default for ProvedLaneMessages<Message> {
fn default() -> Self {
ProvedLaneMessages {
lane_state: None,
messages: Vec::new(),
}
}
}
impl<DispatchPayload: Decode, Fee> From<Message<Fee>> for DispatchMessage<DispatchPayload, Fee> {
fn from(message: Message<Fee>) -> Self {
DispatchMessage {