From: HAOYUatHZ <37070449+HAOYUatHZ@users.noreply.github.com> Date: Sun, 2 Sep 2018 14:00:55 +0000 (+0800) Subject: Fix (#1310) X-Git-Tag: v1.0.6rc1~4^2 X-Git-Url: http://git.osdn.net/view?a=commitdiff_plain;h=e9ca8db92bfe875c8eea2fb873b38fbd5aaa2018;p=bytom%2Fbytom.git Fix (#1310) * Avoid copying arrays in loops * Add delay to refresh cycle when no seed nodes are found --- diff --git a/p2p/discover/net.go b/p2p/discover/net.go index 0010a52f..ef43e9c9 100644 --- a/p2p/discover/net.go +++ b/p2p/discover/net.go @@ -657,7 +657,7 @@ func (net *Network) refresh(done chan<- struct{}) { } if len(seeds) == 0 { log.Debug("no seed nodes found") - close(done) + time.AfterFunc(time.Second*10, func() { close(done) }) return } for _, n := range seeds { diff --git a/p2p/discover/table.go b/p2p/discover/table.go index a4d4e217..8012c3fe 100644 --- a/p2p/discover/table.go +++ b/p2p/discover/table.go @@ -68,7 +68,7 @@ func (tab *Table) chooseBucketRefreshTarget() common.Hash { fmt.Println() fmt.Println("self ", "id:", tab.self.ID, " hex:", crypto.Sha256Hash(tab.self.ID[:]).Hex()) } - for i, b := range tab.buckets { + for i, b := range &tab.buckets { entries += len(b.entries) if printTable { for _, e := range b.entries { @@ -80,7 +80,7 @@ func (tab *Table) chooseBucketRefreshTarget() common.Hash { prefix := binary.BigEndian.Uint64(tab.self.sha[0:8]) dist := ^uint64(0) entry := int(randUint(uint32(entries + 1))) - for _, b := range tab.buckets { + for _, b := range &tab.buckets { if entry < len(b.entries) { n := b.entries[entry] dist = binary.BigEndian.Uint64(n.sha[0:8]) ^ prefix @@ -108,7 +108,7 @@ func (tab *Table) readRandomNodes(buf []*Node) (n int) { // TODO: tree-based buckets would help here // Find all non-empty buckets and get a fresh slice of their entries. var buckets [][]*Node - for _, b := range tab.buckets { + for _, b := range &tab.buckets { if len(b.entries) > 0 { buckets = append(buckets, b.entries[:]) } @@ -163,7 +163,7 @@ func (tab *Table) closest(target common.Hash, nresults int) *nodesByDistance { // obviously correct. I believe that tree-based buckets would make // this easier to implement efficiently. close := &nodesByDistance{target: target} - for _, b := range tab.buckets { + for _, b := range &tab.buckets { for _, n := range b.entries { close.push(n, nresults) }