Skip to content

Commit

Permalink
Merge branch 'main' into Events
Browse files Browse the repository at this point in the history
  • Loading branch information
malik672 authored Dec 5, 2024
2 parents bf349a1 + d154e2f commit faf5e12
Show file tree
Hide file tree
Showing 76 changed files with 1,477 additions and 494 deletions.
26 changes: 25 additions & 1 deletion .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,29 @@ Resolves #
### Changes

- [x] step 1
- [ ] step 2
- [ ] ...

<!--
If you are solving a budget issue, please add your wallet address here.
We will use it to send you the bounty once the PR is merged.
Polygon Address: `0x0000000000000000000000000000000000000000`
-->

<!--
NOTES for testing your changes:
```
# Clippy all packages
cargo cc
# Run all tests
cargo tt
# Build all examples
cargo be
# Run tests for examples
cargo te
```
-->
7 changes: 7 additions & 0 deletions Cargo.lock

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

27 changes: 16 additions & 11 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
[profile]
dev = { panic = "abort"}
dev = { panic = "abort" }
release = { panic = "unwind" }

[workspace]
members = [
"abi",
"codegen",
"compiler",
"compiler/filetests",
"elko",
"evm/opcodes",
"evm/abi",
"zink/abi",
"zink/codegen",
"zint",
"zink/elko",
"zink/zint",
]
resolver = "2"

Expand Down Expand Up @@ -42,9 +42,11 @@ semver = "1.0.21"
serde = { version = "1.0.196", default-features = false }
serde_json = "1.0.113"
smallvec = "1.13.1"
syn = { version = "2.0.77", features = [ "full" ] }
syn = { version = "2.0.77", features = ["full"] }
thiserror = "1.0.56"
tiny-keccak = { version = "2.0.2", features = ["keccak"], default-features = false }
tiny-keccak = { version = "2.0.2", features = [
"keccak",
], default-features = false }
toml = "0.8.9"
tracing = "0.1.40"
tracing-subscriber = "0.3.18"
Expand All @@ -53,18 +55,20 @@ wasmparser = "0.121.0"
wat = "1.0.85"

## EVM packages
opcodes = { package = "evm-opcodes", path = "evm/opcodes", version = "=0.0.4", features = [ "data" ] }
opcodes = { package = "evm-opcodes", path = "evm/opcodes", version = "=0.0.4", features = [
"data",
] }
sol-abi = { path = "evm/abi", version = "=0.0.1" }

## Zink packages
elko = { path = "elko", version = "0.1.11" }
filetests = { package = "zinkc-filetests", path = "compiler/filetests", version = "0.1.11" }
zabi = { path = "abi", version = "0.1.11" }
zingen = { path = "codegen", version = "0.1.11" }
zabi = { path = "zink/abi", version = "0.1.11" }
zingen = { path = "codegen", version = "0.1.11" }
zink = { path = ".", version = "0.1.11" }
zink-codegen = { path = "zink/codegen", version = "0.1.11" }
zinkc = { path = "compiler", version = "0.1.11" }
zint = { path = "zint", version = "0.1.11" }
zint = { path = "zink/zint", version = "0.1.11" }

[workspace.metadata.conta]
packages = [
Expand All @@ -75,7 +79,7 @@ packages = [
"zint",
"zink-codegen",
"zink",
"elko"
"elko",
]

# Zink Programming Language
Expand All @@ -97,6 +101,7 @@ readme = "zink/README.md"
path = "zink/src/lib.rs"

[dependencies]
fmt = "0.1.0"
paste.workspace = true
zink-codegen.workspace = true

Expand Down
98 changes: 38 additions & 60 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,91 +1,69 @@
<img align="right" width="150" height="150" top="100" src = "https://avatars.githubusercontent.com/u/138247979?s=400&u=cbf4b9e9da048899a947f08d92e030806d5bd50b&v=4"/>

# The Zink Project
# The Zink Language

> [!CAUTION]
>
> This project is still under active development, plz DO NOT use it in production.
> This project is still under active development, please DO NOT use it in production.
[![zink][version-badge]][version-link]
[![ci][ci-badge]][ci-link]
[![telegram][telegram-badge]][telegram-group]

[The Zink project][book] mainly provides a singlepass compiler `zinkc` which compiles
WASM to EVM bytecode, the source code of your smart contracts could be any language you like!
Welcome to the Zink Language! [Bounty issues](https://zink-lang.org/budgets) are now available, join the development of Zink by reading the [book](https://zink-lang.org/).

```mermaid
flowchart LR
R{{Rust}} --> W(WebAssembly)
O[...] --> W
W --> Z{Zink Compiler}
Z --> V[(EVM)]
```
```rust
//! ERC20 Example (WIP)
#[zink::contract]
pub struct ERC20;

#[zink::calls]
impl ERC20 {
/// VMs that zink supports
pub fn support() -> [zink::String; 4] {
["EVM", "WASM", "RISC-V", "...OTHER_VMS"]
}
}

Here we highly recommend you to choose `rust` as the language of your smart contracts
which will unlock all of the following features:
#[zink::interface]
impl ERC20 for ERC20 {
fn name() -> zink::String {
"Zink Language".to_string()
}
}
```

- **Safe**: `rustc` is watching you! Furthermore, after compiling your rust code to WASM,
`zinkc` will precompute all of the stack and memory usage in your contracts to ensure they
are safe in EVM bytecode as well!
- **Safe**: `rustc` monitors your code!

- **High Performance**: The optimizations are provided by the three of `rustc`, `wasm-opt`
and `zinkc`, your contracts will have the smallest size with **strong performance** in EVM
bytecode at the end!
- **Efficient**: Efficient EVM bytecode from `rustc`, `wasm-opt`, and `zinkc`.

- **Compatible**: All of the `no_std` libraries in rust are your libraries, you can use your
solidity contracts as part of your zink contracts and your zink contracts as part of your
solidity contracts :)
- **Modular**: Upload and download your contract components via `crates.io`.

- **Easy Debugging**: Developing your smart contracts with only one programming language!
zink will provide everything you need for developing your contracts officially based on the
stable projects in rust like the `foundry` tools.
- **Rusty**: All of the rust tools are available for your contracts!

Run `cargo install zinkup` to install the toolchain!

## Fibonacci Example

| fib(n) | Zink | [email protected] |
| ------ | ---- | --------------- |
| 0 | 110 | 614 |
| 1 | 110 | 614 |
| 2 | 262 | 1322 |
| 3 | 414 | 2030 |
| 4 | 718 | 3446 |
| 5 | 1174 | 5570 |

```rust
/// Calculates the nth fibonacci number using recursion.
#[no_mangle]
pub extern "C" fn recursion(n: usize) -> usize {
if n < 2 {
n
} else {
recursion(n - 1) + recursion(n - 2)
}
}
```

As an example for the benchmark, calculating fibonacci sequence with recursion, missed
vyper because it doesn't support recursion...Zink is 5x fast on this, but it is mainly
caused by our current implementation is not completed yet ( missing logic to adapt to more
situations ), let's stay tuned for `v0.3.0`.
## Testing & Development

## Donation
| Command | Description |
| ---------- | ---------------------- |
| `cargo cc` | Clippy all packages |
| `cargo tt` | Run all tests |
| `cargo be` | Build all examples |
| `cargo te` | Run tests for examples |

After completing the ERC20 implementation, Zink will focus on MEV logic since everything could
be even more compact and realistic from this dark forest.
We're using `cargo-nextest` for testing, the commands above are described in [.cargo/config.toml](.cargo/config.toml).

Zink is now moving forward without any grants or backups, if you like this dreaming project,
please feel free to reach out, would be appreciated for any opportunities ^ ^
## Special Thanks

- ETH: `0xf0306047Fa598fe95502f466aeb49b68dd94365B`
- SOL: `AZGXAerErfwVzJkiSR8moVPZxe1nEhvjdkvxQ7qR6Yst`
- [MegaETH](https://github.com/megaeth-labs) for the funding and trust!
- [revm](https://github.com/bluealloy/revm) for the EVM in rust!

## LICENSE

GPL-3.0-only

[book]: https://docs.zink-lang.org/
[book]: https://zink-lang.org/
[telegram-badge]: https://img.shields.io/endpoint?label=chat&style=flat&url=https%3A%2F%2Fmogyo.ro%2Fquart-apis%2Ftgmembercount%3Fchat_id%3Dzinklang
[telegram-group]: https://t.me/zinklang
[version-badge]: https://img.shields.io/crates/v/zinkc
Expand Down
28 changes: 16 additions & 12 deletions codegen/src/asm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
//! TODO: refactor this module with Result as outputs. (issue-21)
use crate::{Buffer, Error, Result};
use opcodes::{for_each_shanghai_operator, OpCode as _, ShangHai as OpCode};
use opcodes::{for_each_cancun_operator, Cancun as OpCode, OpCode as _};

const MAX_STACK_SIZE: u16 = 1024;

/// Low level assembler implementation for EVM.
#[derive(Default, Clone, Debug)]
Expand All @@ -18,8 +20,8 @@ pub struct Assembler {
gas: u128,
/// Memory pointer for byte offset.
pub mp: usize,
/// Stack pointer, maximum 1024 items.
pub sp: u8,
/// Stack pointer, maximum `MAX_STACK_SIZE` items.
pub sp: u16,
}

impl Assembler {
Expand All @@ -41,7 +43,7 @@ impl Assembler {
}

/// Increment stack pointer
pub fn increment_sp(&mut self, items: u8) -> Result<()> {
pub fn increment_sp(&mut self, items: u16) -> Result<()> {
if items == 0 {
return Ok(());
}
Expand All @@ -51,18 +53,20 @@ impl Assembler {
self.sp,
self.sp + items
);
self.sp += items;
self.sp = self
.sp
.checked_add(items)
.ok_or(Error::StackOverflow(self.sp, items))?;

// TODO: fix this limitation: should be 1024. (#127)
if self.sp > 254 {
return Err(Error::StackOverflow(self.sp));
if self.sp > MAX_STACK_SIZE {
return Err(Error::StackOverflow(self.sp, items));
}

Ok(())
}

/// Decrement stack pointer
pub fn decrement_sp(&mut self, items: u8) -> Result<()> {
pub fn decrement_sp(&mut self, items: u16) -> Result<()> {
if items == 0 {
return Ok(());
}
Expand Down Expand Up @@ -118,10 +122,10 @@ impl Assembler {
/// the stack usages.
pub fn emit_op(&mut self, opcode: OpCode) -> Result<()> {
tracing::trace!("emit opcode: {:?}", opcode);
self.decrement_sp(opcode.stack_in() as u8)?;
self.decrement_sp(opcode.stack_in())?;
self.emit(opcode.into());
self.increment_gas(opcode.gas().into());
self.increment_sp(opcode.stack_out() as u8)?;
self.increment_sp(opcode.stack_out())?;

Ok(())
}
Expand All @@ -141,5 +145,5 @@ macro_rules! impl_opcodes {

/// Basic instruction implementations
impl Assembler {
for_each_shanghai_operator!(impl_opcodes);
for_each_cancun_operator!(impl_opcodes);
}
2 changes: 1 addition & 1 deletion codegen/src/codegen/dispatcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ impl Dispatcher {
self.asm.increment_sp(1)?;

// Prepare the `PC` of the callee function.
self.table.call(self.asm.pc_offset(), func);
self.table.call(self.asm.pc(), func);

if last {
self.asm._swap1()?;
Expand Down
2 changes: 1 addition & 1 deletion codegen/src/codegen/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ impl Function {
/// Finish code generation.
pub fn finish(self, jump_table: &mut JumpTable, pc: u16) -> Result<Buffer> {
let sp = self.masm.sp();
if !self.is_main && self.abi.is_none() && self.masm.sp() != self.ty.results().len() as u8 {
if !self.is_main && self.abi.is_none() && self.masm.sp() != self.ty.results().len() as u16 {
return Err(Error::StackNotBalanced(sp));
}

Expand Down
4 changes: 2 additions & 2 deletions codegen/src/control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ pub struct ControlStackFrame {
result: BlockType,

/// Original stack pointer.
pub original_sp: u8,
pub original_sp: u16,
}

impl ControlStackFrame {
/// Create a new control stack frame.
pub fn new(
ty: ControlStackFrameType,
original_pc_offset: u16,
original_sp: u8,
original_sp: u16,
result: BlockType,
) -> Self {
Self {
Expand Down
Loading

0 comments on commit faf5e12

Please sign in to comment.