revm_interpreter/instructions/
host_env.rs
1use crate::{
2 gas,
3 primitives::{Spec, SpecId::*, U256},
4 Host, Interpreter,
5};
6
7pub fn chainid<H: Host + ?Sized, SPEC: Spec>(interpreter: &mut Interpreter, host: &mut H) {
9 check!(interpreter, ISTANBUL);
10 gas!(interpreter, gas::BASE);
11 push!(interpreter, U256::from(host.env().cfg.chain_id));
12}
13
14pub fn coinbase<H: Host + ?Sized>(interpreter: &mut Interpreter, host: &mut H) {
15 gas!(interpreter, gas::BASE);
16 push_b256!(interpreter, host.env().block.coinbase.into_word());
17}
18
19pub fn timestamp<H: Host + ?Sized>(interpreter: &mut Interpreter, host: &mut H) {
20 gas!(interpreter, gas::BASE);
21 push!(interpreter, host.env().block.timestamp);
22}
23
24pub fn block_number<H: Host + ?Sized>(interpreter: &mut Interpreter, host: &mut H) {
25 gas!(interpreter, gas::BASE);
26 push!(interpreter, host.env().block.number);
27}
28
29pub fn difficulty<H: Host + ?Sized, SPEC: Spec>(interpreter: &mut Interpreter, host: &mut H) {
30 gas!(interpreter, gas::BASE);
31 if SPEC::enabled(MERGE) {
32 push_b256!(interpreter, host.env().block.prevrandao.unwrap());
33 } else {
34 push!(interpreter, host.env().block.difficulty);
35 }
36}
37
38pub fn gaslimit<H: Host + ?Sized>(interpreter: &mut Interpreter, host: &mut H) {
39 gas!(interpreter, gas::BASE);
40 push!(interpreter, host.env().block.gas_limit);
41}
42
43pub fn gasprice<H: Host + ?Sized>(interpreter: &mut Interpreter, host: &mut H) {
44 gas!(interpreter, gas::BASE);
45 push!(interpreter, host.env().effective_gas_price());
46}
47
48pub fn basefee<H: Host + ?Sized, SPEC: Spec>(interpreter: &mut Interpreter, host: &mut H) {
50 check!(interpreter, LONDON);
51 gas!(interpreter, gas::BASE);
52 push!(interpreter, host.env().block.basefee);
53}
54
55pub fn origin<H: Host + ?Sized>(interpreter: &mut Interpreter, host: &mut H) {
56 gas!(interpreter, gas::BASE);
57 push_b256!(interpreter, host.env().tx.caller.into_word());
58}
59
60pub fn blob_hash<H: Host + ?Sized, SPEC: Spec>(interpreter: &mut Interpreter, host: &mut H) {
62 check!(interpreter, CANCUN);
63 gas!(interpreter, gas::VERYLOW);
64 pop_top!(interpreter, index);
65 let i = as_usize_saturated!(index);
66 *index = match host.env().tx.blob_hashes.get(i) {
67 Some(hash) => U256::from_be_bytes(hash.0),
68 None => U256::ZERO,
69 };
70}
71
72pub fn blob_basefee<H: Host + ?Sized, SPEC: Spec>(interpreter: &mut Interpreter, host: &mut H) {
74 check!(interpreter, CANCUN);
75 gas!(interpreter, gas::BASE);
76 push!(
77 interpreter,
78 U256::from(host.env().block.get_blob_gasprice().unwrap_or_default())
79 );
80}