txdb: fix lockedUnconfirmed if FINALIZE is inserted with block #512
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This is an edge case we should have addressed in #464 especially after dealing with basically the same behavior in #387. And actually if I'd written better test coverage in #387 to actually include TRANSFER and FINALIZE, we probably would've fixed all the bugs six months ago 🤷♂️ 😬
The issue is that in bcoin and hsd,
unconfirmed
includes all confirmed amounts in addition to unconfirmed amounts, they are not mutually exclusive. Normally when we send a TX, it is inserted into the database as unconfirmed. Then when we see it in a block, we update the confirmed balance but we leave the unconfirmed balance alone (it was already updated when the TX was first seen in the mempool).During a rescan (or more explicitly, an import-from-seed or deep rescan) we never see the TX as unconfirmed -- it is inserted into the database with the block that confirms it. So to keep the
unconfirmed
balance in sync, we need to update that value first, and then continue with the confirmation process that updates the confirmed balance.