forked from libp2p/go-libp2p
-
Notifications
You must be signed in to change notification settings - Fork 0
/
blank.go
232 lines (191 loc) · 5.81 KB
/
blank.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package blankhost
import (
"context"
"errors"
"fmt"
"io"
"github.com/zbliujia/go-libp2p/core/connmgr"
"github.com/zbliujia/go-libp2p/core/event"
"github.com/zbliujia/go-libp2p/core/host"
"github.com/zbliujia/go-libp2p/core/network"
"github.com/zbliujia/go-libp2p/core/peer"
"github.com/zbliujia/go-libp2p/core/peerstore"
"github.com/zbliujia/go-libp2p/core/protocol"
"github.com/zbliujia/go-libp2p/core/record"
"github.com/zbliujia/go-libp2p/p2p/host/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[protocol.ID]
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[protocol.ID](),
}
if bh.eventbus == nil {
bh.eventbus = eventbus.NewBus(eventbus.WithMetricsTracer(eventbus.NewMetricsTracer()))
}
// 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")
}
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)
if err != nil {
return fmt.Errorf("failed to dial: %w", err)
}
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, fmt.Errorf("failed to open stream: %w", err)
}
selected, err := mstream.SelectOneOf(protos, s)
if err != nil {
s.Reset()
return nil, fmt.Errorf("failed to negotiate protocol: %w", err)
}
s.SetProtocol(selected)
bh.Peerstore().AddProtocols(p, selected)
return s, nil
}
func (bh *BlankHost) RemoveStreamHandler(pid protocol.ID) {
bh.Mux().RemoveHandler(pid)
bh.emitters.evtLocalProtocolsUpdated.Emit(event.EvtLocalProtocolsUpdated{
Removed: []protocol.ID{pid},
})
}
func (bh *BlankHost) SetStreamHandler(pid protocol.ID, handler network.StreamHandler) {
bh.Mux().AddHandler(pid, func(p protocol.ID, rwc io.ReadWriteCloser) error {
is := rwc.(network.Stream)
is.SetProtocol(p)
handler(is)
return nil
})
bh.emitters.evtLocalProtocolsUpdated.Emit(event.EvtLocalProtocolsUpdated{
Added: []protocol.ID{pid},
})
}
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 {
is := rwc.(network.Stream)
is.SetProtocol(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(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
}