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.
This commit is contained in:
Roman Khimov 2020-02-24 18:52:09 +03:00
parent 5c9681508b
commit 36c6b6af14

View file

@ -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.