eth_valkyoth_protocol/header/
error.rs1use core::fmt;
2
3use eth_valkyoth_codec::{DecodeError, DecodeErrorCategory};
4
5#[non_exhaustive]
7#[derive(Clone, Copy, Debug, Eq, PartialEq)]
8pub enum BlockHeaderField {
9 ParentHash,
11 OmmersHash,
13 Beneficiary,
15 StateRoot,
17 TransactionsRoot,
19 ReceiptsRoot,
21 LogsBloom,
23 Difficulty,
25 Number,
27 GasLimit,
29 GasUsed,
31 Timestamp,
33 ExtraData,
35 MixHash,
37 Nonce,
39 BaseFeePerGas,
41 WithdrawalsRoot,
43 BlobGasUsed,
45 ExcessBlobGas,
47 ParentBeaconBlockRoot,
49 RequestsHash,
51}
52
53#[non_exhaustive]
55#[derive(Clone, Copy, Debug, Eq, PartialEq)]
56pub enum BlockHeaderDecodeError {
57 Decode(DecodeError),
59 WrongFieldCount {
61 expected: usize,
63 found: usize,
65 },
66 FieldDecode {
68 field: BlockHeaderField,
70 source: DecodeError,
72 },
73 InvalidFieldLength {
75 field: BlockHeaderField,
77 expected: usize,
79 found: usize,
81 },
82}
83
84impl BlockHeaderDecodeError {
85 #[must_use]
87 pub const fn code(self) -> &'static str {
88 match self {
89 Self::Decode(error) => error.code(),
90 Self::WrongFieldCount { .. } => "ETH_HEADER_WRONG_FIELD_COUNT",
91 Self::FieldDecode { source, .. } => source.code(),
92 Self::InvalidFieldLength { .. } => "ETH_HEADER_INVALID_FIELD_LENGTH",
93 }
94 }
95
96 #[must_use]
98 pub const fn message(self) -> &'static str {
99 match self {
100 Self::Decode(error) => error.message(),
101 Self::WrongFieldCount { .. } => {
102 "block header field count does not match the selected field set"
103 }
104 Self::FieldDecode { .. } => "block header field failed bounded decoding",
105 Self::InvalidFieldLength { .. } => {
106 "block header fixed-width field has an invalid byte length"
107 }
108 }
109 }
110
111 #[must_use]
113 pub const fn category(self) -> BlockHeaderDecodeErrorCategory {
114 match self {
115 Self::WrongFieldCount { .. } | Self::InvalidFieldLength { .. } => {
116 BlockHeaderDecodeErrorCategory::MalformedInput
117 }
118 Self::Decode(error) | Self::FieldDecode { source: error, .. } => {
119 match error.category() {
120 DecodeErrorCategory::ResourceExhaustion => {
121 BlockHeaderDecodeErrorCategory::ResourceExhaustion
122 }
123 _ => BlockHeaderDecodeErrorCategory::MalformedInput,
124 }
125 }
126 }
127 }
128}
129
130impl fmt::Display for BlockHeaderDecodeError {
131 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
132 formatter.write_str(self.message())
133 }
134}
135
136#[cfg(feature = "std")]
137impl std::error::Error for BlockHeaderDecodeError {}
138
139#[non_exhaustive]
141#[derive(Clone, Copy, Debug, Eq, PartialEq)]
142pub enum BlockHeaderDecodeErrorCategory {
143 MalformedInput,
145 ResourceExhaustion,
147}