reth_eth_wire_types/snap/
messages.rs1use crate::{snap::AccountData, BlockAccessLists};
4use alloc::vec::Vec;
5use alloy_primitives::{Bytes, B256, U256};
6use alloy_rlp::{BufMut, Decodable, Encodable, RlpDecodable, RlpEncodable};
7use reth_codecs_derive::add_arbitrary_tests;
8
9#[derive(Debug, Clone, PartialEq, Eq, RlpEncodable, RlpDecodable)]
12#[cfg_attr(any(test, feature = "arbitrary"), derive(arbitrary::Arbitrary))]
13#[add_arbitrary_tests(rlp)]
14pub struct GetAccountRangeMessage {
15 pub request_id: u64,
17 pub root_hash: B256,
19 pub starting_hash: B256,
21 pub limit_hash: B256,
23 pub response_bytes: u64,
25}
26
27#[derive(Debug, Clone, PartialEq, Eq, RlpEncodable, RlpDecodable)]
30#[cfg_attr(any(test, feature = "arbitrary"), derive(arbitrary::Arbitrary))]
31#[add_arbitrary_tests(rlp)]
32pub struct AccountRangeMessage {
33 pub request_id: u64,
35 pub accounts: Vec<AccountData>,
37 pub proof: Vec<Bytes>,
39}
40
41#[derive(Debug, Clone, PartialEq, Eq, RlpEncodable, RlpDecodable)]
44#[cfg_attr(any(test, feature = "arbitrary"), derive(arbitrary::Arbitrary))]
45#[add_arbitrary_tests(rlp)]
46pub struct GetStorageRangesMessage {
47 pub request_id: u64,
49 pub root_hash: B256,
51 pub account_hashes: Vec<B256>,
53 pub starting_hash: RangeBound,
56 pub limit_hash: RangeBound,
59 pub response_bytes: u64,
61}
62
63#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
67#[cfg_attr(any(test, feature = "arbitrary"), derive(arbitrary::Arbitrary))]
68pub struct RangeBound(Option<B256>);
69
70impl RangeBound {
71 pub const fn unwrap_or(self, default: B256) -> B256 {
73 match self.0 {
74 Some(hash) => hash,
75 None => default,
76 }
77 }
78}
79
80impl From<B256> for RangeBound {
81 fn from(hash: B256) -> Self {
82 Self(Some(hash))
83 }
84}
85
86impl Encodable for RangeBound {
87 fn encode(&self, out: &mut dyn BufMut) {
88 match self.0 {
89 Some(hash) => hash.encode(out),
90 None => Bytes::new().encode(out),
91 }
92 }
93
94 fn length(&self) -> usize {
95 match self.0 {
96 Some(hash) => hash.length(),
97 None => Bytes::new().length(),
98 }
99 }
100}
101
102impl Decodable for RangeBound {
103 fn decode(buf: &mut &[u8]) -> alloy_rlp::Result<Self> {
104 let bytes = Bytes::decode(buf)?;
105 match bytes.len() {
106 0 => Ok(Self(None)),
107 32 => Ok(Self(Some(B256::from_slice(&bytes)))),
108 _ => Err(alloy_rlp::Error::UnexpectedLength),
109 }
110 }
111}
112
113#[derive(Debug, Clone, PartialEq, Eq, RlpEncodable, RlpDecodable)]
115#[cfg_attr(any(test, feature = "arbitrary"), derive(arbitrary::Arbitrary))]
116#[add_arbitrary_tests(rlp)]
117pub struct StorageData {
118 pub hash: B256,
120 pub data: Bytes,
122}
123
124impl StorageData {
125 pub fn from_value(hash: B256, value: U256) -> Self {
127 Self { hash, data: alloy_rlp::encode(value).into() }
128 }
129
130 pub fn value(&self) -> alloy_rlp::Result<U256> {
132 alloy_rlp::decode_exact(&self.data)
133 }
134}
135
136#[derive(Debug, Clone, PartialEq, Eq, RlpEncodable, RlpDecodable)]
141#[cfg_attr(any(test, feature = "arbitrary"), derive(arbitrary::Arbitrary))]
142#[add_arbitrary_tests(rlp)]
143pub struct StorageRangesMessage {
144 pub request_id: u64,
146 pub slots: Vec<Vec<StorageData>>,
148 pub proof: Vec<Bytes>,
150}
151
152#[derive(Debug, Clone, PartialEq, Eq, RlpEncodable, RlpDecodable)]
155#[cfg_attr(any(test, feature = "arbitrary"), derive(arbitrary::Arbitrary))]
156#[add_arbitrary_tests(rlp)]
157pub struct GetByteCodesMessage {
158 pub request_id: u64,
160 pub hashes: Vec<B256>,
162 pub response_bytes: u64,
164}
165
166#[derive(Debug, Clone, PartialEq, Eq, RlpEncodable, RlpDecodable)]
169#[cfg_attr(any(test, feature = "arbitrary"), derive(arbitrary::Arbitrary))]
170#[add_arbitrary_tests(rlp)]
171pub struct ByteCodesMessage {
172 pub request_id: u64,
174 pub codes: Vec<Bytes>,
176}
177
178#[derive(Debug, Clone, PartialEq, Eq, RlpEncodable, RlpDecodable)]
180#[cfg_attr(any(test, feature = "arbitrary"), derive(arbitrary::Arbitrary))]
181#[add_arbitrary_tests(rlp)]
182pub struct GetBlockAccessListsMessage {
183 pub request_id: u64,
185 pub block_hashes: Vec<B256>,
187 pub response_bytes: u64,
189}
190
191#[derive(Debug, Clone, PartialEq, Eq, RlpEncodable, RlpDecodable)]
193#[cfg_attr(any(test, feature = "arbitrary"), derive(arbitrary::Arbitrary))]
194#[add_arbitrary_tests(rlp)]
195pub struct BlockAccessListsMessage {
196 pub request_id: u64,
198 pub block_access_lists: BlockAccessLists,
200}
201
202#[cfg(test)]
203mod tests {
204 use super::*;
205
206 #[test]
207 fn storage_data_rejects_trailing_bytes() {
208 let mut slot = StorageData::from_value(B256::repeat_byte(4), U256::ONE);
209 slot.data = [slot.data.as_ref(), &[0x00]].concat().into();
210
211 assert!(slot.value().is_err());
212 }
213}