-
Notifications
You must be signed in to change notification settings - Fork 39
/
p2p.go
479 lines (411 loc) · 13.4 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
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
package p2p
import (
"fmt"
"io"
"math/big"
"net"
"sync"
"sync/atomic"
"time"
"github.com/obscuronet/go-obscuro/go/enclave/core"
"github.com/obscuronet/go-obscuro/go/common/measure"
"github.com/obscuronet/go-obscuro/go/common/retry"
"github.com/obscuronet/go-obscuro/go/common/subscription"
"github.com/pkg/errors"
"github.com/ethereum/go-ethereum/rlp"
"github.com/obscuronet/go-obscuro/go/common"
"github.com/obscuronet/go-obscuro/go/common/host"
"github.com/obscuronet/go-obscuro/go/common/log"
"github.com/obscuronet/go-obscuro/go/config"
gethlog "github.com/ethereum/go-ethereum/log"
gethmetrics "github.com/ethereum/go-ethereum/metrics"
)
const (
tcp = "tcp"
msgTypeTx msgType = iota
msgTypeBatches
msgTypeBatchRequest
)
var (
_alertPeriod = 5 * time.Minute
errUnknownSequencer = errors.New("sequencer address not known")
)
// A P2P message's type.
type msgType uint8
// Associates an encoded message to its type.
type message struct {
Sender string // todo (#1619) - this needs to be authed in the future
Type msgType
Contents []byte
}
type p2pServiceLocator interface {
L1Publisher() host.L1Publisher
L2Repo() host.L2BatchRepository
}
// NewSocketP2PLayer - returns the Socket implementation of the P2P
func NewSocketP2PLayer(config *config.HostConfig, serviceLocator p2pServiceLocator, logger gethlog.Logger, metricReg gethmetrics.Registry) *Service {
return &Service{
batchSubscribers: subscription.NewManager[host.P2PBatchHandler](),
txSubscribers: subscription.NewManager[host.P2PTxHandler](),
batchReqHandlers: subscription.NewManager[host.P2PBatchRequestHandler](),
sl: serviceLocator,
isSequencer: config.NodeType == common.Sequencer,
ourBindAddress: config.P2PBindAddress,
ourPublicAddress: config.P2PPublicAddress,
peerAddresses: []string{},
p2pTimeout: config.P2PConnectionTimeout,
peerAddressesMutex: sync.RWMutex{},
// monitoring
peerTracker: newPeerTracker(),
metricsRegistry: metricReg,
logger: logger,
isIncomingP2PDisabled: config.IsInboundP2PDisabled,
}
}
type Service struct {
batchSubscribers *subscription.Manager[host.P2PBatchHandler]
txSubscribers *subscription.Manager[host.P2PTxHandler]
batchReqHandlers *subscription.Manager[host.P2PBatchRequestHandler]
listener net.Listener
running atomic.Bool // new connections won't be accepted if this is false
sl p2pServiceLocator
isSequencer bool
ourBindAddress string
ourPublicAddress string
peerAddresses []string
p2pTimeout time.Duration
peerTracker *peerTracker
metricsRegistry gethmetrics.Registry
logger gethlog.Logger
peerAddressesMutex sync.RWMutex
isIncomingP2PDisabled bool
}
func (p *Service) Start() error {
p.running.Store(true)
if p.isIncomingP2PDisabled {
go p.RefreshPeerList()
return nil
}
// We listen for P2P connections.
listener, err := net.Listen("tcp", p.ourBindAddress)
if err != nil {
return fmt.Errorf("could not listen for P2P connections on %s: %w", p.ourBindAddress, err)
}
p.logger.Info("P2P server started listening", "bindAddress", p.ourBindAddress, "publicAddress", p.ourPublicAddress)
p.listener = listener
go p.handleConnections()
// ensure we have re-synced the peer list from management contract after startup
go p.RefreshPeerList()
return nil
}
func (p *Service) Stop() error {
p.logger.Info("Shutting down P2P.")
p.running.Store(false)
if p.listener != nil {
// todo immediately shutting down the listener seems to impact other hosts shutdown process
time.Sleep(time.Second)
return p.listener.Close()
}
return nil
}
func (p *Service) HealthStatus() host.HealthStatus {
msg := ""
if err := p.verifyHealth(); err != nil {
msg = err.Error()
}
return &host.BasicErrHealthStatus{
ErrMsg: msg,
}
}
func (p *Service) SubscribeForBatches(handler host.P2PBatchHandler) func() {
if p.isIncomingP2PDisabled {
return nil
}
return p.batchSubscribers.Subscribe(handler)
}
func (p *Service) SubscribeForTx(handler host.P2PTxHandler) func() {
return p.txSubscribers.Subscribe(handler)
}
func (p *Service) SubscribeForBatchRequests(handler host.P2PBatchRequestHandler) func() {
if p.isIncomingP2PDisabled {
return nil
}
return p.batchReqHandlers.Subscribe(handler)
}
// RefreshPeerList - fetches the latest peer list from L1 and updates the peerAddresses.
// Note: this is designed to be run in a separate goroutine, it will retry a few times before giving up.
func (p *Service) RefreshPeerList() {
var newPeers []string
err := retry.Do(func() error {
if !p.running.Load() {
return retry.FailFast(fmt.Errorf("p2p service is stopped - abandoning peer list refresh"))
}
var retryErr error
newPeers, retryErr = p.sl.L1Publisher().FetchLatestPeersList()
if retryErr != nil {
p.logger.Error("failed to fetch latest peer list from L1", log.ErrKey, retryErr)
return retryErr
}
return nil
}, retry.NewTimeoutStrategy(1*time.Minute, 5*time.Second))
if err != nil {
p.logger.Error("unable to fetch latest peer list from L1", log.ErrKey, err)
return
}
p.peerAddressesMutex.Lock()
defer p.peerAddressesMutex.Unlock()
p.logger.Info(fmt.Sprintf("Updated peer list - old: %s new: %s", p.peerAddresses, newPeers))
p.peerAddresses = newPeers
}
func (p *Service) SendTxToSequencer(tx common.EncryptedTx) error {
if p.isSequencer {
return errors.New("sequencer cannot send tx to itself")
}
msg := message{Sender: p.ourPublicAddress, Type: msgTypeTx, Contents: tx}
sequencer, err := p.getSequencer()
if err != nil {
return fmt.Errorf("failed to find sequencer - %w", err)
}
return p.send(msg, sequencer)
}
func (p *Service) BroadcastBatches(batches []*common.ExtBatch) error {
if p.isIncomingP2PDisabled {
return nil
}
if !p.isSequencer {
return errors.New("only sequencer can broadcast batches")
}
batchMsg := host.BatchMsg{
Batches: batches,
IsLive: true,
}
encodedBatchMsg, err := rlp.EncodeToBytes(batchMsg)
if err != nil {
return fmt.Errorf("could not encode batch using RLP. Cause: %w", err)
}
msg := message{Sender: p.ourPublicAddress, Type: msgTypeBatches, Contents: encodedBatchMsg}
return p.broadcast(msg)
}
func (p *Service) RequestBatchesFromSequencer(fromSeqNo *big.Int) error {
if p.isIncomingP2PDisabled {
return nil
}
if p.isSequencer {
return errors.New("sequencer cannot request batches from itself")
}
batchRequest := &common.BatchRequest{
Requester: p.ourPublicAddress,
FromSeqNo: fromSeqNo,
}
defer core.LogMethodDuration(p.logger, measure.NewStopwatch(), "Requested batches from sequencer", "fromSeqNo", batchRequest.FromSeqNo)
encodedBatchRequest, err := rlp.EncodeToBytes(batchRequest)
if err != nil {
return fmt.Errorf("could not encode batch request using RLP. Cause: %w", err)
}
msg := message{Sender: p.ourPublicAddress, Type: msgTypeBatchRequest, Contents: encodedBatchRequest}
// todo (#718) - allow missing batches to be requested from peers other than sequencer?
sequencer, err := p.getSequencer()
if err != nil {
return fmt.Errorf("failed to find sequencer - %w", err)
}
return p.send(msg, sequencer)
}
func (p *Service) RespondToBatchRequest(requestID string, batches []*common.ExtBatch) error {
if p.isIncomingP2PDisabled {
return nil
}
if !p.isSequencer {
return errors.New("only sequencer can respond to batch requests")
}
batchMsg := &host.BatchMsg{
Batches: batches,
IsLive: false,
}
encodedBatchMsg, err := rlp.EncodeToBytes(batchMsg)
if err != nil {
return fmt.Errorf("could not encode batches using RLP. Cause: %w", err)
}
msg := message{Sender: p.ourPublicAddress, Type: msgTypeBatches, Contents: encodedBatchMsg}
return p.send(msg, requestID)
}
// HealthCheck returns whether the p2p is considered healthy
// Currently it considers itself unhealthy
// if there's more than 100 failures on a given fail type
// if there's a known peer for which a message hasn't been received
func (p *Service) verifyHealth() error {
if p.isIncomingP2PDisabled {
return nil
}
var noMsgReceivedPeers []string
for peer, lastMsgTimestamp := range p.peerTracker.receivedMessagesByPeer() {
if time.Now().After(lastMsgTimestamp.Add(_alertPeriod)) {
noMsgReceivedPeers = append(noMsgReceivedPeers, peer)
p.logger.Warn("no message from peer in the alert period",
"ourAddress", p.ourPublicAddress,
"peer", peer,
"alertPeriod", _alertPeriod,
)
}
}
if len(noMsgReceivedPeers) > 0 {
return errors.New("no message received from peers")
}
return nil
}
// Listens for connections and handles them in a separate goroutine.
func (p *Service) handleConnections() {
for p.running.Load() {
conn, err := p.listener.Accept()
if err != nil {
if p.running.Load() {
p.logger.Debug("Could not form P2P connection", log.ErrKey, err)
}
return
}
go p.handle(conn)
}
}
// Receives and decodes a P2P message, and pushes it to the correct channel.
func (p *Service) handle(conn net.Conn) {
if conn != nil {
defer conn.Close()
}
encodedMsg, err := io.ReadAll(conn)
if err != nil {
p.logger.Debug("Failed to read message from peer", log.ErrKey, err)
return
}
msg := message{}
err = rlp.DecodeBytes(encodedMsg, &msg)
if err != nil {
p.logger.Debug("Failed to decode message received from peer: ", log.ErrKey, err)
return
}
switch msg.Type {
case msgTypeTx:
if !p.isSequencer {
p.logger.Error("Received transaction from peer, but not a sequencer node")
return
}
// The transaction is encrypted, so we cannot check that it's correctly formed.
for _, txSubs := range p.txSubscribers.Subscribers() {
txSubs.HandleTransaction(msg.Contents)
}
case msgTypeBatches:
if p.isSequencer {
p.logger.Error("received batch from peer, but this is a sequencer node")
return
}
var batchMsg *host.BatchMsg
err := rlp.DecodeBytes(msg.Contents, &batchMsg)
if err != nil {
p.logger.Warn("unable to decode batch received from peer", log.ErrKey, err)
// nothing to send to subscribers
break
}
// todo - check the batch signature
for _, batchSubs := range p.batchSubscribers.Subscribers() {
go batchSubs.HandleBatches(batchMsg.Batches, batchMsg.IsLive)
}
case msgTypeBatchRequest:
if !p.isSequencer {
p.logger.Error("received batch request from peer, but not a sequencer node")
return
}
// this is an incoming request, p2p service is responsible for finding the response and returning it
go p.handleBatchRequest(msg.Contents)
}
p.peerTracker.receivedPeerMsg(msg.Sender)
}
// Broadcasts a message to all peers.
func (p *Service) broadcast(msg message) error {
msgEncoded, err := rlp.EncodeToBytes(msg)
if err != nil {
return fmt.Errorf("could not encode message to send to peers. Cause: %w", err)
}
// clone current known addresses
p.peerAddressesMutex.RLock()
currentAddresses := make([]string, len(p.peerAddresses))
copy(currentAddresses, p.peerAddresses)
p.peerAddressesMutex.RUnlock()
for _, address := range currentAddresses {
closureAddr := address
go func() {
err := p.sendBytesWithRetry(closureAddr, msgEncoded)
if err != nil {
p.logger.Debug("Could not send message to peer", "peer", closureAddr, log.ErrKey, err)
}
}()
}
return nil
}
// Sends a message to the provided address.
func (p *Service) send(msg message, to string) error {
// sanity check the message to discover bugs
if !(msg.Type >= 1 && msg.Type <= 3) {
p.logger.Error(fmt.Sprintf("Sending message with wrong message type: %v", msg))
}
if len(msg.Sender) == 0 {
p.logger.Error(fmt.Sprintf("Sending message with wrong sender type: %v", msg))
}
if len(msg.Contents) == 0 {
p.logger.Error(fmt.Sprintf("Sending message with empty contents: %v", msg))
}
msgEncoded, err := rlp.EncodeToBytes(msg)
if err != nil {
return fmt.Errorf("could not encode message to send to sequencer. Cause: %w", err)
}
err = p.sendBytesWithRetry(to, msgEncoded)
if err != nil {
return err
}
return nil
}
// Sends the bytes to the provided address.
// Until introducing libp2p (or equivalent), we have a simple retry
func (p *Service) sendBytesWithRetry(address string, msgEncoded []byte) error {
// retry for about 2 seconds
err := retry.Do(func() error {
return p.sendBytes(address, msgEncoded)
}, retry.NewDoublingBackoffStrategy(100*time.Millisecond, 5))
return err
}
// Sends the bytes to the provided address.
func (p *Service) sendBytes(address string, tx []byte) error {
conn, err := net.DialTimeout(tcp, address, p.p2pTimeout)
if conn != nil {
defer conn.Close()
}
if err != nil {
p.logger.Debug(fmt.Sprintf("could not connect to peer on address %s", address), log.ErrKey, err)
return err
}
_, err = conn.Write(tx)
if err != nil {
p.logger.Debug(fmt.Sprintf("could not send message to peer on address %s", address), log.ErrKey, err)
return err
}
return nil
}
// Retrieves the sequencer's address.
// todo (#718) - use better method to identify the sequencer?
func (p *Service) getSequencer() (string, error) {
p.peerAddressesMutex.RLock()
defer p.peerAddressesMutex.RUnlock()
if len(p.peerAddresses) == 0 {
return "", errUnknownSequencer
}
return p.peerAddresses[0], nil
}
func (p *Service) handleBatchRequest(encodedBatchRequest common.EncodedBatchRequest) {
var batchRequest *common.BatchRequest
err := rlp.DecodeBytes(encodedBatchRequest, &batchRequest)
if err != nil {
p.logger.Warn("unable to decode batch request received from peer using RLP", log.ErrKey, err)
return
}
// todo (@matt) should this response be synchronous?
for _, requestHandler := range p.batchReqHandlers.Subscribers() {
go requestHandler.HandleBatchRequest(batchRequest.Requester, batchRequest.FromSeqNo)
}
}