Add a "tx output spender" index

Adds an outpoint -> txid index, which can be used to find which transactions spent a given output.
We use a composite key with 2 parts (suggested by @romanz): hash(spent outpoint) and tx position, with an empty value.
To find the spending tx for a given outpoint, we do a prefix search (prefix being the hash of the provided outpoint), and for all keys that match this prefix
we load the tx at the position specified in the key and return it, along with the block hash, if does spend the provided outpoint.
To handle reorgs we just erase the keys computed from the removed block.

This index is extremely useful for Lightning and more generally for layer-2 protocols that rely on chains of unpublished transactions.
If enabled, this index will be used by `gettxspendingprevout` when it does not find a spending transaction in the mempool.
This commit is contained in:
sstone
2022-03-10 10:26:57 +01:00
committed by sstone
parent 2706758dc3
commit 3d82ec5bdd
18 changed files with 611 additions and 105 deletions

View File

@@ -55,6 +55,7 @@ Subdirectory | File(s) | Description
`blocks/` | `xor.dat` | Rolling XOR pattern for block and undo data files
`chainstate/` | LevelDB database | Blockchain state (a compact representation of all currently unspent transaction outputs (UTXOs) and metadata about the transactions they are from)
`indexes/txindex/` | LevelDB database | Transaction index; *optional*, used if `-txindex=1`
`indexes/txospenderindex/` | LevelDB database | Transaction spender index; *optional*, used if `-txospenderindex=1`
`indexes/blockfilter/basic/db/` | LevelDB database | Blockfilter index LevelDB database for the basic filtertype; *optional*, used if `-blockfilterindex=basic`
`indexes/blockfilter/basic/` | `fltrNNNNN.dat`<sup>[\[2\]](#note2)</sup> | Blockfilter index filters for the basic filtertype; *optional*, used if `-blockfilterindex=basic`
`indexes/coinstatsindex/db/` | LevelDB database | Coinstats index; *optional*, used if `-coinstatsindex=1`

View File

@@ -0,0 +1,14 @@
New settings
------------
- `-txospenderindex` enables the creation of a transaction output spender
index that, if present, will be scanned by `gettxspendingprevout` if a
spending transaction was not found in the mempool.
(#24539)
Updated RPCs
------------
- `gettxspendingprevout` has 2 new optional arguments: `mempool_only` and `return_spending_tx`.
If `mempool_only` is true it will limit scans to the mempool even if `txospenderindex` is available.
If `return_spending_tx` is true, the full spending tx will be returned.
In addition if `txospenderindex` is available and a confirmed spending transaction is found,
its block hash will be returned. (#24539)