The current built-in node crashes with a `Couldn't find coin for input` error when a block containing txs gets reorged in some situations.
Here's what we think happened:happened (with the bug-relevant bits bold):
1. Transaction T got added to the mempoolConnected block A, which contained tx T
2. ConnectReceived block AB, mining Twhich also contained T
3. Also connAvalanche selected block B, which also contained the tx to be finalized
4. Avalanche selNode disconnected block B to be finalizedA (Chronik didn't receive this yet)
5. **Node disconnected block A (Chronik didn't receive this yetadded T back to the mempool** (due to the re-org)
6. Node connected block B, removing T and **marking its coins as spent**
7. Only now Chronik disconnects block A
8. Chronik adds T back to the mempool (due to the re-org), however, FindCoins failed for the inputs.
It seems like the issue might be that the node already reorged the block and because we have a queue when processing tasks in the validation interface. Chronik disconnecte**FindCoins can't find the block latercoins anymore, which might have caused FindCoins to failas they've been spent in step 6**.
To fix this, we thread the spent_coins from the node to the validation interface queue, by adding spent_coins to TransactionAddedToMempool. This way Chronik is guaranteed to have the spent coins ready without having to query them from the node.
We use a `std::shared_ptr<const std::vector<Coin>>`, to ensure coins aren't freed and reused before they're used by the indexer. The interfaces take an owned shared_ptr, to make sure the memory isn't freed too early. In the (relatively far) future, we might move `spent_coins` into `CTransaction`, which would manage the memory for us.
List of changes:
1. Add (Rust) `CCoin` to the FFI pointing to C++'s `Coin`.
2. Change `bridge_tx` to take a vector of spent coins instead of `ChronikBridge` (and fix the bug).
3. Thread the spent coins to handle_tx_added_to_mempool and feed them to bridge_tx.
Depends on D14386.