Skip to content

Commit

Permalink
feat(example): tests for properties
Browse files Browse the repository at this point in the history
  • Loading branch information
poltao committed Dec 8, 2024
1 parent 94f2b2e commit e615e76
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 26 deletions.
62 changes: 38 additions & 24 deletions examples/properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,38 +6,52 @@ extern crate zink;
use zink::primitives::{properties, Bytes32};

#[zink::external]
pub fn blockhash(block_number: u64) -> Bytes32 {
properties::blockhash(block_number)
pub fn number() -> u64 {
properties::number()
}

#[zink::external]
pub fn number() -> u64 {
properties::number()
pub fn blockhash(number: u64) -> Bytes32 {
properties::blockhash(number)
}

#[cfg(not(target_arch = "wasm32"))]
fn main() {}

#[test]
fn test_block_properties() -> anyhow::Result<()> {
#[cfg(test)]
mod tests {
use zint::{Bytes32, Contract, EVM};

let mut evm = EVM::default().commit(true);
let contract = Contract::search("properties")?.compile()?;
let raw_info = evm.deploy(&contract.bytecode()?)?;

let info = evm
.calldata(&contract.encode(&[b"number()".to_vec()])?)
.call(raw_info.address)?;
assert_eq!(info.ret, 0u64.to_bytes32(), "{info:?}");

let info = evm
.calldata(&contract.encode(&[
b"blockhash(uint64)".to_vec(),
599423545u64.to_bytes32().to_vec(),
])?)
.call(raw_info.address)?;
assert_eq!(info.ret, 0u64.to_bytes32(), "{info:?}");

Ok(())
fn get_block_attr() -> ([u8; 32], [u8; 32]) {
let mut block_number = 599423545u64.to_bytes32();
block_number.reverse();
let hash_bytes =
hex::decode("29045A592007D0C246EF02C2223570DA9522D0CF0F73282C79A1BC8F0BB2C238")
.unwrap();
let mut block_hash = [0; 32];
block_hash.copy_from_slice(&hash_bytes);
(block_number, block_hash)
}

#[test]
fn test_block_properties() -> anyhow::Result<()> {
let (block_number, block_hash) = get_block_attr();
let mut evm = EVM::default()
.block_number(block_number)
.block_hash(block_hash)
.commit(false);
let contract = Contract::search("properties")?.compile()?;
let address = evm.deploy(&contract.bytecode()?)?.address;

let info = evm
.calldata(&contract.encode(["number()".as_bytes()])?)
.call(address)?;
assert_eq!(info.ret, block_number, "{info:?}");

let info = evm
.calldata(&contract.encode(["blockhash(uint64)".as_bytes(), &block_number])?)
.call(address)?;
assert_eq!(info.ret, 0u64.to_bytes32(), "{info:?}");
Ok(())
}
}
2 changes: 1 addition & 1 deletion zink/src/primitives/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ impl Address {
Address(Bytes20::empty())
}

/// Returns empty address
/// Returns the caller address
#[inline(always)]
pub fn caller() -> Self {
unsafe { ffi::evm::caller() }
Expand Down
6 changes: 6 additions & 0 deletions zink/src/storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ impl StorageValue for u32 {
}
}

impl StorageValue for u64 {
fn sload() -> Self {
unsafe { ffi::asm::sload_u64() }
}
}

impl TransientStorageValue for i32 {
fn tload() -> Self {
unsafe { ffi::asm::tload_i32() }
Expand Down
27 changes: 26 additions & 1 deletion zink/zint/src/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ pub struct EVM<'e> {
inner: Revm<'e, (), InMemoryDB>,
/// Caller for the execution
pub caller: [u8; 20],
/// The block’s number
pub block_number: [u8; 32],
/// The block's hash
pub block_hash: [u8; 32],
/// If commit changes
commit: bool,
}
Expand All @@ -38,6 +42,8 @@ impl<'e> Default for EVM<'e> {
Self {
inner: evm,
caller: [0; 20],
block_number: [0; 32],
block_hash: [0; 32],
commit: false,
}
}
Expand Down Expand Up @@ -72,13 +78,32 @@ impl EVM<'_> {
self
}

/// Set block number
pub fn block_number(mut self, number: [u8; 32]) -> Self {
self.block_number = number;
self
}

/// Set block hash
pub fn block_hash(mut self, hash: [u8; 32]) -> Self {
self.block_hash = hash;
self
}

/// Send transaction to the provided address.
pub fn call(&mut self, to: [u8; 20]) -> Result<Info> {
let to = TransactTo::Call(to.into());
self.inner.tx_mut().gas_limit = GAS_LIMIT;
self.inner.tx_mut().transact_to = to;
self.inner.tx_mut().caller = self.caller.into();

let block_number = U256::from_be_bytes(self.block_number);
self.inner.block_mut().number = block_number;
self.inner
.db_mut()
.block_hashes
.insert(block_number, self.block_hash.into());

if self.commit {
self.inner.transact_commit()?.try_into()
} else {
Expand Down Expand Up @@ -147,7 +172,7 @@ impl TryFrom<ExecutionResult> for Info {
gas: result.gas_used(),
..Default::default()
};

println!("{:?}", result);
match result {
ExecutionResult::Success {
logs,
Expand Down

0 comments on commit e615e76

Please sign in to comment.