Skip to content

Commit

Permalink
test: add gas_left function test
Browse files Browse the repository at this point in the history
  • Loading branch information
poltao committed Dec 20, 2024
1 parent 08687e5 commit 71805f9
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 3 deletions.
28 changes: 28 additions & 0 deletions examples/properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ pub fn timestamp() -> u64 {
properties::timestamp()
}

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

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

Expand Down Expand Up @@ -193,4 +198,27 @@ mod tests {
assert_eq!(info.ret, 26u64.to_bytes32(), "{info:?}");
Ok(())
}

#[test]
fn test_gas_left() -> anyhow::Result<()> {
let contract = Contract::search("properties")?.compile()?;

let mut evm1 = EVM::default().tx_gas_limit(50000).commit(true);
let info1 = evm1.deploy(&contract.bytecode()?)?;
let info1 = evm1
.calldata(&contract.encode(["gasleft()".as_bytes()])?)
.call(info1.address)?;
let gasleft1 = u64::from_be_bytes(info1.ret[24..].try_into().unwrap());
let gas1 = 50000 - gasleft1;

let mut evm2 = EVM::default().tx_gas_limit(70000).commit(true);
let info2 = evm2.deploy(&contract.bytecode()?)?;
let info2 = evm2
.calldata(&contract.encode(["gasleft()".as_bytes()])?)
.call(info2.address)?;
let gasleft2 = u64::from_be_bytes(info2.ret[24..].try_into().unwrap());
let gas2 = 70000 - gasleft2;
assert_eq!(gas1, gas2);
Ok(())
}
}
2 changes: 1 addition & 1 deletion zink/src/ffi/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ extern "C" {
pub fn gaslimit() -> Bytes32;

/// Get the amount of available gas.
pub fn gas() -> u64;
pub fn gas() -> Bytes32;

/// Get the block’s timestamp.
pub fn timestamp() -> u64;
Expand Down
2 changes: 1 addition & 1 deletion zink/src/primitives/properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pub fn gaslimit() -> Bytes32 {
}

/// Get the amount of available gas.
pub fn gas() -> u64 {
pub fn gasleft() -> Bytes32 {
unsafe { ffi::evm::gas() }
}

Expand Down
11 changes: 10 additions & 1 deletion zink/zint/src/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ pub struct EVM<'e> {
pub caller: [u8; 20],
/// Blob hashes
pub blob_hashes: Option<Vec<B256>>,
/// The gas limit of the transaction
pub tx_gas_limit: u64,
/// If commit changes
commit: bool,
}
Expand All @@ -41,6 +43,7 @@ impl<'e> Default for EVM<'e> {
inner: evm,
caller: [0; 20],
blob_hashes: None,
tx_gas_limit: GAS_LIMIT,
commit: false,
}
}
Expand Down Expand Up @@ -148,10 +151,16 @@ impl EVM<'_> {
self
}

/// Set tx’s gaslimit
pub fn tx_gas_limit(mut self, gaslimit: u64) -> Self {
self.tx_gas_limit = gaslimit;
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().gas_limit = self.tx_gas_limit;
self.inner.tx_mut().transact_to = to;
self.inner.tx_mut().caller = self.caller.into();
if let Some(hashes) = &self.blob_hashes {
Expand Down

0 comments on commit 71805f9

Please sign in to comment.