pallet-revive-uapi 0.5.0

Exposes all the host functions that a contract can import.
Documentation
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
// Copyright (C) Parity Technologies (UK) Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#![allow(unused_variables)]

use crate::{
	host::{CallFlags, HostFn, HostFnImpl, Result, StorageFlags},
	pack_hi_lo, ReturnFlags,
};
use pallet_revive_proc_macro::unstable_hostfn;

mod sys {
	use crate::ReturnCode;

	#[polkavm_derive::polkavm_define_abi]
	mod abi {}

	impl abi::FromHost for ReturnCode {
		type Regs = (u64,);

		fn from_host((a0,): Self::Regs) -> Self {
			ReturnCode(a0 as _)
		}
	}

	#[polkavm_derive::polkavm_import(abi = self::abi)]
	extern "C" {
		pub fn set_storage(
			flags: u32,
			key_ptr: *const u8,
			key_len: u32,
			value_ptr: *const u8,
			value_len: u32,
		) -> ReturnCode;
		pub fn set_storage_or_clear(
			flags: u32,
			key_ptr: *const u8,
			value_ptr: *const u8,
		) -> ReturnCode;
		pub fn clear_storage(flags: u32, key_ptr: *const u8, key_len: u32) -> ReturnCode;
		pub fn get_storage(
			flags: u32,
			key_ptr: *const u8,
			key_len: u32,
			out_ptr: *mut u8,
			out_len_ptr: *mut u32,
		) -> ReturnCode;
		pub fn get_storage_or_zero(flags: u32, key_ptr: *const u8, out_ptr: *mut u8);
		pub fn contains_storage(flags: u32, key_ptr: *const u8, key_len: u32) -> ReturnCode;
		pub fn take_storage(
			flags: u32,
			key_ptr: *const u8,
			key_len: u32,
			out_ptr: *mut u8,
			out_len_ptr: *mut u32,
		) -> ReturnCode;
		pub fn call(
			flags_and_callee: u64,
			ref_time_limit: u64,
			proof_size_limit: u64,
			deposit_and_value: u64,
			input_data: u64,
			output_data: u64,
		) -> ReturnCode;
		pub fn delegate_call(
			flags_and_callee: u64,
			ref_time_limit: u64,
			proof_size_limit: u64,
			deposit_ptr: *const u8,
			input_data: u64,
			output_data: u64,
		) -> ReturnCode;
		pub fn instantiate(
			ref_time_limit: u64,
			proof_size_limit: u64,
			deposit_and_value: u64,
			input_data: u64,
			output_data: u64,
			address_and_salt: u64,
		) -> ReturnCode;
		pub fn terminate(beneficiary_ptr: *const u8);
		pub fn call_data_copy(out_ptr: *mut u8, out_len: u32, offset: u32);
		pub fn call_data_load(out_ptr: *mut u8, offset: u32);
		pub fn seal_return(flags: u32, data_ptr: *const u8, data_len: u32);
		pub fn caller(out_ptr: *mut u8);
		pub fn origin(out_ptr: *mut u8);
		pub fn is_contract(account_ptr: *const u8) -> ReturnCode;
		pub fn to_account_id(address_ptr: *const u8, out_ptr: *mut u8);
		pub fn code_hash(address_ptr: *const u8, out_ptr: *mut u8);
		pub fn code_size(address_ptr: *const u8) -> u64;
		pub fn own_code_hash(out_ptr: *mut u8);
		pub fn caller_is_origin() -> ReturnCode;
		pub fn caller_is_root() -> ReturnCode;
		pub fn address(out_ptr: *mut u8);
		pub fn weight_to_fee(ref_time: u64, proof_size: u64, out_ptr: *mut u8);
		pub fn weight_left(out_ptr: *mut u8, out_len_ptr: *mut u32);
		pub fn ref_time_left() -> u64;
		pub fn get_immutable_data(out_ptr: *mut u8, out_len_ptr: *mut u32);
		pub fn set_immutable_data(ptr: *const u8, len: u32);
		pub fn balance(out_ptr: *mut u8);
		pub fn balance_of(addr_ptr: *const u8, out_ptr: *mut u8);
		pub fn chain_id(out_ptr: *mut u8);
		pub fn value_transferred(out_ptr: *mut u8);
		pub fn now(out_ptr: *mut u8);
		pub fn gas_limit() -> u64;
		pub fn minimum_balance(out_ptr: *mut u8);
		pub fn deposit_event(
			topics_ptr: *const [u8; 32],
			num_topic: u32,
			data_ptr: *const u8,
			data_len: u32,
		);
		pub fn gas_price() -> u64;
		pub fn base_fee(out_ptr: *mut u8);
		pub fn call_data_size() -> u64;
		pub fn block_number(out_ptr: *mut u8);
		pub fn block_hash(block_number_ptr: *const u8, out_ptr: *mut u8);
		pub fn block_author(out_ptr: *mut u8);
		pub fn hash_keccak_256(input_ptr: *const u8, input_len: u32, out_ptr: *mut u8);
		pub fn hash_blake2_256(input_ptr: *const u8, input_len: u32, out_ptr: *mut u8);
		pub fn hash_blake2_128(input_ptr: *const u8, input_len: u32, out_ptr: *mut u8);
		pub fn call_chain_extension(
			id: u32,
			input_ptr: *const u8,
			input_len: u32,
			out_ptr: *mut u8,
			out_len_ptr: *mut u32,
		) -> ReturnCode;
		pub fn sr25519_verify(
			signature_ptr: *const u8,
			pub_key_ptr: *const u8,
			message_len: u32,
			message_ptr: *const u8,
		) -> ReturnCode;
		pub fn set_code_hash(code_hash_ptr: *const u8);
		pub fn ecdsa_to_eth_address(key_ptr: *const u8, out_ptr: *mut u8) -> ReturnCode;
		pub fn instantiation_nonce() -> u64;
		pub fn return_data_size() -> u64;
		pub fn return_data_copy(out_ptr: *mut u8, out_len_ptr: *mut u32, offset: u32);
	}
}

#[inline(always)]
fn extract_from_slice(output: &mut &mut [u8], new_len: usize) {
	debug_assert!(new_len <= output.len());
	let tmp = core::mem::take(output);
	*output = &mut tmp[..new_len];
}

#[inline(always)]
fn ptr_len_or_sentinel(data: &mut Option<&mut &mut [u8]>) -> (*mut u8, u32) {
	match data {
		Some(ref mut data) => (data.as_mut_ptr(), data.len() as _),
		None => (crate::SENTINEL as _, 0),
	}
}

#[inline(always)]
fn ptr_or_sentinel(data: &Option<&[u8; 32]>) -> *const u8 {
	match data {
		Some(ref data) => data.as_ptr(),
		None => crate::SENTINEL as _,
	}
}

impl HostFn for HostFnImpl {
	fn instantiate(
		ref_time_limit: u64,
		proof_size_limit: u64,
		deposit_limit: &[u8; 32],
		value: &[u8; 32],
		input: &[u8],
		mut address: Option<&mut [u8; 20]>,
		mut output: Option<&mut &mut [u8]>,
		salt: Option<&[u8; 32]>,
	) -> Result {
		let address = match address {
			Some(ref mut data) => data.as_mut_ptr(),
			None => crate::SENTINEL as _,
		};
		let (output_ptr, mut output_len_ptr) = ptr_len_or_sentinel(&mut output);
		let deposit_limit_ptr = deposit_limit.as_ptr();
		let salt_ptr = ptr_or_sentinel(&salt);

		let deposit_and_value = pack_hi_lo(deposit_limit_ptr as _, value.as_ptr() as _);
		let address_and_salt = pack_hi_lo(address as _, salt_ptr as _);
		let input_data = pack_hi_lo(input.len() as _, input.as_ptr() as _);
		let output_data = pack_hi_lo(&mut output_len_ptr as *mut _ as _, output_ptr as _);

		let ret_code = unsafe {
			sys::instantiate(
				ref_time_limit,
				proof_size_limit,
				deposit_and_value,
				input_data,
				output_data,
				address_and_salt,
			)
		};

		if let Some(ref mut output) = output {
			extract_from_slice(output, output_len_ptr as usize);
		}

		ret_code.into()
	}

	fn call(
		flags: CallFlags,
		callee: &[u8; 20],
		ref_time_limit: u64,
		proof_size_limit: u64,
		deposit_limit: &[u8; 32],
		value: &[u8; 32],
		input: &[u8],
		mut output: Option<&mut &mut [u8]>,
	) -> Result {
		let (output_ptr, mut output_len) = ptr_len_or_sentinel(&mut output);
		let deposit_limit_ptr = deposit_limit.as_ptr();

		let flags_and_callee = pack_hi_lo(flags.bits(), callee.as_ptr() as _);
		let deposit_and_value = pack_hi_lo(deposit_limit_ptr as _, value.as_ptr() as _);
		let input_data = pack_hi_lo(input.len() as _, input.as_ptr() as _);
		let output_data = pack_hi_lo(&mut output_len as *mut _ as _, output_ptr as _);

		let ret_code = unsafe {
			sys::call(
				flags_and_callee,
				ref_time_limit,
				proof_size_limit,
				deposit_and_value,
				input_data,
				output_data,
			)
		};

		if let Some(ref mut output) = output {
			extract_from_slice(output, output_len as usize);
		}

		ret_code.into()
	}

	fn delegate_call(
		flags: CallFlags,
		address: &[u8; 20],
		ref_time_limit: u64,
		proof_size_limit: u64,
		deposit_limit: &[u8; 32],
		input: &[u8],
		mut output: Option<&mut &mut [u8]>,
	) -> Result {
		let (output_ptr, mut output_len) = ptr_len_or_sentinel(&mut output);
		let deposit_limit_ptr = deposit_limit.as_ptr();

		let flags_and_callee = pack_hi_lo(flags.bits(), address.as_ptr() as u32);
		let input_data = pack_hi_lo(input.len() as u32, input.as_ptr() as u32);
		let output_data = pack_hi_lo(&mut output_len as *mut _ as u32, output_ptr as u32);

		let ret_code = unsafe {
			sys::delegate_call(
				flags_and_callee,
				ref_time_limit,
				proof_size_limit,
				deposit_limit_ptr as _,
				input_data,
				output_data,
			)
		};

		if let Some(ref mut output) = output {
			extract_from_slice(output, output_len as usize);
		}

		ret_code.into()
	}

	fn deposit_event(topics: &[[u8; 32]], data: &[u8]) {
		unsafe {
			sys::deposit_event(
				topics.as_ptr(),
				topics.len() as u32,
				data.as_ptr(),
				data.len() as u32,
			)
		}
	}

	fn set_storage(flags: StorageFlags, key: &[u8], encoded_value: &[u8]) -> Option<u32> {
		let ret_code = unsafe {
			sys::set_storage(
				flags.bits(),
				key.as_ptr(),
				key.len() as u32,
				encoded_value.as_ptr(),
				encoded_value.len() as u32,
			)
		};
		ret_code.into()
	}

	fn set_storage_or_clear(
		flags: StorageFlags,
		key: &[u8; 32],
		encoded_value: &[u8; 32],
	) -> Option<u32> {
		let ret_code = unsafe {
			sys::set_storage_or_clear(flags.bits(), key.as_ptr(), encoded_value.as_ptr())
		};
		ret_code.into()
	}

	fn get_storage_or_zero(flags: StorageFlags, key: &[u8; 32], output: &mut [u8; 32]) {
		unsafe { sys::get_storage_or_zero(flags.bits(), key.as_ptr(), output.as_mut_ptr()) };
	}

	fn get_storage(flags: StorageFlags, key: &[u8], output: &mut &mut [u8]) -> Result {
		let mut output_len = output.len() as u32;
		let ret_code = {
			unsafe {
				sys::get_storage(
					flags.bits(),
					key.as_ptr(),
					key.len() as u32,
					output.as_mut_ptr(),
					&mut output_len,
				)
			}
		};
		extract_from_slice(output, output_len as usize);
		ret_code.into()
	}

	fn call_data_load(out_ptr: &mut [u8; 32], offset: u32) {
		unsafe { sys::call_data_load(out_ptr.as_mut_ptr(), offset) };
	}

	fn gas_limit() -> u64 {
		unsafe { sys::gas_limit() }
	}

	fn call_data_size() -> u64 {
		unsafe { sys::call_data_size() }
	}

	fn return_value(flags: ReturnFlags, return_value: &[u8]) -> ! {
		unsafe { sys::seal_return(flags.bits(), return_value.as_ptr(), return_value.len() as u32) }
		panic!("seal_return does not return");
	}

	fn gas_price() -> u64 {
		unsafe { sys::gas_price() }
	}

	fn base_fee(output: &mut [u8; 32]) {
		unsafe { sys::base_fee(output.as_mut_ptr()) }
	}

	fn balance(output: &mut [u8; 32]) {
		unsafe { sys::balance(output.as_mut_ptr()) }
	}

	fn value_transferred(output: &mut [u8; 32]) {
		unsafe { sys::value_transferred(output.as_mut_ptr()) }
	}

	fn now(output: &mut [u8; 32]) {
		unsafe { sys::now(output.as_mut_ptr()) }
	}

	fn chain_id(output: &mut [u8; 32]) {
		unsafe { sys::chain_id(output.as_mut_ptr()) }
	}

	fn address(output: &mut [u8; 20]) {
		unsafe { sys::address(output.as_mut_ptr()) }
	}

	fn caller(output: &mut [u8; 20]) {
		unsafe { sys::caller(output.as_mut_ptr()) }
	}

	fn origin(output: &mut [u8; 20]) {
		unsafe { sys::origin(output.as_mut_ptr()) }
	}

	fn block_number(output: &mut [u8; 32]) {
		unsafe { sys::block_number(output.as_mut_ptr()) }
	}

	fn block_author(output: &mut [u8; 20]) {
		unsafe { sys::block_author(output.as_mut_ptr()) }
	}

	fn weight_to_fee(ref_time_limit: u64, proof_size_limit: u64, output: &mut [u8; 32]) {
		unsafe { sys::weight_to_fee(ref_time_limit, proof_size_limit, output.as_mut_ptr()) };
	}

	fn hash_keccak_256(input: &[u8], output: &mut [u8; 32]) {
		unsafe { sys::hash_keccak_256(input.as_ptr(), input.len() as u32, output.as_mut_ptr()) }
	}

	fn get_immutable_data(output: &mut &mut [u8]) {
		let mut output_len = output.len() as u32;
		unsafe { sys::get_immutable_data(output.as_mut_ptr(), &mut output_len) };
		extract_from_slice(output, output_len as usize);
	}

	fn set_immutable_data(data: &[u8]) {
		unsafe { sys::set_immutable_data(data.as_ptr(), data.len() as u32) }
	}

	fn balance_of(address: &[u8; 20], output: &mut [u8; 32]) {
		unsafe { sys::balance_of(address.as_ptr(), output.as_mut_ptr()) };
	}

	fn code_hash(address: &[u8; 20], output: &mut [u8; 32]) {
		unsafe { sys::code_hash(address.as_ptr(), output.as_mut_ptr()) }
	}

	fn code_size(address: &[u8; 20]) -> u64 {
		unsafe { sys::code_size(address.as_ptr()) }
	}

	fn return_data_size() -> u64 {
		unsafe { sys::return_data_size() }
	}

	fn return_data_copy(output: &mut &mut [u8], offset: u32) {
		let mut output_len = output.len() as u32;
		{
			unsafe { sys::return_data_copy(output.as_mut_ptr(), &mut output_len, offset) };
		}
		extract_from_slice(output, output_len as usize);
	}

	fn ref_time_left() -> u64 {
		unsafe { sys::ref_time_left() }
	}

	#[unstable_hostfn]
	fn to_account_id(address: &[u8; 20], output: &mut [u8]) {
		unsafe { sys::to_account_id(address.as_ptr(), output.as_mut_ptr()) }
	}

	#[unstable_hostfn]
	fn block_hash(block_number_ptr: &[u8; 32], output: &mut [u8; 32]) {
		unsafe { sys::block_hash(block_number_ptr.as_ptr(), output.as_mut_ptr()) };
	}

	#[unstable_hostfn]
	fn call_chain_extension(func_id: u32, input: &[u8], mut output: Option<&mut &mut [u8]>) -> u32 {
		let (output_ptr, mut output_len) = ptr_len_or_sentinel(&mut output);
		let ret_code = {
			unsafe {
				sys::call_chain_extension(
					func_id,
					input.as_ptr(),
					input.len() as u32,
					output_ptr,
					&mut output_len,
				)
			}
		};

		if let Some(ref mut output) = output {
			extract_from_slice(output, output_len as usize);
		}
		ret_code.into_u32()
	}

	fn call_data_copy(output: &mut [u8], offset: u32) {
		let len = output.len() as u32;
		unsafe { sys::call_data_copy(output.as_mut_ptr(), len, offset) };
	}

	#[unstable_hostfn]
	fn caller_is_origin() -> bool {
		let ret_val = unsafe { sys::caller_is_origin() };
		ret_val.into_bool()
	}

	#[unstable_hostfn]
	fn caller_is_root() -> bool {
		let ret_val = unsafe { sys::caller_is_root() };
		ret_val.into_bool()
	}

	#[unstable_hostfn]
	fn clear_storage(flags: StorageFlags, key: &[u8]) -> Option<u32> {
		let ret_code = unsafe { sys::clear_storage(flags.bits(), key.as_ptr(), key.len() as u32) };
		ret_code.into()
	}

	#[unstable_hostfn]
	fn contains_storage(flags: StorageFlags, key: &[u8]) -> Option<u32> {
		let ret_code =
			unsafe { sys::contains_storage(flags.bits(), key.as_ptr(), key.len() as u32) };
		ret_code.into()
	}

	#[unstable_hostfn]
	fn ecdsa_to_eth_address(pubkey: &[u8; 33], output: &mut [u8; 20]) -> Result {
		let ret_code = unsafe { sys::ecdsa_to_eth_address(pubkey.as_ptr(), output.as_mut_ptr()) };
		ret_code.into()
	}

	#[unstable_hostfn]
	fn hash_blake2_256(input: &[u8], output: &mut [u8; 32]) {
		unsafe { sys::hash_blake2_256(input.as_ptr(), input.len() as u32, output.as_mut_ptr()) }
	}

	#[unstable_hostfn]
	fn hash_blake2_128(input: &[u8], output: &mut [u8; 16]) {
		unsafe { sys::hash_blake2_128(input.as_ptr(), input.len() as u32, output.as_mut_ptr()) }
	}

	#[unstable_hostfn]
	fn is_contract(address: &[u8; 20]) -> bool {
		let ret_val = unsafe { sys::is_contract(address.as_ptr()) };
		ret_val.into_bool()
	}

	#[unstable_hostfn]
	fn minimum_balance(output: &mut [u8; 32]) {
		unsafe { sys::minimum_balance(output.as_mut_ptr()) }
	}

	#[unstable_hostfn]
	fn own_code_hash(output: &mut [u8; 32]) {
		unsafe { sys::own_code_hash(output.as_mut_ptr()) }
	}

	#[unstable_hostfn]
	fn set_code_hash(code_hash: &[u8; 32]) {
		unsafe { sys::set_code_hash(code_hash.as_ptr()) }
	}

	#[unstable_hostfn]
	fn sr25519_verify(signature: &[u8; 64], message: &[u8], pub_key: &[u8; 32]) -> Result {
		let ret_code = unsafe {
			sys::sr25519_verify(
				signature.as_ptr(),
				pub_key.as_ptr(),
				message.len() as u32,
				message.as_ptr(),
			)
		};
		ret_code.into()
	}

	#[unstable_hostfn]
	fn take_storage(flags: StorageFlags, key: &[u8], output: &mut &mut [u8]) -> Result {
		let mut output_len = output.len() as u32;
		let ret_code = {
			unsafe {
				sys::take_storage(
					flags.bits(),
					key.as_ptr(),
					key.len() as u32,
					output.as_mut_ptr(),
					&mut output_len,
				)
			}
		};
		extract_from_slice(output, output_len as usize);
		ret_code.into()
	}

	#[unstable_hostfn]
	fn terminate(beneficiary: &[u8; 20]) -> ! {
		unsafe { sys::terminate(beneficiary.as_ptr()) }
		panic!("terminate does not return");
	}

	#[unstable_hostfn]
	fn weight_left(output: &mut &mut [u8]) {
		let mut output_len = output.len() as u32;
		unsafe { sys::weight_left(output.as_mut_ptr(), &mut output_len) }
		extract_from_slice(output, output_len as usize)
	}
}