Improve I/O error propagation

This commit is contained in:
Edvard
2019-02-14 16:03:57 +01:00
parent f1d6e417fe
commit 6cd4e5ea95
18 changed files with 180 additions and 98 deletions
+26 -6
View File
@@ -17,6 +17,7 @@
package history
import (
"github.com/sirupsen/logrus"
"github.com/vulcanize/vulcanizedb/pkg/core"
"github.com/vulcanize/vulcanizedb/pkg/datastore"
)
@@ -35,11 +36,30 @@ func NewBlockValidator(blockchain core.BlockChain, blockRepository datastore.Blo
}
}
func (bv BlockValidator) ValidateBlocks() ValidationWindow {
window := MakeValidationWindow(bv.blockchain, bv.windowSize)
func (bv BlockValidator) ValidateBlocks() (ValidationWindow, error) {
window, err := MakeValidationWindow(bv.blockchain, bv.windowSize)
if err != nil {
logrus.Error("ValidateBlocks: error creating validation window: ", err)
return ValidationWindow{}, err
}
blockNumbers := MakeRange(window.LowerBound, window.UpperBound)
RetrieveAndUpdateBlocks(bv.blockchain, bv.blockRepository, blockNumbers)
lastBlock := bv.blockchain.LastBlock().Int64()
bv.blockRepository.SetBlocksStatus(lastBlock)
return window
_, err = RetrieveAndUpdateBlocks(bv.blockchain, bv.blockRepository, blockNumbers)
if err != nil {
logrus.Error("ValidateBlocks: error getting and updating blocks: ", err)
return ValidationWindow{}, err
}
lastBlock, err := bv.blockchain.LastBlock()
if err != nil {
logrus.Error("ValidateBlocks: error getting last block: ", err)
return ValidationWindow{}, err
}
err = bv.blockRepository.SetBlocksStatus(lastBlock.Int64())
if err != nil {
logrus.Error("ValidateBlocks: error setting block status: ", err)
return ValidationWindow{}, err
}
return window, nil
}
+9 -6
View File
@@ -17,8 +17,7 @@
package history
import (
log "github.com/sirupsen/logrus"
"github.com/sirupsen/logrus"
"github.com/vulcanize/vulcanizedb/pkg/core"
"github.com/vulcanize/vulcanizedb/pkg/datastore"
)
@@ -38,11 +37,15 @@ func NewHeaderValidator(blockChain core.BlockChain, repository datastore.HeaderR
}
func (validator HeaderValidator) ValidateHeaders() (ValidationWindow, error) {
window := MakeValidationWindow(validator.blockChain, validator.windowSize)
blockNumbers := MakeRange(window.LowerBound, window.UpperBound)
_, err := RetrieveAndUpdateHeaders(validator.blockChain, validator.headerRepository, blockNumbers)
window, err := MakeValidationWindow(validator.blockChain, validator.windowSize)
if err != nil {
log.Error("Error in ValidateHeaders: ", err)
logrus.Error("ValidateHeaders: error creating validation window: ", err)
return ValidationWindow{}, err
}
blockNumbers := MakeRange(window.LowerBound, window.UpperBound)
_, err = RetrieveAndUpdateHeaders(validator.blockChain, validator.headerRepository, blockNumbers)
if err != nil {
logrus.Error("ValidateHeaders: error getting/updating headers: ", err)
return ValidationWindow{}, err
}
return window, nil
+25 -12
View File
@@ -23,28 +23,41 @@ import (
"github.com/vulcanize/vulcanizedb/pkg/datastore"
)
func PopulateMissingBlocks(blockchain core.BlockChain, blockRepository datastore.BlockRepository, startingBlockNumber int64) int {
lastBlock := blockchain.LastBlock().Int64()
blockRange := blockRepository.MissingBlockNumbers(startingBlockNumber, lastBlock, blockchain.Node().ID)
func PopulateMissingBlocks(blockchain core.BlockChain, blockRepository datastore.BlockRepository, startingBlockNumber int64) (int, error) {
lastBlock, err := blockchain.LastBlock()
if err != nil {
log.Error("PopulateMissingBlocks: error getting last block: ", err)
return 0, err
}
blockRange := blockRepository.MissingBlockNumbers(startingBlockNumber, lastBlock.Int64(), blockchain.Node().ID)
if len(blockRange) == 0 {
return 0
return 0, nil
}
log.Printf("Backfilling %d blocks\n\n", len(blockRange))
RetrieveAndUpdateBlocks(blockchain, blockRepository, blockRange)
return len(blockRange)
_, err = RetrieveAndUpdateBlocks(blockchain, blockRepository, blockRange)
if err != nil {
log.Error("PopulateMissingBlocks: error gettings/updating blocks: ", err)
return 0, err
}
return len(blockRange), nil
}
func RetrieveAndUpdateBlocks(blockchain core.BlockChain, blockRepository datastore.BlockRepository, blockNumbers []int64) int {
func RetrieveAndUpdateBlocks(blockchain core.BlockChain, blockRepository datastore.BlockRepository, blockNumbers []int64) (int, error) {
for _, blockNumber := range blockNumbers {
block, err := blockchain.GetBlockByNumber(blockNumber)
if err != nil {
log.Printf("failed to retrieve block number: %d\n", blockNumber)
return 0
log.Error("RetrieveAndUpdateBlocks: error getting block: ", err)
return 0, err
}
// TODO: handle possible error here
blockRepository.CreateOrUpdateBlock(block)
_, err = blockRepository.CreateOrUpdateBlock(block)
if err != nil {
log.Error("RetrieveAndUpdateBlocks: error creating/updating block: ", err)
return 0, err
}
}
return len(blockNumbers)
return len(blockNumbers), nil
}
+7 -8
View File
@@ -25,25 +25,24 @@ import (
)
func PopulateMissingHeaders(blockchain core.BlockChain, headerRepository datastore.HeaderRepository, startingBlockNumber int64) (int, error) {
lastBlock := blockchain.LastBlock().Int64()
headerAlreadyExists, err := headerRepository.HeaderExists(lastBlock)
lastBlock, err := blockchain.LastBlock()
if err != nil {
log.Error("Error in checking header in PopulateMissingHeaders: ", err)
log.Error("PopulateMissingHeaders: Error getting last block: ", err)
return 0, err
} else if headerAlreadyExists {
return 0, nil
}
blockNumbers, err := headerRepository.MissingBlockNumbers(startingBlockNumber, lastBlock, blockchain.Node().ID)
blockNumbers, err := headerRepository.MissingBlockNumbers(startingBlockNumber, lastBlock.Int64(), blockchain.Node().ID)
if err != nil {
log.Error("Error getting missing block numbers in PopulateMissingHeaders: ", err)
log.Error("PopulateMissingHeaders: Error getting missing block numbers: ", err)
return 0, err
} else if len(blockNumbers) == 0 {
return 0, nil
}
log.Printf("Backfilling %d blocks\n\n", len(blockNumbers))
_, err = RetrieveAndUpdateHeaders(blockchain, headerRepository, blockNumbers)
if err != nil {
log.Error("PopulateMissingHeaders: Error getting/updating headers:", err)
return 0, err
}
return len(blockNumbers), nil
+9 -12
View File
@@ -18,17 +18,10 @@ package history
import (
"fmt"
log "github.com/sirupsen/logrus"
"github.com/vulcanize/vulcanizedb/pkg/core"
"text/template"
)
const WindowTemplate = `Validating Blocks
|{{.LowerBound}}|-- Validation Window --|{{.UpperBound}}| ({{.UpperBound}}:HEAD)
`
var ParsedWindowTemplate = *template.Must(template.New("window").Parse(WindowTemplate))
type ValidationWindow struct {
LowerBound int64
UpperBound int64
@@ -38,10 +31,14 @@ func (window ValidationWindow) Size() int {
return int(window.UpperBound - window.LowerBound)
}
func MakeValidationWindow(blockchain core.BlockChain, windowSize int) ValidationWindow {
upperBound := blockchain.LastBlock().Int64()
lowerBound := upperBound - int64(windowSize)
return ValidationWindow{lowerBound, upperBound}
func MakeValidationWindow(blockchain core.BlockChain, windowSize int) (ValidationWindow, error) {
upperBound, err := blockchain.LastBlock()
if err != nil {
log.Error("MakeValidationWindow: error getting LastBlock: ", err)
return ValidationWindow{}, err
}
lowerBound := upperBound.Int64() - int64(windowSize)
return ValidationWindow{lowerBound, upperBound.Int64()}, nil
}
func MakeRange(min, max int64) []int64 {