VDB-302 Sleep when no missing blocks in lightSync (#129)

* Sleep when no missing blocks in lightSync

* Fix tests and error propagation

* Correct geth.log to logrus
This commit is contained in:
Edvard Hübinette
2019-01-11 10:58:41 +01:00
committed by GitHub
parent 77209d3b62
commit ebca338b1e
9 changed files with 83 additions and 30 deletions
+8 -3
View File
@@ -1,6 +1,7 @@
package history
import (
log "github.com/sirupsen/logrus"
"github.com/vulcanize/vulcanizedb/pkg/core"
"github.com/vulcanize/vulcanizedb/pkg/datastore"
)
@@ -19,9 +20,13 @@ func NewHeaderValidator(blockChain core.BlockChain, repository datastore.HeaderR
}
}
func (validator HeaderValidator) ValidateHeaders() ValidationWindow {
func (validator HeaderValidator) ValidateHeaders() (ValidationWindow, error) {
window := MakeValidationWindow(validator.blockChain, validator.windowSize)
blockNumbers := MakeRange(window.LowerBound, window.UpperBound)
RetrieveAndUpdateHeaders(validator.blockChain, validator.headerRepository, blockNumbers)
return window
_, err := RetrieveAndUpdateHeaders(validator.blockChain, validator.headerRepository, blockNumbers)
if err != nil {
log.Error("Error in ValidateHeaders: ", err)
return ValidationWindow{}, err
}
return window, nil
}
+24 -3
View File
@@ -1,22 +1,43 @@
package history_test
import (
"errors"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/vulcanize/vulcanizedb/pkg/fakes"
"github.com/vulcanize/vulcanizedb/pkg/history"
"math/big"
)
var _ = Describe("Header validator", func() {
var (
headerRepository *fakes.MockHeaderRepository
blockChain *fakes.MockBlockChain
)
BeforeEach(func() {
headerRepository = fakes.NewMockHeaderRepository()
blockChain = fakes.NewMockBlockChain()
})
It("attempts to create every header in the validation window", func() {
headerRepository := fakes.NewMockHeaderRepository()
headerRepository.SetMissingBlockNumbers([]int64{})
blockChain := fakes.NewMockBlockChain()
blockChain.SetLastBlock(big.NewInt(3))
validator := history.NewHeaderValidator(blockChain, headerRepository, 2)
validator.ValidateHeaders()
_, err := validator.ValidateHeaders()
Expect(err).NotTo(HaveOccurred())
headerRepository.AssertCreateOrUpdateHeaderCallCountAndPassedBlockNumbers(3, []int64{1, 2, 3})
})
It("propagates header repository errors", func() {
blockChain.SetLastBlock(big.NewInt(3))
headerRepositoryError := errors.New("CreateOrUpdate")
headerRepository.SetCreateOrUpdateHeaderReturnErr(headerRepositoryError)
validator := history.NewHeaderValidator(blockChain, headerRepository, 2)
_, err := validator.ValidateHeaders()
Expect(err).To(MatchError(headerRepositoryError))
})
})
+8 -1
View File
@@ -11,13 +11,20 @@ import (
func PopulateMissingHeaders(blockchain core.BlockChain, headerRepository datastore.HeaderRepository, startingBlockNumber int64) (int, error) {
lastBlock := blockchain.LastBlock().Int64()
headerAlreadyExists, err := headerRepository.HeaderExists(lastBlock)
if err != nil {
log.Error("Error in checking header in PopulateMissingHeaders: ", err)
return 0, err
} else if headerAlreadyExists {
return 0, nil
}
blockNumbers := headerRepository.MissingBlockNumbers(startingBlockNumber, lastBlock, blockchain.Node().ID)
blockNumbers, err := headerRepository.MissingBlockNumbers(startingBlockNumber, lastBlock, blockchain.Node().ID)
if err != nil {
log.Error("Error getting missing block numbers in PopulateMissingHeaders: ", err)
return 0, err
}
log.Printf("Backfilling %d blocks\n\n", len(blockNumbers))
_, err = RetrieveAndUpdateHeaders(blockchain, headerRepository, blockNumbers)
if err != nil {