-
Notifications
You must be signed in to change notification settings - Fork 3
/
node_syncing.go
831 lines (748 loc) · 25.3 KB
/
node_syncing.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
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
package node
import (
"fmt"
"math/rand"
"net"
"strconv"
"sync"
"time"
"github.com/PositionExchange/posichain/internal/tikv"
prom "github.com/PositionExchange/posichain/api/service/prometheus"
"github.com/prometheus/client_golang/prometheus"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/rlp"
lru "github.com/hashicorp/golang-lru"
"github.com/pkg/errors"
"github.com/PositionExchange/posichain/api/service"
"github.com/PositionExchange/posichain/api/service/legacysync"
legdownloader "github.com/PositionExchange/posichain/api/service/legacysync/downloader"
downloader_pb "github.com/PositionExchange/posichain/api/service/legacysync/downloader/proto"
"github.com/PositionExchange/posichain/api/service/synchronize"
"github.com/PositionExchange/posichain/core"
"github.com/PositionExchange/posichain/core/types"
"github.com/PositionExchange/posichain/hmy/downloader"
nodeconfig "github.com/PositionExchange/posichain/internal/configs/node"
"github.com/PositionExchange/posichain/internal/utils"
"github.com/PositionExchange/posichain/node/worker"
"github.com/PositionExchange/posichain/p2p"
"github.com/PositionExchange/posichain/shard"
)
// Constants related to doing syncing.
const (
SyncFrequency = 60 * time.Second
// getBlocksRequestHardCap is the hard capped message size at server side for getBlocks request.
// The number is 4MB (default gRPC message size) minus 2k reserved for message overhead.
getBlocksRequestHardCap = 4*1024*1024 - 2*1024
// largeNumberDiff is the number of big block diff to set un-sync
// TODO: refactor this.
largeNumberDiff = 1000
)
var letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
// BeaconSyncHook is the hook function called after inserted beacon in downloader
// TODO: This is a small misc piece of consensus logic. Better put it to consensus module.
func (node *Node) BeaconSyncHook() {
node.BroadcastCrossLinkFromShardsToBeacon()
}
// GenerateRandomString generates a random string with given length
func GenerateRandomString(n int) string {
b := make([]rune, n)
for i := range b {
b[i] = letterRunes[rand.Intn(len(letterRunes))]
}
return string(b)
}
// getNeighborPeers is a helper function to return list of peers
// based on different neightbor map
func getNeighborPeers(neighbor *sync.Map) []p2p.Peer {
tmp := []p2p.Peer{}
neighbor.Range(func(k, v interface{}) bool {
p := v.(p2p.Peer)
t := p.Port
p.Port = legacysync.GetSyncingPort(t)
tmp = append(tmp, p)
return true
})
return tmp
}
// DoSyncWithoutConsensus gets sync-ed to blockchain without joining consensus
func (node *Node) DoSyncWithoutConsensus() {
go node.DoSyncing(node.Blockchain(), node.Worker, false) //Don't join consensus
}
// IsSameHeight tells whether node is at same bc height as a peer
func (node *Node) IsSameHeight() (uint64, bool) {
if node.stateSync == nil {
node.stateSync = node.createStateSync(node.Blockchain())
}
return node.stateSync.IsSameBlockchainHeight(node.Blockchain())
}
func (node *Node) createStateSync(bc core.BlockChain) *legacysync.StateSync {
// Temp hack: The actual port used in dns sync is node.downloaderServer.Port.
// But registration is done through an old way of port arithmetics (syncPort + 3000).
// Thus for compatibility, we are doing the arithmetics here, and not to change the
// protocol itself. This is just the temporary hack and will not be a concern after
// state sync.
var mySyncPort int
if node.downloaderServer != nil {
mySyncPort = node.downloaderServer.Port
} else {
// If local sync server is not started, the port field in protocol is actually not
// functional, simply set it to default value.
mySyncPort = nodeconfig.DefaultDNSPort
}
mutatedPort := strconv.Itoa(mySyncPort + legacysync.SyncingPortDifference)
role := node.NodeConfig.Role()
return legacysync.CreateStateSync(bc, node.SelfPeer.IP, mutatedPort,
node.GetSyncID(), node.NodeConfig.Role() == nodeconfig.ExplorerNode, role)
}
// SyncingPeerProvider is an interface for getting the peers in the given shard.
type SyncingPeerProvider interface {
SyncingPeers(shardID uint32) (peers []p2p.Peer, err error)
}
// DNSSyncingPeerProvider uses the given DNS zone to resolve syncing peers.
type DNSSyncingPeerProvider struct {
zone, port string
lookupHost func(name string) (addrs []string, err error)
}
// NewDNSSyncingPeerProvider returns a provider that uses given DNS name and
// port number to resolve syncing peers.
func NewDNSSyncingPeerProvider(zone, port string) *DNSSyncingPeerProvider {
return &DNSSyncingPeerProvider{
zone: zone,
port: port,
lookupHost: net.LookupHost,
}
}
// SyncingPeers resolves DNS name into peers and returns them.
func (p *DNSSyncingPeerProvider) SyncingPeers(shardID uint32) (peers []p2p.Peer, err error) {
dns := fmt.Sprintf("s%d.%s", shardID, p.zone)
addrs, err := p.lookupHost(dns)
if err != nil {
return nil, errors.Wrapf(err,
"[SYNC] cannot find peers using DNS name %#v", dns)
}
for _, addr := range addrs {
peers = append(peers, p2p.Peer{IP: addr, Port: p.port})
}
return peers, nil
}
// LocalSyncingPeerProvider uses localnet deployment convention to synthesize
// syncing peers.
type LocalSyncingPeerProvider struct {
basePort, selfPort uint16
numShards, shardSize uint32
}
// NewLocalSyncingPeerProvider returns a provider that synthesizes syncing
// peers given the network configuration
func NewLocalSyncingPeerProvider(
basePort, selfPort uint16, numShards, shardSize uint32,
) *LocalSyncingPeerProvider {
return &LocalSyncingPeerProvider{
basePort: basePort,
selfPort: selfPort,
numShards: numShards,
shardSize: shardSize,
}
}
// SyncingPeers returns local syncing peers using the sharding configuration.
func (p *LocalSyncingPeerProvider) SyncingPeers(shardID uint32) (peers []p2p.Peer, err error) {
if shardID >= p.numShards {
return nil, errors.Errorf(
"shard ID %d out of range 0..%d", shardID, p.numShards-1)
}
firstPort := uint32(p.basePort) + shardID
endPort := uint32(p.basePort) + p.numShards*p.shardSize
for port := firstPort; port < endPort; port += p.numShards {
if port == uint32(p.selfPort) {
continue // do not sync from self
}
peers = append(peers, p2p.Peer{IP: "127.0.0.1", Port: fmt.Sprint(port)})
}
return peers, nil
}
// doBeaconSyncing update received beaconchain blocks and downloads missing beacon chain blocks
func (node *Node) doBeaconSyncing() {
if node.NodeConfig.IsOffline {
return
}
if node.HarmonyConfig.General.RunElasticMode {
return
}
if !node.NodeConfig.Downloader {
// If Downloader is not working, we need also deal with blocks from beaconBlockChannel
go func(node *Node) {
// TODO ek – infinite loop; add shutdown/cleanup logic
for _ = range node.BeaconBlockChannel {
}
}(node)
}
// TODO ek – infinite loop; add shutdown/cleanup logic
for {
if node.epochSync == nil {
utils.Logger().Info().Msg("[EPOCHSYNC] initializing beacon sync")
node.epochSync = node.createStateSync(node.EpochChain()).IntoEpochSync()
}
peersCount := node.epochSync.GetActivePeerNumber()
if peersCount < legacysync.NumPeersLowBound {
utils.Logger().Warn().
Msgf("[EPOCHSYNC] num peers %d less than low bound %d; bootstrapping beacon sync config", peersCount, legacysync.NumPeersLowBound)
peers, err := node.SyncingPeerProvider.SyncingPeers(shard.BeaconChainShardID)
if err != nil {
utils.Logger().
Err(err).
Msg("[EPOCHSYNC] cannot retrieve beacon syncing peers")
continue
}
if err := node.epochSync.CreateSyncConfig(peers, shard.BeaconChainShardID); err != nil {
utils.Logger().Warn().Err(err).Msg("[EPOCHSYNC] cannot create beacon sync config")
continue
}
}
<-time.After(node.epochSync.SyncLoop(node.EpochChain(), true, nil))
}
}
// DoSyncing keep the node in sync with other peers, willJoinConsensus means the node will try to join consensus after catch up
func (node *Node) DoSyncing(bc core.BlockChain, worker *worker.Worker, willJoinConsensus bool) {
if node.NodeConfig.IsOffline {
return
}
ticker := time.NewTicker(SyncFrequency)
defer ticker.Stop()
// TODO ek – infinite loop; add shutdown/cleanup logic
for {
select {
case <-ticker.C:
node.doSync(bc, worker, willJoinConsensus)
case <-node.Consensus.BlockNumLowChan:
node.doSync(bc, worker, willJoinConsensus)
}
}
}
// doSync keep the node in sync with other peers, willJoinConsensus means the node will try to join consensus after catch up
func (node *Node) doSync(bc core.BlockChain, worker *worker.Worker, willJoinConsensus bool) {
if node.stateSync.GetActivePeerNumber() < legacysync.NumPeersLowBound {
shardID := bc.ShardID()
peers, err := node.SyncingPeerProvider.SyncingPeers(shardID)
if err != nil {
utils.Logger().Warn().
Err(err).
Uint32("shard_id", shardID).
Msg("cannot retrieve syncing peers")
return
}
if err := node.stateSync.CreateSyncConfig(peers, shardID); err != nil {
utils.Logger().Warn().
Err(err).
Interface("peers", peers).
Msg("[SYNC] create peers error")
return
}
utils.Logger().Debug().Int("len", node.stateSync.GetActivePeerNumber()).Msg("[SYNC] Get Active Peers")
}
// TODO: treat fake maximum height
if result := node.stateSync.GetSyncStatusDoubleChecked(); !result.IsInSync {
node.IsInSync.UnSet()
if willJoinConsensus {
node.Consensus.BlocksNotSynchronized()
}
node.stateSync.SyncLoop(bc, worker, false, node.Consensus)
if willJoinConsensus {
node.IsInSync.Set()
node.Consensus.BlocksSynchronized()
}
}
node.IsInSync.Set()
}
// SupportGRPCSyncServer do gRPC sync server
func (node *Node) SupportGRPCSyncServer(port int) {
node.InitSyncingServer(port)
node.StartSyncingServer(port)
}
// StartGRPCSyncClient start the legacy gRPC sync process
func (node *Node) StartGRPCSyncClient() {
if node.Blockchain().ShardID() != shard.BeaconChainShardID {
utils.Logger().Info().
Uint32("shardID", node.Blockchain().ShardID()).
Msg("SupportBeaconSyncing")
go node.doBeaconSyncing()
}
}
// NodeSyncing makes sure to start all the processes needed to sync the node based on different configuration factors.
func (node *Node) NodeSyncing() {
if node.HarmonyConfig.General.RunElasticMode {
node.syncFromTiKVWriter() // this is for both reader and backup writers
if node.HarmonyConfig.TiKV.Role == tikv.RoleReader {
node.Consensus.UpdateConsensusInformation()
}
if node.HarmonyConfig.TiKV.Role == tikv.RoleWriter {
node.supportSyncing() // the writer needs to be in sync with it's other peers
}
} else if !node.HarmonyConfig.General.IsOffline && node.HarmonyConfig.DNSSync.Client {
node.supportSyncing() // for non-writer-reader mode a.k.a tikv nodes
}
}
// supportSyncing keeps sleeping until it's doing consensus or it's a leader.
func (node *Node) supportSyncing() {
joinConsensus := false
// Check if the current node is explorer node.
switch node.NodeConfig.Role() {
case nodeconfig.Validator:
joinConsensus = true
}
// Send new block to unsync node if the current node is not explorer node.
// TODO: leo this pushing logic has to be removed
if joinConsensus {
go node.SendNewBlockToUnsync()
}
if node.stateSync == nil {
node.stateSync = node.createStateSync(node.Blockchain())
utils.Logger().Debug().Msg("[SYNC] initialized state sync")
}
go node.DoSyncing(node.Blockchain(), node.Worker, joinConsensus)
}
// InitSyncingServer starts downloader server.
func (node *Node) InitSyncingServer(port int) {
if node.downloaderServer == nil {
node.downloaderServer = legdownloader.NewServer(node, port)
}
}
// StartSyncingServer starts syncing server.
func (node *Node) StartSyncingServer(port int) {
utils.Logger().Info().Msg("[SYNC] support_syncing: StartSyncingServer")
if node.downloaderServer.GrpcServer == nil {
node.downloaderServer.Start()
}
}
// SendNewBlockToUnsync send latest verified block to unsync, registered nodes
func (node *Node) SendNewBlockToUnsync() {
for {
block := <-node.Consensus.VerifiedNewBlock
blockBytes, err := rlp.EncodeToBytes(block)
if err != nil {
utils.Logger().Warn().Msg("[SYNC] unable to encode block to hashes")
continue
}
blockWithSigBytes, err := node.getEncodedBlockWithSigFromBlock(block)
if err != nil {
utils.Logger().Warn().Err(err).Msg("[SYNC] rlp encode BlockWithSig")
continue
}
node.stateMutex.Lock()
for peerID, config := range node.peerRegistrationRecord {
elapseTime := time.Now().UnixNano() - config.timestamp
if elapseTime > broadcastTimeout {
utils.Logger().Warn().Str("peerID", peerID).Msg("[SYNC] SendNewBlockToUnsync to peer timeout")
node.peerRegistrationRecord[peerID].client.Close()
delete(node.peerRegistrationRecord, peerID)
continue
}
sendBytes := blockBytes
if config.withSig {
sendBytes = blockWithSigBytes
}
response, err := config.client.PushNewBlock(node.GetSyncID(), sendBytes, false)
// close the connection if cannot push new block to unsync node
if err != nil {
node.peerRegistrationRecord[peerID].client.Close()
delete(node.peerRegistrationRecord, peerID)
}
if response != nil && response.Type == downloader_pb.DownloaderResponse_INSYNC {
node.peerRegistrationRecord[peerID].client.Close()
delete(node.peerRegistrationRecord, peerID)
}
}
node.stateMutex.Unlock()
}
}
// CalculateResponse implements DownloadInterface on Node object.
func (node *Node) CalculateResponse(request *downloader_pb.DownloaderRequest, incomingPeer string) (*downloader_pb.DownloaderResponse, error) {
response := &downloader_pb.DownloaderResponse{}
if node.NodeConfig.IsOffline {
return response, nil
}
switch request.Type {
case downloader_pb.DownloaderRequest_BLOCKHASH:
dnsServerRequestCounterVec.With(dnsReqMetricLabel("block_hash")).Inc()
if request.BlockHash == nil {
return response, fmt.Errorf("[SYNC] GetBlockHashes Request BlockHash is NIL")
}
if request.Size == 0 || request.Size > legacysync.SyncLoopBatchSize {
return response, fmt.Errorf("[SYNC] GetBlockHashes Request contains invalid Size %v", request.Size)
}
size := uint64(request.Size)
var startHashHeader common.Hash
copy(startHashHeader[:], request.BlockHash[:])
startHeader := node.Blockchain().GetHeaderByHash(startHashHeader)
if startHeader == nil {
return response, fmt.Errorf("[SYNC] GetBlockHashes Request cannot find startHash %s", startHashHeader.Hex())
}
startHeight := startHeader.Number().Uint64()
endHeight := node.Blockchain().CurrentBlock().NumberU64()
if startHeight >= endHeight {
utils.Logger().
Debug().
Uint64("myHeight", endHeight).
Uint64("requestHeight", startHeight).
Str("incomingIP", request.Ip).
Str("incomingPort", request.Port).
Str("incomingPeer", incomingPeer).
Msg("[SYNC] GetBlockHashes Request: I am not higher than requested node")
return response, nil
}
for blockNum := startHeight; blockNum <= startHeight+size; blockNum++ {
header := node.Blockchain().GetHeaderByNumber(blockNum)
if header == nil {
break
}
blockHash := header.Hash()
response.Payload = append(response.Payload, blockHash[:])
}
case downloader_pb.DownloaderRequest_BLOCKHEADER:
dnsServerRequestCounterVec.With(dnsReqMetricLabel("block_header")).Inc()
var hash common.Hash
for _, bytes := range request.Hashes {
hash.SetBytes(bytes)
encodedBlockHeader, err := node.getEncodedBlockHeaderByHash(hash)
if err == nil {
response.Payload = append(response.Payload, encodedBlockHeader)
}
}
case downloader_pb.DownloaderRequest_BLOCK:
dnsServerRequestCounterVec.With(dnsReqMetricLabel("block")).Inc()
var hash common.Hash
payloadSize := 0
withSig := request.GetBlocksWithSig
for _, bytes := range request.Hashes {
hash.SetBytes(bytes)
var (
encoded []byte
err error
)
if withSig {
encoded, err = node.getEncodedBlockWithSigByHash(hash)
} else {
encoded, err = node.getEncodedBlockByHash(hash)
}
if err != nil {
continue
}
payloadSize += len(encoded)
if payloadSize > getBlocksRequestHardCap {
utils.Logger().Warn().Err(err).
Int("req size", len(request.Hashes)).
Int("cur size", len(response.Payload)).
Msg("[SYNC] Max blocks response size reached, ignoring the rest.")
break
}
response.Payload = append(response.Payload, encoded)
}
case downloader_pb.DownloaderRequest_BLOCKHEIGHT:
dnsServerRequestCounterVec.With(dnsReqMetricLabel("block_height")).Inc()
response.BlockHeight = node.Blockchain().CurrentBlock().NumberU64()
// this is the out of sync node acts as grpc server side
case downloader_pb.DownloaderRequest_NEWBLOCK:
dnsServerRequestCounterVec.With(dnsReqMetricLabel("new block")).Inc()
if node.IsInSync.IsSet() {
response.Type = downloader_pb.DownloaderResponse_INSYNC
return response, nil
}
block, err := legacysync.RlpDecodeBlockOrBlockWithSig(request.BlockHash)
if err != nil {
utils.Logger().Warn().Err(err).Msg("[SYNC] unable to decode received new block")
return response, err
}
node.stateSync.AddNewBlock(request.PeerHash, block)
case downloader_pb.DownloaderRequest_REGISTER:
peerID := string(request.PeerHash[:])
ip := request.Ip
port := request.Port
withSig := request.RegisterWithSig
node.stateMutex.Lock()
defer node.stateMutex.Unlock()
if _, ok := node.peerRegistrationRecord[peerID]; ok {
response.Type = downloader_pb.DownloaderResponse_FAIL
utils.Logger().Warn().
Interface("ip", ip).
Interface("port", port).
Msg("[SYNC] peerRegistration record already exists")
return response, nil
} else if len(node.peerRegistrationRecord) >= maxBroadcastNodes {
response.Type = downloader_pb.DownloaderResponse_FAIL
utils.Logger().Debug().
Str("ip", ip).
Str("port", port).
Msg("[SYNC] maximum registration limit exceeds")
return response, nil
} else {
response.Type = downloader_pb.DownloaderResponse_FAIL
syncPort := legacysync.GetSyncingPort(port)
client := legdownloader.ClientSetup(ip, syncPort)
if client == nil {
utils.Logger().Warn().
Str("ip", ip).
Str("port", port).
Msg("[SYNC] unable to setup client for peerID")
return response, nil
}
config := &syncConfig{timestamp: time.Now().UnixNano(), client: client, withSig: withSig}
node.peerRegistrationRecord[peerID] = config
utils.Logger().Debug().
Str("ip", ip).
Str("port", port).
Msg("[SYNC] register peerID success")
response.Type = downloader_pb.DownloaderResponse_SUCCESS
}
case downloader_pb.DownloaderRequest_REGISTERTIMEOUT:
if !node.IsInSync.IsSet() {
count := node.stateSync.RegisterNodeInfo()
utils.Logger().Debug().
Int("number", count).
Msg("[SYNC] extra node registered")
}
case downloader_pb.DownloaderRequest_BLOCKBYHEIGHT:
if len(request.Heights) == 0 {
return response, errors.New("empty heights list provided")
}
if len(request.Heights) > int(legacysync.SyncLoopBatchSize) {
return response, errors.New("exceed size limit")
}
out := make([][]byte, 0, len(request.Heights))
for _, v := range request.Heights {
block := node.Blockchain().GetBlockByNumber(v)
if block == nil {
return response, errors.Errorf("no block with height %d found", v)
}
blockBytes, err := node.getEncodedBlockWithSigByHeight(v)
if err != nil {
return response, errors.Errorf("failed to get block")
}
out = append(out, blockBytes)
}
response.Payload = out
}
return response, nil
}
func init() {
prom.PromRegistry().MustRegister(
dnsServerRequestCounterVec,
)
}
var (
dnsServerRequestCounterVec = prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: "psc",
Subsystem: "dns_server",
Name: "request_count",
Help: "request count for each dns request",
},
[]string{"method"},
)
)
func dnsReqMetricLabel(method string) prometheus.Labels {
return prometheus.Labels{
"method": method,
}
}
const (
headerCacheSize = 10000
blockCacheSize = 10000
blockWithSigCacheSize = 10000
)
var (
// Cached fields for block header and block requests
headerReqCache, _ = lru.New(headerCacheSize)
blockReqCache, _ = lru.New(blockCacheSize)
blockWithSigReqCache, _ = lru.New(blockWithSigCacheSize)
errHeaderNotExist = errors.New("header not exist")
errBlockNotExist = errors.New("block not exist")
)
func (node *Node) getEncodedBlockHeaderByHash(hash common.Hash) ([]byte, error) {
if b, ok := headerReqCache.Get(hash); ok {
return b.([]byte), nil
}
h := node.Blockchain().GetHeaderByHash(hash)
if h == nil {
return nil, errHeaderNotExist
}
b, err := rlp.EncodeToBytes(h)
if err != nil {
return nil, err
}
headerReqCache.Add(hash, b)
return b, nil
}
func (node *Node) getEncodedBlockByHash(hash common.Hash) ([]byte, error) {
if b, ok := blockReqCache.Get(hash); ok {
return b.([]byte), nil
}
blk := node.Blockchain().GetBlockByHash(hash)
if blk == nil {
return nil, errBlockNotExist
}
b, err := rlp.EncodeToBytes(blk)
if err != nil {
return nil, err
}
blockReqCache.Add(hash, b)
return b, nil
}
func (node *Node) getEncodedBlockWithSigByHash(hash common.Hash) ([]byte, error) {
if b, ok := blockWithSigReqCache.Get(hash); ok {
return b.([]byte), nil
}
blk := node.Blockchain().GetBlockByHash(hash)
if blk == nil {
return nil, errBlockNotExist
}
sab, err := node.getCommitSigAndBitmapFromChildOrDB(blk)
if err != nil {
return nil, err
}
bwh := legacysync.BlockWithSig{
Block: blk,
CommitSigAndBitmap: sab,
}
b, err := rlp.EncodeToBytes(bwh)
if err != nil {
return nil, err
}
blockWithSigReqCache.Add(hash, b)
return b, nil
}
func (node *Node) getEncodedBlockWithSigByHeight(height uint64) ([]byte, error) {
blk := node.Blockchain().GetBlockByNumber(height)
if blk == nil {
return nil, errBlockNotExist
}
sab, err := node.getCommitSigAndBitmapFromChildOrDB(blk)
if err != nil {
return nil, err
}
bwh := legacysync.BlockWithSig{
Block: blk,
CommitSigAndBitmap: sab,
}
b, err := rlp.EncodeToBytes(bwh)
if err != nil {
return nil, err
}
return b, nil
}
func (node *Node) getEncodedBlockWithSigFromBlock(block *types.Block) ([]byte, error) {
bwh := legacysync.BlockWithSig{
Block: block,
CommitSigAndBitmap: block.GetCurrentCommitSig(),
}
return rlp.EncodeToBytes(bwh)
}
func (node *Node) getCommitSigAndBitmapFromChildOrDB(block *types.Block) ([]byte, error) {
child := node.Blockchain().GetBlockByNumber(block.NumberU64() + 1)
if child != nil {
return node.getCommitSigFromChild(block, child)
}
return node.getCommitSigFromDB(block)
}
func (node *Node) getCommitSigFromChild(parent, child *types.Block) ([]byte, error) {
if child.ParentHash() != parent.Hash() {
return nil, fmt.Errorf("child's parent hash unexpected: %v / %v",
child.ParentHash().String(), parent.Hash().String())
}
sig := child.Header().LastCommitSignature()
bitmap := child.Header().LastCommitBitmap()
return append(sig[:], bitmap...), nil
}
func (node *Node) getCommitSigFromDB(block *types.Block) ([]byte, error) {
return node.Blockchain().ReadCommitSig(block.NumberU64())
}
// SyncStatus return the syncing status, including whether node is syncing
// and the target block number, and the difference between current block
// and target block.
func (node *Node) SyncStatus(shardID uint32) (bool, uint64, uint64) {
if node.NodeConfig.Role() == nodeconfig.ExplorerNode {
exp, err := node.getExplorerService()
if err != nil {
// unreachable code
return false, 0, largeNumberDiff
}
if !exp.IsAvailable() {
return false, 0, largeNumberDiff
}
}
ds := node.getDownloaders()
if ds == nil || !ds.IsActive() {
// downloaders inactive. Ask DNS sync instead
return node.legacySyncStatus(shardID)
}
return ds.SyncStatus(shardID)
}
func (node *Node) legacySyncStatus(shardID uint32) (bool, uint64, uint64) {
switch shardID {
case node.NodeConfig.ShardID:
if node.stateSync == nil {
return false, 0, 0
}
result := node.stateSync.GetSyncStatus()
return result.IsInSync, result.OtherHeight, result.HeightDiff
case shard.BeaconChainShardID:
if node.epochSync == nil {
return false, 0, 0
}
result := node.epochSync.GetSyncStatus()
return result.IsInSync, result.OtherHeight, result.HeightDiff
default:
// Shard node is not working on
return false, 0, 0
}
}
// IsOutOfSync return whether the node is out of sync of the given hsardID
func (node *Node) IsOutOfSync(shardID uint32) bool {
ds := node.getDownloaders()
if ds == nil || !ds.IsActive() {
// downloaders inactive. Ask DNS sync instead
return node.legacyIsOutOfSync(shardID)
}
isSyncing, _, _ := ds.SyncStatus(shardID)
return !isSyncing
}
func (node *Node) legacyIsOutOfSync(shardID uint32) bool {
switch shardID {
case node.NodeConfig.ShardID:
if node.stateSync == nil {
return true
}
result := node.stateSync.GetSyncStatus()
return !result.IsInSync
case shard.BeaconChainShardID:
if node.epochSync == nil {
return true
}
result := node.epochSync.GetSyncStatus()
return !result.IsInSync
default:
return true
}
}
// SyncPeers return connected sync peers for each shard
func (node *Node) SyncPeers() map[string]int {
ds := node.getDownloaders()
if ds == nil {
return nil
}
nums := ds.NumPeers()
res := make(map[string]int)
for sid, num := range nums {
s := fmt.Sprintf("shard-%v", sid)
res[s] = num
}
return res
}
func (node *Node) getDownloaders() *downloader.Downloaders {
syncService := node.serviceManager.GetService(service.Synchronize)
if syncService == nil {
return nil
}
dsService, ok := syncService.(*synchronize.Service)
if !ok {
return nil
}
return dsService.Downloaders
}