Skip to content

Fix issue with 32bit architecture on big numbers' random generation #1494

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions core/blockchainsync/downloadBlockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ func (bd *BlockchainDownloader) DownloadFromPeer(feederPeer *model.Peer, chainBl
}

monitoring.IncrementMainchainDownloadCycleDebugger(bd.ChainType, 51)
initialPeerIdx := int(commonUtil.GetSecurePositiveRandom()) % len(peersSlice)
initialPeerIdx := uint64(commonUtil.GetSecurePositiveRandom()) % uint64(len(peersSlice))
nextPeerIdx := initialPeerIdx
peerUsed := feederPeer
blocksSegments := [][]*model.Block{}
Expand All @@ -326,7 +326,7 @@ func (bd *BlockchainDownloader) DownloadFromPeer(feederPeer *model.Peer, chainBl
if start != uint32(0) {
peerUsed = peersSlice[nextPeerIdx]
nextPeerIdx++
if nextPeerIdx >= len(peersSlice) {
if nextPeerIdx >= uint64(len(peersSlice)) {
nextPeerIdx = 0
}
if nextPeerIdx == initialPeerIdx {
Expand Down
14 changes: 7 additions & 7 deletions p2p/strategy/nativeStrategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,11 +305,11 @@ func (ns *NativeStrategy) GetAnyResolvedPeer() *model.Peer {
if len(resolvedPeers) < 1 {
return nil
}
randomIdx := int(util.GetSecurePositiveRandom())
randomIdx := uint64(util.GetSecurePositiveRandom())
if randomIdx != 0 {
randomIdx %= len(resolvedPeers)
randomIdx %= uint64(len(resolvedPeers))
}
idx := 0
var idx uint64
for _, peer := range resolvedPeers {
if idx == randomIdx {
return peer
Expand Down Expand Up @@ -365,8 +365,8 @@ func (ns *NativeStrategy) GetAnyUnresolvedPeer() *model.Peer {
if len(unresolvedPeers) < 1 {
return nil
}
randomIdx := int(util.GetSecurePositiveRandom()) % len(unresolvedPeers)
idx := 0
randomIdx := uint64(util.GetSecurePositiveRandom()) % uint64(len(unresolvedPeers))
var idx uint64
for _, peer := range unresolvedPeers {
if idx == randomIdx {
return peer
Expand Down Expand Up @@ -493,8 +493,8 @@ func (ns *NativeStrategy) GetAnyKnownPeer() *model.Peer {
if len(knownPeers) < 1 {
panic("No well known peer is found")
}
randomIdx := int(util.GetSecurePositiveRandom()) % len(knownPeers)
idx := 0
randomIdx := uint64(util.GetSecurePositiveRandom()) % uint64(len(knownPeers))
var idx uint64
for _, peer := range knownPeers {
if idx == randomIdx {
return peer
Expand Down
4 changes: 2 additions & 2 deletions p2p/strategy/peerStrategyHelper.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ func NewPeerStrategyHelper() *PeerStrategyHelper {
func (ps *PeerStrategyHelper) GetRandomPeerWithoutRepetition(peers map[string]*model.Peer, mutex *sync.Mutex) *model.Peer {
var (
peer *model.Peer
idx uint64
)
randomIdx := int(util.GetSecurePositiveRandom()) % len(peers)
idx := 0
randomIdx := uint64(util.GetSecurePositiveRandom()) % uint64(len(peers))
for _, knownPeer := range peers {
if idx == randomIdx {
peer = knownPeer
Expand Down
14 changes: 7 additions & 7 deletions p2p/strategy/priorityStrategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -968,11 +968,11 @@ func (ps *PriorityStrategy) GetAnyResolvedPeer() *model.Peer {
if lengthResolvoedPeers < 1 {
return nil
}
randomIdx := int(util.GetSecurePositiveRandom())
randomIdx := uint64(util.GetSecurePositiveRandom())
if randomIdx != 0 {
randomIdx %= lengthResolvoedPeers
randomIdx %= uint64(lengthResolvoedPeers)
}
idx := 0
var idx uint64
for _, peer := range ps.NodeConfigurationService.GetHost().ResolvedPeers {
if idx == randomIdx {
return peer
Expand Down Expand Up @@ -1106,8 +1106,8 @@ func (ps *PriorityStrategy) GetAnyUnresolvedPeer() *model.Peer {
if len(unresolvedPeers) < 1 {
return nil
}
randomIdx := int(util.GetSecurePositiveRandom()) % len(unresolvedPeers)
idx := 0
randomIdx := uint64(util.GetSecurePositiveRandom()) % uint64(len(unresolvedPeers))
var idx uint64
for _, peer := range unresolvedPeers {
if idx == randomIdx {
return peer
Expand Down Expand Up @@ -1310,8 +1310,8 @@ func (ps *PriorityStrategy) GetAnyKnownPeer() *model.Peer {
if len(knownPeers) < 1 {
panic("No well known peer is found")
}
randomIdx := int(util.GetSecurePositiveRandom()) % len(knownPeers)
idx := 0
randomIdx := uint64(util.GetSecurePositiveRandom()) % uint64(len(knownPeers))
var idx uint64
for _, peer := range knownPeers {
if idx == randomIdx {
return peer
Expand Down