[NOD-435] Rollback transactions if they were not commited (#475)

This commit is contained in:
Svarog 2019-11-18 11:14:29 +02:00 committed by Dan Aharoni
parent 80307d108b
commit 7284815c21
2 changed files with 10 additions and 6 deletions

View File

@ -2,11 +2,12 @@ package controllers
import (
"encoding/hex"
"net/http"
"github.com/daglabs/btcd/apiserver/apimodels"
"github.com/daglabs/btcd/apiserver/dbmodels"
"github.com/daglabs/btcd/httpserverutils"
"github.com/pkg/errors"
"net/http"
"github.com/daglabs/btcd/apiserver/database"
"github.com/daglabs/btcd/util/daghash"
@ -45,7 +46,8 @@ func GetBlockByHashHandler(blockHash string) (interface{}, error) {
return nil, httpserverutils.NewHandlerError(http.StatusNotFound, errors.New("No block with the given block hash was found"))
}
if httpserverutils.HasDBError(dbErrors) {
return nil, httpserverutils.NewErrorFromDBErrors("Some errors were encountered when loading transactions from the database:", dbResult.GetErrors())
return nil, httpserverutils.NewErrorFromDBErrors("Some errors were encountered when loading transactions from the database:",
dbResult.GetErrors())
}
return convertBlockModelToBlockResponse(block), nil
}

View File

@ -147,13 +147,13 @@ func syncSelectedParentChain(client *jsonrpc.Client) error {
// blue score in the database. If the database is empty,
// return nil.
func findHashOfBluestBlock(mustBeChainBlock bool) (*string, error) {
dbTx, err := database.DB()
db, err := database.DB()
if err != nil {
return nil, err
}
var block dbmodels.Block
dbQuery := dbTx.Order("blue_score DESC")
dbQuery := db.Order("blue_score DESC")
if mustBeChainBlock {
dbQuery = dbQuery.Where(&dbmodels.Block{IsChainBlock: true})
}
@ -226,6 +226,7 @@ func addBlock(client *jsonrpc.Client, block string, rawBlock btcjson.GetBlockVer
return err
}
dbTx := db.Begin()
defer dbTx.RollbackUnlessCommitted()
// Skip this block if it already exists.
blockExists, err := doesBlockExist(dbTx, rawBlock.Hash)
@ -703,6 +704,7 @@ func updateSelectedParentChain(removedChainHashes []string, addedChainBlocks []b
return err
}
dbTx := db.Begin()
defer dbTx.RollbackUnlessCommitted()
for _, removedHash := range removedChainHashes {
err := updateRemovedChainHashes(dbTx, removedHash)
@ -949,7 +951,7 @@ func processChainChangedMsgs() {
// canHandleChainChangedMsg checks whether we have all the necessary data
// to successfully handle a ChainChangedMsg.
func canHandleChainChangedMsg(chainChanged *jsonrpc.ChainChangedMsg) (bool, error) {
dbTx, err := database.DB()
db, err := database.DB()
if err != nil {
return false, err
}
@ -965,7 +967,7 @@ func canHandleChainChangedMsg(chainChanged *jsonrpc.ChainChangedMsg) (bool, erro
// Make sure that all the hashes exist in the database
var dbBlocks []dbmodels.Block
dbResult := dbTx.
dbResult := db.
Model(&dbmodels.Block{}).
Where("block_hash in (?)", hashesIn).
Find(&dbBlocks)