blockchain: Add tfFresh to txoFlags

This change is part of the effort to add utxocache support to btcd.

The fresh flag indicates that the entry is fresh and that the parent
view (the database) hasn't yet seen the entry.  This is very useful as
a performance optimization for the utxo cache as if a fresh entry is
spent, we can simply remove it from the cache and don't bother trying to
delete it from the database.
This commit is contained in:
Calvin Kim 2023-05-16 16:46:05 +09:00
parent 1f2dfa2d6e
commit 35c42688d9

View file

@ -28,6 +28,13 @@ const (
// tfModified indicates that a txout has been modified since it was // tfModified indicates that a txout has been modified since it was
// loaded. // loaded.
tfModified tfModified
// tfFresh indicates that the entry is fresh. This means that the parent
// view never saw this entry. Note that tfFresh is a performance
// optimization with which we can erase entries that are fully spent if we
// know we do not need to commit them. It is always safe to not mark
// tfFresh if that condition is not guaranteed.
tfFresh
) )
// UtxoEntry houses details about an individual transaction output in a utxo // UtxoEntry houses details about an individual transaction output in a utxo
@ -58,6 +65,12 @@ func (entry *UtxoEntry) isModified() bool {
return entry.packedFlags&tfModified == tfModified return entry.packedFlags&tfModified == tfModified
} }
// isFresh returns whether or not it's certain the output has never previously
// been stored in the database.
func (entry *UtxoEntry) isFresh() bool {
return entry.packedFlags&tfFresh == tfFresh
}
// IsCoinBase returns whether or not the output was contained in a coinbase // IsCoinBase returns whether or not the output was contained in a coinbase
// transaction. // transaction.
func (entry *UtxoEntry) IsCoinBase() bool { func (entry *UtxoEntry) IsCoinBase() bool {
@ -199,7 +212,7 @@ func (view *UtxoViewpoint) addTxOut(outpoint wire.OutPoint, txOut *wire.TxOut, i
entry.amount = txOut.Value entry.amount = txOut.Value
entry.pkScript = txOut.PkScript entry.pkScript = txOut.PkScript
entry.blockHeight = blockHeight entry.blockHeight = blockHeight
entry.packedFlags = tfModified entry.packedFlags = tfFresh | tfModified
if isCoinBase { if isCoinBase {
entry.packedFlags |= tfCoinBase entry.packedFlags |= tfCoinBase
} }