Skip to content

Commit

Permalink
Fees, weights, message delivery and dispatch (paritytech#339)
Browse files Browse the repository at this point in the history
* introduce BridgedHeaderChain trait

* LaneMessageVerifier + tests

* fixed tests

* do not expose intenal functions

* cargo fmt --all + fix no_std compilation

* ByWeightDispatcher

* process queued messages from message-lane::on_initialize

* scheduled_messages_are_processed_from_on_initialize

* flush

* deal with fees + weights

* drop heavy messages on dispatch

* cargo fmt

* clippy

* fix comment

* Update primitives/message-lane/src/source_chain.rs

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

* removed messages_processed

* Update primitives/message-lane/src/source_chain.rs

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

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

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

* remove queueing from message-lane

* also remove queueing from RPCs

* remove by-weight traces

* dispatch fee

* receiving -> delivery

* receival -> delivery

* remove extra line

* Update primitives/message-lane/src/source_chain.rs

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

* cargo fmt --all

* clippy

* let dispatch_weight to be larger than actual_dispatch_weight

* post-merge fix

Co-authored-by: Tomasz Drwięga <tomusdrw@users.noreply.github.com>
Co-authored-by: Hernando Castano <HCastano@users.noreply.github.com>
  • Loading branch information
3 people authored Sep 25, 2020
1 parent 60b4735 commit 9493810
Show file tree
Hide file tree
Showing 13 changed files with 802 additions and 144 deletions.
22 changes: 11 additions & 11 deletions bridges/modules/call-dispatch/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,6 @@ use sp_std::{marker::PhantomData, prelude::*};
/// Spec version type.
pub type SpecVersion = u32;

// TODO: update me (https://github.com/paritytech/parity-bridges-common/issues/78)
/// Weight of single deposit_event() call.
const DEPOSIT_EVENT_WEIGHT: Weight = 0;

/// Origin of the call on the target chain.
#[derive(RuntimeDebug, Encode, Decode, Clone)]
pub enum CallOrigin<SourceChainAccountPublic, TargetChainAccountPublic, TargetChainSignature> {
Expand Down Expand Up @@ -149,7 +145,11 @@ impl<T: Trait<I>, I: Instance> MessageDispatch<T::MessageId> for Module<T, I> {
<T as Trait<I>>::Call,
>;

fn dispatch(bridge: InstanceId, id: T::MessageId, message: Self::Message) -> Weight {
fn dispatch_weight(message: &Self::Message) -> Weight {
message.weight
}

fn dispatch(bridge: InstanceId, id: T::MessageId, message: Self::Message) {
// verify spec version
// (we want it to be the same, because otherwise we may decode Call improperly)
let expected_version = <T as frame_system::Trait>::Version::get().spec_version;
Expand All @@ -167,7 +167,7 @@ impl<T: Trait<I>, I: Instance> MessageDispatch<T::MessageId> for Module<T, I> {
expected_version,
message.spec_version,
));
return DEPOSIT_EVENT_WEIGHT;
return;
}

// verify weight
Expand All @@ -189,7 +189,7 @@ impl<T: Trait<I>, I: Instance> MessageDispatch<T::MessageId> for Module<T, I> {
expected_weight,
message.weight,
));
return DEPOSIT_EVENT_WEIGHT;
return;
}

// prepare dispatch origin
Expand All @@ -210,7 +210,7 @@ impl<T: Trait<I>, I: Instance> MessageDispatch<T::MessageId> for Module<T, I> {
target_signature,
);
Self::deposit_event(RawEvent::MessageSignatureMismatch(bridge, id));
return DEPOSIT_EVENT_WEIGHT;
return;
}

target_account
Expand All @@ -222,9 +222,11 @@ impl<T: Trait<I>, I: Instance> MessageDispatch<T::MessageId> for Module<T, I> {
let dispatch_result = message.call.dispatch(origin);
let actual_call_weight = extract_actual_weight(&dispatch_result, &dispatch_info);
frame_support::debug::trace!(
"Message {:?}/{:?} has been dispatched. Result: {:?}",
"Message {:?}/{:?} has been dispatched. Weight: {} of {}. Result: {:?}",
bridge,
id,
actual_call_weight,
message.weight,
dispatch_result,
);

Expand All @@ -233,8 +235,6 @@ impl<T: Trait<I>, I: Instance> MessageDispatch<T::MessageId> for Module<T, I> {
id,
dispatch_result.map(drop).map_err(|e| e.error),
));

actual_call_weight + DEPOSIT_EVENT_WEIGHT
}
}

Expand Down
6 changes: 5 additions & 1 deletion bridges/modules/message-lane/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ edition = "2018"
license = "GPL-3.0-or-later WITH Classpath-exception-2.0"

[dependencies]
bp-message-dispatch = { path = "../../primitives/message-dispatch", default-features = false }
bp-message-lane = { path = "../../primitives/message-lane", default-features = false }
bp-runtime = { path = "../../primitives/runtime", default-features = false }
codec = { package = "parity-scale-codec", version = "1.3.1", default-features = false }

# Substrate Based Dependencies
Expand Down Expand Up @@ -48,9 +50,11 @@ git = "https://github.com/paritytech/substrate/"
[features]
default = ["std"]
std = [
"bp-message-dispatch/std",
"bp-message-lane/std",
"bp-runtime/std",
"codec/std",
"frame-support/std",
"frame-system/std",
"sp-std/std"
"sp-std/std",
]
14 changes: 7 additions & 7 deletions bridges/modules/message-lane/rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub type MessagesProof = Bytes;

/// Trie-based storage proof that the message(s) with given key(s) have been received by the bridged chain.
/// SCALE-encoded trie nodes array `Vec<Vec<u8>>`.
pub type MessagesReceivingProof = Bytes;
pub type MessagesDeliveryProof = Bytes;

/// Runtime adapter.
pub trait Runtime: Send + Sync + 'static {
Expand All @@ -63,14 +63,14 @@ pub trait MessageLaneApi<BlockHash> {
block: Option<BlockHash>,
) -> FutureResult<MessagesProof>;

/// Returns proof-of-message(s) receiving.
#[rpc(name = "messageLane_proveMessagesReceiving")]
fn prove_messages_receiving(
/// Returns proof-of-message(s) delivery.
#[rpc(name = "messageLane_proveMessagesDelivery")]
fn prove_messages_delivery(
&self,
instance: InstanceId,
lane: LaneId,
block: Option<BlockHash>,
) -> FutureResult<MessagesReceivingProof>;
) -> FutureResult<MessagesDeliveryProof>;
}

/// Implements the MessageLaneApi trait for interacting with message lanes.
Expand Down Expand Up @@ -119,12 +119,12 @@ where
)
}

fn prove_messages_receiving(
fn prove_messages_delivery(
&self,
instance: InstanceId,
lane: LaneId,
block: Option<Block::Hash>,
) -> FutureResult<MessagesReceivingProof> {
) -> FutureResult<MessagesDeliveryProof> {
Box::new(
prove_keys_read(
self.backend.clone(),
Expand Down
20 changes: 12 additions & 8 deletions bridges/modules/message-lane/src/inbound_lane.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,16 @@

//! Everything about incoming messages receival.

use bp_message_lane::{InboundLaneData, LaneId, Message, MessageKey, MessageNonce, OnMessageReceived};
use bp_message_lane::{
target_chain::MessageDispatch, InboundLaneData, LaneId, Message, MessageData, MessageKey, MessageNonce,
};

/// Inbound lane storage.
pub trait InboundLaneStorage {
/// Message payload.
type Payload;
/// Delivery and dispatch fee type on source chain.
type MessageFee;

/// Lane id.
fn id(&self) -> LaneId;
Expand All @@ -43,10 +47,10 @@ impl<S: InboundLaneStorage> InboundLane<S> {
}

/// Receive new message.
pub fn receive_message<P: OnMessageReceived<S::Payload>>(
pub fn receive_message<P: MessageDispatch<S::Payload, S::MessageFee>>(
&mut self,
nonce: MessageNonce,
payload: S::Payload,
message_data: MessageData<S::Payload, S::MessageFee>,
) -> bool {
let mut data = self.storage.data();
let is_correct_message = nonce == data.latest_received_nonce + 1;
Expand All @@ -57,12 +61,12 @@ impl<S: InboundLaneStorage> InboundLane<S> {
data.latest_received_nonce = nonce;
self.storage.set_data(data);

P::on_message_received(Message {
P::dispatch(Message {
key: MessageKey {
lane_id: self.storage.id(),
nonce,
},
payload,
data: message_data,
});

true
Expand All @@ -74,14 +78,14 @@ mod tests {
use super::*;
use crate::{
inbound_lane,
mock::{run_test, TestRuntime, REGULAR_PAYLOAD, TEST_LANE_ID},
mock::{message_data, run_test, TestMessageDispatch, TestRuntime, REGULAR_PAYLOAD, TEST_LANE_ID},
};

#[test]
fn fails_to_receive_message_with_incorrect_nonce() {
run_test(|| {
let mut lane = inbound_lane::<TestRuntime, _>(TEST_LANE_ID);
assert!(!lane.receive_message::<()>(10, REGULAR_PAYLOAD));
assert!(!lane.receive_message::<TestMessageDispatch>(10, message_data(REGULAR_PAYLOAD)));
assert_eq!(lane.storage.data().latest_received_nonce, 0);
});
}
Expand All @@ -90,7 +94,7 @@ mod tests {
fn correct_message_is_processed_instantly() {
run_test(|| {
let mut lane = inbound_lane::<TestRuntime, _>(TEST_LANE_ID);
assert!(lane.receive_message::<()>(1, REGULAR_PAYLOAD));
assert!(lane.receive_message::<TestMessageDispatch>(1, message_data(REGULAR_PAYLOAD)));
assert_eq!(lane.storage.data().latest_received_nonce, 1);
});
}
Expand Down
Loading

0 comments on commit 9493810

Please sign in to comment.