2021-06-16 20:19:45 +00:00
|
|
|
package blankhost
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
|
2022-11-04 13:57:20 +00:00
|
|
|
"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-libp2p/p2p/host/eventbus"
|
2021-06-16 20:19:45 +00:00
|
|
|
|
2022-04-01 16:16:46 +00:00
|
|
|
logging "github.com/ipfs/go-log/v2"
|
2021-06-16 20:19:45 +00:00
|
|
|
|
|
|
|
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
|
2023-02-22 21:58:17 +00:00
|
|
|
mux *mstream.MultistreamMuxer[protocol.ID]
|
2021-06-16 20:19:45 +00:00
|
|
|
cmgr connmgr.ConnManager
|
|
|
|
eventbus event.Bus
|
|
|
|
emitters struct {
|
|
|
|
evtLocalProtocolsUpdated event.Emitter
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type config struct {
|
2022-04-01 16:16:46 +00:00
|
|
|
cmgr connmgr.ConnManager
|
|
|
|
eventBus event.Bus
|
2021-06-16 20:19:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Option = func(cfg *config)
|
|
|
|
|
|
|
|
func WithConnectionManager(cmgr connmgr.ConnManager) Option {
|
|
|
|
return func(cfg *config) {
|
|
|
|
cfg.cmgr = cmgr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-01 16:16:46 +00:00
|
|
|
func WithEventBus(eventBus event.Bus) Option {
|
|
|
|
return func(cfg *config) {
|
|
|
|
cfg.eventBus = eventBus
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-16 20:19:45 +00:00
|
|
|
func NewBlankHost(n network.Network, options ...Option) *BlankHost {
|
|
|
|
cfg := config{
|
|
|
|
cmgr: &connmgr.NullConnMgr{},
|
|
|
|
}
|
|
|
|
for _, opt := range options {
|
|
|
|
opt(&cfg)
|
|
|
|
}
|
|
|
|
|
|
|
|
bh := &BlankHost{
|
2022-04-01 16:16:46 +00:00
|
|
|
n: n,
|
|
|
|
cmgr: cfg.cmgr,
|
2023-02-22 21:58:17 +00:00
|
|
|
mux: mstream.NewMultistreamMuxer[protocol.ID](),
|
2022-04-01 16:16:46 +00:00
|
|
|
}
|
|
|
|
if bh.eventbus == nil {
|
2023-02-22 21:58:17 +00:00
|
|
|
bh.eventbus = eventbus.NewBus(eventbus.WithMetricsTracer(eventbus.NewMetricsTracer()))
|
2021-06-16 20:19:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
|
|
|
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")
|
|
|
|
}
|
2022-04-01 16:16:46 +00:00
|
|
|
rec := peer.PeerRecordFromAddrInfo(peer.AddrInfo{ID: bh.ID(), Addrs: bh.Addrs()})
|
2021-06-16 20:19:45 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-02-22 21:58:17 +00:00
|
|
|
selected, err := mstream.SelectOneOf(protos, s)
|
2021-06-16 20:19:45 +00:00
|
|
|
if err != nil {
|
|
|
|
s.Reset()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-02-22 21:58:17 +00:00
|
|
|
s.SetProtocol(selected)
|
2021-06-16 20:19:45 +00:00
|
|
|
bh.Peerstore().AddProtocols(p, selected)
|
|
|
|
|
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bh *BlankHost) RemoveStreamHandler(pid protocol.ID) {
|
2023-02-22 21:58:17 +00:00
|
|
|
bh.Mux().RemoveHandler(pid)
|
2021-06-16 20:19:45 +00:00
|
|
|
bh.emitters.evtLocalProtocolsUpdated.Emit(event.EvtLocalProtocolsUpdated{
|
|
|
|
Removed: []protocol.ID{pid},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bh *BlankHost) SetStreamHandler(pid protocol.ID, handler network.StreamHandler) {
|
2023-02-22 21:58:17 +00:00
|
|
|
bh.Mux().AddHandler(pid, func(p protocol.ID, rwc io.ReadWriteCloser) error {
|
2021-06-16 20:19:45 +00:00
|
|
|
is := rwc.(network.Stream)
|
2023-02-22 21:58:17 +00:00
|
|
|
is.SetProtocol(p)
|
2021-06-16 20:19:45 +00:00
|
|
|
handler(is)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
bh.emitters.evtLocalProtocolsUpdated.Emit(event.EvtLocalProtocolsUpdated{
|
|
|
|
Added: []protocol.ID{pid},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-02-22 21:58:17 +00:00
|
|
|
func (bh *BlankHost) SetStreamHandlerMatch(pid protocol.ID, m func(protocol.ID) bool, handler network.StreamHandler) {
|
|
|
|
bh.Mux().AddHandlerWithFunc(pid, m, func(p protocol.ID, rwc io.ReadWriteCloser) error {
|
2021-06-16 20:19:45 +00:00
|
|
|
is := rwc.(network.Stream)
|
2023-02-22 21:58:17 +00:00
|
|
|
is.SetProtocol(p)
|
2021-06-16 20:19:45 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-02-22 21:58:17 +00:00
|
|
|
s.SetProtocol(protoID)
|
2021-06-16 20:19:45 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|