1use crate::{EthMessageID, EthVersion, SnapVersion};
4use alloc::{borrow::Cow, string::String, vec::Vec};
5use alloy_primitives::bytes::Bytes;
6use alloy_rlp::{Decodable, Encodable, RlpDecodable, RlpEncodable};
7use bytes::BufMut;
8use core::fmt;
9use reth_codecs_derive::add_arbitrary_tests;
10
11#[derive(Debug, Clone, Eq, PartialEq)]
13#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
14pub struct RawCapabilityMessage {
15 pub id: usize,
17 pub payload: Bytes,
19}
20
21impl RawCapabilityMessage {
22 pub const fn new(id: usize, payload: Bytes) -> Self {
24 Self { id, payload }
25 }
26
27 pub const fn eth(id: EthMessageID, payload: Bytes) -> Self {
33 Self::new(id.to_u8() as usize, payload)
34 }
35
36 pub fn encoded(&self) -> Bytes {
38 alloy_rlp::encode(self).into()
39 }
40}
41
42impl Encodable for RawCapabilityMessage {
43 fn encode(&self, out: &mut dyn BufMut) {
45 self.id.encode(out);
46 out.put_slice(&self.payload);
47 }
48
49 fn length(&self) -> usize {
51 self.id.length() + self.payload.len()
52 }
53}
54
55impl Decodable for RawCapabilityMessage {
56 fn decode(buf: &mut &[u8]) -> alloy_rlp::Result<Self> {
58 let id = usize::decode(buf)?;
59 let payload = Bytes::copy_from_slice(buf);
60 *buf = &buf[buf.len()..];
61
62 Ok(Self { id, payload })
63 }
64}
65
66#[add_arbitrary_tests(rlp)]
68#[derive(Clone, Debug, PartialEq, Eq, RlpEncodable, RlpDecodable, Default, Hash)]
69#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
70pub struct Capability {
71 pub name: Cow<'static, str>,
73 pub version: usize,
75}
76
77impl Capability {
78 pub const fn new(name: String, version: usize) -> Self {
80 Self { name: Cow::Owned(name), version }
81 }
82
83 pub const fn new_static(name: &'static str, version: usize) -> Self {
85 Self { name: Cow::Borrowed(name), version }
86 }
87
88 pub const fn eth(version: EthVersion) -> Self {
90 Self::new_static("eth", version as usize)
91 }
92
93 pub const fn snap(version: SnapVersion) -> Self {
95 Self::new_static("snap", version as usize)
96 }
97
98 pub const fn eth_66() -> Self {
100 Self::eth(EthVersion::Eth66)
101 }
102
103 pub const fn eth_67() -> Self {
105 Self::eth(EthVersion::Eth67)
106 }
107
108 pub const fn eth_68() -> Self {
110 Self::eth(EthVersion::Eth68)
111 }
112
113 pub const fn eth_69() -> Self {
115 Self::eth(EthVersion::Eth69)
116 }
117
118 pub const fn eth_70() -> Self {
120 Self::eth(EthVersion::Eth70)
121 }
122
123 pub const fn eth_71() -> Self {
125 Self::eth(EthVersion::Eth71)
126 }
127
128 pub const fn eth_72() -> Self {
130 Self::eth(EthVersion::Eth72)
131 }
132
133 pub const fn eth_73() -> Self {
135 Self::eth(EthVersion::Eth73)
136 }
137
138 pub const fn snap_2() -> Self {
140 Self::snap(SnapVersion::V2)
141 }
142
143 #[inline]
145 pub fn is_eth_v66(&self) -> bool {
146 self.name == "eth" && self.version == 66
147 }
148
149 #[inline]
151 pub fn is_eth_v67(&self) -> bool {
152 self.name == "eth" && self.version == 67
153 }
154
155 #[inline]
157 pub fn is_eth_v68(&self) -> bool {
158 self.name == "eth" && self.version == 68
159 }
160
161 #[inline]
163 pub fn is_eth_v69(&self) -> bool {
164 self.name == "eth" && self.version == 69
165 }
166
167 #[inline]
169 pub fn is_eth_v70(&self) -> bool {
170 self.name == "eth" && self.version == 70
171 }
172
173 #[inline]
175 pub fn is_eth_v71(&self) -> bool {
176 self.name == "eth" && self.version == 71
177 }
178
179 #[inline]
181 pub fn is_eth_v72(&self) -> bool {
182 self.name == "eth" && self.version == 72
183 }
184
185 pub fn is_eth_v73(&self) -> bool {
187 self.name == "eth" && self.version == 73
188 }
189
190 #[inline]
192 pub fn is_eth(&self) -> bool {
193 self.is_eth_v66() ||
194 self.is_eth_v67() ||
195 self.is_eth_v68() ||
196 self.is_eth_v69() ||
197 self.is_eth_v70() ||
198 self.is_eth_v71() ||
199 self.is_eth_v72() ||
200 self.is_eth_v73()
201 }
202}
203
204impl fmt::Display for Capability {
205 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
206 write!(f, "{}/{}", self.name, self.version)
207 }
208}
209
210impl From<EthVersion> for Capability {
211 #[inline]
212 fn from(value: EthVersion) -> Self {
213 Self::eth(value)
214 }
215}
216
217#[cfg(any(test, feature = "arbitrary"))]
218impl<'a> arbitrary::Arbitrary<'a> for Capability {
219 fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
220 let version = u.int_in_range(66..=73)?; Ok(Self::new_static("eth", version))
224 }
225}
226
227#[derive(Debug, Clone, Eq, PartialEq)]
229pub struct Capabilities {
230 inner: Vec<Capability>,
232 eth_66: bool,
233 eth_67: bool,
234 eth_68: bool,
235 eth_69: bool,
236 eth_70: bool,
237 eth_71: bool,
238 eth_72: bool,
239 eth_73: bool,
240}
241
242impl Capabilities {
243 pub fn new(value: Vec<Capability>) -> Self {
245 Self {
246 eth_66: value.iter().any(Capability::is_eth_v66),
247 eth_67: value.iter().any(Capability::is_eth_v67),
248 eth_68: value.iter().any(Capability::is_eth_v68),
249 eth_69: value.iter().any(Capability::is_eth_v69),
250 eth_70: value.iter().any(Capability::is_eth_v70),
251 eth_71: value.iter().any(Capability::is_eth_v71),
252 eth_72: value.iter().any(Capability::is_eth_v72),
253 eth_73: value.iter().any(Capability::is_eth_v73),
254 inner: value,
255 }
256 }
257
258 pub const fn supports_eth_at_least(&self, version: &EthVersion) -> bool {
268 match version {
269 EthVersion::Eth66 => {
270 self.eth_66 ||
271 self.eth_67 ||
272 self.eth_68 ||
273 self.eth_69 ||
274 self.eth_70 ||
275 self.eth_71 ||
276 self.eth_72 ||
277 self.eth_73
278 }
279 EthVersion::Eth67 => {
280 self.eth_67 ||
281 self.eth_68 ||
282 self.eth_69 ||
283 self.eth_70 ||
284 self.eth_71 ||
285 self.eth_72 ||
286 self.eth_73
287 }
288 EthVersion::Eth68 => {
289 self.eth_68 ||
290 self.eth_69 ||
291 self.eth_70 ||
292 self.eth_71 ||
293 self.eth_72 ||
294 self.eth_73
295 }
296 EthVersion::Eth69 => {
297 self.eth_69 || self.eth_70 || self.eth_71 || self.eth_72 || self.eth_73
298 }
299 EthVersion::Eth70 => self.eth_70 || self.eth_71 || self.eth_72 || self.eth_73,
300 EthVersion::Eth71 => self.eth_71 || self.eth_72 || self.eth_73,
301 EthVersion::Eth72 => self.eth_72 || self.eth_73,
302 EthVersion::Eth73 => self.eth_73,
303 }
304 }
305
306 #[inline]
308 pub fn capabilities(&self) -> &[Capability] {
309 &self.inner
310 }
311
312 #[inline]
314 pub fn into_inner(self) -> Vec<Capability> {
315 self.inner
316 }
317
318 #[inline]
320 pub const fn supports_eth(&self) -> bool {
321 self.eth_73 ||
322 self.eth_72 ||
323 self.eth_71 ||
324 self.eth_70 ||
325 self.eth_69 ||
326 self.eth_68 ||
327 self.eth_67 ||
328 self.eth_66
329 }
330
331 #[inline]
333 pub const fn supports_eth_v66(&self) -> bool {
334 self.eth_66
335 }
336
337 #[inline]
339 pub const fn supports_eth_v67(&self) -> bool {
340 self.eth_67
341 }
342
343 #[inline]
345 pub const fn supports_eth_v68(&self) -> bool {
346 self.eth_68
347 }
348
349 #[inline]
351 pub const fn supports_eth_v69(&self) -> bool {
352 self.eth_69
353 }
354
355 #[inline]
357 pub const fn supports_eth_v70(&self) -> bool {
358 self.eth_70
359 }
360
361 #[inline]
363 pub const fn supports_eth_v71(&self) -> bool {
364 self.eth_71
365 }
366}
367
368impl From<Vec<Capability>> for Capabilities {
369 fn from(value: Vec<Capability>) -> Self {
370 Self::new(value)
371 }
372}
373
374impl Encodable for Capabilities {
375 fn length(&self) -> usize {
376 self.inner.length()
377 }
378
379 fn encode(&self, out: &mut dyn BufMut) {
380 self.inner.encode(out)
381 }
382}
383
384impl Decodable for Capabilities {
385 fn decode(buf: &mut &[u8]) -> alloy_rlp::Result<Self> {
386 let inner = Vec::<Capability>::decode(buf)?;
387
388 Ok(Self {
389 eth_66: inner.iter().any(Capability::is_eth_v66),
390 eth_67: inner.iter().any(Capability::is_eth_v67),
391 eth_68: inner.iter().any(Capability::is_eth_v68),
392 eth_69: inner.iter().any(Capability::is_eth_v69),
393 eth_70: inner.iter().any(Capability::is_eth_v70),
394 eth_71: inner.iter().any(Capability::is_eth_v71),
395 eth_72: inner.iter().any(Capability::is_eth_v72),
396 eth_73: inner.iter().any(Capability::is_eth_v73),
397 inner,
398 })
399 }
400}
401
402#[cfg(test)]
403mod tests {
404 use super::*;
405 use proptest::prelude::*;
406
407 #[test]
408 fn eth73_capability_inherits_message_versions() {
409 let capability = Capability::eth_73();
410 assert!(capability.is_eth());
411 assert!(capability.is_eth_v73());
412 let capabilities = Capabilities::new(vec![capability]);
413 assert!(capabilities.supports_eth());
414 for version in [
415 EthVersion::Eth66,
416 EthVersion::Eth67,
417 EthVersion::Eth68,
418 EthVersion::Eth69,
419 EthVersion::Eth70,
420 EthVersion::Eth71,
421 EthVersion::Eth72,
422 EthVersion::Eth73,
423 ] {
424 assert!(capabilities.supports_eth_at_least(&version));
425 }
426 let old = Capabilities::new(vec![Capability::eth_72()]);
427 assert!(!old.supports_eth_at_least(&EthVersion::Eth73));
428 }
429
430 proptest! {
431 #[test]
432 fn capabilities_rlp_roundtrip_and_length(
433 versions in proptest::collection::vec(66usize..=73, 0..32),
434 ) {
435 let capabilities = Capabilities::new(
436 versions.into_iter().map(|version| Capability::new_static("eth", version)).collect(),
437 );
438 let encoded = alloy_rlp::encode(&capabilities);
439 prop_assert_eq!(capabilities.length(), encoded.len());
440 let mut buf = encoded.as_slice();
441 let decoded = Capabilities::decode(&mut buf)?;
442 prop_assert_eq!(decoded, capabilities);
443 prop_assert!(buf.is_empty());
444 }
445 }
446}