1
1
# ink!ternals
2
2
3
- todo: rework this doc
4
-
5
3
This document describes the architecture of ink!. The information
6
4
here targets those who want to understand or modify the inner
7
5
workings of this project.
8
6
9
- In general we treat documentation as a first-class citizen.
7
+ In general, we treat documentation as a first-class citizen.
10
8
All crates mentioned below should be documented really well.
11
9
You can find the crate documentation on docs.rs or for our
12
10
` master ` branch under GitHub pages. So for ` ink ` e.g.:
@@ -30,7 +28,7 @@ ink! is composed of a number of crates that are all found in the
30
28
This includes getting and setting a smart contracts storage, as well
31
29
as the mentioned environmental functions.
32
30
* [ ` metadata ` ] ( https://github.com/use-ink/ink/tree/master/crates/metadata ) :
33
- Describes the contract in a platform agnostic way, i.e. its interface
31
+ Describes the contract in a platform- agnostic way, i.e. its interface
34
32
and the types, its storage layout, etc.
35
33
* [ ` prelude ` ] ( https://github.com/use-ink/ink/tree/master/crates/prelude ) :
36
34
Provides an interface to typical standard library types and
@@ -57,12 +55,19 @@ a `no_std` environment.
57
55
Exceptions are ` metadata ` and ` engine ` , which cover use-cases that
58
56
are only relevant off-chain.
59
57
60
- ink! contracts are compiled for a WebAssembly (Wasm) target architecture,
61
- i.e. they are executed in a Wasm sandbox execution environment on the
62
- blockchain itself ‒ hence a ` no_std ` environment.
63
- More specifically they are executed by the [ ` pallet-revive ` ] ( https://github.com/paritytech/substrate/tree/master/frame/contracts ) ,
64
- a module of the Substrate blockchain framework. This module takes ink!
65
- smart contracts and runs them in a sandbox environment.
58
+ ink! contracts are compiled to RISC-V bytecode for
59
+ [ the PolkaVM interpreter] ( https://github.com/paritytech/polkavm ) .
60
+ This is how ink! smart contracts are executed on a blockchain:
61
+ they are uploaded to a blockchain that runs PolkaVM, PolkaVM then
62
+ interprets them.
63
+ As contracts are executed in a sandbox execution environment on the
64
+ blockchain itself we compile them to a ` no_std ` environment.
65
+ More specifically they are executed by the [ ` pallet-revive ` ] ( https://github.com/paritytech/substrate/tree/master/frame/revive ) ,
66
+ a module of the Polkadot SDK blockchain framework. This module takes ink!
67
+ smart contracts and runs them in a PolkaVM sandbox environment.
68
+ It also provides an API to smart contracts for anything a smart contract
69
+ needs: storing + retrieving data, calling other contracts, sending value,
70
+ fetching the block number, ….
66
71
67
72
## Overview
68
73
@@ -72,24 +77,49 @@ The above diagram shows the main components of the ink! language
72
77
and how they interact. This pipeline is run once you execute
73
78
` cargo build ` on an ink! smart contract.
74
79
75
- The central delegating crate for the ink! eDSL is ` ink ` .
80
+ The central umbrella crate for the ink! eDSL is ` ink ` .
76
81
77
82
In the ` crates/ink/ ` folder you'll find three separate
78
83
crates on which ` ink ` relies heavily:
79
84
80
85
* ` ink_macro ` : The procedural macros, they take code annotated with e.g.
81
- ` [ink::contract] ` and forwards it to ` ink_ir ` .
82
- * ` ink_ir ` : Defines everything the procedural macro needs in order to
83
- parse, analyze and generate code for ink! smart contracts.
86
+ ` [ink::contract] ` and forward it to ` ink_ir ` .
87
+ * ` ink_ir ` : The ink! Intermediate Representation (IR). Defines everything
88
+ the procedural macro needs in order to parse, analyze and generate code
89
+ for ink! smart contracts.
84
90
* ` ink_codegen ` : Generates Rust code from the ink! IR.
85
91
92
+ The ` cargo-expand ` tool can be used to display the Rust source code that
93
+ ` ink_codegen ` generates for an ink! contract:
94
+
95
+ ``` ignore
96
+ cd ink/integration-tests/public/flipper/
97
+ cargo expand --no-default-features --target riscv64gc-unknown-none-elf
98
+ ```
99
+
100
+ Ideally we'd use the target JSON file from PolkaVM for the ` --target ` ,
101
+ but [ ` cargo-expand ` ] ( https://crates.io/crates/cargo-expand ) doesn't
102
+ support JSON files for this parameter at the time of writing.
103
+
86
104
## Building ink! contracts
87
105
88
106
While you can build an ink! smart contract with just ` cargo build ` , we
89
107
recommend using our build tool [ ` cargo-contract ` ] ( https://github.com/use-ink/cargo-contract ) .
90
- It automatically compiles for the correct WebAssembly target
108
+ It automatically compiles for the correct PolkaVM target
91
109
architecture and uses an optimal set of compiler flags.
92
110
111
+ Ann approximation of the build command it will execute is:
112
+
113
+ ``` bash
114
+ cd ink/integration-tests/public/flipper/
115
+ cargo +nightly build
116
+ --no-default-features
117
+ --target ~ /polkavm/crates/polkavm-linker/riscv64emac-unknown-none-polkavm.json
118
+ -Zbuild-std=" core,alloc"
119
+ ```
120
+
121
+ You can also ` build ` or ` check ` a contract with this command.
122
+
93
123
## Allocator
94
124
95
125
ink! smart contracts use a very simple bump allocator for dynamic
@@ -101,7 +131,9 @@ This was done with the intention of reducing its complexity, which
101
131
would have resulted in higher costs for the user (due to increased
102
132
gas costs) and a lower transaction throughput. Freeing memory is
103
133
irrelevant for our use-case anyway, as the entire memory instance
104
- is set up fresh for each individual contract call anyway.
134
+ is set up fresh for each individual contract call.
135
+
136
+ _ todo reviewed until here, below not yet_
105
137
106
138
## Unstable Rust features in ink!
107
139
@@ -135,7 +167,7 @@ This `pallet-revive` is the smart contracts module of
135
167
136
168
The relationship is as depicted in this diagram:
137
169
138
- <img src =" ./.images/pallet-contracts .png " alt =" pallet-revive Interaction " width =" 800 " />
170
+ <img src =" ./.images/pallet-revive .png " alt =" pallet-revive Architecture " width =" 800 " />
139
171
140
172
### Communication with the pallet
141
173
ink! uses a static buffer for interacting with ` pallet-revive ` , i.e.
0 commit comments