-
Notifications
You must be signed in to change notification settings - Fork 417
/
Copy pathtests.rs
372 lines (329 loc) · 13.4 KB
/
tests.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
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
// This file is part of Astar.
// Copyright (C) Stake Technologies Pte.Ltd.
// SPDX-License-Identifier: GPL-3.0-or-later
// Astar 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.
// Astar 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 Astar. If not, see <http://www.gnu.org/licenses/>.
use core::str::from_utf8;
use frame_support::dispatch::GetDispatchInfo;
use frame_support::traits::Currency;
use libsecp256k1::PublicKeyFormat;
use sp_core::crypto::{AccountId32, Ss58Codec};
use crate::mock::*;
use astar_primitives::evm::EvmAddress;
use hex_literal::hex;
use parity_scale_codec::Encode;
use precompile_utils::testing::*;
use sp_core::{ecdsa, Pair};
fn precompiles() -> TestPrecompileSet<TestRuntime> {
PrecompilesValue::get()
}
#[test]
fn dispatch_calls_on_behalf_of_lockdrop_works() {
ExtBuilder.build().execute_with(|| {
// Transfer balance to Alice
let call = RuntimeCall::Balances(pallet_balances::Call::transfer_keep_alive {
dest: ALICE,
value: 15 * ONE,
});
// Sanity check - Alice holds no Balance
assert_eq!(Balances::free_balance(ALICE), 0);
// Get Alice EVM address based on the Public Key
let alice_eth = crate::tests::eth_address(&alice_secret());
// Get derived AccountId from the Blake2b hash of the compressed ECDSA Public key
let account_id = account_id(&alice_secret());
// Fund this account (fund the lockdrop account)
let _ = Balances::deposit_creating(&account_id, ONE * 20);
// Get the full 64 bytes ECDSA Public key
let pubkey = crate::tests::public_key_full(&alice_secret());
precompiles()
.prepare_test(
alice_eth,
PRECOMPILE_ADDRESS,
PrecompileCall::dispatch_lockdrop_call {
call: call.encode().into(),
pubkey: pubkey.into(),
},
)
.expect_no_logs()
.execute_returns(true);
// Get Balance of ALICE in pallet balances
assert_eq!(Balances::free_balance(ALICE), 15 * ONE);
});
}
#[test]
fn proper_gas_is_charged() {
ExtBuilder.build().execute_with(|| {
let call = RuntimeCall::Balances(pallet_balances::Call::transfer_keep_alive {
dest: ALICE,
value: 15 * ONE,
});
// Dispatch a call and ensure gas is charged properly
// Expected gas is the constant weight of 1_000_000_000 and the weight of the call
// In mock one unit of ref_time us charged 1
let expected_gas = 1_000_000_000u64 + call.get_dispatch_info().weight.ref_time();
// Get Alice EVM address based on the Public Key
let alice_eth = crate::tests::eth_address(&alice_secret());
// Get derived AccountId from the Blake2b hash of the compressed ECDSA Public key
let account_id = account_id(&alice_secret());
// Fund this account (fund the lockdrop account)
let _ = Balances::deposit_creating(&account_id, ONE * 20);
// Get the full 64 bytes ECDSA Public key
let pubkey = crate::tests::public_key_full(&alice_secret());
precompiles()
.prepare_test(
alice_eth,
PRECOMPILE_ADDRESS,
PrecompileCall::dispatch_lockdrop_call {
call: call.encode().into(),
pubkey: pubkey.into(),
},
)
.expect_cost(expected_gas)
.execute_returns(true);
});
}
#[test]
fn pubkey_does_not_match_caller_address() {
ExtBuilder.build().execute_with(|| {
// Transfer balance to Alice
let call = RuntimeCall::Balances(pallet_balances::Call::transfer_keep_alive {
dest: ALICE,
value: 15 * ONE,
});
// Sanity check - Alice holds no Balance
assert_eq!(Balances::free_balance(ALICE), 0);
// Get Alice EVM address based on the Public Key
let alice_eth = crate::tests::eth_address(&alice_secret());
// Dummy AccountId to sign the EIP712 payload with
let account_id = DUMMY;
// Fund this dummy account
let _ = Balances::deposit_creating(&account_id, ONE * 20);
// Create a dummy pubkey
let pubkey = [10u8; 64];
precompiles()
.prepare_test(
alice_eth,
PRECOMPILE_ADDRESS,
PrecompileCall::dispatch_lockdrop_call {
call: call.encode().into(),
pubkey: pubkey.into(),
},
)
.expect_no_logs()
.execute_reverts(|output| output == b"caller does not match the public key");
// Get Balance of ALICE in pallet balances and ensure it has not received any funds
assert_eq!(Balances::free_balance(ALICE), 0);
});
}
#[test]
fn pubkey_derive_to_proper_ss58() {
ExtBuilder.build().execute_with(|| {
// Transfer balance to Alice
let call = RuntimeCall::Balances(pallet_balances::Call::transfer_keep_alive {
dest: ALICE,
value: 15 * ONE,
});
// Sanity check - Alice holds no Balance
assert_eq!(Balances::free_balance(ALICE), 0);
// The seed "ac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80"
// should resolve to the SS58 address "5EGynCAEvv8NLeHx8vDMvb8hTcEcMYUMWCDQEEncNEfNWB2W"
// If we fund this account, it will be able to dispatch the Transfer call
let pair = ecdsa::Pair::from_seed(&hex!(
"ac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80"
));
let pubkey = crate::tests::public_key_full_from_compressed(pair.public().as_ref());
let alice_eth = EvmAddress::from_slice(&sp_io::hashing::keccak_256(&pubkey)[12..]);
let account_id =
AccountId::from_ss58check("5EGynCAEvv8NLeHx8vDMvb8hTcEcMYUMWCDQEEncNEfNWB2W").unwrap();
// Fund this account
let _ = Balances::deposit_creating(&account_id, ONE * 20);
precompiles()
.prepare_test(
alice_eth,
PRECOMPILE_ADDRESS,
PrecompileCall::dispatch_lockdrop_call {
call: call.encode().into(),
pubkey: pubkey.into(),
},
)
.expect_no_logs()
.execute_returns(true);
// Assert that the call (Transfer) was successful
assert_eq!(Balances::free_balance(ALICE), 15 * ONE);
});
}
#[test]
fn decode_limit_too_high() {
ExtBuilder.build().execute_with(|| {
let mut nested_call =
RuntimeCall::System(frame_system::Call::remark { remark: Vec::new() });
// More than 8 depth
for _ in 0..9 {
nested_call = RuntimeCall::Utility(pallet_utility::Call::as_derivative {
index: 0,
call: Box::new(nested_call),
});
}
// Get Alice EVM address based on the Public Key
let alice_eth = crate::tests::eth_address(&alice_secret());
// Get derived AccountId from the Blake2b hash of the compressed ECDSA Public key
let account_id = account_id(&alice_secret());
// Fund this account (fund the lockdrop account)
let _ = Balances::deposit_creating(&account_id, ONE * 20);
// Get the full 64 bytes ECDSA Public key
let pubkey = crate::tests::public_key_full(&alice_secret());
precompiles()
.prepare_test(
alice_eth,
PRECOMPILE_ADDRESS,
PrecompileCall::dispatch_lockdrop_call {
call: nested_call.encode().into(),
pubkey: pubkey.into(),
},
)
.expect_no_logs()
.execute_reverts(|output| from_utf8(output).unwrap().contains("could not decode call"));
});
}
#[test]
fn decode_limit_bounded() {
ExtBuilder.build().execute_with(|| {
let calls: Vec<_> = (0..681)
.map(|_| RuntimeCall::System(frame_system::Call::remark { remark: Vec::new() }))
.collect();
let call = RuntimeCall::Utility(pallet_utility::Call::batch_all { calls });
let out_of_bounds_calls: Vec<_> = (0..682)
.map(|_| RuntimeCall::System(frame_system::Call::remark { remark: Vec::new() }))
.collect();
let out_of_bounds_call = RuntimeCall::Utility(pallet_utility::Call::batch_all {
calls: out_of_bounds_calls,
});
// Get Alice EVM address based on the Public Key
let alice_eth = crate::tests::eth_address(&alice_secret());
// Get derived AccountId from the Blake2b hash of the compressed ECDSA Public key
let account_id = account_id(&alice_secret());
// Fund this account (fund the lockdrop account)
let _ = Balances::deposit_creating(&account_id, ONE * 20);
// Get the full 64 bytes ECDSA Public key
let pubkey = crate::tests::public_key_full(&alice_secret());
precompiles()
.prepare_test(
alice_eth,
PRECOMPILE_ADDRESS,
PrecompileCall::dispatch_lockdrop_call {
call: call.encode().into(),
pubkey: pubkey.into(),
},
)
.expect_no_logs()
.execute_returns(true);
precompiles()
.prepare_test(
alice_eth,
PRECOMPILE_ADDRESS,
PrecompileCall::dispatch_lockdrop_call {
call: out_of_bounds_call.encode().into(),
pubkey: pubkey.into(),
},
)
.expect_no_logs()
.execute_reverts(|output| output == b"call: Value is too large for length");
});
}
#[test]
fn decode_limit_ok() {
ExtBuilder.build().execute_with(|| {
let mut nested_call =
RuntimeCall::System(frame_system::Call::remark { remark: Vec::new() });
for _ in 0..8 {
nested_call = RuntimeCall::Utility(pallet_utility::Call::as_derivative {
index: 0,
call: Box::new(nested_call),
});
}
// Get Alice EVM address based on the Public Key
let alice_eth = crate::tests::eth_address(&alice_secret());
// Get derived AccountId from the Blake2b hash of the compressed ECDSA Public key
let account_id = account_id(&alice_secret());
// Fund this account (fund the lockdrop account)
let _ = Balances::deposit_creating(&account_id, ONE * 20);
// Get the full 64 bytes ECDSA Public key
let pubkey = crate::tests::public_key_full(&alice_secret());
precompiles()
.prepare_test(
alice_eth,
PRECOMPILE_ADDRESS,
PrecompileCall::dispatch_lockdrop_call {
call: nested_call.encode().into(),
pubkey: pubkey.into(),
},
)
.expect_no_logs()
.execute_returns(true);
});
}
#[test]
fn only_whitelisted_calls_can_be_dispatched() {
ExtBuilder.build().execute_with(|| {
// Transfer balance to Alice
let call = RuntimeCall::System(frame_system::Call::remark_with_event {
remark: b"Hello World".to_vec(),
});
// Get Alice EVM address based on the Public Key
let alice_eth = crate::tests::eth_address(&alice_secret());
// Get derived AccountId from the Blake2b hash of the compressed ECDSA Public key
let account_id = crate::tests::account_id(&alice_secret());
// Fund this account (fund the lockdrop account)
let _ = Balances::deposit_creating(&account_id, ONE * 20);
// Get the full 64 bytes ECDSA Public key
let pubkey = crate::tests::public_key_full(&alice_secret());
precompiles()
.prepare_test(
alice_eth,
PRECOMPILE_ADDRESS,
PrecompileCall::dispatch_lockdrop_call {
call: call.encode().into(),
pubkey: pubkey.into(),
},
)
.expect_no_logs()
.execute_reverts(|output| output == b"invalid Call");
});
}
fn account_id(secret: &libsecp256k1::SecretKey) -> AccountId32 {
sp_io::hashing::blake2_256(
ecdsa::Public::from_full(
&libsecp256k1::PublicKey::from_secret_key(secret).serialize()[1..65],
)
.unwrap()
.as_ref(),
)
.into()
}
fn eth_address(secret: &libsecp256k1::SecretKey) -> EvmAddress {
EvmAddress::from_slice(
&sp_io::hashing::keccak_256(
&libsecp256k1::PublicKey::from_secret_key(secret).serialize()[1..65],
)[12..],
)
}
fn public_key_full_from_compressed(pubkey: &[u8]) -> [u8; 64] {
libsecp256k1::PublicKey::parse_slice(pubkey, Some(PublicKeyFormat::Compressed))
.unwrap()
.serialize()[1..65]
.try_into()
.unwrap()
}
fn public_key_full(secret: &libsecp256k1::SecretKey) -> [u8; 64] {
libsecp256k1::PublicKey::from_secret_key(secret).serialize()[1..65]
.try_into()
.unwrap()
}