Skip to content

Commit f03cab8

Browse files
authored
feat(zink): introduce storage mapping (#233)
* feat(zink): introduce storage registry * docs(storage): design of storage * feat(zink): introduce mapping interface * feat(zink): expand module storage * feat(zink): mainly use trait for kv storage * feat(storage): introduce mapping interface * refactor(zink): declare storage on exposed struct * feat(zink): proc-macro for mapping storage * feat(zink): add tests of mapping * feat(zink): mapping tests in bytecode level * feat(zink): complete the test of mappings * feat(zink): clean unused work * feat(zink): bump version to 0.1.11
1 parent 9043c51 commit f03cab8

File tree

27 files changed

+496
-148
lines changed

27 files changed

+496
-148
lines changed

Cargo.lock

Lines changed: 12 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ members = [
1717
resolver = "2"
1818

1919
[workspace.package]
20-
version = "0.1.10"
20+
version = "0.1.11"
2121
authors = ["clearloop"]
2222
edition = "2021"
2323
license = "GPL-3.0-only"
@@ -43,7 +43,7 @@ serde_json = "1.0.113"
4343
smallvec = "1.13.1"
4444
syn = { version = "2.0.48", features = [ "full" ] }
4545
thiserror = "1.0.56"
46-
tiny-keccak = "2.0.2"
46+
tiny-keccak = { version = "2.0.2", features = ["keccak"], default-features = false }
4747
toml = "0.8.9"
4848
tracing = "0.1.40"
4949
tracing-subscriber = "0.3.18"
@@ -52,18 +52,18 @@ wasmparser = "0.121.0"
5252
wat = "1.0.85"
5353

5454
## EVM packages
55-
opcodes = { package = "evm-opcodes", path = "evm/opcodes", version = "=0.0.3", features = [ "data" ] }
55+
opcodes = { package = "evm-opcodes", path = "evm/opcodes", version = "=0.0.4", features = [ "data" ] }
5656
sol-abi = { path = "evm/abi", version = "=0.0.1" }
5757

5858
## Zink packages
59-
elko = { path = "elko", version = "0.1.10" }
60-
filetests = { package = "zinkc-filetests", path = "compiler/filetests", version = "0.1.10" }
61-
zabi = { path = "abi", version = "0.1.10" }
62-
zingen = { path = "codegen", version = "0.1.10" }
63-
zink = { path = ".", version = "0.1.10" }
64-
zink-codegen = { path = "zink/codegen", version = "0.1.10" }
65-
zinkc = { path = "compiler", version = "0.1.10" }
66-
zint = { path = "zint", version = "0.1.10" }
59+
elko = { path = "elko", version = "0.1.11" }
60+
filetests = { package = "zinkc-filetests", path = "compiler/filetests", version = "0.1.11" }
61+
zabi = { path = "abi", version = "0.1.11" }
62+
zingen = { path = "codegen", version = "0.1.11" }
63+
zink = { path = ".", version = "0.1.11" }
64+
zink-codegen = { path = "zink/codegen", version = "0.1.11" }
65+
zinkc = { path = "compiler", version = "0.1.11" }
66+
zint = { path = "zint", version = "0.1.11" }
6767

6868
[workspace.metadata.conta]
6969
packages = [
@@ -102,4 +102,7 @@ zink-codegen.workspace = true
102102
[target.'cfg(not(target_arch = "wasm32"))'.dev-dependencies]
103103
anyhow.workspace = true
104104
filetests.workspace = true
105+
opcodes = { workspace = true, features = ["data"] }
106+
tracing.workspace = true
105107
zint.workspace = true
108+
hex.workspace = true

abi/src/selector.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
#![cfg(feature = "selector")]
33

44
use crate::Abi;
5-
use tiny_keccak::{Hasher, Sha3};
5+
use tiny_keccak::{Hasher, Keccak};
66

77
/// Generate a keccak hash of the input (sha3)
88
pub fn keccak256(input: &[u8]) -> [u8; 32] {
9-
let mut hasher = Sha3::v256();
9+
let mut hasher = Keccak::v256();
1010
let mut output = [0; 32];
1111
hasher.update(input);
1212
hasher.finalize(&mut output);

codegen/src/asm.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,11 @@ impl Assembler {
4646
return Ok(());
4747
}
4848

49-
tracing::trace!(
50-
"increment stack pointer {}({items}) -> {}",
51-
self.sp,
52-
self.sp + items
53-
);
49+
// tracing::trace!(
50+
// "increment stack pointer {}({items}) -> {}",
51+
// self.sp,
52+
// self.sp + items
53+
// );
5454
self.sp += items;
5555

5656
// TODO: fix this limitation: should be 1024. (#127)

codegen/src/wasm/host.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,10 @@ impl TryFrom<(&str, &str)> for HostFunc {
4848
Ok(Self::NoOp)
4949
}
5050
}
51-
("evm", name) => {
52-
Ok(Self::Evm(OpCode::from_str(name).map_err(|_| {
53-
Error::HostFuncNotFound(module.into(), name.into())
54-
})?))
55-
}
51+
("evm", name) => Ok(Self::Evm(OpCode::from_str(name).map_err(|_| {
52+
tracing::error!("Failed to load host function: {:?}", import);
53+
Error::HostFuncNotFound(module.into(), name.into())
54+
})?)),
5655
("zinkc", "emit_abi") => Ok(Self::EmitABI),
5756
_ => {
5857
tracing::warn!("Failed to load host function: {:?}", import);

docs/SUMMARY.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,24 @@
1010
- [Log](./examples/log.md)
1111
- [Select](./examples/select.md)
1212
- [Storage](./examples/storage.md)
13-
- [Command Line Tool](./cli/README.md)
14-
- [elko](./cli/elko.md)
15-
- [zinkc](./cli/zinkc.md)
1613
- [Styles](./styles/README.md)
1714
- [Compiler](./compiler/README.md)
1815
- [Arithmetic](./compiler/arithmetic.md)
1916
- [Calls](./compiler/calls.md)
2017
- [Control Flow](./compiler/control-flow.md)
2118
- [Locals](./compiler/locals.md)
2219
- [Recursion](./compiler/recursion.md)
20+
- [Storage](./compiler/storage.md)
2321
- [Stability](./stability/README.md)
2422
- [v0.1.0](./stability/v0.1.0.md)
2523
- [Security](./security.md)
2624
- [Benchmarks](./benchmarks/README.md)
2725
- [Fibonacci](./benchmarks/fibonacci.md)
2826
- [Log](./benchmarks/log.md)
2927
- [Storage](./benchmarks/storage.md)
28+
- [Command Line Tool](./cli/README.md)
29+
- [elko](./cli/elko.md)
30+
- [zinkc](./cli/zinkc.md)
3031
- [Contributing](./contributing/README.md)
3132
- [Architecture](./contributing/architecture.md)
3233
- [Building](./contributing/building.md)

docs/compiler/storage.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Storage
2+
3+
The storage keys in Zink is slot based, for example, the first detected
4+
storage in compilation will be using `0` as storage key.
5+
6+
```solidity
7+
// Loading storage at 0
8+
PUSH0
9+
SLOAD
10+
11+
// Loading storage at 1
12+
PUSH1 0x01
13+
SLOAD
14+
```
15+
16+
## Key-Value
17+
18+
As mentioned above, all key-value pairs follows using number as storage key, however, the value
19+
will be limited with 32 bytes, dynamic value like string is currently not supported.
20+
21+
## Mapping
22+
23+
Mapping keys are generated via `keccak256(slot, key)`
24+
25+
## Array
26+
27+
Similar to mappings, but the keys will be using `u32` / `u64` for indexing due to the optimization
28+
on the wasm side in the zink compiler, which means, the max size of an array is `max(u64)`.

evm/abi/src/abi.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
use crate::Arg;
44
use core::{convert::Infallible, fmt, str::FromStr};
55

6+
#[cfg(feature = "syn")]
7+
use quote::ToTokens;
8+
69
#[cfg(not(feature = "std"))]
710
use crate::std::{String, ToString, Vec};
811

@@ -28,9 +31,9 @@ impl From<&syn::Signature> for Abi {
2831
.inputs
2932
.iter()
3033
.filter_map(|arg| {
31-
if let syn::FnArg::Typed(syn::PatType { ty, .. }) = arg {
34+
if let syn::FnArg::Typed(syn::PatType { pat, ty, .. }) = arg {
3235
Some(Arg {
33-
name: sig.ident.to_string(),
36+
name: pat.to_token_stream().to_string(),
3437
ty: crate::Param::from(ty),
3538
})
3639
} else {
@@ -41,7 +44,8 @@ impl From<&syn::Signature> for Abi {
4144

4245
let outputs = if let syn::ReturnType::Type(_, ty) = &sig.output {
4346
vec![Arg {
44-
name: sig.ident.to_string(),
47+
// TODO: how to name the output?
48+
name: "output".into(),
4549
ty: crate::Param::from(ty),
4650
}]
4751
} else {

evm/opcodes/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
name = "evm-opcodes"
33
description = "Rust implementation of EVM opcode"
44
documentation = "https://docs.rs/evm-opcodes"
5-
version = "0.0.3"
5+
version = "0.0.4"
66
authors.workspace = true
77
edition.workspace = true
88
homepage.workspace = true

evm/opcodes/src/shanghai.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ opcodes! {
3030
(0x1b, SHL, 3, 2, 1, "Left shift operation", Constantinople, ComparisonBitwiseLogic),
3131
(0x1c, SHR, 3, 2, 1, "Logical right shift operation", Constantinople, ComparisonBitwiseLogic),
3232
(0x1d, SAR, 3, 2, 1, "Arithmetic (signed) right shift operation", Constantinople, ComparisonBitwiseLogic),
33-
(0x20, SHA3, 30, 2, 1, "Compute Keccak-256 hash.", Frontier, StopArithmetic),
33+
(0x20, KECCAK256, 30, 2, 1, "Compute Keccak-256 hash.", Frontier, StopArithmetic),
3434
(0x30, ADDRESS, 2, 0, 1, "Get address of currently executing account.", Frontier, EnvironmentalInformation),
3535
(0x31, BALANCE, 20, 1, 1, "Get balance of the given account.", Frontier, EnvironmentalInformation),
3636
(0x32, ORIGIN, 2, 0, 1, "Get execution origination address.", Frontier, EnvironmentalInformation),

0 commit comments

Comments
 (0)