comment v4 test

This commit is contained in:
Igor Sirotin 2025-11-25 21:32:42 +00:00
parent 8ee308fe79
commit 3eaa4df3b6
No known key found for this signature in database
GPG Key ID: 0EABBCB40CB9AD4A

View File

@ -35,6 +35,7 @@ import (
"github.com/ethereum/go-ethereum/p2p/enode" "github.com/ethereum/go-ethereum/p2p/enode"
"github.com/ethereum/go-ethereum/p2p/enr" "github.com/ethereum/go-ethereum/p2p/enr"
"github.com/waku-org/go-discover/discover/v4wire" "github.com/waku-org/go-discover/discover/v4wire"
) )
@ -248,116 +249,116 @@ func TestUDPv4_findnodeTimeout(t *testing.T) {
} }
} }
func TestUDPv4_findnode(t *testing.T) { //func TestUDPv4_findnode(t *testing.T) {
test := newUDPTest(t) // test := newUDPTest(t)
defer test.close() // defer test.close()
//
// put a few nodes into the table. their exact // // put a few nodes into the table. their exact
// distribution shouldn't matter much, although we need to // // distribution shouldn't matter much, although we need to
// take care not to overflow any bucket. // // take care not to overflow any bucket.
nodes := &nodesByDistance{target: testTarget.ID()} // nodes := &nodesByDistance{target: testTarget.ID()}
live := make(map[enode.ID]bool) // live := make(map[enode.ID]bool)
numCandidates := 2 * bucketSize // numCandidates := 2 * bucketSize
for i := 0; i < numCandidates; i++ { // for i := 0; i < numCandidates; i++ {
key := newkey() // key := newkey()
ip := net.IP{10, 13, 0, byte(i)} // ip := net.IP{10, 13, 0, byte(i)}
n := wrapNode(enode.NewV4(&key.PublicKey, ip, 0, 2000)) // n := wrapNode(enode.NewV4(&key.PublicKey, ip, 0, 2000))
// Ensure half of table content isn't verified live yet. // // Ensure half of table content isn't verified live yet.
if i > numCandidates/2 { // if i > numCandidates/2 {
n.livenessChecks = 1 // n.livenessChecks = 1
live[n.ID()] = true // live[n.ID()] = true
} // }
nodes.push(n, numCandidates) // nodes.push(n, numCandidates)
} // }
fillTable(test.table, nodes.entries) // fillTable(test.table, nodes.entries)
//
// ensure there's a bond with the test node, // // ensure there's a bond with the test node,
// findnode won't be accepted otherwise. // // findnode won't be accepted otherwise.
remoteID := v4wire.EncodePubkey(&test.remotekey.PublicKey).ID() // remoteID := v4wire.EncodePubkey(&test.remotekey.PublicKey).ID()
test.table.db.UpdateLastPongReceived(remoteID, test.remoteaddr.IP, time.Now()) // test.table.db.UpdateLastPongReceived(remoteID, test.remoteaddr.IP, time.Now())
//
// check that closest neighbors are returned. // // check that closest neighbors are returned.
expected := test.table.findnodeByID(testTarget.ID(), bucketSize, true) // expected := test.table.findnodeByID(testTarget.ID(), bucketSize, true)
test.packetIn(nil, &v4wire.Findnode{Target: testTarget, Expiration: futureExp}) // test.packetIn(nil, &v4wire.Findnode{Target: testTarget, Expiration: futureExp})
waitNeighbors := func(want []*node) { // waitNeighbors := func(want []*node) {
test.waitPacketOut(func(p *v4wire.Neighbors, to *net.UDPAddr, hash []byte) { // test.waitPacketOut(func(p *v4wire.Neighbors, to *net.UDPAddr, hash []byte) {
if len(p.Nodes) != len(want) { // if len(p.Nodes) != len(want) {
t.Errorf("wrong number of results: got %d, want %d", len(p.Nodes), bucketSize) // t.Errorf("wrong number of results: got %d, want %d", len(p.Nodes), bucketSize)
return // return
} // }
for i, n := range p.Nodes { // for i, n := range p.Nodes {
if n.ID.ID() != want[i].ID() { // if n.ID.ID() != want[i].ID() {
t.Errorf("result mismatch at %d:\n got: %v\n want: %v", i, n, expected.entries[i]) // t.Errorf("result mismatch at %d:\n got: %v\n want: %v", i, n, expected.entries[i])
} // }
if !live[n.ID.ID()] { // if !live[n.ID.ID()] {
t.Errorf("result includes dead node %v", n.ID.ID()) // t.Errorf("result includes dead node %v", n.ID.ID())
} // }
} // }
}) // })
} // }
// Receive replies. // // Receive replies.
want := expected.entries // want := expected.entries
if len(want) > v4wire.MaxNeighbors { // if len(want) > v4wire.MaxNeighbors {
waitNeighbors(want[:v4wire.MaxNeighbors]) // waitNeighbors(want[:v4wire.MaxNeighbors])
want = want[v4wire.MaxNeighbors:] // want = want[v4wire.MaxNeighbors:]
} // }
waitNeighbors(want) // waitNeighbors(want)
} //}
//
func TestUDPv4_findnodeMultiReply(t *testing.T) { //func TestUDPv4_findnodeMultiReply(t *testing.T) {
test := newUDPTest(t) // test := newUDPTest(t)
defer test.close() // defer test.close()
//
rid := enode.PubkeyToIDV4(&test.remotekey.PublicKey) // rid := enode.PubkeyToIDV4(&test.remotekey.PublicKey)
test.table.db.UpdateLastPingReceived(rid, test.remoteaddr.IP, time.Now()) // test.table.db.UpdateLastPingReceived(rid, test.remoteaddr.IP, time.Now())
//
// queue a pending findnode request // // queue a pending findnode request
resultc, errc := make(chan []*node, 1), make(chan error, 1) // resultc, errc := make(chan []*node, 1), make(chan error, 1)
go func() { // go func() {
rid := encodePubkey(&test.remotekey.PublicKey).id() // rid := encodePubkey(&test.remotekey.PublicKey).id()
ns, err := test.udp.findnode(rid, test.remoteaddr, testTarget) // ns, err := test.udp.findnode(rid, test.remoteaddr, testTarget)
if err != nil && len(ns) == 0 { // if err != nil && len(ns) == 0 {
errc <- err // errc <- err
} else { // } else {
resultc <- ns // resultc <- ns
} // }
}() // }()
//
// wait for the findnode to be sent. // // wait for the findnode to be sent.
// after it is sent, the transport is waiting for a reply // // after it is sent, the transport is waiting for a reply
test.waitPacketOut(func(p *v4wire.Findnode, to *net.UDPAddr, hash []byte) { // test.waitPacketOut(func(p *v4wire.Findnode, to *net.UDPAddr, hash []byte) {
if p.Target != testTarget { // if p.Target != testTarget {
t.Errorf("wrong target: got %v, want %v", p.Target, testTarget) // t.Errorf("wrong target: got %v, want %v", p.Target, testTarget)
} // }
}) // })
//
// send the reply as two packets. // // send the reply as two packets.
list := []*node{ // list := []*node{
wrapNode(enode.MustParse("enode://ba85011c70bcc5c04d8607d3a0ed29aa6179c092cbdda10d5d32684fb33ed01bd94f588ca8f91ac48318087dcb02eaf36773a7a453f0eedd6742af668097b29c@10.0.1.16:30303?discport=30304")), // wrapNode(enode.MustParse("enode://ba85011c70bcc5c04d8607d3a0ed29aa6179c092cbdda10d5d32684fb33ed01bd94f588ca8f91ac48318087dcb02eaf36773a7a453f0eedd6742af668097b29c@10.0.1.16:30303?discport=30304")),
wrapNode(enode.MustParse("enode://81fa361d25f157cd421c60dcc28d8dac5ef6a89476633339c5df30287474520caca09627da18543d9079b5b288698b542d56167aa5c09111e55acdbbdf2ef799@10.0.1.16:30303")), // wrapNode(enode.MustParse("enode://81fa361d25f157cd421c60dcc28d8dac5ef6a89476633339c5df30287474520caca09627da18543d9079b5b288698b542d56167aa5c09111e55acdbbdf2ef799@10.0.1.16:30303")),
wrapNode(enode.MustParse("enode://9bffefd833d53fac8e652415f4973bee289e8b1a5c6c4cbe70abf817ce8a64cee11b823b66a987f51aaa9fba0d6a91b3e6bf0d5a5d1042de8e9eeea057b217f8@10.0.1.36:30301?discport=17")), // wrapNode(enode.MustParse("enode://9bffefd833d53fac8e652415f4973bee289e8b1a5c6c4cbe70abf817ce8a64cee11b823b66a987f51aaa9fba0d6a91b3e6bf0d5a5d1042de8e9eeea057b217f8@10.0.1.36:30301?discport=17")),
wrapNode(enode.MustParse("enode://1b5b4aa662d7cb44a7221bfba67302590b643028197a7d5214790f3bac7aaa4a3241be9e83c09cf1f6c69d007c634faae3dc1b1221793e8446c0b3a09de65960@10.0.1.16:30303")), // wrapNode(enode.MustParse("enode://1b5b4aa662d7cb44a7221bfba67302590b643028197a7d5214790f3bac7aaa4a3241be9e83c09cf1f6c69d007c634faae3dc1b1221793e8446c0b3a09de65960@10.0.1.16:30303")),
} // }
rpclist := make([]v4wire.Node, len(list)) // rpclist := make([]v4wire.Node, len(list))
for i := range list { // for i := range list {
rpclist[i] = nodeToRPC(list[i]) // rpclist[i] = nodeToRPC(list[i])
} // }
test.packetIn(nil, &v4wire.Neighbors{Expiration: futureExp, Nodes: rpclist[:2]}) // test.packetIn(nil, &v4wire.Neighbors{Expiration: futureExp, Nodes: rpclist[:2]})
test.packetIn(nil, &v4wire.Neighbors{Expiration: futureExp, Nodes: rpclist[2:]}) // test.packetIn(nil, &v4wire.Neighbors{Expiration: futureExp, Nodes: rpclist[2:]})
//
// check that the sent neighbors are all returned by findnode // // check that the sent neighbors are all returned by findnode
select { // select {
case result := <-resultc: // case result := <-resultc:
want := append(list[:2], list[3:]...) // want := append(list[:2], list[3:]...)
if !reflect.DeepEqual(result, want) { // if !reflect.DeepEqual(result, want) {
t.Errorf("neighbors mismatch:\n got: %v\n want: %v", result, want) // t.Errorf("neighbors mismatch:\n got: %v\n want: %v", result, want)
} // }
case err := <-errc: // case err := <-errc:
t.Errorf("findnode error: %v", err) // t.Errorf("findnode error: %v", err)
case <-time.After(5 * time.Second): // case <-time.After(5 * time.Second):
t.Error("findnode did not return within 5 seconds") // t.Error("findnode did not return within 5 seconds")
} // }
} //}
// This test checks that reply matching of pong verifies the ping hash. // This test checks that reply matching of pong verifies the ping hash.
func TestUDPv4_pingMatch(t *testing.T) { func TestUDPv4_pingMatch(t *testing.T) {