Add messages count parameter to delivery transaction (#581)

* add messages count parameter to delivery transaction

* fix benchmarks compilation
This commit is contained in:
Svyatoslav Nikolsky
2020-12-17 12:47:45 +03:00
committed by Bastian Köcher
parent 6317a31e25
commit 63e2655c8b
13 changed files with 96 additions and 71 deletions
@@ -25,7 +25,7 @@ use frame_benchmarking::{account, benchmarks_instance};
use frame_support::{traits::Get, weights::Weight};
use frame_system::RawOrigin;
use num_traits::Zero;
use sp_std::{convert::TryInto, ops::RangeInclusive, prelude::*};
use sp_std::{ops::RangeInclusive, prelude::*};
/// Message crafted with this size factor should be the largest possible message.
pub const WORST_MESSAGE_SIZE_FACTOR: u32 = 1000;
@@ -127,7 +127,7 @@ benchmarks_instance! {
message_nonces: 1..=1,
outbound_lane_data: None,
});
}: receive_messages_proof(RawOrigin::Signed(relayer_id_on_target), relayer_id_on_source, proof, dispatch_weight)
}: receive_messages_proof(RawOrigin::Signed(relayer_id_on_target), relayer_id_on_source, proof, 1, dispatch_weight)
verify {
assert_eq!(
crate::Module::<T, I>::inbound_latest_received_nonce(bench_lane_id()),
@@ -154,7 +154,7 @@ benchmarks_instance! {
message_nonces: 1..=2,
outbound_lane_data: None,
});
}: receive_messages_proof(RawOrigin::Signed(relayer_id_on_target), relayer_id_on_source, proof, dispatch_weight)
}: receive_messages_proof(RawOrigin::Signed(relayer_id_on_target), relayer_id_on_source, proof, 2, dispatch_weight)
verify {
assert_eq!(
crate::Module::<T, I>::inbound_latest_received_nonce(bench_lane_id()),
@@ -188,7 +188,7 @@ benchmarks_instance! {
latest_generated_nonce: 21,
}),
});
}: receive_messages_proof(RawOrigin::Signed(relayer_id_on_target), relayer_id_on_source, proof, dispatch_weight)
}: receive_messages_proof(RawOrigin::Signed(relayer_id_on_target), relayer_id_on_source, proof, 1, dispatch_weight)
verify {
assert_eq!(
crate::Module::<T, I>::inbound_latest_received_nonce(bench_lane_id()),
@@ -214,19 +214,24 @@ benchmarks_instance! {
// `weight(receive_two_messages_proof) - weight(receive_single_message_proof)`. So it may be used
// to verify that the other approximation is correct.
receive_multiple_messages_proof {
let i in 1..T::MaxMessagesInDeliveryTransaction::get()
.try_into()
.expect("Value of MaxMessagesInDeliveryTransaction is too large");
let i in 1..128;
let relayer_id_on_source = T::bridged_relayer_id();
let relayer_id_on_target = account("relayer", 0, SEED);
let messages_count = i as _;
let (proof, dispatch_weight) = T::prepare_message_proof(MessageProofParams {
lane: bench_lane_id(),
message_nonces: 1..=i as _,
outbound_lane_data: None,
});
}: receive_messages_proof(RawOrigin::Signed(relayer_id_on_target), relayer_id_on_source, proof, dispatch_weight)
}: receive_messages_proof(
RawOrigin::Signed(relayer_id_on_target),
relayer_id_on_source,
proof,
messages_count,
dispatch_weight
)
verify {
assert_eq!(
crate::Module::<T, I>::inbound_latest_received_nonce(bench_lane_id()),
@@ -244,12 +249,11 @@ benchmarks_instance! {
// `weight(receive_single_message_proof_with_outbound_lane_state) - weight(receive_single_message_proof)`.
// So it may be used to verify that the other approximation is correct.
receive_multiple_messages_proof_with_outbound_lane_state {
let i in 1..T::MaxMessagesInDeliveryTransaction::get()
.try_into()
.expect("Value of MaxMessagesInDeliveryTransaction is too large");
let i in 1..128;
let relayer_id_on_source = T::bridged_relayer_id();
let relayer_id_on_target = account("relayer", 0, SEED);
let messages_count = i as _;
// mark messages 1..=20 as delivered
receive_messages::<T, I>(20);
@@ -263,7 +267,13 @@ benchmarks_instance! {
latest_generated_nonce: 21,
}),
});
}: receive_messages_proof(RawOrigin::Signed(relayer_id_on_target), relayer_id_on_source, proof, dispatch_weight)
}: receive_messages_proof(
RawOrigin::Signed(relayer_id_on_target),
relayer_id_on_source,
proof,
messages_count,
dispatch_weight
)
verify {
assert_eq!(
crate::Module::<T, I>::inbound_latest_received_nonce(bench_lane_id()),
+14 -13
View File
@@ -93,11 +93,6 @@ pub trait Config<I = DefaultInstance>: frame_system::Config {
/// This constant limits difference between last message from last entry of the
/// `InboundLaneData::relayers` and first message at the first entry.
type MaxUnconfirmedMessagesAtInboundLane: Get<MessageNonce>;
/// Maximal number of messages in single delivery transaction. This directly affects the base
/// weight of the delivery transaction.
///
/// All transactions that deliver more messages than this number, are rejected.
type MaxMessagesInDeliveryTransaction: Get<MessageNonce>;
/// Payload type of outbound messages. This payload is dispatched on the bridged chain.
type OutboundPayload: Parameter;
@@ -322,17 +317,16 @@ decl_module! {
}
/// Receive messages proof from bridged chain.
#[weight = DELIVERY_OVERHEAD_WEIGHT
.saturating_add(
T::MaxMessagesInDeliveryTransaction::get()
.saturating_mul(SINGLE_MESSAGE_DELIVERY_WEIGHT)
)
#[weight = messages_count
.saturating_mul(SINGLE_MESSAGE_DELIVERY_WEIGHT)
.saturating_add(DELIVERY_OVERHEAD_WEIGHT)
.saturating_add(*dispatch_weight)
]
pub fn receive_messages_proof(
origin,
relayer_id: T::InboundRelayer,
proof: MessagesProofOf<T, I>,
messages_count: MessageNonce,
dispatch_weight: Weight,
) -> DispatchResult {
ensure_operational::<T, I>()?;
@@ -343,7 +337,7 @@ decl_module! {
T::SourceHeaderChain,
T::InboundMessageFee,
T::InboundPayload,
>(proof, T::MaxMessagesInDeliveryTransaction::get())
>(proof, messages_count)
.map_err(|err| {
frame_support::debug::trace!(
"Rejecting invalid messages proof: {:?}",
@@ -674,9 +668,9 @@ impl<T: Config<I>, I: Instance> OutboundLaneStorage for RuntimeOutboundLaneStora
/// Verify messages proof and return proved messages with decoded payload.
fn verify_and_decode_messages_proof<Chain: SourceHeaderChain<Fee>, Fee, DispatchPayload: Decode>(
proof: Chain::MessagesProof,
max_messages: MessageNonce,
messages_count: MessageNonce,
) -> Result<ProvedMessages<DispatchMessage<DispatchPayload, Fee>>, Chain::Error> {
Chain::verify_messages_proof(proof, max_messages).map(|messages_by_lane| {
Chain::verify_messages_proof(proof, messages_count).map(|messages_by_lane| {
messages_by_lane
.into_iter()
.map(|(lane, lane_data)| {
@@ -846,6 +840,7 @@ mod tests {
Origin::signed(1),
TEST_RELAYER_A,
Ok(vec![message(2, REGULAR_PAYLOAD)]).into(),
1,
REGULAR_PAYLOAD.1,
),
Error::<TestRuntime, DefaultInstance>::Halted,
@@ -924,6 +919,7 @@ mod tests {
Origin::signed(1),
TEST_RELAYER_A,
Ok(vec![message(1, REGULAR_PAYLOAD)]).into(),
1,
REGULAR_PAYLOAD.1,
));
@@ -964,6 +960,7 @@ mod tests {
Origin::signed(1),
TEST_RELAYER_A,
message_proof,
1,
REGULAR_PAYLOAD.1,
));
@@ -995,6 +992,7 @@ mod tests {
Origin::signed(1),
TEST_RELAYER_A,
Ok(vec![message(1, REGULAR_PAYLOAD)]).into(),
1,
REGULAR_PAYLOAD.1 - 1,
),
Error::<TestRuntime, DefaultInstance>::InvalidMessagesDispatchWeight,
@@ -1010,6 +1008,7 @@ mod tests {
Origin::signed(1),
TEST_RELAYER_A,
Err(()).into(),
1,
0,
),
Error::<TestRuntime, DefaultInstance>::InvalidMessagesProof,
@@ -1112,6 +1111,7 @@ mod tests {
Origin::signed(1),
TEST_RELAYER_A,
Ok(vec![invalid_message]).into(),
1,
0, // weight may be zero in this case (all messages are improperly encoded)
),);
@@ -1134,6 +1134,7 @@ mod tests {
message(3, REGULAR_PAYLOAD),
])
.into(),
3,
REGULAR_PAYLOAD.1 + REGULAR_PAYLOAD.1,
),);
+1 -3
View File
@@ -97,7 +97,6 @@ parameter_types! {
pub const MaxMessagesToPruneAtOnce: u64 = 10;
pub const MaxUnrewardedRelayerEntriesAtInboundLane: u64 = 16;
pub const MaxUnconfirmedMessagesAtInboundLane: u64 = 32;
pub const MaxMessagesInDeliveryTransaction: u64 = 128;
}
impl Config for TestRuntime {
@@ -105,7 +104,6 @@ impl Config for TestRuntime {
type MaxMessagesToPruneAtOnce = MaxMessagesToPruneAtOnce;
type MaxUnrewardedRelayerEntriesAtInboundLane = MaxUnrewardedRelayerEntriesAtInboundLane;
type MaxUnconfirmedMessagesAtInboundLane = MaxUnconfirmedMessagesAtInboundLane;
type MaxMessagesInDeliveryTransaction = MaxMessagesInDeliveryTransaction;
type OutboundPayload = TestPayload;
type OutboundMessageFee = TestMessageFee;
@@ -279,7 +277,7 @@ impl SourceHeaderChain<TestMessageFee> for TestSourceHeaderChain {
fn verify_messages_proof(
proof: Self::MessagesProof,
_max_messages: MessageNonce,
_messages_count: MessageNonce,
) -> Result<ProvedMessages<Message<TestMessageFee>>, Self::Error> {
proof
.result