Skip to content
This repository has been archived by the owner on Feb 29, 2024. It is now read-only.

Prepare messages-substrate polkadot-v0.9.33 for Darwinia-2.0 #235

Merged
merged 26 commits into from
Jan 12, 2023
Merged
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
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
327 changes: 134 additions & 193 deletions Cargo.lock

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions modules/dispatch/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ scale-info = { version = "2.1", default-features = false, features = ["derive"]
bp-message-dispatch = { default-features = false, path = "../../primitives/message-dispatch" }
bp-runtime = { default-features = false, path = "../../primitives/runtime" }
# paritytech
frame-support = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.30" }
frame-system = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.30" }
sp-core = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.30" }
sp-runtime = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.30" }
sp-std = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.30" }
frame-support = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.33" }
frame-system = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.33" }
sp-core = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.33" }
sp-runtime = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.33" }
sp-std = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.33" }

[dev-dependencies]
sp-io = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.30" }
sp-io = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.33" }

[features]
default = ["std"]
Expand Down
4 changes: 2 additions & 2 deletions modules/dispatch/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ impl<T: Config<I>, I: 'static> MessageDispatch<T::AccountId, T::BridgeMessageId>
// because otherwise Calls may be dispatched at lower price)
let dispatch_info = call.get_dispatch_info();
let expected_weight = dispatch_info.weight;
if message.weight.all_lt(expected_weight) {
if message.weight.ref_time() < expected_weight.ref_time() {
log::trace!(
target: "runtime::bridge-dispatch",
"Message {:?}/{:?}: passed weight is too low. Expected at least {:?}, got {:?}",
Expand Down Expand Up @@ -736,7 +736,7 @@ mod tests {
let mut message = prepare_root_message(call);
message.weight = Weight::from_ref_time(7);
assert!(
call_weight.all_gt(Weight::from_ref_time(7)),
call_weight.ref_time() > message.weight.ref_time(),
"needed for test to actually trigger a weight mismatch"
);

Expand Down
18 changes: 9 additions & 9 deletions modules/fee-market/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,21 @@ version = "0.1.0"
codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false }
scale-info = { version = "2.0.1", default-features = false, features = ["derive"] }
# paritytech
frame-benchmarking = { optional = true, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.30", default-features = false }
frame-support = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.30", default-features = false }
frame-system = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.30", default-features = false }
pallet-timestamp = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.30", default-features = false }
sp-core = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.30", default-features = false }
sp-io = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.30", default-features = false }
sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.30", default-features = false }
sp-std = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.30", default-features = false }
frame-benchmarking = { optional = true, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.33", default-features = false }
frame-support = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.33", default-features = false }
frame-system = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.33", default-features = false }
pallet-timestamp = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.33", default-features = false }
sp-core = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.33", default-features = false }
sp-io = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.33", default-features = false }
sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.33", default-features = false }
sp-std = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.33", default-features = false }
# darwinia-bridges-substrate
bp-messages = { default-features = false, path = "../../primitives/messages" }
bp-runtime = { default-features = false, path = "../../primitives/runtime" }

[dev-dependencies]
bitvec = { version = "1" }
pallet-balances = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.30" }
pallet-balances = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.33" }
pallet-bridge-messages = { path = "../messages" }

[features]
Expand Down
18 changes: 9 additions & 9 deletions modules/grandpa/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@ bp-header-chain = { default-features = false, path = "../../primitives/header-ch
bp-runtime = { default-features = false, path = "../../primitives/runtime" }
bp-test-utils = { optional = true, default-features = false, path = "../../primitives/test-utils" }
# paritytech
frame-benchmarking = { optional = true, default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.30" }
frame-support = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.30" }
frame-system = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.30" }
sp-finality-grandpa = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.30" }
sp-runtime = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.30" }
sp-std = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.30" }
sp-trie = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.30" }
frame-benchmarking = { optional = true, default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.33" }
frame-support = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.33" }
frame-system = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.33" }
sp-finality-grandpa = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.33" }
sp-runtime = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.33" }
sp-std = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.33" }
sp-trie = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.33" }

[dev-dependencies]
# paritytech
sp-core = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.30" }
sp-io = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.30" }
sp-core = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.33" }
sp-io = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.33" }

[features]
default = ["std"]
Expand Down
2 changes: 1 addition & 1 deletion modules/grandpa/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ const MAX_VOTE_ANCESTRIES: u32 = 1000;

// The maximum number of pre-commits to include in a justification. In practice this scales with the
// number of validators.
const MAX_VALIDATOR_SET_SIZE: u32 = 1024;
pub const MAX_VALIDATOR_SET_SIZE: u32 = 1024;

// `1..MAX_VALIDATOR_SET_SIZE` and `1..MAX_VOTE_ANCESTRIES` are too large && benchmarks are
// running for almost 40m (steps=50, repeat=20) on a decent laptop, which is too much. Since
Expand Down
16 changes: 8 additions & 8 deletions modules/messages/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,19 @@ bp-message-dispatch = { default-features = false, path = "../../primitives/messa
bp-messages = { default-features = false, path = "../../primitives/messages" }
bp-runtime = { default-features = false, path = "../../primitives/runtime" }
# paritytech
frame-benchmarking = { optional = true, default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.30" }
frame-support = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.30" }
frame-system = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.30" }
sp-core = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.30" }
sp-runtime = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.30" }
sp-std = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.30" }
frame-benchmarking = { optional = true, default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.33" }
frame-support = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.33" }
frame-system = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.33" }
sp-core = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.33" }
sp-runtime = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.33" }
sp-std = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.33" }

[dev-dependencies]
# darwinia-network
bp-test-utils = { path = "../../primitives/test-utils" }
# paritytech
pallet-balances = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.30" }
sp-io = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.30" }
pallet-balances = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.33" }
sp-io = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.33" }

[features]
default = ["std"]
Expand Down
30 changes: 6 additions & 24 deletions modules/messages/src/inbound_lane.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@ use crate::Config;
use bp_messages::{
target_chain::{DispatchMessage, DispatchMessageData, MessageDispatch},
DeliveredMessages, InboundLaneData, LaneId, MessageKey, MessageNonce, OutboundLaneData,
UnrewardedRelayer,
ReceivalResult, UnrewardedRelayer,
};
use bp_runtime::messages::MessageDispatchResult;
// paritytech
use frame_support::{traits::Get, RuntimeDebug};

Expand Down Expand Up @@ -104,24 +103,6 @@ impl<T: Config<I>, I: 'static> MaxEncodedLen for StoredInboundLaneData<T, I> {
}
}

/// Result of single message receival.
#[derive(RuntimeDebug, PartialEq, Eq)]
pub enum ReceivalResult {
/// Message has been received and dispatched. Note that we don't care whether dispatch has
/// been successful or not - in both case message falls into this category.
///
/// The message dispatch result is also returned.
Dispatched(MessageDispatchResult),
/// Message has invalid nonce and lane has rejected to accept this message.
InvalidNonce,
/// There are too many unrewarded relayer entries at the lane.
TooManyUnrewardedRelayers,
/// There are too many unconfirmed messages at the lane.
TooManyUnconfirmedMessages,
/// Pre-dispatch validation failed before message dispatch.
PreDispatchValidateFailed,
}

/// Inbound messages lane.
pub struct InboundLane<S> {
storage: S,
Expand Down Expand Up @@ -178,12 +159,12 @@ impl<S: InboundLaneStorage> InboundLane<S> {
}

/// Receive new message.
pub fn receive_message<P: MessageDispatch<AccountId, S::MessageFee>, AccountId>(
pub fn receive_message<Dispatch: MessageDispatch<AccountId, S::MessageFee>, AccountId>(
&mut self,
relayer_at_bridged_chain: &S::Relayer,
relayer_at_this_chain: &AccountId,
nonce: MessageNonce,
message_data: DispatchMessageData<P::DispatchPayload, S::MessageFee>,
message_data: DispatchMessageData<Dispatch::DispatchPayload, S::MessageFee>,
) -> ReceivalResult {
let mut data = self.storage.data();
let is_correct_message = nonce == data.last_delivered_nonce() + 1;
Expand All @@ -206,13 +187,14 @@ impl<S: InboundLaneStorage> InboundLane<S> {
key: MessageKey { lane_id: self.storage.id(), nonce },
data: message_data,
};

// if there are some extra pre-dispatch validation errors, reject this message.
if P::pre_dispatch(relayer_at_this_chain, &dispatch_message).is_err() {
if Dispatch::pre_dispatch(relayer_at_this_chain, &dispatch_message).is_err() {
return ReceivalResult::PreDispatchValidateFailed;
}

// then, dispatch message
let dispatch_result = P::dispatch(relayer_at_this_chain, dispatch_message);
let dispatch_result = Dispatch::dispatch(relayer_at_this_chain, dispatch_message);

// now let's update inbound lane storage
let push_new = match data.relayers.back_mut() {
Expand Down
45 changes: 33 additions & 12 deletions modules/messages/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ use codec::{Decode, Encode, MaxEncodedLen};
use num_traits::{SaturatingAdd, Zero};
// darwinia-network
use crate::{
inbound_lane::{InboundLane, InboundLaneStorage, ReceivalResult},
inbound_lane::{InboundLane, InboundLaneStorage},
outbound_lane::{OutboundLane, OutboundLaneStorage, ReceivalConfirmationResult},
};
use bp_messages::{
Expand Down Expand Up @@ -91,6 +91,7 @@ pub const LOG_TARGET: &str = "runtime::bridge-messages";
#[frame_support::pallet]
pub mod pallet {
use super::*;
use bp_messages::{ReceivalResult, ReceivedMessages};
use frame_support::pallet_prelude::*;
use frame_system::pallet_prelude::*;

Expand Down Expand Up @@ -334,6 +335,7 @@ pub mod pallet {
// dispatch messages and (optionally) update lane(s) state(s)
let mut total_messages = 0;
let mut valid_messages = 0;
let mut messages_received_status = Vec::with_capacity(messages.len());
let mut dispatch_weight_left = dispatch_weight;
for (lane_id, lane_data) in messages {
let mut lane = inbound_lane::<T, I>(lane_id);
Expand All @@ -350,24 +352,37 @@ pub mod pallet {
}
}

let mut lane_messages_received_status =
ReceivedMessages::new(lane_id, Vec::with_capacity(lane_data.messages.len()));
let mut is_lane_processing_stopped_no_weight_left = false;

for mut message in lane_data.messages {
debug_assert_eq!(message.key.lane_id, lane_id);
total_messages += 1;

if is_lane_processing_stopped_no_weight_left {
lane_messages_received_status
.push_skipped_for_not_enough_weight(message.key.nonce);
continue;
}

// ensure that relayer has declared enough weight for dispatching next message
// on this lane. We can't dispatch lane messages out-of-order, so if declared
// weight is not enough, let's move to next lane
let dispatch_weight = T::MessageDispatch::dispatch_weight(&mut message);
if dispatch_weight.any_gt(dispatch_weight_left) {
let message_dispatch_weight = T::MessageDispatch::dispatch_weight(&mut message);
if message_dispatch_weight.any_gt(dispatch_weight_left) {
log::trace!(
target: LOG_TARGET,
"Cannot dispatch any more messages on lane {:?}. Weight: declared={}, left={}",
lane_id,
dispatch_weight,
message_dispatch_weight,
dispatch_weight_left,
);
break;
lane_messages_received_status
.push_skipped_for_not_enough_weight(message.key.nonce);
is_lane_processing_stopped_no_weight_left = true;
continue;
}
total_messages += 1;

let receival_result = lane.receive_message::<T::MessageDispatch, T::AccountId>(
&relayer_id_at_bridged_chain,
Expand All @@ -382,7 +397,7 @@ pub mod pallet {
// losing funds for messages dispatch. But keep in mind that relayer pays base
// delivery transaction cost anyway. And base cost covers everything except
// dispatch, so we have a balance here.
let (unspent_weight, refund_pay_dispatch_fee) = match receival_result {
let (unspent_weight, refund_pay_dispatch_fee) = match &receival_result {
ReceivalResult::Dispatched(dispatch_result) => {
valid_messages += 1;
(
Expand All @@ -393,11 +408,11 @@ pub mod pallet {
ReceivalResult::InvalidNonce
| ReceivalResult::TooManyUnrewardedRelayers
| ReceivalResult::PreDispatchValidateFailed
| ReceivalResult::TooManyUnconfirmedMessages => (dispatch_weight, true),
| ReceivalResult::TooManyUnconfirmedMessages => (message_dispatch_weight, true),
};

let unspent_weight = unspent_weight.min(dispatch_weight);
dispatch_weight_left -= dispatch_weight - unspent_weight;
lane_messages_received_status.push(message.key.nonce, receival_result);
let unspent_weight = unspent_weight.min(message_dispatch_weight);
dispatch_weight_left -= message_dispatch_weight - unspent_weight;
actual_weight = actual_weight.saturating_sub(unspent_weight).saturating_sub(
// delivery call weight formula assumes that the fee is paid at
// this (target) chain. If the message is prepaid at the source
Expand All @@ -409,9 +424,11 @@ pub mod pallet {
},
);
}

messages_received_status.push(lane_messages_received_status);
}

log::trace!(
log::debug!(
target: LOG_TARGET,
"Received messages: total={}, valid={}. Weight used: {}/{}",
total_messages,
Expand All @@ -420,6 +437,8 @@ pub mod pallet {
declared_weight,
);

Self::deposit_event(Event::MessagesReceived(messages_received_status));

Ok(PostDispatchInfo { actual_weight: Some(actual_weight), pays_fee: Pays::Yes })
}

Expand Down Expand Up @@ -587,6 +606,8 @@ pub mod pallet {
ParameterUpdated { parameter: T::Parameter },
/// Message has been accepted and is waiting to be delivered.
MessageAccepted { lane_id: LaneId, nonce: MessageNonce },
/// Messages have been received from the bridged chain.
MessagesReceived(Vec<ReceivedMessages<ReceivalResult>>),
/// Messages in the inclusive range have been delivered to the bridged chain.
MessagesDelivered { lane_id: LaneId, messages: DeliveredMessages },
}
Expand Down
Loading