Skip to content

Commit cf330cc

Browse files
authored
[pallet-revive] refactor uapi with better types (#5555)
start using better type for address, code_hash, and salt in runtime and the uapi crate fix #5575
1 parent d5346e7 commit cf330cc

35 files changed

Lines changed: 211 additions & 201 deletions

prdoc/pr_5555.prdoc

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
title: Make salt optional
2+
3+
doc:
4+
- audience: Runtime Dev
5+
description: |
6+
Remove address_len and salt_len from uapi as both are now fixed size
7+
8+
crates:
9+
- name: pallet-revive
10+
bump: patch
11+
- name: pallet-revive-uapi
12+
bump: patch
13+
- name: pallet-revive-fixtures
14+
bump: patch
15+

substrate/frame/revive/fixtures/build.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ mod build {
159159
fn post_process(input_path: &Path, output_path: &Path) -> Result<()> {
160160
let mut config = polkavm_linker::Config::default();
161161
config.set_strip(true);
162+
config.set_optimize(false);
162163
let orig =
163164
fs::read(input_path).with_context(|| format!("Failed to read {:?}", input_path))?;
164165
let linked = polkavm_linker::program_from_elf(config, orig.as_ref())

substrate/frame/revive/fixtures/contracts/call.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ pub extern "C" fn deploy() {}
3131
pub extern "C" fn call() {
3232
input!(
3333
callee_input: [u8; 4],
34-
callee_addr: [u8; 20],
34+
callee_addr: &[u8; 20],
3535
);
3636

3737
// Call the callee

substrate/frame/revive/fixtures/contracts/call_return_code.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ pub extern "C" fn deploy() {}
3333
pub extern "C" fn call() {
3434
input!(
3535
100,
36-
callee_addr: [u8; 20],
36+
callee_addr: &[u8; 20],
3737
input: [u8],
3838
);
3939

substrate/frame/revive/fixtures/contracts/call_runtime_and_call.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ pub extern "C" fn call() {
3131
input!(
3232
512,
3333
callee_input: [u8; 4],
34-
callee_addr: [u8; 20],
34+
callee_addr: &[u8; 20],
3535
call: [u8],
3636
);
3737

substrate/frame/revive/fixtures/contracts/call_with_flags_and_value.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ pub extern "C" fn deploy() {}
3131
pub extern "C" fn call() {
3232
input!(
3333
256,
34-
callee_addr: [u8; 20],
34+
callee_addr: &[u8; 20],
3535
flags: u32,
3636
value: u64,
3737
forwarded_input: [u8],

substrate/frame/revive/fixtures/contracts/call_with_limit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ pub extern "C" fn deploy() {}
3232
pub extern "C" fn call() {
3333
input!(
3434
256,
35-
callee_addr: [u8; 20],
35+
callee_addr: &[u8; 20],
3636
ref_time: u64,
3737
proof_size: u64,
3838
forwarded_input: [u8],

substrate/frame/revive/fixtures/contracts/caller_contract.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ pub extern "C" fn deploy() {}
2828
#[no_mangle]
2929
#[polkavm_derive::polkavm_export]
3030
pub extern "C" fn call() {
31-
input!(code_hash: [u8; 32],);
31+
input!(code_hash: &[u8; 32],);
3232

3333
// The value to transfer on instantiation and calls. Chosen to be greater than existential
3434
// deposit.
@@ -73,7 +73,6 @@ pub extern "C" fn call() {
7373

7474
// Deploy the contract successfully.
7575
let mut callee = [0u8; 20];
76-
let callee = &mut &mut callee[..];
7776

7877
api::instantiate(
7978
code_hash,
@@ -82,17 +81,16 @@ pub extern "C" fn call() {
8281
None, // No deposit limit.
8382
&value,
8483
&input,
85-
Some(callee),
84+
Some(&mut callee),
8685
None,
8786
&salt,
8887
)
8988
.unwrap();
90-
assert_eq!(callee.len(), 20);
9189

9290
// Call the new contract and expect it to return failing exit code.
9391
let res = api::call(
9492
uapi::CallFlags::empty(),
95-
callee,
93+
&callee,
9694
0u64, // How much ref_time weight to devote for the execution. 0 = all.
9795
0u64, // How much proof_size weight to devote for the execution. 0 = all.
9896
None, // No deposit limit.
@@ -105,7 +103,7 @@ pub extern "C" fn call() {
105103
// Fail to call the contract due to insufficient ref_time weight.
106104
let res = api::call(
107105
uapi::CallFlags::empty(),
108-
callee,
106+
&callee,
109107
1u64, // Too little ref_time weight.
110108
0u64, // How much proof_size weight to devote for the execution. 0 = all.
111109
None, // No deposit limit.
@@ -118,7 +116,7 @@ pub extern "C" fn call() {
118116
// Fail to call the contract due to insufficient proof_size weight.
119117
let res = api::call(
120118
uapi::CallFlags::empty(),
121-
callee,
119+
&callee,
122120
0u64, // How much ref_time weight to devote for the execution. 0 = all.
123121
1u64, // too little proof_size weight
124122
None, // No deposit limit.
@@ -132,7 +130,7 @@ pub extern "C" fn call() {
132130
let mut output = [0u8; 4];
133131
api::call(
134132
uapi::CallFlags::empty(),
135-
callee,
133+
&callee,
136134
0u64, // How much ref_time weight to devote for the execution. 0 = all.
137135
0u64, // How much proof_size weight to devote for the execution. 0 = all.
138136
None, // No deposit limit.

substrate/frame/revive/fixtures/contracts/chain_extension_temp_storage.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
#![no_std]
2121
#![no_main]
2222

23-
use common::{input, output};
23+
use common::input;
2424
use uapi::{HostFn, HostFnImpl as api};
2525

2626
#[no_mangle]
@@ -47,12 +47,13 @@ pub extern "C" fn call() {
4747
input[8] = 1u8;
4848

4949
// Read the contract address.
50-
output!(addr, [0u8; 32], api::address,);
50+
let mut addr = [0u8; 20];
51+
api::address(&mut addr);
5152

5253
// call self
5354
api::call(
5455
uapi::CallFlags::ALLOW_REENTRY,
55-
addr,
56+
&addr,
5657
0u64, // How much ref_time to devote for the execution. 0 = all.
5758
0u64, // How much proof_size to devote for the execution. 0 = all.
5859
None, // No deposit limit.

substrate/frame/revive/fixtures/contracts/common/src/lib.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,23 @@ macro_rules! input {
100100
input!(@inner $input, $cursor + $n, $($rest)*);
101101
};
102102

103+
// Match an array reference of the given size.
104+
// e.g input!(var1: &[u8; 32], );
105+
(@inner $input:expr, $cursor:expr, $var:ident: &[u8; $n:expr], $($rest:tt)*) => {
106+
let $var: &[u8; $n] = &$input[$cursor..$cursor+$n].try_into().unwrap();
107+
input!(@inner $input, $cursor + $n, $($rest)*);
108+
};
109+
103110
// Size of a u8 slice.
104111
(@size $size:expr, $var:ident: [u8; $n:expr], $($rest:tt)*) => {
105112
input!(@size $size + $n, $($rest)*)
106113
};
107114

115+
// Size of an array reference.
116+
(@size $size:expr, $var:ident: &[u8; $n:expr], $($rest:tt)*) => {
117+
input!(@size $size + $n, $($rest)*)
118+
};
119+
108120
// Entry point, with the buffer and it's size specified first.
109121
// e.g input!(buffer, 512, var1: u32, var2: [u8], );
110122
($buffer:ident, $size:expr, $($rest:tt)*) => {

0 commit comments

Comments
 (0)