reth_optimism_primitives/transaction/
tx_type.rs

1//! Optimism transaction type.
2
3#[cfg(test)]
4mod tests {
5    use alloy_consensus::constants::EIP7702_TX_TYPE_ID;
6    use op_alloy_consensus::{OpTxType, DEPOSIT_TX_TYPE_ID};
7    use reth_codecs::{txtype::*, Compact};
8    use rstest::rstest;
9
10    #[rstest]
11    #[case(OpTxType::Legacy, COMPACT_IDENTIFIER_LEGACY, vec![])]
12    #[case(OpTxType::Eip2930, COMPACT_IDENTIFIER_EIP2930, vec![])]
13    #[case(OpTxType::Eip1559, COMPACT_IDENTIFIER_EIP1559, vec![])]
14    #[case(OpTxType::Eip7702, COMPACT_EXTENDED_IDENTIFIER_FLAG, vec![EIP7702_TX_TYPE_ID])]
15    #[case(OpTxType::Deposit, COMPACT_EXTENDED_IDENTIFIER_FLAG, vec![DEPOSIT_TX_TYPE_ID])]
16    fn test_txtype_to_compact(
17        #[case] tx_type: OpTxType,
18        #[case] expected_identifier: usize,
19        #[case] expected_buf: Vec<u8>,
20    ) {
21        let mut buf = vec![];
22        let identifier = tx_type.to_compact(&mut buf);
23
24        assert_eq!(
25            identifier, expected_identifier,
26            "Unexpected identifier for OpTxType {tx_type:?}",
27        );
28        assert_eq!(buf, expected_buf, "Unexpected buffer for OpTxType {tx_type:?}",);
29    }
30
31    #[rstest]
32    #[case(OpTxType::Legacy, COMPACT_IDENTIFIER_LEGACY, vec![])]
33    #[case(OpTxType::Eip2930, COMPACT_IDENTIFIER_EIP2930, vec![])]
34    #[case(OpTxType::Eip1559, COMPACT_IDENTIFIER_EIP1559, vec![])]
35    #[case(OpTxType::Eip7702, COMPACT_EXTENDED_IDENTIFIER_FLAG, vec![EIP7702_TX_TYPE_ID])]
36    #[case(OpTxType::Deposit, COMPACT_EXTENDED_IDENTIFIER_FLAG, vec![DEPOSIT_TX_TYPE_ID])]
37    fn test_txtype_from_compact(
38        #[case] expected_type: OpTxType,
39        #[case] identifier: usize,
40        #[case] buf: Vec<u8>,
41    ) {
42        let (actual_type, remaining_buf) = OpTxType::from_compact(&buf, identifier);
43
44        assert_eq!(actual_type, expected_type, "Unexpected TxType for identifier {identifier}");
45        assert!(remaining_buf.is_empty(), "Buffer not fully consumed for identifier {identifier}");
46    }
47}