Skip to content

Commit 2b8b7f7

Browse files
authored
Merge branch 'master' into add-autotls-example
2 parents f44f4ef + 080e6c8 commit 2b8b7f7

File tree

93 files changed

+2131
-879
lines changed

Some content is hidden

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

93 files changed

+2131
-879
lines changed

.github/workflows/go-test-template.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ jobs:
109109
run: test_analysis ${{ env.GOTESTFLAGS }}
110110
- name: Upload test results
111111
if: always()
112-
uses: actions/upload-artifact@v3
112+
uses: actions/upload-artifact@v4
113113
with:
114114
name: ${{ matrix.os }}_${{ matrix.go }}_test_results.db
115115
path: ./test_results.db
@@ -131,7 +131,7 @@ jobs:
131131
run: test_analysis -race ${{ env.GORACEFLAGS }} ./...
132132
- name: Upload test results (Race)
133133
if: (steps.race.conclusion == 'success' || steps.race.conclusion == 'failure')
134-
uses: actions/upload-artifact@v3
134+
uses: actions/upload-artifact@v4
135135
with:
136136
name: ${{ matrix.os }}_${{ matrix.go }}_test_results_race.db
137137
path: ./test_results.db

.github/workflows/release-check.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@ concurrency:
1616

1717
jobs:
1818
release-check:
19-
uses: marcopolo/unified-github-workflows/.github/workflows/release-check.yml@e66cb9667a2e1148efda4591e29c56258eaf385b
19+
uses: ipdxco/unified-github-workflows/.github/workflows/release-check.yml@v1.0

README.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,25 @@ Guidelines:
6767
- ask questions or talk about things in our [discussion forums](https://discuss.libp2p.io), or open an [issue](https://github.com/libp2p/go-libp2p/issues) for bug reports, or #libp2p-implementers on [Filecoin slack](https://filecoin.io/slack).
6868
- ensure you are able to contribute (no legal issues please -- we use the DCO)
6969
- get in touch with @libp2p/go-libp2p-maintainers about how best to contribute
70+
- No drive-by contributions seeking to collect airdrops.
71+
- Many projects aim to reward contributors to common goods. Great. However,
72+
this creates an unfortunate incentive for low-effort PRs, submitted solely to
73+
claim rewards. These PRs consume maintainers’ time and energy to triage, with
74+
little to no impact on end users. If we suspect this is the intent of a PR,
75+
we may close it without comment. If you believe this was done in error,
76+
contact us via email. Reference this README section and explain why your PR
77+
is not a “drive-by contribution.”
7078
- have fun!
7179

7280
There's a few things you can do right now to help out:
73-
- Go through the modules below and **check out existing issues**. This would be especially useful for modules in active development. Some knowledge of IPFS/libp2p may be required, as well as the infrastructure behind it - for instance, you may need to read up on p2p and more complex operations like muxing to be able to help technically.
7481
- **Perform code reviews**.
7582
- **Add tests**. There can never be enough tests.
83+
- Go through the modules below and **check out existing issues**. This would
84+
be especially useful for modules in active development. Some knowledge of
85+
IPFS/libp2p may be required, as well as the infrastructure behind it - for
86+
instance, you may need to read up on p2p and more complex operations like
87+
muxing to be able to help technically.
88+
7689

7790
## Supported Go Versions
7891

config/config.go

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -446,12 +446,9 @@ func (cfg *Config) newBasicHost(swrm *swarm.Swarm, eventBus event.Bus) (*bhost.B
446446
return h, nil
447447
}
448448

449-
// NewNode constructs a new libp2p Host from the Config.
450-
//
451-
// This function consumes the config. Do not reuse it (really!).
452-
func (cfg *Config) NewNode() (host.Host, error) {
449+
func (cfg *Config) validate() error {
453450
if cfg.EnableAutoRelay && !cfg.Relay {
454-
return nil, fmt.Errorf("cannot enable autorelay; relay is not enabled")
451+
return fmt.Errorf("cannot enable autorelay; relay is not enabled")
455452
}
456453
// If possible check that the resource manager conn limit is higher than the
457454
// limit set in the conn manager.
@@ -462,6 +459,33 @@ func (cfg *Config) NewNode() (host.Host, error) {
462459
}
463460
}
464461

462+
if len(cfg.PSK) > 0 && cfg.ShareTCPListener {
463+
return errors.New("cannot use shared TCP listener with PSK")
464+
}
465+
466+
return nil
467+
}
468+
469+
// NewNode constructs a new libp2p Host from the Config.
470+
//
471+
// This function consumes the config. Do not reuse it (really!).
472+
func (cfg *Config) NewNode() (host.Host, error) {
473+
474+
validateErr := cfg.validate()
475+
if validateErr != nil {
476+
if cfg.ResourceManager != nil {
477+
cfg.ResourceManager.Close()
478+
}
479+
if cfg.ConnManager != nil {
480+
cfg.ConnManager.Close()
481+
}
482+
if cfg.Peerstore != nil {
483+
cfg.Peerstore.Close()
484+
}
485+
486+
return nil, validateErr
487+
}
488+
465489
if !cfg.DisableMetrics {
466490
rcmgr.MustRegisterWith(cfg.PrometheusRegisterer)
467491
}

core/crypto/pb/crypto.pb.go

Lines changed: 16 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

core/network/conn.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,60 @@ package network
22

33
import (
44
"context"
5+
"fmt"
56
"io"
67

78
ic "github.com/libp2p/go-libp2p/core/crypto"
9+
810
"github.com/libp2p/go-libp2p/core/peer"
911
"github.com/libp2p/go-libp2p/core/protocol"
1012

1113
ma "github.com/multiformats/go-multiaddr"
1214
)
1315

16+
type ConnErrorCode uint32
17+
18+
type ConnError struct {
19+
Remote bool
20+
ErrorCode ConnErrorCode
21+
TransportError error
22+
}
23+
24+
func (c *ConnError) Error() string {
25+
side := "local"
26+
if c.Remote {
27+
side = "remote"
28+
}
29+
if c.TransportError != nil {
30+
return fmt.Sprintf("connection closed (%s): code: 0x%x: transport error: %s", side, c.ErrorCode, c.TransportError)
31+
}
32+
return fmt.Sprintf("connection closed (%s): code: 0x%x", side, c.ErrorCode)
33+
}
34+
35+
func (c *ConnError) Is(target error) bool {
36+
if tce, ok := target.(*ConnError); ok {
37+
return tce.ErrorCode == c.ErrorCode && tce.Remote == c.Remote
38+
}
39+
return false
40+
}
41+
42+
func (c *ConnError) Unwrap() []error {
43+
return []error{ErrReset, c.TransportError}
44+
}
45+
46+
const (
47+
ConnNoError ConnErrorCode = 0
48+
ConnProtocolNegotiationFailed ConnErrorCode = 0x1000
49+
ConnResourceLimitExceeded ConnErrorCode = 0x1001
50+
ConnRateLimited ConnErrorCode = 0x1002
51+
ConnProtocolViolation ConnErrorCode = 0x1003
52+
ConnSupplanted ConnErrorCode = 0x1004
53+
ConnGarbageCollected ConnErrorCode = 0x1005
54+
ConnShutdown ConnErrorCode = 0x1006
55+
ConnGated ConnErrorCode = 0x1007
56+
ConnCodeOutOfRange ConnErrorCode = 0x1008
57+
)
58+
1459
// Conn is a connection to a remote peer. It multiplexes streams.
1560
// Usually there is no need to use a Conn directly, but it may
1661
// be useful to get information about the peer on the other side:
@@ -24,6 +69,11 @@ type Conn interface {
2469
ConnStat
2570
ConnScoper
2671

72+
// CloseWithError closes the connection with errCode. The errCode is sent to the
73+
// peer on a best effort basis. For transports that do not support sending error
74+
// codes on connection close, the behavior is identical to calling Close.
75+
CloseWithError(errCode ConnErrorCode) error
76+
2777
// ID returns an identifier that uniquely identifies this Conn within this
2878
// host, during this run. Connection IDs may repeat across restarts.
2979
ID() string

core/network/mux.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package network
33
import (
44
"context"
55
"errors"
6+
"fmt"
67
"io"
78
"net"
89
"time"
@@ -11,6 +12,49 @@ import (
1112
// ErrReset is returned when reading or writing on a reset stream.
1213
var ErrReset = errors.New("stream reset")
1314

15+
type StreamErrorCode uint32
16+
17+
type StreamError struct {
18+
ErrorCode StreamErrorCode
19+
Remote bool
20+
TransportError error
21+
}
22+
23+
func (s *StreamError) Error() string {
24+
side := "local"
25+
if s.Remote {
26+
side = "remote"
27+
}
28+
if s.TransportError != nil {
29+
return fmt.Sprintf("stream reset (%s): code: 0x%x: transport error: %s", side, s.ErrorCode, s.TransportError)
30+
}
31+
return fmt.Sprintf("stream reset (%s): code: 0x%x", side, s.ErrorCode)
32+
}
33+
34+
func (s *StreamError) Is(target error) bool {
35+
if tse, ok := target.(*StreamError); ok {
36+
return tse.ErrorCode == s.ErrorCode && tse.Remote == s.Remote
37+
}
38+
return false
39+
}
40+
41+
func (s *StreamError) Unwrap() []error {
42+
return []error{ErrReset, s.TransportError}
43+
}
44+
45+
const (
46+
StreamNoError StreamErrorCode = 0
47+
StreamProtocolNegotiationFailed StreamErrorCode = 0x1001
48+
StreamResourceLimitExceeded StreamErrorCode = 0x1002
49+
StreamRateLimited StreamErrorCode = 0x1003
50+
StreamProtocolViolation StreamErrorCode = 0x1004
51+
StreamSupplanted StreamErrorCode = 0x1005
52+
StreamGarbageCollected StreamErrorCode = 0x1006
53+
StreamShutdown StreamErrorCode = 0x1007
54+
StreamGated StreamErrorCode = 0x1008
55+
StreamCodeOutOfRange StreamErrorCode = 0x1009
56+
)
57+
1458
// MuxedStream is a bidirectional io pipe within a connection.
1559
type MuxedStream interface {
1660
io.Reader
@@ -56,6 +100,11 @@ type MuxedStream interface {
56100
// side to hang up and go away.
57101
Reset() error
58102

103+
// ResetWithError aborts both ends of the stream with `errCode`. `errCode` is sent
104+
// to the peer on a best effort basis. For transports that do not support sending
105+
// error codes to remote peer, the behavior is identical to calling Reset
106+
ResetWithError(errCode StreamErrorCode) error
107+
59108
SetDeadline(time.Time) error
60109
SetReadDeadline(time.Time) error
61110
SetWriteDeadline(time.Time) error
@@ -75,6 +124,10 @@ type MuxedConn interface {
75124
// Close closes the stream muxer and the the underlying net.Conn.
76125
io.Closer
77126

127+
// CloseWithError closes the connection with errCode. The errCode is sent
128+
// to the peer.
129+
CloseWithError(errCode ConnErrorCode) error
130+
78131
// IsClosed returns whether a connection is fully closed, so it can
79132
// be garbage collected.
80133
IsClosed() bool

core/network/stream.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,8 @@ type Stream interface {
2727

2828
// Scope returns the user's view of this stream's resource scope
2929
Scope() StreamScope
30+
31+
// ResetWithError closes both ends of the stream with errCode. The errCode is sent
32+
// to the peer.
33+
ResetWithError(errCode StreamErrorCode) error
3034
}

0 commit comments

Comments
 (0)