diff --git a/lib/ain-ocean/src/indexer/mod.rs b/lib/ain-ocean/src/indexer/mod.rs index da9d98e34f..a964630978 100644 --- a/lib/ain-ocean/src/indexer/mod.rs +++ b/lib/ain-ocean/src/indexer/mod.rs @@ -141,18 +141,21 @@ fn index_script_aggregation_vin( record: &mut BTreeMap<[u8; 32], ScriptAggregation>, ) { let hid = as_sha256(&vout.script.hex); - let entry = record.entry(hid).or_insert_with(|| ScriptAggregation { - hid, - block: block.clone(), - script: ScriptAggregationScript { - r#type: vout.script.r#type.clone(), - hex: vout.script.hex.clone(), - }, - statistic: ScriptAggregationStatistic::default(), - amount: ScriptAggregationAmount::default(), - }); - entry.statistic.tx_out_count += 1; - entry.amount.tx_out += vout.value; + record.entry(hid) + .and_modify(|script| { + script.statistic.tx_out_count += 1; + script.amount.tx_out += vout.value; + }) + .or_insert_with(|| ScriptAggregation { + hid, + block: block.clone(), + script: ScriptAggregationScript { + r#type: vout.script.r#type.clone(), + hex: vout.script.hex.clone(), + }, + statistic: ScriptAggregationStatistic::default(), + amount: ScriptAggregationAmount::default(), + }); } fn index_script_unspent_vin( @@ -219,18 +222,21 @@ fn index_script_aggregation_vout( ) { let hid = as_sha256(&vout.script_pub_key.hex); - let entry = record.entry(hid).or_insert_with(|| ScriptAggregation { - hid, - block: block.clone(), - script: ScriptAggregationScript { - r#type: vout.script_pub_key.r#type.clone(), - hex: vout.script_pub_key.hex.clone(), - }, - statistic: ScriptAggregationStatistic::default(), - amount: ScriptAggregationAmount::default(), - }); - entry.statistic.tx_in_count += 1; - entry.amount.tx_in += vout.value; + record.entry(hid) + .and_modify(|script| { + script.statistic.tx_in_count += 1; + script.amount.tx_in += vout.value; + }) + .or_insert_with(|| ScriptAggregation { + hid, + block: block.clone(), + script: ScriptAggregationScript { + r#type: vout.script_pub_key.r#type.clone(), + hex: vout.script_pub_key.hex.clone(), + }, + statistic: ScriptAggregationStatistic::default(), + amount: ScriptAggregationAmount::default(), + }); } fn index_script_unspent_vout(services: &Arc, vout: &Vout, ctx: &Context) -> Result<()> { @@ -354,8 +360,6 @@ fn index_script(services: &Arc, ctx: &Context, txs: &[Transaction]) -> &(aggregation.hid, ctx.block.height.to_be_bytes()), &aggregation, )?; - - record.insert(aggregation.hid, aggregation); } log_elapsed(start, format!("Indexed script {:x}", ctx.tx.txid));