reth_trie_common/
target_v2.rs1use crate::Nibbles;
4use alloc::vec::Vec;
5use alloy_primitives::{keccak256, map::B256Map, B256};
6use revm::state::EvmState;
7
8#[derive(Debug, Copy, Clone)]
11pub struct ProofV2Target {
12 pub key_nibbles: Nibbles,
14 pub parent: ProofV2TargetParent,
16}
17
18impl ProofV2Target {
19 pub fn new(key: B256) -> Self {
22 let key_nibbles = unsafe { Nibbles::unpack_unchecked(key.as_slice()) };
24 Self { key_nibbles, parent: ProofV2TargetParent::NONE }
25 }
26
27 pub fn key(&self) -> B256 {
29 B256::from_slice(&self.key_nibbles.pack())
30 }
31
32 pub const fn with_parent(mut self, parent: ProofV2TargetParent) -> Self {
34 self.parent = parent;
35 self
36 }
37}
38
39impl From<B256> for ProofV2Target {
40 fn from(key: B256) -> Self {
41 Self::new(key)
42 }
43}
44
45#[derive(Debug, Default, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
56pub struct ProofV2TargetParent(Option<u8>);
57
58impl ProofV2TargetParent {
59 pub const NONE: Self = Self(None);
61
62 pub const fn new(path_len: usize) -> Self {
68 assert!(path_len < 64, "parent path length must be less than 64");
69 Self(Some(path_len as u8))
70 }
71
72 pub const fn is_known(self) -> bool {
74 self.0.is_some()
75 }
76
77 pub const fn path_len(self) -> Option<usize> {
79 match self.0 {
80 Some(path_len) => Some(path_len as usize),
81 None => None,
82 }
83 }
84
85 pub fn path(self, mut target_path: Nibbles) -> Option<Nibbles> {
87 target_path.truncate(self.path_len()?);
88 Some(target_path)
89 }
90}
91
92#[derive(Debug, Default)]
95pub struct MultiProofTargetsV2 {
96 pub account_targets: Vec<ProofV2Target>,
98 pub storage_targets: B256Map<Vec<ProofV2Target>>,
100}
101
102impl MultiProofTargetsV2 {
103 pub fn is_empty(&self) -> bool {
105 self.account_targets.is_empty() && self.storage_targets.is_empty()
106 }
107
108 pub fn chunking_length(&self) -> usize {
110 self.account_targets.len() +
111 self.storage_targets.values().map(|slots| slots.len()).sum::<usize>()
112 }
113
114 pub fn chunks(self, chunk_size: usize) -> impl Iterator<Item = Self> {
116 ChunkedMultiProofTargetsV2::new(self, chunk_size)
117 }
118
119 pub fn from_state(state: EvmState) -> (Self, usize) {
122 let mut targets = Self::default();
123 targets.account_targets.reserve(state.len());
124 targets.storage_targets.reserve(state.len());
125 let mut storage_target_count = 0;
126 for (addr, account) in state {
127 if !account.is_touched() || account.is_selfdestructed() {
135 continue
136 }
137
138 let hashed_address = keccak256(addr);
139
140 if account.info != account.original_info() {
141 targets.account_targets.push(hashed_address.into());
142 }
143
144 let mut storage_slots = Vec::with_capacity(account.storage.len());
145 for (key, slot) in account.storage {
146 if !slot.is_changed() {
148 continue
149 }
150
151 let hashed_slot = keccak256(B256::new(key.to_be_bytes()));
152 storage_slots.push(ProofV2Target::from(hashed_slot));
153 }
154
155 storage_target_count += storage_slots.len();
156 if !storage_slots.is_empty() {
157 targets.storage_targets.insert(hashed_address, storage_slots);
158 }
159 }
160
161 (targets, storage_target_count)
162 }
163}
164
165#[derive(Debug)]
173pub struct ChunkedMultiProofTargetsV2 {
174 account_targets: alloc::vec::IntoIter<ProofV2Target>,
176 storage_targets: B256Map<Vec<ProofV2Target>>,
178 current_account_storage: Option<(B256, alloc::vec::IntoIter<ProofV2Target>)>,
180 size: usize,
182}
183
184impl ChunkedMultiProofTargetsV2 {
185 pub fn new(targets: MultiProofTargetsV2, size: usize) -> Self {
187 Self {
188 account_targets: targets.account_targets.into_iter(),
189 storage_targets: targets.storage_targets,
190 current_account_storage: None,
191 size,
192 }
193 }
194}
195
196impl Iterator for ChunkedMultiProofTargetsV2 {
197 type Item = MultiProofTargetsV2;
198
199 fn next(&mut self) -> Option<Self::Item> {
200 let mut chunk = MultiProofTargetsV2::default();
201 let mut count = 0;
202
203 if let Some((account_addr, ref mut storage_iter)) = self.current_account_storage {
205 let remaining_capacity = self.size - count;
206 let slots: Vec<_> = storage_iter.by_ref().take(remaining_capacity).collect();
207
208 count += slots.len();
209 chunk.storage_targets.insert(account_addr, slots);
210
211 if storage_iter.len() == 0 {
213 self.current_account_storage = None;
214 }
215 }
216
217 while count < self.size {
219 let Some(account_target) = self.account_targets.next() else {
220 break;
221 };
222
223 chunk.account_targets.push(account_target);
225 count += 1;
226
227 let account_addr = account_target.key();
229 if let Some(storage_slots) = self.storage_targets.remove(&account_addr) {
230 let remaining_capacity = self.size - count;
231
232 if storage_slots.len() <= remaining_capacity {
233 count += storage_slots.len();
235 chunk.storage_targets.insert(account_addr, storage_slots);
236 } else {
237 let mut storage_iter = storage_slots.into_iter();
239 let slots_in_chunk: Vec<_> =
240 storage_iter.by_ref().take(remaining_capacity).collect();
241 count += slots_in_chunk.len();
242
243 chunk.storage_targets.insert(account_addr, slots_in_chunk);
244
245 self.current_account_storage = Some((account_addr, storage_iter));
247 break;
248 }
249 }
250 }
251
252 while let Some((account_addr, storage_slots)) = self.storage_targets.iter_mut().next() &&
254 count < self.size
255 {
256 let account_addr = *account_addr;
257 let storage_slots = core::mem::take(storage_slots);
258 let remaining_capacity = self.size - count;
259
260 self.storage_targets.remove(&account_addr);
263
264 if storage_slots.len() <= remaining_capacity {
265 count += storage_slots.len();
267 chunk.storage_targets.insert(account_addr, storage_slots);
268 } else {
269 let mut storage_iter = storage_slots.into_iter();
271 let slots_in_chunk: Vec<_> =
272 storage_iter.by_ref().take(remaining_capacity).collect();
273
274 chunk.storage_targets.insert(account_addr, slots_in_chunk);
275
276 if storage_iter.len() > 0 {
278 self.current_account_storage = Some((account_addr, storage_iter));
279 }
280 break;
281 }
282 }
283
284 if chunk.account_targets.is_empty() && chunk.storage_targets.is_empty() {
285 None
286 } else {
287 Some(chunk)
288 }
289 }
290}