Skip to content

Commit

Permalink
fix(chain): redefine relevant txs in IndexedTxGraph
Browse files Browse the repository at this point in the history
A transaction's relevancy was originally only determined by the spks
referenced by the tx's inputs and outputs. A new rule is added where if
a tx shares inputs with anything contained in TxGraph, then it should
also be considered relevant. This fixes a potential double spending
problem.
  • Loading branch information
LagginTimes committed Dec 9, 2024
1 parent bcff89d commit a0fdfbb
Showing 1 changed file with 25 additions and 7 deletions.
32 changes: 25 additions & 7 deletions crates/chain/src/indexed_tx_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,12 @@ where

let mut tx_graph = tx_graph::ChangeSet::default();
for (tx, anchors) in txs {
if self.index.is_tx_relevant(&tx) {
if self.index.is_tx_relevant(&tx)
|| self
.graph
.direct_conflicts(&tx)
.any(|(_, txid)| self.graph.get_tx(txid).is_some())
{
let txid = tx.compute_txid();
tx_graph.merge(self.graph.insert_tx(tx.clone()));
for anchor in anchors {
Expand Down Expand Up @@ -218,11 +223,18 @@ where
indexer.merge(self.index.index_tx(tx));
}

let graph = self.graph.batch_insert_unconfirmed(
txs.into_iter()
.filter(|(tx, _)| self.index.is_tx_relevant(tx))
.map(|(tx, seen_at)| (tx.clone(), seen_at)),
);
let mut relevant_txs = Vec::new();
for (tx, seen_at) in txs.into_iter() {
if self.index.is_tx_relevant(&tx)
|| self
.graph
.direct_conflicts(&tx)
.any(|(_, txid)| self.graph.get_tx(txid).is_some())
{
relevant_txs.push((tx.clone(), seen_at));
}
}
let graph = self.graph.batch_insert_unconfirmed(relevant_txs);

ChangeSet {
tx_graph: graph,
Expand Down Expand Up @@ -278,7 +290,13 @@ where
let mut changeset = ChangeSet::<A, I::ChangeSet>::default();
for (tx_pos, tx) in block.txdata.iter().enumerate() {
changeset.indexer.merge(self.index.index_tx(tx));
if self.index.is_tx_relevant(tx) {

if self.index.is_tx_relevant(tx)
|| self
.graph
.direct_conflicts(tx)
.any(|(_, txid)| self.graph.get_tx(txid).is_some())
{
let txid = tx.compute_txid();
let anchor = TxPosInBlock {
block,
Expand Down

0 comments on commit a0fdfbb

Please sign in to comment.