Update tests for error propagation

This commit is contained in:
Edvard 2019-02-14 16:04:12 +01:00
parent 6cd4e5ea95
commit 13eacf2081
15 changed files with 54 additions and 62 deletions

View File

@ -48,7 +48,7 @@ var _ = Describe("Reading from the Geth blockchain", func() {
It("reads two blocks", func(done Done) {
blocks := fakes.NewMockBlockRepository()
lastBlock := blockChain.LastBlock()
lastBlock, _ := blockChain.LastBlock()
queriedBlocks := []int64{lastBlock.Int64() - 5, lastBlock.Int64() - 6}
history.RetrieveAndUpdateBlocks(blockChain, blocks, queriedBlocks)
blocks.AssertCreateOrUpdateBlocksCallCountAndBlockNumbersEquals(2, []int64{lastBlock.Int64() - 5, lastBlock.Int64() - 6})
@ -60,7 +60,7 @@ var _ = Describe("Reading from the Geth blockchain", func() {
Expect(err).ToNot(HaveOccurred())
firstBlock, err := blockChain.GetBlockByNumber(int64(1))
Expect(err).ToNot(HaveOccurred())
lastBlockNumber := blockChain.LastBlock()
lastBlockNumber, _ := blockChain.LastBlock()
Expect(genesisBlock.Number).To(Equal(int64(0)))
Expect(firstBlock.Number).To(Equal(int64(1)))

View File

@ -134,8 +134,9 @@ var _ = Describe("Postgres DB", func() {
err := logRepository.CreateLogs([]core.Log{badLog}, 123)
Expect(err).ToNot(BeNil())
savedBlock := logRepository.GetLogs("x123", 1)
savedBlock, err := logRepository.GetLogs("x123", 1)
Expect(savedBlock).To(BeNil())
Expect(err).To(Not(HaveOccurred()))
})
It("does not commit block or transactions if transaction is invalid", func() {

View File

@ -273,21 +273,4 @@ var _ = Describe("Block header repository", func() {
Expect(missingBlockNumbers).To(ConsistOf([]int64{1, 2, 3, 4, 5}))
})
})
Describe("HeaderExists", func() {
It("returns true if the header record exists", func() {
_, err = repo.CreateOrUpdateHeader(header)
Expect(err).NotTo(HaveOccurred())
result, err := repo.HeaderExists(header.BlockNumber)
Expect(err).NotTo(HaveOccurred())
Expect(result).To(BeTrue())
})
It("returns false if the header record doesn't exist", func() {
result, err := repo.HeaderExists(1)
Expect(err).NotTo(HaveOccurred())
Expect(result).To(BeFalse())
})
})
})

View File

@ -65,8 +65,9 @@ var _ = Describe("Logs Repository", func() {
Data: "xabc",
}}, receiptId)
log := logsRepository.GetLogs("x123", blockNumber)
log, err := logsRepository.GetLogs("x123", blockNumber)
Expect(err).NotTo(HaveOccurred())
Expect(log).NotTo(BeNil())
Expect(log[0].BlockNumber).To(Equal(blockNumber))
Expect(log[0].Address).To(Equal("x123"))
@ -79,7 +80,8 @@ var _ = Describe("Logs Repository", func() {
})
It("returns nil if log does not exist", func() {
log := logsRepository.GetLogs("x123", 1)
log, err := logsRepository.GetLogs("x123", 1)
Expect(err).NotTo(HaveOccurred())
Expect(log).To(BeNil())
})
@ -89,7 +91,7 @@ var _ = Describe("Logs Repository", func() {
Expect(err).NotTo(HaveOccurred())
receiptId, err := receiptRepository.CreateReceipt(blockId, core.Receipt{})
Expect(err).NotTo(HaveOccurred())
logsRepository.CreateLogs([]core.Log{{
_ = logsRepository.CreateLogs([]core.Log{{
BlockNumber: blockNumber,
Index: 0,
Address: "x123",
@ -97,7 +99,7 @@ var _ = Describe("Logs Repository", func() {
Topics: core.Topics{0: "x777", 1: "x888", 2: "x999"},
Data: "xabc",
}}, receiptId)
logsRepository.CreateLogs([]core.Log{{
_ = logsRepository.CreateLogs([]core.Log{{
BlockNumber: blockNumber,
Index: 1,
Address: "x123",
@ -105,7 +107,7 @@ var _ = Describe("Logs Repository", func() {
Topics: core.Topics{0: "x111", 1: "x222", 2: "x333"},
Data: "xdef",
}}, receiptId)
logsRepository.CreateLogs([]core.Log{{
_ = logsRepository.CreateLogs([]core.Log{{
BlockNumber: 2,
Index: 0,
Address: "x123",
@ -114,12 +116,14 @@ var _ = Describe("Logs Repository", func() {
Data: "xabc",
}}, receiptId)
log := logsRepository.GetLogs("x123", blockNumber)
log, err := logsRepository.GetLogs("x123", blockNumber)
Expect(err).NotTo(HaveOccurred())
type logIndex struct {
blockNumber int64
Index int64
}
var uniqueBlockNumbers []logIndex
for _, log := range log {
uniqueBlockNumbers = append(uniqueBlockNumbers,
@ -199,8 +203,9 @@ var _ = Describe("Logs Repository", func() {
block := core.Block{Transactions: []core.Transaction{transaction}}
_, err := blockRepository.CreateOrUpdateBlock(block)
Expect(err).To(Not(HaveOccurred()))
retrievedLogs := logsRepository.GetLogs("0x99041f808d598b782d5a3e498681c2452a31da08", 4745407)
retrievedLogs, err := logsRepository.GetLogs("0x99041f808d598b782d5a3e498681c2452a31da08", 4745407)
Expect(err).NotTo(HaveOccurred())
expected := logs[1:]
Expect(retrievedLogs).To(Equal(expected))
})

View File

@ -87,14 +87,19 @@ var _ = Describe("Receipts Repository", func() {
Expect(err).NotTo(HaveOccurred())
Expect(persistedReceiptOne).NotTo(BeNil())
Expect(persistedReceiptOne.TxHash).To(Equal(txHashOne))
persistedReceiptTwo, err := receiptRepository.GetReceipt(txHashTwo)
Expect(err).NotTo(HaveOccurred())
Expect(persistedReceiptTwo).NotTo(BeNil())
Expect(persistedReceiptTwo.TxHash).To(Equal(txHashTwo))
persistedAddressOneLogs := logRepository.GetLogs(addressOne, blockNumber)
persistedAddressOneLogs, err := logRepository.GetLogs(addressOne, blockNumber)
Expect(err).NotTo(HaveOccurred())
Expect(persistedAddressOneLogs).NotTo(BeNil())
Expect(len(persistedAddressOneLogs)).To(Equal(2))
persistedAddressTwoLogs := logRepository.GetLogs(addressTwo, blockNumber)
persistedAddressTwoLogs, err := logRepository.GetLogs(addressTwo, blockNumber)
Expect(err).NotTo(HaveOccurred())
Expect(persistedAddressTwoLogs).NotTo(BeNil())
Expect(len(persistedAddressTwoLogs)).To(Equal(1))
})

View File

@ -85,9 +85,10 @@ func (repository *MockBlockRepository) MissingBlockNumbers(startingBlockNumber i
return repository.missingBlockNumbersReturnArray
}
func (repository *MockBlockRepository) SetBlocksStatus(chainHead int64) {
func (repository *MockBlockRepository) SetBlocksStatus(chainHead int64) error {
repository.setBlockStatusCalled = true
repository.setBlockStatusPassedChainHead = chainHead
return nil
}
func (repository *MockBlockRepository) AssertCreateOrUpdateBlockCallCountEquals(times int) {

View File

@ -108,8 +108,8 @@ func (chain *MockBlockChain) CallContract(contractHash string, input []byte, blo
return []byte{}, nil
}
func (chain *MockBlockChain) LastBlock() *big.Int {
return chain.lastBlock
func (chain *MockBlockChain) LastBlock() (*big.Int, error) {
return chain.lastBlock, nil
}
func (chain *MockBlockChain) Node() core.Node {

View File

@ -65,14 +65,6 @@ func (repository *MockHeaderRepository) MissingBlockNumbers(startingBlockNumber,
return repository.missingBlockNumbers, nil
}
func (repository *MockHeaderRepository) HeaderExists(blockNumber int64) (bool, error) {
return repository.headerExists, nil
}
func (repository *MockHeaderRepository) SetHeaderExists(headerExists bool) {
repository.headerExists = headerExists
}
func (repository *MockHeaderRepository) SetGetHeaderError(err error) {
repository.getHeaderError = err
}

View File

@ -221,7 +221,7 @@ var _ = Describe("Geth blockchain", func() {
blockNumber := int64(100)
mockClient.SetHeaderByNumberReturnHeader(&types.Header{Number: big.NewInt(blockNumber)})
result := blockChain.LastBlock()
result, _ := blockChain.LastBlock()
mockClient.AssertHeaderByNumberCalledWith(context.Background(), nil)
Expect(result).To(Equal(big.NewInt(blockNumber)))

View File

@ -20,7 +20,6 @@ import (
"github.com/ethereum/go-ethereum/core/types"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres/repositories"
"github.com/vulcanize/vulcanizedb/pkg/fakes"
"github.com/vulcanize/vulcanizedb/pkg/geth/cold_import"
@ -55,7 +54,8 @@ var _ = Describe("Geth cold importer", func() {
mockEthereumDatabase.SetReturnBlock(fakeGethBlock)
importer := cold_import.NewColdImporter(mockEthereumDatabase, mockBlockRepository, mockReceiptRepository, blockConverter)
importer.Execute(startingBlockNumber, endingBlockNumber, nodeId)
err := importer.Execute(startingBlockNumber, endingBlockNumber, nodeId)
Expect(err).NotTo(HaveOccurred())
mockBlockRepository.AssertMissingBlockNumbersCalledWith(startingBlockNumber, endingBlockNumber, nodeId)
mockEthereumDatabase.AssertGetBlockHashCalledWith(missingBlockNumber)
@ -76,7 +76,8 @@ var _ = Describe("Geth cold importer", func() {
mockEthereumDatabase.SetReturnBlock(fakeGethBlock)
importer := cold_import.NewColdImporter(mockEthereumDatabase, mockBlockRepository, mockReceiptRepository, blockConverter)
importer.Execute(blockNumber, blockNumber, "node_id")
err := importer.Execute(blockNumber, blockNumber, "node_id")
Expect(err).NotTo(HaveOccurred())
mockEthereumDatabase.AssertGetBlockHashCalledWith(blockNumber)
mockEthereumDatabase.AssertGetBlockCalledWith(fakeHash, blockNumber)
@ -101,8 +102,8 @@ var _ = Describe("Geth cold importer", func() {
mockEthereumDatabase.SetReturnBlock(fakeGethBlock)
importer := cold_import.NewColdImporter(mockEthereumDatabase, mockBlockRepository, mockReceiptRepository, blockConverter)
importer.Execute(startingBlockNumber, endingBlockNumber, "node_id")
err := importer.Execute(startingBlockNumber, endingBlockNumber, "node_id")
Expect(err).NotTo(HaveOccurred())
mockBlockRepository.AssertSetBlockStatusCalledWith(endingBlockNumber)
})
@ -122,8 +123,8 @@ var _ = Describe("Geth cold importer", func() {
mockEthereumDatabase.SetReturnReceipts(fakeReceipts)
importer := cold_import.NewColdImporter(mockEthereumDatabase, mockBlockRepository, mockReceiptRepository, blockConverter)
importer.Execute(blockNumber, blockNumber, "node_id")
err := importer.Execute(blockNumber, blockNumber, "node_id")
Expect(err).NotTo(HaveOccurred())
expectedReceipts := vulcCommon.ToCoreReceipts(fakeReceipts)
mockReceiptRepository.AssertCreateReceiptsAndLogsCalledWith(blockId, expectedReceipts)
})

View File

@ -44,9 +44,8 @@ var _ = Describe("Block header converter", func() {
converter := common2.HeaderConverter{}
hash := fakes.FakeHash.String()
coreHeader, err := converter.Convert(gethHeader, hash)
coreHeader := converter.Convert(gethHeader, hash)
Expect(err).NotTo(HaveOccurred())
Expect(coreHeader.BlockNumber).To(Equal(gethHeader.Number.Int64()))
Expect(coreHeader.Hash).To(Equal(hash))
Expect(coreHeader.Timestamp).To(Equal(gethHeader.Time.String()))
@ -56,9 +55,8 @@ var _ = Describe("Block header converter", func() {
gethHeader := types.Header{Number: big.NewInt(123)}
converter := common2.HeaderConverter{}
coreHeader, err := converter.Convert(&gethHeader, fakes.FakeHash.String())
coreHeader := converter.Convert(&gethHeader, fakes.FakeHash.String())
Expect(err).NotTo(HaveOccurred())
expectedJSON, err := json.Marshal(gethHeader)
Expect(err).NotTo(HaveOccurred())
Expect(coreHeader.Raw).To(Equal(expectedJSON))

View File

@ -34,7 +34,8 @@ var _ = Describe("Blocks validator", func() {
blocksRepository := fakes.NewMockBlockRepository()
validator := history.NewBlockValidator(blockChain, blocksRepository, 2)
window := validator.ValidateBlocks()
window, err := validator.ValidateBlocks()
Expect(err).NotTo(HaveOccurred())
Expect(window).To(Equal(history.ValidationWindow{LowerBound: 5, UpperBound: 7}))
blocksRepository.AssertCreateOrUpdateBlockCallCountEquals(3)
@ -43,7 +44,7 @@ var _ = Describe("Blocks validator", func() {
It("returns the number of largest block", func() {
blockChain := fakes.NewMockBlockChain()
blockChain.SetLastBlock(big.NewInt(3))
maxBlockNumber := blockChain.LastBlock()
maxBlockNumber, _ := blockChain.LastBlock()
Expect(maxBlockNumber.Int64()).To(Equal(int64(3)))
})

View File

@ -36,8 +36,9 @@ var _ = Describe("Populating blocks", func() {
blockChain.SetLastBlock(big.NewInt(2))
blockRepository.SetMissingBlockNumbersReturnArray([]int64{2})
blocksAdded := history.PopulateMissingBlocks(blockChain, blockRepository, 1)
_, err := blockRepository.GetBlock(1)
blocksAdded, err := history.PopulateMissingBlocks(blockChain, blockRepository, 1)
Expect(err).NotTo(HaveOccurred())
_, err = blockRepository.GetBlock(1)
Expect(blocksAdded).To(Equal(1))
Expect(err).ToNot(HaveOccurred())
@ -48,8 +49,9 @@ var _ = Describe("Populating blocks", func() {
blockChain.SetLastBlock(big.NewInt(13))
blockRepository.SetMissingBlockNumbersReturnArray([]int64{5, 8, 10})
blocksAdded := history.PopulateMissingBlocks(blockChain, blockRepository, 5)
blocksAdded, err := history.PopulateMissingBlocks(blockChain, blockRepository, 5)
Expect(err).NotTo(HaveOccurred())
Expect(blocksAdded).To(Equal(3))
blockRepository.AssertCreateOrUpdateBlocksCallCountAndBlockNumbersEquals(3, []int64{5, 8, 10})
})
@ -59,16 +61,18 @@ var _ = Describe("Populating blocks", func() {
blockChain.SetLastBlock(big.NewInt(6))
blockRepository.SetMissingBlockNumbersReturnArray([]int64{4, 5})
numberOfBlocksCreated := history.PopulateMissingBlocks(blockChain, blockRepository, 3)
numberOfBlocksCreated, err := history.PopulateMissingBlocks(blockChain, blockRepository, 3)
Expect(err).NotTo(HaveOccurred())
Expect(numberOfBlocksCreated).To(Equal(2))
})
It("updates the repository with a range of blocks w/in the range ", func() {
blockChain := fakes.NewMockBlockChain()
history.RetrieveAndUpdateBlocks(blockChain, blockRepository, history.MakeRange(2, 5))
_, err := history.RetrieveAndUpdateBlocks(blockChain, blockRepository, history.MakeRange(2, 5))
Expect(err).NotTo(HaveOccurred())
blockRepository.AssertCreateOrUpdateBlocksCallCountAndBlockNumbersEquals(4, []int64{2, 3, 4, 5})
})
@ -77,8 +81,9 @@ var _ = Describe("Populating blocks", func() {
blockChain.SetGetBlockByNumberErr(fakes.FakeError)
blocks := history.MakeRange(1, 10)
history.RetrieveAndUpdateBlocks(blockChain, blockRepository, blocks)
_, err := history.RetrieveAndUpdateBlocks(blockChain, blockRepository, blocks)
Expect(err).To(HaveOccurred())
blockRepository.AssertCreateOrUpdateBlockCallCountEquals(0)
})
})

View File

@ -59,7 +59,6 @@ var _ = Describe("Populating headers", func() {
It("returns early if the db is already synced up to the head of the chain", func() {
blockChain := fakes.NewMockBlockChain()
blockChain.SetLastBlock(big.NewInt(2))
headerRepository.SetHeaderExists(true)
headersAdded, err := history.PopulateMissingHeaders(blockChain, headerRepository, 2)
Expect(err).NotTo(HaveOccurred())

View File

@ -30,8 +30,9 @@ var _ = Describe("Validation window", func() {
blockChain := fakes.NewMockBlockChain()
blockChain.SetLastBlock(big.NewInt(5))
validationWindow := history.MakeValidationWindow(blockChain, 2)
validationWindow, err := history.MakeValidationWindow(blockChain, 2)
Expect(err).NotTo(HaveOccurred())
Expect(validationWindow.LowerBound).To(Equal(int64(3)))
Expect(validationWindow.UpperBound).To(Equal(int64(5)))
})