OSDN Git Service

Fix (#1310)
authorHAOYUatHZ <37070449+HAOYUatHZ@users.noreply.github.com>
Sun, 2 Sep 2018 14:00:55 +0000 (22:00 +0800)
committerPaladz <yzhu101@uottawa.ca>
Sun, 2 Sep 2018 14:00:55 +0000 (22:00 +0800)
* Avoid copying arrays in loops

* Add delay to refresh cycle when no seed nodes are found

p2p/discover/net.go
p2p/discover/table.go

index 0010a52..ef43e9c 100644 (file)
@@ -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 {
index a4d4e21..8012c3f 100644 (file)
@@ -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)
                }