Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(zink): add block properties
Browse files Browse the repository at this point in the history
poltao committed Dec 6, 2024
1 parent e827947 commit cd65490
Showing 5 changed files with 128 additions and 8 deletions.
2 changes: 1 addition & 1 deletion evm/opcodes/src/cancun.rs
Original file line number Diff line number Diff line change
@@ -51,7 +51,7 @@ opcodes! {
(0x41, COINBASE, 2, 0, 1, "Get the block's beneficiary address.", Frontier, BlockInformation),
(0x42, TIMESTAMP, 2, 0, 1, "Get the block's timestamp.", Frontier, BlockInformation),
(0x43, NUMBER, 2, 0, 1, "Get the block's number.", Frontier, BlockInformation),
(0x44, DIFFICULTY, 2, 0, 1, "Get the block's difficulty.", Frontier, BlockInformation),
(0x44, PREVRANDAO, 2, 0, 1, "Get the previous block’s RANDAO mix", Frontier, BlockInformation),
(0x45, GASLIMIT, 2, 0, 1, "Get the block's gas limit.", Frontier, BlockInformation),
(0x46, CHAINID, 2, 0, 1, "Get the chain ID.", Istanbul, BlockInformation),
(0x47, SELFBALANCE, 5, 0, 1, "Get balance of currently executing account.", Istanbul, BlockInformation),
43 changes: 43 additions & 0 deletions examples/properties.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//! Example for Block and Transaction Properties.
#![cfg_attr(target_arch = "wasm32", no_std)]
#![cfg_attr(target_arch = "wasm32", no_main)]

extern crate zink;
use zink::primitives::{properties, Bytes32};

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

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

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

#[test]
fn test_block_properties() -> anyhow::Result<()> {
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(())
}
38 changes: 31 additions & 7 deletions zink/src/ffi/evm.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! EVM FFI.
use crate::primitives::Address;
use crate::primitives::{Address, Bytes32};

#[link(wasm_import_module = "evm")]
#[allow(improper_ctypes)]
@@ -134,12 +134,6 @@ extern "C" {
/// Get the current message sender
pub fn caller() -> Address;

/// Get the current blob hash at index
pub fn blobhash();

/// Get the current blob base fee
pub fn blobbasefee();

/// Append log record with no topics
pub fn log0(name: &'static [u8]);

@@ -165,4 +159,34 @@ extern "C" {
topic3: &'static [u8],
topic4: &'static [u8],
);

/// Get the current block number.
pub fn number() -> u64;

/// Get the hash of one of the 256 most recent complete blocks.
pub fn blockhash(block_number: u64) -> Bytes32;

/// Get versioned hashes.
pub fn blobhash(index: u64) -> Bytes32;

/// Get the current block’s base fee.
pub fn basefee() -> u64;

/// Get the current block’s blob base fee.
pub fn blobbasefee() -> u64;

/// Get the current chain id.
pub fn chainid() -> u64;

/// Get the block’s beneficiary address.
pub fn coinbase() -> Address;

/// Get the previous block’s RANDAO mix.
pub fn prevrandao() -> Bytes32;

/// Get the current block gaslimit.
pub fn gaslimit() -> u64;

/// Get the block’s timestamp.
pub fn timestamp() -> u64;
}
1 change: 1 addition & 0 deletions zink/src/primitives/mod.rs
Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@
mod address;
pub mod bytes;
pub mod numeric;
pub mod properties;
mod u256;

pub use {address::Address, bytes::*, u256::U256};
52 changes: 52 additions & 0 deletions zink/src/primitives/properties.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
use super::{Address, Bytes32};
use crate::ffi;

/// Get the current block number.
pub fn number() -> u64 {
unsafe { ffi::evm::number() }
}

/// Get the hash of one of the 256 most recent complete blocks.
pub fn blockhash(block_number: u64) -> Bytes32 {
unsafe { ffi::evm::blockhash(block_number) }
}

/// Get versioned hashes.
pub fn blobhash(index: u64) -> Bytes32 {
unsafe { ffi::evm::blobhash(index) }
}

/// Get the current block’s base fee.
pub fn basefee() -> u64 {
unsafe { ffi::evm::basefee() }
}

/// Get the current block’s blob base fee.
pub fn blobbasefee() -> u64 {
unsafe { ffi::evm::blobbasefee() }
}

/// Get the current chain id.
pub fn chainid() -> u64 {
unsafe { ffi::evm::chainid() }
}

/// Get the block’s beneficiary address.
pub fn coinbase() -> Address {
unsafe { ffi::evm::coinbase() }
}

/// Get the previous block’s RANDAO mix.
pub fn prevrandao() -> Bytes32 {
unsafe { ffi::evm::prevrandao() }
}

/// Get the current block gaslimit.
pub fn gaslimit() -> u64 {
unsafe { ffi::evm::gaslimit() }
}

/// Get the block’s timestamp.
pub fn timestamp() -> u64 {
unsafe { ffi::evm::timestamp() }
}

0 comments on commit cd65490

Please sign in to comment.