From 13eacf2081b6af8b679d00b0f7175cd994701ec6 Mon Sep 17 00:00:00 2001 From: Edvard Date: Thu, 14 Feb 2019 16:04:12 +0100 Subject: [PATCH] Update tests for error propagation --- integration_test/geth_blockchain_test.go | 4 ++-- pkg/datastore/postgres/postgres_test.go | 3 ++- .../repositories/header_repository_test.go | 17 ----------------- .../repositories/logs_repository_test.go | 19 ++++++++++++------- .../repositories/receipts_repository_test.go | 9 +++++++-- pkg/fakes/mock_block_repository.go | 3 ++- pkg/fakes/mock_blockchain.go | 4 ++-- pkg/fakes/mock_header_repository.go | 8 -------- pkg/geth/blockchain_test.go | 2 +- pkg/geth/cold_import/importer_test.go | 15 ++++++++------- .../common/header_converter_test.go | 6 ++---- pkg/history/block_validator_test.go | 5 +++-- pkg/history/populate_blocks_test.go | 17 +++++++++++------ pkg/history/populate_headers_test.go | 1 - pkg/history/validation_window_test.go | 3 ++- 15 files changed, 54 insertions(+), 62 deletions(-) diff --git a/integration_test/geth_blockchain_test.go b/integration_test/geth_blockchain_test.go index 54719774..2cd85cb3 100644 --- a/integration_test/geth_blockchain_test.go +++ b/integration_test/geth_blockchain_test.go @@ -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))) diff --git a/pkg/datastore/postgres/postgres_test.go b/pkg/datastore/postgres/postgres_test.go index 447665e3..ea68c49a 100644 --- a/pkg/datastore/postgres/postgres_test.go +++ b/pkg/datastore/postgres/postgres_test.go @@ -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() { diff --git a/pkg/datastore/postgres/repositories/header_repository_test.go b/pkg/datastore/postgres/repositories/header_repository_test.go index fbcd50bb..ba97195e 100644 --- a/pkg/datastore/postgres/repositories/header_repository_test.go +++ b/pkg/datastore/postgres/repositories/header_repository_test.go @@ -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()) - }) - }) }) diff --git a/pkg/datastore/postgres/repositories/logs_repository_test.go b/pkg/datastore/postgres/repositories/logs_repository_test.go index 1f88cc6c..1402d672 100644 --- a/pkg/datastore/postgres/repositories/logs_repository_test.go +++ b/pkg/datastore/postgres/repositories/logs_repository_test.go @@ -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)) }) diff --git a/pkg/datastore/postgres/repositories/receipts_repository_test.go b/pkg/datastore/postgres/repositories/receipts_repository_test.go index 4ba64db4..33572bb7 100644 --- a/pkg/datastore/postgres/repositories/receipts_repository_test.go +++ b/pkg/datastore/postgres/repositories/receipts_repository_test.go @@ -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)) }) diff --git a/pkg/fakes/mock_block_repository.go b/pkg/fakes/mock_block_repository.go index b39a034f..4eca601f 100644 --- a/pkg/fakes/mock_block_repository.go +++ b/pkg/fakes/mock_block_repository.go @@ -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) { diff --git a/pkg/fakes/mock_blockchain.go b/pkg/fakes/mock_blockchain.go index 1cedff56..80cdff2f 100644 --- a/pkg/fakes/mock_blockchain.go +++ b/pkg/fakes/mock_blockchain.go @@ -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 { diff --git a/pkg/fakes/mock_header_repository.go b/pkg/fakes/mock_header_repository.go index a8a4e6ba..05f06bb3 100644 --- a/pkg/fakes/mock_header_repository.go +++ b/pkg/fakes/mock_header_repository.go @@ -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 } diff --git a/pkg/geth/blockchain_test.go b/pkg/geth/blockchain_test.go index 217e5326..370563e2 100644 --- a/pkg/geth/blockchain_test.go +++ b/pkg/geth/blockchain_test.go @@ -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))) diff --git a/pkg/geth/cold_import/importer_test.go b/pkg/geth/cold_import/importer_test.go index 3e50b110..9277fd15 100644 --- a/pkg/geth/cold_import/importer_test.go +++ b/pkg/geth/cold_import/importer_test.go @@ -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) }) diff --git a/pkg/geth/converters/common/header_converter_test.go b/pkg/geth/converters/common/header_converter_test.go index 7eb12d83..c7d5c973 100644 --- a/pkg/geth/converters/common/header_converter_test.go +++ b/pkg/geth/converters/common/header_converter_test.go @@ -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)) diff --git a/pkg/history/block_validator_test.go b/pkg/history/block_validator_test.go index 3509e20e..2022dc6d 100644 --- a/pkg/history/block_validator_test.go +++ b/pkg/history/block_validator_test.go @@ -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))) }) diff --git a/pkg/history/populate_blocks_test.go b/pkg/history/populate_blocks_test.go index 5ff67a13..d629da28 100644 --- a/pkg/history/populate_blocks_test.go +++ b/pkg/history/populate_blocks_test.go @@ -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) }) }) diff --git a/pkg/history/populate_headers_test.go b/pkg/history/populate_headers_test.go index 6b06fbb4..398744e8 100644 --- a/pkg/history/populate_headers_test.go +++ b/pkg/history/populate_headers_test.go @@ -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()) diff --git a/pkg/history/validation_window_test.go b/pkg/history/validation_window_test.go index 50683be4..0d3ee1fa 100644 --- a/pkg/history/validation_window_test.go +++ b/pkg/history/validation_window_test.go @@ -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))) })