From 246ae317486101268f181bf5a1f511eb0393509e Mon Sep 17 00:00:00 2001 From: facuMH Date: Tue, 17 Dec 2024 17:13:03 +0100 Subject: [PATCH] Add error handling in multiple files --- evmcore/tx_cacher.go | 5 ++++- evmcore/tx_pool.go | 9 +++++++-- gossip/handler.go | 16 +++++++++++++--- utils/file.go | 4 ++-- 4 files changed, 26 insertions(+), 8 deletions(-) diff --git a/evmcore/tx_cacher.go b/evmcore/tx_cacher.go index 7982365e6..089a97026 100644 --- a/evmcore/tx_cacher.go +++ b/evmcore/tx_cacher.go @@ -20,6 +20,7 @@ import ( "runtime" "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/log" ) // senderCacher is a concurrent transaction sender recoverer and cacher. @@ -62,7 +63,9 @@ func newTxSenderCacher(threads int) *txSenderCacher { func (cacher *txSenderCacher) cache() { for task := range cacher.tasks { for i := 0; i < len(task.txs); i += task.inc { - types.Sender(task.signer, task.txs[i]) + if _, err := types.Sender(task.signer, task.txs[i]); err != nil { + log.Warn("Failed to recover sender", "err", err) + } } } } diff --git a/evmcore/tx_pool.go b/evmcore/tx_pool.go index ba8aac022..de828816c 100644 --- a/evmcore/tx_pool.go +++ b/evmcore/tx_pool.go @@ -1654,7 +1654,9 @@ func (pool *TxPool) demoteUnexecutables() { log.Trace("Demoting pending transaction", "hash", hash) // Internal shuffle shouldn't touch the lookup set. - pool.enqueueTx(hash, tx, false, false) + if _, err := pool.enqueueTx(hash, tx, false, false); err != nil { + log.Warn("Failed to re-enqueue invalidated transaction", "hash", hash, "err", err) + } } pool.priced.Removed(len(olds) + len(drops)) // invalid are only moved into queue pendingGauge.Dec(int64(len(olds) + len(drops) + len(invalids))) @@ -1669,7 +1671,10 @@ func (pool *TxPool) demoteUnexecutables() { log.Error("Demoting invalidated transaction", "hash", hash) // Internal shuffle shouldn't touch the lookup set. - pool.enqueueTx(hash, tx, false, false) + if _, err := pool.enqueueTx(hash, tx, false, false); err != nil { + log.Warn("Failed to re-enqueue invalidated transaction", "hash", hash, "err", err) + } + } pendingGauge.Dec(int64(len(gapped))) } diff --git a/gossip/handler.go b/gossip/handler.go index 732c8a147..018049730 100644 --- a/gossip/handler.go +++ b/gossip/handler.go @@ -697,7 +697,10 @@ func (h *handler) handleMsg(p *peer) error { if msg.Size > protocolMaxMsgSize { return errResp(ErrMsgTooLarge, "%v > %v", msg.Size, protocolMaxMsgSize) } - defer msg.Discard() + defer func() { + // dicard is a writte operation into io.Discard, it cannot return an error + _ = msg.Discard() + }() // Acquire semaphore for serialized messages eventsSizeEst := dag.Metric{ Num: 1, @@ -1153,9 +1156,16 @@ func (h *handler) peerInfoCollectionLoop(stop <-chan struct{}) { for _, peer := range peers { // If we do not have the peer's end-point or it is too old, request it. if info := peer.endPoint.Load(); info == nil || time.Since(info.timestamp) > h.config.Protocol.PeerEndPointUpdatePeriod { - peer.SendEndPointUpdateRequest() + if err := peer.SendEndPointUpdateRequest(); err != nil { + log.Warn("Failed to send end-point update request", "peer", peer.id, "err", err) + // If the end-point update request fails, do not send the peer info request. + continue + } + } + + if err := peer.SendPeerInfoRequest(); err != nil { + log.Warn("Failed to send peer info request", "peer", peer.id, "err", err) } - peer.SendPeerInfoRequest() } // Drop a redundant connection if there are too many connections. diff --git a/utils/file.go b/utils/file.go index b9cfef383..559e36278 100644 --- a/utils/file.go +++ b/utils/file.go @@ -16,11 +16,11 @@ func OpenFile(path string, isSyncMode bool) *os.File { if isSyncMode { sync = os.O_SYNC } - fh, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|sync, 0666) + fileHandle, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|sync, 0666) if err != nil { log.Crit("Failed to open file", "file", path, "err", err) } - return fh + return fileHandle } func FileExists(path string) bool {