forked from thetatoken/theta-protocol-ledger
/
connection.go
588 lines (500 loc) · 14.7 KB
/
connection.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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
package connection
import (
"bufio"
"context"
"fmt"
"io"
"net"
"runtime/debug"
"sync"
"sync/atomic"
"time"
log "github.com/sirupsen/logrus"
"github.com/thetatoken/theta/common"
"github.com/thetatoken/theta/common/timer"
"github.com/thetatoken/theta/p2p/connection/flowrate"
"github.com/thetatoken/theta/p2p/types"
p2ptypes "github.com/thetatoken/theta/p2p/types"
"github.com/thetatoken/theta/rlp"
)
var logger *log.Entry = log.WithFields(log.Fields{"prefix": "p2p"})
//
// Connection models the connection between the current node and a peer node.
// A connection has a ChannelGroup which can contain multiple Channels
//
type Connection struct {
netconn net.Conn
bufWriter *bufio.Writer
sendMonitor *flowrate.Monitor
bufReader *bufio.Reader
recvMonitor *flowrate.Monitor
bufConn io.ReadWriter
channelGroup ChannelGroup
onParse MessageParser
onEncode MessageEncoder
onReceive ReceiveHandler
onError ErrorHandler
errored uint32
sendPulse chan bool
pongPulse chan bool
quitPulse chan bool
flushTimer *timer.ThrottleTimer // flush writes as necessary but throttled
pingTimer *timer.RepeatTimer // send pings periodically
pendingPings uint32
config ConnectionConfig
rmu, wmu sync.Mutex
rw *rlpxFrameRW
// Life cycle
wg *sync.WaitGroup
quit chan struct{}
ctx context.Context
cancel context.CancelFunc
stopped bool
}
//
// ConnectionConfig specifies the configurations of the Connection
//
type ConnectionConfig struct {
SendRate int64
RecvRate int64
PacketBatchSize int64
FlushThrottle time.Duration
PingTimeout time.Duration
MaxPendingPings uint32
}
// MessageParser parses the raw message bytes to type p2ptypes.Message
type MessageParser func(channelID common.ChannelIDEnum, rawMessageBytes common.Bytes) (p2ptypes.Message, error)
// MessageEncoder encodes type p2ptypes.Message to raw message bytes
type MessageEncoder func(channelID common.ChannelIDEnum, message interface{}) (common.Bytes, error)
var defaultMessageEncoder MessageEncoder = func(channelID common.ChannelIDEnum, message interface{}) (common.Bytes, error) {
return rlp.EncodeToBytes(message)
}
// ReceiveHandler is the callback function to handle received bytes from the given channel
type ReceiveHandler func(message p2ptypes.Message) error
// ErrorHandler is the callback function to handle channel read errors
type ErrorHandler func(interface{})
// CreateConnection creates a Connection instance
func CreateConnection(netconn net.Conn, config ConnectionConfig) *Connection {
if netconn != nil {
logger.Debugf("Create connection, local: %v, remote: %v", netconn.LocalAddr(), netconn.RemoteAddr())
}
channelCheckpoint := createDefaultChannel(common.ChannelIDCheckpoint)
channelHeader := createDefaultChannel(common.ChannelIDHeader)
channelBlock := createDefaultChannel(common.ChannelIDBlock)
channelProposal := createDefaultChannel(common.ChannelIDProposal)
channelVote := createDefaultChannel(common.ChannelIDVote)
channelTransaction := createDefaultChannel(common.ChannelIDTransaction)
channelPeerDiscover := createDefaultChannel(common.ChannelIDPeerDiscovery)
channelPing := createDefaultChannel(common.ChannelIDPing)
channels := []*Channel{
&channelCheckpoint,
&channelHeader,
&channelBlock,
&channelProposal,
&channelVote,
&channelTransaction,
&channelPeerDiscover,
&channelPing,
}
success, channelGroup := createChannelGroup(getDefaultChannelGroupConfig(), channels)
if !success {
return nil
}
conn := &Connection{
netconn: netconn,
bufWriter: bufio.NewWriter(netconn),
sendMonitor: flowrate.New(0, 0),
bufReader: bufio.NewReader(netconn),
recvMonitor: flowrate.New(0, 0),
channelGroup: channelGroup,
sendPulse: make(chan bool, 1),
pongPulse: make(chan bool, 1),
quitPulse: make(chan bool, 1),
flushTimer: timer.NewThrottleTimer("flush", config.FlushThrottle),
pingTimer: timer.NewRepeatTimer("ping", config.PingTimeout),
config: config,
wg: &sync.WaitGroup{},
onEncode: defaultMessageEncoder,
}
conn.bufConn = struct {
io.Reader
io.Writer
}{Reader: conn.bufReader, Writer: conn.netconn}
return conn
}
// GetDefaultConnectionConfig returns the default ConnectionConfig
func GetDefaultConnectionConfig() ConnectionConfig {
return ConnectionConfig{
SendRate: int64(512000), // 500KB/s
RecvRate: int64(512000), // 500KB/s
PacketBatchSize: int64(10),
FlushThrottle: 100 * time.Millisecond,
PingTimeout: 40 * time.Second,
MaxPendingPings: 3,
}
}
// SetPingTimer for testing purpose
func (conn *Connection) SetPingTimer(seconds time.Duration) {
conn.pingTimer = timer.NewRepeatTimer("ping", seconds*time.Second)
conn.pingTimer.Reset()
}
// Start is called when the connection starts
func (conn *Connection) Start(ctx context.Context) bool {
c, cancel := context.WithCancel(ctx)
conn.ctx = c
conn.cancel = cancel
// NOTE: the life cycle of recvRoutine() is not managed by
// the wg since rlp.Decode() is a blocking call
conn.wg.Add(1)
go conn.sendRoutine()
go conn.recvRoutine()
return true
}
// Wait suspends the caller goroutine
func (conn *Connection) Wait() {
conn.wg.Wait()
conn.netconn.Close()
}
// CancelConnection for testing purpose only
func (conn *Connection) CancelConnection() {
conn.cancel()
}
// Stop is called when the connection stops
func (conn *Connection) Stop() {
if conn.GetNetconn() == nil {
return
}
logger.Warnf("Stopping connection, local: %v, remote: %v", conn.GetNetconn().LocalAddr(), conn.GetNetconn().RemoteAddr())
err := conn.netconn.Close()
if err != nil {
logger.Errorf("Failed to close connection: %v", err)
}
if conn.cancel != nil {
conn.cancel()
}
}
// SetMessageParser sets the message parser for the connection
func (conn *Connection) SetMessageParser(messageParser MessageParser) {
conn.onParse = messageParser
}
// SetMessageEncoder sets the message encoder for the connection
func (conn *Connection) SetMessageEncoder(messageEncoder MessageEncoder) {
conn.onEncode = messageEncoder
}
// SetReceiveHandler sets the receive handler for the connection
func (conn *Connection) SetReceiveHandler(receiveHandler ReceiveHandler) {
conn.onReceive = receiveHandler
}
// SetErrorHandler sets the error handler for the connection
func (conn *Connection) SetErrorHandler(errorHandler ErrorHandler) {
conn.onError = errorHandler
}
// EnqueueMessage enqueues the given message to the target channel.
// The message will be sent out later
func (conn *Connection) EnqueueMessage(channelID common.ChannelIDEnum, message interface{}) bool {
channel := conn.channelGroup.getChannel(channelID)
if channel == nil {
logger.Errorf("Failed to get channel for ID: %v", channelID)
return false
}
msgBytes, err := conn.onEncode(channelID, message)
if err != nil {
logger.Errorf("Failed to encode message to bytes: %v, err: %v", message, err)
return false
}
success := channel.enqueueMessage(msgBytes)
if success {
conn.scheduleSendPulse()
}
return success
}
// AttemptToEnqueueMessage attempts to enqueue the given message to the
// target channel. The message will be sent out later (non-blocking)
func (conn *Connection) AttemptToEnqueueMessage(channelID common.ChannelIDEnum, message interface{}) bool {
channel := conn.channelGroup.getChannel(channelID)
if channel == nil {
logger.Errorf("Failed to get channel for ID: %v", channelID)
return false
}
msgBytes, err := conn.onEncode(channelID, message)
if err != nil {
logger.Errorf("Failed to encode message to bytes: %v, error: %v", message, err)
return false
}
success := channel.attemptToEnqueueMessage(msgBytes)
if success {
conn.scheduleSendPulse()
}
return success
}
// CanEnqueueMessage returns whether more messages can still be enqueued
// into the connection at the moment
func (conn *Connection) CanEnqueueMessage(channelID common.ChannelIDEnum) bool {
channel := conn.channelGroup.getChannel(channelID)
if channel == nil {
return false
}
return channel.canEnqueueMessage()
}
// --------------------- Send goroutine --------------------- //
func (conn *Connection) sendRoutine() {
defer conn.wg.Done()
defer conn.recover()
for {
var err error
select {
case <-conn.ctx.Done():
conn.wmu.Lock()
conn.stopped = true
conn.wmu.Unlock()
return
case <-conn.flushTimer.Ch:
conn.flush()
case <-conn.pingTimer.Ch:
err = conn.sendPingSignal()
case <-conn.pongPulse:
err = conn.sendPongSignal()
case <-conn.sendPulse:
conn.sendPacketBatchAndScheduleSendPulse()
case <-conn.quitPulse:
return
}
if err != nil {
logger.Errorf("sendRoutine error: %v", err)
conn.stopForError(err)
return
}
}
}
func (conn *Connection) sendPingSignal() error {
if atomic.LoadUint32(&conn.pendingPings) >= conn.config.MaxPendingPings {
//conn.onError(nil)
conn.stopForError(nil)
logger.Errorf("Peer not responding to ping %v", conn.netconn.RemoteAddr())
return fmt.Errorf("Peer not responding to ping %v", conn.netconn.RemoteAddr())
}
pingPacket := &Packet{
ChannelID: common.ChannelIDPing,
Bytes: []byte{p2ptypes.PingSignal},
IsEOF: byte(0x01),
}
err := conn.writePacket(pingPacket)
if err != nil {
return err
}
conn.sendMonitor.Update(int(1))
conn.flush()
atomic.AddUint32(&conn.pendingPings, 1)
return nil
}
func (conn *Connection) sendPongSignal() error {
pongPacket := &Packet{
ChannelID: common.ChannelIDPing,
Bytes: []byte{p2ptypes.PongSignal},
IsEOF: byte(0x01),
}
err := conn.writePacket(pongPacket)
if err != nil {
return err
}
conn.sendMonitor.Update(int(1))
conn.flush()
return nil
}
func (conn *Connection) sendPacketBatchAndScheduleSendPulse() {
success, dataExhausted := conn.sendPacketBatch()
if !success || !dataExhausted {
conn.scheduleSendPulse()
}
}
// --------------------- Recv goroutine --------------------- //
func (conn *Connection) readPacket() (*Packet, error) {
// Plaintext transport.
if conn.rw == nil {
packet := &Packet{}
s := rlp.NewStream(conn.bufReader, maxPayloadSize*1024)
err := s.Decode(packet)
return packet, err
}
// Encrypted transport.
conn.rmu.Lock()
defer conn.rmu.Unlock()
return conn.rw.ReadPacket()
}
func (conn *Connection) writePacket(packet *Packet) error {
// Plaintext transport.
if conn.rw == nil {
return rlp.Encode(conn.bufWriter, packet)
}
// Encrypted transport.
conn.wmu.Lock()
defer conn.wmu.Unlock()
return conn.rw.WritePacket(packet)
}
func (conn *Connection) recvRoutine() {
//defer conn.wg.Done() // NOTE: rlp.Decode() is a blocking call
defer conn.recover()
for {
select {
case <-conn.ctx.Done():
conn.wmu.Lock()
conn.stopped = true
conn.wmu.Unlock()
return
default:
}
// Block until recvMonitor allows reading
conn.recvMonitor.Limit(maxPacketTotalSize, atomic.LoadInt64(&conn.config.RecvRate), true)
packet, err := conn.readPacket()
if err != nil {
logger.Warnf("recvRoutine: failed to decode packet: %v, error: %v", packet, err)
return
}
conn.recvMonitor.Update(int(1))
switch packet.ChannelID {
case common.ChannelIDPing:
conn.handlePingPong(packet)
default:
conn.handleReceivedPacket(packet)
}
//conn.pingTimer.Reset() // TODO: replace with lightweight Reset()
atomic.StoreUint32(&conn.pendingPings, 0)
}
}
func (conn *Connection) handlePingPong(packet *Packet) (success bool) {
if packet.ChannelID != common.ChannelIDPing {
logger.Errorf("Invalid channel for Ping/Pong signal")
return false
}
if len(packet.Bytes) != 1 {
logger.Errorf("Invalid Ping/Pong packet")
return false
}
pingpong := packet.Bytes[0]
switch pingpong {
case p2ptypes.PingSignal:
conn.schedulePongPulse()
case p2ptypes.PongSignal:
// do nothing for now
default:
logger.Errorf("Invalid Ping/Pong signal")
return false
}
return true
}
func (conn *Connection) handleReceivedPacket(packet *Packet) (success bool) {
channelID := packet.ChannelID
channel := conn.channelGroup.getChannel(channelID)
if channel == nil {
return false
}
aggregatedBytes, success := channel.receivePacket(packet)
if !success {
return false
}
if aggregatedBytes == nil {
return true
}
message, err := conn.onParse(packet.ChannelID, aggregatedBytes)
if err != nil {
logger.Errorf("Error parsing packet: %v, err: %v", packet, err)
return false
}
err = conn.onReceive(message)
if err != nil {
logger.Debugf("Error handling message: %v, err: %v", message, err)
return false
}
return true
}
// --------------------- IO Handling --------------------- //
func (conn *Connection) flush() error {
err := conn.bufWriter.Flush()
return err
}
func (conn *Connection) sendPacketBatch() (success bool, exhausted bool) {
// Block until sendMonitor allows sending
conn.sendMonitor.Limit(maxPacketTotalSize, atomic.LoadInt64(&conn.config.SendRate), true)
// Now send out the packet batch
packetBatchSize := conn.config.PacketBatchSize
for i := int64(0); i < packetBatchSize; i++ {
success, exhausted := conn.sendPacket()
if !success {
logger.Errorf("sendPacketBatch: failed to send out packet")
return false, exhausted
}
if exhausted {
return true, true
}
}
return true, false
}
// Boolean exhausted indicates whether the data in the selected channel has exhausted
func (conn *Connection) sendPacket() (success bool, exhausted bool) {
success, channel := conn.channelGroup.nextChannelToSendPacket()
if !success {
return false, false // TODO: error handling
}
if channel == nil {
return true, true // Nothing to be sent
}
nonemptyPacket, numBytes, err := channel.sendPacketTo(conn)
if err != nil {
return false, !nonemptyPacket
}
if !nonemptyPacket {
return true, true // Nothing to be sent
}
conn.sendMonitor.Update(numBytes)
conn.flushTimer.Set()
return true, false
}
// --------------------- Utils --------------------- //
// GetNetconn returns the attached network connection
func (conn *Connection) GetNetconn() net.Conn {
return conn.netconn
}
// GetBufNetconn returns buffered network connection
func (conn *Connection) GetBufNetconn() io.ReadWriter {
return conn.bufConn
}
// GetBufReader returns buffered reader for network connection
func (conn *Connection) GetBufReader() *bufio.Reader {
return conn.bufReader
}
func (conn *Connection) stopForError(r interface{}) {
logger.Errorf("Connection error: %v", r)
if atomic.CompareAndSwapUint32(&conn.errored, 0, 1) {
conn.Stop()
if conn.onError != nil {
conn.onError(r)
}
}
}
func (conn *Connection) recover() {
if r := recover(); r != nil {
stack := debug.Stack()
err := types.StackError{
r, stack,
}
conn.stopForError(err)
}
}
func (conn *Connection) scheduleSendPulse() {
select {
case conn.sendPulse <- true:
default:
}
}
func (conn *Connection) schedulePongPulse() {
select {
case conn.pongPulse <- true:
default:
}
}
func (conn *Connection) scheduleQuitPulse() {
select {
case conn.quitPulse <- true:
default:
}
}