Skip to content
This repository has been archived by the owner on Aug 31, 2021. It is now read-only.

Commit

Permalink
Update tests for error propagation
Browse files Browse the repository at this point in the history
  • Loading branch information
m0ar committed Feb 14, 2019
1 parent 6cd4e5e commit 13eacf2
Show file tree
Hide file tree
Showing 15 changed files with 54 additions and 62 deletions.
4 changes: 2 additions & 2 deletions integration_test/geth_blockchain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand All @@ -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)))
Expand Down
3 changes: 2 additions & 1 deletion pkg/datastore/postgres/postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
17 changes: 0 additions & 17 deletions pkg/datastore/postgres/repositories/header_repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
})
})
})
19 changes: 12 additions & 7 deletions pkg/datastore/postgres/repositories/logs_repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand All @@ -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())
})

Expand All @@ -89,23 +91,23 @@ 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",
TxHash: "x456",
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",
TxHash: "x789",
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",
Expand All @@ -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,
Expand Down Expand Up @@ -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))
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
})
Expand Down
3 changes: 2 additions & 1 deletion pkg/fakes/mock_block_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions pkg/fakes/mock_blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
8 changes: 0 additions & 8 deletions pkg/fakes/mock_header_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/geth/blockchain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Expand Down
15 changes: 8 additions & 7 deletions pkg/geth/cold_import/importer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
})

Expand All @@ -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)
})
Expand Down
6 changes: 2 additions & 4 deletions pkg/geth/converters/common/header_converter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
Expand All @@ -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))
Expand Down
5 changes: 3 additions & 2 deletions pkg/history/block_validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)))
})
Expand Down
17 changes: 11 additions & 6 deletions pkg/history/populate_blocks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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})
})
Expand All @@ -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})
})

Expand All @@ -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)
})
})
1 change: 0 additions & 1 deletion pkg/history/populate_headers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
3 changes: 2 additions & 1 deletion pkg/history/validation_window_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
})
Expand Down

0 comments on commit 13eacf2

Please sign in to comment.