2017-11-06 20:36:12 +00:00
|
|
|
package history_test
|
|
|
|
|
|
|
|
import (
|
2018-03-27 21:06:12 +00:00
|
|
|
"errors"
|
|
|
|
|
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/core"
|
2018-02-13 16:31:57 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/datastore/inmemory"
|
2018-01-06 20:31:53 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/fakes"
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/history"
|
2017-11-06 20:36:12 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var _ = Describe("Populating blocks", func() {
|
2018-02-12 16:54:05 +00:00
|
|
|
var inMemory *inmemory.InMemory
|
|
|
|
var blockRepository *inmemory.BlockRepository
|
|
|
|
|
|
|
|
BeforeEach(func() {
|
|
|
|
inMemory = inmemory.NewInMemory()
|
|
|
|
blockRepository = &inmemory.BlockRepository{InMemory: inMemory}
|
|
|
|
})
|
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-01-02 18:39:55 +00:00
|
|
|
blocks := []core.Block{
|
|
|
|
{Number: 1},
|
|
|
|
{Number: 2},
|
|
|
|
}
|
2018-07-18 20:59:40 +00:00
|
|
|
blockchain := fakes.NewMockBlockChainWithBlocks(blocks)
|
2017-11-06 20:36:12 +00:00
|
|
|
|
2018-02-12 16:54:05 +00:00
|
|
|
blockRepository.CreateOrUpdateBlock(core.Block{Number: 2})
|
|
|
|
|
|
|
|
blocksAdded := history.PopulateMissingBlocks(blockchain, blockRepository, 1)
|
|
|
|
_, 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-18 20:59:40 +00:00
|
|
|
blockchain := fakes.NewMockBlockChainWithBlocks([]core.Block{
|
2017-11-06 20:36:12 +00:00
|
|
|
{Number: 4},
|
|
|
|
{Number: 5},
|
2018-01-02 18:39:55 +00:00
|
|
|
{Number: 6},
|
|
|
|
{Number: 7},
|
2017-11-06 20:36:12 +00:00
|
|
|
{Number: 8},
|
2018-01-02 18:39:55 +00:00
|
|
|
{Number: 9},
|
2017-11-06 20:36:12 +00:00
|
|
|
{Number: 10},
|
2018-01-02 18:39:55 +00:00
|
|
|
{Number: 11},
|
|
|
|
{Number: 12},
|
2017-11-06 20:36:12 +00:00
|
|
|
{Number: 13},
|
|
|
|
})
|
2018-02-12 16:54:05 +00:00
|
|
|
blockRepository.CreateOrUpdateBlock(core.Block{Number: 1})
|
|
|
|
blockRepository.CreateOrUpdateBlock(core.Block{Number: 2})
|
|
|
|
blockRepository.CreateOrUpdateBlock(core.Block{Number: 3})
|
|
|
|
blockRepository.CreateOrUpdateBlock(core.Block{Number: 6})
|
|
|
|
blockRepository.CreateOrUpdateBlock(core.Block{Number: 7})
|
|
|
|
blockRepository.CreateOrUpdateBlock(core.Block{Number: 9})
|
|
|
|
blockRepository.CreateOrUpdateBlock(core.Block{Number: 11})
|
|
|
|
blockRepository.CreateOrUpdateBlock(core.Block{Number: 12})
|
2018-07-17 21:23:07 +00:00
|
|
|
blockRepository.CreateOrUpdateBlock(core.Block{Number: 13})
|
2018-02-12 16:54:05 +00:00
|
|
|
|
|
|
|
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-17 21:23:07 +00:00
|
|
|
Expect(blockRepository.BlockCount()).To(Equal(12))
|
2018-02-12 16:54:05 +00:00
|
|
|
_, err := blockRepository.GetBlock(4)
|
2017-12-13 16:51:11 +00:00
|
|
|
Expect(err).To(HaveOccurred())
|
2018-02-12 16:54:05 +00:00
|
|
|
_, err = blockRepository.GetBlock(5)
|
2017-12-13 16:51:11 +00:00
|
|
|
Expect(err).ToNot(HaveOccurred())
|
2018-02-12 16:54:05 +00:00
|
|
|
_, err = blockRepository.GetBlock(8)
|
2017-12-13 16:51:11 +00:00
|
|
|
Expect(err).ToNot(HaveOccurred())
|
2018-02-12 16:54:05 +00:00
|
|
|
_, err = blockRepository.GetBlock(10)
|
2017-12-13 16:51:11 +00:00
|
|
|
Expect(err).ToNot(HaveOccurred())
|
2018-07-17 21:23:07 +00:00
|
|
|
_, err = blockRepository.GetBlock(14)
|
2017-12-13 16:51:11 +00:00
|
|
|
Expect(err).To(HaveOccurred())
|
2017-11-06 20:36:12 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
It("returns the number of blocks created", func() {
|
2018-07-18 20:59:40 +00:00
|
|
|
blockchain := fakes.NewMockBlockChainWithBlocks([]core.Block{
|
2017-11-06 20:36:12 +00:00
|
|
|
{Number: 4},
|
|
|
|
{Number: 5},
|
2018-01-02 18:39:55 +00:00
|
|
|
{Number: 6},
|
2017-11-06 20:36:12 +00:00
|
|
|
})
|
2018-02-12 16:54:05 +00:00
|
|
|
blockRepository.CreateOrUpdateBlock(core.Block{Number: 3})
|
|
|
|
blockRepository.CreateOrUpdateBlock(core.Block{Number: 6})
|
2017-11-06 20:36:12 +00:00
|
|
|
|
2018-02-12 16:54:05 +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-18 20:59:40 +00:00
|
|
|
blockchain := fakes.NewMockBlockChainWithBlocks([]core.Block{
|
2017-12-20 20:06:22 +00:00
|
|
|
{Number: 1},
|
|
|
|
{Number: 2},
|
|
|
|
{Number: 3},
|
2018-01-05 17:55:00 +00:00
|
|
|
{Number: 4},
|
|
|
|
{Number: 5},
|
2017-12-20 20:06:22 +00:00
|
|
|
})
|
|
|
|
|
2018-02-12 16:54:05 +00:00
|
|
|
history.RetrieveAndUpdateBlocks(blockchain, blockRepository, history.MakeRange(2, 5))
|
2018-07-17 21:23:07 +00:00
|
|
|
Expect(blockRepository.BlockCount()).To(Equal(4))
|
|
|
|
Expect(blockRepository.CreateOrUpdateBlockCallCount).To(Equal(4))
|
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-18 20:59:40 +00:00
|
|
|
blockchain := fakes.NewMockBlockChain(errors.New("error getting block"))
|
2018-03-27 21:06:12 +00:00
|
|
|
blocks := history.MakeRange(1, 10)
|
|
|
|
history.RetrieveAndUpdateBlocks(blockchain, blockRepository, blocks)
|
|
|
|
Expect(blockRepository.BlockCount()).To(Equal(0))
|
|
|
|
Expect(blockRepository.CreateOrUpdateBlockCallCount).To(Equal(0))
|
|
|
|
})
|
2017-11-06 20:36:12 +00:00
|
|
|
})
|