Skip to content

Commit

Permalink
just query peers in local routing table
Browse files Browse the repository at this point in the history
  • Loading branch information
yxq committed Jun 29, 2021
1 parent 679344d commit c520313
Showing 1 changed file with 2 additions and 28 deletions.
30 changes: 2 additions & 28 deletions system/p2p/dht/protocol/p2pstore/refresh.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"time"

"github.com/33cn/chain33/types"
"github.com/libp2p/go-libp2p-core/peer"
kbt "github.com/libp2p/go-libp2p-kbucket"
)

Expand Down Expand Up @@ -87,16 +86,14 @@ func (p *Protocol) updateExtendRoutingTable() {
_, _ = p.extendRoutingTable.TryAddPeer(pid, true, true)
}
if key != nil {
peers = p.RoutingTable.NearestPeers(genDHTID(key), backup-1)
peers = p.RoutingTable.NearestPeers(genDHTID(key), backup)
}

searchedPeers := make(map[peer.ID]struct{})
for i, pid := range peers {
// 保证 extendRoutingTable 至少有 300 个节点,且至少从 3 个节点上获取新节点,
// 至少从 3 个节点上获取新节点,
if i+1 > 3 && p.extendRoutingTable.Size() > p.SubConfig.MaxExtendRoutingTableSize {
break
}
searchedPeers[pid] = struct{}{}
extendPeers, err := p.fetchShardPeers(key, count, pid)
if err != nil {
log.Error("updateExtendRoutingTable", "fetchShardPeers error", err, "peer id", pid)
Expand All @@ -109,28 +106,5 @@ func (p *Protocol) updateExtendRoutingTable() {
_, _ = p.extendRoutingTable.TryAddPeer(cPid, true, true)
}
}

// 如果扩展路由表节点数小于200,则迭代查询增加节点
var lastSize int //如果经过一轮迭代节点数没有增加则结束迭代,防止节点数不到200导致无法退出
for p.extendRoutingTable.Size() < p.SubConfig.MinExtendRoutingTableSize && p.extendRoutingTable.Size() > lastSize {
lastSize = p.extendRoutingTable.Size()
for _, pid := range p.extendRoutingTable.ListPeers() {
if _, ok := searchedPeers[pid]; ok {
continue
}
searchedPeers[pid] = struct{}{}
closerPeers, err := p.fetchShardPeers(key, count, pid)
if err != nil {
log.Error("updateExtendRoutingTable", "fetchShardPeers error", err, "peer id", pid)
continue
}
for _, cPid := range closerPeers {
if cPid == p.Host.ID() {
continue
}
_, _ = p.extendRoutingTable.TryAddPeer(cPid, true, true)
}
}
}
log.Info("updateExtendRoutingTable", "pid", p.Host.ID(), "local peers count", p.RoutingTable.Size(), "extendRoutingTable peer count", p.extendRoutingTable.Size(), "time cost", time.Since(start), "origin count", count)
}

0 comments on commit c520313

Please sign in to comment.