mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-21 20:26:41 +00:00
p2p/discover: using map[enode.ID]struct{} instead of map[enode.ID]bool
This commit is contained in:
parent
127d1f42bb
commit
ab0a8096d4
1 changed files with 9 additions and 9 deletions
|
|
@ -33,7 +33,7 @@ type lookup struct {
|
||||||
queryfunc queryFunc
|
queryfunc queryFunc
|
||||||
replyCh chan []*enode.Node
|
replyCh chan []*enode.Node
|
||||||
cancelCh <-chan struct{}
|
cancelCh <-chan struct{}
|
||||||
asked, seen map[enode.ID]bool
|
asked, seen map[enode.ID]struct{}
|
||||||
result nodesByDistance
|
result nodesByDistance
|
||||||
replyBuffer []*enode.Node
|
replyBuffer []*enode.Node
|
||||||
queries int
|
queries int
|
||||||
|
|
@ -45,16 +45,16 @@ func newLookup(ctx context.Context, tab *Table, target enode.ID, q queryFunc) *l
|
||||||
it := &lookup{
|
it := &lookup{
|
||||||
tab: tab,
|
tab: tab,
|
||||||
queryfunc: q,
|
queryfunc: q,
|
||||||
asked: make(map[enode.ID]bool),
|
asked: make(map[enode.ID]struct{}),
|
||||||
seen: make(map[enode.ID]bool),
|
seen: make(map[enode.ID]struct{}),
|
||||||
result: nodesByDistance{target: target},
|
result: nodesByDistance{target: target},
|
||||||
replyCh: make(chan []*enode.Node, alpha),
|
replyCh: make(chan []*enode.Node, alpha),
|
||||||
cancelCh: ctx.Done(),
|
cancelCh: ctx.Done(),
|
||||||
}
|
}
|
||||||
// Don't query further if we hit ourself.
|
// Don't query further if we hit ourself.
|
||||||
// Unlikely to happen often in practice.
|
// Unlikely to happen often in practice.
|
||||||
it.asked[tab.self().ID()] = true
|
it.asked[tab.self().ID()] = struct{}{}
|
||||||
it.seen[tab.self().ID()] = true
|
it.seen[tab.self().ID()] = struct{}{}
|
||||||
|
|
||||||
// Initialize the lookup with nodes from table.
|
// Initialize the lookup with nodes from table.
|
||||||
closest := it.tab.findnodeByID(it.result.target, bucketSize, false)
|
closest := it.tab.findnodeByID(it.result.target, bucketSize, false)
|
||||||
|
|
@ -94,8 +94,8 @@ func (it *lookup) advance() bool {
|
||||||
func (it *lookup) addNodes(nodes []*enode.Node) {
|
func (it *lookup) addNodes(nodes []*enode.Node) {
|
||||||
it.replyBuffer = it.replyBuffer[:0]
|
it.replyBuffer = it.replyBuffer[:0]
|
||||||
for _, n := range nodes {
|
for _, n := range nodes {
|
||||||
if n != nil && !it.seen[n.ID()] {
|
if _, ok := it.seen[n.ID()]; n != nil && !ok {
|
||||||
it.seen[n.ID()] = true
|
it.seen[n.ID()] = struct{}{}
|
||||||
it.result.push(n, bucketSize)
|
it.result.push(n, bucketSize)
|
||||||
it.replyBuffer = append(it.replyBuffer, n)
|
it.replyBuffer = append(it.replyBuffer, n)
|
||||||
}
|
}
|
||||||
|
|
@ -119,8 +119,8 @@ func (it *lookup) startQueries() bool {
|
||||||
// Ask the closest nodes that we haven't asked yet.
|
// Ask the closest nodes that we haven't asked yet.
|
||||||
for i := 0; i < len(it.result.entries) && it.queries < alpha; i++ {
|
for i := 0; i < len(it.result.entries) && it.queries < alpha; i++ {
|
||||||
n := it.result.entries[i]
|
n := it.result.entries[i]
|
||||||
if !it.asked[n.ID()] {
|
if _, ok := it.asked[n.ID()]; ok {
|
||||||
it.asked[n.ID()] = true
|
it.asked[n.ID()] = struct{}{}
|
||||||
it.queries++
|
it.queries++
|
||||||
go it.query(n, it.replyCh)
|
go it.query(n, it.replyCh)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue