forked from cerc-io/ipld-eth-server
Consolidate test doubles
- Migrate various mocks of core namespaces to shared version in `fakes` pkg - Err on the side of making test doubles less sophisticated - Don't pull over mocks of namespaces that are only used in example code
This commit is contained in:
@@ -6,12 +6,12 @@ import (
|
||||
)
|
||||
|
||||
type BlockValidator struct {
|
||||
blockchain core.Blockchain
|
||||
blockchain core.BlockChain
|
||||
blockRepository datastore.BlockRepository
|
||||
windowSize int
|
||||
}
|
||||
|
||||
func NewBlockValidator(blockchain core.Blockchain, blockRepository datastore.BlockRepository, windowSize int) *BlockValidator {
|
||||
func NewBlockValidator(blockchain core.BlockChain, blockRepository datastore.BlockRepository, windowSize int) *BlockValidator {
|
||||
return &BlockValidator{
|
||||
blockchain: blockchain,
|
||||
blockRepository: blockRepository,
|
||||
|
||||
@@ -4,39 +4,29 @@ import (
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
|
||||
"github.com/vulcanize/vulcanizedb/pkg/core"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/datastore/inmemory"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/fakes"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/history"
|
||||
"math/big"
|
||||
)
|
||||
|
||||
var _ = Describe("Blocks validator", func() {
|
||||
|
||||
It("calls create or update for all blocks within the window", func() {
|
||||
blockchain := fakes.NewMockBlockChainWithBlocks([]core.Block{
|
||||
{Number: 4},
|
||||
{Number: 5},
|
||||
{Number: 6},
|
||||
{Number: 7},
|
||||
})
|
||||
inMemoryDB := inmemory.NewInMemory()
|
||||
blocksRepository := &inmemory.BlockRepository{InMemory: inMemoryDB}
|
||||
validator := history.NewBlockValidator(blockchain, blocksRepository, 2)
|
||||
blockChain := fakes.NewMockBlockChain()
|
||||
blockChain.SetLastBlock(big.NewInt(7))
|
||||
blocksRepository := fakes.NewMockBlockRepository()
|
||||
validator := history.NewBlockValidator(blockChain, blocksRepository, 2)
|
||||
|
||||
window := validator.ValidateBlocks()
|
||||
|
||||
Expect(window).To(Equal(history.ValidationWindow{LowerBound: 5, UpperBound: 7}))
|
||||
Expect(blocksRepository.BlockCount()).To(Equal(3))
|
||||
Expect(blocksRepository.CreateOrUpdateBlockCallCount).To(Equal(3))
|
||||
blocksRepository.AssertCreateOrUpdateBlockCallCountEquals(3)
|
||||
})
|
||||
|
||||
It("returns the number of largest block", func() {
|
||||
blockchain := fakes.NewMockBlockChainWithBlocks([]core.Block{
|
||||
{Number: 1},
|
||||
{Number: 2},
|
||||
{Number: 3},
|
||||
})
|
||||
maxBlockNumber := blockchain.LastBlock()
|
||||
blockChain := fakes.NewMockBlockChain()
|
||||
blockChain.SetLastBlock(big.NewInt(3))
|
||||
maxBlockNumber := blockChain.LastBlock()
|
||||
|
||||
Expect(maxBlockNumber.Int64()).To(Equal(int64(3)))
|
||||
})
|
||||
|
||||
@@ -6,12 +6,12 @@ import (
|
||||
)
|
||||
|
||||
type HeaderValidator struct {
|
||||
blockChain core.Blockchain
|
||||
blockChain core.BlockChain
|
||||
headerRepository datastore.HeaderRepository
|
||||
windowSize int
|
||||
}
|
||||
|
||||
func NewHeaderValidator(blockChain core.Blockchain, repository datastore.HeaderRepository, windowSize int) HeaderValidator {
|
||||
func NewHeaderValidator(blockChain core.BlockChain, repository datastore.HeaderRepository, windowSize int) HeaderValidator {
|
||||
return HeaderValidator{
|
||||
blockChain: blockChain,
|
||||
headerRepository: repository,
|
||||
|
||||
@@ -1,39 +1,22 @@
|
||||
package history_test
|
||||
|
||||
import (
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/core"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/datastore/inmemory"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/fakes"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/history"
|
||||
"math/big"
|
||||
)
|
||||
|
||||
var _ = Describe("Header validator", func() {
|
||||
It("replaces headers in the validation window that have changed", func() {
|
||||
blockNumber := int64(10)
|
||||
oldHash := common.HexToHash("0x0987654321").Hex()
|
||||
oldHeader := core.Header{
|
||||
BlockNumber: blockNumber,
|
||||
Hash: oldHash,
|
||||
}
|
||||
inMemory := inmemory.NewInMemory()
|
||||
headerRepository := inmemory.NewHeaderRepository(inMemory)
|
||||
headerRepository.CreateOrUpdateHeader(oldHeader)
|
||||
newHash := common.HexToHash("0x123456789").Hex()
|
||||
newHeader := core.Header{
|
||||
BlockNumber: blockNumber,
|
||||
Hash: newHash,
|
||||
}
|
||||
headers := []core.Header{newHeader}
|
||||
blockChain := fakes.NewMockBlockChainWithHeaders(headers)
|
||||
validator := history.NewHeaderValidator(blockChain, headerRepository, 1)
|
||||
It("attempts to create every header in the validation window", func() {
|
||||
headerRepository := fakes.NewMockHeaderRepository()
|
||||
headerRepository.SetMissingBlockNumbers([]int64{})
|
||||
blockChain := fakes.NewMockBlockChain()
|
||||
blockChain.SetLastBlock(big.NewInt(3))
|
||||
validator := history.NewHeaderValidator(blockChain, headerRepository, 2)
|
||||
|
||||
validator.ValidateHeaders()
|
||||
|
||||
dbHeader, _ := headerRepository.GetHeader(blockNumber)
|
||||
Expect(dbHeader.Hash).NotTo(Equal(oldHash))
|
||||
Expect(dbHeader.Hash).To(Equal(newHash))
|
||||
headerRepository.AssertCreateOrUpdateHeaderCallCountAndPassedBlockNumbers(3, []int64{1, 2, 3})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -7,7 +7,7 @@ import (
|
||||
"github.com/vulcanize/vulcanizedb/pkg/datastore"
|
||||
)
|
||||
|
||||
func PopulateMissingBlocks(blockchain core.Blockchain, blockRepository datastore.BlockRepository, startingBlockNumber int64) int {
|
||||
func PopulateMissingBlocks(blockchain core.BlockChain, blockRepository datastore.BlockRepository, startingBlockNumber int64) int {
|
||||
lastBlock := blockchain.LastBlock().Int64()
|
||||
blockRange := blockRepository.MissingBlockNumbers(startingBlockNumber, lastBlock, blockchain.Node().ID)
|
||||
log.SetPrefix("")
|
||||
@@ -16,7 +16,7 @@ func PopulateMissingBlocks(blockchain core.Blockchain, blockRepository datastore
|
||||
return len(blockRange)
|
||||
}
|
||||
|
||||
func RetrieveAndUpdateBlocks(blockchain core.Blockchain, blockRepository datastore.BlockRepository, blockNumbers []int64) int {
|
||||
func RetrieveAndUpdateBlocks(blockchain core.BlockChain, blockRepository datastore.BlockRepository, blockNumbers []int64) int {
|
||||
for _, blockNumber := range blockNumbers {
|
||||
block, err := blockchain.GetBlockByNumber(blockNumber)
|
||||
if err != nil {
|
||||
|
||||
@@ -1,35 +1,26 @@
|
||||
package history_test
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/core"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/datastore/inmemory"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/fakes"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/history"
|
||||
"math/big"
|
||||
)
|
||||
|
||||
var _ = Describe("Populating blocks", func() {
|
||||
var inMemory *inmemory.InMemory
|
||||
var blockRepository *inmemory.BlockRepository
|
||||
var blockRepository *fakes.MockBlockRepository
|
||||
|
||||
BeforeEach(func() {
|
||||
inMemory = inmemory.NewInMemory()
|
||||
blockRepository = &inmemory.BlockRepository{InMemory: inMemory}
|
||||
blockRepository = fakes.NewMockBlockRepository()
|
||||
})
|
||||
|
||||
It("fills in the only missing block (BlockNumber 1)", func() {
|
||||
blocks := []core.Block{
|
||||
{Number: 1},
|
||||
{Number: 2},
|
||||
}
|
||||
blockchain := fakes.NewMockBlockChainWithBlocks(blocks)
|
||||
blockChain := fakes.NewMockBlockChain()
|
||||
blockChain.SetLastBlock(big.NewInt(2))
|
||||
blockRepository.SetMissingBlockNumbersReturnArray([]int64{2})
|
||||
|
||||
blockRepository.CreateOrUpdateBlock(core.Block{Number: 2})
|
||||
|
||||
blocksAdded := history.PopulateMissingBlocks(blockchain, blockRepository, 1)
|
||||
blocksAdded := history.PopulateMissingBlocks(blockChain, blockRepository, 1)
|
||||
_, err := blockRepository.GetBlock(1)
|
||||
|
||||
Expect(blocksAdded).To(Equal(1))
|
||||
@@ -37,77 +28,41 @@ var _ = Describe("Populating blocks", func() {
|
||||
})
|
||||
|
||||
It("fills in the three missing blocks (Numbers: 5,8,10)", func() {
|
||||
blockchain := fakes.NewMockBlockChainWithBlocks([]core.Block{
|
||||
{Number: 4},
|
||||
{Number: 5},
|
||||
{Number: 6},
|
||||
{Number: 7},
|
||||
{Number: 8},
|
||||
{Number: 9},
|
||||
{Number: 10},
|
||||
{Number: 11},
|
||||
{Number: 12},
|
||||
{Number: 13},
|
||||
})
|
||||
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})
|
||||
blockRepository.CreateOrUpdateBlock(core.Block{Number: 13})
|
||||
blockChain := fakes.NewMockBlockChain()
|
||||
blockChain.SetLastBlock(big.NewInt(13))
|
||||
blockRepository.SetMissingBlockNumbersReturnArray([]int64{5, 8, 10})
|
||||
|
||||
blocksAdded := history.PopulateMissingBlocks(blockchain, blockRepository, 5)
|
||||
blocksAdded := history.PopulateMissingBlocks(blockChain, blockRepository, 5)
|
||||
|
||||
Expect(blocksAdded).To(Equal(3))
|
||||
Expect(blockRepository.BlockCount()).To(Equal(12))
|
||||
_, err := blockRepository.GetBlock(4)
|
||||
Expect(err).To(HaveOccurred())
|
||||
_, err = blockRepository.GetBlock(5)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
_, err = blockRepository.GetBlock(8)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
_, err = blockRepository.GetBlock(10)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
_, err = blockRepository.GetBlock(14)
|
||||
Expect(err).To(HaveOccurred())
|
||||
blockRepository.AssertCreateOrUpdateBlocksCallCountAndBlockNumbersEquals(3, []int64{5, 8, 10})
|
||||
})
|
||||
|
||||
It("returns the number of blocks created", func() {
|
||||
blockchain := fakes.NewMockBlockChainWithBlocks([]core.Block{
|
||||
{Number: 4},
|
||||
{Number: 5},
|
||||
{Number: 6},
|
||||
})
|
||||
blockRepository.CreateOrUpdateBlock(core.Block{Number: 3})
|
||||
blockRepository.CreateOrUpdateBlock(core.Block{Number: 6})
|
||||
blockChain := fakes.NewMockBlockChain()
|
||||
blockChain.SetLastBlock(big.NewInt(6))
|
||||
blockRepository.SetMissingBlockNumbersReturnArray([]int64{4, 5})
|
||||
|
||||
numberOfBlocksCreated := history.PopulateMissingBlocks(blockchain, blockRepository, 3)
|
||||
numberOfBlocksCreated := history.PopulateMissingBlocks(blockChain, blockRepository, 3)
|
||||
|
||||
Expect(numberOfBlocksCreated).To(Equal(2))
|
||||
})
|
||||
|
||||
It("updates the repository with a range of blocks w/in the range ", func() {
|
||||
blockchain := fakes.NewMockBlockChainWithBlocks([]core.Block{
|
||||
{Number: 1},
|
||||
{Number: 2},
|
||||
{Number: 3},
|
||||
{Number: 4},
|
||||
{Number: 5},
|
||||
})
|
||||
blockChain := fakes.NewMockBlockChain()
|
||||
|
||||
history.RetrieveAndUpdateBlocks(blockchain, blockRepository, history.MakeRange(2, 5))
|
||||
Expect(blockRepository.BlockCount()).To(Equal(4))
|
||||
Expect(blockRepository.CreateOrUpdateBlockCallCount).To(Equal(4))
|
||||
history.RetrieveAndUpdateBlocks(blockChain, blockRepository, history.MakeRange(2, 5))
|
||||
|
||||
blockRepository.AssertCreateOrUpdateBlocksCallCountAndBlockNumbersEquals(4, []int64{2, 3, 4, 5})
|
||||
})
|
||||
|
||||
It("does not call repository create block when there is an error", func() {
|
||||
blockchain := fakes.NewMockBlockChain(errors.New("error getting block"))
|
||||
blockChain := fakes.NewMockBlockChain()
|
||||
blockChain.SetGetBlockByNumberErr(fakes.FakeError)
|
||||
blocks := history.MakeRange(1, 10)
|
||||
history.RetrieveAndUpdateBlocks(blockchain, blockRepository, blocks)
|
||||
Expect(blockRepository.BlockCount()).To(Equal(0))
|
||||
Expect(blockRepository.CreateOrUpdateBlockCallCount).To(Equal(0))
|
||||
|
||||
history.RetrieveAndUpdateBlocks(blockChain, blockRepository, blocks)
|
||||
|
||||
blockRepository.AssertCreateOrUpdateBlockCallCountEquals(0)
|
||||
})
|
||||
})
|
||||
|
||||
@@ -6,7 +6,7 @@ import (
|
||||
"log"
|
||||
)
|
||||
|
||||
func PopulateMissingHeaders(blockchain core.Blockchain, headerRepository datastore.HeaderRepository, startingBlockNumber int64) int {
|
||||
func PopulateMissingHeaders(blockchain core.BlockChain, headerRepository datastore.HeaderRepository, startingBlockNumber int64) int {
|
||||
lastBlock := blockchain.LastBlock().Int64()
|
||||
blockRange := headerRepository.MissingBlockNumbers(startingBlockNumber, lastBlock, blockchain.Node().ID)
|
||||
log.SetPrefix("")
|
||||
@@ -15,7 +15,7 @@ func PopulateMissingHeaders(blockchain core.Blockchain, headerRepository datasto
|
||||
return len(blockRange)
|
||||
}
|
||||
|
||||
func RetrieveAndUpdateHeaders(blockchain core.Blockchain, headerRepository datastore.HeaderRepository, blockNumbers []int64) int {
|
||||
func RetrieveAndUpdateHeaders(blockchain core.BlockChain, headerRepository datastore.HeaderRepository, blockNumbers []int64) int {
|
||||
for _, blockNumber := range blockNumbers {
|
||||
header, err := blockchain.GetHeaderByNumber(blockNumber)
|
||||
if err != nil {
|
||||
|
||||
@@ -1,33 +1,29 @@
|
||||
package history_test
|
||||
|
||||
import (
|
||||
"math/big"
|
||||
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/core"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/datastore/inmemory"
|
||||
|
||||
"github.com/vulcanize/vulcanizedb/pkg/fakes"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/history"
|
||||
)
|
||||
|
||||
var _ = Describe("Populating headers", func() {
|
||||
|
||||
var inMemory *inmemory.InMemory
|
||||
var headerRepository *inmemory.HeaderRepository
|
||||
var headerRepository *fakes.MockHeaderRepository
|
||||
|
||||
BeforeEach(func() {
|
||||
inMemory = inmemory.NewInMemory()
|
||||
headerRepository = inmemory.NewHeaderRepository(inMemory)
|
||||
headerRepository = fakes.NewMockHeaderRepository()
|
||||
})
|
||||
|
||||
Describe("When 1 missing header", func() {
|
||||
|
||||
It("returns number of headers added", func() {
|
||||
headers := []core.Header{
|
||||
{BlockNumber: 1},
|
||||
{BlockNumber: 2},
|
||||
}
|
||||
blockChain := fakes.NewMockBlockChainWithHeaders(headers)
|
||||
headerRepository.CreateOrUpdateHeader(core.Header{BlockNumber: 2})
|
||||
blockChain := fakes.NewMockBlockChain()
|
||||
blockChain.SetLastBlock(big.NewInt(2))
|
||||
headerRepository.SetMissingBlockNumbers([]int64{2})
|
||||
|
||||
headersAdded := history.PopulateMissingHeaders(blockChain, headerRepository, 1)
|
||||
|
||||
@@ -36,17 +32,12 @@ var _ = Describe("Populating headers", func() {
|
||||
})
|
||||
|
||||
It("adds missing headers to the db", func() {
|
||||
headers := []core.Header{
|
||||
{BlockNumber: 1},
|
||||
{BlockNumber: 2},
|
||||
}
|
||||
blockChain := fakes.NewMockBlockChainWithHeaders(headers)
|
||||
dbHeader, _ := headerRepository.GetHeader(1)
|
||||
Expect(dbHeader.BlockNumber).To(BeZero())
|
||||
blockChain := fakes.NewMockBlockChain()
|
||||
blockChain.SetLastBlock(big.NewInt(2))
|
||||
headerRepository.SetMissingBlockNumbers([]int64{2})
|
||||
|
||||
history.PopulateMissingHeaders(blockChain, headerRepository, 1)
|
||||
|
||||
dbHeader, _ = headerRepository.GetHeader(1)
|
||||
Expect(dbHeader.BlockNumber).To(Equal(int64(1)))
|
||||
headerRepository.AssertCreateOrUpdateHeaderCallCountAndPassedBlockNumbers(1, []int64{2})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -22,7 +22,7 @@ func (window ValidationWindow) Size() int {
|
||||
return int(window.UpperBound - window.LowerBound)
|
||||
}
|
||||
|
||||
func MakeValidationWindow(blockchain core.Blockchain, windowSize int) ValidationWindow {
|
||||
func MakeValidationWindow(blockchain core.BlockChain, windowSize int) ValidationWindow {
|
||||
upperBound := blockchain.LastBlock().Int64()
|
||||
lowerBound := upperBound - int64(windowSize)
|
||||
return ValidationWindow{lowerBound, upperBound}
|
||||
|
||||
@@ -6,22 +6,17 @@ import (
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
|
||||
"github.com/vulcanize/vulcanizedb/pkg/core"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/fakes"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/history"
|
||||
"math/big"
|
||||
)
|
||||
|
||||
var _ = Describe("", func() {
|
||||
var _ = Describe("Validation window", func() {
|
||||
It("creates a ValidationWindow equal to (HEAD-windowSize, HEAD)", func() {
|
||||
blockchain := fakes.NewMockBlockChainWithBlocks([]core.Block{
|
||||
{Number: 1},
|
||||
{Number: 2},
|
||||
{Number: 3},
|
||||
{Number: 4},
|
||||
{Number: 5},
|
||||
})
|
||||
blockChain := fakes.NewMockBlockChain()
|
||||
blockChain.SetLastBlock(big.NewInt(5))
|
||||
|
||||
validationWindow := history.MakeValidationWindow(blockchain, 2)
|
||||
validationWindow := history.MakeValidationWindow(blockChain, 2)
|
||||
|
||||
Expect(validationWindow.LowerBound).To(Equal(int64(3)))
|
||||
Expect(validationWindow.UpperBound).To(Equal(int64(5)))
|
||||
|
||||
Reference in New Issue
Block a user