Merge pull request #118 from 8thlight/update_lastblock

Update populate blocks to use blockchain rather than db to determine …
This commit is contained in:
Matt K 2018-01-02 13:44:37 -06:00 committed by GitHub
commit 13748a92e5
2 changed files with 21 additions and 10 deletions

View File

@ -16,14 +16,15 @@ func (window Window) Size() int {
} }
func PopulateMissingBlocks(blockchain core.Blockchain, repository repositories.Repository, startingBlockNumber int64) int { func PopulateMissingBlocks(blockchain core.Blockchain, repository repositories.Repository, startingBlockNumber int64) int {
blockRange := repository.MissingBlockNumbers(startingBlockNumber, repository.MaxBlockNumber()) lastBlock := blockchain.LastBlock().Int64()
blockRange := repository.MissingBlockNumbers(startingBlockNumber, lastBlock-1)
updateBlockRange(blockchain, repository, blockRange) updateBlockRange(blockchain, repository, blockRange)
return len(blockRange) return len(blockRange)
} }
func UpdateBlocksWindow(blockchain core.Blockchain, repository repositories.Repository, windowSize int) Window { func UpdateBlocksWindow(blockchain core.Blockchain, repository repositories.Repository, windowSize int) Window {
maxBlockNumber := repository.MaxBlockNumber() maxBlockNumber := blockchain.LastBlock().Int64()
upperBound := repository.MaxBlockNumber() - int64(2) upperBound := maxBlockNumber - int64(1)
lowerBound := upperBound - int64(windowSize) lowerBound := upperBound - int64(windowSize)
blockRange := MakeRange(lowerBound, upperBound) blockRange := MakeRange(lowerBound, upperBound)
updateBlockRange(blockchain, repository, blockRange) updateBlockRange(blockchain, repository, blockRange)

View File

@ -11,25 +11,33 @@ import (
var _ = Describe("Populating blocks", func() { var _ = Describe("Populating blocks", func() {
It("fills in the only missing block", func() { It("fills in the only missing block (Number 1)", func() {
blocks := []core.Block{{Number: 1, Hash: "x012343"}} blocks := []core.Block{
{Number: 1},
{Number: 2},
}
blockchain := fakes.NewBlockchainWithBlocks(blocks) blockchain := fakes.NewBlockchainWithBlocks(blocks)
repository := repositories.NewInMemory() repository := repositories.NewInMemory()
repository.CreateOrUpdateBlock(core.Block{Number: 2}) repository.CreateOrUpdateBlock(core.Block{Number: 2})
history.PopulateMissingBlocks(blockchain, repository, 1) blocksAdded := history.PopulateMissingBlocks(blockchain, repository, 1)
_, err := repository.FindBlockByNumber(1)
block, err := repository.FindBlockByNumber(1) Expect(blocksAdded).To(Equal(1))
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
Expect(block.Hash).To(Equal("x012343"))
}) })
It("fills in the three missing blocks (5,8,10)", func() { It("fills in the three missing blocks (Numbers: 5,8,10)", func() {
blockchain := fakes.NewBlockchainWithBlocks([]core.Block{ blockchain := fakes.NewBlockchainWithBlocks([]core.Block{
{Number: 4}, {Number: 4},
{Number: 5}, {Number: 5},
{Number: 6},
{Number: 7},
{Number: 8}, {Number: 8},
{Number: 9},
{Number: 10}, {Number: 10},
{Number: 11},
{Number: 12},
{Number: 13}, {Number: 13},
}) })
repository := repositories.NewInMemory() repository := repositories.NewInMemory()
@ -42,8 +50,9 @@ var _ = Describe("Populating blocks", func() {
repository.CreateOrUpdateBlock(core.Block{Number: 11}) repository.CreateOrUpdateBlock(core.Block{Number: 11})
repository.CreateOrUpdateBlock(core.Block{Number: 12}) repository.CreateOrUpdateBlock(core.Block{Number: 12})
history.PopulateMissingBlocks(blockchain, repository, 5) blocksAdded := history.PopulateMissingBlocks(blockchain, repository, 5)
Expect(blocksAdded).To(Equal(3))
Expect(repository.BlockCount()).To(Equal(11)) Expect(repository.BlockCount()).To(Equal(11))
_, err := repository.FindBlockByNumber(4) _, err := repository.FindBlockByNumber(4)
Expect(err).To(HaveOccurred()) Expect(err).To(HaveOccurred())
@ -85,6 +94,7 @@ var _ = Describe("Populating blocks", func() {
blockchain := fakes.NewBlockchainWithBlocks([]core.Block{ blockchain := fakes.NewBlockchainWithBlocks([]core.Block{
{Number: 4}, {Number: 4},
{Number: 5}, {Number: 5},
{Number: 6},
}) })
repository := repositories.NewInMemory() repository := repositories.NewInMemory()
repository.CreateOrUpdateBlock(core.Block{Number: 3}) repository.CreateOrUpdateBlock(core.Block{Number: 3})