From 3fd6269b781c52de71e5949cfce3068a884062fb Mon Sep 17 00:00:00 2001 From: Rob Mulholand Date: Wed, 17 Apr 2019 16:10:58 -0500 Subject: [PATCH] Don't lookup transactions if no log events - Prevents EOF error on transactions lookup --- libraries/shared/transactions/syncer.go | 3 +++ libraries/shared/transactions/syncer_test.go | 15 +++++++++++---- pkg/geth/blockchain.go | 2 -- 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/libraries/shared/transactions/syncer.go b/libraries/shared/transactions/syncer.go index 8e043dd7..f8590e2b 100644 --- a/libraries/shared/transactions/syncer.go +++ b/libraries/shared/transactions/syncer.go @@ -28,6 +28,9 @@ func NewTransactionsSyncer(db *postgres.DB, blockChain core.BlockChain) Transact func (syncer TransactionsSyncer) SyncTransactions(headerID int64, logs []types.Log) error { transactionHashes := getUniqueTransactionHashes(logs) + if len(transactionHashes) < 1 { + return nil + } transactions, transactionErr := syncer.BlockChain.GetTransactions(transactionHashes) if transactionErr != nil { return transactionErr diff --git a/libraries/shared/transactions/syncer_test.go b/libraries/shared/transactions/syncer_test.go index 53df7817..f38f68d2 100644 --- a/libraries/shared/transactions/syncer_test.go +++ b/libraries/shared/transactions/syncer_test.go @@ -24,12 +24,19 @@ var _ = Describe("Transaction syncer", func() { }) It("fetches transactions for logs", func() { - err := syncer.SyncTransactions(0, []types.Log{}) + err := syncer.SyncTransactions(0, []types.Log{{TxHash: fakes.FakeHash}}) Expect(err).NotTo(HaveOccurred()) Expect(blockChain.GetTransactionsCalled).To(BeTrue()) }) + It("does not fetch transactions if no logs", func() { + err := syncer.SyncTransactions(0, []types.Log{}) + + Expect(err).NotTo(HaveOccurred()) + Expect(blockChain.GetTransactionsCalled).To(BeFalse()) + }) + It("only fetches transactions with unique hashes", func() { err := syncer.SyncTransactions(0, []types.Log{{ TxHash: fakes.FakeHash, @@ -44,7 +51,7 @@ var _ = Describe("Transaction syncer", func() { It("returns error if fetching transactions fails", func() { blockChain.GetTransactionsError = fakes.FakeError - err := syncer.SyncTransactions(0, []types.Log{}) + err := syncer.SyncTransactions(0, []types.Log{{TxHash: fakes.FakeHash}}) Expect(err).To(HaveOccurred()) Expect(err).To(MatchError(fakes.FakeError)) @@ -55,7 +62,7 @@ var _ = Describe("Transaction syncer", func() { mockHeaderRepository := fakes.NewMockHeaderRepository() syncer.Repository = mockHeaderRepository - err := syncer.SyncTransactions(0, []types.Log{}) + err := syncer.SyncTransactions(0, []types.Log{{TxHash: fakes.FakeHash}}) Expect(err).NotTo(HaveOccurred()) Expect(mockHeaderRepository.CreateTransactionsCalled).To(BeTrue()) @@ -67,7 +74,7 @@ var _ = Describe("Transaction syncer", func() { mockHeaderRepository.CreateTransactionsError = fakes.FakeError syncer.Repository = mockHeaderRepository - err := syncer.SyncTransactions(0, []types.Log{}) + err := syncer.SyncTransactions(0, []types.Log{{TxHash: fakes.FakeHash}}) Expect(err).To(HaveOccurred()) Expect(err).To(MatchError(fakes.FakeError)) diff --git a/pkg/geth/blockchain.go b/pkg/geth/blockchain.go index a2b641c7..5d2c156c 100644 --- a/pkg/geth/blockchain.go +++ b/pkg/geth/blockchain.go @@ -18,7 +18,6 @@ package geth import ( "errors" - "fmt" "github.com/ethereum/go-ethereum" "math/big" "strconv" @@ -122,7 +121,6 @@ func (blockChain *BlockChain) GetTransactions(transactionHashes []common.Hash) ( rpcErr := blockChain.rpcClient.BatchCall(batch) if rpcErr != nil { - fmt.Println("rpc err") return []core.TransactionModel{}, rpcErr }