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
// KILT Blockchain – https://botlabs.org
// Copyright (C) 2019-2024 BOTLabs GmbH

// The KILT Blockchain 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.

// The KILT Blockchain 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 this program.  If not, see <https://www.gnu.org/licenses/>.

// If you feel like getting in touch with us, you can do so at info@botlabs.org
#![cfg(feature = "runtime-benchmarks")]

//! Benchmarking

use frame_benchmarking::{account, benchmarks, impl_benchmark_test_suite};
use frame_support::{
	crypto::ecdsa::ECDSAExt,
	traits::{
		fungible::{Inspect, Mutate},
		Get,
	},
};
use frame_system::{pallet_prelude::BlockNumberFor, RawOrigin};
use sha3::{Digest, Keccak256};
use sp_io::crypto::{ecdsa_generate, ed25519_generate, sr25519_generate};
use sp_runtime::{
	app_crypto::{ed25519, sr25519},
	traits::IdentifyAccount,
	AccountId32, KeyTypeId,
};

use kilt_support::{traits::GenerateBenchmarkOrigin, Deposit};

use crate::{
	account::AccountId20,
	associate_account_request::{get_challenge, AssociateAccountRequest},
	linkable_account::LinkableAccountId,
	signature::get_wrapped_payload,
	AccountIdOf, Call, Config, ConnectedAccounts, ConnectedDids, CurrencyOf, Pallet,
};

const SEED: u32 = 0;

// Free 2x deposit amount + existential deposit so that we can use this function
// to link an account two times to two different DIDs.
fn make_free_for_did<T: Config>(account: &AccountIdOf<T>)
where
	<T as Config>::Currency: Mutate<T::AccountId>,
{
	let balance = <CurrencyOf<T> as Inspect<AccountIdOf<T>>>::minimum_balance()
		+ <T as Config>::Deposit::get()
		+ <T as Config>::Deposit::get();
	CurrencyOf::<T>::set_balance(account, balance);
}

benchmarks! {
	where_clause {
		where
		T::AccountId: From<sr25519::Public> + From<ed25519::Public> + Into<LinkableAccountId> + Into<AccountId32> + From<sp_runtime::AccountId32>,
		T::DidIdentifier: From<T::AccountId>,
		T::EnsureOrigin: GenerateBenchmarkOrigin<T::RuntimeOrigin, T::AccountId, T::DidIdentifier>,
		<T as Config>::Currency: Mutate<T::AccountId>,
	}

	associate_account_multisig_sr25519 {
		let caller: T::AccountId = account("caller", 0, SEED);
		let did: T::DidIdentifier = account("did", 0, SEED);
		let previous_did: T::DidIdentifier = account("prev", 0, SEED + 1);
		let connected_acc = sr25519_generate(KeyTypeId(*b"aura"), None);
		let connected_acc_id: T::AccountId = connected_acc.into();
		let linkable_id: LinkableAccountId = connected_acc_id.clone().into();
		let expire_at: BlockNumberFor<T> = 500_u32.into();

		let sig = sp_io::crypto::sr25519_sign(
			KeyTypeId(*b"aura"),
			&connected_acc,
			&get_wrapped_payload(
				get_challenge(&did, expire_at).as_bytes(),
				crate::signature::WrapType::Substrate,
			))
			.ok_or("Error while building signature.")?;

		make_free_for_did::<T>(&caller);

		// Add existing connected_acc -> previous_did connection that will be replaced
		Pallet::<T>::add_association(caller.clone(), previous_did.clone(), linkable_id.clone()).expect("should create previous association");
		assert!(ConnectedAccounts::<T>::get(&previous_did, linkable_id.clone()).is_some());
		let origin = T::EnsureOrigin::generate_origin(caller, did.clone());
		let id_arg = linkable_id.clone();
		let req = AssociateAccountRequest::Polkadot(connected_acc_id.into(), sig.into());
	}: associate_account<T::RuntimeOrigin>(origin, req, expire_at)
	verify {
		assert!(ConnectedDids::<T>::get(linkable_id.clone()).is_some());
		assert!(ConnectedAccounts::<T>::get(&previous_did, linkable_id.clone()).is_none());
		assert!(ConnectedAccounts::<T>::get(did, linkable_id).is_some());
	}

	associate_account_multisig_ed25519 {
		let caller: T::AccountId = account("caller", 0, SEED);
		let did: T::DidIdentifier = account("did", 0, SEED);
		let previous_did: T::DidIdentifier = account("prev", 0, SEED + 1);
		let connected_acc = ed25519_generate(KeyTypeId(*b"aura"), None);
		let connected_acc_id: T::AccountId = connected_acc.into();
		let linkable_id: LinkableAccountId = connected_acc_id.clone().into();
		let expire_at: BlockNumberFor<T> = 500_u32.into();

		let sig = sp_io::crypto::ed25519_sign(
			KeyTypeId(*b"aura"),
			&connected_acc,
			&get_wrapped_payload(
				get_challenge(&did, expire_at).as_bytes(),
				crate::signature::WrapType::Substrate,
			))
			.ok_or("Error while building signature.")?;

		make_free_for_did::<T>(&caller);

		// Add existing connected_acc -> previous_did connection that will be replaced
		Pallet::<T>::add_association(caller.clone(), previous_did.clone(), linkable_id.clone()).expect("should create previous association");
		assert!(ConnectedAccounts::<T>::get(&previous_did, linkable_id.clone()).is_some());
		let origin = T::EnsureOrigin::generate_origin(caller, did.clone());
		let id_arg = linkable_id.clone();
		let req = AssociateAccountRequest::Polkadot(connected_acc_id.into(), sig.into());
	}: associate_account<T::RuntimeOrigin>(origin, req, expire_at)
	verify {
		assert!(ConnectedDids::<T>::get(linkable_id.clone()).is_some());
		assert!(ConnectedAccounts::<T>::get(&previous_did, linkable_id.clone()).is_none());
		assert!(ConnectedAccounts::<T>::get(did, linkable_id).is_some());
	}

	associate_account_multisig_ecdsa {
		let caller: T::AccountId = account("caller", 0, SEED);
		let did: T::DidIdentifier = account("did", 0, SEED);
		let previous_did: T::DidIdentifier = account("prev", 0, SEED + 1);
		let connected_acc = ecdsa_generate(KeyTypeId(*b"aura"), None);
		let connected_acc_id = sp_runtime::MultiSigner::from(connected_acc).into_account();
		let linkable_id: LinkableAccountId = connected_acc_id.clone().into();
		let expire_at: BlockNumberFor<T> = 500_u32.into();

		let sig = sp_io::crypto::ecdsa_sign(
			KeyTypeId(*b"aura"),
			&connected_acc,
			&get_wrapped_payload(
				get_challenge(&did, expire_at).as_bytes(),
				crate::signature::WrapType::Substrate,
			))
			.ok_or("Error while building signature.")?;

		make_free_for_did::<T>(&caller);

		// Add existing connected_acc -> previous_did connection that will be replaced
		Pallet::<T>::add_association(caller.clone(), previous_did.clone(), linkable_id.clone()).expect("should create previous association");
		assert!(ConnectedAccounts::<T>::get(&previous_did, linkable_id.clone()).is_some());
		let origin = T::EnsureOrigin::generate_origin(caller, did.clone());
		let id_arg = linkable_id.clone();
		let req = AssociateAccountRequest::Polkadot(connected_acc_id, sig.into());
	}: associate_account<T::RuntimeOrigin>(origin, req, expire_at)
	verify {
		assert!(ConnectedDids::<T>::get(linkable_id.clone()).is_some());
		assert!(ConnectedAccounts::<T>::get(&previous_did, linkable_id.clone()).is_none());
		assert!(ConnectedAccounts::<T>::get(did, linkable_id).is_some());
	}

	associate_eth_account {
		let caller: T::AccountId = account("caller", 0, SEED);
		let did: T::DidIdentifier = account("did", 0, SEED);
		let previous_did: T::DidIdentifier = account("prev", 0, SEED + 1);
		let expire_at: BlockNumberFor<T> = 500_u32.into();

		let eth_public_key = ecdsa_generate(KeyTypeId(*b"aura"), None);
		let eth_account = AccountId20(eth_public_key.to_eth_address().unwrap());

		let wrapped_payload = get_wrapped_payload(
			get_challenge(&did, expire_at).as_bytes(),
			crate::signature::WrapType::Ethereum,
		);

		let sig = sp_io::crypto::ecdsa_sign_prehashed(
			KeyTypeId(*b"aura"),
			&eth_public_key,
			&Keccak256::digest(wrapped_payload).try_into().unwrap(),
		).ok_or("Error while building signature.")?;

		make_free_for_did::<T>(&caller);

		// Add existing connected_acc -> previous_did connection that will be replaced
		Pallet::<T>::add_association(caller.clone(), previous_did.clone(), eth_account.into()).expect("should create previous association");
		assert!(ConnectedAccounts::<T>::get(&previous_did, LinkableAccountId::from(eth_account)).is_some());
		let origin = T::EnsureOrigin::generate_origin(caller, did.clone());
		let req = AssociateAccountRequest::Ethereum(eth_account, sig.into());
	}: associate_account<T::RuntimeOrigin>(origin, req, expire_at)
	verify {
		assert!(ConnectedDids::<T>::get(LinkableAccountId::from(eth_account)).is_some());
		assert!(ConnectedAccounts::<T>::get(&previous_did, LinkableAccountId::from(eth_account)).is_none());
		assert!(ConnectedAccounts::<T>::get(did, LinkableAccountId::from(eth_account)).is_some());
	}

	associate_sender {
		let caller: T::AccountId = account("caller", 0, SEED);
		let linkable_id: LinkableAccountId = caller.clone().into();
		let did: T::DidIdentifier = account("did", 0, SEED);
		let previous_did: T::DidIdentifier = account("prev", 0, SEED + 1);

		make_free_for_did::<T>(&caller);

		// Add existing sender -> previous_did connection that will be replaced
		Pallet::<T>::add_association(caller.clone(), previous_did.clone(), caller.clone().into()).expect("should create previous association");
		assert!(ConnectedAccounts::<T>::get(&previous_did, &linkable_id).is_some());
		let origin = T::EnsureOrigin::generate_origin(caller, did.clone());
	}: _<T::RuntimeOrigin>(origin)
	verify {
		assert!(ConnectedDids::<T>::get(&linkable_id).is_some());
		assert!(ConnectedAccounts::<T>::get(previous_did, &linkable_id).is_none());
		assert!(ConnectedAccounts::<T>::get(did, linkable_id).is_some());
	}

	remove_sender_association {
		let caller: T::AccountId = account("caller", 0, SEED);
		let linkable_id: LinkableAccountId = caller.clone().into();
		let did: T::DidIdentifier = account("did", 0, SEED);

		make_free_for_did::<T>(&caller);
		Pallet::<T>::add_association(caller.clone(), did.clone(), linkable_id.clone()).expect("should create association");

		let origin = RawOrigin::Signed(caller);
	}: _(origin)
	verify {
		assert!(ConnectedDids::<T>::get(&linkable_id).is_none());
		assert!(ConnectedAccounts::<T>::get(did, linkable_id).is_none());
	}

	remove_account_association {
		let caller: T::AccountId = account("caller", 0, SEED);
		let linkable_id: LinkableAccountId = caller.clone().into();
		let did: T::DidIdentifier = account("did", 0, SEED);
		make_free_for_did::<T>(&caller);

		Pallet::<T>::add_association(caller.clone(), did.clone(), linkable_id.clone()).expect("should create association");

		let origin = T::EnsureOrigin::generate_origin(caller, did.clone());
		let id_arg = linkable_id.clone();
	}: _<T::RuntimeOrigin>(origin, id_arg)
	verify {
		assert!(ConnectedDids::<T>::get(&linkable_id).is_none());
		assert!(ConnectedAccounts::<T>::get(did, linkable_id).is_none());
	}

	change_deposit_owner {
		let deposit_owner_old: T::AccountId = account("caller", 0, SEED);
		let deposit_owner_new: T::AccountId = account("caller", 1, SEED);
		let linkable_id: LinkableAccountId = deposit_owner_old.clone().into();
		let did: T::DidIdentifier = account("did", 0, SEED);
		make_free_for_did::<T>(&deposit_owner_old);
		make_free_for_did::<T>(&deposit_owner_new);

		Pallet::<T>::add_association(deposit_owner_old, did.clone(), linkable_id.clone()).expect("should create association");

		let origin = T::EnsureOrigin::generate_origin(deposit_owner_new.clone(), did);
		let id_arg = linkable_id.clone();
	}: _<T::RuntimeOrigin>(origin, id_arg)
	verify {
		assert_eq!(
			ConnectedDids::<T>::get(&linkable_id).expect("should retain link").deposit,
			Deposit {
				owner: deposit_owner_new,
				amount: <T as Config>::Deposit::get(),
			},
		);
	}

	update_deposit {
		let deposit_owner: T::AccountId = account("caller", 0, SEED);
		let linkable_id: LinkableAccountId = deposit_owner.clone().into();
		let did: T::DidIdentifier = account("did", 0, SEED);
		make_free_for_did::<T>(&deposit_owner);

		Pallet::<T>::add_association(
			deposit_owner.clone(),
			did,
			linkable_id.clone()
		).expect("should create association");

		let origin = RawOrigin::Signed(deposit_owner.clone());
		let id_arg = linkable_id.clone();
	}: _(origin, id_arg)
	verify {
		assert_eq!(
			ConnectedDids::<T>::get(&linkable_id).expect("should retain link").deposit,
			Deposit {
				owner: deposit_owner,
				amount: <T as Config>::Deposit::get(),
			},
		);
	}
}

#[cfg(test)]
use crate::Pallet as DidLookup;

impl_benchmark_test_suite!(
	DidLookup,
	crate::mock::ExtBuilder::default().build_with_keystore(),
	crate::mock::Test
);