Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

p2p/discover: fix handling of distance 256 in lookupDistances #26087

Merged
merged 4 commits into from
Nov 2, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion p2p/discover/v5_udp.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ func lookupDistances(target, dest enode.ID) (dists []uint) {
td := enode.LogDist(target, dest)
dists = append(dists, uint(td))
for i := 1; len(dists) < lookupRequestLimit; i++ {
if td+i < 256 {
if td+i <= 256 {
dists = append(dists, uint(td+i))
}
if td-i > 0 {
Expand Down
7 changes: 7 additions & 0 deletions p2p/discover/v5_udp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,13 @@ func TestUDPv5_lookup(t *testing.T) {
t.Parallel()
test := newUDPV5Test(t)

// Ensure lookupDistances includes 256 when necessary.
node255 := nodesAtDistance(test.table.self().ID(), 255, 1)[0]
jtraglia marked this conversation as resolved.
Show resolved Hide resolved
dists := lookupDistances(test.table.self().ID(), node255.ID())
if !containsUint(256, dists) {
t.Errorf("lookup distances does not contain 256: %v", dists)
}

// Lookup on empty table returns no nodes.
if results := test.udp.Lookup(lookupTestnet.target.id()); len(results) > 0 {
t.Fatalf("lookup on empty table returned %d results: %#v", len(results), results)
Expand Down
4 changes: 2 additions & 2 deletions p2p/discover/v5wire/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ type (
handshakeAuthData struct {
h struct {
SrcID enode.ID
SigSize byte // ignature data
SigSize byte // signature data
PubkeySize byte // offset of
}
// Trailing variable-size data.
Expand Down Expand Up @@ -529,7 +529,7 @@ func (c *Codec) decodeHandshake(fromAddr string, head *Header) (n *enode.Node, a
if err != nil {
return nil, auth, nil, errInvalidAuthKey
}
// Derive sesssion keys.
// Derive session keys.
session := deriveKeys(sha256.New, c.privkey, ephkey, auth.h.SrcID, c.localnode.ID(), cdata)
session = session.keysFlipped()
return n, auth, session, nil
Expand Down