Skip to main content

tempo_revm/
instructions.rs

1use crate::evm::TempoContext;
2use alloy_evm::Database;
3use revm::{
4    handler::instructions::EthInstructions,
5    interpreter::{Instruction, InstructionContext, interpreter::EthInterpreter, push},
6};
7use tempo_chainspec::hardfork::TempoHardfork;
8
9/// Instruction ID for opcode returning milliseconds timestamp.
10const MILLIS_TIMESTAMP: u8 = 0x4F;
11
12/// Gas cost for [`MILLIS_TIMESTAMP`] instruction. Same as other opcodes accessing block information.
13const MILLIS_TIMESTAMP_GAS_COST: u64 = 2;
14
15/// Alias for Tempo-specific [`InstructionContext`].
16type TempoInstructionContext<'a, DB> = InstructionContext<'a, TempoContext<DB>, EthInterpreter>;
17
18/// Opcode returning current timestamp in milliseconds.
19fn millis_timestamp<DB: Database>(context: TempoInstructionContext<'_, DB>) {
20    push!(context.interpreter, context.host.block.timestamp_millis());
21}
22
23/// Returns configured instructions table for Tempo.
24pub(crate) fn tempo_instructions<DB: Database>(
25    spec: TempoHardfork,
26) -> EthInstructions<EthInterpreter, TempoContext<DB>> {
27    let mut instructions = EthInstructions::new_mainnet_with_spec(spec.into());
28    if !spec.is_t1c() {
29        instructions.insert_instruction(
30            MILLIS_TIMESTAMP,
31            Instruction::new(millis_timestamp, MILLIS_TIMESTAMP_GAS_COST),
32        );
33    }
34    instructions
35}