reth_trie_parallel/
targets_v2.rs1use alloy_primitives::{map::B256Map, B256};
4use reth_trie::proof_v2;
5
6#[derive(Debug, Default)]
9pub struct MultiProofTargetsV2 {
10 pub account_targets: Vec<proof_v2::Target>,
12 pub storage_targets: B256Map<Vec<proof_v2::Target>>,
14}
15
16impl MultiProofTargetsV2 {
17 pub fn is_empty(&self) -> bool {
19 self.account_targets.is_empty() && self.storage_targets.is_empty()
20 }
21}
22
23#[derive(Debug)]
31pub struct ChunkedMultiProofTargetsV2 {
32 account_targets: std::vec::IntoIter<proof_v2::Target>,
34 storage_targets: B256Map<Vec<proof_v2::Target>>,
36 current_account_storage: Option<(B256, std::vec::IntoIter<proof_v2::Target>)>,
38 size: usize,
40}
41
42impl ChunkedMultiProofTargetsV2 {
43 pub fn new(targets: MultiProofTargetsV2, size: usize) -> Self {
45 Self {
46 account_targets: targets.account_targets.into_iter(),
47 storage_targets: targets.storage_targets,
48 current_account_storage: None,
49 size,
50 }
51 }
52}
53
54impl Iterator for ChunkedMultiProofTargetsV2 {
55 type Item = MultiProofTargetsV2;
56
57 fn next(&mut self) -> Option<Self::Item> {
58 let mut chunk = MultiProofTargetsV2::default();
59 let mut count = 0;
60
61 if let Some((account_addr, ref mut storage_iter)) = self.current_account_storage {
63 let remaining_capacity = self.size - count;
64 let slots: Vec<_> = storage_iter.by_ref().take(remaining_capacity).collect();
65
66 count += slots.len();
67 chunk.storage_targets.insert(account_addr, slots);
68
69 if storage_iter.len() == 0 {
71 self.current_account_storage = None;
72 }
73 }
74
75 while count < self.size {
77 let Some(account_target) = self.account_targets.next() else {
78 break;
79 };
80
81 chunk.account_targets.push(account_target);
83 count += 1;
84
85 let account_addr = account_target.key();
87 if let Some(storage_slots) = self.storage_targets.remove(&account_addr) {
88 let remaining_capacity = self.size - count;
89
90 if storage_slots.len() <= remaining_capacity {
91 count += storage_slots.len();
93 chunk.storage_targets.insert(account_addr, storage_slots);
94 } else {
95 let mut storage_iter = storage_slots.into_iter();
97 let slots_in_chunk: Vec<_> =
98 storage_iter.by_ref().take(remaining_capacity).collect();
99 count += slots_in_chunk.len();
100
101 chunk.storage_targets.insert(account_addr, slots_in_chunk);
102
103 self.current_account_storage = Some((account_addr, storage_iter));
105 break;
106 }
107 }
108 }
109
110 while let Some((account_addr, storage_slots)) = self.storage_targets.iter_mut().next() &&
112 count < self.size
113 {
114 let account_addr = *account_addr;
115 let storage_slots = std::mem::take(storage_slots);
116 let remaining_capacity = self.size - count;
117
118 self.storage_targets.remove(&account_addr);
121
122 if storage_slots.len() <= remaining_capacity {
123 count += storage_slots.len();
125 chunk.storage_targets.insert(account_addr, storage_slots);
126 } else {
127 let mut storage_iter = storage_slots.into_iter();
129 let slots_in_chunk: Vec<_> =
130 storage_iter.by_ref().take(remaining_capacity).collect();
131
132 chunk.storage_targets.insert(account_addr, slots_in_chunk);
133
134 if storage_iter.len() > 0 {
136 self.current_account_storage = Some((account_addr, storage_iter));
137 }
138 break;
139 }
140 }
141
142 if chunk.account_targets.is_empty() && chunk.storage_targets.is_empty() {
143 None
144 } else {
145 Some(chunk)
146 }
147 }
148}