Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Commit 6da3aa1

Browse files
svyatonikHCastano
andauthored
Limit messages weight in batch (#496)
* limit messages in the batch by weight/count * fixed components compilation * reverted obsolete parts of #469 * implement generated_messages_weights * actually use computed weight in message proof * fmt and clippy * fixed TODO * clippy * Update relays/messages-relay/src/message_race_loop.rs Co-authored-by: Hernando Castano <[email protected]> * add issue reference * add assert message * grumbles * fmt * reexport weight from bp-message-lane Co-authored-by: Hernando Castano <[email protected]> Co-authored-by: Hernando Castano <[email protected]>
1 parent ec50557 commit 6da3aa1

File tree

22 files changed

+848
-397
lines changed

22 files changed

+848
-397
lines changed

Cargo.lock

Lines changed: 0 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bin/millau/node/src/service.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,6 @@ pub fn new_full(config: Configuration) -> Result<TaskManager, ServiceError> {
221221
finality_proof_provider.clone(),
222222
)));
223223
io.extend_with(MessageLaneApi::to_delegate(MessageLaneRpcHandler::new(
224-
client.clone(),
225224
backend.clone(),
226225
Arc::new(MillauMessageLaneKeys),
227226
)));

bin/millau/runtime/src/lib.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -542,12 +542,19 @@ impl_runtime_apis! {
542542

543543
// TODO: runtime should support several chains (https://github.com/paritytech/parity-bridges-common/issues/457)
544544
impl bp_message_lane::OutboundLaneApi<Block> for Runtime {
545-
fn messages_dispatch_weight(lane: bp_message_lane::LaneId, begin: bp_message_lane::MessageNonce, end: bp_message_lane::MessageNonce) -> Weight {
546-
(begin..=end)
547-
.filter_map(|nonce| BridgeRialtoMessageLane::outbound_message_payload(lane, nonce))
548-
.filter_map(|encoded_payload| rialto_messages::ToRialtoMessagePayload::decode(&mut &encoded_payload[..]).ok())
549-
.map(|decoded_payload| decoded_payload.weight)
550-
.fold(0, |sum, weight| sum.saturating_add(weight))
545+
fn messages_dispatch_weight(
546+
lane: bp_message_lane::LaneId,
547+
begin: bp_message_lane::MessageNonce,
548+
end: bp_message_lane::MessageNonce,
549+
) -> Vec<(bp_message_lane::MessageNonce, Weight)> {
550+
(begin..=end).filter_map(|nonce| {
551+
let encoded_payload = BridgeRialtoMessageLane::outbound_message_payload(lane, nonce)?;
552+
let decoded_payload = rialto_messages::ToRialtoMessagePayload::decode(
553+
&mut &encoded_payload[..]
554+
).ok()?;
555+
Some((nonce, decoded_payload.weight))
556+
})
557+
.collect()
551558
}
552559

553560
fn latest_received_nonce(lane: bp_message_lane::LaneId) -> bp_message_lane::MessageNonce {

bin/rialto/node/src/service.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,6 @@ pub fn new_full(config: Configuration) -> Result<TaskManager, ServiceError> {
220220
finality_proof_provider.clone(),
221221
)));
222222
io.extend_with(MessageLaneApi::to_delegate(MessageLaneRpcHandler::new(
223-
client.clone(),
224223
backend.clone(),
225224
Arc::new(RialtoMessageLaneKeys),
226225
)));

bin/rialto/runtime/src/lib.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -706,12 +706,19 @@ impl_runtime_apis! {
706706

707707
// TODO: runtime should support several chains (https://github.com/paritytech/parity-bridges-common/issues/457)
708708
impl bp_message_lane::OutboundLaneApi<Block> for Runtime {
709-
fn messages_dispatch_weight(lane: bp_message_lane::LaneId, begin: bp_message_lane::MessageNonce, end: bp_message_lane::MessageNonce) -> Weight {
710-
(begin..=end)
711-
.filter_map(|nonce| BridgeMillauMessageLane::outbound_message_payload(lane, nonce))
712-
.filter_map(|encoded_payload| millau_messages::ToMillauMessagePayload::decode(&mut &encoded_payload[..]).ok())
713-
.map(|decoded_payload| decoded_payload.weight)
714-
.fold(0, |sum, weight| sum.saturating_add(weight))
709+
fn messages_dispatch_weight(
710+
lane: bp_message_lane::LaneId,
711+
begin: bp_message_lane::MessageNonce,
712+
end: bp_message_lane::MessageNonce,
713+
) -> Vec<(bp_message_lane::MessageNonce, Weight)> {
714+
(begin..=end).filter_map(|nonce| {
715+
let encoded_payload = BridgeMillauMessageLane::outbound_message_payload(lane, nonce)?;
716+
let decoded_payload = millau_messages::ToMillauMessagePayload::decode(
717+
&mut &encoded_payload[..]
718+
).ok()?;
719+
Some((nonce, decoded_payload.weight))
720+
})
721+
.collect()
715722
}
716723

717724
fn latest_received_nonce(lane: bp_message_lane::LaneId) -> bp_message_lane::MessageNonce {

modules/message-lane/rpc/Cargo.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@ bp-message-lane = { path = "../../../primitives/message-lane" }
2020

2121
# Substrate Dependencies
2222

23-
frame-support = "2.0"
2423
sc-client-api = "2.0"
25-
sp-api = "2.0"
2624
sp-blockchain = "2.0"
2725
sp-core = "2.0"
2826
sp-runtime = "2.0"

modules/message-lane/rpc/src/lib.rs

Lines changed: 11 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,12 @@
1818
1919
use crate::error::{Error, FutureResult};
2020

21-
use bp_message_lane::{LaneId, MessageNonce, OutboundLaneApi};
21+
use bp_message_lane::{LaneId, MessageNonce};
2222
use bp_runtime::InstanceId;
23-
use frame_support::weights::Weight;
2423
use futures::{FutureExt, TryFutureExt};
2524
use jsonrpc_core::futures::Future as _;
2625
use jsonrpc_derive::rpc;
2726
use sc_client_api::Backend as BackendT;
28-
use sp_api::ProvideRuntimeApi;
2927
use sp_blockchain::{Error as BlockchainError, HeaderBackend};
3028
use sp_core::{storage::StorageKey, Bytes};
3129
use sp_runtime::{codec::Encode, generic::BlockId, traits::Block as BlockT};
@@ -56,8 +54,8 @@ pub trait Runtime: Send + Sync + 'static {
5654
/// Provides RPC methods for interacting with message-lane pallet.
5755
#[rpc]
5856
pub trait MessageLaneApi<BlockHash> {
59-
/// Returns cumulative dispatch weight of messages in given inclusive range and their storage proof.
60-
/// The state of outbound lane is included in the proof if `include_outbound_lane_state` is true.
57+
/// Returns storage proof of messages in given inclusive range. The state of outbound
58+
/// lane is included in the proof if `include_outbound_lane_state` is true.
6159
#[rpc(name = "messageLane_proveMessages")]
6260
fn prove_messages(
6361
&self,
@@ -67,7 +65,7 @@ pub trait MessageLaneApi<BlockHash> {
6765
end: MessageNonce,
6866
include_outbound_lane_state: bool,
6967
block: Option<BlockHash>,
70-
) -> FutureResult<(Weight, MessagesProof)>;
68+
) -> FutureResult<MessagesProof>;
7169

7270
/// Returns proof-of-message(s) delivery.
7371
#[rpc(name = "messageLane_proveMessagesDelivery")]
@@ -80,30 +78,26 @@ pub trait MessageLaneApi<BlockHash> {
8078
}
8179

8280
/// Implements the MessageLaneApi trait for interacting with message lanes.
83-
pub struct MessageLaneRpcHandler<Block, Client, Backend, R> {
84-
client: Arc<Client>,
81+
pub struct MessageLaneRpcHandler<Block, Backend, R> {
8582
backend: Arc<Backend>,
8683
runtime: Arc<R>,
8784
_phantom: std::marker::PhantomData<Block>,
8885
}
8986

90-
impl<Block, Client, Backend, R> MessageLaneRpcHandler<Block, Client, Backend, R> {
87+
impl<Block, Backend, R> MessageLaneRpcHandler<Block, Backend, R> {
9188
/// Creates new mesage lane RPC handler.
92-
pub fn new(client: Arc<Client>, backend: Arc<Backend>, runtime: Arc<R>) -> Self {
89+
pub fn new(backend: Arc<Backend>, runtime: Arc<R>) -> Self {
9390
Self {
94-
client,
9591
backend,
9692
runtime,
9793
_phantom: Default::default(),
9894
}
9995
}
10096
}
10197

102-
impl<Block, Client, Backend, R> MessageLaneApi<Block::Hash> for MessageLaneRpcHandler<Block, Client, Backend, R>
98+
impl<Block, Backend, R> MessageLaneApi<Block::Hash> for MessageLaneRpcHandler<Block, Backend, R>
10399
where
104100
Block: BlockT,
105-
Client: ProvideRuntimeApi<Block> + Send + Sync + 'static,
106-
Client::Api: OutboundLaneApi<Block>,
107101
Backend: BackendT<Block> + 'static,
108102
R: Runtime,
109103
{
@@ -115,22 +109,7 @@ where
115109
end: MessageNonce,
116110
include_outbound_lane_state: bool,
117111
block: Option<Block::Hash>,
118-
) -> FutureResult<(Weight, MessagesProof)> {
119-
let block = unwrap_or_best(&*self.backend, block);
120-
121-
let messages_dispatch_weight_result =
122-
self.client
123-
.runtime_api()
124-
.messages_dispatch_weight(&BlockId::Hash(block), lane, begin, end);
125-
let messages_dispatch_weight = match messages_dispatch_weight_result {
126-
Ok(messages_dispatch_weight) => messages_dispatch_weight,
127-
Err(error) => {
128-
return Box::new(jsonrpc_core::futures::future::err(
129-
blockchain_err(BlockchainError::Execution(Box::new(format!("{:?}", error)))).into(),
130-
))
131-
}
132-
};
133-
112+
) -> FutureResult<MessagesProof> {
134113
let runtime = self.runtime.clone();
135114
let outbound_lane_data_key = if include_outbound_lane_state {
136115
Some(runtime.inbound_lane_data_key(&instance, &lane))
@@ -140,14 +119,14 @@ where
140119
Box::new(
141120
prove_keys_read(
142121
self.backend.clone(),
143-
Some(block),
122+
block,
144123
(begin..=end)
145124
.map(move |nonce| runtime.message_key(&instance, &lane, nonce))
146125
.chain(outbound_lane_data_key.into_iter()),
147126
)
148127
.boxed()
149128
.compat()
150-
.map(move |proof| (messages_dispatch_weight, serialize_storage_proof(proof)))
129+
.map(serialize_storage_proof)
151130
.map_err(Into::into),
152131
)
153132
}

primitives/message-lane/src/lib.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,16 @@
2323
#![allow(clippy::unnecessary_mut_passed)]
2424

2525
use codec::{Decode, Encode};
26-
use frame_support::{weights::Weight, RuntimeDebug};
26+
use frame_support::RuntimeDebug;
2727
use sp_api::decl_runtime_apis;
2828
use sp_std::{collections::vec_deque::VecDeque, prelude::*};
2929

3030
pub mod source_chain;
3131
pub mod target_chain;
3232

33+
// Weight is reexported to avoid additional frame-support dependencies in message-lane related crates.
34+
pub use frame_support::weights::Weight;
35+
3336
/// Lane identifier.
3437
pub type LaneId = [u8; 4];
3538

@@ -127,7 +130,14 @@ decl_runtime_apis! {
127130
/// Outbound message lane API.
128131
pub trait OutboundLaneApi {
129132
/// Returns dispatch weight of all messages in given inclusive range.
130-
fn messages_dispatch_weight(lane: LaneId, begin: MessageNonce, end: MessageNonce) -> Weight;
133+
///
134+
/// If some (or all) messages are missing from the storage, they'll also will
135+
/// be missing from the resulting vector. The vector is ordered by the nonce.
136+
fn messages_dispatch_weight(
137+
lane: LaneId,
138+
begin: MessageNonce,
139+
end: MessageNonce,
140+
) -> Vec<(MessageNonce, Weight)>;
131141
/// Returns nonce of the latest message, received by bridged chain.
132142
fn latest_received_nonce(lane: LaneId) -> MessageNonce;
133143
/// Returns nonce of the latest message, generated by given lane.

relays/messages-relay/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ async-trait = "0.1.40"
1111
futures = "0.3.5"
1212
hex = "0.4"
1313
log = "0.4.11"
14-
num-traits = "0.2"
1514
parking_lot = "0.11.0"
1615

1716
# Bridge Dependencies

relays/messages-relay/src/message_lane.rs

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
2222
use relay_utils::HeaderId;
2323

24-
use num_traits::{CheckedSub, One, Zero};
2524
use std::fmt::Debug;
2625

2726
/// One-way message lane.
@@ -31,21 +30,6 @@ pub trait MessageLane: Clone + Send + Sync {
3130
/// Name of the messages target.
3231
const TARGET_NAME: &'static str;
3332

34-
/// Message nonce type.
35-
type MessageNonce: Clone
36-
+ Send
37-
+ Sync
38-
+ Copy
39-
+ Debug
40-
+ Default
41-
+ From<u32>
42-
+ Into<u64>
43-
+ Ord
44-
+ CheckedSub
45-
+ std::ops::Add<Output = Self::MessageNonce>
46-
+ One
47-
+ Zero;
48-
4933
/// Messages proof.
5034
type MessagesProof: Clone + Send + Sync;
5135
/// Messages receiving proof.

0 commit comments

Comments
 (0)