referrerpolicy=no-referrer-when-downgrade

snowbridge_pallet_outbound_queue_v2/
benchmarking.rs

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
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2023 Snowfork <hello@snowfork.com>
use super::*;

use bridge_hub_common::AggregateMessageOrigin;
use codec::Encode;
use frame_benchmarking::v2::*;
use frame_support::{traits::Hooks, BoundedVec};
use snowbridge_outbound_queue_primitives::v2::{Command, Initializer, Message};
use sp_core::{H160, H256};

#[allow(unused_imports)]
use crate::Pallet as OutboundQueue;

#[benchmarks(
	where
		<T as Config>::MaxMessagePayloadSize: Get<u32>,
)]
mod benchmarks {
	use super::*;

	/// Build `Upgrade` message with `MaxMessagePayloadSize`, in the worst-case.
	fn build_message<T: Config>() -> (Message, OutboundMessage) {
		let commands = vec![Command::Upgrade {
			impl_address: H160::zero(),
			impl_code_hash: H256::zero(),
			initializer: Initializer {
				params: core::iter::repeat_with(|| 1_u8)
					.take(<T as Config>::MaxMessagePayloadSize::get() as usize)
					.collect(),
				maximum_required_gas: 200_000,
			},
		}];
		let message = Message {
			origin: Default::default(),
			id: H256::default(),
			fee: 0,
			commands: BoundedVec::try_from(commands.clone()).unwrap(),
		};
		let wrapped_commands: Vec<OutboundCommandWrapper> = commands
			.into_iter()
			.map(|command| OutboundCommandWrapper {
				kind: command.index(),
				gas: T::GasMeter::maximum_dispatch_gas_used_at_most(&command),
				payload: command.abi_encode(),
			})
			.collect();
		let outbound_message = OutboundMessage {
			origin: Default::default(),
			nonce: 1,
			topic: H256::default(),
			commands: wrapped_commands.clone().try_into().unwrap(),
		};
		(message, outbound_message)
	}

	/// Initialize `MaxMessagesPerBlock` messages need to be committed, in the worst-case.
	fn initialize_worst_case<T: Config>() {
		for _ in 0..T::MaxMessagesPerBlock::get() {
			initialize_with_one_message::<T>();
		}
	}

	/// Initialize with a single message
	fn initialize_with_one_message<T: Config>() {
		let (message, outbound_message) = build_message::<T>();
		let leaf = <T as Config>::Hashing::hash(&message.encode());
		MessageLeaves::<T>::append(leaf);
		Messages::<T>::append(outbound_message);
	}

	/// Benchmark for processing a message.
	#[benchmark]
	fn do_process_message() -> Result<(), BenchmarkError> {
		let (enqueued_message, _) = build_message::<T>();
		let origin = AggregateMessageOrigin::SnowbridgeV2([1; 32].into());
		let message = enqueued_message.encode();

		#[block]
		{
			let _ = OutboundQueue::<T>::do_process_message(origin, &message).unwrap();
		}

		assert_eq!(MessageLeaves::<T>::decode_len().unwrap(), 1);

		Ok(())
	}

	/// Benchmark for producing final messages commitment, in the worst-case
	#[benchmark]
	fn commit() -> Result<(), BenchmarkError> {
		initialize_worst_case::<T>();

		#[block]
		{
			OutboundQueue::<T>::commit();
		}

		Ok(())
	}

	/// Benchmark for producing commitment for a single message, used to estimate the delivery
	/// cost. The assumption is that cost of commit a single message is even higher than the average
	/// cost of commit all messages.
	#[benchmark]
	fn commit_single() -> Result<(), BenchmarkError> {
		initialize_with_one_message::<T>();

		#[block]
		{
			OutboundQueue::<T>::commit();
		}

		Ok(())
	}

	/// Benchmark for `on_initialize` in the worst-case
	#[benchmark]
	fn on_initialize() -> Result<(), BenchmarkError> {
		initialize_worst_case::<T>();
		#[block]
		{
			OutboundQueue::<T>::on_initialize(1_u32.into());
		}
		Ok(())
	}

	/// Benchmark the entire process flow in the worst-case. This can be used to determine
	/// appropriate values for the configuration parameters `MaxMessagesPerBlock` and
	/// `MaxMessagePayloadSize`
	#[benchmark]
	fn process() -> Result<(), BenchmarkError> {
		initialize_worst_case::<T>();
		let origin = AggregateMessageOrigin::SnowbridgeV2([1; 32].into());
		let (enqueued_message, _) = build_message::<T>();
		let message = enqueued_message.encode();

		#[block]
		{
			OutboundQueue::<T>::on_initialize(1_u32.into());
			for _ in 0..T::MaxMessagesPerBlock::get() {
				OutboundQueue::<T>::do_process_message(origin, &message).unwrap();
			}
			OutboundQueue::<T>::commit();
		}

		Ok(())
	}

	impl_benchmark_test_suite!(OutboundQueue, crate::mock::new_tester(), crate::mock::Test,);
}