reth_codecs/alloy/
txkind.rs

1//! Native Compact codec impl for primitive alloy [`TxKind`].
2
3use crate::Compact;
4use alloy_primitives::{Address, TxKind};
5
6/// Identifier for [`TxKind::Create`]
7const TX_KIND_TYPE_CREATE: usize = 0;
8
9/// Identifier for [`TxKind::Call`]
10const TX_KIND_TYPE_CALL: usize = 1;
11
12impl Compact for TxKind {
13    fn to_compact<B>(&self, buf: &mut B) -> usize
14    where
15        B: bytes::BufMut + AsMut<[u8]>,
16    {
17        match self {
18            Self::Create => TX_KIND_TYPE_CREATE,
19            Self::Call(address) => {
20                address.to_compact(buf);
21                TX_KIND_TYPE_CALL
22            }
23        }
24    }
25    fn from_compact(buf: &[u8], identifier: usize) -> (Self, &[u8]) {
26        match identifier {
27            TX_KIND_TYPE_CREATE => (Self::Create, buf),
28            TX_KIND_TYPE_CALL => {
29                let (addr, buf) = Address::from_compact(buf, buf.len());
30                (addr.into(), buf)
31            }
32            _ => {
33                unreachable!("Junk data in database: unknown TransactionKind variant",)
34            }
35        }
36    }
37}