forked from cometbft/cometbft
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpeer.go
500 lines (422 loc) · 11.6 KB
/
peer.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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
package p2p
import (
"fmt"
"net"
"reflect"
"time"
"github.com/cosmos/gogoproto/proto"
"github.com/cometbft/cometbft/internal/cmap"
"github.com/cometbft/cometbft/libs/log"
"github.com/cometbft/cometbft/libs/service"
na "github.com/cometbft/cometbft/p2p/netaddr"
ni "github.com/cometbft/cometbft/p2p/nodeinfo"
"github.com/cometbft/cometbft/p2p/nodekey"
tcpconn "github.com/cometbft/cometbft/p2p/transport/tcp/conn"
"github.com/cometbft/cometbft/types"
)
//go:generate ../scripts/mockery_generate.sh Peer
// Same as the default Prometheus scrape interval in order to not lose
// granularity.
const metricsTickerDuration = 1 * time.Second
// Peer is an interface representing a peer connected on a reactor.
type Peer interface {
service.Service
FlushStop()
ID() nodekey.ID // peer's cryptographic ID
RemoteIP() net.IP // remote IP of the connection
RemoteAddr() net.Addr // remote address of the connection
IsOutbound() bool // did we dial the peer
IsPersistent() bool // do we redial this peer when we disconnect
// Conn returns the underlying connection.
Conn() net.Conn
NodeInfo() ni.NodeInfo // peer's info
Status() tcpconn.ConnectionStatus
SocketAddr() *na.NetAddr // actual address of the socket
HasChannel(chID byte) bool // Does the peer implement this channel?
Send(e Envelope) bool // Send a message to the peer, blocking version
TrySend(e Envelope) bool // Send a message to the peer, non-blocking version
Set(key string, value any)
Get(key string) any
SetRemovalFailed()
GetRemovalFailed() bool
}
// ----------------------------------------------------------
// peerConn contains the raw connection and its config.
type peerConn struct {
outbound bool
persistent bool
conn net.Conn // Source connection
socketAddr *na.NetAddr
// cached RemoteIP()
ip net.IP
}
func newPeerConn(
outbound, persistent bool,
conn net.Conn,
socketAddr *na.NetAddr,
) peerConn {
return peerConn{
outbound: outbound,
persistent: persistent,
conn: conn,
socketAddr: socketAddr,
}
}
// ID returns the peer's ID.
//
// Only used in tests.
func (pc peerConn) ID() nodekey.ID {
return pc.socketAddr.ID
}
// Return the IP from the connection RemoteAddr.
func (pc peerConn) RemoteIP() net.IP {
if pc.ip != nil {
return pc.ip
}
host, _, err := net.SplitHostPort(pc.conn.RemoteAddr().String())
if err != nil {
panic(err)
}
ips, err := net.LookupIP(host)
if err != nil {
panic(err)
}
pc.ip = ips[0]
return pc.ip
}
// peer implements Peer.
//
// Before using a peer, you will need to perform a handshake on connection.
type peer struct {
service.BaseService
// raw peerConn and the multiplex connection
peerConn
mconn *tcpconn.MConnection
// peer's node info and the channel it knows about
// channels = nodeInfo.Channels
// cached to avoid copying nodeInfo in HasChannel
nodeInfo ni.NodeInfo
channels []byte
// User data
Data *cmap.CMap
metrics *Metrics
pendingMetrics *peerPendingMetricsCache
// When removal of a peer fails, we set this flag
removalAttemptFailed bool
}
type PeerOption func(*peer)
func newPeer(
pc peerConn,
mConfig tcpconn.MConnConfig,
nodeInfo ni.NodeInfo,
reactorsByCh map[byte]Reactor,
msgTypeByChID map[byte]proto.Message,
streams []StreamDescriptor,
onPeerError func(Peer, any),
options ...PeerOption,
) *peer {
p := &peer{
peerConn: pc,
nodeInfo: nodeInfo,
channels: nodeInfo.(ni.Default).Channels,
Data: cmap.NewCMap(),
metrics: NopMetrics(),
pendingMetrics: newPeerPendingMetricsCache(),
}
// TODO: rip this out from the peer
p.mconn = createMConnection(
pc.conn,
p,
reactorsByCh,
msgTypeByChID,
streams,
onPeerError,
mConfig,
)
p.BaseService = *service.NewBaseService(nil, "Peer", p)
for _, option := range options {
option(p)
}
return p
}
// String representation.
func (p *peer) String() string {
if p.outbound {
return fmt.Sprintf("Peer{%v %v out}", p.mconn, p.ID())
}
return fmt.Sprintf("Peer{%v %v in}", p.mconn, p.ID())
}
// ---------------------------------------------------
// Implements service.Service
// SetLogger implements BaseService.
func (p *peer) SetLogger(l log.Logger) {
p.Logger = l
p.mconn.SetLogger(l)
}
// OnStart implements BaseService.
func (p *peer) OnStart() error {
if err := p.BaseService.OnStart(); err != nil {
return err
}
if err := p.mconn.Start(); err != nil {
return err
}
go p.metricsReporter()
return nil
}
// FlushStop mimics OnStop but additionally ensures that all successful
// .Send() calls will get flushed before closing the connection.
//
// NOTE: it is not safe to call this method more than once.
func (p *peer) FlushStop() {
p.mconn.FlushStop() // stop everything and close the conn
}
// OnStop implements BaseService.
func (p *peer) OnStop() {
if err := p.mconn.Stop(); err != nil { // stop everything and close the conn
p.Logger.Debug("Error while stopping peer", "err", err)
}
}
// ---------------------------------------------------
// Implements Peer
// ID returns the peer's ID - the hex encoded hash of its pubkey.
func (p *peer) ID() nodekey.ID {
return p.nodeInfo.ID()
}
// IsOutbound returns true if the connection is outbound, false otherwise.
func (p *peer) IsOutbound() bool {
return p.peerConn.outbound
}
// IsPersistent returns true if the peer is persistent, false otherwise.
func (p *peer) IsPersistent() bool {
return p.peerConn.persistent
}
// NodeInfo returns a copy of the peer's NodeInfo.
func (p *peer) NodeInfo() ni.NodeInfo {
return p.nodeInfo
}
// SocketAddr returns the address of the socket.
// For outbound peers, it's the address dialed (after DNS resolution).
// For inbound peers, it's the address returned by the underlying connection
// (not what's reported in the peer's NodeInfo).
func (p *peer) SocketAddr() *na.NetAddr {
return p.peerConn.socketAddr
}
// Status returns the peer's ConnectionStatus.
func (p *peer) Status() tcpconn.ConnectionStatus {
return p.mconn.Status()
}
// Send msg bytes to the channel identified by chID byte. Returns false if the
// send queue is full after timeout, specified by MConnection.
//
// thread safe.
func (p *peer) Send(e Envelope) bool {
return p.send(e.ChannelID, e.Message, p.mconn.Send)
}
// TrySend msg bytes to the channel identified by chID byte. Immediately returns
// false if the send queue is full.
//
// thread safe.
func (p *peer) TrySend(e Envelope) bool {
return p.send(e.ChannelID, e.Message, p.mconn.TrySend)
}
func (p *peer) send(chID byte, msg proto.Message, sendFunc func(byte, []byte) bool) bool {
if !p.IsRunning() {
return false
} else if !p.HasChannel(chID) {
return false
}
msgType := getMsgType(msg)
if w, ok := msg.(types.Wrapper); ok {
msg = w.Wrap()
}
msgBytes, err := proto.Marshal(msg)
if err != nil {
p.Logger.Error("marshaling message to send", "error", err)
return false
}
res := sendFunc(chID, msgBytes)
if res {
p.pendingMetrics.AddPendingSendBytes(msgType, len(msgBytes))
}
return res
}
// Get the data for a given key.
//
// thread safe.
func (p *peer) Get(key string) any {
return p.Data.Get(key)
}
// Set sets the data for the given key.
//
// thread safe.
func (p *peer) Set(key string, data any) {
p.Data.Set(key, data)
}
// HasChannel returns whether the peer reported implementing this channel.
func (p *peer) HasChannel(chID byte) bool {
for _, ch := range p.channels {
if ch == chID {
return true
}
}
return false
}
// Conn returns the underlying peer source connection.
func (p *peer) Conn() net.Conn {
return p.peerConn.conn
}
func (p *peer) SetRemovalFailed() {
p.removalAttemptFailed = true
}
func (p *peer) GetRemovalFailed() bool {
return p.removalAttemptFailed
}
// ---------------------------------------------------
// methods only used for testing
// TODO: can we remove these?
// RemoteAddr returns peer's remote network address.
func (p *peer) RemoteAddr() net.Addr {
return p.peerConn.conn.RemoteAddr()
}
// CanSend returns true if the send queue is not full, false otherwise.
func (p *peer) CanSend(chID byte) bool {
if !p.IsRunning() {
return false
}
return p.mconn.CanSend(chID)
}
// ---------------------------------------------------
func PeerMetrics(metrics *Metrics) PeerOption {
return func(p *peer) {
p.metrics = metrics
}
}
func (p *peer) metricsReporter() {
metricsTicker := time.NewTicker(metricsTickerDuration)
defer metricsTicker.Stop()
for {
select {
case <-metricsTicker.C:
status := p.mconn.Status()
var sendQueueSize float64
for _, chStatus := range status.Channels {
sendQueueSize += float64(chStatus.SendQueueSize)
}
p.metrics.RecvRateLimiterDelay.With("peer_id", string(p.ID())).
Add(status.RecvMonitor.SleepTime.Seconds())
p.metrics.SendRateLimiterDelay.With("peer_id", string(p.ID())).
Add(status.SendMonitor.SleepTime.Seconds())
p.metrics.PeerPendingSendBytes.With("peer_id", string(p.ID())).Set(sendQueueSize)
// Report per peer, per message total bytes, since the last interval
func() {
p.pendingMetrics.mtx.Lock()
defer p.pendingMetrics.mtx.Unlock()
for _, entry := range p.pendingMetrics.perMessageCache {
if entry.pendingSendBytes > 0 {
p.metrics.MessageSendBytesTotal.
With("message_type", entry.label).
Add(float64(entry.pendingSendBytes))
entry.pendingSendBytes = 0
}
if entry.pendingRecvBytes > 0 {
p.metrics.MessageReceiveBytesTotal.
With("message_type", entry.label).
Add(float64(entry.pendingRecvBytes))
entry.pendingRecvBytes = 0
}
}
}()
case <-p.Quit():
return
}
}
}
// ------------------------------------------------------------------
// helper funcs
func createMConnection(
conn net.Conn,
p *peer,
reactorsByCh map[byte]Reactor,
msgTypeByChID map[byte]proto.Message,
streamDescs []StreamDescriptor,
onPeerError func(Peer, any),
config tcpconn.MConnConfig,
) *tcpconn.MConnection {
onReceive := func(chID byte, msgBytes []byte) {
reactor := reactorsByCh[chID]
if reactor == nil {
// Note that its ok to panic here as it's caught in the conn._recover,
// which does onPeerError.
panic(fmt.Sprintf("Unknown channel %X", chID))
}
mt := msgTypeByChID[chID]
msg := proto.Clone(mt)
err := proto.Unmarshal(msgBytes, msg)
if err != nil {
panic(fmt.Sprintf("unmarshaling message: %v into type: %s", err, reflect.TypeOf(mt)))
}
if w, ok := msg.(types.Unwrapper); ok {
msg, err = w.Unwrap()
if err != nil {
panic(fmt.Sprintf("unwrapping message: %v", err))
}
}
p.pendingMetrics.AddPendingRecvBytes(getMsgType(msg), len(msgBytes))
reactor.Receive(Envelope{
ChannelID: chID,
Src: p,
Message: msg,
})
}
onError := func(r any) {
onPeerError(p, r)
}
// filter out non-tcpconn.ChannelDescriptor streams
tcpDescs := make([]*tcpconn.ChannelDescriptor, 0, len(streamDescs))
for _, stream := range streamDescs {
var ok bool
d, ok := stream.(*tcpconn.ChannelDescriptor)
if !ok {
continue
}
tcpDescs = append(tcpDescs, d)
}
return tcpconn.NewMConnectionWithConfig(
conn,
tcpDescs,
onReceive,
onError,
config,
)
}
func wrapPeer(c net.Conn, ni ni.NodeInfo, cfg peerConfig, socketAddr *na.NetAddr, mConfig tcpconn.MConnConfig) Peer {
persistent := false
if cfg.isPersistent != nil {
if cfg.outbound {
persistent = cfg.isPersistent(socketAddr)
} else {
selfReportedAddr, err := ni.NetAddr()
if err == nil {
persistent = cfg.isPersistent(selfReportedAddr)
}
}
}
peerConn := newPeerConn(
cfg.outbound,
persistent,
c,
socketAddr,
)
p := newPeer(
peerConn,
mConfig,
ni,
cfg.reactorsByCh,
cfg.msgTypeByChID,
cfg.streamDescs,
cfg.onPeerError,
PeerMetrics(cfg.metrics),
)
return p
}