[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 ( import (
"encoding/hex" "encoding/hex"
"net/http"
"github.com/daglabs/btcd/apiserver/apimodels" "github.com/daglabs/btcd/apiserver/apimodels"
"github.com/daglabs/btcd/apiserver/dbmodels" "github.com/daglabs/btcd/apiserver/dbmodels"
"github.com/daglabs/btcd/httpserverutils" "github.com/daglabs/btcd/httpserverutils"
"github.com/pkg/errors" "github.com/pkg/errors"
"net/http"
"github.com/daglabs/btcd/apiserver/database" "github.com/daglabs/btcd/apiserver/database"
"github.com/daglabs/btcd/util/daghash" "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")) return nil, httpserverutils.NewHandlerError(http.StatusNotFound, errors.New("No block with the given block hash was found"))
} }
if httpserverutils.HasDBError(dbErrors) { 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 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, // blue score in the database. If the database is empty,
// return nil. // return nil.
func findHashOfBluestBlock(mustBeChainBlock bool) (*string, error) { func findHashOfBluestBlock(mustBeChainBlock bool) (*string, error) {
dbTx, err := database.DB() db, err := database.DB()
if err != nil { if err != nil {
return nil, err return nil, err
} }
var block dbmodels.Block var block dbmodels.Block
dbQuery := dbTx.Order("blue_score DESC") dbQuery := db.Order("blue_score DESC")
if mustBeChainBlock { if mustBeChainBlock {
dbQuery = dbQuery.Where(&dbmodels.Block{IsChainBlock: true}) dbQuery = dbQuery.Where(&dbmodels.Block{IsChainBlock: true})
} }
@ -226,6 +226,7 @@ func addBlock(client *jsonrpc.Client, block string, rawBlock btcjson.GetBlockVer
return err return err
} }
dbTx := db.Begin() dbTx := db.Begin()
defer dbTx.RollbackUnlessCommitted()
// Skip this block if it already exists. // Skip this block if it already exists.
blockExists, err := doesBlockExist(dbTx, rawBlock.Hash) blockExists, err := doesBlockExist(dbTx, rawBlock.Hash)
@ -703,6 +704,7 @@ func updateSelectedParentChain(removedChainHashes []string, addedChainBlocks []b
return err return err
} }
dbTx := db.Begin() dbTx := db.Begin()
defer dbTx.RollbackUnlessCommitted()
for _, removedHash := range removedChainHashes { for _, removedHash := range removedChainHashes {
err := updateRemovedChainHashes(dbTx, removedHash) err := updateRemovedChainHashes(dbTx, removedHash)
@ -949,7 +951,7 @@ func processChainChangedMsgs() {
// canHandleChainChangedMsg checks whether we have all the necessary data // canHandleChainChangedMsg checks whether we have all the necessary data
// to successfully handle a ChainChangedMsg. // to successfully handle a ChainChangedMsg.
func canHandleChainChangedMsg(chainChanged *jsonrpc.ChainChangedMsg) (bool, error) { func canHandleChainChangedMsg(chainChanged *jsonrpc.ChainChangedMsg) (bool, error) {
dbTx, err := database.DB() db, err := database.DB()
if err != nil { if err != nil {
return false, err return false, err
} }
@ -965,7 +967,7 @@ func canHandleChainChangedMsg(chainChanged *jsonrpc.ChainChangedMsg) (bool, erro
// Make sure that all the hashes exist in the database // Make sure that all the hashes exist in the database
var dbBlocks []dbmodels.Block var dbBlocks []dbmodels.Block
dbResult := dbTx. dbResult := db.
Model(&dbmodels.Block{}). Model(&dbmodels.Block{}).
Where("block_hash in (?)", hashesIn). Where("block_hash in (?)", hashesIn).
Find(&dbBlocks) Find(&dbBlocks)