mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2024-12-23 03:51:29 +00:00
cec32cb996
* Append Dynamic Addinng Trusted Peer Apis * Append unit tests for Dynamic Addinng Trusted Peer Apis * Update beacon-chain/p2p/peers/peerdata/store.go * Update beacon-chain/p2p/peers/peerdata/store_test.go * Update beacon-chain/p2p/peers/peerdata/store_test.go * Update beacon-chain/p2p/peers/peerdata/store_test.go * Update beacon-chain/p2p/peers/status.go * Update beacon-chain/p2p/peers/status_test.go * Update beacon-chain/p2p/peers/status_test.go * Update beacon-chain/rpc/eth/node/handlers.go * Update beacon-chain/rpc/eth/node/handlers.go * Update beacon-chain/rpc/eth/node/handlers.go * Update beacon-chain/rpc/eth/node/handlers.go * Move trusted peer apis from rpc/eth/v1/node to rpc/prysm/node; move peersToWatch into ensurePeerConnections function; * Update beacon-chain/rpc/prysm/node/server.go * Update beacon-chain/rpc/prysm/node/server.go * fix go lint problem * p2p/watch_peers.go: trusted peer makes AddrInfo structure by itself instead of using MakePeer(). p2p/service.go: add connectWithAllTrustedPeers function, before connectWithPeer, add trusted peer info into peer status. p2p/peers/status.go: trusted peers are not included, when pruning outdated and disconnected peers. * use readlock for GetTrustedPeers and IsTrustedPeers --------- Co-authored-by: simon <sunminghui2@huawei.com> Co-authored-by: Radosław Kapka <rkapka@wp.pl> Co-authored-by: Nishant Das <nishdas93@gmail.com>
66 lines
1.8 KiB
Go
66 lines
1.8 KiB
Go
package p2p
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/libp2p/go-libp2p/core/host"
|
|
"github.com/libp2p/go-libp2p/core/peer"
|
|
ma "github.com/multiformats/go-multiaddr"
|
|
"github.com/prysmaticlabs/prysm/v4/beacon-chain/p2p/peers"
|
|
)
|
|
|
|
// ensurePeerConnections will attempt to reestablish connection to the peers
|
|
// if there are currently no connections to that peer.
|
|
func ensurePeerConnections(ctx context.Context, h host.Host, peers *peers.Status, relayNodes ...string) {
|
|
// every time reset peersToWatch, add RelayNodes and trust peers
|
|
var peersToWatch []*peer.AddrInfo
|
|
|
|
// add RelayNodes
|
|
for _, node := range relayNodes {
|
|
if node == "" {
|
|
continue
|
|
}
|
|
peerInfo, err := MakePeer(node)
|
|
if err != nil {
|
|
log.WithError(err).Error("Could not make peer")
|
|
continue
|
|
}
|
|
peersToWatch = append(peersToWatch, peerInfo)
|
|
}
|
|
|
|
// add trusted peers
|
|
trustedPeers := peers.GetTrustedPeers()
|
|
for _, trustedPeer := range trustedPeers {
|
|
maddr, err := peers.Address(trustedPeer)
|
|
|
|
// avoid invalid trusted peers
|
|
if err != nil || maddr == nil {
|
|
log.WithField("peer", trustedPeers).WithError(err).Error("Could not get peer address")
|
|
continue
|
|
}
|
|
peerInfo := &peer.AddrInfo{ID: trustedPeer}
|
|
peerInfo.Addrs = []ma.Multiaddr{maddr}
|
|
peersToWatch = append(peersToWatch, peerInfo)
|
|
}
|
|
|
|
if len(peersToWatch) == 0 {
|
|
return
|
|
}
|
|
for _, p := range peersToWatch {
|
|
c := h.Network().ConnsToPeer(p.ID)
|
|
if len(c) == 0 {
|
|
if err := connectWithTimeout(ctx, h, p); err != nil {
|
|
log.WithField("peer", p.ID).WithField("addrs", p.Addrs).WithError(err).Errorf("Failed to reconnect to peer")
|
|
continue
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func connectWithTimeout(ctx context.Context, h host.Host, peer *peer.AddrInfo) error {
|
|
log.WithField("peer", peer.ID).Debug("No connections to peer, reconnecting")
|
|
ctx, cancel := context.WithTimeout(ctx, maxDialTimeout)
|
|
defer cancel()
|
|
return h.Connect(ctx, *peer)
|
|
}
|