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

Use pallet-contracts-uapi #2038

Merged
merged 49 commits into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from 48 commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
2be8da5
rm stuff
pgherveou Dec 8, 2023
5d25207
wip
pgherveou Jan 2, 2024
79c3814
rm dead code
pgherveou Jan 2, 2024
2d1db8b
match existing calls
pgherveou Jan 2, 2024
e891996
wip
pgherveou Jan 2, 2024
76acdca
Fixes
pgherveou Jan 4, 2024
a59d065
Fix tests
pgherveou Jan 4, 2024
a0fac1d
fix
pgherveou Jan 4, 2024
4339ef0
Update architecture
pgherveou Jan 5, 2024
dd7a7c0
use git dep for now
pgherveou Jan 5, 2024
33175c2
fmt
pgherveou Jan 5, 2024
1685260
fmt
pgherveou Jan 5, 2024
ea60dfa
Add ink-debug
pgherveou Jan 5, 2024
1c67ad8
fix
pgherveou Jan 5, 2024
5ce8c4f
update clippy
pgherveou Jan 5, 2024
7e88004
fmt
pgherveou Jan 5, 2024
76d93b2
fix
pgherveou Jan 5, 2024
7431342
fix fmt
pgherveou Jan 5, 2024
ee10340
fix clippy error
pgherveou Jan 5, 2024
eebb2c0
update lock
pgherveou Jan 5, 2024
b7e58b6
fix
pgherveou Jan 5, 2024
a998813
fix clippy
pgherveou Jan 5, 2024
a354db0
fix
pgherveou Jan 5, 2024
e1cddd0
fix fmt
pgherveou Jan 5, 2024
fc85d3f
Fix doc
pgherveou Jan 5, 2024
fdabcd6
fix docstring
pgherveou Jan 6, 2024
0464b72
wip
pgherveou Jan 8, 2024
c99afd6
Fix
pgherveou Jan 8, 2024
bb5e710
fix
pgherveou Jan 8, 2024
66de170
update cargo.toml
pgherveou Jan 8, 2024
fb30f38
fix lock
pgherveou Jan 8, 2024
62502f1
fix
pgherveou Jan 8, 2024
e951fa7
Test bump uapi branch
pgherveou Jan 9, 2024
9c38942
Test with add-fixtures-test-size
pgherveou Jan 10, 2024
71a7080
Test do not compose errors
pgherveou Jan 10, 2024
3519e2c
use inline_always_uapi branch
pgherveou Jan 10, 2024
064e3f2
Bump locks
pgherveou Jan 10, 2024
133dee5
Bump lockwq
pgherveou Jan 11, 2024
1675bd8
tweak generator / ReturnFlags
pgherveou Jan 11, 2024
825601d
Revert "Test do not compose errors"
pgherveou Jan 11, 2024
686d1c8
generator dispatch fix
pgherveou Jan 11, 2024
6b74370
fix
pgherveou Jan 11, 2024
f5db4ef
fix
pgherveou Jan 11, 2024
945c7e3
fix lock
pgherveou Jan 11, 2024
9914c7c
rm EnvError alias
pgherveou Jan 23, 2024
a21837c
PR review
pgherveou Jan 23, 2024
f73fbae
Use crates.io package
pgherveou Jan 23, 2024
49d49dc
Fix clippy
pgherveou Jan 23, 2024
987e869
use ink-pallet-contracts-uapi until newer version get published
pgherveou Jan 23, 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
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ env:
# We plan to fully support RISC-V as a bytecode for contracts soon.
# RISC-V does not have a standard library in contrast to Wasm. Compiling against
# this target also makes sure that we don't pull in `std` by accident (through dependencies).
# RISC-V is a modular archtitecture. We might switch to a different flavor with more features
# RISC-V is a modular architecture. We might switch to a different flavor with more features
# later. For example, `riscv32imc-unknown-none-elf`.
RISCV_TARGET: riscv32i-unknown-none-elf

Expand Down
4 changes: 2 additions & 2 deletions ARCHITECTURE.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,8 @@ If you look at the implementations you'll see a common pattern of
for contract developers.

### The pallet API
The function signature of the `pallet-contracts` API functions is defined in
[`ink_env/src/engine/on_chain/ext.rs`](https://github.com/paritytech/ink/blob/master/crates/env/src/engine/on_chain/ext.rs).
Signatures of host API functions are defined in
[`pallet-contracts-uapi`](https://github.com/paritytech/polkadot-sdk/blob/master/substrate/frame/contracts/uapi/src/host/wasm32.rs).
You'll see that we import different versions of API functions, something
like the following excerpt:

Expand Down
43 changes: 43 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ const_env = { version = "0.1"}

# Substrate dependencies
pallet-contracts-primitives = { version = "26.0.0", default-features = false }
pallet-contracts-uapi = { version = "5.0.0", default-features = false }
sp-core = { version = "23.0.0", default-features = false }
sp-keyring = { version = "26.0.0", default-features = false }
sp-runtime = { version = "26.0.0", default-features = false }
Expand Down
1 change: 1 addition & 0 deletions crates/engine/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ include = ["Cargo.toml", "src/**/*.rs", "README.md", "LICENSE"]
[dependencies]
ink_primitives = { workspace = true }
scale = { workspace = true }
pallet-contracts-uapi = { workspace = true }
derive_more = { workspace = true, features = ["from", "display"] }

sha2 = { workspace = true }
Expand Down
77 changes: 1 addition & 76 deletions crates/engine/src/ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,85 +31,10 @@ use crate::{
BlockTimestamp,
},
};
pub use pallet_contracts_uapi::ReturnErrorCode as Error;
use scale::Encode;
use std::panic::panic_any;

macro_rules! define_error_codes {
(
$(
$( #[$attr:meta] )*
$name:ident = $discr:literal,
)*
) => {
/// Every error that can be returned to a contract when it calls any of the host functions.
#[cfg_attr(test, derive(PartialEq, Eq))]
#[derive(Debug)]
#[repr(u32)]
pub enum Error {
$(
$( #[$attr] )*
$name = $discr,
)*
/// Returns if an unknown error was received from the host module.
Unknown,
}

impl From<ReturnCode> for Result<(), Error> {
#[inline]
fn from(return_code: ReturnCode) -> Self {
match return_code.0 {
0 => Ok(()),
$(
$discr => Err(Error::$name),
)*
_ => Err(Error::Unknown),
}
}
}
};
}
define_error_codes! {
/// The called function trapped and has its state changes reverted.
/// In this case no output buffer is returned.
/// Can only be returned from `call` and `instantiate`.
CalleeTrapped = 1,
/// The called function ran to completion but decided to revert its state.
/// An output buffer is returned when one was supplied.
/// Can only be returned from `call` and `instantiate`.
CalleeReverted = 2,
/// The passed key does not exist in storage.
KeyNotFound = 3,
/// Deprecated and no longer returned: There is only the minimum balance.
_BelowSubsistenceThreshold = 4,
/// Transfer failed for other not further specified reason. Most probably
/// reserved or locked balance of the sender that was preventing the transfer.
TransferFailed = 5,
/// Deprecated and no longer returned: Endowment is no longer required.
_EndowmentTooLow = 6,
/// No code could be found at the supplied code hash.
CodeNotFound = 7,
/// The account that was called is no contract.
NotCallable = 8,
/// The call to `debug_message` had no effect because debug message
/// recording was disabled.
LoggingDisabled = 9,
/// ECDSA public key recovery failed. Most probably wrong recovery id or signature.
EcdsaRecoveryFailed = 11,
/// sr25519 signature verification failed. This may be because of an invalid public key, invalid message or invalid signature.
Sr25519VerifyFailed = 12,
}

/// The raw return code returned by the host side.
#[repr(transparent)]
pub struct ReturnCode(u32);

impl ReturnCode {
/// Returns the raw underlying `u32` representation.
pub fn into_u32(self) -> u32 {
self.0
}
}

/// The off-chain engine.
pub struct Engine {
/// The environment database.
Expand Down
1 change: 1 addition & 0 deletions crates/env/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ ink_allocator = { workspace = true }
ink_storage_traits = { workspace = true }
ink_prelude = { workspace = true }
ink_primitives = { workspace = true }
pallet-contracts-uapi = { workspace = true }

scale = { workspace = true }
derive_more = { workspace = true, features = ["from", "display"] }
Expand Down
2 changes: 1 addition & 1 deletion crates/env/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use crate::{
backend::{
EnvBackend,
ReturnFlags,
TypedEnvBackend,
},
call::{
Expand All @@ -42,6 +41,7 @@ use crate::{
Result,
};
use ink_storage_traits::Storable;
use pallet_contracts_uapi::ReturnFlags;

/// Returns the address of the caller of the executed contract.
///
Expand Down
137 changes: 1 addition & 136 deletions crates/env/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,142 +30,7 @@ use crate::{
Result,
};
use ink_storage_traits::Storable;

/// The flags to indicate further information about the end of a contract execution.
#[derive(Default)]
pub struct ReturnFlags {
value: u32,
}

impl ReturnFlags {
/// Initialize [`ReturnFlags`] with the reverted flag.
pub fn new_with_reverted(has_reverted: bool) -> Self {
Self::default().set_reverted(has_reverted)
}

/// Sets the bit to indicate that the execution is going to be reverted.
#[must_use]
pub fn set_reverted(mut self, has_reverted: bool) -> Self {
match has_reverted {
true => self.value |= has_reverted as u32,
false => self.value &= !has_reverted as u32,
}
self
}

/// Returns the underlying `u32` representation.
#[cfg(not(feature = "std"))]
pub(crate) fn into_u32(self) -> u32 {
self.value
}
}

/// The flags used to change the behavior of a contract call.
#[must_use]
#[derive(Copy, Clone, Debug, Default)]
pub struct CallFlags {
forward_input: bool,
clone_input: bool,
tail_call: bool,
allow_reentry: bool,
}

impl CallFlags {
/// Forwards the input for the current function to the callee.
///
/// # Note
///
/// A forwarding call will consume the current contracts input. Any attempt to
/// access the input after this call returns (e.g. by trying another forwarding call)
/// will lead to a contract revert.
/// Consider using [`Self::set_clone_input`] in order to preserve the input.
pub const fn set_forward_input(mut self, forward_input: bool) -> Self {
self.forward_input = forward_input;
self
}

/// Identical to [`Self::set_forward_input`] but without consuming the input.
///
/// This adds some additional weight costs to the call.
///
/// # Note
///
/// This implies [`Self::set_forward_input`] and takes precedence when both are set.
pub const fn set_clone_input(mut self, clone_input: bool) -> Self {
self.clone_input = clone_input;
self
}

/// Do not return from the call but rather return the result of the callee to the
/// callers caller.
///
/// # Note
///
/// This makes the current contract completely transparent to its caller by replacing
/// this contracts potential output with the callee ones. Any code after the contract
/// calls has been invoked can be safely considered unreachable.
pub const fn set_tail_call(mut self, tail_call: bool) -> Self {
self.tail_call = tail_call;
self
}

/// Allow the callee to reenter into the current contract.
///
/// Without this flag any reentrancy into the current contract that originates from
/// the callee (or any of its callees) is denied. This includes the first callee:
/// You cannot call into yourself with this flag set.
pub const fn set_allow_reentry(mut self, allow_reentry: bool) -> Self {
self.allow_reentry = allow_reentry;
self
}

/// Returns the underlying `u32` representation of the call flags.
///
/// This value is used to forward the call flag information to the
/// `contracts` pallet.
pub(crate) const fn into_u32(self) -> u32 {
self.forward_input as u32
| ((self.clone_input as u32) << 1)
| ((self.tail_call as u32) << 2)
| ((self.allow_reentry as u32) << 3)
}

/// Returns `true` if input forwarding is set.
///
/// # Note
///
/// See [`Self::set_forward_input`] for more information.
pub const fn forward_input(&self) -> bool {
self.forward_input
}

/// Returns `true` if input cloning is set.
///
/// # Note
///
/// See [`Self::set_clone_input`] for more information.
pub const fn clone_input(&self) -> bool {
self.clone_input
}

/// Returns `true` if the tail call property is set.
///
/// # Note
///
/// See [`Self::set_tail_call`] for more information.
pub const fn tail_call(&self) -> bool {
self.tail_call
}

/// Returns `true` if call reentry is allowed.
///
/// # Note
///
/// See [`Self::set_allow_reentry`] for more information.
pub const fn allow_reentry(&self) -> bool {
self.allow_reentry
}
}
pub use pallet_contracts_uapi::ReturnFlags;

/// Environmental contract functionality that does not require `Environment`.
pub trait EnvBackend {
Expand Down
Loading
Loading