Skip to content
This repository has been archived by the owner on Sep 6, 2022. It is now read-only.

Absorb go-libp2p abstractions and core types into this module #1

Merged
merged 31 commits into from
May 22, 2019
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
1e60799
genesis of go-libp2p-core.
raulk Apr 16, 2019
12d7eef
reorg null connmgr and OOTB network notifiees.
raulk Apr 16, 2019
54a1b70
add routing interfaces.
raulk Apr 16, 2019
557fb9e
downgrade to go 1.11
raulk Apr 16, 2019
bdf6ef0
remove go 1.11 directive from go.mod.
raulk Apr 16, 2019
95ab6c0
mv protocol/protocol.go => protocol/switch.go
raulk Apr 16, 2019
5163265
update peerstore.TempAddrTTL to 2 min.
raulk Apr 17, 2019
40efb03
address minor review comments.
raulk Apr 17, 2019
c9a30b9
absorb transport tests.
raulk Apr 17, 2019
2e11bca
reorg network context options.
raulk Apr 17, 2019
4e1f238
absorb discovery opts.
raulk Apr 17, 2019
47396eb
reorg code; see below.
raulk Apr 17, 2019
f41b5dd
add mux test suite.
raulk Apr 17, 2019
978f710
sec/test/ttest.go -> sec/test/transport.go.
raulk Apr 22, 2019
ec2a527
add top-level aliases for central primitives of go-libp2p.
raulk Apr 22, 2019
3a54eb6
absorb go-libp2p-crypto.
raulk Apr 22, 2019
c30d4b4
rename peer.Info to peer.AddrInfo.
raulk Apr 22, 2019
06118ba
keep go-flow-metrics untouched.
raulk Apr 25, 2019
ed42958
refactor test utilities, harnesses and suites.
raulk May 8, 2019
757c750
move peer and crypto test utils to go-libp2p-testing.
raulk May 8, 2019
0906f2d
rename transport.{UpgradedConn => CapableConn}.
raulk May 15, 2019
1e915f1
rename mux.{NoOpHandler => NoopHandler}.
raulk May 16, 2019
c1c817c
Godocs for core packages (#3)
yusefnapora May 16, 2019
09ae22d
sync w/ go-libp2p-crypto master: rsa openssl.
raulk May 18, 2019
9a572d1
connmgr sync with master: UpsertTag().
raulk May 18, 2019
9b47104
go fmt.
raulk May 18, 2019
d017d06
remove WIP notice from README.md.
raulk May 18, 2019
a69990c
add Close to interface, implement in nullconnmgr
vyzo May 18, 2019
3953b18
Merge pull request #6 from libp2p/update-connmgr-interface
vyzo May 18, 2019
0b2b19c
metrics: allow getting metrics by peer/protocol (#5)
May 22, 2019
f5a63f2
sync peerstore interface changes.
raulk May 22, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions alias.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Package core provides convenient access to foundational, central go-libp2p primitives via type aliases.
package core

import (
"github.com/libp2p/go-libp2p-core/host"
"github.com/libp2p/go-libp2p-core/network"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p-core/protocol"
"github.com/multiformats/go-multiaddr"
)

// Multiaddr aliases the Multiaddr type from github.com/multiformats/go-multiaddr.
//
// Refer to the docs on that type for more info.
type Multiaddr = multiaddr.Multiaddr

// PeerID aliases peer.ID.
//
// Refer to the docs on that type for more info.
type PeerID = peer.ID

// PeerID aliases protocol.ID.
//
// Refer to the docs on that type for more info.
type ProtocolID = protocol.ID

// PeerAddrInfo aliases peer.AddrInfo.
//
// Refer to the docs on that type for more info.
type PeerAddrInfo = peer.AddrInfo

// Host aliases host.Host.
//
// Refer to the docs on that type for more info.
type Host = host.Host

// Network aliases network.Network.
//
// Refer to the docs on that type for more info.
type Network = network.Network

// Conn aliases network.Conn.
//
// Refer to the docs on that type for more info.
type Conn = network.Conn

// Stream aliases network.Stream.
//
// Refer to the docs on that type for more info.
type Stream = network.Stream
68 changes: 68 additions & 0 deletions connmgr/connmgr.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Package connmgr provides connection tracking and management interfaces for libp2p.
//
// The ConnManager interface exported from this package allows libp2p to enforce an
// upper bound on the total number of open connections. To avoid service disruptions,
// connections can be tagged with metadata and optionally "protected" to ensure that
// essential connections are not arbitrarily cut.
package connmgr

import (
"context"
"time"

"github.com/libp2p/go-libp2p-core/network"
"github.com/libp2p/go-libp2p-core/peer"
)

// ConnManager tracks connections to peers, and allows consumers to associate metadata
// with each peer.
//
// It enables connections to be trimmed based on implementation-defined heuristics.
// The ConnManager allows libp2p to enforce an upper bound on the total number of
// open connections.
type ConnManager interface {
vyzo marked this conversation as resolved.
Show resolved Hide resolved

// TagPeer tags a peer with a string, associating a weight with the tag.
TagPeer(peer.ID, string, int)

// Untag removes the tagged value from the peer.
UntagPeer(p peer.ID, tag string)

// GetTagInfo returns the metadata associated with the peer,
// or nil if no metadata has been recorded for the peer.
GetTagInfo(p peer.ID) *TagInfo

// TrimOpenConns terminates open connections based on an implementation-defined
// heuristic.
TrimOpenConns(ctx context.Context)

// Notifee returns an implementation that can be called back to inform of
// opened and closed connections.
Notifee() network.Notifiee

// Protect protects a peer from having its connection(s) pruned.
//
// Tagging allows different parts of the system to manage protections without interfering with one another.
//
// Calls to Protect() with the same tag are idempotent. They are not refcounted, so after multiple calls
// to Protect() with the same tag, a single Unprotect() call bearing the same tag will revoke the protection.
Protect(id peer.ID, tag string)

// Unprotect removes a protection that may have been placed on a peer, under the specified tag.
//
// The return value indicates whether the peer continues to be protected after this call, by way of a different tag.
// See notes on Protect() for more info.
Unprotect(id peer.ID, tag string) (protected bool)
}

// TagInfo stores metadata associated with a peer.
type TagInfo struct {
FirstSeen time.Time
Value int

// Tags maps tag ids to the numerical values.
Tags map[string]int

// Conns maps connection ids (such as remote multiaddr) to their creation time.
Conns map[string]time.Time
}
21 changes: 21 additions & 0 deletions connmgr/null.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package connmgr

import (
"context"

"github.com/libp2p/go-libp2p-core/network"
"github.com/libp2p/go-libp2p-core/peer"
)

// NullConnMgr is a ConnMgr that provides no functionality.
type NullConnMgr struct{}
raulk marked this conversation as resolved.
Show resolved Hide resolved

var _ ConnManager = (*NullConnMgr)(nil)

func (_ NullConnMgr) TagPeer(peer.ID, string, int) {}
func (_ NullConnMgr) UntagPeer(peer.ID, string) {}
func (_ NullConnMgr) GetTagInfo(peer.ID) *TagInfo { return &TagInfo{} }
func (_ NullConnMgr) TrimOpenConns(ctx context.Context) {}
func (_ NullConnMgr) Notifee() network.Notifiee { return network.GlobalNoopNotifiee }
func (_ NullConnMgr) Protect(peer.ID, string) {}
func (_ NullConnMgr) Unprotect(peer.ID, string) bool { return false }
84 changes: 84 additions & 0 deletions crypto/bench_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package crypto

import "testing"

func BenchmarkSignRSA1B(b *testing.B) { RunBenchmarkSignRSA(b, 1) }
func BenchmarkSignRSA10B(b *testing.B) { RunBenchmarkSignRSA(b, 10) }
func BenchmarkSignRSA100B(b *testing.B) { RunBenchmarkSignRSA(b, 100) }
func BenchmarkSignRSA1000B(b *testing.B) { RunBenchmarkSignRSA(b, 1000) }
func BenchmarkSignRSA10000B(b *testing.B) { RunBenchmarkSignRSA(b, 10000) }
func BenchmarkSignRSA100000B(b *testing.B) { RunBenchmarkSignRSA(b, 100000) }

func BenchmarkVerifyRSA1B(b *testing.B) { RunBenchmarkVerifyRSA(b, 1) }
func BenchmarkVerifyRSA10B(b *testing.B) { RunBenchmarkVerifyRSA(b, 10) }
func BenchmarkVerifyRSA100B(b *testing.B) { RunBenchmarkVerifyRSA(b, 100) }
func BenchmarkVerifyRSA1000B(b *testing.B) { RunBenchmarkVerifyRSA(b, 1000) }
func BenchmarkVerifyRSA10000B(b *testing.B) { RunBenchmarkVerifyRSA(b, 10000) }
func BenchmarkVerifyRSA100000B(b *testing.B) { RunBenchmarkVerifyRSA(b, 100000) }

func BenchmarkSignEd255191B(b *testing.B) { RunBenchmarkSignEd25519(b, 1) }
func BenchmarkSignEd2551910B(b *testing.B) { RunBenchmarkSignEd25519(b, 10) }
func BenchmarkSignEd25519100B(b *testing.B) { RunBenchmarkSignEd25519(b, 100) }
func BenchmarkSignEd255191000B(b *testing.B) { RunBenchmarkSignEd25519(b, 1000) }
func BenchmarkSignEd2551910000B(b *testing.B) { RunBenchmarkSignEd25519(b, 10000) }
func BenchmarkSignEd25519100000B(b *testing.B) { RunBenchmarkSignEd25519(b, 100000) }

func BenchmarkVerifyEd255191B(b *testing.B) { RunBenchmarkVerifyEd25519(b, 1) }
func BenchmarkVerifyEd2551910B(b *testing.B) { RunBenchmarkVerifyEd25519(b, 10) }
func BenchmarkVerifyEd25519100B(b *testing.B) { RunBenchmarkVerifyEd25519(b, 100) }
func BenchmarkVerifyEd255191000B(b *testing.B) { RunBenchmarkVerifyEd25519(b, 1000) }
func BenchmarkVerifyEd2551910000B(b *testing.B) { RunBenchmarkVerifyEd25519(b, 10000) }
func BenchmarkVerifyEd25519100000B(b *testing.B) { RunBenchmarkVerifyEd25519(b, 100000) }

func RunBenchmarkSignRSA(b *testing.B, numBytes int) {
runBenchmarkSign(b, numBytes, RSA)
}

func RunBenchmarkSignEd25519(b *testing.B, numBytes int) {
runBenchmarkSign(b, numBytes, Ed25519)
}

func runBenchmarkSign(b *testing.B, numBytes int, t int) {
secret, _, err := GenerateKeyPair(t, 1024)
if err != nil {
b.Fatal(err)
}
someData := make([]byte, numBytes)
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err := secret.Sign(someData)
if err != nil {
b.Fatal(err)
}
}
}

func RunBenchmarkVerifyRSA(b *testing.B, numBytes int) {
runBenchmarkSign(b, numBytes, RSA)
}

func RunBenchmarkVerifyEd25519(b *testing.B, numBytes int) {
runBenchmarkSign(b, numBytes, Ed25519)
}

func runBenchmarkVerify(b *testing.B, numBytes int, t int) {
secret, public, err := GenerateKeyPair(t, 1024)
if err != nil {
b.Fatal(err)
}
someData := make([]byte, numBytes)
signature, err := secret.Sign(someData)
if err != nil {
b.Fatal(err)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
valid, err := public.Verify(someData, signature)
if err != nil {
b.Fatal(err)
}
if !valid {
b.Fatal("signature should be valid")
}
}
}
Loading