2018-07-17 21:23:07 +00:00
|
|
|
package history_test
|
|
|
|
|
|
|
|
import (
|
2018-07-20 16:37:46 +00:00
|
|
|
"math/big"
|
|
|
|
|
2018-07-17 21:23:07 +00:00
|
|
|
. "github.com/onsi/ginkgo"
|
|
|
|
. "github.com/onsi/gomega"
|
2018-07-20 16:37:46 +00:00
|
|
|
|
2018-07-24 16:38:49 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/core"
|
2018-07-26 21:15:24 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres/repositories"
|
2018-07-17 21:23:07 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/fakes"
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/history"
|
2018-07-24 16:38:49 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/transformers"
|
2018-07-17 21:23:07 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var _ = Describe("Populating headers", func() {
|
|
|
|
|
2018-07-20 16:37:46 +00:00
|
|
|
var headerRepository *fakes.MockHeaderRepository
|
2018-07-17 21:23:07 +00:00
|
|
|
|
|
|
|
BeforeEach(func() {
|
2018-07-20 16:37:46 +00:00
|
|
|
headerRepository = fakes.NewMockHeaderRepository()
|
2018-07-17 21:23:07 +00:00
|
|
|
})
|
|
|
|
|
2018-07-24 16:38:49 +00:00
|
|
|
It("returns number of headers added", func() {
|
|
|
|
blockChain := fakes.NewMockBlockChain()
|
|
|
|
blockChain.SetLastBlock(big.NewInt(2))
|
|
|
|
headerRepository.SetMissingBlockNumbers([]int64{2})
|
2018-07-17 21:23:07 +00:00
|
|
|
|
2018-07-24 16:38:49 +00:00
|
|
|
headersAdded, err := history.PopulateMissingHeaders(blockChain, headerRepository, 1, []transformers.Transformer{})
|
2018-07-17 21:23:07 +00:00
|
|
|
|
2018-07-24 16:38:49 +00:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
Expect(headersAdded).To(Equal(1))
|
2018-07-17 21:23:07 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
It("adds missing headers to the db", func() {
|
2018-07-20 16:37:46 +00:00
|
|
|
blockChain := fakes.NewMockBlockChain()
|
|
|
|
blockChain.SetLastBlock(big.NewInt(2))
|
|
|
|
headerRepository.SetMissingBlockNumbers([]int64{2})
|
2018-07-17 21:23:07 +00:00
|
|
|
|
2018-07-24 16:38:49 +00:00
|
|
|
_, err := history.PopulateMissingHeaders(blockChain, headerRepository, 1, []transformers.Transformer{})
|
2018-07-17 21:23:07 +00:00
|
|
|
|
2018-07-24 16:38:49 +00:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
2018-07-20 16:37:46 +00:00
|
|
|
headerRepository.AssertCreateOrUpdateHeaderCallCountAndPassedBlockNumbers(1, []int64{2})
|
2018-07-17 21:23:07 +00:00
|
|
|
})
|
2018-07-24 16:38:49 +00:00
|
|
|
|
|
|
|
It("executes passed transformers with created headers", func() {
|
|
|
|
blockNumber := int64(54321)
|
|
|
|
blockChain := fakes.NewMockBlockChain()
|
|
|
|
blockChain.SetLastBlock(big.NewInt(blockNumber))
|
|
|
|
headerRepository.SetMissingBlockNumbers([]int64{blockNumber})
|
|
|
|
headerID := int64(12345)
|
|
|
|
headerRepository.SetCreateOrUpdateHeaderReturnID(headerID)
|
|
|
|
transformer := fakes.NewMockTransformer()
|
|
|
|
|
2018-07-26 21:15:24 +00:00
|
|
|
_, err := history.PopulateMissingHeaders(blockChain, headerRepository, blockNumber, []transformers.Transformer{transformer})
|
2018-07-24 16:38:49 +00:00
|
|
|
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
transformer.AssertExecuteCalledWith(core.Header{BlockNumber: blockNumber}, headerID)
|
|
|
|
})
|
|
|
|
|
2018-07-26 21:15:24 +00:00
|
|
|
It("does not execute transformer if repository indicates header already exists", func() {
|
|
|
|
blockNumber := int64(54321)
|
|
|
|
blockChain := fakes.NewMockBlockChain()
|
|
|
|
blockChain.SetLastBlock(big.NewInt(blockNumber))
|
|
|
|
headerRepository.SetMissingBlockNumbers([]int64{blockNumber})
|
|
|
|
headerRepository.SetCreateOrUpdateHeaderReturnErr(repositories.ErrValidHeaderExists)
|
|
|
|
transformer := fakes.NewMockTransformer()
|
|
|
|
|
|
|
|
_, err := history.PopulateMissingHeaders(blockChain, headerRepository, blockNumber, []transformers.Transformer{transformer})
|
|
|
|
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
transformer.AssertExecuteNotCalled()
|
|
|
|
})
|
|
|
|
|
2018-07-24 16:38:49 +00:00
|
|
|
It("returns error if executing transformer fails", func() {
|
|
|
|
blockNumber := int64(54321)
|
|
|
|
blockChain := fakes.NewMockBlockChain()
|
|
|
|
blockChain.SetLastBlock(big.NewInt(blockNumber))
|
|
|
|
headerRepository.SetMissingBlockNumbers([]int64{blockNumber})
|
|
|
|
headerID := int64(12345)
|
|
|
|
headerRepository.SetCreateOrUpdateHeaderReturnID(headerID)
|
|
|
|
transformer := fakes.NewMockTransformer()
|
|
|
|
transformer.SetExecuteErr(fakes.FakeError)
|
|
|
|
|
2018-07-26 21:15:24 +00:00
|
|
|
_, err := history.PopulateMissingHeaders(blockChain, headerRepository, blockNumber, []transformers.Transformer{transformer})
|
2018-07-24 16:38:49 +00:00
|
|
|
|
|
|
|
Expect(err).To(HaveOccurred())
|
|
|
|
Expect(err).To(MatchError(fakes.FakeError))
|
|
|
|
})
|
2018-07-17 21:23:07 +00:00
|
|
|
})
|