/
p2p.go
290 lines (248 loc) · 7.73 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
// 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"
"github.com/libp2p/go-libp2p-core/network"
"github.com/libp2p/go-libp2p-core/transport"
pubsub "github.com/libp2p/go-libp2p-pubsub"
pb "github.com/libp2p/go-libp2p-pubsub/pb"
"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/hash"
"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"
)
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
ctx context.Context
chainContext string
host core.Host
pubsub *pubsub.PubSub
registerAddresses []multiaddr.Multiaddr
topics map[common.Namespace]*topicHandler
logger *logging.Logger
}
// 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 {
var peers []string
for _, peerID := range p.pubsub.ListPeers(p.topicIDForRuntime(runtimeID)) {
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
}
// Publish publishes a message to the gossip network.
func (p *P2P) Publish(ctx context.Context, runtimeID common.Namespace, msg *Message) {
rawMsg := cbor.Marshal(msg)
p.RLock()
defer p.RUnlock()
h := p.topics[runtimeID]
if h == nil {
p.logger.Error("attempted to publish message for unknown runtime ID",
"runtime_id", runtimeID,
)
return
}
if err := h.tryPublishing(rawMsg); err != nil {
h.logger.Error("failed to publish message to the network",
"err", err,
)
}
}
// RegisterHandler registers a message handler for the specified runtime.
// If multiple handlers are registered for the same runtime, each of the
// handlers will get invoked.
func (p *P2P) RegisterHandler(runtimeID common.Namespace, handler Handler) {
p.Lock()
defer p.Unlock()
topic := p.topics[runtimeID]
switch topic {
case nil:
// New topic.
topicID, h, err := newTopicHandler(p, runtimeID, []Handler{handler})
if err != nil {
panic(fmt.Sprintf("worker/common/p2p: failed to initialize topic handler: %s", err))
}
p.topics[runtimeID] = h
_ = p.pubsub.RegisterTopicValidator(
topicID,
h.topicMessageValidator,
pubsub.WithValidatorConcurrency(viper.GetInt(CfgP2PValidateConcurrency)),
)
default:
topic.handlersLock.Lock()
defer topic.handlersLock.Unlock()
// Existing topic, add handler.
topic.handlers = append(topic.handlers, handler)
}
}
func (p *P2P) handleConnection(conn core.Conn) {
if conn.Stat().Direction != network.DirInbound {
return
}
p.logger.Debug("new connection from peer",
"peer_id", conn.RemotePeer(),
)
}
func (p *P2P) topicIDForRuntime(runtimeID common.Namespace) string {
return fmt.Sprintf("%s/%d/%s",
p.chainContext,
version.RuntimeCommitteeProtocol.Major,
runtimeID.String(),
)
}
// New creates a new P2P node.
func New(ctx context.Context, 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),
)
// Oh hey, they finally got around to fixing the NAT traversal code,
// so if people feel brave enough to want to interact with the
// mountain of terrible uPNP/NAT-PMP implementations out there,
// they can.
host, err := libp2p.New(
ctx,
libp2p.ListenAddrs(sourceMultiAddr),
libp2p.Identity(signerToPrivKey(identity.P2PSigner)),
)
if err != nil {
return nil, fmt.Errorf("worker/common/p2p: failed to initialize libp2p host: %w", err)
}
// Initialize the gossipsub router.
pubsub, err := pubsub.NewGossipSub(
ctx,
host,
pubsub.WithMessageSigning(true),
pubsub.WithStrictSignatureVerification(true),
pubsub.WithFloodPublish(true),
pubsub.WithPeerOutboundQueueSize(viper.GetInt(CfgP2PPeerOutboundQueueSize)),
pubsub.WithValidateQueueSize(viper.GetInt(CfgP2PValidateQueueSize)),
pubsub.WithValidateThrottle(viper.GetInt(CfgP2PValidateThrottle)),
pubsub.WithMessageIdFn(func(pmsg *pb.Message) string {
h := hash.NewFromBytes(pmsg.Data)
return string(h[:])
}),
)
if err != nil {
return nil, fmt.Errorf("worker/common/p2p: failed to initialize libp2p gossipsub: %w", err)
}
doc, err := consensus.GetGenesisDocument(ctx)
if err != nil {
return nil, fmt.Errorf("worker/common/p2p: failed to get consensus genesis document: %w", err)
}
p := &P2P{
PeerManager: newPeerManager(ctx, host, consensus),
ctx: ctx,
chainContext: doc.ChainContext(),
host: host,
pubsub: pubsub,
registerAddresses: registerAddresses,
topics: make(map[common.Namespace]*topicHandler),
logger: logging.GetLogger("worker/common/p2p"),
}
p.host.Network().SetConnHandler(p.handleConnection)
p.logger.Info("p2p host initialized",
"address", fmt.Sprintf("%+v", host.Addrs()),
)
return p, nil
}
func init() {
// Make sure to decrease (global!) transport timeouts.
transport.AcceptTimeout = 5 * time.Second
}