Skip to content

Commit

Permalink
docs(log): introduce example and benchmark (#128)
Browse files Browse the repository at this point in the history
* docs(highlight): support python in mdbook

* chore(zink): bump version to v0.1.3

* docs(log): introduce example

* docs(log): introduce benchmarks

* chore(compiler): remove oudated tests
  • Loading branch information
clearloop authored Oct 2, 2023
1 parent d0a7535 commit 9253c7a
Show file tree
Hide file tree
Showing 10 changed files with 222 additions and 55 deletions.
5 changes: 2 additions & 3 deletions .github/workflows/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: Docs

on:
push:
branches: [ main ]
branches: [main]

jobs:
deploy:
Expand All @@ -18,7 +18,7 @@ jobs:
- name: Setup mdBook
uses: peaceiris/actions-mdbook@v1
with:
mdbook-version: 'latest'
mdbook-version: "latest"

- name: Build the book
run: cd docs && mdbook build
Expand All @@ -38,7 +38,6 @@ jobs:

- name: Deploy
uses: peaceiris/actions-gh-pages@v3
# if: github.ref == 'refs/heads/main'
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: ./docs/book
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.log
**/*target
docs/book
.DS_Store
10 changes: 5 additions & 5 deletions Cargo.lock

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

12 changes: 6 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[workspace.package]
version = "0.1.2"
version = "0.1.3"
authors = ["clearloop"]
edition = "2021"
license = "GPL-3.0-only"
Expand Down Expand Up @@ -42,12 +42,12 @@ wasmparser = "0.107.0"
wat = "1.0.66"

# Local Dependencies.
zinkup = { path = "cli", version = "=0.1.2" }
zinkup = { path = "cli", version = "=0.1.3" }
opcodes = { package = "evm-opcodes", path = "codegen/opcodes", version = "=0.0.3", features = ["data"] }
zingen = { path = "codegen", version = "=0.1.2" }
zinkc = { path = "compiler", version = "=0.1.2" }
zink = { path = "zink", version = "=0.1.2" }
zint = { path = "zint", version = "=0.1.2" }
zingen = { path = "codegen", version = "=0.1.3" }
zinkc = { path = "compiler", version = "=0.1.3" }
zink = { path = "zink", version = "=0.1.3" }
zint = { path = "zint", version = "=0.1.3" }

[profile]
dev = { panic = "abort"}
Expand Down
2 changes: 2 additions & 0 deletions docs/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- [Examples](./examples/README.md)
- [AddTwo](./examples/add-two.md)
- [Fibonacci](./examples/fibonacci.md)
- [Log](./examples/log.md)
- [Select](./examples/select.md)
- [Storage](./examples/storage.md)
- [Command Line Tool](./cli/README.md)
Expand All @@ -24,6 +25,7 @@
- [Security](./security.md)
- [Benchmarks](./benchmarks/README.md)
- [Fibonacci](./benchmarks/fibonacci.md)
- [Log](./benchmarks/log.md)
- [Storage](./benchmarks/storage.md)
- [Contributing](./contributing/README.md)
- [Architecture](./contributing/architecture.md)
Expand Down
78 changes: 78 additions & 0 deletions docs/benchmarks/log.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# Log

Zink only supports static byte array for log arguments for now, see issue
[#129][#129] for more details.

| | Zink | [email protected] | [email protected] |
| ------------ | ---- | ----------- | --------------- |
| Gas Cost | 1031 | 2777 | 2894 |
| Runtime Code | 42 | 632 | 774 |

## `zink`

```rust
//! Addition example.
#![no_std]

// for the panic handler.
#[cfg(not(test))]
extern crate zink;

use zink::Event;

/// A `Ping` event.
///
/// TODO: generate this with proc-macro.
struct Ping;

/// TODO: generate this with proc-macro.
impl Event for Ping {
const NAME: &'static [u8] = b"Ping";
}

#[no_mangle]
pub extern "C" fn log1() {
Ping.log1(b"pong");
}
```

```
63706f6e676350696e6760005260206000a15f5ff3
```

## `vyper`

```python
event Ping:
name: String[4]
topic1: String[4]

@external
def l():
log Ping("Ping", "pong")
```

```
6003361161000c57610127565b5f3560e01c3461012b5763ece866b98118610125577fcf8d08d4ab9d61004e3c20715af5b44c3badc3d3f41ddccbedbef447355ebff460408060c05260046040527f50696e670000000000000000000000000000000000000000000000000000000060605260408160c00181516020830160208301815181525050808252508051806020830101601f825f03163682375050601f19601f8251602001011690509050810190508060e05260046080527f706f6e670000000000000000000000000000000000000000000000000000000060a05260808160c00181516020830160208301815181525050808252508051806020830101601f825f03163682375050601f19601f82516020010116905090508101905060c0a1005b505b5f5ffd5b5f80fda165767970657283000309000b
```

## `solidity`

```sol
// SPDX-License-Identifier: MIT
pragma solidity >=0.7.0 <0.9.0;
contract Test {
event Ping(string name, string topic);
function l() public {
emit Ping("Ping", "pong");
}
}
```

```
608060405234801561001057600080fd5b506004361061002b5760003560e01c8063ece866b914610030575b600080fd5b61003861003a565b005b7fcf8d08d4ab9d61004e3c20715af5b44c3badc3d3f41ddccbedbef447355ebff46040516100679061011a565b60405180910390a1565b600082825260208201905092915050565b7f50696e6700000000000000000000000000000000000000000000000000000000600082015250565b60006100b8600483610071565b91506100c382610082565b602082019050919050565b7f706f6e6700000000000000000000000000000000000000000000000000000000600082015250565b6000610104600483610071565b915061010f826100ce565b602082019050919050565b60006040820190508181036000830152610133816100ab565b90508181036020830152610146816100f7565b905091905056fea26469706673582212201af2a7b1c1d4743d1d089d3eaa1bbd6ecb4186fc95a43320108b18882d7a6dfc64736f6c63430008120033
```

[#129]: https://github.com/zink-lang/zink/issues/129
6 changes: 3 additions & 3 deletions docs/benchmarks/storage.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ selector!

### Gas Cost

| io | Zink | [email protected] | [email protected] |
| --- | ----- | ----------- | --------------- |
| 42 | 22237 | 22345 | 27738 |
| Zink | [email protected] | [email protected] |
| ----- | ----------- | --------------- |
| 22237 | 22345 | 27738 |

The gas costs here are measured by `transaction cost` + `execution cost`,
for example, the transaction of this function in solidity is `24120`, and
Expand Down
16 changes: 10 additions & 6 deletions docs/examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@

This chapter provides various zink examples in rust:

| name | knowledges | description |
|--------------------------|--------------------------------------------|---------------------------------------------------|
| [`add-two`][add-two] | `params` | basic program in zink |
| [`if-else`][if-else] | `params`, `code-section` | program with extra instruction `select` from WASM |
| [`fibonacci`][fibonacci] | `params`, `calls`, `recursion`, `if-block` | recursion implementation |
| name | knowledges | description |
| ------------------------ | -------------------------------- | ------------------------------------------------- |
| [`add-two`][add-two] | `params` | basic program in zink |
| [`fibonacci`][fibonacci] | `calls`, `recursion`, `if-block` | recursion implementation |
| [`log`][log] | `log` | log APIs |
| [`select`][select] | `wasm built-in functions` | program with extra instruction `select` from WASM |
| [`storage`][storage] | `storage` | storage operations |

[add-two]: /examples/add-two.md
[if-else]: /examples/if-else.md
[fibonacci]: /examples/fibonacci.md
[log]: /examples/log.md
[select]: /examples/select.md
[storage]: /examples/storage.md
54 changes: 54 additions & 0 deletions docs/examples/log.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Log

```rust
//! Addition example.
#![no_std]

// for the panic handler.
#[cfg(not(test))]
extern crate zink;

use zink::Event;

/// A `Ping` event.
///
/// TODO: generate this with proc-macro.
struct Ping;

/// TODO: generate this with proc-macro.
impl Event for Ping {
const NAME: &'static [u8] = b"Ping";
}

#[no_mangle]
pub extern "C" fn log1() {
Ping.log1(b"pong");
}
```

The log API of zink is derived by the trait `Event` which provides methods
`log0`, `log1`, `log2`, `log3`, `log4`. We current only supports static
bytes in this API.

```wasm
(module
(type (;0;) (func))
(type (;1;) (func (param i32 i32 i32 i32)))
(import "evm" "log1" (func (;1;) (type 1)))
(import "env" "memory" (memory (;0;) 17))
(func (;1;) (type 0)
i32.const 1048576
i32.const 4
i32.const 1048580
i32.const 4
call 0)
(export "log1" (func 1))
(data (;0;) (i32.const 1048576) "Pingpong"))
```

The static byte array will be compiled to the data section of wasm, `zinkc`
gets it from the data section then process it to the logging interfaces.

```
63706f6e676350696e6760005260206000a15f5ff3
```
93 changes: 61 additions & 32 deletions docs/theme/highlight.js

Large diffs are not rendered by default.

0 comments on commit 9253c7a

Please sign in to comment.