use core::fmt;
use eth_valkyoth_codec::{DecodeError, DecodeErrorCategory};
use super::AccessListTransactionField;
use crate::transaction::{TransactionEnvelopeError, TransactionEnvelopeErrorCategory};
#[non_exhaustive]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum AccessListTransactionDecodeError {
Envelope(TransactionEnvelopeError),
WrongTransactionType {
type_byte: u8,
},
WrongFieldCount {
expected: usize,
found: usize,
},
FieldDecode {
field: AccessListTransactionField,
source: DecodeError,
},
InvalidToLength {
found: usize,
},
InvalidYParity {
value: u64,
},
InvalidAccessListEntryFieldCount {
found: usize,
},
InvalidAccessListAddressLength {
found: usize,
},
InvalidStorageKeyLength {
found: usize,
},
}
impl AccessListTransactionDecodeError {
#[must_use]
pub const fn code(self) -> &'static str {
match self {
Self::Envelope(error) => error.code(),
Self::WrongTransactionType { .. } => "ETH_ACCESS_LIST_TX_WRONG_TYPE",
Self::WrongFieldCount { .. } => "ETH_ACCESS_LIST_TX_WRONG_FIELD_COUNT",
Self::FieldDecode { source, .. } => source.code(),
Self::InvalidToLength { .. } => "ETH_ACCESS_LIST_TX_INVALID_TO_LENGTH",
Self::InvalidYParity { .. } => "ETH_ACCESS_LIST_TX_INVALID_Y_PARITY",
Self::InvalidAccessListEntryFieldCount { .. } => {
"ETH_ACCESS_LIST_TX_INVALID_ENTRY_FIELD_COUNT"
}
Self::InvalidAccessListAddressLength { .. } => {
"ETH_ACCESS_LIST_TX_INVALID_ADDRESS_LENGTH"
}
Self::InvalidStorageKeyLength { .. } => "ETH_ACCESS_LIST_TX_INVALID_STORAGE_KEY_LENGTH",
}
}
#[must_use]
pub const fn message(self) -> &'static str {
match self {
Self::Envelope(error) => error.message(),
Self::WrongTransactionType { .. } => {
"access-list transaction decoder received a different envelope type"
}
Self::WrongFieldCount { .. } => {
"access-list transaction must contain exactly eleven RLP fields"
}
Self::FieldDecode { .. } => "access-list transaction field failed bounded decoding",
Self::InvalidToLength { .. } => {
"access-list transaction to field must be empty or a 20-byte address"
}
Self::InvalidYParity { .. } => "access-list transaction y parity must be 0 or 1",
Self::InvalidAccessListEntryFieldCount { .. } => {
"access-list entry must contain exactly address and storage-key list"
}
Self::InvalidAccessListAddressLength { .. } => {
"access-list address must be a 20-byte scalar"
}
Self::InvalidStorageKeyLength { .. } => {
"access-list storage key must be a 32-byte scalar"
}
}
}
#[must_use]
pub const fn category(self) -> AccessListTransactionDecodeErrorCategory {
match self {
Self::Envelope(error) => match error.category() {
TransactionEnvelopeErrorCategory::ResourceExhaustion => {
AccessListTransactionDecodeErrorCategory::ResourceExhaustion
}
TransactionEnvelopeErrorCategory::Unsupported => {
AccessListTransactionDecodeErrorCategory::WrongType
}
_ => AccessListTransactionDecodeErrorCategory::MalformedInput,
},
Self::WrongTransactionType { .. } => {
AccessListTransactionDecodeErrorCategory::WrongType
}
Self::FieldDecode { source, .. } => match source.category() {
DecodeErrorCategory::ResourceExhaustion => {
AccessListTransactionDecodeErrorCategory::ResourceExhaustion
}
_ => AccessListTransactionDecodeErrorCategory::MalformedInput,
},
Self::WrongFieldCount { .. }
| Self::InvalidToLength { .. }
| Self::InvalidYParity { .. }
| Self::InvalidAccessListEntryFieldCount { .. }
| Self::InvalidAccessListAddressLength { .. }
| Self::InvalidStorageKeyLength { .. } => {
AccessListTransactionDecodeErrorCategory::MalformedInput
}
}
}
}
impl fmt::Display for AccessListTransactionDecodeError {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str(self.message())
}
}
#[cfg(feature = "std")]
impl std::error::Error for AccessListTransactionDecodeError {}
#[non_exhaustive]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum AccessListTransactionDecodeErrorCategory {
MalformedInput,
WrongType,
ResourceExhaustion,
}