eth_valkyoth_protocol/transaction/set_code/
error.rs1use core::fmt;
2
3use eth_valkyoth_codec::{DecodeError, DecodeErrorCategory};
4
5use super::SetCodeTransactionField;
6use crate::transaction::{TransactionEnvelopeError, TransactionEnvelopeErrorCategory};
7
8#[non_exhaustive]
10#[derive(Clone, Copy, Debug, Eq, PartialEq)]
11pub enum SetCodeAuthorizationField {
12 ChainId,
14 Address,
16 Nonce,
18 YParity,
20 R,
22 S,
24}
25
26#[non_exhaustive]
28#[derive(Clone, Copy, Debug, Eq, PartialEq)]
29pub enum SetCodeTransactionDecodeError {
30 Envelope(TransactionEnvelopeError),
32 WrongTransactionType {
34 type_byte: u8,
36 },
37 WrongFieldCount {
39 expected: usize,
41 found: usize,
43 },
44 FieldDecode {
46 field: SetCodeTransactionField,
48 source: DecodeError,
50 },
51 AuthorizationFieldDecode {
53 field: SetCodeAuthorizationField,
55 source: DecodeError,
57 },
58 InvalidToLength {
60 found: usize,
62 },
63 InvalidYParity {
65 value: u64,
67 },
68 InvalidAccessListEntryFieldCount {
70 found: usize,
72 },
73 InvalidAccessListAddressLength {
75 found: usize,
77 },
78 InvalidStorageKeyLength {
80 found: usize,
82 },
83 InvalidAuthorizationFieldCount {
85 found: usize,
87 },
88 InvalidAuthorizationAddressLength {
90 found: usize,
92 },
93 InvalidAuthorizationYParity {
95 value: u64,
97 },
98}
99
100impl SetCodeTransactionDecodeError {
101 #[must_use]
103 pub const fn code(self) -> &'static str {
104 match self {
105 Self::Envelope(error) => error.code(),
106 Self::WrongTransactionType { .. } => "ETH_SET_CODE_TX_WRONG_TYPE",
107 Self::WrongFieldCount { .. } => "ETH_SET_CODE_TX_WRONG_FIELD_COUNT",
108 Self::FieldDecode { source, .. } => source.code(),
109 Self::AuthorizationFieldDecode { source, .. } => source.code(),
110 Self::InvalidToLength { .. } => "ETH_SET_CODE_TX_INVALID_TO_LENGTH",
111 Self::InvalidYParity { .. } => "ETH_SET_CODE_TX_INVALID_Y_PARITY",
112 Self::InvalidAccessListEntryFieldCount { .. } => {
113 "ETH_SET_CODE_TX_INVALID_ENTRY_FIELD_COUNT"
114 }
115 Self::InvalidAccessListAddressLength { .. } => {
116 "ETH_SET_CODE_TX_INVALID_ACCESS_ADDRESS_LENGTH"
117 }
118 Self::InvalidStorageKeyLength { .. } => "ETH_SET_CODE_TX_INVALID_STORAGE_KEY_LENGTH",
119 Self::InvalidAuthorizationFieldCount { .. } => {
120 "ETH_SET_CODE_TX_INVALID_AUTH_FIELD_COUNT"
121 }
122 Self::InvalidAuthorizationAddressLength { .. } => {
123 "ETH_SET_CODE_TX_INVALID_AUTH_ADDRESS_LENGTH"
124 }
125 Self::InvalidAuthorizationYParity { .. } => "ETH_SET_CODE_TX_INVALID_AUTH_Y_PARITY",
126 }
127 }
128
129 #[must_use]
131 pub const fn message(self) -> &'static str {
132 match self {
133 Self::Envelope(error) => error.message(),
134 Self::WrongTransactionType { .. } => {
135 "set-code transaction decoder received a different envelope type"
136 }
137 Self::WrongFieldCount { .. } => {
138 "set-code transaction must contain exactly thirteen RLP fields"
139 }
140 Self::FieldDecode { .. } => "set-code transaction field failed bounded decoding",
141 Self::AuthorizationFieldDecode { .. } => {
142 "set-code authorization tuple field failed bounded decoding"
143 }
144 Self::InvalidToLength { .. } => {
145 "set-code transaction destination field must be a 20-byte address"
146 }
147 Self::InvalidYParity { .. } => "set-code transaction y parity must be 0 or 1",
148 Self::InvalidAccessListEntryFieldCount { .. } => {
149 "set-code transaction access-list entry must contain exactly address and storage-key list"
150 }
151 Self::InvalidAccessListAddressLength { .. } => {
152 "set-code transaction access-list address must be a 20-byte scalar"
153 }
154 Self::InvalidStorageKeyLength { .. } => {
155 "set-code transaction access-list storage key must be a 32-byte scalar"
156 }
157 Self::InvalidAuthorizationFieldCount { .. } => {
158 "set-code authorization tuple must contain exactly chain id, address, nonce, y parity, r, and s"
159 }
160 Self::InvalidAuthorizationAddressLength { .. } => {
161 "set-code authorization address must be a 20-byte scalar"
162 }
163 Self::InvalidAuthorizationYParity { .. } => {
164 "set-code authorization y parity must be 0 or 1"
165 }
166 }
167 }
168
169 #[must_use]
171 pub const fn category(self) -> SetCodeTransactionDecodeErrorCategory {
172 match self {
173 Self::Envelope(error) => match error.category() {
174 TransactionEnvelopeErrorCategory::ResourceExhaustion => {
175 SetCodeTransactionDecodeErrorCategory::ResourceExhaustion
176 }
177 TransactionEnvelopeErrorCategory::Unsupported => {
178 SetCodeTransactionDecodeErrorCategory::WrongType
179 }
180 _ => SetCodeTransactionDecodeErrorCategory::MalformedInput,
181 },
182 Self::WrongTransactionType { .. } => SetCodeTransactionDecodeErrorCategory::WrongType,
183 Self::FieldDecode { source, .. } | Self::AuthorizationFieldDecode { source, .. } => {
184 match source.category() {
185 DecodeErrorCategory::ResourceExhaustion => {
186 SetCodeTransactionDecodeErrorCategory::ResourceExhaustion
187 }
188 _ => SetCodeTransactionDecodeErrorCategory::MalformedInput,
189 }
190 }
191 Self::WrongFieldCount { .. }
192 | Self::InvalidToLength { .. }
193 | Self::InvalidYParity { .. }
194 | Self::InvalidAccessListEntryFieldCount { .. }
195 | Self::InvalidAccessListAddressLength { .. }
196 | Self::InvalidStorageKeyLength { .. }
197 | Self::InvalidAuthorizationFieldCount { .. }
198 | Self::InvalidAuthorizationAddressLength { .. }
199 | Self::InvalidAuthorizationYParity { .. } => {
200 SetCodeTransactionDecodeErrorCategory::MalformedInput
201 }
202 }
203 }
204}
205
206impl fmt::Display for SetCodeTransactionDecodeError {
207 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
208 formatter.write_str(self.message())
209 }
210}
211
212#[cfg(feature = "std")]
213impl std::error::Error for SetCodeTransactionDecodeError {}
214
215#[non_exhaustive]
217#[derive(Clone, Copy, Debug, Eq, PartialEq)]
218pub enum SetCodeTransactionDecodeErrorCategory {
219 MalformedInput,
221 WrongType,
223 ResourceExhaustion,
225}