revm_interpreter/instructions/
block_info.rs

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