2017-11-06 20:36:12 +00:00
|
|
|
package history_test
|
|
|
|
|
|
|
|
import (
|
2018-02-02 21:53:16 +00:00
|
|
|
. "github.com/onsi/ginkgo"
|
|
|
|
. "github.com/onsi/gomega"
|
2018-01-06 20:31:53 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/fakes"
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/history"
|
2018-07-20 16:37:46 +00:00
|
|
|
"math/big"
|
2017-11-06 20:36:12 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var _ = Describe("Populating blocks", func() {
|
2018-07-20 16:37:46 +00:00
|
|
|
var blockRepository *fakes.MockBlockRepository
|
2018-02-12 16:54:05 +00:00
|
|
|
|
|
|
|
BeforeEach(func() {
|
2018-07-20 16:37:46 +00:00
|
|
|
blockRepository = fakes.NewMockBlockRepository()
|
2018-02-12 16:54:05 +00:00
|
|
|
})
|
2017-11-06 20:36:12 +00:00
|
|
|
|
2018-07-17 21:23:07 +00:00
|
|
|
It("fills in the only missing block (BlockNumber 1)", func() {
|
2018-07-20 16:37:46 +00:00
|
|
|
blockChain := fakes.NewMockBlockChain()
|
|
|
|
blockChain.SetLastBlock(big.NewInt(2))
|
|
|
|
blockRepository.SetMissingBlockNumbersReturnArray([]int64{2})
|
2018-02-12 16:54:05 +00:00
|
|
|
|
2018-07-20 16:37:46 +00:00
|
|
|
blocksAdded := history.PopulateMissingBlocks(blockChain, blockRepository, 1)
|
2018-02-12 16:54:05 +00:00
|
|
|
_, err := blockRepository.GetBlock(1)
|
2017-11-06 20:36:12 +00:00
|
|
|
|
2018-01-02 18:39:55 +00:00
|
|
|
Expect(blocksAdded).To(Equal(1))
|
2017-12-13 16:51:11 +00:00
|
|
|
Expect(err).ToNot(HaveOccurred())
|
2017-11-06 20:36:12 +00:00
|
|
|
})
|
|
|
|
|
2018-01-02 18:39:55 +00:00
|
|
|
It("fills in the three missing blocks (Numbers: 5,8,10)", func() {
|
2018-07-20 16:37:46 +00:00
|
|
|
blockChain := fakes.NewMockBlockChain()
|
|
|
|
blockChain.SetLastBlock(big.NewInt(13))
|
|
|
|
blockRepository.SetMissingBlockNumbersReturnArray([]int64{5, 8, 10})
|
|
|
|
|
|
|
|
blocksAdded := history.PopulateMissingBlocks(blockChain, blockRepository, 5)
|
2017-11-06 20:36:12 +00:00
|
|
|
|
2018-01-02 18:39:55 +00:00
|
|
|
Expect(blocksAdded).To(Equal(3))
|
2018-07-20 16:37:46 +00:00
|
|
|
blockRepository.AssertCreateOrUpdateBlocksCallCountAndBlockNumbersEquals(3, []int64{5, 8, 10})
|
2017-11-06 20:36:12 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
It("returns the number of blocks created", func() {
|
2018-07-20 16:37:46 +00:00
|
|
|
blockChain := fakes.NewMockBlockChain()
|
|
|
|
blockChain.SetLastBlock(big.NewInt(6))
|
|
|
|
blockRepository.SetMissingBlockNumbersReturnArray([]int64{4, 5})
|
2017-11-06 20:36:12 +00:00
|
|
|
|
2018-07-20 16:37:46 +00:00
|
|
|
numberOfBlocksCreated := history.PopulateMissingBlocks(blockChain, blockRepository, 3)
|
2017-11-06 20:36:12 +00:00
|
|
|
|
|
|
|
Expect(numberOfBlocksCreated).To(Equal(2))
|
|
|
|
})
|
|
|
|
|
2018-01-05 17:55:00 +00:00
|
|
|
It("updates the repository with a range of blocks w/in the range ", func() {
|
2018-07-20 16:37:46 +00:00
|
|
|
blockChain := fakes.NewMockBlockChain()
|
|
|
|
|
|
|
|
history.RetrieveAndUpdateBlocks(blockChain, blockRepository, history.MakeRange(2, 5))
|
|
|
|
|
|
|
|
blockRepository.AssertCreateOrUpdateBlocksCallCountAndBlockNumbersEquals(4, []int64{2, 3, 4, 5})
|
2017-12-20 20:06:22 +00:00
|
|
|
})
|
2018-01-05 17:55:00 +00:00
|
|
|
|
2018-03-27 21:06:12 +00:00
|
|
|
It("does not call repository create block when there is an error", func() {
|
2018-07-20 16:37:46 +00:00
|
|
|
blockChain := fakes.NewMockBlockChain()
|
|
|
|
blockChain.SetGetBlockByNumberErr(fakes.FakeError)
|
2018-03-27 21:06:12 +00:00
|
|
|
blocks := history.MakeRange(1, 10)
|
2018-07-20 16:37:46 +00:00
|
|
|
|
|
|
|
history.RetrieveAndUpdateBlocks(blockChain, blockRepository, blocks)
|
|
|
|
|
|
|
|
blockRepository.AssertCreateOrUpdateBlockCallCountEquals(0)
|
2018-03-27 21:06:12 +00:00
|
|
|
})
|
2017-11-06 20:36:12 +00:00
|
|
|
})
|