Skip to content

Commit

Permalink
feat(zint): switch to full evm
Browse files Browse the repository at this point in the history
  • Loading branch information
clearloop committed Dec 18, 2023
1 parent e25c084 commit 55e30ac
Show file tree
Hide file tree
Showing 13 changed files with 141 additions and 185 deletions.
3 changes: 0 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ etc = "0.1.16"
ethers = "2.0.11"
hex = "0.4.3"
indexmap = "2.1.0"
once_cell = "1.19.0"
parking_lot = "0.12.1"
paste = "1.0.14"
postcard = { version = "1.0.8", default-features = false }
proc-macro2 = "1.0.70"
Expand All @@ -44,7 +42,6 @@ serde_json = "1.0.108"
sha3 = "0.10.8"
smallvec = "1.11.2"
syn = { version = "2.0.41", features = [ "full" ] }
target-lexicon = "0.12.12"
thiserror = "1.0.51"
tokio = "1.35.0"
toml = "0.8.8"
Expand Down
3 changes: 3 additions & 0 deletions examples/constructor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ mod tests {
.constructor(true)
.compile()?;

// let info = contract.execute([])?;
// println!("{:?}", info);

let abi: Abi = Abi::load(&*contract.json_abi()?.as_bytes())
.map_err(|e| anyhow::anyhow!("Failed to load abi {e}"))?;
let factory = api.factory(abi, contract.bytecode)?;
Expand Down
5 changes: 1 addition & 4 deletions examples/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ fn main() {}

#[test]
fn selector() -> anyhow::Result<()> {
use zint::{Bytes32, Contract, InstructionResult, U256};
use zint::{Bytes32, Contract, U256};

let mut contract = Contract::search("storage")?.compile()?;

Expand All @@ -49,13 +49,11 @@ fn selector() -> anyhow::Result<()> {
let value: i32 = 42;
let info = contract.execute(&[b"set(int32)".to_vec(), value.to_bytes32().to_vec()])?;
assert!(info.ret.is_empty());
assert_eq!(info.instr, InstructionResult::Return);
assert_eq!(info.storage.get(&U256::from(key)), Some(&U256::from(value)));
}

{
let info = contract.execute(&["get()"])?;
assert_eq!(info.instr, InstructionResult::Return);
assert_eq!(info.ret, 0.to_bytes32());
}

Expand All @@ -64,7 +62,6 @@ fn selector() -> anyhow::Result<()> {
let value = 42;
let info =
contract.execute(&[b"set_and_get(int32)".to_vec(), value.to_bytes32().to_vec()])?;
assert_eq!(info.instr, InstructionResult::Return);
assert_eq!(info.ret, value.to_bytes32());
assert_eq!(info.storage.get(&U256::from(key)), Some(&U256::from(value)));
}
Expand Down
8 changes: 5 additions & 3 deletions tests/br_if.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! br_if tests for the zink compiler.
use anyhow::Result;
use filetests::Test;
use zint::{Contract, InstructionResult};
use zint::{Contract, Halt, OutOfGasError};

#[test]
fn as_block_last() -> Result<()> {
Expand All @@ -10,11 +10,13 @@ fn as_block_last() -> Result<()> {
.compile()?;

let info = contract.execute(&[0])?;
assert_eq!(info.instr, InstructionResult::Return);
assert!(info.ret.is_empty());

let info = contract.execute(&[42])?;
assert_eq!(info.instr, InstructionResult::OutOfGas);
assert_eq!(
info.halt,
Some(Halt::OutOfGas(OutOfGasError::BasicOutOfGas))
);
assert!(info.ret.is_empty());

Ok(())
Expand Down
3 changes: 1 addition & 2 deletions tests/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use anyhow::Result;
use filetests::Test;
use zint::{Bytes32, Contract, InstructionResult};
use zint::{Bytes32, Contract};

#[test]
fn dummy() -> Result<()> {
Expand All @@ -12,7 +12,6 @@ fn dummy() -> Result<()> {
.compile()?;
let info = contract.execute::<()>([])?;

assert_eq!(info.instr, InstructionResult::Return);
assert!(info.ret.is_empty());
Ok(())
}
Expand Down
4 changes: 1 addition & 3 deletions tests/if.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! if-else tests for the zink compiler.
use anyhow::Result;
use filetests::Test;
use zint::{Bytes32, Contract, InstructionResult};
use zint::{Bytes32, Contract};

#[test]
fn if_then() -> Result<()> {
Expand Down Expand Up @@ -32,12 +32,10 @@ fn singular() -> Result<()> {
//
// Enter if block if 1
let info = contract.execute(&[1])?;
assert_eq!(info.instr, InstructionResult::Return);
assert_eq!(info.ret, 7.to_bytes32());

// test else
let info = contract.execute(&[0])?;
assert_eq!(info.instr, InstructionResult::Return);
assert_eq!(info.ret, 8.to_bytes32());

Ok(())
Expand Down
8 changes: 5 additions & 3 deletions tests/loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use anyhow::Result;
use filetests::Test;
use zint::{Bytes32, Contract, InstructionResult};
use zint::{Bytes32, Contract, Halt, OutOfGasError};

#[test]
fn singular() -> Result<()> {
Expand All @@ -22,10 +22,12 @@ fn as_br_if() -> Result<()> {
.without_dispatcher()
.compile()?;
let info = contract.execute([0])?;
assert_eq!(info.instr, InstructionResult::OutOfGas);
assert_eq!(
info.halt,
Some(Halt::OutOfGas(OutOfGasError::BasicOutOfGas))
);

let info = contract.execute([1])?;
assert_eq!(info.instr, InstructionResult::Return);
assert_eq!(info.ret, 7.to_bytes32());

Ok(())
Expand Down
9 changes: 3 additions & 6 deletions tests/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,18 @@

use anyhow::Result;
use filetests::Test;
use zint::{Bytes32, Contract, InstructionResult, U256};
use zint::{Bytes32, Contract, U256};

#[test]
fn store() -> Result<()> {
let mut contract = Contract::new(Test::STORAGE_STORE)
.without_dispatcher()
.compile()?;

let key = 0;
let value = 42;
let key = 0u64;
let value = 42u64;
let info = contract.execute([value])?;
assert!(info.ret.is_empty());
assert_eq!(info.instr, InstructionResult::Return);
assert_eq!(info.storage.get(&U256::from(key)), Some(&U256::from(value)));

Ok(())
Expand All @@ -29,7 +28,6 @@ fn load() -> Result<()> {

let value = 42;
let info = contract.execute([value])?;
assert_eq!(info.instr, InstructionResult::Return);
assert_eq!(info.ret, value.to_bytes32());

Ok(())
Expand All @@ -43,7 +41,6 @@ fn basic() -> Result<()> {

let value = 42;
let info = contract.execute([value])?;
assert_eq!(info.instr, InstructionResult::Return);
assert_eq!(info.ret, 42.to_bytes32());

Ok(())
Expand Down
4 changes: 2 additions & 2 deletions zint/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Contract Instance
use crate::{interp::Interp, Bytes32, Info};
use crate::{Bytes32, Info, EVM};
use anyhow::{anyhow, Result};
use serde::Deserialize;
use std::{fs, path::PathBuf};
Expand Down Expand Up @@ -152,6 +152,6 @@ impl Contract {
calldata.extend_from_slice(&input.to_bytes32());
}

Ok(Interp::run(&self.bytecode, &calldata))
EVM::interp(&self.bytecode, &calldata)
}
}
Loading

0 comments on commit 55e30ac

Please sign in to comment.