revm_interpreter/instructions/
block_info.rs

1use crate::{
2    gas,
3    instructions::utility::IntoU256,
4    interpreter::Interpreter,
5    interpreter_types::{InterpreterTypes, LoopControl, RuntimeFlag, StackTr},
6    Host,
7};
8use context_interface::{Block, Cfg};
9use primitives::U256;
10use specification::hardfork::SpecId::*;
11
12/// EIP-1344: ChainID opcode
13pub fn chainid<WIRE: InterpreterTypes, H: Host + ?Sized>(
14    interpreter: &mut Interpreter<WIRE>,
15    host: &mut H,
16) {
17    check!(interpreter, ISTANBUL);
18    gas!(interpreter, gas::BASE);
19    push!(interpreter, U256::from(host.cfg().chain_id()));
20}
21
22pub fn coinbase<WIRE: InterpreterTypes, H: Host + ?Sized>(
23    interpreter: &mut Interpreter<WIRE>,
24    host: &mut H,
25) {
26    gas!(interpreter, gas::BASE);
27    push!(interpreter, host.block().beneficiary().into_word().into());
28}
29
30pub fn timestamp<WIRE: InterpreterTypes, H: Host + ?Sized>(
31    interpreter: &mut Interpreter<WIRE>,
32    host: &mut H,
33) {
34    gas!(interpreter, gas::BASE);
35    push!(interpreter, U256::from(host.block().timestamp()));
36}
37
38pub fn block_number<WIRE: InterpreterTypes, H: Host + ?Sized>(
39    interpreter: &mut Interpreter<WIRE>,
40    host: &mut H,
41) {
42    gas!(interpreter, gas::BASE);
43    push!(interpreter, U256::from(host.block().number()));
44}
45
46pub fn difficulty<WIRE: InterpreterTypes, H: Host + ?Sized>(
47    interpreter: &mut Interpreter<WIRE>,
48    host: &mut H,
49) {
50    gas!(interpreter, gas::BASE);
51    if interpreter.runtime_flag.spec_id().is_enabled_in(MERGE) {
52        // Unwrap is safe as this fields is checked in validation handler.
53        push!(
54            interpreter,
55            (host.block().prevrandao().unwrap()).into_u256()
56        );
57    } else {
58        push!(interpreter, host.block().difficulty());
59    }
60}
61
62pub fn gaslimit<WIRE: InterpreterTypes, H: Host + ?Sized>(
63    interpreter: &mut Interpreter<WIRE>,
64    host: &mut H,
65) {
66    gas!(interpreter, gas::BASE);
67    push!(interpreter, U256::from(host.block().gas_limit()));
68}
69
70/// EIP-3198: BASEFEE opcode
71pub fn basefee<WIRE: InterpreterTypes, H: Host + ?Sized>(
72    interpreter: &mut Interpreter<WIRE>,
73    host: &mut H,
74) {
75    check!(interpreter, LONDON);
76    gas!(interpreter, gas::BASE);
77    push!(interpreter, U256::from(host.block().basefee()));
78}
79
80/// EIP-7516: BLOBBASEFEE opcode
81pub fn blob_basefee<WIRE: InterpreterTypes, H: Host + ?Sized>(
82    interpreter: &mut Interpreter<WIRE>,
83    host: &mut H,
84) {
85    check!(interpreter, CANCUN);
86    gas!(interpreter, gas::BASE);
87    push!(
88        interpreter,
89        U256::from(host.block().blob_gasprice().unwrap_or_default())
90    );
91}