Skip to content

Commit f78f709

Browse files
committed
add temporary HashMap for deduplicating transactions in address state
Signed-off-by: William Hankins <[email protected]>
1 parent f035970 commit f78f709

File tree

1 file changed

+9
-11
lines changed

1 file changed

+9
-11
lines changed

modules/address_state/src/state.rs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use std::{
2-
collections::HashSet,
2+
collections::{HashMap, HashSet},
33
path::{Path, PathBuf},
44
sync::Arc,
55
};
@@ -176,15 +176,12 @@ impl State {
176176
let addresses = self.volatile.window.back_mut().expect("window should never be empty");
177177

178178
// Keeps track of previous tx to avoid duplicating tx hashes or overcounting totals tx count
179-
let mut last_block: Option<u32> = None;
180-
let mut last_tx_index: Option<u16> = None;
179+
let mut seen: HashMap<Address, HashSet<TxIdentifier>> = HashMap::new();
181180

182181
for delta in deltas {
182+
let tx_id = TxIdentifier::from(delta.utxo);
183183
let entry = addresses.entry(delta.address.clone()).or_default();
184184

185-
let same_tx = last_block == Some(delta.utxo.block_number())
186-
&& last_tx_index == Some(delta.utxo.tx_index());
187-
188185
if self.config.store_info {
189186
let utxos = entry.utxos.get_or_insert(Vec::new());
190187
if delta.value.lovelace > 0 {
@@ -196,16 +193,19 @@ impl State {
196193

197194
if self.config.store_transactions {
198195
let txs = entry.transactions.get_or_insert(Vec::new());
196+
let seen_for_addr = seen.entry(delta.address.clone()).or_default();
199197

200-
if !same_tx {
201-
txs.push(TxIdentifier::from(delta.utxo));
198+
if !seen_for_addr.contains(&tx_id) {
199+
seen_for_addr.insert(tx_id);
200+
txs.push(tx_id);
202201
}
203202
}
204203

205204
if self.config.store_totals {
206205
let totals = entry.totals.get_or_insert(Vec::new());
206+
let seen_for_addr = seen.entry(delta.address.clone()).or_default();
207207

208-
if same_tx {
208+
if seen_for_addr.contains(&tx_id) {
209209
if let Some(last_total) = totals.last_mut() {
210210
// Create temporary map for summing same tx deltas efficently
211211
let mut map = ValueDeltaMap::from(last_total.clone());
@@ -216,8 +216,6 @@ impl State {
216216
totals.push(delta.value.clone());
217217
}
218218
}
219-
last_block = Some(delta.utxo.block_number());
220-
last_tx_index = Some(delta.utxo.tx_index());
221219
}
222220
}
223221

0 commit comments

Comments
 (0)