Skip to content

Commit 621b423

Browse files
authored
p2p/discover: fix handling of distance 256 in lookupDistances (#26087)
Noticed that lookupDistances for FINDNODE requests didn't consider 256 a valid distance. This is actually part of the example in the comment above the function, surprised that wasn't tested before.
1 parent 24f08ec commit 621b423

File tree

3 files changed

+40
-3
lines changed

3 files changed

+40
-3
lines changed

p2p/discover/v5_udp.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ func lookupDistances(target, dest enode.ID) (dists []uint) {
323323
td := enode.LogDist(target, dest)
324324
dists = append(dists, uint(td))
325325
for i := 1; len(dists) < lookupRequestLimit; i++ {
326-
if td+i < 256 {
326+
if td+i <= 256 {
327327
dists = append(dists, uint(td+i))
328328
}
329329
if td-i > 0 {

p2p/discover/v5_udp_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import (
3434
"github.com/ethereum/go-ethereum/p2p/enode"
3535
"github.com/ethereum/go-ethereum/p2p/enr"
3636
"github.com/ethereum/go-ethereum/rlp"
37+
"github.com/stretchr/testify/require"
3738
)
3839

3940
// Real sockets, real crypto: this test checks end-to-end connectivity for UDPv5.
@@ -519,6 +520,42 @@ func TestUDPv5_talkRequest(t *testing.T) {
519520
}
520521
}
521522

523+
// This test checks that lookupDistances works.
524+
func TestUDPv5_lookupDistances(t *testing.T) {
525+
test := newUDPV5Test(t)
526+
lnID := test.table.self().ID()
527+
528+
t.Run("target distance of 1", func(t *testing.T) {
529+
node := nodeAtDistance(lnID, 1, intIP(0))
530+
dists := lookupDistances(lnID, node.ID())
531+
require.Equal(t, []uint{1, 2, 3}, dists)
532+
})
533+
534+
t.Run("target distance of 2", func(t *testing.T) {
535+
node := nodeAtDistance(lnID, 2, intIP(0))
536+
dists := lookupDistances(lnID, node.ID())
537+
require.Equal(t, []uint{2, 3, 1}, dists)
538+
})
539+
540+
t.Run("target distance of 128", func(t *testing.T) {
541+
node := nodeAtDistance(lnID, 128, intIP(0))
542+
dists := lookupDistances(lnID, node.ID())
543+
require.Equal(t, []uint{128, 129, 127}, dists)
544+
})
545+
546+
t.Run("target distance of 255", func(t *testing.T) {
547+
node := nodeAtDistance(lnID, 255, intIP(0))
548+
dists := lookupDistances(lnID, node.ID())
549+
require.Equal(t, []uint{255, 256, 254}, dists)
550+
})
551+
552+
t.Run("target distance of 256", func(t *testing.T) {
553+
node := nodeAtDistance(lnID, 256, intIP(0))
554+
dists := lookupDistances(lnID, node.ID())
555+
require.Equal(t, []uint{256, 255, 254}, dists)
556+
})
557+
}
558+
522559
// This test checks that lookup works.
523560
func TestUDPv5_lookup(t *testing.T) {
524561
t.Parallel()

p2p/discover/v5wire/encoding.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ type (
6565
handshakeAuthData struct {
6666
h struct {
6767
SrcID enode.ID
68-
SigSize byte // ignature data
68+
SigSize byte // signature data
6969
PubkeySize byte // offset of
7070
}
7171
// Trailing variable-size data.
@@ -529,7 +529,7 @@ func (c *Codec) decodeHandshake(fromAddr string, head *Header) (n *enode.Node, a
529529
if err != nil {
530530
return nil, auth, nil, errInvalidAuthKey
531531
}
532-
// Derive sesssion keys.
532+
// Derive session keys.
533533
session := deriveKeys(sha256.New, c.privkey, ephkey, auth.h.SrcID, c.localnode.ID(), cdata)
534534
session = session.keysFlipped()
535535
return n, auth, session, nil

0 commit comments

Comments
 (0)