Skip to content

Commit fa67893

Browse files
committed
add spender_vin return value to blockchain.outpoint.subscribe
see romanz#788 and spesmilo/electrumx#90 (comment)
1 parent 834bbd7 commit fa67893

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

src/status.rs

+19-9
Original file line numberDiff line numberDiff line change
@@ -555,7 +555,7 @@ fn filter_block_txs<T: Send>(
555555
pub(crate) struct OutPointStatus {
556556
outpoint: OutPoint,
557557
funding: Option<Height>,
558-
spending: Option<(Txid, Height)>,
558+
spending: Option<(Txid, u32, Height)>,
559559
tip: BlockHash,
560560
}
561561

@@ -568,8 +568,9 @@ impl Serialize for OutPointStatus {
568568
if let Some(funding) = &self.funding {
569569
map.serialize_entry("height", &funding)?;
570570
}
571-
if let Some((txid, height)) = &self.spending {
571+
if let Some((txid, index, height)) = &self.spending {
572572
map.serialize_entry("spender_txhash", &txid)?;
573+
map.serialize_entry("spender_vin", &index)?;
573574
map.serialize_entry("spender_height", &height)?;
574575
}
575576
map.end()
@@ -642,22 +643,26 @@ impl OutPointStatus {
642643
index: &Index,
643644
daemon: &Daemon,
644645
mempool: &Mempool,
645-
) -> Result<Option<(Txid, Height)>> {
646+
) -> Result<Option<(Txid, u32, Height)>> {
646647
let chain = index.chain();
647648
if !self.is_reorg(chain) {
648-
if let Some((_, Height::Confirmed { .. })) = &self.spending {
649+
if let Some((_, _, Height::Confirmed { .. })) = &self.spending {
649650
return Ok(self.spending);
650651
}
651652
}
652653
let spending_blockhashes = index.filter_by_spending(self.outpoint);
653654
let mut confirmed = None;
654655
daemon.for_blocks(spending_blockhashes, |blockhash, block| {
655656
for tx in block.txdata {
656-
for txi in &tx.input {
657+
for (index, txi) in (&tx.input).iter().enumerate() {
657658
if txi.previous_output == self.outpoint {
658659
// TODO: there should be only one spending input
659660
assert!(confirmed.is_none(), "double spend of {}", self.outpoint);
660-
confirmed = Some((tx.txid(), Height::from_blockhash(blockhash, chain)));
661+
confirmed = Some((
662+
tx.txid(),
663+
index as u32,
664+
Height::from_blockhash(blockhash, chain),
665+
));
661666
return;
662667
}
663668
}
@@ -666,9 +671,14 @@ impl OutPointStatus {
666671
Ok(confirmed.or_else(|| {
667672
let entries = mempool.filter_by_spending(&self.outpoint);
668673
assert!(entries.len() <= 1, "double spend of {}", self.outpoint);
669-
entries
670-
.first()
671-
.map(|entry| (entry.txid, Height::unconfirmed(entry)))
674+
entries.first().map(|entry| {
675+
for (index, txi) in entry.tx.input.iter().enumerate() {
676+
if txi.previous_output == self.outpoint {
677+
return (entry.txid, index as u32, Height::unconfirmed(entry));
678+
}
679+
}
680+
panic!();
681+
})
672682
}))
673683
}
674684
}

0 commit comments

Comments
 (0)