Skip to content

Commit

Permalink
Merge pull request Blockstream#94 from mempool/mononaut/fix-summary-t…
Browse files Browse the repository at this point in the history
…x-ordering

Fix summary tx ordering
  • Loading branch information
junderw authored May 14, 2024
2 parents 3dab4c7 + f7b77a3 commit 7fc4912
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions src/new_index/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -533,23 +533,23 @@ impl ChainQuery {
let rows = self
.history_iter_scan_reverse(code, hash)
.map(TxHistoryRow::from_row)
.map(|row| (row.get_txid(), row.key.txinfo))
.skip_while(|(txid, _)| {
.map(|row| (row.get_txid(), row.key.txinfo, row.key.tx_position))
.skip_while(|(txid, _, _)| {
// skip until we reach the last_seen_txid
last_seen_txid.map_or(false, |last_seen_txid| last_seen_txid != txid)
})
.skip_while(|(txid, _)| {
.skip_while(|(txid, _, _)| {
// skip the last_seen_txid itself
last_seen_txid.map_or(false, |last_seen_txid| last_seen_txid == txid)
})
.filter_map(|(txid, info)| {
.filter_map(|(txid, info, tx_position)| {
self.tx_confirming_block(&txid)
.map(|b| (txid, info, b.height, b.time))
.map(|b| (txid, info, b.height, b.time, tx_position))
});

// collate utxo funding/spending events by transaction
let mut map: HashMap<Txid, TxHistorySummary> = HashMap::new();
for (txid, info, height, time) in rows {
for (txid, info, height, time, tx_position) in rows {
if !map.contains_key(&txid) && map.len() == limit {
break;
}
Expand All @@ -565,6 +565,7 @@ impl ChainQuery {
value: info.value.try_into().unwrap_or(0),
height,
time,
tx_position,
});
}
#[cfg(not(feature = "liquid"))]
Expand All @@ -578,6 +579,7 @@ impl ChainQuery {
value: 0_i64.saturating_sub(info.value.try_into().unwrap_or(0)),
height,
time,
tx_position,
});
}
#[cfg(feature = "liquid")]
Expand All @@ -587,6 +589,7 @@ impl ChainQuery {
value: 0,
height,
time,
tx_position,
});
}
#[cfg(feature = "liquid")]
Expand All @@ -596,6 +599,7 @@ impl ChainQuery {
value: 0,
height,
time,
tx_position,
});
}
#[cfg(feature = "liquid")]
Expand All @@ -606,7 +610,11 @@ impl ChainQuery {
let mut tx_summaries = map.into_values().collect::<Vec<TxHistorySummary>>();
tx_summaries.sort_by(|a, b| {
if a.height == b.height {
a.value.cmp(&b.value)
if a.tx_position == b.tx_position {
a.value.cmp(&b.value)
} else {
b.tx_position.cmp(&a.tx_position)
}
} else {
b.height.cmp(&a.height)
}
Expand Down Expand Up @@ -1702,6 +1710,7 @@ pub struct TxHistorySummary {
height: usize,
value: i64,
time: u32,
tx_position: u16,
}

#[derive(Serialize, Deserialize)]
Expand Down

0 comments on commit 7fc4912

Please sign in to comment.