Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Contracts: Translate .wat fixtures to rust #2654

Merged
merged 78 commits into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
78 commits
Select commit Hold shift + click to select a range
9975e91
compile risc-v
pgherveou Nov 29, 2023
260411f
rm dup
pgherveou Nov 29, 2023
235b1f6
tweaks
pgherveou Nov 30, 2023
619f314
conditional build for riscv
pgherveou Nov 30, 2023
7b3e2fb
check CI & macos
pgherveou Nov 30, 2023
c28c53a
rm macos test
pgherveou Nov 30, 2023
4115768
PR review rm unneeded dep from Cargo.toml
pgherveou Dec 1, 2023
fb7d8fe
update rustup_toolchain
pgherveou Dec 7, 2023
d4533ee
Merge branch 'pg/riscv-fixtures' of https://github.com/paritytech/pol…
pgherveou Dec 7, 2023
3fc102f
Merge branch 'master' into pg/riscv-fixtures
pgherveou Dec 7, 2023
10ef504
fix build
pgherveou Dec 7, 2023
d39ceeb
testing
pgherveou Dec 7, 2023
14f48fc
rm warnings on risc-v
pgherveou Dec 7, 2023
43bb175
specify rustup_home
pgherveou Dec 7, 2023
382042b
Merge branch 'pg/riscv-fixtures' of https://github.com/paritytech/pol…
pgherveou Dec 7, 2023
2c1ea12
rollback toolchain name
pgherveou Dec 7, 2023
7bb8397
Merge branch 'master' into pg/riscv-fixtures
pgherveou Dec 7, 2023
ddbddaf
Test updating openssl
pgherveou Dec 8, 2023
0bd773c
used shared toolchain name
pgherveou Dec 8, 2023
07a4520
fix
pgherveou Dec 8, 2023
0adc1e9
Update toolchain name
pgherveou Dec 11, 2023
366acbd
Update error message
pgherveou Dec 11, 2023
c077e4b
Update .gitlab-ci.yml
pgherveou Dec 12, 2023
29d188d
Apply suggestions from code review
pgherveou Dec 12, 2023
e1f7df1
Update substrate/frame/contracts/fixtures/build.rs
pgherveou Dec 12, 2023
cbf90e5
Apply suggestions from code review
pgherveou Dec 12, 2023
c17cb91
try new ci image
alvicsam Dec 12, 2023
a3b8913
Merge branch 'master' into pg/riscv-fixtures
pgherveou Dec 13, 2023
f501a49
update ci image
alvicsam Dec 13, 2023
7ea4e83
update cargo.lock
alvicsam Dec 13, 2023
6dbc6cc
Fix CI for new ci image (#2701)
alvicsam Dec 13, 2023
2bb8c47
Cargo.lock
alvicsam Dec 13, 2023
65e04fe
Merge branch 'master' into pg/riscv-fixtures
alvicsam Dec 13, 2023
af99fbd
Cargo.lock
alvicsam Dec 14, 2023
f265376
Update to make it work while we are getting the CI toolchain ready
pgherveou Jan 2, 2024
5d50f14
Merge branch 'master' into pg/riscv-fixtures
pgherveou Jan 2, 2024
ead54b4
update lock
pgherveou Jan 2, 2024
547d2e6
clippy fixes
pgherveou Jan 2, 2024
2c46883
fix test
pgherveou Jan 2, 2024
d97459d
revert docker file images
pgherveou Jan 2, 2024
5bf77a3
add comment
pgherveou Jan 2, 2024
a9186b8
fix
pgherveou Jan 2, 2024
d75d5f1
rm comment
pgherveou Jan 3, 2024
71f3249
update
pgherveou Jan 3, 2024
328e450
update
pgherveou Jan 3, 2024
3b34076
Roll back hacks
pgherveou Jan 3, 2024
b0a2d8b
fix
pgherveou Jan 3, 2024
b1ce7f3
fixes
pgherveou Jan 3, 2024
0c7385e
move stuff from pg/add-fixtures in this PR
pgherveou Jan 4, 2024
cfeb1ba
Update substrate/frame/contracts/fixtures/Cargo.toml
pgherveou Jan 4, 2024
bab4839
Update substrate/frame/contracts/fixtures/build/riscv_memory_layout.ld
pgherveou Jan 4, 2024
cf6ca58
Merge branch 'master' into pg/riscv-fixtures
pgherveou Jan 4, 2024
dac377c
update lock
pgherveou Jan 4, 2024
ae228f7
Add fixtures
pgherveou Dec 7, 2023
a9e3b0d
Merge branch 'master' into pg/add-fixtures
pgherveou Jan 4, 2024
3b9149e
clippy lints
pgherveou Jan 4, 2024
cf24a38
PR comment: rm useless cfg
pgherveou Jan 5, 2024
5ef8a26
Leave MaxEncodedLen as marker trait
pgherveou Jan 5, 2024
ba98c6c
fix doc
pgherveou Jan 5, 2024
853e5c3
add back import
pgherveou Jan 5, 2024
4daf1b7
Add regression test
pgherveou Jan 5, 2024
68d7079
add back chain extension stuffs
pgherveou Jan 5, 2024
e4fdea8
use val1
pgherveou Jan 5, 2024
bd6a2cd
bump polkavm
pgherveou Jan 5, 2024
53489f6
Contracts rename riscv-experimental and enable ci tests
pgherveou Jan 8, 2024
cdecb95
Merge branch 'master' into pg/add-fixtures
pgherveou Jan 8, 2024
6b15780
Fix uapi signature s/Option<&mut [u8]>/Option<&mut &mut [u8]>
pgherveou Jan 8, 2024
edb4f1a
Merge branch 'master' into pg/add-fixtures
pgherveou Jan 9, 2024
0143d30
".git/.scripts/commands/fmt/fmt.sh"
Jan 9, 2024
9998a96
Merge branch 'master' into pg/add-fixtures
pgherveou Jan 9, 2024
62f9ae1
fix for new toolchain
pgherveou Jan 9, 2024
0e3cfea
fmt
pgherveou Jan 9, 2024
1ded19f
Try to add back some inline(always) statement to compare contracts size
pgherveou Jan 9, 2024
f9d16f2
Revert "Try to add back some inline(always) statement to compare cont…
pgherveou Jan 10, 2024
9396576
Tweak to reduce sizes
pgherveou Jan 12, 2024
679fa53
fmt
pgherveou Jan 12, 2024
4f4d8eb
Fix
pgherveou Jan 12, 2024
92cfb13
Merge branch 'master' into pg/add-fixtures
pgherveou Jan 12, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// This file is part of Substrate.

// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0

// 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.

//! This fixture tests if account_reentrance_count works as expected.
#![no_std]
#![no_main]

use common::input;
use uapi::{HostFn, HostFnImpl as api};

#[no_mangle]
#[polkavm_derive::polkavm_export]
pub extern "C" fn deploy() {}

#[no_mangle]
#[polkavm_derive::polkavm_export]
pub extern "C" fn call() {
input!(callee: [u8; 32],);

#[allow(deprecated)]
let reentrance_count = api::account_reentrance_count(callee);

// Return the reentrance count.
api::return_value(uapi::ReturnFlags::empty(), &reentrance_count.to_le_bytes());
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// This file is part of Substrate.

// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0

// 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.

//! This contract tests the behavior of adding / removing delegate_dependencies when delegate
//! calling into a contract.
#![no_std]
#![no_main]

use common::input;
use uapi::{HostFn, HostFnImpl as api};

const ALICE: [u8; 32] = [1u8; 32];

/// Load input data and perform the action specified by the input.
/// If `delegate_call` is true, then delegate call into the contract.
fn load_input(delegate_call: bool) {
input!(
action: u32,
code_hash: [u8; 32],
);

match action {
// 1 = Add delegate dependency
1 => {
#[allow(deprecated)]
api::add_delegate_dependency(code_hash);
},
// 2 = Remove delegate dependency
2 => {
#[allow(deprecated)]
api::remove_delegate_dependency(code_hash);
},
// 3 = Terminate
3 => {
api::terminate_v1(&ALICE);
},
// Everything else is a noop
_ => {},
}

if delegate_call {
api::delegate_call(uapi::CallFlags::empty(), code_hash, &[], None).unwrap();
}
}

#[no_mangle]
#[polkavm_derive::polkavm_export]
pub extern "C" fn deploy() {
load_input(false);
}

#[no_mangle]
#[polkavm_derive::polkavm_export]
pub extern "C" fn call() {
load_input(true);
}
36 changes: 36 additions & 0 deletions substrate/frame/contracts/fixtures/contracts/balance.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// This file is part of Substrate.

// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0

// 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.

#![no_std]
#![no_main]

use common::output;
use uapi::{HostFn, HostFnImpl as api};

#[no_mangle]
#[polkavm_derive::polkavm_export]
pub extern "C" fn deploy() {}

#[no_mangle]
#[polkavm_derive::polkavm_export]
pub extern "C" fn call() {
// Initialize buffer with 1s so that we can check that it is overwritten.
output!(balance, [1u8; 8], api::balance,);

// Assert that the balance is 0.
assert_eq!(&[0u8; 8], balance);
}
25 changes: 11 additions & 14 deletions substrate/frame/contracts/fixtures/contracts/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
#![no_std]
#![no_main]

extern crate common;
use uapi::{CallFlags, HostFn, HostFnImpl as api};
use common::input;
use uapi::{HostFn, HostFnImpl as api};

#[no_mangle]
#[polkavm_derive::polkavm_export]
Expand All @@ -29,21 +29,18 @@ pub extern "C" fn deploy() {}
#[no_mangle]
#[polkavm_derive::polkavm_export]
pub extern "C" fn call() {
let mut buffer = [0u8; 40];
let callee_input = 0..4;
let callee_addr = 4..36;
let value = 36..40;

// Read the input data.
api::input(&mut &mut buffer[..]);
input!(
callee_input: [u8; 4],
callee_addr: [u8; 32],
);

// Call the callee
api::call_v1(
CallFlags::empty(),
&buffer[callee_addr],
0u64, // How much gas to devote for the execution. 0 = all.
&buffer[value],
&buffer[callee_input],
uapi::CallFlags::empty(),
callee_addr,
0u64, // How much gas to devote for the execution. 0 = all.
&0u64.to_le_bytes(), // value transferred to the contract.
callee_input,
None,
)
.unwrap();
Expand Down
54 changes: 54 additions & 0 deletions substrate/frame/contracts/fixtures/contracts/call_return_code.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// This file is part of Substrate.

// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0

// 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.

//! This calls the supplied dest and transfers 100 balance during this call and copies
//! the return code of this call to the output buffer.
//! It also forwards its input to the callee.
#![no_std]
#![no_main]

use common::input;
use uapi::{HostFn, HostFnImpl as api};

#[no_mangle]
#[polkavm_derive::polkavm_export]
pub extern "C" fn deploy() {}

#[no_mangle]
#[polkavm_derive::polkavm_export]
pub extern "C" fn call() {
input!(
100,
callee_addr: [u8; 32],
input: [u8],
);

// Call the callee
let err_code = match api::call_v1(
uapi::CallFlags::empty(),
callee_addr,
0u64, // How much gas to devote for the execution. 0 = all.
&100u64.to_le_bytes(), // value transferred to the contract.
input,
None,
) {
Ok(_) => 0u32,
Err(code) => code as u32,
};

api::return_value(uapi::ReturnFlags::empty(), &err_code.to_le_bytes());
}
42 changes: 42 additions & 0 deletions substrate/frame/contracts/fixtures/contracts/call_runtime.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// This file is part of Substrate.

// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0

// 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.

//! This passes its input to `call_runtime` and returns the return value to its caller.
#![no_std]
#![no_main]

use common::input;
use uapi::{HostFn, HostFnImpl as api};

#[no_mangle]
#[polkavm_derive::polkavm_export]
pub extern "C" fn deploy() {}

#[no_mangle]
#[polkavm_derive::polkavm_export]
pub extern "C" fn call() {
// Fixture calls should fit into 100 bytes.
input!(100, call: [u8], );

// Use the call passed as input to call the runtime.
let err_code = match api::call_runtime(call) {
Ok(_) => 0u32,
Err(code) => code as u32,
};

api::return_value(uapi::ReturnFlags::empty(), &err_code.to_le_bytes());
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// This file is part of Substrate.

// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0

// 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.

#![no_std]
#![no_main]

use common::input;
use uapi::{HostFn, HostFnImpl as api};

#[no_mangle]
#[polkavm_derive::polkavm_export]
pub extern "C" fn deploy() {}

#[no_mangle]
#[polkavm_derive::polkavm_export]
pub extern "C" fn call() {
input!(
512,
callee_input: [u8; 4],
callee_addr: [u8; 32],
call: [u8],
);

// Use the call passed as input to call the runtime.
api::call_runtime(call).unwrap();

// Call the callee
api::call_v1(
uapi::CallFlags::empty(),
callee_addr,
0u64, // How much gas to devote for the execution. 0 = all.
&0u64.to_le_bytes(), // value transferred to the contract.
callee_input,
None,
)
.unwrap();
}
51 changes: 51 additions & 0 deletions substrate/frame/contracts/fixtures/contracts/call_with_limit.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// This file is part of Substrate.

// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0

// 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.

//! This fixture calls the account_id with the 2D Weight limit.
//! It returns the result of the call as output data.
#![no_std]
#![no_main]

use common::input;
use uapi::{HostFn, HostFnImpl as api};

#[no_mangle]
#[polkavm_derive::polkavm_export]
pub extern "C" fn deploy() {}

#[no_mangle]
#[polkavm_derive::polkavm_export]
pub extern "C" fn call() {
input!(
callee_addr: [u8; 32],
ref_time: u64,
proof_size: u64,
);

#[allow(deprecated)]
api::call_v2(
uapi::CallFlags::empty(),
callee_addr,
ref_time,
proof_size,
None, // No deposit limit.
&0u64.to_le_bytes(), // value transferred to the contract.
&[0u8; 0], // input data.
None,
)
.unwrap();
}
Loading