referrerpolicy=no-referrer-when-downgrade
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Parity Bridges Common.

// Parity Bridges Common is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Parity Bridges Common is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Parity Bridges Common.  If not, see <http://www.gnu.org/licenses/>.

//! Messages pallet benchmarking.

#![cfg(feature = "runtime-benchmarks")]

use crate::{
	active_outbound_lane, weights_ext::EXPECTED_DEFAULT_MESSAGE_LENGTH, BridgedChainOf, Call,
	InboundLanes, OutboundLanes,
};

use bp_messages::{
	source_chain::FromBridgedChainMessagesDeliveryProof,
	target_chain::FromBridgedChainMessagesProof, ChainWithMessages, DeliveredMessages,
	InboundLaneData, LaneState, MessageNonce, OutboundLaneData, UnrewardedRelayer,
	UnrewardedRelayersState,
};
use bp_runtime::{AccountIdOf, HashOf, UnverifiedStorageProofParams};
use codec::Decode;
use frame_benchmarking::{account, v2::*};
use frame_support::weights::Weight;
use frame_system::RawOrigin;
use sp_runtime::{traits::TrailingZeroInput, BoundedVec};
use sp_std::{ops::RangeInclusive, prelude::*};

const SEED: u32 = 0;

/// Pallet we're benchmarking here.
pub struct Pallet<T: Config<I>, I: 'static = ()>(crate::Pallet<T, I>);

/// Benchmark-specific message proof parameters.
#[derive(Debug)]
pub struct MessageProofParams<LaneId> {
	/// Id of the lane.
	pub lane: LaneId,
	/// Range of messages to include in the proof.
	pub message_nonces: RangeInclusive<MessageNonce>,
	/// If `Some`, the proof needs to include this outbound lane data.
	pub outbound_lane_data: Option<OutboundLaneData>,
	/// If `true`, the caller expects that the proof will contain correct messages that will
	/// be successfully dispatched. This is only called from the "optional"
	/// `receive_single_message_proof_with_dispatch` benchmark. If you don't need it, just
	/// return `true` from the `is_message_successfully_dispatched`.
	pub is_successful_dispatch_expected: bool,
	/// Proof size requirements.
	pub proof_params: UnverifiedStorageProofParams,
}

/// Benchmark-specific message delivery proof parameters.
#[derive(Debug)]
pub struct MessageDeliveryProofParams<ThisChainAccountId, LaneId> {
	/// Id of the lane.
	pub lane: LaneId,
	/// The proof needs to include this inbound lane data.
	pub inbound_lane_data: InboundLaneData<ThisChainAccountId>,
	/// Proof size requirements.
	pub proof_params: UnverifiedStorageProofParams,
}

/// Trait that must be implemented by runtime.
pub trait Config<I: 'static>: crate::Config<I> {
	/// Lane id to use in benchmarks.
	fn bench_lane_id() -> Self::LaneId {
		Self::LaneId::default()
	}

	/// Return id of relayer account at the bridged chain.
	///
	/// By default, zero account is returned.
	fn bridged_relayer_id() -> AccountIdOf<BridgedChainOf<Self, I>> {
		Decode::decode(&mut TrailingZeroInput::zeroes()).unwrap()
	}

	/// Create given account and give it enough balance for test purposes. Used to create
	/// relayer account at the target chain. Is strictly necessary when your rewards scheme
	/// assumes that the relayer account must exist.
	///
	/// Does nothing by default.
	fn endow_account(_account: &Self::AccountId) {}

	/// Prepare messages proof to receive by the module.
	fn prepare_message_proof(
		params: MessageProofParams<Self::LaneId>,
	) -> (FromBridgedChainMessagesProof<HashOf<BridgedChainOf<Self, I>>, Self::LaneId>, Weight);
	/// Prepare messages delivery proof to receive by the module.
	fn prepare_message_delivery_proof(
		params: MessageDeliveryProofParams<Self::AccountId, Self::LaneId>,
	) -> FromBridgedChainMessagesDeliveryProof<HashOf<BridgedChainOf<Self, I>>, Self::LaneId>;

	/// Returns true if message has been successfully dispatched or not.
	fn is_message_successfully_dispatched(_nonce: MessageNonce) -> bool {
		true
	}

	/// Returns true if given relayer has been rewarded for some of its actions.
	fn is_relayer_rewarded(relayer: &Self::AccountId) -> bool;
}

fn send_regular_message<T: Config<I>, I: 'static>() {
	OutboundLanes::<T, I>::insert(
		T::bench_lane_id(),
		OutboundLaneData {
			state: LaneState::Opened,
			latest_generated_nonce: 1,
			..Default::default()
		},
	);

	let mut outbound_lane = active_outbound_lane::<T, I>(T::bench_lane_id()).unwrap();
	outbound_lane.send_message(BoundedVec::try_from(vec![]).expect("We craft valid messages"));
}

fn receive_messages<T: Config<I>, I: 'static>(nonce: MessageNonce) {
	InboundLanes::<T, I>::insert(
		T::bench_lane_id(),
		InboundLaneData {
			state: LaneState::Opened,
			relayers: vec![UnrewardedRelayer {
				relayer: T::bridged_relayer_id(),
				messages: DeliveredMessages::new(nonce),
			}]
			.into(),
			last_confirmed_nonce: 0,
		},
	);
}

struct ReceiveMessagesProofSetup<T: Config<I>, I: 'static> {
	relayer_id_on_src: AccountIdOf<BridgedChainOf<T, I>>,
	relayer_id_on_tgt: T::AccountId,
	msgs_count: u32,
	_phantom_data: sp_std::marker::PhantomData<I>,
}

impl<T: Config<I>, I: 'static> ReceiveMessagesProofSetup<T, I> {
	const LATEST_RECEIVED_NONCE: MessageNonce = 20;

	fn new(msgs_count: u32) -> Self {
		let setup = Self {
			relayer_id_on_src: T::bridged_relayer_id(),
			relayer_id_on_tgt: account("relayer", 0, SEED),
			msgs_count,
			_phantom_data: Default::default(),
		};
		T::endow_account(&setup.relayer_id_on_tgt);
		// mark messages 1..=latest_recvd_nonce as delivered
		receive_messages::<T, I>(Self::LATEST_RECEIVED_NONCE);

		setup
	}

	fn relayer_id_on_src(&self) -> AccountIdOf<BridgedChainOf<T, I>> {
		self.relayer_id_on_src.clone()
	}

	fn relayer_id_on_tgt(&self) -> T::AccountId {
		self.relayer_id_on_tgt.clone()
	}

	fn last_nonce(&self) -> MessageNonce {
		Self::LATEST_RECEIVED_NONCE + self.msgs_count as u64
	}

	fn nonces(&self) -> RangeInclusive<MessageNonce> {
		(Self::LATEST_RECEIVED_NONCE + 1)..=self.last_nonce()
	}

	fn check_last_nonce(&self) {
		assert_eq!(
			crate::InboundLanes::<T, I>::get(&T::bench_lane_id()).map(|d| d.last_delivered_nonce()),
			Some(self.last_nonce()),
		);
	}
}

#[instance_benchmarks]
mod benchmarks {
	use super::*;

	//
	// Benchmarks that are used directly by the runtime calls weight formulae.
	//

	fn max_msgs<T: Config<I>, I: 'static>() -> u32 {
		T::BridgedChain::MAX_UNCONFIRMED_MESSAGES_IN_CONFIRMATION_TX as u32 -
			ReceiveMessagesProofSetup::<T, I>::LATEST_RECEIVED_NONCE as u32
	}

	// 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;
	// * message is dispatched (reminder: dispatch weight should be minimal);
	// * message requires all heavy checks done by dispatcher.
	#[benchmark]
	fn receive_single_message_proof() {
		// setup code
		let setup = ReceiveMessagesProofSetup::<T, I>::new(1);
		let (proof, dispatch_weight) = T::prepare_message_proof(MessageProofParams {
			lane: T::bench_lane_id(),
			message_nonces: setup.nonces(),
			outbound_lane_data: None,
			is_successful_dispatch_expected: false,
			proof_params: UnverifiedStorageProofParams::from_db_size(
				EXPECTED_DEFAULT_MESSAGE_LENGTH,
			),
		});

		#[extrinsic_call]
		receive_messages_proof(
			RawOrigin::Signed(setup.relayer_id_on_tgt()),
			setup.relayer_id_on_src(),
			Box::new(proof),
			setup.msgs_count,
			dispatch_weight,
		);

		// verification code
		setup.check_last_nonce();
	}

	// Benchmark `receive_messages_proof` extrinsic with `n` minimal-weight messages and following
	// conditions:
	// * proof does not include outbound lane state proof;
	// * inbound lane already has state, so it needs to be read and decoded;
	// * message is dispatched (reminder: dispatch weight should be minimal);
	// * message requires all heavy checks done by dispatcher.
	#[benchmark]
	fn receive_n_messages_proof(n: Linear<1, { max_msgs::<T, I>() }>) {
		// setup code
		let setup = ReceiveMessagesProofSetup::<T, I>::new(n);
		let (proof, dispatch_weight) = T::prepare_message_proof(MessageProofParams {
			lane: T::bench_lane_id(),
			message_nonces: setup.nonces(),
			outbound_lane_data: None,
			is_successful_dispatch_expected: false,
			proof_params: UnverifiedStorageProofParams::from_db_size(
				EXPECTED_DEFAULT_MESSAGE_LENGTH,
			),
		});

		#[extrinsic_call]
		receive_messages_proof(
			RawOrigin::Signed(setup.relayer_id_on_tgt()),
			setup.relayer_id_on_src(),
			Box::new(proof),
			setup.msgs_count,
			dispatch_weight,
		);

		// verification code
		setup.check_last_nonce();
	}

	// Benchmark `receive_messages_proof` extrinsic with single minimal-weight message and following
	// conditions:
	// * proof includes outbound lane state proof;
	// * inbound lane already has state, so it needs to be read and decoded;
	// * message is successfully dispatched (reminder: dispatch weight should be minimal);
	// * message requires all heavy checks done by dispatcher.
	//
	// The weight of outbound lane state delivery would be
	// `weight(receive_single_message_proof_with_outbound_lane_state) -
	// weight(receive_single_message_proof)`. This won't be super-accurate if message has non-zero
	// dispatch weight, but estimation should be close enough to real weight.
	#[benchmark]
	fn receive_single_message_proof_with_outbound_lane_state() {
		// setup code
		let setup = ReceiveMessagesProofSetup::<T, I>::new(1);
		let (proof, dispatch_weight) = T::prepare_message_proof(MessageProofParams {
			lane: T::bench_lane_id(),
			message_nonces: setup.nonces(),
			outbound_lane_data: Some(OutboundLaneData {
				state: LaneState::Opened,
				oldest_unpruned_nonce: setup.last_nonce(),
				latest_received_nonce: ReceiveMessagesProofSetup::<T, I>::LATEST_RECEIVED_NONCE,
				latest_generated_nonce: setup.last_nonce(),
			}),
			is_successful_dispatch_expected: false,
			proof_params: UnverifiedStorageProofParams::from_db_size(
				EXPECTED_DEFAULT_MESSAGE_LENGTH,
			),
		});

		#[extrinsic_call]
		receive_messages_proof(
			RawOrigin::Signed(setup.relayer_id_on_tgt()),
			setup.relayer_id_on_src(),
			Box::new(proof),
			setup.msgs_count,
			dispatch_weight,
		);

		// verification code
		setup.check_last_nonce();
	}

	// Benchmark `receive_messages_proof` extrinsic with single minimal-weight message and following
	// conditions:
	// * the proof has large leaf with total size ranging between 1KB and 16KB;
	// * proof does not include outbound lane state proof;
	// * inbound lane already has state, so it needs to be read and decoded;
	// * message is dispatched (reminder: dispatch weight should be minimal);
	// * message requires all heavy checks done by dispatcher.
	#[benchmark]
	fn receive_single_n_bytes_message_proof(
		/// Proof size in KB
		n: Linear<1, { 16 * 1024 }>,
	) {
		// setup code
		let setup = ReceiveMessagesProofSetup::<T, I>::new(1);
		let (proof, dispatch_weight) = T::prepare_message_proof(MessageProofParams {
			lane: T::bench_lane_id(),
			message_nonces: setup.nonces(),
			outbound_lane_data: None,
			is_successful_dispatch_expected: false,
			proof_params: UnverifiedStorageProofParams::from_db_size(n),
		});

		#[extrinsic_call]
		receive_messages_proof(
			RawOrigin::Signed(setup.relayer_id_on_tgt()),
			setup.relayer_id_on_src(),
			Box::new(proof),
			setup.msgs_count,
			dispatch_weight,
		);

		// verification code
		setup.check_last_nonce();
	}

	// Benchmark `receive_messages_delivery_proof` extrinsic with following conditions:
	// * single relayer is rewarded for relaying single message;
	// * relayer account does not exist (in practice it needs to exist in production environment).
	//
	// This is base benchmark for all other confirmations delivery benchmarks.
	#[benchmark]
	fn receive_delivery_proof_for_single_message() {
		let relayer_id: T::AccountId = account("relayer", 0, SEED);

		// send message that we're going to confirm
		send_regular_message::<T, I>();

		let relayers_state = UnrewardedRelayersState {
			unrewarded_relayer_entries: 1,
			messages_in_oldest_entry: 1,
			total_messages: 1,
			last_delivered_nonce: 1,
		};
		let proof = T::prepare_message_delivery_proof(MessageDeliveryProofParams {
			lane: T::bench_lane_id(),
			inbound_lane_data: InboundLaneData {
				state: LaneState::Opened,
				relayers: vec![UnrewardedRelayer {
					relayer: relayer_id.clone(),
					messages: DeliveredMessages::new(1),
				}]
				.into_iter()
				.collect(),
				last_confirmed_nonce: 0,
			},
			proof_params: UnverifiedStorageProofParams::default(),
		});

		#[extrinsic_call]
		receive_messages_delivery_proof(
			RawOrigin::Signed(relayer_id.clone()),
			proof,
			relayers_state,
		);

		assert_eq!(
			OutboundLanes::<T, I>::get(T::bench_lane_id()).map(|s| s.latest_received_nonce),
			Some(1)
		);
		assert!(T::is_relayer_rewarded(&relayer_id));
	}

	// Benchmark `receive_messages_delivery_proof` extrinsic with following conditions:
	// * single relayer is rewarded for relaying two messages;
	// * relayer account does not exist (in practice it needs to exist in production environment).
	//
	// Additional weight for paying single-message reward to the same relayer could be computed
	// as `weight(receive_delivery_proof_for_two_messages_by_single_relayer)
	//   - weight(receive_delivery_proof_for_single_message)`.
	#[benchmark]
	fn receive_delivery_proof_for_two_messages_by_single_relayer() {
		let relayer_id: T::AccountId = account("relayer", 0, SEED);

		// send message that we're going to confirm
		send_regular_message::<T, I>();
		send_regular_message::<T, I>();

		let relayers_state = UnrewardedRelayersState {
			unrewarded_relayer_entries: 1,
			messages_in_oldest_entry: 2,
			total_messages: 2,
			last_delivered_nonce: 2,
		};
		let mut delivered_messages = DeliveredMessages::new(1);
		delivered_messages.note_dispatched_message();
		let proof = T::prepare_message_delivery_proof(MessageDeliveryProofParams {
			lane: T::bench_lane_id(),
			inbound_lane_data: InboundLaneData {
				state: LaneState::Opened,
				relayers: vec![UnrewardedRelayer {
					relayer: relayer_id.clone(),
					messages: delivered_messages,
				}]
				.into_iter()
				.collect(),
				last_confirmed_nonce: 0,
			},
			proof_params: UnverifiedStorageProofParams::default(),
		});

		#[extrinsic_call]
		receive_messages_delivery_proof(
			RawOrigin::Signed(relayer_id.clone()),
			proof,
			relayers_state,
		);

		assert_eq!(
			OutboundLanes::<T, I>::get(T::bench_lane_id()).map(|s| s.latest_received_nonce),
			Some(2)
		);
		assert!(T::is_relayer_rewarded(&relayer_id));
	}

	// Benchmark `receive_messages_delivery_proof` extrinsic with following conditions:
	// * two relayers are rewarded for relaying single message each;
	// * relayer account does not exist (in practice it needs to exist in production environment).
	//
	// Additional weight for paying reward to the next relayer could be computed
	// as `weight(receive_delivery_proof_for_two_messages_by_two_relayers)
	//   - weight(receive_delivery_proof_for_two_messages_by_single_relayer)`.
	#[benchmark]
	fn receive_delivery_proof_for_two_messages_by_two_relayers() {
		let relayer1_id: T::AccountId = account("relayer1", 1, SEED);
		let relayer2_id: T::AccountId = account("relayer2", 2, SEED);

		// send message that we're going to confirm
		send_regular_message::<T, I>();
		send_regular_message::<T, I>();

		let relayers_state = UnrewardedRelayersState {
			unrewarded_relayer_entries: 2,
			messages_in_oldest_entry: 1,
			total_messages: 2,
			last_delivered_nonce: 2,
		};
		let proof = T::prepare_message_delivery_proof(MessageDeliveryProofParams {
			lane: T::bench_lane_id(),
			inbound_lane_data: InboundLaneData {
				state: LaneState::Opened,
				relayers: vec![
					UnrewardedRelayer {
						relayer: relayer1_id.clone(),
						messages: DeliveredMessages::new(1),
					},
					UnrewardedRelayer {
						relayer: relayer2_id.clone(),
						messages: DeliveredMessages::new(2),
					},
				]
				.into_iter()
				.collect(),
				last_confirmed_nonce: 0,
			},
			proof_params: UnverifiedStorageProofParams::default(),
		});

		#[extrinsic_call]
		receive_messages_delivery_proof(
			RawOrigin::Signed(relayer1_id.clone()),
			proof,
			relayers_state,
		);

		assert_eq!(
			OutboundLanes::<T, I>::get(T::bench_lane_id()).map(|s| s.latest_received_nonce),
			Some(2)
		);
		assert!(T::is_relayer_rewarded(&relayer1_id));
		assert!(T::is_relayer_rewarded(&relayer2_id));
	}

	//
	// Benchmarks that the runtime developers may use for proper pallet configuration.
	//

	// This benchmark is optional and may be used when runtime developer need a way to compute
	// message dispatch weight. In this case, he needs to provide messages that can go the whole
	// dispatch
	//
	// Benchmark `receive_messages_proof` extrinsic with single 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;
	// * message is **SUCCESSFULLY** dispatched;
	// * message requires all heavy checks done by dispatcher.
	#[benchmark]
	fn receive_single_n_bytes_message_proof_with_dispatch(
		/// Proof size in KB
		n: Linear<1, { 16 * 1024 }>,
	) {
		// setup code
		let setup = ReceiveMessagesProofSetup::<T, I>::new(1);
		let (proof, dispatch_weight) = T::prepare_message_proof(MessageProofParams {
			lane: T::bench_lane_id(),
			message_nonces: setup.nonces(),
			outbound_lane_data: None,
			is_successful_dispatch_expected: true,
			proof_params: UnverifiedStorageProofParams::from_db_size(n),
		});

		#[extrinsic_call]
		receive_messages_proof(
			RawOrigin::Signed(setup.relayer_id_on_tgt()),
			setup.relayer_id_on_src(),
			Box::new(proof),
			setup.msgs_count,
			dispatch_weight,
		);

		// verification code
		setup.check_last_nonce();
		assert!(T::is_message_successfully_dispatched(setup.last_nonce()));
	}

	impl_benchmark_test_suite!(
		Pallet,
		crate::tests::mock::new_test_ext(),
		crate::tests::mock::TestRuntime
	);
}