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

@@ -100,7 +100,7 @@ class RpcMiscTest(BitcoinTestFramework):
assert_equal(node.getindexinfo(), {})
# Restart the node with indices and wait for them to sync
self.restart_node(0, ["-txindex", "-blockfilterindex", "-coinstatsindex"])
self.restart_node(0, ["-txindex", "-blockfilterindex", "-coinstatsindex", "-txospenderindex"])
self.wait_until(lambda: all(i["synced"] for i in node.getindexinfo().values()))
# Returns a list of all running indices by default
@@ -111,10 +111,11 @@ class RpcMiscTest(BitcoinTestFramework):
"txindex": values,
"basic block filter index": values,
"coinstatsindex": values,
"txospenderindex": values
}
)
# Specifying an index by name returns only the status of that index
for i in {"txindex", "basic block filter index", "coinstatsindex"}:
for i in {"txindex", "basic block filter index", "coinstatsindex", "txospenderindex"}:
assert_equal(node.getindexinfo(i), {i: values})
# Specifying an unknown index name returns an empty result