btcsuite / btcwallet

A secure bitcoin wallet daemon written in Go (golang)
ISC License
1.15k stars 590 forks source link

chain: remove redundant `Add(tx)` in zmq event #877

Open yyforyongyu opened 1 year ago

yyforyongyu commented 1 year ago

This commit removes adding tx to mempool when received from rawtx zmq to prevent the same tx gets to be added multiple times since it will be notified multiple times. This also means we need to send the new tx in mempoolPoller to make sure the subscriber won't miss the event.

In details, when we subscribe to an input spend via NotifySpent, we do two things,

  1. register it via channel rescanUpdate,
  2. a quick lookup in our local mempool, if found return it

Meanwhile, filterTx has two relevant logic steps that,

  1. when an unconfirmed tx is received multiple times, only the first one will be processed.
  2. when processing the tx, it will notify if the tx is relevant, meaning it's registered via rescanUpdate.

In our second good case, if the tx is received via rawtx, then it will be sent to filterTx, but not registered via rescanUpdate. However, as long as the mempool poller also receives it and saves it to the local mempool, by the time we subscribe it, it will be found via step 2 in NotifySpent.

As in our bad case, when the tx is processed by filterTx, it won't be notified due to it's not registered, so not relevant. Then we subscribe, which registers it. However, by the time the mempool sees the tx and sends it to filterTx again, it will be ignored because filterTx has already seen it.