Skip to content

Commit

Permalink
Increase message fee call (paritytech#718)
Browse files Browse the repository at this point in the history
* fn increase_message_fee()

* benchmarks + weights

* - extra lines

* split error
  • Loading branch information
svyatonik authored and serban300 committed Apr 10, 2024
1 parent 67da592 commit c879742
Show file tree
Hide file tree
Showing 3 changed files with 231 additions and 87 deletions.
105 changes: 65 additions & 40 deletions bridges/modules/message-lane/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ pub struct MessageDeliveryProofParams<ThisChainAccountId> {

/// Trait that must be implemented by runtime.
pub trait Config<I: Instance>: crate::Config<I> {
/// Lane id to use in benchmarks.
fn bench_lane_id() -> LaneId {
Default::default()
}
/// Get maximal size of the message payload.
fn maximal_message_size() -> u32;
/// Return id of relayer account at the bridged chain.
Expand Down Expand Up @@ -121,7 +125,7 @@ benchmarks_instance! {
// (estimated using `send_half_maximal_message_worst_case` and `send_maximal_message_worst_case`) is
// added.
send_minimal_message_worst_case {
let lane_id = bench_lane_id();
let lane_id = T::bench_lane_id();
let sender = account("sender", 0, SEED);
T::endow_account(&sender);

Expand All @@ -138,7 +142,7 @@ benchmarks_instance! {
}: send_message(RawOrigin::Signed(sender), lane_id, payload, fee)
verify {
assert_eq!(
crate::Module::<T, I>::outbound_latest_generated_nonce(bench_lane_id()),
crate::Module::<T, I>::outbound_latest_generated_nonce(T::bench_lane_id()),
T::MaxMessagesToPruneAtOnce::get() + 1,
);
}
Expand All @@ -152,7 +156,7 @@ benchmarks_instance! {
// With single KB of message size, the weight of the call is increased (roughly) by
// `(send_16_kb_message_worst_case - send_1_kb_message_worst_case) / 15`.
send_1_kb_message_worst_case {
let lane_id = bench_lane_id();
let lane_id = T::bench_lane_id();
let sender = account("sender", 0, SEED);
T::endow_account(&sender);

Expand All @@ -175,7 +179,7 @@ benchmarks_instance! {
}: send_message(RawOrigin::Signed(sender), lane_id, payload, fee)
verify {
assert_eq!(
crate::Module::<T, I>::outbound_latest_generated_nonce(bench_lane_id()),
crate::Module::<T, I>::outbound_latest_generated_nonce(T::bench_lane_id()),
T::MaxMessagesToPruneAtOnce::get() + 1,
);
}
Expand All @@ -189,7 +193,7 @@ benchmarks_instance! {
// With single KB of message size, the weight of the call is increased (roughly) by
// `(send_16_kb_message_worst_case - send_1_kb_message_worst_case) / 15`.
send_16_kb_message_worst_case {
let lane_id = bench_lane_id();
let lane_id = T::bench_lane_id();
let sender = account("sender", 0, SEED);
T::endow_account(&sender);

Expand All @@ -212,11 +216,28 @@ benchmarks_instance! {
}: send_message(RawOrigin::Signed(sender), lane_id, payload, fee)
verify {
assert_eq!(
crate::Module::<T, I>::outbound_latest_generated_nonce(bench_lane_id()),
crate::Module::<T, I>::outbound_latest_generated_nonce(T::bench_lane_id()),
T::MaxMessagesToPruneAtOnce::get() + 1,
);
}

// Benchmark `increase_message_fee` with following conditions:
// * message has maximal message;
// * submitter account is killed because its balance is less than ED after payment.
increase_message_fee {
let sender = account("sender", 42, SEED);
T::endow_account(&sender);

let additional_fee = T::account_balance(&sender);
let lane_id = T::bench_lane_id();
let nonce = 1;

send_regular_message_with_payload::<T, I>(vec![42u8; T::maximal_message_size() as _]);
}: increase_message_fee(RawOrigin::Signed(sender.clone()), lane_id, nonce, additional_fee)
verify {
assert_eq!(T::account_balance(&sender), 0.into());
}

// Benchmark `receive_messages_proof` extrinsic with single minimal-weight message and following conditions:
// * proof does not include outbound lane state proof;
// * inbound lane already has state, so it needs to be read and decoded;
Expand All @@ -232,15 +253,15 @@ benchmarks_instance! {
receive_messages::<T, I>(20);

let (proof, dispatch_weight) = T::prepare_message_proof(MessageProofParams {
lane: bench_lane_id(),
lane: T::bench_lane_id(),
message_nonces: 21..=21,
outbound_lane_data: None,
size: ProofSize::Minimal(EXPECTED_DEFAULT_MESSAGE_LENGTH),
});
}: 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()),
crate::Module::<T, I>::inbound_latest_received_nonce(T::bench_lane_id()),
21,
);
}
Expand All @@ -263,15 +284,15 @@ benchmarks_instance! {
receive_messages::<T, I>(20);

let (proof, dispatch_weight) = T::prepare_message_proof(MessageProofParams {
lane: bench_lane_id(),
lane: T::bench_lane_id(),
message_nonces: 21..=22,
outbound_lane_data: None,
size: ProofSize::Minimal(EXPECTED_DEFAULT_MESSAGE_LENGTH),
});
}: 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()),
crate::Module::<T, I>::inbound_latest_received_nonce(T::bench_lane_id()),
22,
);
}
Expand All @@ -294,7 +315,7 @@ benchmarks_instance! {
receive_messages::<T, I>(20);

let (proof, dispatch_weight) = T::prepare_message_proof(MessageProofParams {
lane: bench_lane_id(),
lane: T::bench_lane_id(),
message_nonces: 21..=21,
outbound_lane_data: Some(OutboundLaneData {
oldest_unpruned_nonce: 21,
Expand All @@ -306,11 +327,11 @@ benchmarks_instance! {
}: 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()),
crate::Module::<T, I>::inbound_latest_received_nonce(T::bench_lane_id()),
21,
);
assert_eq!(
crate::Module::<T, I>::inbound_latest_confirmed_nonce(bench_lane_id()),
crate::Module::<T, I>::inbound_latest_confirmed_nonce(T::bench_lane_id()),
20,
);
}
Expand All @@ -332,15 +353,15 @@ benchmarks_instance! {
receive_messages::<T, I>(20);

let (proof, dispatch_weight) = T::prepare_message_proof(MessageProofParams {
lane: bench_lane_id(),
lane: T::bench_lane_id(),
message_nonces: 21..=21,
outbound_lane_data: None,
size: ProofSize::HasExtraNodes(1024),
});
}: 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()),
crate::Module::<T, I>::inbound_latest_received_nonce(T::bench_lane_id()),
21,
);
}
Expand All @@ -364,15 +385,15 @@ benchmarks_instance! {
receive_messages::<T, I>(20);

let (proof, dispatch_weight) = T::prepare_message_proof(MessageProofParams {
lane: bench_lane_id(),
lane: T::bench_lane_id(),
message_nonces: 21..=21,
outbound_lane_data: None,
size: ProofSize::HasExtraNodes(16 * 1024),
});
}: 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()),
crate::Module::<T, I>::inbound_latest_received_nonce(T::bench_lane_id()),
21,
);
}
Expand All @@ -397,7 +418,7 @@ benchmarks_instance! {
total_messages: 1,
};
let proof = T::prepare_message_delivery_proof(MessageDeliveryProofParams {
lane: bench_lane_id(),
lane: T::bench_lane_id(),
inbound_lane_data: InboundLaneData {
relayers: vec![(1, 1, relayer_id.clone())].into_iter().collect(),
last_confirmed_nonce: 0,
Expand Down Expand Up @@ -435,7 +456,7 @@ benchmarks_instance! {
total_messages: 2,
};
let proof = T::prepare_message_delivery_proof(MessageDeliveryProofParams {
lane: bench_lane_id(),
lane: T::bench_lane_id(),
inbound_lane_data: InboundLaneData {
relayers: vec![(1, 2, relayer_id.clone())].into_iter().collect(),
last_confirmed_nonce: 0,
Expand Down Expand Up @@ -472,7 +493,7 @@ benchmarks_instance! {
total_messages: 2,
};
let proof = T::prepare_message_delivery_proof(MessageDeliveryProofParams {
lane: bench_lane_id(),
lane: T::bench_lane_id(),
inbound_lane_data: InboundLaneData {
relayers: vec![
(1, 1, relayer1_id.clone()),
Expand Down Expand Up @@ -502,7 +523,7 @@ benchmarks_instance! {
send_messages_of_various_lengths {
let i in 0..T::maximal_message_size().try_into().unwrap_or_default();

let lane_id = bench_lane_id();
let lane_id = T::bench_lane_id();
let sender = account("sender", 0, SEED);
T::endow_account(&sender);

Expand All @@ -519,7 +540,7 @@ benchmarks_instance! {
}: send_message(RawOrigin::Signed(sender), lane_id, payload, fee)
verify {
assert_eq!(
crate::Module::<T, I>::outbound_latest_generated_nonce(bench_lane_id()),
crate::Module::<T, I>::outbound_latest_generated_nonce(T::bench_lane_id()),
T::MaxMessagesToPruneAtOnce::get() + 1,
);
}
Expand All @@ -544,7 +565,7 @@ benchmarks_instance! {
receive_messages::<T, I>(20);

let (proof, dispatch_weight) = T::prepare_message_proof(MessageProofParams {
lane: bench_lane_id(),
lane: T::bench_lane_id(),
message_nonces: 21..=(20 + i as MessageNonce),
outbound_lane_data: None,
size: ProofSize::Minimal(EXPECTED_DEFAULT_MESSAGE_LENGTH),
Expand All @@ -558,7 +579,7 @@ benchmarks_instance! {
)
verify {
assert_eq!(
crate::Module::<T, I>::inbound_latest_received_nonce(bench_lane_id()),
crate::Module::<T, I>::inbound_latest_received_nonce(T::bench_lane_id()),
20 + i as MessageNonce,
);
}
Expand All @@ -581,7 +602,7 @@ benchmarks_instance! {
receive_messages::<T, I>(20);

let (proof, dispatch_weight) = T::prepare_message_proof(MessageProofParams {
lane: bench_lane_id(),
lane: T::bench_lane_id(),
message_nonces: 21..=21,
outbound_lane_data: None,
size: ProofSize::HasExtraNodes(i as _),
Expand All @@ -595,7 +616,7 @@ benchmarks_instance! {
)
verify {
assert_eq!(
crate::Module::<T, I>::inbound_latest_received_nonce(bench_lane_id()),
crate::Module::<T, I>::inbound_latest_received_nonce(T::bench_lane_id()),
21,
);
}
Expand All @@ -618,7 +639,7 @@ benchmarks_instance! {
receive_messages::<T, I>(20);

let (proof, dispatch_weight) = T::prepare_message_proof(MessageProofParams {
lane: bench_lane_id(),
lane: T::bench_lane_id(),
message_nonces: 21..=21,
outbound_lane_data: None,
size: ProofSize::HasLargeLeaf(i as _),
Expand All @@ -632,7 +653,7 @@ benchmarks_instance! {
)
verify {
assert_eq!(
crate::Module::<T, I>::inbound_latest_received_nonce(bench_lane_id()),
crate::Module::<T, I>::inbound_latest_received_nonce(T::bench_lane_id()),
21,
);
}
Expand All @@ -657,7 +678,7 @@ benchmarks_instance! {
receive_messages::<T, I>(20);

let (proof, dispatch_weight) = T::prepare_message_proof(MessageProofParams {
lane: bench_lane_id(),
lane: T::bench_lane_id(),
message_nonces: 21..=20 + i as MessageNonce,
outbound_lane_data: Some(OutboundLaneData {
oldest_unpruned_nonce: 21,
Expand All @@ -675,11 +696,11 @@ benchmarks_instance! {
)
verify {
assert_eq!(
crate::Module::<T, I>::inbound_latest_received_nonce(bench_lane_id()),
crate::Module::<T, I>::inbound_latest_received_nonce(T::bench_lane_id()),
20 + i as MessageNonce,
);
assert_eq!(
crate::Module::<T, I>::inbound_latest_confirmed_nonce(bench_lane_id()),
crate::Module::<T, I>::inbound_latest_confirmed_nonce(T::bench_lane_id()),
20,
);
}
Expand Down Expand Up @@ -708,7 +729,7 @@ benchmarks_instance! {
total_messages: i as MessageNonce,
};
let proof = T::prepare_message_delivery_proof(MessageDeliveryProofParams {
lane: bench_lane_id(),
lane: T::bench_lane_id(),
inbound_lane_data: InboundLaneData {
relayers: vec![(1, i as MessageNonce, relayer_id.clone())].into_iter().collect(),
last_confirmed_nonce: 0,
Expand Down Expand Up @@ -750,7 +771,7 @@ benchmarks_instance! {
total_messages: i as MessageNonce,
};
let proof = T::prepare_message_delivery_proof(MessageDeliveryProofParams {
lane: bench_lane_id(),
lane: T::bench_lane_id(),
inbound_lane_data: InboundLaneData {
relayers: relayers
.keys()
Expand All @@ -769,25 +790,29 @@ benchmarks_instance! {
}
}

fn bench_lane_id() -> LaneId {
*b"test"
}

fn send_regular_message<T: Config<I>, I: Instance>() {
let mut outbound_lane = outbound_lane::<T, I>(bench_lane_id());
let mut outbound_lane = outbound_lane::<T, I>(T::bench_lane_id());
outbound_lane.send_message(MessageData {
payload: vec![],
fee: MESSAGE_FEE.into(),
});
}

fn send_regular_message_with_payload<T: Config<I>, I: Instance>(payload: Vec<u8>) {
let mut outbound_lane = outbound_lane::<T, I>(T::bench_lane_id());
outbound_lane.send_message(MessageData {
payload,
fee: MESSAGE_FEE.into(),
});
}

fn confirm_message_delivery<T: Config<I>, I: Instance>(nonce: MessageNonce) {
let mut outbound_lane = outbound_lane::<T, I>(bench_lane_id());
let mut outbound_lane = outbound_lane::<T, I>(T::bench_lane_id());
assert!(outbound_lane.confirm_delivery(nonce).is_some());
}

fn receive_messages<T: Config<I>, I: Instance>(nonce: MessageNonce) {
let mut inbound_lane_storage = inbound_lane_storage::<T, I>(bench_lane_id());
let mut inbound_lane_storage = inbound_lane_storage::<T, I>(T::bench_lane_id());
inbound_lane_storage.set_data(InboundLaneData {
relayers: vec![(1, nonce, T::bridged_relayer_id())].into_iter().collect(),
last_confirmed_nonce: 0,
Expand Down
Loading

0 comments on commit c879742

Please sign in to comment.