network: generate randomized server id
math/rand might generate same id's on one environment, so.. use crypto/rand for generation id's
This commit is contained in:
parent
f640dbb331
commit
0a56d3ddbc
1 changed files with 9 additions and 6 deletions
|
@ -1,9 +1,10 @@
|
||||||
package network
|
package network
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"crypto/rand"
|
||||||
|
"encoding/binary"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"math/rand"
|
|
||||||
"net"
|
"net"
|
||||||
"strconv"
|
"strconv"
|
||||||
"sync"
|
"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.
|
// NewServer returns a new Server, initialized with the given configuration.
|
||||||
func NewServer(config ServerConfig, chain core.Blockchainer) *Server {
|
func NewServer(config ServerConfig, chain core.Blockchainer) *Server {
|
||||||
s := &Server{
|
s := &Server{
|
||||||
ServerConfig: config,
|
ServerConfig: config,
|
||||||
chain: chain,
|
chain: chain,
|
||||||
bQueue: newBlockQueue(maxBlockBatch, chain),
|
bQueue: newBlockQueue(maxBlockBatch, chain),
|
||||||
id: rand.Uint32(),
|
id: randomID(),
|
||||||
quit: make(chan struct{}),
|
quit: make(chan struct{}),
|
||||||
addrReq: make(chan *Message, config.MinPeers),
|
addrReq: make(chan *Message, config.MinPeers),
|
||||||
register: make(chan Peer),
|
register: make(chan Peer),
|
||||||
|
@ -536,7 +543,3 @@ func (s *Server) RelayDirectly(p Peer, inv *payload.Inventory) {
|
||||||
p.WriteMsg(NewMessage(s.Net, CMDInv, inv))
|
p.WriteMsg(NewMessage(s.Net, CMDInv, inv))
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
|
||||||
rand.Seed(time.Now().UTC().UnixNano())
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in a new issue