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 <HCastano@users.noreply.github.com>

* add issue reference

* add assert message

* grumbles

* fmt

* reexport weight from bp-message-lane

Co-authored-by: Hernando Castano <HCastano@users.noreply.github.com>
Co-authored-by: Hernando Castano <castano.ha@gmail.com>
This commit is contained in:
Svyatoslav Nikolsky
2020-11-17 23:27:30 +03:00
committed by Bastian Köcher
parent e515f4fb62
commit 23f5f3cdd6
21 changed files with 859 additions and 405 deletions
+44 -12
View File
@@ -25,7 +25,7 @@ use codec::{Decode, Encode};
use frame_support::weights::Weight;
use messages_relay::{
message_lane::{MessageLane, SourceHeaderIdOf, TargetHeaderIdOf},
message_lane_loop::{ClientState, SourceClient, SourceClientState},
message_lane_loop::{ClientState, MessageProofParameters, MessageWeightsMap, SourceClient, SourceClientState},
};
use relay_substrate_client::{Chain, Client, Error as SubstrateError, HashOf, HeaderIdOf};
use relay_utils::HeaderId;
@@ -95,7 +95,6 @@ where
C::Index: DeserializeOwned,
<C::Header as HeaderT>::Number: Into<u64>,
P: MessageLane<
MessageNonce = MessageNonce,
MessagesProof = SubstrateMessagesProof<C>,
SourceHeaderNumber = <C::Header as HeaderT>::Number,
SourceHeaderHash = <C::Header as HeaderT>::Hash,
@@ -119,7 +118,7 @@ where
async fn latest_generated_nonce(
&self,
id: SourceHeaderIdOf<P>,
) -> Result<(SourceHeaderIdOf<P>, P::MessageNonce), Self::Error> {
) -> Result<(SourceHeaderIdOf<P>, MessageNonce), Self::Error> {
let encoded_response = self
.client
.state_call(
@@ -129,7 +128,7 @@ where
Some(id.1),
)
.await?;
let latest_generated_nonce: P::MessageNonce =
let latest_generated_nonce: MessageNonce =
Decode::decode(&mut &encoded_response.0[..]).map_err(SubstrateError::ResponseParseFailed)?;
Ok((id, latest_generated_nonce))
}
@@ -137,7 +136,7 @@ where
async fn latest_confirmed_received_nonce(
&self,
id: SourceHeaderIdOf<P>,
) -> Result<(SourceHeaderIdOf<P>, P::MessageNonce), Self::Error> {
) -> Result<(SourceHeaderIdOf<P>, MessageNonce), Self::Error> {
let encoded_response = self
.client
.state_call(
@@ -147,29 +146,62 @@ where
Some(id.1),
)
.await?;
let latest_received_nonce: P::MessageNonce =
let latest_received_nonce: MessageNonce =
Decode::decode(&mut &encoded_response.0[..]).map_err(SubstrateError::ResponseParseFailed)?;
Ok((id, latest_received_nonce))
}
async fn generated_messages_weights(
&self,
id: SourceHeaderIdOf<P>,
nonces: RangeInclusive<MessageNonce>,
) -> Result<MessageWeightsMap, Self::Error> {
let encoded_response = self
.client
.state_call(
// TODO: https://github.com/paritytech/parity-bridges-common/issues/457
"OutboundLaneApi_messages_dispatch_weight".into(),
Bytes((self.lane, nonces.start(), nonces.end()).encode()),
Some(id.1),
)
.await?;
let weights: Vec<(MessageNonce, Weight)> =
Decode::decode(&mut &encoded_response.0[..]).map_err(SubstrateError::ResponseParseFailed)?;
let mut expected_nonce = *nonces.start();
let mut weights_map = MessageWeightsMap::new();
for (nonce, weight) in weights {
if nonce != expected_nonce {
return Err(SubstrateError::Custom(format!(
"Unexpected nonce in messages_dispatch_weight call result. Expected {}, got {}",
expected_nonce, nonce
)));
}
weights_map.insert(nonce, weight);
expected_nonce += 1;
}
Ok(weights_map)
}
async fn prove_messages(
&self,
id: SourceHeaderIdOf<P>,
nonces: RangeInclusive<P::MessageNonce>,
include_outbound_lane_state: bool,
) -> Result<(SourceHeaderIdOf<P>, RangeInclusive<P::MessageNonce>, P::MessagesProof), Self::Error> {
let (weight, proof) = self
nonces: RangeInclusive<MessageNonce>,
proof_parameters: MessageProofParameters,
) -> Result<(SourceHeaderIdOf<P>, RangeInclusive<MessageNonce>, P::MessagesProof), Self::Error> {
let proof = self
.client
.prove_messages(
self.instance,
self.lane,
nonces.clone(),
include_outbound_lane_state,
proof_parameters.outbound_state_proof_required,
id.1,
)
.await?;
let proof = (id.1, proof, self.lane, *nonces.start(), *nonces.end());
Ok((id, nonces, (weight, proof)))
Ok((id, nonces, (proof_parameters.dispatch_weight, proof)))
}
async fn submit_messages_receiving_proof(
@@ -53,7 +53,7 @@ pub trait SubstrateTransactionMaker<C: Chain, P: MessageLane>: Clone + Send + Sy
async fn make_messages_delivery_transaction(
&self,
generated_at_header: SourceHeaderIdOf<P>,
nonces: RangeInclusive<P::MessageNonce>,
nonces: RangeInclusive<MessageNonce>,
proof: P::MessagesProof,
) -> Result<Self::SignedTransaction, SubstrateError>;
}
@@ -91,7 +91,6 @@ where
C::Index: DeserializeOwned,
<C::Header as HeaderT>::Number: Into<u64>,
P: MessageLane<
MessageNonce = MessageNonce,
MessagesReceivingProof = (HashOf<C>, StorageProof, LaneId),
TargetHeaderNumber = <C::Header as HeaderT>::Number,
TargetHeaderHash = <C::Header as HeaderT>::Hash,
@@ -115,7 +114,7 @@ where
async fn latest_received_nonce(
&self,
id: TargetHeaderIdOf<P>,
) -> Result<(TargetHeaderIdOf<P>, P::MessageNonce), Self::Error> {
) -> Result<(TargetHeaderIdOf<P>, MessageNonce), Self::Error> {
let encoded_response = self
.client
.state_call(
@@ -125,7 +124,7 @@ where
Some(id.1),
)
.await?;
let latest_received_nonce: P::MessageNonce =
let latest_received_nonce: MessageNonce =
Decode::decode(&mut &encoded_response.0[..]).map_err(SubstrateError::ResponseParseFailed)?;
Ok((id, latest_received_nonce))
}
@@ -133,7 +132,7 @@ where
async fn latest_confirmed_received_nonce(
&self,
id: TargetHeaderIdOf<P>,
) -> Result<(TargetHeaderIdOf<P>, P::MessageNonce), Self::Error> {
) -> Result<(TargetHeaderIdOf<P>, MessageNonce), Self::Error> {
let encoded_response = self
.client
.state_call(
@@ -143,7 +142,7 @@ where
Some(id.1),
)
.await?;
let latest_received_nonce: P::MessageNonce =
let latest_received_nonce: MessageNonce =
Decode::decode(&mut &encoded_response.0[..]).map_err(SubstrateError::ResponseParseFailed)?;
Ok((id, latest_received_nonce))
}
@@ -163,9 +162,9 @@ where
async fn submit_messages_proof(
&self,
generated_at_header: SourceHeaderIdOf<P>,
nonces: RangeInclusive<P::MessageNonce>,
nonces: RangeInclusive<MessageNonce>,
proof: P::MessagesProof,
) -> Result<RangeInclusive<P::MessageNonce>, Self::Error> {
) -> Result<RangeInclusive<MessageNonce>, Self::Error> {
let tx = self
.tx_maker
.make_messages_delivery_transaction(generated_at_header, nonces.clone(), proof)
@@ -52,7 +52,6 @@ impl MessageLane for MillauMessagesToRialto {
const SOURCE_NAME: &'static str = "Millau";
const TARGET_NAME: &'static str = "Rialto";
type MessageNonce = MessageNonce;
type MessagesProof = FromMillauMessagesProof;
type MessagesReceivingProof = FromRialtoMessagesReceivingProof;
@@ -144,7 +143,12 @@ pub fn run(
target_tick: rialto_tick,
reconnect_delay,
stall_timeout,
max_unconfirmed_nonces_at_target: bp_rialto::MAX_UNCONFIRMED_MESSAGES_AT_INBOUND_LANE,
delivery_params: messages_relay::message_lane_loop::MessageDeliveryParams {
max_unconfirmed_nonces_at_target: bp_rialto::MAX_UNCONFIRMED_MESSAGES_AT_INBOUND_LANE,
// TODO: subtract base weight of delivery from this when it'll be known
// https://github.com/paritytech/parity-bridges-common/issues/78
max_messages_weight_in_single_batch: bp_rialto::MAXIMUM_EXTRINSIC_WEIGHT,
},
},
MillauSourceClient::new(
millau_client.clone(),