Skip to content

Commit

Permalink
remove Neighbors()
Browse files Browse the repository at this point in the history
  • Loading branch information
Liuhaai committed Jun 28, 2022
1 parent 700d1a0 commit aea991b
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 42 deletions.
2 changes: 1 addition & 1 deletion chainservice/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ func (builder *Builder) buildBlockSyncer() error {
consens.Calibrate(blk.Height())
return nil
},
p2pAgent.Neighbors,
p2pAgent.ConnectedPeers,
p2pAgent.UnicastOutbound,
p2pAgent.BlockPeer,
)
Expand Down
28 changes: 0 additions & 28 deletions p2p/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,6 @@ type (
Info() (peer.AddrInfo, error)
// Self returns the self network address
Self() ([]multiaddr.Multiaddr, error)
// Neighbors returns the neighbors' peer info
Neighbors(ctx context.Context) ([]peer.AddrInfo, error)
// ConnectedPeers returns the connected peers' info
ConnectedPeers(ctx context.Context) ([]peer.AddrInfo, error)
// BlockPeer blocks the peer in p2p layer
Expand Down Expand Up @@ -175,10 +173,6 @@ func (*dummyAgent) Self() ([]multiaddr.Multiaddr, error) {
return nil, nil
}

func (*dummyAgent) Neighbors(ctx context.Context) ([]peer.AddrInfo, error) {
return nil, nil
}

func (*dummyAgent) ConnectedPeers(ctx context.Context) ([]peer.AddrInfo, error) {
return nil, nil
}
Expand Down Expand Up @@ -475,28 +469,6 @@ func (p *agent) Self() ([]multiaddr.Multiaddr, error) {
return p.host.Addresses(), nil
}

func (p *agent) Neighbors(ctx context.Context) ([]peer.AddrInfo, error) {
if p.host == nil {
return nil, ErrAgentNotStarted
}

// filter out bootnodes
var nb []peer.AddrInfo
for _, peer := range p.host.Neighbors(ctx) {
isValid := true
for _, bootNode := range p.bootNodeAddr {
if strings.Contains(bootNode.String(), peer.ID.Pretty()) {
isValid = false
break
}
}
if isValid {
nb = append(nb, peer)
}
}
return nb, nil
}

func (p *agent) ConnectedPeers(ctx context.Context) ([]peer.AddrInfo, error) {
if p.host == nil {
return nil, ErrAgentNotStarted
Expand Down
4 changes: 2 additions & 2 deletions p2p/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func TestDummyAgent(t *testing.T) {
addrs, err := a.Self()
require.Nil(addrs)
require.NoError(err)
neighbors, err := a.Neighbors(nil)
neighbors, err := a.ConnectedPeers(nil)
require.Nil(neighbors)
require.NoError(err)
}
Expand Down Expand Up @@ -147,7 +147,7 @@ func TestUnicast(t *testing.T) {
}

for i := 0; i < n; i++ {
neighbors, err := agents[i].Neighbors(ctx)
neighbors, err := agents[i].ConnectedPeers(ctx)
r.NoError(err)
r.True(len(neighbors) >= n/3)
for _, neighbor := range neighbors {
Expand Down
14 changes: 3 additions & 11 deletions server/itx/heartbeat.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,27 +91,19 @@ func (h *HeartbeatHandler) Log() {
return
}

ctx := context.Background()
peers, err := p2pAgent.Neighbors(ctx)
if err != nil {
h.l.Debug("error when get neighbors.", zap.Error(err))
peers = nil
}

connectedPeers, err := p2pAgent.ConnectedPeers(ctx)
peers, err := p2pAgent.ConnectedPeers(context.Background())
if err != nil {
h.l.Debug("error when get connectedPeers.", zap.Error(err))
peers = nil
}

numPeers := len(peers)
h.l.Debug("Node status.",
zap.Int("numPeers", numPeers),
zap.Int("numConnectedPeers", numPeers),
zap.String("pendingDispatcherEvents", "{"+strings.Join(events, ", ")+"}"),
zap.String("pendingDispatcherEventsAudit", string(dpEvtsAudit)))

_heartbeatMtc.WithLabelValues("numPeers", "node").Set(float64(numPeers))
_heartbeatMtc.WithLabelValues("numConnectedPeers", "node").Set(float64(len(connectedPeers)))
_heartbeatMtc.WithLabelValues("numConnectedPeers", "node").Set(float64(numPeers))
_heartbeatMtc.WithLabelValues("pendingDispatcherEvents", "node").Set(float64(totalDPEventNumber))
// chain service
for _, c := range h.s.chainservices {
Expand Down

0 comments on commit aea991b

Please sign in to comment.