Sitelet https://github.com/r55-eth/r55/commit/1705b9a0e7d2af2b76492b89ee296f108208d94e
Skip to content

Commit 1705b9a

Browse files
authored
feat: add block.* opcodes (#12)
1 parent 7bb81bb commit 1705b9a

5 files changed

Lines changed: 104 additions & 2 deletions

File tree

‎contract-derive/src/lib.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ pub fn contract(_attr: TokenStream, item: TokenStream) -> TokenStream {
9090
// Generate the call method implementation
9191
let call_method = quote! {
9292
use alloy_sol_types::SolValue;
93-
use eth_riscv_runtime::{revert, msg_sender, msg_value, msg_sig, msg_data, return_riscv, slice_from_raw_parts, Contract};
93+
use eth_riscv_runtime::*;
9494

9595
impl Contract for #struct_name {
9696
fn call(&self) {

‎eth-riscv-runtime/src/block.rs‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
use alloy_core::primitives::U256;
2+
use eth_riscv_syscalls::Syscall;
3+
use core::arch::asm;
4+
5+
// Returns current block timestamp in seconds since Unix epoch
6+
pub fn timestamp() -> U256 {
7+
let first: u64;
8+
let second: u64;
9+
let third: u64;
10+
let fourth: u64;
11+
unsafe {
12+
asm!("ecall", lateout("a0") first, lateout("a1") second, lateout("a2") third, lateout("a3") fourth, in("t0") u8::from(Syscall::Timestamp));
13+
}
14+
U256::from_limbs([first, second, third, fourth])
15+
}
16+
17+
// Returns current block base fee (EIP-3198 and EIP-1559)
18+
pub fn base_fee() -> U256 {
19+
let first: u64;
20+
let second: u64;
21+
let third: u64;
22+
let fourth: u64;
23+
unsafe {
24+
asm!("ecall", lateout("a0") first, lateout("a1") second, lateout("a2") third, lateout("a3") fourth, in("t0") u8::from(Syscall::BaseFee));
25+
}
26+
U256::from_limbs([first, second, third, fourth])
27+
}
28+
29+
// Returns current chain ID
30+
pub fn chain_id() -> u64 {
31+
let id: u64;
32+
unsafe {
33+
asm!("ecall", lateout("a0") id, in("t0") u8::from(Syscall::ChainId));
34+
}
35+
id
36+
}
37+
38+
// Returns current block gas limit
39+
pub fn gas_limit() -> U256 {
40+
let first: u64;
41+
let second: u64;
42+
let third: u64;
43+
let fourth: u64;
44+
unsafe {
45+
asm!("ecall", lateout("a0") first, lateout("a1") second, lateout("a2") third, lateout("a3") fourth, in("t0") u8::from(Syscall::GasLimit));
46+
}
47+
U256::from_limbs([first, second, third, fourth])
48+
}
49+
50+
// Returns current block number
51+
pub fn number() -> U256 {
52+
let first: u64;
53+
let second: u64;
54+
let third: u64;
55+
let fourth: u64;
56+
unsafe {
57+
asm!("ecall", lateout("a0") first, lateout("a1") second, lateout("a2") third, lateout("a3") fourth, in("t0") u8::from(Syscall::Number));
58+
}
59+
U256::from_limbs([first, second, third, fourth])
60+
}

‎eth-riscv-runtime/src/lib.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ pub use riscv_rt::entry;
1010

1111
mod alloc;
1212
pub mod types;
13+
pub mod block;
1314

1415
const CALLDATA_ADDRESS: usize = 0x8000_0000;
1516

‎eth-riscv-syscalls/src/lib.rs‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,14 @@ syscalls!(
6161
(0x20, Keccak256, "keccak256"),
6262
(0x33, Caller, "caller"),
6363
(0x34, CallValue, "callvalue"),
64+
(0x42, Timestamp, "timestamp"),
65+
(0x43, Number, "number"),
66+
(0x45, GasLimit, "gaslimit"),
67+
(0x46, ChainId, "chainid"),
68+
(0x48, BaseFee, "basefee"),
6469
(0x54, SLoad, "sload"),
6570
(0x55, SStore, "sstore"),
6671
(0xf1, Call, "call"),
6772
(0xf3, Return, "return"),
68-
(0xfd, Revert, "revert")
73+
(0xfd, Revert, "revert"),
6974
);

‎r55/src/exec.rs‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,42 @@ fn execute_riscv(
288288
emu.cpu.xregs.write(12, limbs[2]);
289289
emu.cpu.xregs.write(13, limbs[3]);
290290
}
291+
Syscall::BaseFee => {
292+
let value = host.env().block.basefee;
293+
let limbs = value.as_limbs();
294+
emu.cpu.xregs.write(10, limbs[0]);
295+
emu.cpu.xregs.write(11, limbs[1]);
296+
emu.cpu.xregs.write(12, limbs[2]);
297+
emu.cpu.xregs.write(13, limbs[3]);
298+
}
299+
Syscall::ChainId => {
300+
let value = host.env().cfg.chain_id;
301+
emu.cpu.xregs.write(10, value);
302+
}
303+
Syscall::GasLimit => {
304+
let limit = host.env().block.gas_limit;
305+
let limbs = limit.as_limbs();
306+
emu.cpu.xregs.write(10, limbs[0]);
307+
emu.cpu.xregs.write(11, limbs[1]);
308+
emu.cpu.xregs.write(12, limbs[2]);
309+
emu.cpu.xregs.write(13, limbs[3]);
310+
}
311+
Syscall::Number => {
312+
let number = host.env().block.number;
313+
let limbs = number.as_limbs();
314+
emu.cpu.xregs.write(10, limbs[0]);
315+
emu.cpu.xregs.write(11, limbs[1]);
316+
emu.cpu.xregs.write(12, limbs[2]);
317+
emu.cpu.xregs.write(13, limbs[3]);
318+
}
319+
Syscall::Timestamp => {
320+
let timestamp = host.env().block.timestamp;
321+
let limbs = timestamp.as_limbs();
322+
emu.cpu.xregs.write(10, limbs[0]);
323+
emu.cpu.xregs.write(11, limbs[1]);
324+
emu.cpu.xregs.write(12, limbs[2]);
325+
emu.cpu.xregs.write(13, limbs[3]);
326+
}
291327
}
292328
}
293329
_ => {

0 commit comments

Comments
 (0)