Skip to content
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

move the go-libp2p-blankhost here #1411

Merged
merged 28 commits into from
Apr 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
cd20113
implement barebones 'blank' host
whyrusleeping Nov 17, 2016
abf3fbb
update go-libp2p-net and host (with peerstore updates)
whyrusleeping Nov 18, 2016
07cdcbb
fix multistream import paths
whyrusleeping Jan 10, 2017
a727489
gx publish 0.1.6
whyrusleeping Jan 10, 2017
ce25588
add in connmgr interfaces
whyrusleeping Jul 12, 2017
8a2f0cb
gx publish 0.2.1
whyrusleeping Oct 6, 2017
12917c5
Consolidate abstractions and core types into go-libp2p-core (#21)
Stebalien May 24, 2019
88e0587
use migrated types; drop import prefixes. (#22)
raulk May 25, 2019
29bca34
integrate the event bus into host implementations (#23)
raulk Jun 20, 2019
8e35f1f
blank host should support signed records
aarshkshah1992 May 4, 2020
f318aed
Merge pull request #42 from libp2p/feat/signed-records
aarshkshah1992 May 5, 2020
4524c22
add WithConnectionManager option to blankhost
yusefnapora May 8, 2020
693d670
Merge pull request #44 from libp2p/feat/connmgr-option
vyzo May 8, 2020
f124798
subscribe connmgr to net notifications
yusefnapora May 11, 2020
0d68774
Merge pull request #45 from libp2p/fix/connmgr-notifee
vyzo May 11, 2020
7c82a6d
fix: remove deprecated log.Warning call
Stebalien Jun 24, 2020
05ebe06
fix: reset stream on error
Stebalien Jun 24, 2020
d96d77f
Merge pull request #52 from libp2p/chore/update-deps
Stebalien Jun 24, 2020
67d1fe5
fix go vet
marten-seemann May 19, 2021
e38b26e
Merge pull request #53 from libp2p/fix-go-vet
marten-seemann May 19, 2021
1893970
chore: update go-log to v2
marten-seemann Nov 28, 2021
8b1829b
Merge pull request #59 from libp2p/update-go-log
marten-seemann Nov 28, 2021
dd821e8
emit the EvtPeerConnectednessChanged event
marten-seemann Nov 28, 2021
fcfa4cc
Merge pull request #58 from libp2p/peer-connectedness
marten-seemann Nov 30, 2021
231f93d
add a WithEventBus constructor option
marten-seemann Nov 30, 2021
1751797
Merge pull request #61 from libp2p/event-bus-option
marten-seemann Dec 2, 2021
f0d63ef
move the go-libp2p-blankhost here
marten-seemann Apr 19, 2022
fc2c34e
switch from github.com/libp2p/go-libp2p-blankhost to p2p/host/blank
marten-seemann Apr 19, 2022
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
2 changes: 1 addition & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ import (
"github.com/libp2p/go-libp2p/p2p/host/autonat"
"github.com/libp2p/go-libp2p/p2p/host/autorelay"
bhost "github.com/libp2p/go-libp2p/p2p/host/basic"
blankhost "github.com/libp2p/go-libp2p/p2p/host/blank"
routed "github.com/libp2p/go-libp2p/p2p/host/routed"
circuitv2 "github.com/libp2p/go-libp2p/p2p/protocol/circuitv2/client"
relayv2 "github.com/libp2p/go-libp2p/p2p/protocol/circuitv2/relay"
"github.com/libp2p/go-libp2p/p2p/protocol/holepunch"

blankhost "github.com/libp2p/go-libp2p-blankhost"
swarm "github.com/libp2p/go-libp2p-swarm"
tptu "github.com/libp2p/go-libp2p-transport-upgrader"

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ require (
github.com/libp2p/go-conn-security-multistream v0.3.0
github.com/libp2p/go-eventbus v0.2.1
github.com/libp2p/go-libp2p-asn-util v0.1.0
github.com/libp2p/go-libp2p-blankhost v0.3.0
github.com/libp2p/go-libp2p-circuit v0.6.0
github.com/libp2p/go-libp2p-core v0.15.1
github.com/libp2p/go-libp2p-nat v0.1.0
Expand Down Expand Up @@ -75,6 +74,7 @@ require (
github.com/koron/go-ssdp v0.0.2 // indirect
github.com/libp2p/go-cidranger v1.1.0 // indirect
github.com/libp2p/go-flow-metrics v0.0.3 // indirect
github.com/libp2p/go-libp2p-blankhost v0.3.0 // indirect
github.com/libp2p/go-libp2p-pnet v0.2.0 // indirect
github.com/libp2p/go-nat v0.1.0 // indirect
github.com/libp2p/go-openssl v0.0.7 // indirect
Expand Down
2 changes: 1 addition & 1 deletion p2p/discovery/backoff/backoffcache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import (
"time"

"github.com/libp2p/go-libp2p/p2p/discovery/mocks"
bhost "github.com/libp2p/go-libp2p/p2p/host/blank"

"github.com/libp2p/go-libp2p-core/discovery"
"github.com/libp2p/go-libp2p-core/peer"

bhost "github.com/libp2p/go-libp2p-blankhost"
swarmt "github.com/libp2p/go-libp2p-swarm/testing"
)

Expand Down
3 changes: 2 additions & 1 deletion p2p/discovery/backoff/backoffconnector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ import (
"testing"
"time"

bhost "github.com/libp2p/go-libp2p/p2p/host/blank"

"github.com/libp2p/go-libp2p-core/host"
"github.com/libp2p/go-libp2p-core/peer"

bhost "github.com/libp2p/go-libp2p-blankhost"
swarmt "github.com/libp2p/go-libp2p-swarm/testing"

"github.com/stretchr/testify/require"
Expand Down
3 changes: 1 addition & 2 deletions p2p/discovery/routing/routing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ import (
"time"

"github.com/libp2p/go-libp2p/p2p/discovery/mocks"

"github.com/libp2p/go-libp2p/p2p/discovery/util"
bhost "github.com/libp2p/go-libp2p/p2p/host/blank"

"github.com/ipfs/go-cid"
bhost "github.com/libp2p/go-libp2p-blankhost"
"github.com/libp2p/go-libp2p-core/discovery"
"github.com/libp2p/go-libp2p-core/host"
"github.com/libp2p/go-libp2p-core/peer"
Expand Down
2 changes: 1 addition & 1 deletion p2p/host/autonat/autonat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import (
"time"

pb "github.com/libp2p/go-libp2p/p2p/host/autonat/pb"
bhost "github.com/libp2p/go-libp2p/p2p/host/blank"

"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"

bhost "github.com/libp2p/go-libp2p-blankhost"
swarmt "github.com/libp2p/go-libp2p-swarm/testing"
"github.com/libp2p/go-msgio/protoio"
ma "github.com/multiformats/go-multiaddr"
Expand Down
2 changes: 1 addition & 1 deletion p2p/host/autonat/dialpolicy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import (
"net"
"testing"

blankhost "github.com/libp2p/go-libp2p-blankhost"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p-core/transport"
swarmt "github.com/libp2p/go-libp2p-swarm/testing"
blankhost "github.com/libp2p/go-libp2p/p2p/host/blank"
"github.com/multiformats/go-multiaddr"
)

Expand Down
3 changes: 2 additions & 1 deletion p2p/host/autonat/svc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ import (
"testing"
"time"

bhost "github.com/libp2p/go-libp2p/p2p/host/blank"

"github.com/libp2p/go-libp2p-core/event"
"github.com/libp2p/go-libp2p-core/host"
"github.com/libp2p/go-libp2p-core/network"

bhost "github.com/libp2p/go-libp2p-blankhost"
swarmt "github.com/libp2p/go-libp2p-swarm/testing"

ma "github.com/multiformats/go-multiaddr"
Expand Down
241 changes: 241 additions & 0 deletions p2p/host/blank/blank.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,241 @@
package blankhost

import (
"context"
"errors"
"fmt"
"io"

"github.com/libp2p/go-libp2p-core/connmgr"
"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"
"github.com/libp2p/go-libp2p-core/peerstore"
"github.com/libp2p/go-libp2p-core/protocol"
"github.com/libp2p/go-libp2p-core/record"

"github.com/libp2p/go-eventbus"

logging "github.com/ipfs/go-log/v2"

ma "github.com/multiformats/go-multiaddr"
mstream "github.com/multiformats/go-multistream"
)

var log = logging.Logger("blankhost")

// BlankHost is the thinnest implementation of the host.Host interface
type BlankHost struct {
n network.Network
mux *mstream.MultistreamMuxer
cmgr connmgr.ConnManager
eventbus event.Bus
emitters struct {
evtLocalProtocolsUpdated event.Emitter
}
}

type config struct {
cmgr connmgr.ConnManager
eventBus event.Bus
}

type Option = func(cfg *config)

func WithConnectionManager(cmgr connmgr.ConnManager) Option {
return func(cfg *config) {
cfg.cmgr = cmgr
}
}

func WithEventBus(eventBus event.Bus) Option {
return func(cfg *config) {
cfg.eventBus = eventBus
}
}

func NewBlankHost(n network.Network, options ...Option) *BlankHost {
cfg := config{
cmgr: &connmgr.NullConnMgr{},
}
for _, opt := range options {
opt(&cfg)
}

bh := &BlankHost{
n: n,
cmgr: cfg.cmgr,
mux: mstream.NewMultistreamMuxer(),
}
if bh.eventbus == nil {
bh.eventbus = eventbus.NewBus()
}

// subscribe the connection manager to network notifications (has no effect with NullConnMgr)
n.Notify(bh.cmgr.Notifee())

var err error
if bh.emitters.evtLocalProtocolsUpdated, err = bh.eventbus.Emitter(&event.EvtLocalProtocolsUpdated{}); err != nil {
return nil
}
evtPeerConnectednessChanged, err := bh.eventbus.Emitter(&event.EvtPeerConnectednessChanged{})
if err != nil {
return nil
}
n.Notify(newPeerConnectWatcher(evtPeerConnectednessChanged))

n.SetStreamHandler(bh.newStreamHandler)

// persist a signed peer record for self to the peerstore.
if err := bh.initSignedRecord(); err != nil {
log.Errorf("error creating blank host, err=%s", err)
return nil
}

return bh
}

func (bh *BlankHost) initSignedRecord() error {
cab, ok := peerstore.GetCertifiedAddrBook(bh.n.Peerstore())
if !ok {
log.Error("peerstore does not support signed records")
return errors.New("peerstore does not support signed records")
}
rec := peer.PeerRecordFromAddrInfo(peer.AddrInfo{ID: bh.ID(), Addrs: bh.Addrs()})
ev, err := record.Seal(rec, bh.Peerstore().PrivKey(bh.ID()))
if err != nil {
log.Errorf("failed to create signed record for self, err=%s", err)
return fmt.Errorf("failed to create signed record for self, err=%s", err)
}
_, err = cab.ConsumePeerRecord(ev, peerstore.PermanentAddrTTL)
if err != nil {
log.Errorf("failed to persist signed record to peerstore,err=%s", err)
return fmt.Errorf("failed to persist signed record for self, err=%s", err)
}
return err
}

var _ host.Host = (*BlankHost)(nil)

func (bh *BlankHost) Addrs() []ma.Multiaddr {
addrs, err := bh.n.InterfaceListenAddresses()
if err != nil {
log.Debug("error retrieving network interface addrs: ", err)
return nil
}

return addrs
}

func (bh *BlankHost) Close() error {
return bh.n.Close()
}

func (bh *BlankHost) Connect(ctx context.Context, ai peer.AddrInfo) error {
// absorb addresses into peerstore
bh.Peerstore().AddAddrs(ai.ID, ai.Addrs, peerstore.TempAddrTTL)

cs := bh.n.ConnsToPeer(ai.ID)
if len(cs) > 0 {
return nil
}

_, err := bh.Network().DialPeer(ctx, ai.ID)
return err
}

func (bh *BlankHost) Peerstore() peerstore.Peerstore {
return bh.n.Peerstore()
}

func (bh *BlankHost) ID() peer.ID {
return bh.n.LocalPeer()
}

func (bh *BlankHost) NewStream(ctx context.Context, p peer.ID, protos ...protocol.ID) (network.Stream, error) {
s, err := bh.n.NewStream(ctx, p)
if err != nil {
return nil, err
}

var protoStrs []string
for _, pid := range protos {
protoStrs = append(protoStrs, string(pid))
}

selected, err := mstream.SelectOneOf(protoStrs, s)
if err != nil {
s.Reset()
return nil, err
}

selpid := protocol.ID(selected)
s.SetProtocol(selpid)
bh.Peerstore().AddProtocols(p, selected)

return s, nil
}

func (bh *BlankHost) RemoveStreamHandler(pid protocol.ID) {
bh.Mux().RemoveHandler(string(pid))
bh.emitters.evtLocalProtocolsUpdated.Emit(event.EvtLocalProtocolsUpdated{
Removed: []protocol.ID{pid},
})
}

func (bh *BlankHost) SetStreamHandler(pid protocol.ID, handler network.StreamHandler) {
bh.Mux().AddHandler(string(pid), func(p string, rwc io.ReadWriteCloser) error {
is := rwc.(network.Stream)
is.SetProtocol(protocol.ID(p))
handler(is)
return nil
})
bh.emitters.evtLocalProtocolsUpdated.Emit(event.EvtLocalProtocolsUpdated{
Added: []protocol.ID{pid},
})
}

func (bh *BlankHost) SetStreamHandlerMatch(pid protocol.ID, m func(string) bool, handler network.StreamHandler) {
bh.Mux().AddHandlerWithFunc(string(pid), m, func(p string, rwc io.ReadWriteCloser) error {
is := rwc.(network.Stream)
is.SetProtocol(protocol.ID(p))
handler(is)
return nil
})
bh.emitters.evtLocalProtocolsUpdated.Emit(event.EvtLocalProtocolsUpdated{
Added: []protocol.ID{pid},
})
}

// newStreamHandler is the remote-opened stream handler for network.Network
func (bh *BlankHost) newStreamHandler(s network.Stream) {
protoID, handle, err := bh.Mux().Negotiate(s)
if err != nil {
log.Infow("protocol negotiation failed", "error", err)
s.Reset()
return
}

s.SetProtocol(protocol.ID(protoID))

go handle(protoID, s)
}

// TODO: i'm not sure this really needs to be here
func (bh *BlankHost) Mux() protocol.Switch {
return bh.mux
}

// TODO: also not sure this fits... Might be better ways around this (leaky abstractions)
func (bh *BlankHost) Network() network.Network {
return bh.n
}

func (bh *BlankHost) ConnManager() connmgr.ConnManager {
return bh.cmgr
}

func (bh *BlankHost) EventBus() event.Bus {
return bh.eventbus
}
Loading