Skip to content

Commit f962966

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

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
@@ -556,7 +556,7 @@ fn filter_block_txs<T: Send>(
556556
pub(crate) struct OutPointStatus {
557557
outpoint: OutPoint,
558558
funding: Option<Height>,
559-
spending: Option<(Txid, Height)>,
559+
spending: Option<(Txid, u32, Height)>,
560560
tip: BlockHash,
561561
}
562562

@@ -569,8 +569,9 @@ impl Serialize for OutPointStatus {
569569
if let Some(funding) = &self.funding {
570570
map.serialize_entry("height", &funding)?;
571571
}
572-
if let Some((txid, height)) = &self.spending {
572+
if let Some((txid, index, height)) = &self.spending {
573573
map.serialize_entry("spender_txhash", &txid)?;
574+
map.serialize_entry("spender_vin", &index)?;
574575
map.serialize_entry("spender_height", &height)?;
575576
}
576577
map.end()
@@ -647,22 +648,26 @@ impl OutPointStatus {
647648
index: &Index,
648649
daemon: &Daemon,
649650
mempool: &Mempool,
650-
) -> Result<Option<(Txid, Height)>> {
651+
) -> Result<Option<(Txid, u32, Height)>> {
651652
let chain = index.chain();
652653
if !self.is_reorg(chain) {
653-
if let Some((_, Height::Confirmed { .. })) = &self.spending {
654+
if let Some((_, _, Height::Confirmed { .. })) = &self.spending {
654655
return Ok(self.spending);
655656
}
656657
}
657658
let spending_blockhashes = index.filter_by_spending(self.outpoint);
658659
let mut confirmed = None;
659660
daemon.for_blocks(spending_blockhashes, |blockhash, block| {
660661
for tx in block.txdata {
661-
for txi in &tx.input {
662+
for (index, txi) in (&tx.input).iter().enumerate() {
662663
if txi.previous_output == self.outpoint {
663664
// TODO: there should be only one spending input
664665
assert!(confirmed.is_none(), "double spend of {}", self.outpoint);
665-
confirmed = Some((tx.txid(), Height::from_blockhash(blockhash, chain)));
666+
confirmed = Some((
667+
tx.txid(),
668+
index as u32,
669+
Height::from_blockhash(blockhash, chain),
670+
));
666671
return;
667672
}
668673
}
@@ -671,9 +676,14 @@ impl OutPointStatus {
671676
Ok(confirmed.or_else(|| {
672677
let entries = mempool.filter_by_spending(&self.outpoint);
673678
assert!(entries.len() <= 1, "double spend of {}", self.outpoint);
674-
entries
675-
.first()
676-
.map(|entry| (entry.txid, Height::unconfirmed(entry)))
679+
entries.first().map(|entry| {
680+
for (index, txi) in entry.tx.input.iter().enumerate() {
681+
if txi.previous_output == self.outpoint {
682+
return (entry.txid, index as u32, Height::unconfirmed(entry));
683+
}
684+
}
685+
panic!();
686+
})
677687
}))
678688
}
679689
}

0 commit comments

Comments
 (0)