From 36c6b6af140a6d7698941ffe63548df94738331b Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Mon, 24 Feb 2020 18:52:09 +0300 Subject: [PATCH] core: put unspents get/put one loop above in storeBlock Everywhere in this code prevHash == input.PrevHash, thus we can easily move some common code out of the loop saving on DB accesses and serialization/deserialization. --- pkg/core/blockchain.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/pkg/core/blockchain.go b/pkg/core/blockchain.go index 3b4f5b276..f1cf536ac 100644 --- a/pkg/core/blockchain.go +++ b/pkg/core/blockchain.go @@ -436,15 +436,12 @@ func (bc *Blockchain) storeBlock(block *block.Block) error { if err != nil { return fmt.Errorf("could not find previous TX: %s", prevHash) } + unspent, err := cache.GetUnspentCoinStateOrNew(prevHash) + if err != nil { + return err + } for _, input := range inputs { - unspent, err := cache.GetUnspentCoinStateOrNew(input.PrevHash) - if err != nil { - return err - } unspent.states[input.PrevIndex] = state.CoinSpent - if err = cache.PutUnspentCoinState(input.PrevHash, unspent); err != nil { - return err - } prevTXOutput := prevTX.Outputs[input.PrevIndex] account, err := cache.GetAccountStateOrNew(prevTXOutput.ScriptHash) if err != nil { @@ -482,6 +479,9 @@ func (bc *Blockchain) storeBlock(block *block.Block) error { return err } } + if err = cache.PutUnspentCoinState(prevHash, unspent); err != nil { + return err + } } // Process the underlying type of the TX.