reth_db_api/models/
accounts.rs1use crate::{
4 impl_fixed_arbitrary,
5 table::{Decode, Encode},
6 DatabaseError,
7};
8use alloy_primitives::{Address, BlockNumber, StorageKey};
9use serde::{Deserialize, Serialize};
10use std::ops::{Bound, Range, RangeBounds, RangeInclusive};
11
12#[derive(
16 Debug, Default, Copy, Clone, PartialEq, Eq, Serialize, Deserialize, Ord, PartialOrd, Hash,
17)]
18pub struct BlockNumberAddress(pub (BlockNumber, Address));
19
20impl BlockNumberAddress {
21 pub fn range(range: RangeInclusive<BlockNumber>) -> Range<Self> {
25 (*range.start(), Address::ZERO).into()..(*range.end() + 1, Address::ZERO).into()
26 }
27
28 pub const fn block_number(&self) -> BlockNumber {
30 self.0 .0
31 }
32
33 pub const fn address(&self) -> Address {
35 self.0 .1
36 }
37
38 pub const fn take(self) -> (BlockNumber, Address) {
40 (self.0 .0, self.0 .1)
41 }
42}
43
44impl From<(BlockNumber, Address)> for BlockNumberAddress {
45 fn from(tpl: (u64, Address)) -> Self {
46 Self(tpl)
47 }
48}
49
50impl Encode for BlockNumberAddress {
51 type Encoded = [u8; 28];
52
53 fn encode(self) -> Self::Encoded {
54 let block_number = self.0 .0;
55 let address = self.0 .1;
56
57 let mut buf = [0u8; 28];
58
59 buf[..8].copy_from_slice(&block_number.to_be_bytes());
60 buf[8..].copy_from_slice(address.as_slice());
61 buf
62 }
63}
64
65impl Decode for BlockNumberAddress {
66 fn decode(value: &[u8]) -> Result<Self, DatabaseError> {
67 let num = u64::from_be_bytes(value[..8].try_into().map_err(|_| DatabaseError::Decode)?);
68 let hash = Address::from_slice(&value[8..]);
69 Ok(Self((num, hash)))
70 }
71}
72
73#[derive(Debug)]
76pub struct BlockNumberAddressRange {
77 pub start: Bound<BlockNumberAddress>,
79 pub end: Bound<BlockNumberAddress>,
81}
82
83impl RangeBounds<BlockNumberAddress> for BlockNumberAddressRange {
84 fn start_bound(&self) -> Bound<&BlockNumberAddress> {
85 self.start.as_ref()
86 }
87
88 fn end_bound(&self) -> Bound<&BlockNumberAddress> {
89 self.end.as_ref()
90 }
91}
92
93impl<R: RangeBounds<BlockNumber>> From<R> for BlockNumberAddressRange {
94 fn from(r: R) -> Self {
95 let start = match r.start_bound() {
96 Bound::Included(n) => Bound::Included(BlockNumberAddress((*n, Address::ZERO))),
97 Bound::Excluded(n) => Bound::Included(BlockNumberAddress((n + 1, Address::ZERO))),
98 Bound::Unbounded => Bound::Unbounded,
99 };
100
101 let end = match r.end_bound() {
102 Bound::Included(n) => Bound::Excluded(BlockNumberAddress((n + 1, Address::ZERO))),
103 Bound::Excluded(n) => Bound::Excluded(BlockNumberAddress((*n, Address::ZERO))),
104 Bound::Unbounded => Bound::Unbounded,
105 };
106
107 Self { start, end }
108 }
109}
110
111#[derive(
115 Debug, Default, Copy, Clone, PartialEq, Eq, Serialize, Deserialize, Ord, PartialOrd, Hash,
116)]
117pub struct AddressStorageKey(pub (Address, StorageKey));
118
119impl Encode for AddressStorageKey {
120 type Encoded = [u8; 52];
121
122 fn encode(self) -> Self::Encoded {
123 let address = self.0 .0;
124 let storage_key = self.0 .1;
125
126 let mut buf = [0u8; 52];
127
128 buf[..20].copy_from_slice(address.as_slice());
129 buf[20..].copy_from_slice(storage_key.as_slice());
130 buf
131 }
132}
133
134impl Decode for AddressStorageKey {
135 fn decode(value: &[u8]) -> Result<Self, DatabaseError> {
136 let address = Address::from_slice(&value[..20]);
137 let storage_key = StorageKey::from_slice(&value[20..]);
138 Ok(Self((address, storage_key)))
139 }
140}
141
142impl_fixed_arbitrary!((BlockNumberAddress, 28), (AddressStorageKey, 52));
143
144#[cfg(test)]
145mod tests {
146 use super::*;
147 use alloy_primitives::address;
148 use rand::{rng, Rng};
149
150 #[test]
151 fn test_block_number_address() {
152 let num = 1u64;
153 let hash = address!("0xba5e000000000000000000000000000000000000");
154 let key = BlockNumberAddress((num, hash));
155
156 let mut bytes = [0u8; 28];
157 bytes[..8].copy_from_slice(&num.to_be_bytes());
158 bytes[8..].copy_from_slice(hash.as_slice());
159
160 let encoded = Encode::encode(key);
161 assert_eq!(encoded, bytes);
162
163 let decoded: BlockNumberAddress = Decode::decode(&encoded).unwrap();
164 assert_eq!(decoded, key);
165 }
166
167 #[test]
168 fn test_block_number_address_rand() {
169 let mut bytes = [0u8; 28];
170 rng().fill(bytes.as_mut_slice());
171 let key = BlockNumberAddress::arbitrary(&mut Unstructured::new(&bytes)).unwrap();
172 assert_eq!(bytes, Encode::encode(key));
173 }
174
175 #[test]
176 fn test_address_storage_key() {
177 let storage_key = StorageKey::random();
178 let address = address!("0xba5e000000000000000000000000000000000000");
179 let key = AddressStorageKey((address, storage_key));
180
181 let mut bytes = [0u8; 52];
182 bytes[..20].copy_from_slice(address.as_slice());
183 bytes[20..].copy_from_slice(storage_key.as_slice());
184
185 let encoded = Encode::encode(key);
186 assert_eq!(encoded, bytes);
187
188 let decoded: AddressStorageKey = Decode::decode(&encoded).unwrap();
189 assert_eq!(decoded, key);
190 }
191
192 #[test]
193 fn test_address_storage_key_rand() {
194 let mut bytes = [0u8; 52];
195 rng().fill(bytes.as_mut_slice());
196 let key = AddressStorageKey::arbitrary(&mut Unstructured::new(&bytes)).unwrap();
197 assert_eq!(bytes, Encode::encode(key));
198 }
199}