-
Notifications
You must be signed in to change notification settings - Fork 107
/
p2p.go
416 lines (351 loc) · 11.5 KB
/
p2p.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
// Package p2p implements the worker committee gossip network.
package p2p
import (
"context"
"fmt"
"net"
"sync"
"time"
"github.com/libp2p/go-libp2p"
core "github.com/libp2p/go-libp2p-core"
pubsub "github.com/libp2p/go-libp2p-pubsub"
pb "github.com/libp2p/go-libp2p-pubsub/pb"
"github.com/libp2p/go-libp2p/p2p/net/conngater"
"github.com/libp2p/go-libp2p/p2p/net/connmgr"
"github.com/multiformats/go-multiaddr"
manet "github.com/multiformats/go-multiaddr/net"
"github.com/spf13/viper"
"github.com/oasisprotocol/oasis-core/go/common"
"github.com/oasisprotocol/oasis-core/go/common/cbor"
"github.com/oasisprotocol/oasis-core/go/common/crypto/tuplehash"
"github.com/oasisprotocol/oasis-core/go/common/identity"
"github.com/oasisprotocol/oasis-core/go/common/logging"
"github.com/oasisprotocol/oasis-core/go/common/node"
"github.com/oasisprotocol/oasis-core/go/common/version"
consensus "github.com/oasisprotocol/oasis-core/go/consensus/api"
registryAPI "github.com/oasisprotocol/oasis-core/go/registry/api"
"github.com/oasisprotocol/oasis-core/go/worker/common/configparser"
"github.com/oasisprotocol/oasis-core/go/worker/common/p2p/rpc"
)
// peersHighWatermarkDelta specifies how many peers after the maximum peer count is reached we ask
// the connection manager to start pruning peers.
const peersHighWatermarkDelta = 30
// messageIdContext is the domain separation context for computing message identifier hashes.
var messageIdContext = []byte("oasis-core/p2p: message id")
// TopicKind is the gossipsub topic kind.
type TopicKind string
const (
// TopicKindCommittee is the topic kind for the topic that is used to gossip batch proposals
// and other committee messages.
TopicKindCommittee TopicKind = "committee"
// TopicKindTx is the topic kind for the topic that is used to gossip transactions.
TopicKindTx TopicKind = "tx"
)
var allowUnroutableAddresses bool
// DebugForceAllowUnroutableAddresses allows unroutable addresses.
func DebugForceAllowUnroutableAddresses() {
allowUnroutableAddresses = true
}
// P2P is a peer-to-peer node using libp2p.
type P2P struct {
sync.RWMutex
*PeerManager
ctxCancel context.CancelFunc
quitCh chan struct{}
chainContext string
host core.Host
pubsub *pubsub.PubSub
registerAddresses []multiaddr.Multiaddr
topics map[common.Namespace]map[TopicKind]*topicHandler
logger *logging.Logger
}
// Cleanup performs the service specific post-termination cleanup.
func (p *P2P) Cleanup() {
}
// Name returns the service name.
func (p *P2P) Name() string {
return "worker p2p"
}
// Start starts the service.
func (p *P2P) Start() error {
// Unfortunately libp2p starts everything on construction.
return nil
}
// Stop halts the service.
func (p *P2P) Stop() {
p.ctxCancel()
_ = p.host.Close() // This blocks until the host stops.
close(p.quitCh)
}
// Quit returns a channel that will be closed when the service terminates.
func (p *P2P) Quit() <-chan struct{} {
return p.quitCh
}
// Addresses returns the P2P addresses of the node.
func (p *P2P) Addresses() []node.Address {
if p == nil {
return nil
}
var addrs []multiaddr.Multiaddr
if len(p.registerAddresses) == 0 {
addrs = p.host.Addrs()
} else {
addrs = p.registerAddresses
}
allowUnroutable := allowUnroutableAddresses
var addresses []node.Address
for _, v := range addrs {
netAddr, err := manet.ToNetAddr(v)
if err != nil {
panic(err)
}
tcpAddr := (netAddr).(*net.TCPAddr)
nodeAddr := node.Address{TCPAddr: *tcpAddr}
if err := registryAPI.VerifyAddress(nodeAddr, allowUnroutable); err != nil {
continue
}
addresses = append(addresses, nodeAddr)
}
return addresses
}
// Peers returns a list of connected P2P peers for the given runtime.
func (p *P2P) Peers(runtimeID common.Namespace) []string {
allPeers := p.pubsub.ListPeers(p.topicIDForRuntime(runtimeID, TopicKindCommittee))
allPeers = append(allPeers, p.pubsub.ListPeers(p.topicIDForRuntime(runtimeID, TopicKindTx))...)
var peers []string
peerMap := make(map[core.PeerID]bool)
for _, peerID := range allPeers {
if peerMap[peerID] {
continue
}
peerMap[peerID] = true
addrs := p.host.Peerstore().Addrs(peerID)
if len(addrs) == 0 {
continue
}
if reachableAddrs := filterGloballyReachableAddresses(addrs); len(reachableAddrs) > 0 {
addrs = reachableAddrs
}
peers = append(peers, fmt.Sprintf("%s/p2p/%s", addrs[0].String(), peerID.Pretty()))
}
return peers
}
func filterGloballyReachableAddresses(addrs []multiaddr.Multiaddr) []multiaddr.Multiaddr {
ret := make([]multiaddr.Multiaddr, 0, len(addrs))
for _, addr := range addrs {
// Ugh, this multiaddr stuff is extremely obnoxious to work with.
addrStr, err := addr.ValueForProtocol(multiaddr.P_IP4)
if err != nil {
addrStr, err = addr.ValueForProtocol(multiaddr.P_IP6)
if err != nil {
continue
}
}
ip := net.ParseIP(addrStr)
if ip == nil {
continue
}
if !common.IsProbablyGloballyReachable(ip) {
continue
}
// I have no idea if multiaddr.Multiaddr copies correctly.
addrCopy, err := multiaddr.NewMultiaddr(addr.String())
if err != nil {
continue
}
ret = append(ret, addrCopy)
}
return ret
}
func (p *P2P) publish(ctx context.Context, runtimeID common.Namespace, kind TopicKind, msg interface{}) {
rawMsg := cbor.Marshal(msg)
p.RLock()
defer p.RUnlock()
topics := p.topics[runtimeID]
if topics == nil {
p.logger.Error("attempted to publish message for unknown runtime ID",
"runtime_id", runtimeID,
"kind", kind,
)
return
}
h := topics[kind]
if h == nil {
p.logger.Error("attempted to publish message for unsupported topic kind",
"runtime_id", runtimeID,
"kind", kind,
)
return
}
if err := h.tryPublishing(rawMsg); err != nil {
h.logger.Error("failed to publish message to the network",
"err", err,
)
}
p.logger.Debug("published message",
"runtime_id", runtimeID,
"kind", kind,
)
}
// PublishCommittee publishes a committee message.
func (p *P2P) PublishCommittee(ctx context.Context, runtimeID common.Namespace, msg *CommitteeMessage) {
p.publish(ctx, runtimeID, TopicKindCommittee, msg)
}
// PublishCommittee publishes a transaction message.
func (p *P2P) PublishTx(ctx context.Context, runtimeID common.Namespace, msg TxMessage) {
p.publish(ctx, runtimeID, TopicKindTx, msg)
}
// RegisterHandler registers a message handler for the specified runtime and topic kind.
func (p *P2P) RegisterHandler(runtimeID common.Namespace, kind TopicKind, handler Handler) {
p.Lock()
defer p.Unlock()
topics := p.topics[runtimeID]
if topics == nil {
topics = make(map[TopicKind]*topicHandler)
p.topics[runtimeID] = topics
}
if topics[kind] != nil {
panic(fmt.Sprintf("worker/common/p2p: handler for topic kind '%s' already registered", kind))
}
topicID, h, err := newTopicHandler(p, runtimeID, kind, handler)
if err != nil {
panic(fmt.Sprintf("worker/common/p2p: failed to initialize topic handler: %s", err))
}
topics[kind] = h
_ = p.pubsub.RegisterTopicValidator(
topicID,
h.topicMessageValidator,
pubsub.WithValidatorConcurrency(viper.GetInt(CfgP2PValidateConcurrency)),
)
p.logger.Debug("registered new topic handler",
"runtime_id", runtimeID,
"kind", kind,
)
}
func (p *P2P) topicIDForRuntime(runtimeID common.Namespace, kind TopicKind) string {
return fmt.Sprintf("%s/%d/%s/%s",
p.chainContext,
version.RuntimeCommitteeProtocol.Major,
runtimeID.String(),
kind,
)
}
// BlockPeer blocks a specific peer from being used by the local node.
func (p *P2P) BlockPeer(peerID core.PeerID) {
p.logger.Warn("blocking peer",
"peer_id", peerID,
)
p.pubsub.BlacklistPeer(peerID)
p.PeerManager.blockPeer(peerID)
}
// GetHost returns the P2P host.
func (p *P2P) GetHost() core.Host {
return p.host
}
// RegisterProtocolServer registers a protocol server for the given protocol.
func (p *P2P) RegisterProtocolServer(srv rpc.Server) {
p.host.SetStreamHandler(srv.Protocol(), srv.HandleStream)
p.logger.Info("registered protocol server",
"protocol_id", srv.Protocol(),
)
}
// GetMinRepublishInterval returns the minimum republish interval that needs to be respected by
// the caller when publishing the same message. If Publish is called for the same message more
// quickly, the message may be dropped and not published.
func (p *P2P) GetMinRepublishInterval() time.Duration {
return pubsub.TimeCacheDuration + 5*time.Second
}
func messageIdFn(pmsg *pb.Message) string {
// id := TupleHash[messageIdContext](topic, data)
h := tuplehash.New256(32, messageIdContext)
_, _ = h.Write([]byte(pmsg.GetTopic()))
_, _ = h.Write(pmsg.Data)
return string(h.Sum(nil))
}
// New creates a new P2P node.
func New(identity *identity.Identity, consensus consensus.Backend) (*P2P, error) {
// Instantiate the libp2p host.
addresses, err := configparser.ParseAddressList(viper.GetStringSlice(cfgP2pAddresses))
if err != nil {
return nil, err
}
port := uint16(viper.GetInt(CfgP2pPort))
var registerAddresses []multiaddr.Multiaddr
for _, addr := range addresses {
var mAddr multiaddr.Multiaddr
mAddr, err = manet.FromNetAddr(&addr.TCPAddr)
if err != nil {
return nil, err
}
registerAddresses = append(registerAddresses, mAddr)
}
sourceMultiAddr, _ := multiaddr.NewMultiaddr(
fmt.Sprintf("/ip4/0.0.0.0/tcp/%d", port),
)
// Set up a connection manager so we can limit the number of connections.
low := int(viper.GetUint32(CfgP2PMaxNumPeers))
cm, err := connmgr.NewConnManager(
low,
low+peersHighWatermarkDelta,
connmgr.WithGracePeriod(viper.GetDuration(CfgP2PPeerGracePeriod)),
)
if err != nil {
return nil, fmt.Errorf("worker/common/p2p: failed to create connection manager: %w", err)
}
// Set up a connection gater.
cg, err := conngater.NewBasicConnectionGater(nil)
if err != nil {
return nil, fmt.Errorf("worker/common/p2p: failed to create connection gater: %w", err)
}
// Create the P2P host.
host, err := libp2p.New(
libp2p.UserAgent(fmt.Sprintf("oasis-core/%s", version.SoftwareVersion)),
libp2p.ListenAddrs(sourceMultiAddr),
libp2p.Identity(signerToPrivKey(identity.P2PSigner)),
libp2p.ConnectionManager(cm),
libp2p.ConnectionGater(cg),
)
if err != nil {
return nil, fmt.Errorf("worker/common/p2p: failed to initialize libp2p host: %w", err)
}
// Initialize the gossipsub router.
ctx, ctxCancel := context.WithCancel(context.Background())
pubsub, err := pubsub.NewGossipSub(
ctx,
host,
pubsub.WithMessageSigning(true),
pubsub.WithStrictSignatureVerification(true),
pubsub.WithFloodPublish(true),
pubsub.WithPeerExchange(true),
pubsub.WithPeerOutboundQueueSize(viper.GetInt(CfgP2PPeerOutboundQueueSize)),
pubsub.WithValidateQueueSize(viper.GetInt(CfgP2PValidateQueueSize)),
pubsub.WithValidateThrottle(viper.GetInt(CfgP2PValidateThrottle)),
pubsub.WithMessageIdFn(messageIdFn),
)
if err != nil {
ctxCancel()
_ = host.Close()
return nil, fmt.Errorf("worker/common/p2p: failed to initialize libp2p gossipsub: %w", err)
}
chainContext, err := consensus.GetChainContext(ctx)
if err != nil {
ctxCancel()
_ = host.Close()
return nil, fmt.Errorf("worker/common/p2p: failed to get consensus chain context: %w", err)
}
p := &P2P{
PeerManager: newPeerManager(ctx, host, cg, consensus),
ctxCancel: ctxCancel,
quitCh: make(chan struct{}),
chainContext: chainContext,
host: host,
pubsub: pubsub,
registerAddresses: registerAddresses,
topics: make(map[common.Namespace]map[TopicKind]*topicHandler),
logger: logging.GetLogger("worker/common/p2p"),
}
p.logger.Info("p2p host initialized",
"address", fmt.Sprintf("%+v", host.Addrs()),
)
return p, nil
}