/
server.go
710 lines (620 loc) · 18.3 KB
/
server.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
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
package p2p
import (
"context"
"errors"
"fmt"
"github.com/libp2p/go-libp2p"
"github.com/libp2p/go-libp2p-core/crypto"
"github.com/libp2p/go-libp2p-core/host"
"github.com/libp2p/go-libp2p-core/network"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p-core/peerstore"
"github.com/multiformats/go-multiaddr"
log "github.com/sirupsen/logrus"
"github.com/theQRL/zond/block"
"github.com/theQRL/zond/chain"
"github.com/theQRL/zond/common"
"github.com/theQRL/zond/config"
"github.com/theQRL/zond/metadata"
"github.com/theQRL/zond/misc"
"github.com/theQRL/zond/ntp"
"github.com/theQRL/zond/p2p/messages"
"github.com/theQRL/zond/protos"
"github.com/theQRL/zond/transactions"
"github.com/willf/bloom"
"net"
"sync"
"time"
)
type conn struct {
fd network.Stream
inbound bool
}
type peerDrop struct {
*Peer
err error
requested bool // true if signaled by the peer
}
type PeerIPWithPLData struct {
multiAddr string
PLData *protos.PLData
}
type Server struct {
config *config.Config
host host.Host
chain *chain.Chain
ntp ntp.NTPInterface
peerData *metadata.PeerData
ipCount map[string]int
inboundCount uint16
totalConnections uint16
listener net.Listener
lock sync.Mutex
peerInfoLock sync.Mutex
running bool
loopWG sync.WaitGroup
exit chan struct{}
connectPeersExit chan struct{}
mrDataConn chan *MRDataConn
addPeerToPeerList chan *PeerIPWithPLData
blockAndPeerChan chan *BlockAndPeer
addPeer chan *conn
delPeer chan *peerDrop
registerAndBroadcastChan chan *messages.RegisterMessage
blockReceivedForAttestation chan *block.Block
attestationReceivedForBlock chan *transactions.Attest
filter *bloom.BloomFilter
mr *MessageReceipt
downloader *Downloader
messagePriority map[protos.LegacyMessage_FuncName]uint64
}
func (srv *Server) GetRegisterAndBroadcastChan() chan *messages.RegisterMessage {
return srv.registerAndBroadcastChan
}
func (srv *Server) GetBlockReceivedForAttestation() chan *block.Block {
return srv.blockReceivedForAttestation
}
func (srv *Server) GetAttestationReceivedForBlock() chan *transactions.Attest {
return srv.attestationReceivedForBlock
}
func (srv *Server) BroadcastBlock(block *block.Block) {
blockHash := block.Hash()
msg := &messages.RegisterMessage{
Msg: &protos.LegacyMessage{
FuncName: protos.LegacyMessage_BK,
Data: &protos.LegacyMessage_Block{
Block: block.PBData(),
},
},
MsgHash: misc.BytesToHexStr(blockHash[:]),
}
srv.registerAndBroadcastChan <- msg
}
func (srv *Server) BroadcastBlockForAttestation(block *block.Block, signature []byte) {
partialBlockSigningHash := block.PartialBlockSigningHash()
msg := &messages.RegisterMessage{
Msg: &protos.LegacyMessage{
FuncName: protos.LegacyMessage_BA,
Data: &protos.LegacyMessage_BlockForAttestation{
BlockForAttestation: &protos.BlockForAttestation{
Block: block.PBData(),
Signature: signature,
},
},
},
MsgHash: misc.BytesToHexStr(partialBlockSigningHash[:]),
}
srv.registerAndBroadcastChan <- msg
}
func (srv *Server) BroadcastAttestationTransaction(attestTx *transactions.Attest,
slotNumber uint64, blockProposer []byte,
parentHeaderHash common.Hash, partialBlockSigningHash common.Hash) {
txHash := attestTx.TxHash(attestTx.GetSigningHash(partialBlockSigningHash))
msg := &messages.RegisterMessage{
Msg: &protos.LegacyMessage{
FuncName: protos.LegacyMessage_AT,
Data: &protos.LegacyMessage_AtData{
AtData: &protos.ProtocolTransactionData{
Tx: attestTx.PBData(),
SlotNumber: slotNumber,
BlockProposer: blockProposer,
ParentHeaderHash: parentHeaderHash[:],
PartialBlockSigningHash: partialBlockSigningHash[:],
},
},
},
MsgHash: misc.BytesToHexStr(txHash[:]),
}
log.Info("[BroadcastAttestationTransaction] Broadcasting Attestation Txn ",
msg.MsgHash)
srv.registerAndBroadcastChan <- msg
}
func (srv *Server) handleStream(s network.Stream) {
log.Println("Got a new stream!")
// Create a buffer stream for non blocking read and write.
srv.addPeer <- &conn{s, true}
}
func (srv *Server) Start(keys crypto.PrivKey) (err error) {
srv.lock.Lock()
defer srv.lock.Unlock()
if srv.running {
return errors.New("server is already running")
}
srv.filter = bloom.New(200000, 5)
//srv.chain.GetTransactionPool().SetRegisterAndBroadcastChan(srv.registerAndBroadcastChan)
if err := srv.startListening(keys); err != nil {
return err
}
srv.running = true
go srv.run()
go srv.downloader.DownloadMonitor()
go srv.ConnectPeers()
return nil
}
func (srv *Server) Stop() (err error) {
srv.lock.Lock()
defer srv.lock.Unlock()
if !srv.running {
return
}
srv.running = false
if srv.listener != nil {
srv.listener.Close()
}
close(srv.exit)
close(srv.connectPeersExit)
srv.loopWG.Wait()
return nil
}
func (srv *Server) ConnectPeer(dest string) error {
maddr, err := multiaddr.NewMultiaddr(dest)
if err != nil {
return err
}
info, err := peer.AddrInfoFromP2pAddr(maddr)
if err != nil {
return err
}
// TODO: Look into PermanentAddrTTL
srv.host.Peerstore().AddAddrs(info.ID, info.Addrs, peerstore.PermanentAddrTTL)
s, err := srv.host.NewStream(context.Background(),
info.ID,
config.GetDevConfig().ProtocolID)
if err != nil {
return err
}
srv.addPeer <- &conn{s, false}
return nil
}
func (srv *Server) ConnectPeers() error {
srv.loopWG.Add(1)
defer srv.loopWG.Done()
bootstrapPeers := make(map[string]bool)
for _, multiAddr := range srv.config.User.Node.PeerList {
//log.Info("Connecting peer ", peer)
err := srv.peerData.AddDisconnectedPeers(multiAddr)
if err != nil {
log.Error("Failed to add bootstrap node in disconnected peers ", multiAddr,
" Reason: ", err.Error())
continue
}
bootstrapPeers[multiAddr] = true
}
peerList := make([]string, 0)
for {
select {
case <-time.After(15 * time.Second):
srv.peerInfoLock.Lock()
if srv.inboundCount > srv.config.User.Node.MaxPeersLimit {
srv.peerInfoLock.Unlock()
break
}
maxConnectionTry := 10
if len(peerList) == 0 {
for _, p := range srv.peerData.DisconnectedPeers() {
if connCount, ok := srv.ipCount[p.IP()]; ok {
// Ignore skipping connection to addresses
// when there is no connection with any peer
if !(srv.totalConnections == 0 && connCount == 0) {
continue
}
}
peerList = append(peerList, p.MultiAddr())
}
}
srv.peerInfoLock.Unlock()
count := 0
removePeers := make([]string, 0)
for _, multiAddr := range peerList {
if !srv.running {
break
}
if count >= maxConnectionTry {
break
}
log.Info("Trying to Connect ", multiAddr)
err := srv.ConnectPeer(multiAddr)
count += 1
// Skip removal of bootstrapPeers
if err != nil && !bootstrapPeers[multiAddr] {
log.Info("Failed to connect to ", multiAddr)
removePeers = append(removePeers, multiAddr)
continue
}
}
srv.peerInfoLock.Lock()
for _, multiAddr := range removePeers {
err := srv.peerData.RemovePeer(multiAddr)
if err != nil {
log.Error("Failed to removePeer",
" Reason: ", err.Error())
continue
}
}
srv.peerInfoLock.Unlock()
peerList = peerList[count:]
case <-srv.connectPeersExit:
return nil
}
}
}
func (srv *Server) startListening(keys crypto.PrivKey) error {
multiAddrStr := fmt.Sprintf("/ip4/%s/tcp/%d",
srv.config.User.Node.BindingIP,
srv.config.User.Node.LocalPort)
sourceMultiAddr, _ := multiaddr.NewMultiaddr(multiAddrStr)
host, err := libp2p.New(
context.Background(),
libp2p.ListenAddrs(sourceMultiAddr),
libp2p.Identity(keys),
)
if err != nil {
return err
}
srv.host = host
host.SetStreamHandler(config.GetDevConfig().ProtocolID,
srv.handleStream)
listenAddr := fmt.Sprintf("%s/p2p/%s",
multiAddrStr, host.ID().Pretty())
log.Info("Listening at ", listenAddr)
//bindingAddress := fmt.Sprintf("%s:%d",
// srv.config.User.Node.BindingIP,
// srv.config.User.Node.LocalPort)
//listener, err := net.Listen("tcp", bindingAddress)
//if err != nil {
// return err
//}
//
//srv.listener = listener
//go srv.listenLoop(listener)
return nil
}
func (srv *Server) run() {
var (
peers = make(map[string]*Peer)
)
srv.loopWG.Add(1)
defer srv.loopWG.Done()
running:
for {
select {
case <-srv.exit:
srv.downloader.Exit()
log.Debug("Shutting Down Server")
break running
case c := <-srv.addPeer:
srv.peerInfoLock.Lock()
log.Debug("Adding peer",
" addr ", c.fd.Conn().RemoteMultiaddr())
p := newPeer(
c.fd,
c.inbound,
srv.chain,
srv.filter,
srv.mr,
srv.peerData,
srv.mrDataConn,
srv.registerAndBroadcastChan,
srv.blockReceivedForAttestation,
srv.attestationReceivedForBlock,
srv.addPeerToPeerList,
srv.blockAndPeerChan,
srv.messagePriority)
go srv.runPeer(p)
peers[c.fd.ID()] = p
ip := misc.IPFromMultiAddr(c.fd.Conn().RemoteMultiaddr().String())
srv.ipCount[ip] += 1
srv.totalConnections += 1
if p.inbound {
srv.inboundCount++
}
if srv.ipCount[ip] > srv.config.User.Node.MaxRedundantConnections {
log.Info("Disconnecting due to max redundant connections")
p.Disconnect()
// TODO: Ban peer
}
srv.peerInfoLock.Unlock()
srv.downloader.AddPeer(p)
case pd := <-srv.delPeer:
srv.peerInfoLock.Lock()
log.Debug("Removing Peer", "err", pd.err)
peer := peers[pd.stream.ID()]
delete(peers, pd.stream.ID())
if pd.inbound {
srv.inboundCount--
}
ip := pd.ip
srv.ipCount[ip] -= 1
srv.totalConnections -= 1
if pd.isPLShared {
err := srv.peerData.AddDisconnectedPeers(pd.multiAddr)
if err != nil {
log.Error("Failed to add peer into disconnected peers",
" ", pd.multiAddr,
" Reason: ", err.Error())
}
}
srv.peerInfoLock.Unlock()
srv.downloader.RemovePeer(peer)
case mrDataConn := <-srv.mrDataConn:
// TODO: Process Message Receipt
// Need to get connection too
mrData := mrDataConn.mrData
msgHash := misc.BytesToHexStr(mrData.Hash)
switch mrData.Type {
case protos.LegacyMessage_BA:
/*
1. Verify if Block Received for attestation is valid
2. Broadcast the block
3. Attest the block if Staking is Enabled on this node
*/
var parentHash common.Hash
copy(parentHash[:], mrData.ParentHeaderHash)
_, err := srv.chain.GetBlock(parentHash)
if err != nil {
log.Info("[BlockForAttestation] Missing Parent Block",
" #", mrData.SlotNumber,
" Partial Block Signing Hash ", misc.BytesToHexStr(mrData.Hash),
" Parent Block ", misc.BytesToHexStr(mrData.ParentHeaderHash))
break
}
if srv.mr.contains(mrData.Hash, mrData.Type) {
break
}
srv.mr.addPeer(mrData, mrDataConn.peer)
value, ok := srv.mr.GetRequestedHash(msgHash)
if ok && value.GetRequested() {
break
}
go srv.RequestFullMessage(mrData)
//TODO: Logic to be written
//srv.blockReceivedForAttestation
case protos.LegacyMessage_BK:
finalizedHeaderHash, err := srv.chain.GetFinalizedHeaderHash()
if err != nil {
log.Error("No Finalized Header Hash ", err)
}
if (finalizedHeaderHash != common.Hash{}) {
finalizedBlock, err := srv.chain.GetBlock(finalizedHeaderHash)
if err != nil {
log.Error("Failed to get finalized block ",
misc.BytesToHexStr(finalizedHeaderHash[:]))
break
}
// skip slot number beyond the Finalized slot Number
if finalizedBlock.SlotNumber() >= mrData.SlotNumber {
log.Warn("[BlockReceived] Block #", mrData.SlotNumber,
" is beyond finalized block #", finalizedBlock.SlotNumber())
break
}
}
var parentHash common.Hash
copy(parentHash[:], mrData.ParentHeaderHash)
_, err = srv.chain.GetBlock(parentHash)
if err != nil {
log.Info("[BlockReceived] Missing Parent Block ",
" #", mrData.SlotNumber,
" Block ", misc.BytesToHexStr(mrData.Hash),
" Parent Block ", misc.BytesToHexStr(mrData.ParentHeaderHash))
break
}
if srv.mr.contains(mrData.Hash, mrData.Type) {
break
}
srv.mr.addPeer(mrData, mrDataConn.peer)
value, ok := srv.mr.GetRequestedHash(msgHash)
if ok && value.GetRequested() {
break
}
go srv.RequestFullMessage(mrData)
// Request for full message
// Check if its already being feeded by any other peer
case protos.LegacyMessage_TT:
srv.HandleTransaction(mrDataConn)
case protos.LegacyMessage_ST:
srv.HandleTransaction(mrDataConn)
case protos.LegacyMessage_AT:
srv.HandleTransaction(mrDataConn)
default:
log.Warn("Unknown Message Receipt Type",
"Type", mrData.Type)
mrDataConn.peer.Disconnect()
}
case blockAndPeer := <-srv.blockAndPeerChan:
srv.BlockReceived(blockAndPeer.peer, blockAndPeer.block)
case addPeerToPeerList := <-srv.addPeerToPeerList:
srv.UpdatePeerList(addPeerToPeerList)
case registerAndBroadcast := <-srv.registerAndBroadcastChan:
srv.mr.Register(registerAndBroadcast.MsgHash, registerAndBroadcast.Msg)
binMsgHash, err := misc.HexStrToBytes(registerAndBroadcast.MsgHash)
if err != nil {
log.Error("Error decoding message hash ", err.Error())
continue
}
out := &Msg{
msg: &protos.LegacyMessage{
FuncName: protos.LegacyMessage_MR,
Data: &protos.LegacyMessage_MrData{
MrData: &protos.MRData{
Hash: binMsgHash,
Type: registerAndBroadcast.Msg.FuncName,
},
},
},
}
b := registerAndBroadcast.Msg.GetBlock()
if b != nil {
out.msg.GetMrData().SlotNumber = b.Header.SlotNumber
out.msg.GetMrData().ParentHeaderHash = b.Header.ParentHash
} else {
ba := registerAndBroadcast.Msg.GetBlockForAttestation()
if ba != nil {
out.msg.GetMrData().SlotNumber = ba.Block.Header.SlotNumber
out.msg.GetMrData().ParentHeaderHash = ba.Block.Header.ParentHash
}
}
ignorePeers := make(map[*Peer]bool, 0)
if msgRequest, ok := srv.mr.GetRequestedHash(registerAndBroadcast.MsgHash); ok {
ignorePeers = msgRequest.peers
}
for _, p := range peers {
if _, ok := ignorePeers[p]; !ok {
p.Send(out)
}
}
}
}
for _, p := range peers {
p.Disconnect()
}
}
func (srv *Server) HandleTransaction(mrDataConn *MRDataConn) {
mrData := mrDataConn.mrData
srv.mr.addPeer(mrData, mrDataConn.peer)
// TODO: Ignore transaction if node is syncing
if srv.downloader.isSyncing {
return
}
if srv.chain.GetTransactionPool().IsFull() {
return
}
go srv.RequestFullMessage(mrData)
}
func (srv *Server) RequestFullMessage(mrData *protos.MRData) {
for {
msgHash := misc.BytesToHexStr(mrData.Hash)
_, ok := srv.mr.GetHashMsg(msgHash)
if ok {
if _, ok = srv.mr.GetRequestedHash(msgHash); ok {
srv.mr.RemoveRequestedHash(msgHash)
}
return
}
requestedHash, ok := srv.mr.GetRequestedHash(msgHash)
if !ok {
return
}
peer := requestedHash.GetPeer()
if peer == nil {
return
}
requestedHash.SetPeer(peer, true)
mrData := &protos.MRData{
Hash: mrData.Hash,
Type: mrData.Type,
}
out := &Msg{}
out.msg = &protos.LegacyMessage{
FuncName: protos.LegacyMessage_SFM,
Data: &protos.LegacyMessage_MrData{
MrData: mrData,
},
}
peer.Send(out)
time.Sleep(time.Duration(srv.config.Dev.MessageReceiptTimeout) * time.Second)
}
}
func (srv *Server) BlockReceived(peer *Peer, b *block.Block) {
blockHash := b.Hash()
headerHash := misc.BytesToHexStr(blockHash[:])
log.Info(">>> Received Block",
" #", b.SlotNumber(),
" Hash ", headerHash)
// TODO: Trigger Syncing/Block downloader
select {
case srv.downloader.blockAndPeerChannel <- &BlockAndPeer{b, peer}:
case <-time.After(5 * time.Second):
log.Info("Timeout for Received Block",
"#", b.SlotNumber(),
"Hash", headerHash)
}
}
func (srv *Server) UpdatePeerList(p *PeerIPWithPLData) error {
err := srv.peerData.AddConnectedPeers(p.multiAddr)
if err != nil {
log.Error("Failed to Add Peer into peer list",
" ", p.multiAddr,
" Reason: ", err.Error())
return err
}
for _, peerMultiAddr := range p.PLData.PeerIps {
if srv.peerData.IsPeerInList(peerMultiAddr) {
continue
}
err = srv.peerData.AddDisconnectedPeers(peerMultiAddr)
if err != nil {
log.Error("Failed to add peer ", peerMultiAddr, " in peer list",
" Reason: ", err.Error())
continue
}
}
return nil
}
func (srv *Server) runPeer(p *Peer) {
remoteRequested := p.run()
srv.delPeer <- &peerDrop{p, nil, remoteRequested}
}
func NewServer(chain *chain.Chain) (*Server, error) {
peerData, err := metadata.NewPeerData()
if err != nil {
return nil, err
}
srv := &Server{
config: config.GetConfig(),
chain: chain,
ntp: ntp.GetNTP(),
peerData: peerData,
ipCount: make(map[string]int),
mr: CreateMR(),
downloader: NewDownloader(chain),
exit: make(chan struct{}),
connectPeersExit: make(chan struct{}),
mrDataConn: make(chan *MRDataConn),
addPeerToPeerList: make(chan *PeerIPWithPLData),
blockAndPeerChan: make(chan *BlockAndPeer),
addPeer: make(chan *conn),
delPeer: make(chan *peerDrop),
registerAndBroadcastChan: make(chan *messages.RegisterMessage, 100),
blockReceivedForAttestation: make(chan *block.Block),
attestationReceivedForBlock: make(chan *transactions.Attest),
messagePriority: make(map[protos.LegacyMessage_FuncName]uint64),
}
srv.messagePriority[protos.LegacyMessage_VE] = 0
srv.messagePriority[protos.LegacyMessage_PL] = 0
srv.messagePriority[protos.LegacyMessage_PONG] = 0
srv.messagePriority[protos.LegacyMessage_MR] = 2
srv.messagePriority[protos.LegacyMessage_SFM] = 1
srv.messagePriority[protos.LegacyMessage_BA] = 1
srv.messagePriority[protos.LegacyMessage_BK] = 1
srv.messagePriority[protos.LegacyMessage_FB] = 0
srv.messagePriority[protos.LegacyMessage_PB] = 0
srv.messagePriority[protos.LegacyMessage_TT] = 1
srv.messagePriority[protos.LegacyMessage_ST] = 1
srv.messagePriority[protos.LegacyMessage_AT] = 1
srv.messagePriority[protos.LegacyMessage_SYNC] = 0
srv.messagePriority[protos.LegacyMessage_CHAINSTATE] = 0
srv.messagePriority[protos.LegacyMessage_EBHREQ] = 0
srv.messagePriority[protos.LegacyMessage_EBHRESP] = 0
srv.messagePriority[protos.LegacyMessage_P2P_ACK] = 0
return srv, nil
}