blockchain: Flush UtxoCache when latch to current.

This forces the UTXO cache to flush to the database when the chain just
latched to current.  This ensures that the full UTXO set is always
flushed to the database when the chain becomes current, which allows for
fetching up-to-date UTXO set stats.

The behavior before these changes is that the UTXO cache will not be
forced to flush to the database until a block is connected AFTER the
chain is already latched to current.  Therefore, if you fetch UTXO stats
when the chain is current but the next block has not been connected yet,
the UTXO stats may not be up to date.
This commit is contained in:
Ryan Staudt 2021-06-19 08:30:35 -05:00 committed by Dave Collins
parent f98e374037
commit dbc6eeb56b

View File

@ -1310,8 +1310,21 @@ func (b *BlockChain) reorganizeChain(target *blockNode) error {
// actually is here versus using the one from above since it might not match
// reality if there were errors while reorganizing.
newTip := b.bestChain.Tip()
wasLatched := b.isCurrentLatch
b.maybeUpdateIsCurrent(newTip)
// If the chain just latched to current, force the UTXO cache to flush to the
// database. This ensures that the full UTXO set is always flushed to the
// database when the chain becomes current, which allows for fetching
// up-to-date UTXO set stats.
if !wasLatched && b.isCurrentLatch {
err := b.utxoCache.MaybeFlush(&newTip.hash, uint32(newTip.height), true,
true)
if err != nil {
return err
}
}
// Log chain reorganizations and send a notification as needed.
if sentReorgingNtfn && newTip != origTip {
// Send a notification that a chain reorganization took place.