From 78b158dc566ef7773c823ac5dd8d363cd86eb19a Mon Sep 17 00:00:00 2001 From: Calvin Kim Date: Wed, 6 Mar 2024 02:42:33 +0900 Subject: [PATCH] blockchain: get rid of database as an argument in fetchInputUtxos Allowing the caller to fetch from either the database or the cache resulted in inconsistencies if the cache were ever to be dirty. Removing this option eliminates this problem. --- blockchain/chain.go | 6 +++--- blockchain/utxoviewpoint.go | 14 +++++--------- blockchain/validate.go | 2 +- 3 files changed, 9 insertions(+), 13 deletions(-) diff --git a/blockchain/chain.go b/blockchain/chain.go index 19de74ab83..fd07575a1d 100644 --- a/blockchain/chain.go +++ b/blockchain/chain.go @@ -960,7 +960,7 @@ func (b *BlockChain) reorganizeChain(detachNodes, attachNodes *list.List) error // Load all of the utxos referenced by the block that aren't // already in the view. - err = view.fetchInputUtxos(nil, b.utxoCache, block) + err = view.fetchInputUtxos(b.utxoCache, block) if err != nil { return err } @@ -1027,7 +1027,7 @@ func (b *BlockChain) reorganizeChain(detachNodes, attachNodes *list.List) error // checkConnectBlock gets skipped, we still need to update the UTXO // view. if b.index.NodeStatus(n).KnownValid() { - err = view.fetchInputUtxos(nil, b.utxoCache, block) + err = view.fetchInputUtxos(b.utxoCache, block) if err != nil { return err } @@ -1089,7 +1089,7 @@ func (b *BlockChain) reorganizeChain(detachNodes, attachNodes *list.List) error // Load all of the utxos referenced by the block that aren't // already in the view. - err := view.fetchInputUtxos(nil, b.utxoCache, block) + err := view.fetchInputUtxos(b.utxoCache, block) if err != nil { return err } diff --git a/blockchain/utxoviewpoint.go b/blockchain/utxoviewpoint.go index fdd165c095..1e687431ae 100644 --- a/blockchain/utxoviewpoint.go +++ b/blockchain/utxoviewpoint.go @@ -666,15 +666,11 @@ func (view *UtxoViewpoint) findInputsToFetch(block *btcutil.Block) []wire.OutPoi // fetchInputUtxos loads the unspent transaction outputs for the inputs // referenced by the transactions in the given block into the view from the -// database or the cache as needed. In particular, referenced entries that -// are earlier in the block are added to the view and entries that are already -// in the view are not modified. -func (view *UtxoViewpoint) fetchInputUtxos(db database.DB, cache *utxoCache, block *btcutil.Block) error { - if cache != nil { - return view.fetchUtxosFromCache(cache, view.findInputsToFetch(block)) - } - // Request the input utxos from the cache. - return view.fetchUtxosMain(db, view.findInputsToFetch(block)) +// cache as needed. In particular, referenced entries that are earlier in +// the block are added to the view and entries that are already in the view +// are not modified. +func (view *UtxoViewpoint) fetchInputUtxos(cache *utxoCache, block *btcutil.Block) error { + return view.fetchUtxosFromCache(cache, view.findInputsToFetch(block)) } // NewUtxoViewpoint returns a new empty unspent transaction output view. diff --git a/blockchain/validate.go b/blockchain/validate.go index 83929810ee..641967fbf2 100644 --- a/blockchain/validate.go +++ b/blockchain/validate.go @@ -1084,7 +1084,7 @@ func (b *BlockChain) checkConnectBlock(node *blockNode, block *btcutil.Block, vi // // These utxo entries are needed for verification of things such as // transaction inputs, counting pay-to-script-hashes, and scripts. - err := view.fetchInputUtxos(nil, b.utxoCache, block) + err := view.fetchInputUtxos(b.utxoCache, block) if err != nil { return err }