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
// 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 snowbridge_core::{
	outbound::{Command, Initializer},
	ChannelId,
};
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::*;

	/// Benchmark for processing a message.
	#[benchmark]
	fn do_process_message() -> Result<(), BenchmarkError> {
		let enqueued_message = QueuedMessage {
			id: H256::zero(),
			channel_id: ChannelId::from([1; 32]),
			command: Command::Upgrade {
				impl_address: H160::zero(),
				impl_code_hash: H256::zero(),
				initializer: Some(Initializer {
					params: [7u8; 256].into_iter().collect(),
					maximum_required_gas: 200_000,
				}),
			},
		};
		let origin = AggregateMessageOrigin::Snowbridge([1; 32].into());
		let encoded_enqueued_message = enqueued_message.encode();

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

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

		Ok(())
	}

	/// Benchmark for producing final messages commitment
	#[benchmark]
	fn commit() -> Result<(), BenchmarkError> {
		// Assume worst case, where `MaxMessagesPerBlock` messages need to be committed.
		for i in 0..T::MaxMessagesPerBlock::get() {
			let leaf_data: [u8; 1] = [i as u8];
			let leaf = <T as Config>::Hashing::hash(&leaf_data);
			MessageLeaves::<T>::append(leaf);
		}

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

		Ok(())
	}

	/// Benchmark for producing commitment for a single message
	#[benchmark]
	fn commit_single() -> Result<(), BenchmarkError> {
		let leaf = <T as Config>::Hashing::hash(&[100; 1]);
		MessageLeaves::<T>::append(leaf);

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

		Ok(())
	}

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