Skip to content

Commit

Permalink
rename the BasicHost to FullHost (and move and rename the package)
Browse files Browse the repository at this point in the history
  • Loading branch information
marten-seemann committed Jan 9, 2022
1 parent ef4b6f0 commit 8536f68
Show file tree
Hide file tree
Showing 12 changed files with 71 additions and 68 deletions.
10 changes: 5 additions & 5 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"github.com/libp2p/go-libp2p/p2p/host/autonat"
"github.com/libp2p/go-libp2p/p2p/host/autorelay"
"github.com/libp2p/go-libp2p/p2p/host/base"
bhost "github.com/libp2p/go-libp2p/p2p/host/basic"
fullhost "github.com/libp2p/go-libp2p/p2p/host/full"
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"
Expand All @@ -40,10 +40,10 @@ var log = logging.Logger("p2p-config")

// AddrsFactory is a function that takes a set of multiaddrs we're listening on and
// returns the set of multiaddrs we should advertise to the network.
type AddrsFactory = bhost.AddrsFactory
type AddrsFactory = fullhost.AddrsFactory

// NATManagerC is a NATManager constructor.
type NATManagerC func(network.Network) bhost.NATManager
type NATManagerC func(network.Network) fullhost.NATManager

type RoutingC func(host.Host) (routing.PeerRouting, error)

Expand Down Expand Up @@ -84,7 +84,7 @@ type Config struct {
RelayServiceOpts []relayv2.Option

ListenAddrs []ma.Multiaddr
AddrsFactory bhost.AddrsFactory
AddrsFactory fullhost.AddrsFactory
ConnectionGater connmgr.ConnectionGater

ConnManager connmgr.ConnManager
Expand Down Expand Up @@ -212,7 +212,7 @@ func (cfg *Config) NewNode() (host.Host, error) {
return nil, err
}

h, err := bhost.NewHost(swrm, &bhost.HostOpts{
h, err := fullhost.NewHost(swrm, &fullhost.HostOpts{
ConnManager: cfg.ConnManager,
AddrsFactory: cfg.AddrsFactory,
NATManager: cfg.NATManager,
Expand Down
4 changes: 2 additions & 2 deletions config/muxer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/libp2p/go-libp2p-core/host"
"github.com/libp2p/go-libp2p-core/peer"
swarmt "github.com/libp2p/go-libp2p-swarm/testing"
bhost "github.com/libp2p/go-libp2p/p2p/host/basic"
fullhost "github.com/libp2p/go-libp2p/p2p/host/full"

"github.com/libp2p/go-libp2p-core/mux"
yamux "github.com/libp2p/go-libp2p-yamux"
Expand Down Expand Up @@ -58,7 +58,7 @@ func TestMuxerBadTypes(t *testing.T) {
}

func TestCatchDuplicateTransportsMuxer(t *testing.T) {
h, err := bhost.NewHost(swarmt.GenSwarm(t), nil)
h, err := fullhost.NewHost(swarmt.GenSwarm(t), nil)
if err != nil {
t.Fatal(err)
}
Expand Down
4 changes: 2 additions & 2 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (

"github.com/libp2p/go-libp2p/config"
"github.com/libp2p/go-libp2p/p2p/host/autorelay"
bhost "github.com/libp2p/go-libp2p/p2p/host/basic"
fullhost "github.com/libp2p/go-libp2p/p2p/host/full"
relayv2 "github.com/libp2p/go-libp2p/p2p/protocol/circuitv2/relay"
"github.com/libp2p/go-libp2p/p2p/protocol/holepunch"

Expand Down Expand Up @@ -349,7 +349,7 @@ func ConnectionGater(cg connmgr.ConnectionGater) Option {
// NATPortMap configures libp2p to use the default NATManager. The default
// NATManager will attempt to open a port in your network's firewall using UPnP.
func NATPortMap() Option {
return NATManager(bhost.NewNATManager)
return NATManager(fullhost.NewNATManager)
}

// NATManager will configure libp2p to use the requested NATManager. This
Expand Down
8 changes: 4 additions & 4 deletions p2p/discovery/mdns_legacy/mdns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/libp2p/go-libp2p-core/host"
"github.com/libp2p/go-libp2p-core/peer"
swarmt "github.com/libp2p/go-libp2p-swarm/testing"
bhost "github.com/libp2p/go-libp2p/p2p/host/basic"
fullhost "github.com/libp2p/go-libp2p/p2p/host/full"
)

type DiscoveryNotifee struct {
Expand All @@ -22,15 +22,15 @@ func (n *DiscoveryNotifee) HandlePeerFound(pi peer.AddrInfo) {
}

func TestMdnsDiscovery(t *testing.T) {
//TODO: re-enable when the new lib will get integrated
// TODO: re-enable when the new lib will get integrated
t.Skip("TestMdnsDiscovery fails randomly with current lib")

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

a, err := bhost.NewHost(swarmt.GenSwarm(t), nil)
a, err := fullhost.NewHost(swarmt.GenSwarm(t), nil)
require.NoError(t, err)
b, err := bhost.NewHost(swarmt.GenSwarm(t), nil)
b, err := fullhost.NewHost(swarmt.GenSwarm(t), nil)
require.NoError(t, err)

sa, err := NewMdnsService(ctx, a, time.Second, "someTag")
Expand Down
14 changes: 7 additions & 7 deletions p2p/host/autorelay/autorelay.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
"github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p-core/routing"

basic "github.com/libp2p/go-libp2p/p2p/host/basic"
fullhost "github.com/libp2p/go-libp2p/p2p/host/full"
relayv1 "github.com/libp2p/go-libp2p/p2p/protocol/circuitv1/relay"
circuitv2 "github.com/libp2p/go-libp2p/p2p/protocol/circuitv2/client"
circuitv2_proto "github.com/libp2p/go-libp2p/p2p/protocol/circuitv2/proto"
Expand Down Expand Up @@ -92,10 +92,10 @@ func WithDiscoverer(discover discovery.Discoverer) Option {

// AutoRelay is a Host that uses relays for connectivity when a NAT is detected.
type AutoRelay struct {
host *basic.BasicHost
host *fullhost.FullHost
discover discovery.Discoverer
router routing.PeerRouting
addrsF basic.AddrsFactory
addrsF fullhost.AddrsFactory

static []peer.AddrInfo

Expand All @@ -113,13 +113,13 @@ type AutoRelay struct {
cachedAddrsExpiry time.Time
}

func NewAutoRelay(bhost *basic.BasicHost, router routing.PeerRouting, opts ...Option) (*AutoRelay, error) {
func NewAutoRelay(h *fullhost.FullHost, router routing.PeerRouting, opts ...Option) (*AutoRelay, error) {
ctx, cancel := context.WithCancel(context.Background())
ar := &AutoRelay{
ctxCancel: cancel,
host: bhost,
host: h,
router: router,
addrsF: bhost.AddrsFactory,
addrsF: h.AddrsFactory,
relays: make(map[peer.ID]*circuitv2.Reservation),
relayFound: make(chan struct{}, 1),
status: network.ReachabilityUnknown,
Expand All @@ -129,7 +129,7 @@ func NewAutoRelay(bhost *basic.BasicHost, router routing.PeerRouting, opts ...Op
return nil, err
}
}
bhost.AddrsFactory = ar.hostAddrs
h.AddrsFactory = ar.hostAddrs
ar.refCount.Add(1)
go ar.background(ctx)
return ar, nil
Expand Down
58 changes: 29 additions & 29 deletions p2p/host/basic/basic_host.go → p2p/host/full/host.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package basichost
package fullhost

import (
"context"
Expand Down Expand Up @@ -46,7 +46,7 @@ const maxAddressResolution = 32
// addrChangeTickrInterval is the interval between two address change ticks.
var addrChangeTickrInterval = 5 * time.Second

var log = logging.Logger("basichost")
var log = logging.Logger("fullhost")

var (
// DefaultNegotiationTimeout is the default value for HostOpts.NegotiationTimeout.
Expand All @@ -60,12 +60,12 @@ var (
// addresses returned by Addrs.
type AddrsFactory func([]ma.Multiaddr) []ma.Multiaddr

// BasicHost is the basic implementation of the host.Host interface. This
// FullHost is an implementation of the host.Host interface. This
// particular host implementation:
// * uses a protocol muxer to mux per-protocol streams
// * uses an identity service to send + receive node information
// * uses a nat service to establish NAT port mappings
type BasicHost struct {
type FullHost struct {
*base.BaseHost // a base host

ctx context.Context
Expand Down Expand Up @@ -104,12 +104,12 @@ type BasicHost struct {
autoNat autonat.AutoNAT
}

var _ host.Host = &BasicHost{}
var _ host.Host = &FullHost{}

// HostOpts holds options that can be passed to NewHost in order to
// customize construction of the *BasicHost.
// customize construction of the *FullHost.
type HostOpts struct {
// MultistreamMuxer is essential for the *BasicHost and will use a sensible default value if omitted.
// MultistreamMuxer is essential for the *FullHost and will use a sensible default value if omitted.
MultistreamMuxer *msmux.MultistreamMuxer

// NegotiationTimeout determines the read and write timeouts on streams.
Expand Down Expand Up @@ -152,8 +152,8 @@ type HostOpts struct {
HolePunchingOptions []holepunch.Option
}

// NewHost constructs a new *BasicHost and activates it by attaching its stream and connection handlers to the given inet.Network.
func NewHost(n network.Network, opts *HostOpts) (*BasicHost, error) {
// NewHost constructs a new *FullHost and activates it by attaching its stream and connection handlers to the given inet.Network.
func NewHost(n network.Network, opts *HostOpts) (*FullHost, error) {
if opts == nil {
opts = &HostOpts{}
}
Expand All @@ -171,7 +171,7 @@ func NewHost(n network.Network, opts *HostOpts) (*BasicHost, error) {

hostCtx, cancel := context.WithCancel(context.Background())

h := &BasicHost{
h := &FullHost{
BaseHost: baseHost,
negtimeout: DefaultNegotiationTimeout,
AddrsFactory: DefaultAddrsFactory,
Expand Down Expand Up @@ -218,7 +218,7 @@ func NewHost(n network.Network, opts *HostOpts) (*BasicHost, error) {
h.mux = opts.MultistreamMuxer
}

// we can't set this as a default above because it depends on the *BasicHost.
// we can't set this as a default above because it depends on the *FullHost.
if h.disableSignedPeerRecord {
h.ids, err = identify.NewIDService(h, identify.UserAgent(opts.UserAgent), identify.DisableSignedPeerRecord())
} else {
Expand Down Expand Up @@ -274,7 +274,7 @@ func NewHost(n network.Network, opts *HostOpts) (*BasicHost, error) {
return h, nil
}

func (h *BasicHost) updateLocalIpAddr() {
func (h *FullHost) updateLocalIpAddr() {
h.addrMu.Lock()
defer h.addrMu.Unlock()

Expand Down Expand Up @@ -343,15 +343,15 @@ func (h *BasicHost) updateLocalIpAddr() {
}

// Start starts background tasks in the host
func (h *BasicHost) Start() {
func (h *FullHost) Start() {
h.BaseHost.Start()
h.refCount.Add(1)
go h.background()
}

// newStreamHandler is the remote-opened stream handler for network.Network
// TODO: this feels a bit wonky
func (h *BasicHost) newStreamHandler(s network.Stream) {
func (h *FullHost) newStreamHandler(s network.Stream) {
before := time.Now()

if h.negtimeout > 0 {
Expand Down Expand Up @@ -400,7 +400,7 @@ func (h *BasicHost) newStreamHandler(s network.Stream) {
// SignalAddressChange signals to the host that it needs to determine whether our listen addresses have recently
// changed.
// Warning: this interface is unstable and may disappear in the future.
func (h *BasicHost) SignalAddressChange() {
func (h *FullHost) SignalAddressChange() {
select {
case h.addrChangeChan <- struct{}{}:
default:
Expand Down Expand Up @@ -439,7 +439,7 @@ func makeUpdatedAddrEvent(prev, current []ma.Multiaddr) *event.EvtLocalAddresses
return &evt
}

func (h *BasicHost) makeSignedPeerRecord(evt *event.EvtLocalAddressesUpdated) (*record.Envelope, error) {
func (h *FullHost) makeSignedPeerRecord(evt *event.EvtLocalAddressesUpdated) (*record.Envelope, error) {
current := make([]ma.Multiaddr, 0, len(evt.Current))
for _, a := range evt.Current {
current = append(current, a.Address)
Expand All @@ -452,7 +452,7 @@ func (h *BasicHost) makeSignedPeerRecord(evt *event.EvtLocalAddressesUpdated) (*
return record.Seal(rec, h.signKey)
}

func (h *BasicHost) background() {
func (h *FullHost) background() {
defer h.refCount.Done()
var lastAddrs []ma.Multiaddr

Expand Down Expand Up @@ -515,15 +515,15 @@ func (h *BasicHost) background() {
}

// IDService returns
func (h *BasicHost) IDService() identify.IDService {
func (h *FullHost) IDService() identify.IDService {
return h.ids
}

// NewStream opens a new stream to given peer p, and writes a p2p/protocol
// header with given protocol.ID. If there is no connection to p, attempts
// to create one. If ProtocolID is "", writes no header.
// (Threadsafe)
func (h *BasicHost) NewStream(ctx context.Context, p peer.ID, pids ...protocol.ID) (network.Stream, error) {
func (h *FullHost) NewStream(ctx context.Context, p peer.ID, pids ...protocol.ID) (network.Stream, error) {
s, err := h.Network().NewStream(ctx, p)
if err != nil {
return nil, err
Expand Down Expand Up @@ -584,7 +584,7 @@ func (h *BasicHost) NewStream(ctx context.Context, p peer.ID, pids ...protocol.I
return s, nil
}

func (h *BasicHost) preferredProtocol(p peer.ID, pids []string) (protocol.ID, error) {
func (h *FullHost) preferredProtocol(p peer.ID, pids []string) (protocol.ID, error) {
supported, err := h.Peerstore().SupportsProtocols(p, pids...)
if err != nil {
return "", err
Expand All @@ -602,7 +602,7 @@ func (h *BasicHost) preferredProtocol(p peer.ID, pids []string) (protocol.ID, er
// h.Network.Dial, and block until a connection is open, or an error is returned.
// Connect will absorb the addresses in pi into its internal peerstore.
// It will also resolve any /dns4, /dns6, and /dnsaddr addresses.
func (h *BasicHost) Connect(ctx context.Context, pi peer.AddrInfo) error {
func (h *FullHost) Connect(ctx context.Context, pi peer.AddrInfo) error {
// absorb addresses into peerstore
h.Peerstore().AddAddrs(pi.ID, pi.Addrs, peerstore.TempAddrTTL)

Expand All @@ -622,7 +622,7 @@ func (h *BasicHost) Connect(ctx context.Context, pi peer.AddrInfo) error {
return h.dialPeer(ctx, pi.ID)
}

func (h *BasicHost) resolveAddrs(ctx context.Context, pi peer.AddrInfo) ([]ma.Multiaddr, error) {
func (h *FullHost) resolveAddrs(ctx context.Context, pi peer.AddrInfo) ([]ma.Multiaddr, error) {
proto := ma.ProtocolWithCode(ma.P_P2P).Name
p2paddr, err := ma.NewMultiaddr("/" + proto + "/" + pi.ID.Pretty())
if err != nil {
Expand Down Expand Up @@ -686,7 +686,7 @@ func (h *BasicHost) resolveAddrs(ctx context.Context, pi peer.AddrInfo) ([]ma.Mu

// dialPeer opens a connection to peer, and makes sure to identify
// the connection once it has been opened.
func (h *BasicHost) dialPeer(ctx context.Context, p peer.ID) error {
func (h *FullHost) dialPeer(ctx context.Context, p peer.ID) error {
log.Debugf("host %s dialing %s", h.ID(), p)
c, err := h.Network().DialPeer(ctx, p)
if err != nil {
Expand All @@ -710,7 +710,7 @@ func (h *BasicHost) dialPeer(ctx context.Context, p peer.ID) error {

// Addrs returns listening addresses that are safe to announce to the network.
// The output is the same as AllAddrs, but processed by AddrsFactory.
func (h *BasicHost) Addrs() []ma.Multiaddr {
func (h *FullHost) Addrs() []ma.Multiaddr {
return h.AddrsFactory(h.AllAddrs())
}

Expand All @@ -728,9 +728,9 @@ func dedupAddrs(addrs []ma.Multiaddr) (uniqueAddrs []ma.Multiaddr) {
return uniqueAddrs
}

// AllAddrs returns all the addresses of BasicHost at this moment in time.
// AllAddrs returns all the addresses of FullHost at this moment in time.
// It's ok to not include addresses if they're not available to be used now.
func (h *BasicHost) AllAddrs() []ma.Multiaddr {
func (h *FullHost) AllAddrs() []ma.Multiaddr {
listenAddrs := h.Network().ListenAddresses()
if len(listenAddrs) == 0 {
return nil
Expand Down Expand Up @@ -921,7 +921,7 @@ func (h *BasicHost) AllAddrs() []ma.Multiaddr {
}

// SetAutoNat sets the autonat service for the host.
func (h *BasicHost) SetAutoNat(a autonat.AutoNAT) {
func (h *FullHost) SetAutoNat(a autonat.AutoNAT) {
h.addrMu.Lock()
defer h.addrMu.Unlock()
if h.autoNat == nil {
Expand All @@ -930,14 +930,14 @@ func (h *BasicHost) SetAutoNat(a autonat.AutoNAT) {
}

// Return the host's AutoNAT service, if AutoNAT is enabled.
func (h *BasicHost) GetAutoNat() autonat.AutoNAT {
func (h *FullHost) GetAutoNat() autonat.AutoNAT {
h.addrMu.Lock()
defer h.addrMu.Unlock()
return h.autoNat
}

// Close shuts down the Host's services (network, etc).
func (h *BasicHost) Close() error {
func (h *FullHost) Close() error {
h.closeSync.Do(func() {
h.BaseHost.Close()
h.ctxCancel()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package basichost
package fullhost

import (
"context"
Expand Down
2 changes: 1 addition & 1 deletion p2p/host/basic/natmgr.go → p2p/host/full/natmgr.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package basichost
package fullhost

import (
"context"
Expand Down
Loading

0 comments on commit 8536f68

Please sign in to comment.