-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(codegen): handle backward jumps in jump table (#286)
* feat(codegen): handle backward jumps in jump table * feat(zink): introduce equal for u256 * feat(examples): introduce example br_balance * chore(examples): mark the control flow problems
- Loading branch information
Showing
7 changed files
with
193 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
#![cfg_attr(target_arch = "wasm32", no_std)] | ||
#![cfg_attr(target_arch = "wasm32", no_main)] | ||
|
||
extern crate zink; | ||
use zink::{storage, Storage}; | ||
|
||
#[storage(i32)] | ||
struct Balance; | ||
|
||
#[zink::external] | ||
fn check_and_update(value: i32) -> bool { | ||
let current = Balance::get(); | ||
|
||
// This mimics the ERC20 balance check | ||
if current < value { | ||
zink::revert!("Not enough balance"); | ||
// TODO: #287 | ||
// return false; | ||
} | ||
|
||
Balance::set(current - value); | ||
return true; | ||
} | ||
|
||
// TODO: identify if the problem is caused by control flow of incorrect opcode mapping. (issue #287) | ||
#[test] | ||
fn test_balance_check() -> anyhow::Result<()> { | ||
use zint::{Bytes32, Contract, EVM}; | ||
|
||
let mut evm = EVM::default().commit(true); | ||
let mut contract = Contract::search("br_balance")?.compile()?; | ||
|
||
// Initialize with balance of 42 | ||
let info = evm.deploy( | ||
&contract | ||
.construct( | ||
[( | ||
Balance::STORAGE_KEY.to_bytes32().into(), | ||
vec![42].try_into()?, | ||
)] | ||
.into_iter() | ||
.collect(), | ||
)? | ||
.bytecode()?, | ||
)?; | ||
|
||
// Try to transfer 21 (should succeed) | ||
let info = evm | ||
.calldata(&contract.encode(&[ | ||
b"check_and_update(int32)".to_vec(), | ||
21i32.to_bytes32().to_vec(), | ||
])?) | ||
.call(info.address)?; | ||
assert_eq!(info.ret, true.to_bytes32(), "{info:?}"); | ||
|
||
Ok(()) | ||
} | ||
|
||
#[cfg(not(target_arch = "wasm32"))] | ||
fn main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters