Skip to main content

alloy_rpc_types_trace/
opcode.rs

1//! Types for opcode tracing.
2
3use alloy_primitives::{BlockHash, TxHash};
4use serde::{Deserialize, Serialize};
5
6/// Opcode gas usage for a block.
7#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
8#[serde(rename_all = "camelCase")]
9pub struct BlockOpcodeGas {
10    /// The block hash
11    pub block_hash: BlockHash,
12    /// The block number
13    pub block_number: u64,
14    /// All executed transactions in the block in the order they were executed, with their opcode
15    /// gas usage.
16    pub transactions: Vec<TransactionOpcodeGas>,
17}
18
19impl BlockOpcodeGas {
20    /// Returns true if the block contains the given opcode.
21    pub fn contains(&self, opcode: &str) -> bool {
22        self.transactions.iter().any(|tx| tx.contains(opcode))
23    }
24}
25
26/// Opcode gas usage for a transaction.
27#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
28#[serde(rename_all = "camelCase")]
29#[doc(alias = "TxOpcodeGas")]
30pub struct TransactionOpcodeGas {
31    /// The transaction hash
32    #[doc(alias = "tx_hash")]
33    pub transaction_hash: TxHash,
34    /// The gas used by each opcode in the transaction
35    pub opcode_gas: Vec<OpcodeGas>,
36}
37
38impl TransactionOpcodeGas {
39    /// Returns true if the transaction contains the given opcode.
40    pub fn contains(&self, opcode: &str) -> bool {
41        self.opcode_gas.iter().any(|op| op.opcode.eq_ignore_ascii_case(opcode))
42    }
43}
44
45/// Gas information for a single opcode.
46#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
47#[serde(rename_all = "camelCase")]
48pub struct OpcodeGas {
49    /// The name of the opcode
50    pub opcode: String,
51    /// How many times the opcode was executed
52    pub count: u64,
53    /// Combined gas used by all instances of the opcode
54    ///
55    /// For opcodes with constant gas costs, this is the constant opcode gas cost times the count.
56    pub gas_used: u64,
57}