Skip to content

Commit 9e4105e

Browse files
lidelMarcoPolo
authored andcommitted
merge: resolve conflicts with origin/master
kept NAT healing functionality with connection failure tracking and rediscovery, while adopting structured logging format from master
2 parents 880992d + 72894e3 commit 9e4105e

File tree

129 files changed

+1682
-1755
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

129 files changed

+1682
-1755
lines changed

.github/workflows/go-check.yml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name: Go Checks
33
on:
44
pull_request:
55
push:
6-
branches: ["master","release-v0[0-9][0-9]"]
6+
branches: ["master", "release-v0[0-9][0-9]"]
77
workflow_dispatch:
88

99
permissions:
@@ -17,6 +17,5 @@ jobs:
1717
go-check:
1818
uses: ipdxco/unified-github-workflows/.github/workflows/[email protected]
1919
with:
20-
go-version: "1.24.x"
20+
go-version: "1.25.x"
2121
go-generate-ignore-protoc-version-comments: true
22-

.github/workflows/go-test.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name: Go Test
33
on:
44
pull_request:
55
push:
6-
branches: ["master","release-v0[0-9][0-9]"]
6+
branches: ["master", "release-v0[0-9][0-9]"]
77
workflow_dispatch:
88

99
permissions:
@@ -17,6 +17,6 @@ jobs:
1717
go-test:
1818
uses: ./.github/workflows/go-test-template.yml
1919
with:
20-
go-versions: '["1.23.x", "1.24.x"]'
20+
go-versions: '["1.24.x", "1.25.x"]'
2121
secrets:
2222
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}

README.md

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,41 @@ There's a few things you can do right now to help out:
8686
instance, you may need to read up on p2p and more complex operations like
8787
muxing to be able to help technically.
8888

89+
## AI Assistance Notice
8990

90-
## Supported Go Versions
91+
> [!IMPORTANT]
92+
>
93+
> If you are using **any kind of AI assistance** to contribute to libp2p,
94+
> it must be disclosed in the pull request.
95+
96+
If you are using any kind of AI assistance while contributing to libp2p,
97+
**this must be disclosed in the pull request**, along with the extent to
98+
which AI assistance was used (e.g. docs only vs. code generation).
99+
If PR responses are being generated by an AI, disclose that as well.
100+
As a small exception, trivial tab-completion doesn't need to be disclosed,
101+
so long as it is limited to single keywords or short phrases.
102+
103+
An example disclosure:
104+
105+
> This PR was written primarily by Claude Code.
106+
107+
Or a more detailed disclosure:
108+
109+
> I consulted ChatGPT to understand the codebase but the solution
110+
> was fully authored manually by myself.
111+
112+
Failure to disclose this is first and foremost rude to the human operators
113+
on the other end of the pull request, but it also makes it difficult to
114+
determine how much scrutiny to apply to the contribution.
115+
116+
In a perfect world, AI assistance would produce equal or higher quality
117+
work than any human. That isn't the world we live in today, and in most cases
118+
it's generating slop. I say this despite being a fan of and using them
119+
successfully myself (with heavy supervision)!
120+
121+
Please be respectful to maintainers and disclose AI assistance.
122+
123+
# Supported Go Versions
91124

92125
We test against and support the two most recent major releases of Go. This is
93126
informed by Go's own [security policy](https://go.dev/doc/security/policy).

config/config.go

Lines changed: 46 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,13 @@ import (
2323
"github.com/libp2p/go-libp2p/core/sec"
2424
"github.com/libp2p/go-libp2p/core/sec/insecure"
2525
"github.com/libp2p/go-libp2p/core/transport"
26+
logging "github.com/libp2p/go-libp2p/gologshim"
2627
"github.com/libp2p/go-libp2p/p2p/host/autonat"
2728
"github.com/libp2p/go-libp2p/p2p/host/autorelay"
2829
bhost "github.com/libp2p/go-libp2p/p2p/host/basic"
2930
blankhost "github.com/libp2p/go-libp2p/p2p/host/blank"
3031
"github.com/libp2p/go-libp2p/p2p/host/eventbus"
32+
"github.com/libp2p/go-libp2p/p2p/host/observedaddrs"
3133
"github.com/libp2p/go-libp2p/p2p/host/peerstore/pstoremem"
3234
rcmgr "github.com/libp2p/go-libp2p/p2p/host/resource-manager"
3335
routed "github.com/libp2p/go-libp2p/p2p/host/routed"
@@ -50,6 +52,8 @@ import (
5052
"go.uber.org/fx/fxevent"
5153
)
5254

55+
var log = logging.Logger("p2p-config")
56+
5357
// AddrsFactory is a function that takes a set of multiaddrs we're listening on and
5458
// returns the set of multiaddrs we should advertise to the network.
5559
type AddrsFactory = bhost.AddrsFactory
@@ -158,9 +162,7 @@ func (cfg *Config) makeSwarm(eventBus event.Bus, enableMetrics bool) (*swarm.Swa
158162

159163
// Check this early. Prevents us from even *starting* without verifying this.
160164
if pnet.ForcePrivateNetwork && len(cfg.PSK) == 0 {
161-
log.Error("tried to create a libp2p node with no Private" +
162-
" Network Protector but usage of Private Networks" +
163-
" is forced by the environment")
165+
log.Error("tried to create a libp2p node with no Private Network Protector but usage of Private Networks is forced by the environment")
164166
// Note: This is *also* checked the upgrader itself, so it'll be
165167
// enforced even *if* you don't use the libp2p constructor.
166168
return nil, pnet.ErrNotInPrivateNetwork
@@ -287,7 +289,11 @@ func (cfg *Config) makeAutoNATV2Host() (host.Host, error) {
287289

288290
func (cfg *Config) addTransports() ([]fx.Option, error) {
289291
fxopts := []fx.Option{
290-
fx.WithLogger(func() fxevent.Logger { return getFXLogger() }),
292+
fx.WithLogger(func() fxevent.Logger {
293+
return &fxevent.SlogLogger{
294+
Logger: log.With("system", "fx"),
295+
}
296+
}),
291297
fx.Provide(fx.Annotate(tptu.New, fx.ParamTags(`name:"security"`))),
292298
fx.Supply(cfg.Muxers),
293299
fx.Provide(func() connmgr.ConnectionGater { return cfg.ConnectionGater }),
@@ -434,23 +440,23 @@ func (cfg *Config) addTransports() ([]fx.Option, error) {
434440
return fxopts, nil
435441
}
436442

437-
func (cfg *Config) newBasicHost(swrm *swarm.Swarm, eventBus event.Bus, an *autonatv2.AutoNAT) (*bhost.BasicHost, error) {
443+
func (cfg *Config) newBasicHost(swrm *swarm.Swarm, eventBus event.Bus, an *autonatv2.AutoNAT, o bhost.ObservedAddrsManager) (*bhost.BasicHost, error) {
438444
h, err := bhost.NewHost(swrm, &bhost.HostOpts{
439-
EventBus: eventBus,
440-
ConnManager: cfg.ConnManager,
441-
AddrsFactory: cfg.AddrsFactory,
442-
NATManager: cfg.NATManager,
443-
EnablePing: !cfg.DisablePing,
444-
UserAgent: cfg.UserAgent,
445-
ProtocolVersion: cfg.ProtocolVersion,
446-
EnableHolePunching: cfg.EnableHolePunching,
447-
HolePunchingOptions: cfg.HolePunchingOptions,
448-
EnableRelayService: cfg.EnableRelayService,
449-
RelayServiceOpts: cfg.RelayServiceOpts,
450-
EnableMetrics: !cfg.DisableMetrics,
451-
PrometheusRegisterer: cfg.PrometheusRegisterer,
452-
DisableIdentifyAddressDiscovery: cfg.DisableIdentifyAddressDiscovery,
453-
AutoNATv2: an,
445+
EventBus: eventBus,
446+
ConnManager: cfg.ConnManager,
447+
AddrsFactory: cfg.AddrsFactory,
448+
NATManager: cfg.NATManager,
449+
EnablePing: !cfg.DisablePing,
450+
UserAgent: cfg.UserAgent,
451+
ProtocolVersion: cfg.ProtocolVersion,
452+
EnableHolePunching: cfg.EnableHolePunching,
453+
HolePunchingOptions: cfg.HolePunchingOptions,
454+
EnableRelayService: cfg.EnableRelayService,
455+
RelayServiceOpts: cfg.RelayServiceOpts,
456+
EnableMetrics: !cfg.DisableMetrics,
457+
PrometheusRegisterer: cfg.PrometheusRegisterer,
458+
AutoNATv2: an,
459+
ObservedAddrsManager: o,
454460
})
455461
if err != nil {
456462
return nil, err
@@ -467,7 +473,7 @@ func (cfg *Config) validate() error {
467473
if l, ok := cfg.ResourceManager.(connmgr.GetConnLimiter); ok {
468474
err := cfg.ConnManager.CheckLimit(l)
469475
if err != nil {
470-
log.Warn(fmt.Sprintf("rcmgr limit conflicts with connmgr limit: %v", err))
476+
log.Warn("rcmgr limit conflicts with connmgr limit", "err", err)
471477
}
472478
}
473479

@@ -529,6 +535,25 @@ func (cfg *Config) NewNode() (host.Host, error) {
529535
})
530536
return sw, nil
531537
}),
538+
fx.Provide(func(eventBus event.Bus, s *swarm.Swarm, lifecycle fx.Lifecycle) (bhost.ObservedAddrsManager, error) {
539+
if cfg.DisableIdentifyAddressDiscovery {
540+
return nil, nil
541+
}
542+
o, err := observedaddrs.NewManager(eventBus, s)
543+
if err != nil {
544+
return nil, err
545+
}
546+
lifecycle.Append(fx.Hook{
547+
OnStart: func(context.Context) error {
548+
o.Start(s)
549+
return nil
550+
},
551+
OnStop: func(context.Context) error {
552+
return o.Close()
553+
},
554+
})
555+
return o, nil
556+
}),
532557
fx.Provide(func() (*autonatv2.AutoNAT, error) {
533558
if !cfg.EnableAutoNATv2 {
534559
return nil, nil

config/log.go

Lines changed: 0 additions & 28 deletions
This file was deleted.

core/canonicallog/canonicallog.go

Lines changed: 49 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,54 @@
11
package canonicallog
22

33
import (
4-
"fmt"
4+
"context"
5+
"log/slog"
56
"math/rand"
67
"net"
7-
"strings"
8+
"os"
9+
"runtime"
10+
"time"
811

912
"github.com/libp2p/go-libp2p/core/peer"
1013

11-
logging "github.com/ipfs/go-log/v2"
14+
logging "github.com/libp2p/go-libp2p/gologshim"
1215
"github.com/multiformats/go-multiaddr"
1316
manet "github.com/multiformats/go-multiaddr/net"
1417
)
1518

16-
var log = logging.WithSkip(logging.Logger("canonical-log"), 1)
19+
var log = slog.New(
20+
slog.NewTextHandler(
21+
os.Stderr,
22+
&slog.HandlerOptions{
23+
Level: logging.ConfigFromEnv().LevelForSystem("canonical-log"),
24+
AddSource: true}))
25+
26+
// logWithSkip logs at level with AddSource pointing to the caller `skip` frames up
27+
// from *this* function’s caller (so skip=0 => the immediate caller of logWithSkip).
28+
func logWithSkip(ctx context.Context, l *slog.Logger, level slog.Level, skip int, msg string, args ...any) {
29+
if !l.Enabled(ctx, level) {
30+
return
31+
}
32+
33+
var pcs [1]uintptr
34+
// +2 to skip runtime.Callers and logWithSkip itself.
35+
runtime.Callers(skip+2, pcs[:])
36+
37+
r := slog.NewRecord(time.Now(), level, msg, pcs[0])
38+
r.Add(args...)
39+
_ = l.Handler().Handle(ctx, r)
40+
}
1741

1842
// LogMisbehavingPeer is the canonical way to log a misbehaving peer.
1943
// Protocols should use this to identify a misbehaving peer to allow the end
2044
// user to easily identify these nodes across protocols and libp2p.
2145
func LogMisbehavingPeer(p peer.ID, peerAddr multiaddr.Multiaddr, component string, err error, msg string) {
22-
log.Warnf("CANONICAL_MISBEHAVING_PEER: peer=%s addr=%s component=%s err=%q msg=%q", p, peerAddr.String(), component, err, msg)
46+
logWithSkip(context.Background(), log, slog.LevelWarn, 1, "CANONICAL_MISBEHAVING_PEER",
47+
"peer", p,
48+
"addr", peerAddr,
49+
"component", component,
50+
"err", err,
51+
"msg", msg)
2352
}
2453

2554
// LogMisbehavingPeerNetAddr is the canonical way to log a misbehaving peer.
@@ -28,7 +57,12 @@ func LogMisbehavingPeer(p peer.ID, peerAddr multiaddr.Multiaddr, component strin
2857
func LogMisbehavingPeerNetAddr(p peer.ID, peerAddr net.Addr, component string, originalErr error, msg string) {
2958
ma, err := manet.FromNetAddr(peerAddr)
3059
if err != nil {
31-
log.Warnf("CANONICAL_MISBEHAVING_PEER: peer=%s net_addr=%s component=%s err=%q msg=%q", p, peerAddr.String(), component, originalErr, msg)
60+
logWithSkip(context.Background(), log, slog.LevelWarn, 1, "CANONICAL_MISBEHAVING_PEER",
61+
"peer", p,
62+
"net_addr", peerAddr.String(),
63+
"component", component,
64+
"err", originalErr,
65+
"msg", msg)
3266
return
3367
}
3468

@@ -44,14 +78,15 @@ func LogMisbehavingPeerNetAddr(p peer.ID, peerAddr net.Addr, component string, o
4478
// like fail2ban to action on the log.
4579
func LogPeerStatus(sampleRate int, p peer.ID, peerAddr multiaddr.Multiaddr, keyVals ...string) {
4680
if rand.Intn(sampleRate) == 0 {
47-
keyValsStr := strings.Builder{}
48-
for i, kOrV := range keyVals {
49-
if i%2 == 0 {
50-
fmt.Fprintf(&keyValsStr, " %v=", kOrV)
51-
} else {
52-
fmt.Fprintf(&keyValsStr, "%q", kOrV)
53-
}
81+
args := []any{
82+
"peer", p,
83+
"addr", peerAddr.String(),
84+
"sample_rate", sampleRate,
85+
}
86+
// Add the additional key-value pairs
87+
for _, kv := range keyVals {
88+
args = append(args, kv)
5489
}
55-
log.Infof("CANONICAL_PEER_STATUS: peer=%s addr=%s sample_rate=%v%s", p, peerAddr.String(), sampleRate, keyValsStr.String())
90+
logWithSkip(context.Background(), log, slog.LevelInfo, 1, "CANONICAL_PEER_STATUS", args...)
5691
}
5792
}

core/canonicallog/canonicallog_test.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,23 @@ package canonicallog
22

33
import (
44
"fmt"
5+
"log/slog"
56
"net"
7+
"os"
68
"testing"
79

810
"github.com/libp2p/go-libp2p/core/test"
911

10-
logging "github.com/ipfs/go-log/v2"
1112
"github.com/multiformats/go-multiaddr"
1213
)
1314

1415
func TestLogs(t *testing.T) {
15-
err := logging.SetLogLevel("canonical-log", "info")
16-
if err != nil {
17-
t.Fatal(err)
18-
}
16+
originalLogger := log
17+
defer func() {
18+
log = originalLogger
19+
}()
20+
// Override to print debug logs
21+
log = slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: slog.LevelInfo, AddSource: true}))
1922

2023
LogMisbehavingPeer(test.RandPeerIDFatal(t), multiaddr.StringCast("/ip4/1.2.3.4"), "somecomponent", fmt.Errorf("something"), "hi")
2124

examples/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/libp2p/go-libp2p/examples
22

3-
go 1.23.0
3+
go 1.24
44

55
require (
66
github.com/caddyserver/certmagic v0.21.6

examples/ipfs-camp-2019/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/libp2p/go-libp2p/examples/ipfs-camp-2019
22

3-
go 1.23
3+
go 1.24
44

55
require (
66
github.com/gogo/protobuf v1.3.2

examples/pubsub/basic-chat-with-rendezvous/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/libp2p/go-libp2p/examples/pubsub/chat
22

3-
go 1.23
3+
go 1.24
44

55
require (
66
github.com/libp2p/go-libp2p v0.33.0

0 commit comments

Comments
 (0)