From 0a56d3ddbce2a81bed482d619a95d86a25dff425 Mon Sep 17 00:00:00 2001 From: Evgeniy Kulikov Date: Fri, 15 Nov 2019 20:42:23 +0300 Subject: [PATCH] network: generate randomized server id math/rand might generate same id's on one environment, so.. use crypto/rand for generation id's --- pkg/network/server.go | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/pkg/network/server.go b/pkg/network/server.go index 79027a3c1..edffdc0cc 100644 --- a/pkg/network/server.go +++ b/pkg/network/server.go @@ -1,9 +1,10 @@ package network import ( + "crypto/rand" + "encoding/binary" "errors" "fmt" - "math/rand" "net" "strconv" "sync" @@ -68,13 +69,19 @@ type ( } ) +func randomID() uint32 { + buf := make([]byte, 4) + _, _ = rand.Read(buf) + return binary.BigEndian.Uint32(buf) +} + // NewServer returns a new Server, initialized with the given configuration. func NewServer(config ServerConfig, chain core.Blockchainer) *Server { s := &Server{ ServerConfig: config, chain: chain, bQueue: newBlockQueue(maxBlockBatch, chain), - id: rand.Uint32(), + id: randomID(), quit: make(chan struct{}), addrReq: make(chan *Message, config.MinPeers), register: make(chan Peer), @@ -536,7 +543,3 @@ func (s *Server) RelayDirectly(p Peer, inv *payload.Inventory) { p.WriteMsg(NewMessage(s.Net, CMDInv, inv)) } - -func init() { - rand.Seed(time.Now().UTC().UnixNano()) -}