diff --git a/pkg/history/populate_blocks.go b/pkg/history/populate_blocks.go index 8d2968d9..4295b4b0 100644 --- a/pkg/history/populate_blocks.go +++ b/pkg/history/populate_blocks.go @@ -16,14 +16,15 @@ func (window Window) Size() 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) return len(blockRange) } func UpdateBlocksWindow(blockchain core.Blockchain, repository repositories.Repository, windowSize int) Window { - maxBlockNumber := repository.MaxBlockNumber() - upperBound := repository.MaxBlockNumber() - int64(2) + maxBlockNumber := blockchain.LastBlock().Int64() + upperBound := maxBlockNumber - int64(1) lowerBound := upperBound - int64(windowSize) blockRange := MakeRange(lowerBound, upperBound) updateBlockRange(blockchain, repository, blockRange) diff --git a/pkg/history/populate_blocks_test.go b/pkg/history/populate_blocks_test.go index 6bfddd7a..b1e6e0d2 100644 --- a/pkg/history/populate_blocks_test.go +++ b/pkg/history/populate_blocks_test.go @@ -11,25 +11,33 @@ import ( var _ = Describe("Populating blocks", func() { - It("fills in the only missing block", func() { - blocks := []core.Block{{Number: 1, Hash: "x012343"}} + It("fills in the only missing block (Number 1)", func() { + blocks := []core.Block{ + {Number: 1}, + {Number: 2}, + } blockchain := fakes.NewBlockchainWithBlocks(blocks) repository := repositories.NewInMemory() 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(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{ {Number: 4}, {Number: 5}, + {Number: 6}, + {Number: 7}, {Number: 8}, + {Number: 9}, {Number: 10}, + {Number: 11}, + {Number: 12}, {Number: 13}, }) repository := repositories.NewInMemory() @@ -42,8 +50,9 @@ var _ = Describe("Populating blocks", func() { repository.CreateOrUpdateBlock(core.Block{Number: 11}) 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)) _, err := repository.FindBlockByNumber(4) Expect(err).To(HaveOccurred()) @@ -85,6 +94,7 @@ var _ = Describe("Populating blocks", func() { blockchain := fakes.NewBlockchainWithBlocks([]core.Block{ {Number: 4}, {Number: 5}, + {Number: 6}, }) repository := repositories.NewInMemory() repository.CreateOrUpdateBlock(core.Block{Number: 3})