-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feat: add support for detecting and logging per address reachability via libp2p AutoNAT v2 #16100
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 6 commits
c02921b
b5b08b6
26d1615
1d6ee8a
4e56d1b
bef010c
f5089a7
0865dab
adb6dec
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,6 +28,7 @@ import ( | |
| "github.com/ethereum/go-ethereum/p2p/enr" | ||
| "github.com/libp2p/go-libp2p" | ||
| pubsub "github.com/libp2p/go-libp2p-pubsub" | ||
| "github.com/libp2p/go-libp2p/core/event" | ||
| "github.com/libp2p/go-libp2p/core/host" | ||
| "github.com/libp2p/go-libp2p/core/network" | ||
| "github.com/libp2p/go-libp2p/core/peer" | ||
|
|
@@ -154,8 +155,6 @@ func NewService(ctx context.Context, cfg *Config) (*Service, error) { | |
| return nil, errors.Wrapf(err, "failed to build p2p options") | ||
| } | ||
|
|
||
| // Sets mplex timeouts | ||
| configureMplex() | ||
| h, err := libp2p.New(opts...) | ||
| if err != nil { | ||
| return nil, errors.Wrapf(err, "failed to create p2p host") | ||
|
|
@@ -259,6 +258,11 @@ func (s *Service) Start() { | |
| // current epoch. | ||
| s.RefreshPersistentSubnets() | ||
|
|
||
| if s.cfg.EnableAutoNAT { | ||
| s.subscribeReachabilityEvents() | ||
| log.Info("AutoNAT v2 enabled for address reachability detection") | ||
| } | ||
|
|
||
| // Periodic functions. | ||
| async.RunEvery(s.ctx, params.BeaconConfig().TtfbTimeoutDuration(), func() { | ||
| ensurePeerConnections(s.ctx, s.host, s.peers, relayNodes...) | ||
|
|
@@ -557,3 +561,42 @@ func (s *Service) downscorePeer(peerID peer.ID, reason string) { | |
| newScore := s.Peers().Scorers().BadResponsesScorer().Increment(peerID) | ||
| log.WithFields(logrus.Fields{"peerID": peerID, "reason": reason, "newScore": newScore}).Debug("Downscore peer") | ||
| } | ||
|
|
||
| func (s *Service) subscribeReachabilityEvents() { | ||
|
||
| sub, err := s.host.EventBus().Subscribe(new(event.EvtHostReachableAddrsChanged)) | ||
| if err != nil { | ||
| log.WithError(err).Error("Failed to subscribe to reachability events") | ||
| return | ||
| } | ||
|
|
||
| go func() { | ||
| defer func() { | ||
| if err := sub.Close(); err != nil { | ||
| log.WithError(err).Debug("Failed to close reachability event subscription") | ||
| } | ||
| }() | ||
| for { | ||
| select { | ||
| case <-s.ctx.Done(): | ||
| return | ||
| case ev := <-sub.Out(): | ||
|
|
||
|
||
| if event, ok := ev.(event.EvtHostReachableAddrsChanged); ok { | ||
| log.WithFields(logrus.Fields{ | ||
| "reachable": multiaddrsToStrings(event.Reachable), | ||
| "unreachable": multiaddrsToStrings(event.Unreachable), | ||
| "unknown": multiaddrsToStrings(event.Unknown), | ||
| }).Info("Address reachability changed") | ||
| } | ||
| } | ||
| } | ||
| }() | ||
| } | ||
|
|
||
| func multiaddrsToStrings(addrs []multiaddr.Multiaddr) []string { | ||
| strs := make([]string, len(addrs)) | ||
| for i, a := range addrs { | ||
| strs[i] = a.String() | ||
| } | ||
| return strs | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| ### Added | ||
|
|
||
| - Add support for detecting and logging per address reachability via libp2p AutoNAT v2. |
Uh oh!
There was an error while loading. Please reload this page.