revm_interpreter/instructions/
block_info.rs

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