Skip to content

Commit

Permalink
test: update block revm test
Browse files Browse the repository at this point in the history
  • Loading branch information
poltao committed Dec 19, 2024
1 parent f42d6a7 commit 280d6a6
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 36 deletions.
43 changes: 25 additions & 18 deletions examples/properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
extern crate zink;
use zink::primitives::{properties, Bytes32};

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

#[zink::external]
pub fn number() -> u64 {
properties::number()
Expand All @@ -15,11 +20,6 @@ pub fn blockhash(number: u64) -> Bytes32 {
properties::blockhash(number)
}

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

#[zink::external]
pub fn gasleft() -> u64 {
properties::gas()
Expand All @@ -37,42 +37,49 @@ fn main() {}
mod tests {
use zint::{Bytes32, Contract, EVM};

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

fn u64_to_bytes32(value: u64) -> Vec<u8> {
let bytes = value.to_be_bytes();
let mut bytes32 = [0; 32];
bytes32[32 - bytes.len()..].copy_from_slice(&bytes);
bytes32.to_vec()
}

#[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)
.chain_id(1)
.block_number(599423555)
.block_hash(599423545, get_block_hash())
.commit(true);
let contract = Contract::search("properties")?.compile()?;
let info = evm.deploy(&contract.bytecode()?)?;
let address = info.address;

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

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

let info = evm
.calldata(&contract.encode(["chainid()".as_bytes()])?)
.calldata(
&contract.encode(["blockhash(uint64)".as_bytes(), &u64_to_bytes32(599423545)])?,
)
.call(address)?;
assert_eq!(info.ret, 1u64.to_bytes32(), "{info:?}");
assert_eq!(info.ret, get_block_hash(), "{info:?}");
Ok(())
}
}
31 changes: 13 additions & 18 deletions zink/zint/src/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ 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 @@ -42,8 +38,6 @@ 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 @@ -78,15 +72,24 @@ impl EVM<'_> {
self
}

/// Set chain id
pub fn chain_id(mut self, id: u64) -> Self {
self.inner.tx_mut().chain_id = Some(id);
self
}

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

/// Set block hash
pub fn block_hash(mut self, hash: [u8; 32]) -> Self {
self.block_hash = hash;
pub fn block_hash(mut self, number: u64, hash: [u8; 32]) -> Self {
self.inner
.db_mut()
.block_hashes
.insert(U256::from(number), hash.into());
self
}

Expand All @@ -96,14 +99,6 @@ impl EVM<'_> {
self.inner.tx_mut().gas_limit = GAS_LIMIT;
self.inner.tx_mut().transact_to = to;
self.inner.tx_mut().caller = self.caller.into();
self.inner.tx_mut().chain_id = Some(1);

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()
Expand Down

0 comments on commit 280d6a6

Please sign in to comment.