Skip to content

Commit 1ad0a50

Browse files
Merge pull request #1683 from libp2p/merge-core
move go-libp2p-core here
2 parents 812a0c1 + 98f098e commit 1ad0a50

File tree

358 files changed

+22682
-865
lines changed

Some content is hidden

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

358 files changed

+22682
-865
lines changed

config/config.go

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,17 @@ import (
55
"fmt"
66
"time"
77

8-
"github.com/libp2p/go-libp2p-core/connmgr"
9-
"github.com/libp2p/go-libp2p-core/crypto"
10-
"github.com/libp2p/go-libp2p-core/host"
11-
"github.com/libp2p/go-libp2p-core/metrics"
12-
"github.com/libp2p/go-libp2p-core/network"
13-
"github.com/libp2p/go-libp2p-core/peer"
14-
"github.com/libp2p/go-libp2p-core/peerstore"
15-
"github.com/libp2p/go-libp2p-core/pnet"
16-
"github.com/libp2p/go-libp2p-core/routing"
17-
"github.com/libp2p/go-libp2p-core/sec"
18-
"github.com/libp2p/go-libp2p-core/transport"
19-
8+
"github.com/libp2p/go-libp2p/core/connmgr"
9+
"github.com/libp2p/go-libp2p/core/crypto"
10+
"github.com/libp2p/go-libp2p/core/host"
11+
"github.com/libp2p/go-libp2p/core/metrics"
12+
"github.com/libp2p/go-libp2p/core/network"
13+
"github.com/libp2p/go-libp2p/core/peer"
14+
"github.com/libp2p/go-libp2p/core/peerstore"
15+
"github.com/libp2p/go-libp2p/core/pnet"
16+
"github.com/libp2p/go-libp2p/core/routing"
17+
"github.com/libp2p/go-libp2p/core/sec"
18+
"github.com/libp2p/go-libp2p/core/transport"
2019
"github.com/libp2p/go-libp2p/p2p/host/autonat"
2120
"github.com/libp2p/go-libp2p/p2p/host/autorelay"
2221
bhost "github.com/libp2p/go-libp2p/p2p/host/basic"

config/constructor_types.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ import (
44
"fmt"
55
"reflect"
66

7-
"github.com/libp2p/go-libp2p-core/connmgr"
8-
"github.com/libp2p/go-libp2p-core/crypto"
9-
"github.com/libp2p/go-libp2p-core/host"
10-
"github.com/libp2p/go-libp2p-core/network"
11-
"github.com/libp2p/go-libp2p-core/peer"
12-
"github.com/libp2p/go-libp2p-core/peerstore"
13-
"github.com/libp2p/go-libp2p-core/pnet"
14-
"github.com/libp2p/go-libp2p-core/sec"
15-
"github.com/libp2p/go-libp2p-core/transport"
7+
"github.com/libp2p/go-libp2p/core/connmgr"
8+
"github.com/libp2p/go-libp2p/core/crypto"
9+
"github.com/libp2p/go-libp2p/core/host"
10+
"github.com/libp2p/go-libp2p/core/network"
11+
"github.com/libp2p/go-libp2p/core/peer"
12+
"github.com/libp2p/go-libp2p/core/peerstore"
13+
"github.com/libp2p/go-libp2p/core/pnet"
14+
"github.com/libp2p/go-libp2p/core/sec"
15+
"github.com/libp2p/go-libp2p/core/transport"
1616
)
1717

1818
var (

config/muxer.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,9 @@ package config
33
import (
44
"fmt"
55

6+
"github.com/libp2p/go-libp2p/core/host"
7+
"github.com/libp2p/go-libp2p/core/network"
68
msmux "github.com/libp2p/go-libp2p/p2p/muxer/muxer-multistream"
7-
8-
"github.com/libp2p/go-libp2p-core/network"
9-
10-
"github.com/libp2p/go-libp2p-core/host"
119
)
1210

1311
// MuxC is a stream multiplex transport constructor.

config/muxer_test.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@ package config
33
import (
44
"testing"
55

6+
"github.com/libp2p/go-libp2p/core/host"
7+
"github.com/libp2p/go-libp2p/core/network"
8+
"github.com/libp2p/go-libp2p/core/peer"
69
bhost "github.com/libp2p/go-libp2p/p2p/host/basic"
710
"github.com/libp2p/go-libp2p/p2p/muxer/yamux"
811
swarmt "github.com/libp2p/go-libp2p/p2p/net/swarm/testing"
9-
10-
"github.com/libp2p/go-libp2p-core/host"
11-
"github.com/libp2p/go-libp2p-core/network"
12-
"github.com/libp2p/go-libp2p-core/peer"
1312
)
1413

1514
func TestMuxerSimple(t *testing.T) {

config/reflection_magic.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,11 @@ import (
66
"reflect"
77
"runtime"
88

9-
"github.com/libp2p/go-libp2p-core/network"
10-
11-
"github.com/libp2p/go-libp2p-core/pnet"
12-
13-
"github.com/libp2p/go-libp2p-core/connmgr"
14-
"github.com/libp2p/go-libp2p-core/host"
15-
"github.com/libp2p/go-libp2p-core/transport"
9+
"github.com/libp2p/go-libp2p/core/connmgr"
10+
"github.com/libp2p/go-libp2p/core/host"
11+
"github.com/libp2p/go-libp2p/core/network"
12+
"github.com/libp2p/go-libp2p/core/pnet"
13+
"github.com/libp2p/go-libp2p/core/transport"
1614
)
1715

1816
var errorType = reflect.TypeOf((*error)(nil)).Elem()

config/security.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@ package config
33
import (
44
"fmt"
55

6+
"github.com/libp2p/go-libp2p/core/crypto"
7+
"github.com/libp2p/go-libp2p/core/host"
8+
"github.com/libp2p/go-libp2p/core/peer"
9+
"github.com/libp2p/go-libp2p/core/sec"
10+
"github.com/libp2p/go-libp2p/core/sec/insecure"
611
csms "github.com/libp2p/go-libp2p/p2p/net/conn-security-multistream"
7-
8-
"github.com/libp2p/go-libp2p-core/crypto"
9-
"github.com/libp2p/go-libp2p-core/host"
10-
"github.com/libp2p/go-libp2p-core/peer"
11-
"github.com/libp2p/go-libp2p-core/sec"
12-
"github.com/libp2p/go-libp2p-core/sec/insecure"
1312
)
1413

1514
// SecC is a security transport constructor.

config/transport.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package config
22

33
import (
4-
"github.com/libp2p/go-libp2p-core/connmgr"
5-
"github.com/libp2p/go-libp2p-core/host"
6-
"github.com/libp2p/go-libp2p-core/network"
7-
"github.com/libp2p/go-libp2p-core/pnet"
8-
"github.com/libp2p/go-libp2p-core/transport"
4+
"github.com/libp2p/go-libp2p/core/connmgr"
5+
"github.com/libp2p/go-libp2p/core/host"
6+
"github.com/libp2p/go-libp2p/core/network"
7+
"github.com/libp2p/go-libp2p/core/pnet"
8+
"github.com/libp2p/go-libp2p/core/transport"
99
)
1010

1111
// TptC is the type for libp2p transport constructors. You probably won't ever

config/transport_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@ package config
33
import (
44
"testing"
55

6+
"github.com/libp2p/go-libp2p/core/peer"
7+
"github.com/libp2p/go-libp2p/core/transport"
68
"github.com/libp2p/go-libp2p/p2p/transport/tcp"
79

8-
"github.com/libp2p/go-libp2p-core/peer"
9-
"github.com/libp2p/go-libp2p-core/transport"
10-
1110
"github.com/stretchr/testify/require"
1211
)
1312

core/alias.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Package core provides convenient access to foundational, central go-libp2p primitives via type aliases.
2+
package core
3+
4+
import (
5+
"github.com/libp2p/go-libp2p/core/host"
6+
"github.com/libp2p/go-libp2p/core/network"
7+
"github.com/libp2p/go-libp2p/core/peer"
8+
"github.com/libp2p/go-libp2p/core/protocol"
9+
10+
"github.com/multiformats/go-multiaddr"
11+
)
12+
13+
// Multiaddr aliases the Multiaddr type from github.com/multiformats/go-multiaddr.
14+
//
15+
// Refer to the docs on that type for more info.
16+
type Multiaddr = multiaddr.Multiaddr
17+
18+
// PeerID aliases peer.ID.
19+
//
20+
// Refer to the docs on that type for more info.
21+
type PeerID = peer.ID
22+
23+
// ProtocolID aliases protocol.ID.
24+
//
25+
// Refer to the docs on that type for more info.
26+
type ProtocolID = protocol.ID
27+
28+
// PeerAddrInfo aliases peer.AddrInfo.
29+
//
30+
// Refer to the docs on that type for more info.
31+
type PeerAddrInfo = peer.AddrInfo
32+
33+
// Host aliases host.Host.
34+
//
35+
// Refer to the docs on that type for more info.
36+
type Host = host.Host
37+
38+
// Network aliases network.Network.
39+
//
40+
// Refer to the docs on that type for more info.
41+
type Network = network.Network
42+
43+
// Conn aliases network.Conn.
44+
//
45+
// Refer to the docs on that type for more info.
46+
type Conn = network.Conn
47+
48+
// Stream aliases network.Stream.
49+
//
50+
// Refer to the docs on that type for more info.
51+
type Stream = network.Stream

core/canonicallog/canonicallog.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package canonicallog
2+
3+
import (
4+
"fmt"
5+
"math/rand"
6+
"net"
7+
"strings"
8+
9+
"github.com/libp2p/go-libp2p/core/peer"
10+
11+
logging "github.com/ipfs/go-log/v2"
12+
"github.com/multiformats/go-multiaddr"
13+
manet "github.com/multiformats/go-multiaddr/net"
14+
)
15+
16+
var log = logging.WithSkip(logging.Logger("canonical-log"), 1)
17+
18+
// LogMisbehavingPeer is the canonical way to log a misbehaving peer.
19+
// Protocols should use this to identify a misbehaving peer to allow the end
20+
// user to easily identify these nodes across protocols and libp2p.
21+
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)
23+
}
24+
25+
// LogMisbehavingPeerNetAddr is the canonical way to log a misbehaving peer.
26+
// Protocols should use this to identify a misbehaving peer to allow the end
27+
// user to easily identify these nodes across protocols and libp2p.
28+
func LogMisbehavingPeerNetAddr(p peer.ID, peerAddr net.Addr, component string, originalErr error, msg string) {
29+
ma, err := manet.FromNetAddr(peerAddr)
30+
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)
32+
return
33+
}
34+
35+
LogMisbehavingPeer(p, ma, component, originalErr, msg)
36+
}
37+
38+
// LogPeerStatus logs any useful information about a peer. It takes in a sample
39+
// rate and will only log one in every sampleRate messages (randomly). This is
40+
// useful in surfacing events that are normal in isolation, but may be abnormal
41+
// in large quantities. For example, a successful connection from an IP address
42+
// is normal. 10,000 connections from that same IP address is not normal. libp2p
43+
// itself does nothing besides emitting this log. Hook this up to another tool
44+
// like fail2ban to action on the log.
45+
func LogPeerStatus(sampleRate int, p peer.ID, peerAddr multiaddr.Multiaddr, keyVals ...string) {
46+
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+
}
54+
}
55+
log.Infof("CANONICAL_PEER_STATUS: peer=%s addr=%s sample_rate=%v%s", p, peerAddr.String(), sampleRate, keyValsStr.String())
56+
}
57+
}

0 commit comments

Comments
 (0)