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

#[allow(unused)]
use crate::Pallet as SnowbridgeControl;
use frame_benchmarking::v2::*;
use frame_system::RawOrigin;
use snowbridge_core::{eth, outbound::OperatingMode};
use sp_runtime::SaturatedConversion;
use xcm::prelude::*;

#[allow(clippy::result_large_err)]
fn fund_sovereign_account<T: Config>(para_id: ParaId) -> Result<(), BenchmarkError> {
	let amount: BalanceOf<T> = (10_000_000_000_000_u64).saturated_into::<u128>().saturated_into();
	let sovereign_account = sibling_sovereign_account::<T>(para_id);
	T::Token::mint_into(&sovereign_account, amount)?;
	Ok(())
}

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

	#[benchmark]
	fn upgrade() -> Result<(), BenchmarkError> {
		let impl_address = H160::repeat_byte(1);
		let impl_code_hash = H256::repeat_byte(1);

		// Assume 256 bytes passed to initializer
		let params: Vec<u8> = (0..256).map(|_| 1u8).collect();

		#[extrinsic_call]
		_(
			RawOrigin::Root,
			impl_address,
			impl_code_hash,
			Some(Initializer { params, maximum_required_gas: 100000 }),
		);

		Ok(())
	}

	#[benchmark]
	fn set_operating_mode() -> Result<(), BenchmarkError> {
		#[extrinsic_call]
		_(RawOrigin::Root, OperatingMode::RejectingOutboundMessages);

		Ok(())
	}

	#[benchmark]
	fn set_pricing_parameters() -> Result<(), BenchmarkError> {
		let params = T::DefaultPricingParameters::get();

		#[extrinsic_call]
		_(RawOrigin::Root, params);

		Ok(())
	}

	#[benchmark]
	fn create_agent() -> Result<(), BenchmarkError> {
		let origin_para_id = 2000;
		let origin_location = Location::new(1, [Parachain(origin_para_id)]);
		let origin = T::Helper::make_xcm_origin(origin_location);
		fund_sovereign_account::<T>(origin_para_id.into())?;

		#[extrinsic_call]
		_(origin as T::RuntimeOrigin);

		Ok(())
	}

	#[benchmark]
	fn create_channel() -> Result<(), BenchmarkError> {
		let origin_para_id = 2000;
		let origin_location = Location::new(1, [Parachain(origin_para_id)]);
		let origin = T::Helper::make_xcm_origin(origin_location);
		fund_sovereign_account::<T>(origin_para_id.into())?;

		SnowbridgeControl::<T>::create_agent(origin.clone())?;

		#[extrinsic_call]
		_(origin as T::RuntimeOrigin, OperatingMode::Normal);

		Ok(())
	}

	#[benchmark]
	fn update_channel() -> Result<(), BenchmarkError> {
		let origin_para_id = 2000;
		let origin_location = Location::new(1, [Parachain(origin_para_id)]);
		let origin = T::Helper::make_xcm_origin(origin_location);
		fund_sovereign_account::<T>(origin_para_id.into())?;
		SnowbridgeControl::<T>::create_agent(origin.clone())?;
		SnowbridgeControl::<T>::create_channel(origin.clone(), OperatingMode::Normal)?;

		#[extrinsic_call]
		_(origin as T::RuntimeOrigin, OperatingMode::RejectingOutboundMessages);

		Ok(())
	}

	#[benchmark]
	fn force_update_channel() -> Result<(), BenchmarkError> {
		let origin_para_id = 2000;
		let origin_location = Location::new(1, [Parachain(origin_para_id)]);
		let origin = T::Helper::make_xcm_origin(origin_location);
		let channel_id: ChannelId = ParaId::from(origin_para_id).into();

		fund_sovereign_account::<T>(origin_para_id.into())?;
		SnowbridgeControl::<T>::create_agent(origin.clone())?;
		SnowbridgeControl::<T>::create_channel(origin.clone(), OperatingMode::Normal)?;

		#[extrinsic_call]
		_(RawOrigin::Root, channel_id, OperatingMode::RejectingOutboundMessages);

		Ok(())
	}

	#[benchmark]
	fn transfer_native_from_agent() -> Result<(), BenchmarkError> {
		let origin_para_id = 2000;
		let origin_location = Location::new(1, [Parachain(origin_para_id)]);
		let origin = T::Helper::make_xcm_origin(origin_location);
		fund_sovereign_account::<T>(origin_para_id.into())?;
		SnowbridgeControl::<T>::create_agent(origin.clone())?;
		SnowbridgeControl::<T>::create_channel(origin.clone(), OperatingMode::Normal)?;

		#[extrinsic_call]
		_(origin as T::RuntimeOrigin, H160::default(), 1);

		Ok(())
	}

	#[benchmark]
	fn force_transfer_native_from_agent() -> Result<(), BenchmarkError> {
		let origin_para_id = 2000;
		let origin_location = Location::new(1, [Parachain(origin_para_id)]);
		let origin = T::Helper::make_xcm_origin(origin_location.clone());
		fund_sovereign_account::<T>(origin_para_id.into())?;
		SnowbridgeControl::<T>::create_agent(origin.clone())?;

		let versioned_location: VersionedLocation = origin_location.into();

		#[extrinsic_call]
		_(RawOrigin::Root, Box::new(versioned_location), H160::default(), 1);

		Ok(())
	}

	#[benchmark]
	fn set_token_transfer_fees() -> Result<(), BenchmarkError> {
		#[extrinsic_call]
		_(RawOrigin::Root, 1, 1, eth(1));

		Ok(())
	}

	#[benchmark]
	fn register_token() -> Result<(), BenchmarkError> {
		let caller: T::AccountId = whitelisted_caller();

		let amount: BalanceOf<T> =
			(10_000_000_000_000_u128).saturated_into::<u128>().saturated_into();

		T::Token::mint_into(&caller, amount)?;

		let relay_token_asset_id: Location = Location::parent();
		let asset = Box::new(VersionedLocation::V4(relay_token_asset_id));
		let asset_metadata = AssetMetadata {
			name: "wnd".as_bytes().to_vec().try_into().unwrap(),
			symbol: "wnd".as_bytes().to_vec().try_into().unwrap(),
			decimals: 12,
		};

		#[extrinsic_call]
		_(RawOrigin::Root, asset, asset_metadata);

		Ok(())
	}

	impl_benchmark_test_suite!(
		SnowbridgeControl,
		crate::mock::new_test_ext(true),
		crate::mock::Test
	);
}