From 27cf70216f3d2be91be41c92194b812fb7c4795b Mon Sep 17 00:00:00 2001 From: Calvin Kim Date: Tue, 16 May 2023 17:04:12 +0900 Subject: [PATCH] blockchain: Add memoryUsage() method on UtxoEntry This change is part of the effort to add utxocache support to btcd. Getting the memory usage of an entry is very useful for the utxo cache as we need to know how much memory all the cached entries are using to guarantee a cache usage limit for the end user. --- blockchain/utxoviewpoint.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/blockchain/utxoviewpoint.go b/blockchain/utxoviewpoint.go index c66e9cdb..0a6b5552 100644 --- a/blockchain/utxoviewpoint.go +++ b/blockchain/utxoviewpoint.go @@ -71,6 +71,16 @@ func (entry *UtxoEntry) isFresh() bool { return entry.packedFlags&tfFresh == tfFresh } +// memoryUsage returns the memory usage in bytes of for the utxo entry. +// It returns 0 for a nil entry. +func (entry *UtxoEntry) memoryUsage() uint64 { + if entry == nil { + return 0 + } + + return baseEntrySize + uint64(cap(entry.pkScript)) +} + // IsCoinBase returns whether or not the output was contained in a coinbase // transaction. func (entry *UtxoEntry) IsCoinBase() bool {