-
Notifications
You must be signed in to change notification settings - Fork 2
/
helpers.go
100 lines (90 loc) · 2.76 KB
/
helpers.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
package legacysync
import (
"fmt"
"sync"
"time"
"github.com/PositionExchange/posichain/api/service/legacysync/downloader"
"github.com/PositionExchange/posichain/internal/utils"
"github.com/PositionExchange/posichain/p2p"
"github.com/ethereum/go-ethereum/common/math"
"github.com/pkg/errors"
)
// getMaxPeerHeight gets the maximum blockchain heights from peers
func getMaxPeerHeight(syncConfig *SyncConfig) uint64 {
maxHeight := uint64(math.MaxUint64)
var (
wg sync.WaitGroup
lock sync.Mutex
)
syncConfig.ForEachPeer(func(peerConfig *SyncPeerConfig) (brk bool) {
wg.Add(1)
go func() {
defer wg.Done()
//debug
// utils.Logger().Debug().Bool("isBeacon", isBeacon).Str("peerIP", peerConfig.ip).Str("peerPort", peerConfig.port).Msg("[Sync]getMaxPeerHeight")
response, err := peerConfig.client.GetBlockChainHeight()
if err != nil {
utils.Logger().Warn().Err(err).Str("peerIP", peerConfig.ip).Str("peerPort", peerConfig.port).Msg("[Sync]GetBlockChainHeight failed")
syncConfig.RemovePeer(peerConfig, fmt.Sprintf("failed getMaxPeerHeight for shard %d with message: %s", syncConfig.ShardID(), err.Error()))
return
}
utils.Logger().Info().Str("peerIP", peerConfig.ip).Uint64("blockHeight", response.BlockHeight).
Msg("[SYNC] getMaxPeerHeight")
lock.Lock()
if response != nil {
if response.BlockHeight < math.MaxUint32 { // That's enough for decades.
if maxHeight == uint64(math.MaxUint64) || maxHeight < response.BlockHeight {
maxHeight = response.BlockHeight
}
}
}
lock.Unlock()
}()
return
})
wg.Wait()
return maxHeight
}
func createSyncConfig(syncConfig *SyncConfig, peers []p2p.Peer, shardID uint32) (*SyncConfig, error) {
// sanity check to ensure no duplicate peers
if err := checkPeersDuplicity(peers); err != nil {
return syncConfig, err
}
// limit the number of dns peers to connect
randSeed := time.Now().UnixNano()
peers = limitNumPeers(peers, randSeed)
utils.Logger().Debug().
Int("len", len(peers)).
Uint32("shardID", shardID).
Msg("[SYNC] CreateSyncConfig: len of peers")
if len(peers) == 0 {
return syncConfig, errors.New("[SYNC] no peers to connect to")
}
if syncConfig != nil {
syncConfig.CloseConnections()
}
syncConfig = NewSyncConfig(shardID, nil)
var wg sync.WaitGroup
for _, peer := range peers {
wg.Add(1)
go func(peer p2p.Peer) {
defer wg.Done()
client := downloader.ClientSetup(peer.IP, peer.Port)
if client == nil {
return
}
peerConfig := &SyncPeerConfig{
ip: peer.IP,
port: peer.Port,
client: client,
}
syncConfig.AddPeer(peerConfig)
}(peer)
}
wg.Wait()
utils.Logger().Info().
Int("len", len(syncConfig.peers)).
Uint32("shardID", shardID).
Msg("[SYNC] Finished making connection to peers")
return syncConfig, nil
}