Skip to main content

tempo_revm/
instructions.rs

1use crate::{evm::TempoContext, gas_credits};
2use alloy_evm::Database;
3use revm::{
4    bytecode::opcode::SSTORE,
5    handler::instructions::EthInstructions,
6    interpreter::{
7        Instruction, InstructionContext, InstructionResult,
8        instructions::{gas_table_spec, instruction_table},
9        interpreter::EthInterpreter,
10        push,
11    },
12};
13use tempo_chainspec::hardfork::TempoHardfork;
14
15/// Instruction ID for opcode returning milliseconds timestamp.
16const MILLIS_TIMESTAMP: u8 = 0x4F;
17
18/// Gas cost for [`MILLIS_TIMESTAMP`] instruction. Same as other opcodes accessing block information.
19const MILLIS_TIMESTAMP_GAS_COST: u16 = 2;
20
21/// Alias for Tempo-specific [`InstructionContext`].
22type TempoInstructionContext<'a, DB> = InstructionContext<'a, TempoContext<DB>, EthInterpreter>;
23
24/// Opcode returning current timestamp in milliseconds.
25fn millis_timestamp<DB: Database>(
26    context: TempoInstructionContext<'_, DB>,
27) -> Result<(), InstructionResult> {
28    push!(context.interpreter, context.host.block.timestamp_millis());
29    Ok(())
30}
31
32/// Returns configured instructions table for Tempo.
33pub(crate) fn tempo_instructions<DB: Database>(
34    spec: TempoHardfork,
35) -> EthInstructions<EthInterpreter, TempoContext<DB>> {
36    let evm_spec = spec.into();
37
38    // +T7: Enable TIP-1060 sstore hook
39    let mut instructions = if spec.is_t7() {
40        EthInstructions::new(
41            {
42                let mut table = instruction_table::<EthInterpreter, TempoContext<DB>>();
43                table[SSTORE as usize] = Instruction::new(gas_credits::sstore);
44                table
45            },
46            gas_table_spec(evm_spec),
47            evm_spec,
48        )
49    } else {
50        EthInstructions::new_mainnet_with_spec(spec.into())
51    };
52
53    if !spec.is_t1c() {
54        instructions.insert_instruction(
55            MILLIS_TIMESTAMP,
56            Instruction::new(millis_timestamp),
57            MILLIS_TIMESTAMP_GAS_COST,
58        );
59    }
60    instructions
61}