Browse Source

p2p: make Switch.DialSeeds use a new PRNG per call

Fixes https://github.com/tendermint/tendermint/issues/875

Ensure that every DialSeeds call uses a new PRNG seeded from
tendermint/tmlibs/common.RandInt which internally uses
crypto/rand to seed its source.
pull/877/head
Emmanuel Odeke 7 years ago
parent
commit
031e10133c
No known key found for this signature in database GPG Key ID: 1CA47A292F89DD40
1 changed files with 6 additions and 3 deletions
  1. +6
    -3
      p2p/switch.go

+ 6
- 3
p2p/switch.go View File

@ -295,7 +295,6 @@ func (sw *Switch) startInitPeer(peer *peer) {
// DialSeeds dials a list of seeds asynchronously in random order. // DialSeeds dials a list of seeds asynchronously in random order.
func (sw *Switch) DialSeeds(addrBook *AddrBook, seeds []string) error { func (sw *Switch) DialSeeds(addrBook *AddrBook, seeds []string) error {
netAddrs, err := NewNetAddressStrings(seeds) netAddrs, err := NewNetAddressStrings(seeds)
if err != nil { if err != nil {
return err return err
@ -315,11 +314,15 @@ func (sw *Switch) DialSeeds(addrBook *AddrBook, seeds []string) error {
addrBook.Save() addrBook.Save()
} }
// Ensure we have a completely undeterministic PRNG. cmd.RandInt64() draws
// from a seed that's initialized with OS entropy on process start.
rng := rand.New(rand.NewSource(cmn.RandInt64()))
// permute the list, dial them in random order. // permute the list, dial them in random order.
perm := rand.Perm(len(netAddrs))
perm := rng.Perm(len(netAddrs))
for i := 0; i < len(perm); i++ { for i := 0; i < len(perm); i++ {
go func(i int) { go func(i int) {
time.Sleep(time.Duration(rand.Int63n(3000)) * time.Millisecond)
time.Sleep(time.Duration(rng.Int63n(3000)) * time.Millisecond)
j := perm[i] j := perm[i]
sw.dialSeed(netAddrs[j]) sw.dialSeed(netAddrs[j])
}(i) }(i)


Loading…
Cancel
Save