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