HomePhabricator

[Chronik] Fix: Race condition when re-orging a block with txs

Description

[Chronik] Fix: Race condition when re-orging a block with txs

Summary:
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 happened (with the bug-relevant bits bold):

  1. Connected block A, which contained tx T
  2. Received block B, which also contained T
  3. Avalanche selected block B to be finalized
  4. Node disconnected block A (Chronik didn't receive this yet)
  5. Node added 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 can't find the coins anymore, as 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.

Test Plan: ninja check-functional

Reviewers: Fabien, #bitcoin_abc

Reviewed By: Fabien, #bitcoin_abc

Differential Revision: https://reviews.bitcoinabc.org/D14361

Details

Provenance
tobias_ruckAuthored on Aug 18 2023, 06:11
tobias_ruckPushed on Aug 18 2023, 15:37
Reviewer
Restricted Project
Differential Revision
D14361: [Chronik] Fix: Race condition when re-orging a block with txs
Parents
rABC49de3da617bb: [Chronik] Add `/pause` and `/resume` endpoints
Branches
Unknown
Tags
Unknown