Merge branch 'staging' into batch-historical-headers
This commit is contained in:
@@ -10,6 +10,11 @@ import (
|
||||
func PopulateMissingBlocks(blockchain core.BlockChain, blockRepository datastore.BlockRepository, startingBlockNumber int64) int {
|
||||
lastBlock := blockchain.LastBlock().Int64()
|
||||
blockRange := blockRepository.MissingBlockNumbers(startingBlockNumber, lastBlock, blockchain.Node().ID)
|
||||
|
||||
if len(blockRange) == 0 {
|
||||
return 0
|
||||
}
|
||||
|
||||
log.Printf("Backfilling %d blocks\n\n", len(blockRange))
|
||||
RetrieveAndUpdateBlocks(blockchain, blockRepository, blockRange)
|
||||
return len(blockRange)
|
||||
|
||||
@@ -10,13 +10,20 @@ import (
|
||||
|
||||
func PopulateMissingHeaders(blockchain core.BlockChain, headerRepository datastore.HeaderRepository, startingBlockNumber int64) (int, error) {
|
||||
lastBlock := blockchain.LastBlock().Int64()
|
||||
blockRange := headerRepository.MissingBlockNumbers(startingBlockNumber, lastBlock, blockchain.Node().ID)
|
||||
log.Printf("Backfilling %d blocks\n\n", len(blockRange))
|
||||
_, err := RetrieveAndUpdateHeaders(blockchain, headerRepository, blockRange)
|
||||
headerAlreadyExists, err := headerRepository.HeaderExists(lastBlock)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
} else if headerAlreadyExists {
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
blockNumbers := headerRepository.MissingBlockNumbers(startingBlockNumber, lastBlock, blockchain.Node().ID)
|
||||
log.Printf("Backfilling %d blocks\n\n", len(blockNumbers))
|
||||
_, err = RetrieveAndUpdateHeaders(blockchain, headerRepository, blockNumbers)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return len(blockRange), nil
|
||||
return len(blockNumbers), nil
|
||||
}
|
||||
|
||||
func RetrieveAndUpdateHeaders(chain core.BlockChain, headerRepository datastore.HeaderRepository, blockNumbers []int64) (int, error) {
|
||||
|
||||
@@ -39,4 +39,14 @@ var _ = Describe("Populating headers", func() {
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
headerRepository.AssertCreateOrUpdateHeaderCallCountAndPassedBlockNumbers(1, []int64{2})
|
||||
})
|
||||
|
||||
It("returns early if the db is already synced up to the head of the chain", func() {
|
||||
blockChain := fakes.NewMockBlockChain()
|
||||
blockChain.SetLastBlock(big.NewInt(2))
|
||||
headerRepository.SetHeaderExists(true)
|
||||
headersAdded, err := history.PopulateMissingHeaders(blockChain, headerRepository, 2)
|
||||
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(headersAdded).To(Equal(0))
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user