Fix small issues from review

This commit is contained in:
Edvard 2019-02-20 12:01:19 +01:00
parent 13eacf2081
commit 1a2f46b308
5 changed files with 33 additions and 15 deletions

View File

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

View File

@ -49,10 +49,8 @@ func (blockRepository BlockRepository) SetBlocksStatus(chainHead int64) error {
UPDATE blocks SET is_final = TRUE UPDATE blocks SET is_final = TRUE
WHERE is_final = FALSE AND number < $1`, WHERE is_final = FALSE AND number < $1`,
cutoff) cutoff)
if err != nil {
return err return err
}
return nil
} }
func (blockRepository BlockRepository) CreateOrUpdateBlock(block core.Block) (int64, error) { func (blockRepository BlockRepository) CreateOrUpdateBlock(block core.Block) (int64, error) {

View File

@ -40,13 +40,19 @@ func (logRepository LogRepository) CreateLogs(lgs []core.Log, receiptId int64) e
tlog.BlockNumber, tlog.Address, tlog.TxHash, tlog.Index, tlog.Topics[0], tlog.Topics[1], tlog.Topics[2], tlog.Topics[3], tlog.Data, receiptId, tlog.BlockNumber, tlog.Address, tlog.TxHash, tlog.Index, tlog.Topics[0], tlog.Topics[1], tlog.Topics[2], tlog.Topics[3], tlog.Data, receiptId,
) )
if err != nil { if err != nil {
tx.Rollback() err = tx.Rollback()
if err != nil {
logrus.Error("CreateLogs: could not perform rollback: ", err)
}
return postgres.ErrDBInsertFailed return postgres.ErrDBInsertFailed
} }
} }
err := tx.Commit() err := tx.Commit()
if err != nil { if err != nil {
tx.Rollback() err = tx.Rollback()
if err != nil {
logrus.Error("CreateLogs: could not perform rollback: ", err)
}
return postgres.ErrDBInsertFailed return postgres.ErrDBInsertFailed
} }
return nil return nil
@ -83,7 +89,8 @@ func (logRepository LogRepository) loadLogs(logsRows *sql.Rows) ([]core.Log, err
var topics core.Topics var topics core.Topics
err := logsRows.Scan(&blockNumber, &address, &txHash, &index, &topics[0], &topics[1], &topics[2], &topics[3], &data) err := logsRows.Scan(&blockNumber, &address, &txHash, &index, &topics[0], &topics[1], &topics[2], &topics[3], &data)
if err != nil { if err != nil {
logrus.Warn("loadLogs: Error scanning a row in logRows") logrus.Error("loadLogs: Error scanning a row in logRows: ", err)
return []core.Log{}, err
} }
lg := core.Log{ lg := core.Log{
BlockNumber: blockNumber, BlockNumber: blockNumber,

View File

@ -56,7 +56,7 @@ var _ = Describe("Logs Repository", func() {
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
receiptId, err := receiptRepository.CreateReceipt(blockId, core.Receipt{}) receiptId, err := receiptRepository.CreateReceipt(blockId, core.Receipt{})
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
logsRepository.CreateLogs([]core.Log{{ err = logsRepository.CreateLogs([]core.Log{{
BlockNumber: blockNumber, BlockNumber: blockNumber,
Index: 0, Index: 0,
Address: "x123", Address: "x123",
@ -64,6 +64,7 @@ var _ = Describe("Logs Repository", func() {
Topics: core.Topics{0: "x777", 1: "x888", 2: "x999"}, Topics: core.Topics{0: "x777", 1: "x888", 2: "x999"},
Data: "xabc", Data: "xabc",
}}, receiptId) }}, receiptId)
Expect(err).NotTo(HaveOccurred())
log, err := logsRepository.GetLogs("x123", blockNumber) log, err := logsRepository.GetLogs("x123", blockNumber)
@ -91,7 +92,8 @@ var _ = Describe("Logs Repository", func() {
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
receiptId, err := receiptRepository.CreateReceipt(blockId, core.Receipt{}) receiptId, err := receiptRepository.CreateReceipt(blockId, core.Receipt{})
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
_ = logsRepository.CreateLogs([]core.Log{{
err = logsRepository.CreateLogs([]core.Log{{
BlockNumber: blockNumber, BlockNumber: blockNumber,
Index: 0, Index: 0,
Address: "x123", Address: "x123",
@ -99,7 +101,9 @@ var _ = Describe("Logs Repository", func() {
Topics: core.Topics{0: "x777", 1: "x888", 2: "x999"}, Topics: core.Topics{0: "x777", 1: "x888", 2: "x999"},
Data: "xabc", Data: "xabc",
}}, receiptId) }}, receiptId)
_ = logsRepository.CreateLogs([]core.Log{{ Expect(err).NotTo(HaveOccurred())
err = logsRepository.CreateLogs([]core.Log{{
BlockNumber: blockNumber, BlockNumber: blockNumber,
Index: 1, Index: 1,
Address: "x123", Address: "x123",
@ -107,7 +111,9 @@ var _ = Describe("Logs Repository", func() {
Topics: core.Topics{0: "x111", 1: "x222", 2: "x333"}, Topics: core.Topics{0: "x111", 1: "x222", 2: "x333"},
Data: "xdef", Data: "xdef",
}}, receiptId) }}, receiptId)
_ = logsRepository.CreateLogs([]core.Log{{ Expect(err).NotTo(HaveOccurred())
err = logsRepository.CreateLogs([]core.Log{{
BlockNumber: 2, BlockNumber: 2,
Index: 0, Index: 0,
Address: "x123", Address: "x123",
@ -115,6 +121,7 @@ var _ = Describe("Logs Repository", func() {
Topics: core.Topics{0: "x777", 1: "x888", 2: "x999"}, Topics: core.Topics{0: "x777", 1: "x888", 2: "x999"},
Data: "xabc", Data: "xabc",
}}, receiptId) }}, receiptId)
Expect(err).NotTo(HaveOccurred())
log, err := logsRepository.GetLogs("x123", blockNumber) log, err := logsRepository.GetLogs("x123", blockNumber)
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())

View File

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