Skip to content

Commit

Permalink
feat(devnet): add pallet-revive
Browse files Browse the repository at this point in the history
  • Loading branch information
peterwht committed Oct 2, 2024
1 parent 5021c2f commit ef6f3a4
Show file tree
Hide file tree
Showing 115 changed files with 27,896 additions and 30 deletions.
294 changes: 264 additions & 30 deletions Cargo.lock

Large diffs are not rendered by default.

26 changes: 26 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -194,3 +194,29 @@ ismp-parachain-runtime-api = { git = "https://github.com/r0gue-io/ismp", branch
pallet-ismp = { git = "https://github.com/r0gue-io/ismp", branch = "polkadot-v1.14.0", default-features = false }
pallet-ismp-rpc = { git = "https://github.com/r0gue-io/ismp", branch = "polkadot-v1.14.0", default-features = false }
pallet-ismp-runtime-api = { git = "https://github.com/r0gue-io/ismp", branch = "polkadot-v1.14.0", default-features = false }


# revive dependencies
anyhow = { version = "1.0.81" }
array-bytes = { version = "6.2.2", default-features = false }
assert_matches = { version = "1.5.0" }
bitflags = { version = "1.3.2" }
environmental = { version = "1.1.4", default-features = false }
pallet-revive = { path = "pallets/revive", default-features = false }
pallet-revive-fixtures = { path = "pallets/revive/fixtures", default-features = false }
pallet-revive-mock-network = { default-features = false, path = "pallets/revive/mock-network" }
pallet-revive-proc-macro = { path = "pallets/revive/proc-macro", default-features = false }
pallet-revive-uapi = { path = "pallets/revive/uapi", default-features = false }
parity-wasm = { version = "0.45.0" }
paste = { version = "1.0.14", default-features = false }
pretty_assertions = { version = "1.3.0" }
proc-macro2 = { version = "1.0.64" }
quote = { version = "1.0.33" }
rlp = { version = "0.5.2", default-features = false }
sp-tracing = { version = "16.0.0", default-features = false }
syn = { version = "2.0.53" }
tempfile = { version = "3.8.1" }
toml = { version = "0.8.8" }
wat = { version = "1.0.0" }

[workspace.lints.rust]
122 changes: 122 additions & 0 deletions pallets/revive/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
[package]
authors.workspace = true
description = "FRAME pallet for PolkaVM contracts."
edition.workspace = true
homepage.workspace = true
include = [ "CHANGELOG.md", "README.md", "build.rs", "src/**/*" ]
license = "Apache-2.0"
name = "pallet-revive"
readme = "README.md"
repository.workspace = true
version = "0.1.0"

[lints]
workspace = true

[package.metadata.docs.rs]
targets = [ "x86_64-unknown-linux-gnu" ]

[dependencies]
bitflags = { workspace = true }
codec = { features = [
"derive",
"max-encoded-len",
], workspace = true }
environmental = { workspace = true }
impl-trait-for-tuples = { workspace = true }
log = { workspace = true }
paste = { workspace = true }
polkavm = { version = "0.11.0", default-features = false }
rlp = { workspace = true }
scale-info = { features = [ "derive" ], workspace = true }
serde = { optional = true, features = [ "derive" ], workspace = true, default-features = true }

# Polkadot SDK Dependencies
frame-benchmarking = { optional = true, workspace = true }
frame-support = { workspace = true }
frame-system = { workspace = true }
pallet-balances = { optional = true, workspace = true }
pallet-revive-fixtures = { workspace = true, default-features = false }
pallet-revive-proc-macro = { workspace = true, default-features = true }
pallet-revive-uapi = { workspace = true, default-features = true }
sp-api = { workspace = true }
sp-core = { workspace = true }
sp-io = { workspace = true }
sp-runtime = { workspace = true }
sp-std = { workspace = true }
xcm = { workspace = true }
xcm-builder = { workspace = true }

[dev-dependencies]
array-bytes = { workspace = true, default-features = true }
assert_matches = { workspace = true }
pallet-revive-fixtures = { workspace = true, default-features = true }
pretty_assertions = { workspace = true }
wat = { workspace = true }

# Polkadot SDK Dependencies
pallet-assets = { workspace = true, default-features = true }
pallet-balances = { workspace = true, default-features = true }
pallet-message-queue = { workspace = true, default-features = true }
pallet-proxy = { workspace = true, default-features = true }
pallet-timestamp = { workspace = true, default-features = true }
pallet-utility = { workspace = true, default-features = true }
sp-keystore = { workspace = true, default-features = true }
sp-tracing = { workspace = true, default-features = true }
xcm-builder = { workspace = true, default-features = true }

[features]
default = [ "std" ]
# enabling this feature will require having a riscv toolchain installed
# if no tests are ran and runtime benchmarks will not work
# apart from this the pallet will stay functional
riscv = [ "pallet-revive-fixtures/riscv" ]
runtime-benchmarks = [
"frame-benchmarking/runtime-benchmarks",
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
"pallet-assets/runtime-benchmarks",
"pallet-balances/runtime-benchmarks",
"pallet-message-queue/runtime-benchmarks",
"pallet-proxy/runtime-benchmarks",
"pallet-timestamp/runtime-benchmarks",
"pallet-utility/runtime-benchmarks",
"sp-runtime/runtime-benchmarks",
"xcm-builder/runtime-benchmarks",
]
std = [
"codec/std",
"environmental/std",
"frame-benchmarking?/std",
"frame-support/std",
"frame-system/std",
"log/std",
"pallet-balances?/std",
"pallet-proxy/std",
"pallet-revive-fixtures/std",
"pallet-timestamp/std",
"pallet-utility/std",
"polkavm/std",
"rlp/std",
"scale-info/std",
"serde",
"sp-api/std",
"sp-core/std",
"sp-io/std",
"sp-keystore/std",
"sp-runtime/std",
"sp-std/std",
"xcm-builder/std",
"xcm/std",
]
try-runtime = [
"frame-support/try-runtime",
"frame-system/try-runtime",
"pallet-assets/try-runtime",
"pallet-balances/try-runtime",
"pallet-message-queue/try-runtime",
"pallet-proxy/try-runtime",
"pallet-timestamp/try-runtime",
"pallet-utility/try-runtime",
"sp-runtime/try-runtime",
]
104 changes: 104 additions & 0 deletions pallets/revive/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
# Revive Pallet

This is an **experimental** module that provides functionality for the runtime to deploy and execute PolkaVM
smart-contracts. It is a heavily modified `pallet_contracts` fork.

## Overview

This module extends accounts based on the [`frame_support::traits::fungible`] traits to have smart-contract
functionality. It can be used with other modules that implement accounts based on [`frame_support::traits::fungible`].
These "smart-contract accounts" have the ability to instantiate smart-contracts and make calls to other contract and
non-contract accounts.

The smart-contract code is stored once, and later retrievable via its `code_hash`. This means that multiple
smart-contracts can be instantiated from the same `code`, without replicating the code each time.

When a smart-contract is called, its associated code is retrieved via the code hash and gets executed. This call can
alter the storage entries of the smart-contract account, instantiate new smart-contracts, or call other smart-contracts.

Finally, when an account is reaped, its associated code and storage of the smart-contract account will also be deleted.

### Weight

Senders must specify a [`Weight`](https://paritytech.github.io/substrate/master/sp_weights/struct.Weight.html) limit
with every call, as all instructions invoked by the smart-contract require weight. Unused weight is refunded after the
call, regardless of the execution outcome.

If the weight limit is reached, then all calls and state changes (including balance transfers) are only reverted at the
current call's contract level. For example, if contract A calls B and B runs out of weight mid-call, then all of B's
calls are reverted. Assuming correct error handling by contract A, A's other calls and state changes still persist.

One `ref_time` `Weight` is defined as one picosecond of execution time on the runtime's reference machine.

### Revert Behaviour

Contract call failures are not cascading. When failures occur in a sub-call, they do not "bubble up", and the call will
only revert at the specific contract level. For example, if contract A calls contract B, and B fails, A can decide how
to handle that failure, either proceeding or reverting A's changes.

## Interface

### Dispatchable functions

Those are documented in the [reference
documentation](https://paritytech.github.io/substrate/master/pallet_revive/index.html#dispatchable-functions).

## Usage

This module executes PolkaVM smart contracts. These can potentially be written in any language that compiles to
RISC-V. For now, the only officially supported languages are Solidity (via [`revive`](https://github.com/xermicus/revive))
and Rust (check the `fixtures` directory for Rust examples).

## Debugging

Contracts can emit messages to the client when called as RPC through the
[`debug_message`](https://paritytech.github.io/substrate/master/pallet_revive/trait.SyscallDocs.html#tymethod.debug_message)
API.

Those messages are gathered into an internal buffer and sent to the RPC client. It is up to the individual client if
and how those messages are presented to the user.

This buffer is also printed as a debug message. In order to see these messages on the node console the log level for the
`runtime::revive` target needs to be raised to at least the `debug` level. However, those messages are easy to
overlook because of the noise generated by block production. A good starting point for observing them on the console is
using this command line in the root directory of the Substrate repository:

```bash
cargo run --release -- --dev -lerror,runtime::revive=debug
```

This raises the log level of `runtime::revive` to `debug` and all other targets to `error` in order to prevent them
from spamming the console.

`--dev`: Use a dev chain spec `--tmp`: Use temporary storage for chain data (the chain state is deleted on exit)

## Host function tracing

For contract authors, it can be a helpful debugging tool to see which host functions are called, with which arguments,
and what the result was.

In order to see these messages on the node console, the log level for the `runtime::revive::strace` target needs to
be raised to the `trace` level.

Example:

```bash
cargo run --release -- --dev -lerror,runtime::revive::strace=trace,runtime::revive=debug
```

## Unstable Interfaces

Driven by the desire to have an iterative approach in developing new contract interfaces this pallet contains the
concept of an unstable interface. Akin to the rust nightly compiler it allows us to add new interfaces but mark them as
unstable so that contract languages can experiment with them and give feedback before we stabilize those.

In order to access interfaces which don't have a stable `#[api_version(x)]` in [`runtime.rs`](src/wasm/runtime.rs)
one need to set `pallet_revive::Config::UnsafeUnstableInterface` to `ConstU32<true>`.
**It should be obvious that any production runtime should never be compiled with this feature: In addition to be
subject to change or removal those interfaces might not have proper weights associated with them and are therefore
considered unsafe**.

New interfaces are generally added as unstable and might go through several iterations before they are promoted to a
stable interface.

License: Apache-2.0
36 changes: 36 additions & 0 deletions pallets/revive/fixtures/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
[package]
authors.workspace = true
description = "Fixtures for testing and benchmarking"
edition.workspace = true
license.workspace = true
name = "pallet-revive-fixtures"
publish = true
version = "0.1.0"

[lints]
workspace = true

[dependencies]
anyhow = { workspace = true, default-features = true, optional = true }
frame-system = { workspace = true, default-features = true, optional = true }
log = { workspace = true }
sp-core = { workspace = true, default-features = true, optional = true }
sp-io = { workspace = true, default-features = true, optional = true }
sp-runtime = { workspace = true, default-features = true, optional = true }

[build-dependencies]
anyhow = { workspace = true, default-features = true }
parity-wasm = { workspace = true }
polkavm-linker = { version = "0.11.0" }
tempfile = { workspace = true }
toml = { workspace = true }

[features]
default = [ "std" ]
# only if the feature is set we are building the test fixtures
# this is because it requires a custom toolchain supporting polkavm
# we will remove this once there is an upstream toolchain
# note: frame-system is included to make formatting happy
riscv = [ "frame-system/default" ]
# only when std is enabled all fixtures are available
std = [ "anyhow", "frame-system", "log/std", "sp-core", "sp-io", "sp-runtime" ]
Loading

0 comments on commit ef6f3a4

Please sign in to comment.