From 49cace8efa90dfbda04769105596f76ab2b552fe Mon Sep 17 00:00:00 2001 From: pingke Date: Wed, 3 Apr 2024 16:22:40 +0800 Subject: [PATCH 1/2] fix crash --- ethstorage/p2p/protocol/peer.go | 11 ----------- ethstorage/p2p/protocol/syncclient.go | 9 +++++++-- 2 files changed, 7 insertions(+), 13 deletions(-) diff --git a/ethstorage/p2p/protocol/peer.go b/ethstorage/p2p/protocol/peer.go index b8fb2a65..66229d83 100644 --- a/ethstorage/p2p/protocol/peer.go +++ b/ethstorage/p2p/protocol/peer.go @@ -5,7 +5,6 @@ package protocol import ( "context" - "fmt" "math/big" "github.com/ethereum/go-ethereum/common" @@ -57,16 +56,6 @@ func (p *Peer) Shards() map[common.Address][]uint64 { return p.shards } -// SetShards this should only be set when doing handshake. -func (p *Peer) SetShards(shards map[common.Address][]uint64) error { - // shards can only be set once. - if p.shards != nil { - return fmt.Errorf("peer shards has been set multi times") - } - p.shards = shards - return nil -} - // IsShardExist checks whether one specific shard is supported by this peer. func (p *Peer) IsShardExist(contract common.Address, shardId uint64) bool { if ids, ok := p.shards[contract]; ok { diff --git a/ethstorage/p2p/protocol/syncclient.go b/ethstorage/p2p/protocol/syncclient.go index b90a243c..6f02a6b1 100644 --- a/ethstorage/p2p/protocol/syncclient.go +++ b/ethstorage/p2p/protocol/syncclient.go @@ -848,8 +848,8 @@ func (s *SyncClient) getIdlePeerForTask(t *task) *Peer { if _, ok := t.statelessPeers[id]; ok { continue } - p := s.peers[id] - if p.IsShardExist(t.Contract, t.ShardId) { + p, ok := s.peers[id] + if ok && p.IsShardExist(t.Contract, t.ShardId) { return p } } @@ -1200,6 +1200,7 @@ func (s *SyncClient) ReportPeerSummary() { select { case <-ticker.C: inbound, outbound := 0, 0 + s.lock.Lock() for _, p := range s.peers { if p.direction == network.DirInbound { inbound++ @@ -1207,6 +1208,7 @@ func (s *SyncClient) ReportPeerSummary() { outbound++ } } + s.lock.Unlock() log.Info("P2P Summary", "activePeers", len(s.peers), "inbound", inbound, "outbound", outbound) case <-s.resCtx.Done(): log.Info("P2P summary stop") @@ -1217,6 +1219,9 @@ func (s *SyncClient) ReportPeerSummary() { } func (s *SyncClient) needThisPeer(contractShards map[common.Address][]uint64) bool { + if contractShards == nil { + return false + } for contract, shards := range contractShards { for _, shard := range shards { for _, t := range s.tasks { From 7325934b1326468fba7c77e386bc01053eba1150 Mon Sep 17 00:00:00 2001 From: pingke Date: Sat, 6 Apr 2024 09:47:07 +0800 Subject: [PATCH 2/2] resolve comments --- ethstorage/p2p/protocol/syncclient.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ethstorage/p2p/protocol/syncclient.go b/ethstorage/p2p/protocol/syncclient.go index 6f02a6b1..c8abf82a 100644 --- a/ethstorage/p2p/protocol/syncclient.go +++ b/ethstorage/p2p/protocol/syncclient.go @@ -1208,8 +1208,8 @@ func (s *SyncClient) ReportPeerSummary() { outbound++ } } - s.lock.Unlock() log.Info("P2P Summary", "activePeers", len(s.peers), "inbound", inbound, "outbound", outbound) + s.lock.Unlock() case <-s.resCtx.Done(): log.Info("P2P summary stop") return