2015-07-07 03:54:22 +03:00
|
|
|
// Copyright 2015 The go-ethereum Authors
|
2015-07-22 19:48:40 +03:00
|
|
|
// This file is part of the go-ethereum library.
|
2015-07-07 03:54:22 +03:00
|
|
|
//
|
2015-07-23 19:35:11 +03:00
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
2015-07-07 03:54:22 +03:00
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
2015-07-22 19:48:40 +03:00
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
2015-07-07 03:54:22 +03:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-07-22 19:48:40 +03:00
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2015-07-07 03:54:22 +03:00
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
2015-07-22 19:48:40 +03:00
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
2015-07-07 03:54:22 +03:00
|
|
|
|
2015-01-27 15:33:26 +02:00
|
|
|
package discover
|
|
|
|
|
|
|
|
import (
|
2015-03-25 17:45:53 +02:00
|
|
|
"bytes"
|
|
|
|
"crypto/ecdsa"
|
2019-01-29 19:39:20 +03:00
|
|
|
crand "crypto/rand"
|
2015-08-08 03:49:28 +03:00
|
|
|
"encoding/binary"
|
2015-03-25 17:45:53 +02:00
|
|
|
"errors"
|
2020-08-24 15:42:39 +03:00
|
|
|
"fmt"
|
2015-03-25 17:45:53 +02:00
|
|
|
"io"
|
2015-08-08 03:49:28 +03:00
|
|
|
"math/rand"
|
2015-01-27 15:33:26 +02:00
|
|
|
"net"
|
2015-03-25 17:45:53 +02:00
|
|
|
"reflect"
|
|
|
|
"sync"
|
2015-01-27 15:33:26 +02:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2019-04-30 14:13:22 +03:00
|
|
|
"github.com/ethereum/go-ethereum/internal/testlog"
|
|
|
|
"github.com/ethereum/go-ethereum/log"
|
2020-06-02 14:20:19 +03:00
|
|
|
"github.com/ethereum/go-ethereum/p2p/discover/v4wire"
|
all: new p2p node representation (#17643)
Package p2p/enode provides a generalized representation of p2p nodes
which can contain arbitrary information in key/value pairs. It is also
the new home for the node database. The "v4" identity scheme is also
moved here from p2p/enr to remove the dependency on Ethereum crypto from
that package.
Record signature handling is changed significantly. The identity scheme
registry is removed and acceptable schemes must be passed to any method
that needs identity. This means records must now be validated explicitly
after decoding.
The enode API is designed to make signature handling easy and safe: most
APIs around the codebase work with enode.Node, which is a wrapper around
a valid record. Going from enr.Record to enode.Node requires a valid
signature.
* p2p/discover: port to p2p/enode
This ports the discovery code to the new node representation in
p2p/enode. The wire protocol is unchanged, this can be considered a
refactoring change. The Kademlia table can now deal with nodes using an
arbitrary identity scheme. This requires a few incompatible API changes:
- Table.Lookup is not available anymore. It used to take a public key
as argument because v4 protocol requires one. Its replacement is
LookupRandom.
- Table.Resolve takes *enode.Node instead of NodeID. This is also for
v4 protocol compatibility because nodes cannot be looked up by ID
alone.
- Types Node and NodeID are gone. Further commits in the series will be
fixes all over the the codebase to deal with those removals.
* p2p: port to p2p/enode and discovery changes
This adapts package p2p to the changes in p2p/discover. All uses of
discover.Node and discover.NodeID are replaced by their equivalents from
p2p/enode.
New API is added to retrieve the enode.Node instance of a peer. The
behavior of Server.Self with discovery disabled is improved. It now
tries much harder to report a working IP address, falling back to
127.0.0.1 if no suitable address can be determined through other means.
These changes were needed for tests of other packages later in the
series.
* p2p/simulations, p2p/testing: port to p2p/enode
No surprises here, mostly replacements of discover.Node, discover.NodeID
with their new equivalents. The 'interesting' API changes are:
- testing.ProtocolSession tracks complete nodes, not just their IDs.
- adapters.NodeConfig has a new method to create a complete node.
These changes were needed to make swarm tests work.
Note that the NodeID change makes the code incompatible with old
simulation snapshots.
* whisper/whisperv5, whisper/whisperv6: port to p2p/enode
This port was easy because whisper uses []byte for node IDs and
URL strings in the API.
* eth: port to p2p/enode
Again, easy to port because eth uses strings for node IDs and doesn't
care about node information in any way.
* les: port to p2p/enode
Apart from replacing discover.NodeID with enode.ID, most changes are in
the server pool code. It now deals with complete nodes instead
of (Pubkey, IP, Port) triples. The database format is unchanged for now,
but we should probably change it to use the node database later.
* node: port to p2p/enode
This change simply replaces discover.Node and discover.NodeID with their
new equivalents.
* swarm/network: port to p2p/enode
Swarm has its own node address representation, BzzAddr, containing both
an overlay address (the hash of a secp256k1 public key) and an underlay
address (enode:// URL).
There are no changes to the BzzAddr format in this commit, but certain
operations such as creating a BzzAddr from a node ID are now impossible
because node IDs aren't public keys anymore.
Most swarm-related changes in the series remove uses of
NewAddrFromNodeID, replacing it with NewAddr which takes a complete node
as argument. ToOverlayAddr is removed because we can just use the node
ID directly.
2018-09-25 01:59:00 +03:00
|
|
|
"github.com/ethereum/go-ethereum/p2p/enode"
|
2019-05-15 07:47:45 +03:00
|
|
|
"github.com/ethereum/go-ethereum/p2p/enr"
|
2015-01-27 15:33:26 +02:00
|
|
|
)
|
|
|
|
|
2015-04-18 02:50:31 +03:00
|
|
|
// shared test variables
|
|
|
|
var (
|
|
|
|
futureExp = uint64(time.Now().Add(10 * time.Hour).Unix())
|
2020-06-02 14:20:19 +03:00
|
|
|
testTarget = v4wire.Pubkey{0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1}
|
|
|
|
testRemote = v4wire.Endpoint{IP: net.ParseIP("1.1.1.1").To4(), UDP: 1, TCP: 2}
|
|
|
|
testLocalAnnounced = v4wire.Endpoint{IP: net.ParseIP("2.2.2.2").To4(), UDP: 3, TCP: 4}
|
|
|
|
testLocal = v4wire.Endpoint{IP: net.ParseIP("3.3.3.3").To4(), UDP: 5, TCP: 6}
|
2015-04-18 02:50:31 +03:00
|
|
|
)
|
|
|
|
|
2015-03-25 17:45:53 +02:00
|
|
|
type udpTest struct {
|
|
|
|
t *testing.T
|
|
|
|
pipe *dgramPipe
|
|
|
|
table *Table
|
2019-01-29 19:39:20 +03:00
|
|
|
db *enode.DB
|
2019-04-30 14:13:22 +03:00
|
|
|
udp *UDPv4
|
2015-03-25 17:45:53 +02:00
|
|
|
sent [][]byte
|
|
|
|
localkey, remotekey *ecdsa.PrivateKey
|
|
|
|
remoteaddr *net.UDPAddr
|
|
|
|
}
|
2015-01-27 15:33:26 +02:00
|
|
|
|
2015-03-25 17:45:53 +02:00
|
|
|
func newUDPTest(t *testing.T) *udpTest {
|
|
|
|
test := &udpTest{
|
|
|
|
t: t,
|
|
|
|
pipe: newpipe(),
|
|
|
|
localkey: newkey(),
|
|
|
|
remotekey: newkey(),
|
2016-11-21 20:11:54 +03:00
|
|
|
remoteaddr: &net.UDPAddr{IP: net.IP{10, 0, 1, 99}, Port: 30303},
|
2015-01-27 15:33:26 +02:00
|
|
|
}
|
2019-04-30 14:13:22 +03:00
|
|
|
|
2019-01-29 19:39:20 +03:00
|
|
|
test.db, _ = enode.OpenDB("")
|
|
|
|
ln := enode.NewLocalNode(test.db, test.localkey)
|
2019-04-30 14:13:22 +03:00
|
|
|
test.udp, _ = ListenV4(test.pipe, ln, Config{
|
|
|
|
PrivateKey: test.localkey,
|
|
|
|
Log: testlog.Logger(t, log.LvlTrace),
|
|
|
|
})
|
|
|
|
test.table = test.udp.tab
|
2018-02-12 15:36:09 +03:00
|
|
|
// Wait for initial refresh so the table doesn't send unexpected findnode.
|
|
|
|
<-test.table.initDone
|
2015-03-25 17:45:53 +02:00
|
|
|
return test
|
|
|
|
}
|
|
|
|
|
2019-01-29 19:39:20 +03:00
|
|
|
func (test *udpTest) close() {
|
2019-04-30 14:13:22 +03:00
|
|
|
test.udp.Close()
|
2019-01-29 19:39:20 +03:00
|
|
|
test.db.Close()
|
|
|
|
}
|
|
|
|
|
2015-03-25 17:45:53 +02:00
|
|
|
// handles a packet as if it had been sent to the transport.
|
2020-06-02 14:20:19 +03:00
|
|
|
func (test *udpTest) packetIn(wantError error, data v4wire.Packet) {
|
2019-04-30 14:13:22 +03:00
|
|
|
test.t.Helper()
|
|
|
|
|
2019-05-15 07:47:45 +03:00
|
|
|
test.packetInFrom(wantError, test.remotekey, test.remoteaddr, data)
|
2019-01-29 19:39:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// handles a packet as if it had been sent to the transport by the key/endpoint.
|
2020-06-02 14:20:19 +03:00
|
|
|
func (test *udpTest) packetInFrom(wantError error, key *ecdsa.PrivateKey, addr *net.UDPAddr, data v4wire.Packet) {
|
2019-04-30 14:13:22 +03:00
|
|
|
test.t.Helper()
|
|
|
|
|
2020-06-02 14:20:19 +03:00
|
|
|
enc, _, err := v4wire.Encode(key, data)
|
2015-03-25 17:45:53 +02:00
|
|
|
if err != nil {
|
2020-06-02 14:20:19 +03:00
|
|
|
test.t.Errorf("%s encode error: %v", data.Name(), err)
|
2015-01-27 15:33:26 +02:00
|
|
|
}
|
2015-03-25 17:45:53 +02:00
|
|
|
test.sent = append(test.sent, enc)
|
2019-01-29 19:39:20 +03:00
|
|
|
if err = test.udp.handlePacket(addr, enc); err != wantError {
|
2019-04-30 14:13:22 +03:00
|
|
|
test.t.Errorf("error mismatch: got %q, want %q", err, wantError)
|
2015-01-27 15:33:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-25 17:45:53 +02:00
|
|
|
// waits for a packet to be sent by the transport.
|
2019-04-30 14:13:22 +03:00
|
|
|
// validate should have type func(X, *net.UDPAddr, []byte), where X is a packet type.
|
|
|
|
func (test *udpTest) waitPacketOut(validate interface{}) (closed bool) {
|
|
|
|
test.t.Helper()
|
|
|
|
|
2020-04-08 10:57:23 +03:00
|
|
|
dgram, err := test.pipe.receive()
|
|
|
|
if err == errClosed {
|
2019-04-30 14:13:22 +03:00
|
|
|
return true
|
2020-04-08 10:57:23 +03:00
|
|
|
} else if err != nil {
|
|
|
|
test.t.Error("packet receive error:", err)
|
|
|
|
return false
|
2019-04-30 14:13:22 +03:00
|
|
|
}
|
2020-06-02 14:20:19 +03:00
|
|
|
p, _, hash, err := v4wire.Decode(dgram.data)
|
2015-03-25 17:45:53 +02:00
|
|
|
if err != nil {
|
2019-04-30 14:13:22 +03:00
|
|
|
test.t.Errorf("sent packet decode error: %v", err)
|
|
|
|
return false
|
2015-01-27 15:33:26 +02:00
|
|
|
}
|
2015-03-25 17:45:53 +02:00
|
|
|
fn := reflect.ValueOf(validate)
|
|
|
|
exptype := fn.Type().In(0)
|
2019-04-30 14:13:22 +03:00
|
|
|
if !reflect.TypeOf(p).AssignableTo(exptype) {
|
|
|
|
test.t.Errorf("sent packet type mismatch, got: %v, want: %v", reflect.TypeOf(p), exptype)
|
|
|
|
return false
|
2015-03-25 17:45:53 +02:00
|
|
|
}
|
2019-04-30 14:13:22 +03:00
|
|
|
fn.Call([]reflect.Value{reflect.ValueOf(p), reflect.ValueOf(&dgram.to), reflect.ValueOf(hash)})
|
|
|
|
return false
|
2015-01-27 15:33:26 +02:00
|
|
|
}
|
|
|
|
|
2019-04-30 14:13:22 +03:00
|
|
|
func TestUDPv4_packetErrors(t *testing.T) {
|
2015-03-25 17:45:53 +02:00
|
|
|
test := newUDPTest(t)
|
2019-01-29 19:39:20 +03:00
|
|
|
defer test.close()
|
2015-03-25 17:45:53 +02:00
|
|
|
|
2020-06-02 14:20:19 +03:00
|
|
|
test.packetIn(errExpired, &v4wire.Ping{From: testRemote, To: testLocalAnnounced, Version: 4})
|
|
|
|
test.packetIn(errUnsolicitedReply, &v4wire.Pong{ReplyTok: []byte{}, Expiration: futureExp})
|
|
|
|
test.packetIn(errUnknownNode, &v4wire.Findnode{Expiration: futureExp})
|
|
|
|
test.packetIn(errUnsolicitedReply, &v4wire.Neighbors{Expiration: futureExp})
|
2015-03-25 17:45:53 +02:00
|
|
|
}
|
|
|
|
|
2019-04-30 14:13:22 +03:00
|
|
|
func TestUDPv4_pingTimeout(t *testing.T) {
|
2015-03-25 17:45:53 +02:00
|
|
|
t.Parallel()
|
|
|
|
test := newUDPTest(t)
|
2019-01-29 19:39:20 +03:00
|
|
|
defer test.close()
|
2015-03-25 17:45:53 +02:00
|
|
|
|
2019-04-30 14:13:22 +03:00
|
|
|
key := newkey()
|
2015-03-25 17:45:53 +02:00
|
|
|
toaddr := &net.UDPAddr{IP: net.ParseIP("1.2.3.4"), Port: 2222}
|
2019-04-30 14:13:22 +03:00
|
|
|
node := enode.NewV4(&key.PublicKey, toaddr.IP, 0, toaddr.Port)
|
2019-05-15 07:47:45 +03:00
|
|
|
if _, err := test.udp.ping(node); err != errTimeout {
|
2015-03-25 17:45:53 +02:00
|
|
|
t.Error("expected timeout error, got", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-15 07:47:45 +03:00
|
|
|
type testPacket byte
|
|
|
|
|
2020-06-02 14:20:19 +03:00
|
|
|
func (req testPacket) Kind() byte { return byte(req) }
|
|
|
|
func (req testPacket) Name() string { return "" }
|
2019-05-15 07:47:45 +03:00
|
|
|
|
2019-04-30 14:13:22 +03:00
|
|
|
func TestUDPv4_responseTimeouts(t *testing.T) {
|
2015-08-08 03:49:28 +03:00
|
|
|
t.Parallel()
|
|
|
|
test := newUDPTest(t)
|
2019-01-29 19:39:20 +03:00
|
|
|
defer test.close()
|
2015-08-08 03:49:28 +03:00
|
|
|
|
|
|
|
randomDuration := func(max time.Duration) time.Duration {
|
|
|
|
return time.Duration(rand.Int63n(int64(max)))
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
nReqs = 200
|
|
|
|
nTimeouts = 0 // number of requests with ptype > 128
|
|
|
|
nilErr = make(chan error, nReqs) // for requests that get a reply
|
|
|
|
timeoutErr = make(chan error, nReqs) // for requests that time out
|
|
|
|
)
|
|
|
|
for i := 0; i < nReqs; i++ {
|
|
|
|
// Create a matcher for a random request in udp.loop. Requests
|
|
|
|
// with ptype <= 128 will not get a reply and should time out.
|
|
|
|
// For all other requests, a reply is scheduled to arrive
|
|
|
|
// within the timeout window.
|
2019-01-29 19:39:20 +03:00
|
|
|
p := &replyMatcher{
|
2015-08-08 03:49:28 +03:00
|
|
|
ptype: byte(rand.Intn(255)),
|
2020-06-02 14:20:19 +03:00
|
|
|
callback: func(v4wire.Packet) (bool, bool) { return true, true },
|
2015-08-08 03:49:28 +03:00
|
|
|
}
|
|
|
|
binary.BigEndian.PutUint64(p.from[:], uint64(i))
|
|
|
|
if p.ptype <= 128 {
|
|
|
|
p.errc = timeoutErr
|
2019-01-29 19:39:20 +03:00
|
|
|
test.udp.addReplyMatcher <- p
|
2015-08-08 03:49:28 +03:00
|
|
|
nTimeouts++
|
|
|
|
} else {
|
|
|
|
p.errc = nilErr
|
2019-01-29 19:39:20 +03:00
|
|
|
test.udp.addReplyMatcher <- p
|
2015-08-08 03:49:28 +03:00
|
|
|
time.AfterFunc(randomDuration(60*time.Millisecond), func() {
|
2019-05-15 07:47:45 +03:00
|
|
|
if !test.udp.handleReply(p.from, p.ip, testPacket(p.ptype)) {
|
2015-08-08 03:49:28 +03:00
|
|
|
t.Logf("not matched: %v", p)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
time.Sleep(randomDuration(30 * time.Millisecond))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that all timeouts were delivered and that the rest got nil errors.
|
|
|
|
// The replies must be delivered.
|
|
|
|
var (
|
|
|
|
recvDeadline = time.After(20 * time.Second)
|
|
|
|
nTimeoutsRecv, nNil = 0, 0
|
|
|
|
)
|
|
|
|
for i := 0; i < nReqs; i++ {
|
|
|
|
select {
|
|
|
|
case err := <-timeoutErr:
|
|
|
|
if err != errTimeout {
|
|
|
|
t.Fatalf("got non-timeout error on timeoutErr %d: %v", i, err)
|
|
|
|
}
|
|
|
|
nTimeoutsRecv++
|
|
|
|
case err := <-nilErr:
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("got non-nil error on nilErr %d: %v", i, err)
|
|
|
|
}
|
|
|
|
nNil++
|
|
|
|
case <-recvDeadline:
|
|
|
|
t.Fatalf("exceeded recv deadline")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if nTimeoutsRecv != nTimeouts {
|
|
|
|
t.Errorf("wrong number of timeout errors received: got %d, want %d", nTimeoutsRecv, nTimeouts)
|
|
|
|
}
|
|
|
|
if nNil != nReqs-nTimeouts {
|
|
|
|
t.Errorf("wrong number of successful replies: got %d, want %d", nNil, nReqs-nTimeouts)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-30 14:13:22 +03:00
|
|
|
func TestUDPv4_findnodeTimeout(t *testing.T) {
|
2015-01-27 15:33:26 +02:00
|
|
|
t.Parallel()
|
2015-03-25 17:45:53 +02:00
|
|
|
test := newUDPTest(t)
|
2019-01-29 19:39:20 +03:00
|
|
|
defer test.close()
|
2015-01-27 15:33:26 +02:00
|
|
|
|
2015-03-25 17:45:53 +02:00
|
|
|
toaddr := &net.UDPAddr{IP: net.ParseIP("1.2.3.4"), Port: 2222}
|
all: new p2p node representation (#17643)
Package p2p/enode provides a generalized representation of p2p nodes
which can contain arbitrary information in key/value pairs. It is also
the new home for the node database. The "v4" identity scheme is also
moved here from p2p/enr to remove the dependency on Ethereum crypto from
that package.
Record signature handling is changed significantly. The identity scheme
registry is removed and acceptable schemes must be passed to any method
that needs identity. This means records must now be validated explicitly
after decoding.
The enode API is designed to make signature handling easy and safe: most
APIs around the codebase work with enode.Node, which is a wrapper around
a valid record. Going from enr.Record to enode.Node requires a valid
signature.
* p2p/discover: port to p2p/enode
This ports the discovery code to the new node representation in
p2p/enode. The wire protocol is unchanged, this can be considered a
refactoring change. The Kademlia table can now deal with nodes using an
arbitrary identity scheme. This requires a few incompatible API changes:
- Table.Lookup is not available anymore. It used to take a public key
as argument because v4 protocol requires one. Its replacement is
LookupRandom.
- Table.Resolve takes *enode.Node instead of NodeID. This is also for
v4 protocol compatibility because nodes cannot be looked up by ID
alone.
- Types Node and NodeID are gone. Further commits in the series will be
fixes all over the the codebase to deal with those removals.
* p2p: port to p2p/enode and discovery changes
This adapts package p2p to the changes in p2p/discover. All uses of
discover.Node and discover.NodeID are replaced by their equivalents from
p2p/enode.
New API is added to retrieve the enode.Node instance of a peer. The
behavior of Server.Self with discovery disabled is improved. It now
tries much harder to report a working IP address, falling back to
127.0.0.1 if no suitable address can be determined through other means.
These changes were needed for tests of other packages later in the
series.
* p2p/simulations, p2p/testing: port to p2p/enode
No surprises here, mostly replacements of discover.Node, discover.NodeID
with their new equivalents. The 'interesting' API changes are:
- testing.ProtocolSession tracks complete nodes, not just their IDs.
- adapters.NodeConfig has a new method to create a complete node.
These changes were needed to make swarm tests work.
Note that the NodeID change makes the code incompatible with old
simulation snapshots.
* whisper/whisperv5, whisper/whisperv6: port to p2p/enode
This port was easy because whisper uses []byte for node IDs and
URL strings in the API.
* eth: port to p2p/enode
Again, easy to port because eth uses strings for node IDs and doesn't
care about node information in any way.
* les: port to p2p/enode
Apart from replacing discover.NodeID with enode.ID, most changes are in
the server pool code. It now deals with complete nodes instead
of (Pubkey, IP, Port) triples. The database format is unchanged for now,
but we should probably change it to use the node database later.
* node: port to p2p/enode
This change simply replaces discover.Node and discover.NodeID with their
new equivalents.
* swarm/network: port to p2p/enode
Swarm has its own node address representation, BzzAddr, containing both
an overlay address (the hash of a secp256k1 public key) and an underlay
address (enode:// URL).
There are no changes to the BzzAddr format in this commit, but certain
operations such as creating a BzzAddr from a node ID are now impossible
because node IDs aren't public keys anymore.
Most swarm-related changes in the series remove uses of
NewAddrFromNodeID, replacing it with NewAddr which takes a complete node
as argument. ToOverlayAddr is removed because we can just use the node
ID directly.
2018-09-25 01:59:00 +03:00
|
|
|
toid := enode.ID{1, 2, 3, 4}
|
2020-06-02 14:20:19 +03:00
|
|
|
target := v4wire.Pubkey{4, 5, 6, 7}
|
2015-03-25 17:45:53 +02:00
|
|
|
result, err := test.udp.findnode(toid, toaddr, target)
|
|
|
|
if err != errTimeout {
|
|
|
|
t.Error("expected timeout error, got", err)
|
|
|
|
}
|
|
|
|
if len(result) > 0 {
|
|
|
|
t.Error("expected empty result, got", result)
|
|
|
|
}
|
|
|
|
}
|
2015-01-27 15:33:26 +02:00
|
|
|
|
2019-04-30 14:13:22 +03:00
|
|
|
func TestUDPv4_findnode(t *testing.T) {
|
2015-03-25 17:45:53 +02:00
|
|
|
test := newUDPTest(t)
|
2019-01-29 19:39:20 +03:00
|
|
|
defer test.close()
|
2015-03-25 17:45:53 +02:00
|
|
|
|
|
|
|
// put a few nodes into the table. their exact
|
2017-01-06 20:44:35 +03:00
|
|
|
// distribution shouldn't matter much, although we need to
|
2015-03-25 17:45:53 +02:00
|
|
|
// take care not to overflow any bucket.
|
2020-06-02 14:20:19 +03:00
|
|
|
nodes := &nodesByDistance{target: testTarget.ID()}
|
2019-01-29 19:39:20 +03:00
|
|
|
live := make(map[enode.ID]bool)
|
|
|
|
numCandidates := 2 * bucketSize
|
|
|
|
for i := 0; i < numCandidates; i++ {
|
all: new p2p node representation (#17643)
Package p2p/enode provides a generalized representation of p2p nodes
which can contain arbitrary information in key/value pairs. It is also
the new home for the node database. The "v4" identity scheme is also
moved here from p2p/enr to remove the dependency on Ethereum crypto from
that package.
Record signature handling is changed significantly. The identity scheme
registry is removed and acceptable schemes must be passed to any method
that needs identity. This means records must now be validated explicitly
after decoding.
The enode API is designed to make signature handling easy and safe: most
APIs around the codebase work with enode.Node, which is a wrapper around
a valid record. Going from enr.Record to enode.Node requires a valid
signature.
* p2p/discover: port to p2p/enode
This ports the discovery code to the new node representation in
p2p/enode. The wire protocol is unchanged, this can be considered a
refactoring change. The Kademlia table can now deal with nodes using an
arbitrary identity scheme. This requires a few incompatible API changes:
- Table.Lookup is not available anymore. It used to take a public key
as argument because v4 protocol requires one. Its replacement is
LookupRandom.
- Table.Resolve takes *enode.Node instead of NodeID. This is also for
v4 protocol compatibility because nodes cannot be looked up by ID
alone.
- Types Node and NodeID are gone. Further commits in the series will be
fixes all over the the codebase to deal with those removals.
* p2p: port to p2p/enode and discovery changes
This adapts package p2p to the changes in p2p/discover. All uses of
discover.Node and discover.NodeID are replaced by their equivalents from
p2p/enode.
New API is added to retrieve the enode.Node instance of a peer. The
behavior of Server.Self with discovery disabled is improved. It now
tries much harder to report a working IP address, falling back to
127.0.0.1 if no suitable address can be determined through other means.
These changes were needed for tests of other packages later in the
series.
* p2p/simulations, p2p/testing: port to p2p/enode
No surprises here, mostly replacements of discover.Node, discover.NodeID
with their new equivalents. The 'interesting' API changes are:
- testing.ProtocolSession tracks complete nodes, not just their IDs.
- adapters.NodeConfig has a new method to create a complete node.
These changes were needed to make swarm tests work.
Note that the NodeID change makes the code incompatible with old
simulation snapshots.
* whisper/whisperv5, whisper/whisperv6: port to p2p/enode
This port was easy because whisper uses []byte for node IDs and
URL strings in the API.
* eth: port to p2p/enode
Again, easy to port because eth uses strings for node IDs and doesn't
care about node information in any way.
* les: port to p2p/enode
Apart from replacing discover.NodeID with enode.ID, most changes are in
the server pool code. It now deals with complete nodes instead
of (Pubkey, IP, Port) triples. The database format is unchanged for now,
but we should probably change it to use the node database later.
* node: port to p2p/enode
This change simply replaces discover.Node and discover.NodeID with their
new equivalents.
* swarm/network: port to p2p/enode
Swarm has its own node address representation, BzzAddr, containing both
an overlay address (the hash of a secp256k1 public key) and an underlay
address (enode:// URL).
There are no changes to the BzzAddr format in this commit, but certain
operations such as creating a BzzAddr from a node ID are now impossible
because node IDs aren't public keys anymore.
Most swarm-related changes in the series remove uses of
NewAddrFromNodeID, replacing it with NewAddr which takes a complete node
as argument. ToOverlayAddr is removed because we can just use the node
ID directly.
2018-09-25 01:59:00 +03:00
|
|
|
key := newkey()
|
2019-01-29 19:39:20 +03:00
|
|
|
ip := net.IP{10, 13, 0, byte(i)}
|
|
|
|
n := wrapNode(enode.NewV4(&key.PublicKey, ip, 0, 2000))
|
|
|
|
// Ensure half of table content isn't verified live yet.
|
|
|
|
if i > numCandidates/2 {
|
|
|
|
n.livenessChecks = 1
|
|
|
|
live[n.ID()] = true
|
|
|
|
}
|
|
|
|
nodes.push(n, numCandidates)
|
2015-01-27 15:33:26 +02:00
|
|
|
}
|
2019-01-29 19:39:20 +03:00
|
|
|
fillTable(test.table, nodes.entries)
|
2015-03-25 17:45:53 +02:00
|
|
|
|
|
|
|
// ensure there's a bond with the test node,
|
|
|
|
// findnode won't be accepted otherwise.
|
2020-06-02 14:20:19 +03:00
|
|
|
remoteID := v4wire.EncodePubkey(&test.remotekey.PublicKey).ID()
|
2019-01-29 19:39:20 +03:00
|
|
|
test.table.db.UpdateLastPongReceived(remoteID, test.remoteaddr.IP, time.Now())
|
2018-02-16 22:16:23 +03:00
|
|
|
|
2015-03-25 17:45:53 +02:00
|
|
|
// check that closest neighbors are returned.
|
2020-08-24 15:42:39 +03:00
|
|
|
expected := test.table.findnodeByID(testTarget.ID(), bucketSize, true)
|
2020-06-02 14:20:19 +03:00
|
|
|
test.packetIn(nil, &v4wire.Findnode{Target: testTarget, Expiration: futureExp})
|
all: new p2p node representation (#17643)
Package p2p/enode provides a generalized representation of p2p nodes
which can contain arbitrary information in key/value pairs. It is also
the new home for the node database. The "v4" identity scheme is also
moved here from p2p/enr to remove the dependency on Ethereum crypto from
that package.
Record signature handling is changed significantly. The identity scheme
registry is removed and acceptable schemes must be passed to any method
that needs identity. This means records must now be validated explicitly
after decoding.
The enode API is designed to make signature handling easy and safe: most
APIs around the codebase work with enode.Node, which is a wrapper around
a valid record. Going from enr.Record to enode.Node requires a valid
signature.
* p2p/discover: port to p2p/enode
This ports the discovery code to the new node representation in
p2p/enode. The wire protocol is unchanged, this can be considered a
refactoring change. The Kademlia table can now deal with nodes using an
arbitrary identity scheme. This requires a few incompatible API changes:
- Table.Lookup is not available anymore. It used to take a public key
as argument because v4 protocol requires one. Its replacement is
LookupRandom.
- Table.Resolve takes *enode.Node instead of NodeID. This is also for
v4 protocol compatibility because nodes cannot be looked up by ID
alone.
- Types Node and NodeID are gone. Further commits in the series will be
fixes all over the the codebase to deal with those removals.
* p2p: port to p2p/enode and discovery changes
This adapts package p2p to the changes in p2p/discover. All uses of
discover.Node and discover.NodeID are replaced by their equivalents from
p2p/enode.
New API is added to retrieve the enode.Node instance of a peer. The
behavior of Server.Self with discovery disabled is improved. It now
tries much harder to report a working IP address, falling back to
127.0.0.1 if no suitable address can be determined through other means.
These changes were needed for tests of other packages later in the
series.
* p2p/simulations, p2p/testing: port to p2p/enode
No surprises here, mostly replacements of discover.Node, discover.NodeID
with their new equivalents. The 'interesting' API changes are:
- testing.ProtocolSession tracks complete nodes, not just their IDs.
- adapters.NodeConfig has a new method to create a complete node.
These changes were needed to make swarm tests work.
Note that the NodeID change makes the code incompatible with old
simulation snapshots.
* whisper/whisperv5, whisper/whisperv6: port to p2p/enode
This port was easy because whisper uses []byte for node IDs and
URL strings in the API.
* eth: port to p2p/enode
Again, easy to port because eth uses strings for node IDs and doesn't
care about node information in any way.
* les: port to p2p/enode
Apart from replacing discover.NodeID with enode.ID, most changes are in
the server pool code. It now deals with complete nodes instead
of (Pubkey, IP, Port) triples. The database format is unchanged for now,
but we should probably change it to use the node database later.
* node: port to p2p/enode
This change simply replaces discover.Node and discover.NodeID with their
new equivalents.
* swarm/network: port to p2p/enode
Swarm has its own node address representation, BzzAddr, containing both
an overlay address (the hash of a secp256k1 public key) and an underlay
address (enode:// URL).
There are no changes to the BzzAddr format in this commit, but certain
operations such as creating a BzzAddr from a node ID are now impossible
because node IDs aren't public keys anymore.
Most swarm-related changes in the series remove uses of
NewAddrFromNodeID, replacing it with NewAddr which takes a complete node
as argument. ToOverlayAddr is removed because we can just use the node
ID directly.
2018-09-25 01:59:00 +03:00
|
|
|
waitNeighbors := func(want []*node) {
|
2020-06-02 14:20:19 +03:00
|
|
|
test.waitPacketOut(func(p *v4wire.Neighbors, to *net.UDPAddr, hash []byte) {
|
2015-05-13 22:29:32 +03:00
|
|
|
if len(p.Nodes) != len(want) {
|
|
|
|
t.Errorf("wrong number of results: got %d, want %d", len(p.Nodes), bucketSize)
|
2022-06-07 13:15:22 +03:00
|
|
|
return
|
2015-05-13 21:03:17 +03:00
|
|
|
}
|
2019-01-29 19:39:20 +03:00
|
|
|
for i, n := range p.Nodes {
|
2020-06-02 14:20:19 +03:00
|
|
|
if n.ID.ID() != want[i].ID() {
|
2019-01-29 19:39:20 +03:00
|
|
|
t.Errorf("result mismatch at %d:\n got: %v\n want: %v", i, n, expected.entries[i])
|
|
|
|
}
|
2020-06-02 14:20:19 +03:00
|
|
|
if !live[n.ID.ID()] {
|
|
|
|
t.Errorf("result includes dead node %v", n.ID.ID())
|
2015-05-13 22:29:32 +03:00
|
|
|
}
|
2015-02-09 12:02:32 +02:00
|
|
|
}
|
2015-05-13 22:29:32 +03:00
|
|
|
})
|
|
|
|
}
|
2019-01-29 19:39:20 +03:00
|
|
|
// Receive replies.
|
|
|
|
want := expected.entries
|
2020-06-02 14:20:19 +03:00
|
|
|
if len(want) > v4wire.MaxNeighbors {
|
|
|
|
waitNeighbors(want[:v4wire.MaxNeighbors])
|
|
|
|
want = want[v4wire.MaxNeighbors:]
|
2019-01-29 19:39:20 +03:00
|
|
|
}
|
|
|
|
waitNeighbors(want)
|
2015-01-27 15:33:26 +02:00
|
|
|
}
|
|
|
|
|
2019-04-30 14:13:22 +03:00
|
|
|
func TestUDPv4_findnodeMultiReply(t *testing.T) {
|
2015-03-25 17:45:53 +02:00
|
|
|
test := newUDPTest(t)
|
2019-01-29 19:39:20 +03:00
|
|
|
defer test.close()
|
2015-01-27 15:33:26 +02:00
|
|
|
|
all: new p2p node representation (#17643)
Package p2p/enode provides a generalized representation of p2p nodes
which can contain arbitrary information in key/value pairs. It is also
the new home for the node database. The "v4" identity scheme is also
moved here from p2p/enr to remove the dependency on Ethereum crypto from
that package.
Record signature handling is changed significantly. The identity scheme
registry is removed and acceptable schemes must be passed to any method
that needs identity. This means records must now be validated explicitly
after decoding.
The enode API is designed to make signature handling easy and safe: most
APIs around the codebase work with enode.Node, which is a wrapper around
a valid record. Going from enr.Record to enode.Node requires a valid
signature.
* p2p/discover: port to p2p/enode
This ports the discovery code to the new node representation in
p2p/enode. The wire protocol is unchanged, this can be considered a
refactoring change. The Kademlia table can now deal with nodes using an
arbitrary identity scheme. This requires a few incompatible API changes:
- Table.Lookup is not available anymore. It used to take a public key
as argument because v4 protocol requires one. Its replacement is
LookupRandom.
- Table.Resolve takes *enode.Node instead of NodeID. This is also for
v4 protocol compatibility because nodes cannot be looked up by ID
alone.
- Types Node and NodeID are gone. Further commits in the series will be
fixes all over the the codebase to deal with those removals.
* p2p: port to p2p/enode and discovery changes
This adapts package p2p to the changes in p2p/discover. All uses of
discover.Node and discover.NodeID are replaced by their equivalents from
p2p/enode.
New API is added to retrieve the enode.Node instance of a peer. The
behavior of Server.Self with discovery disabled is improved. It now
tries much harder to report a working IP address, falling back to
127.0.0.1 if no suitable address can be determined through other means.
These changes were needed for tests of other packages later in the
series.
* p2p/simulations, p2p/testing: port to p2p/enode
No surprises here, mostly replacements of discover.Node, discover.NodeID
with their new equivalents. The 'interesting' API changes are:
- testing.ProtocolSession tracks complete nodes, not just their IDs.
- adapters.NodeConfig has a new method to create a complete node.
These changes were needed to make swarm tests work.
Note that the NodeID change makes the code incompatible with old
simulation snapshots.
* whisper/whisperv5, whisper/whisperv6: port to p2p/enode
This port was easy because whisper uses []byte for node IDs and
URL strings in the API.
* eth: port to p2p/enode
Again, easy to port because eth uses strings for node IDs and doesn't
care about node information in any way.
* les: port to p2p/enode
Apart from replacing discover.NodeID with enode.ID, most changes are in
the server pool code. It now deals with complete nodes instead
of (Pubkey, IP, Port) triples. The database format is unchanged for now,
but we should probably change it to use the node database later.
* node: port to p2p/enode
This change simply replaces discover.Node and discover.NodeID with their
new equivalents.
* swarm/network: port to p2p/enode
Swarm has its own node address representation, BzzAddr, containing both
an overlay address (the hash of a secp256k1 public key) and an underlay
address (enode:// URL).
There are no changes to the BzzAddr format in this commit, but certain
operations such as creating a BzzAddr from a node ID are now impossible
because node IDs aren't public keys anymore.
Most swarm-related changes in the series remove uses of
NewAddrFromNodeID, replacing it with NewAddr which takes a complete node
as argument. ToOverlayAddr is removed because we can just use the node
ID directly.
2018-09-25 01:59:00 +03:00
|
|
|
rid := enode.PubkeyToIDV4(&test.remotekey.PublicKey)
|
2019-01-29 19:39:20 +03:00
|
|
|
test.table.db.UpdateLastPingReceived(rid, test.remoteaddr.IP, time.Now())
|
2018-07-03 16:24:12 +03:00
|
|
|
|
2015-03-25 17:45:53 +02:00
|
|
|
// queue a pending findnode request
|
2022-09-23 11:51:12 +03:00
|
|
|
resultc, errc := make(chan []*node, 1), make(chan error, 1)
|
2015-03-25 17:45:53 +02:00
|
|
|
go func() {
|
all: new p2p node representation (#17643)
Package p2p/enode provides a generalized representation of p2p nodes
which can contain arbitrary information in key/value pairs. It is also
the new home for the node database. The "v4" identity scheme is also
moved here from p2p/enr to remove the dependency on Ethereum crypto from
that package.
Record signature handling is changed significantly. The identity scheme
registry is removed and acceptable schemes must be passed to any method
that needs identity. This means records must now be validated explicitly
after decoding.
The enode API is designed to make signature handling easy and safe: most
APIs around the codebase work with enode.Node, which is a wrapper around
a valid record. Going from enr.Record to enode.Node requires a valid
signature.
* p2p/discover: port to p2p/enode
This ports the discovery code to the new node representation in
p2p/enode. The wire protocol is unchanged, this can be considered a
refactoring change. The Kademlia table can now deal with nodes using an
arbitrary identity scheme. This requires a few incompatible API changes:
- Table.Lookup is not available anymore. It used to take a public key
as argument because v4 protocol requires one. Its replacement is
LookupRandom.
- Table.Resolve takes *enode.Node instead of NodeID. This is also for
v4 protocol compatibility because nodes cannot be looked up by ID
alone.
- Types Node and NodeID are gone. Further commits in the series will be
fixes all over the the codebase to deal with those removals.
* p2p: port to p2p/enode and discovery changes
This adapts package p2p to the changes in p2p/discover. All uses of
discover.Node and discover.NodeID are replaced by their equivalents from
p2p/enode.
New API is added to retrieve the enode.Node instance of a peer. The
behavior of Server.Self with discovery disabled is improved. It now
tries much harder to report a working IP address, falling back to
127.0.0.1 if no suitable address can be determined through other means.
These changes were needed for tests of other packages later in the
series.
* p2p/simulations, p2p/testing: port to p2p/enode
No surprises here, mostly replacements of discover.Node, discover.NodeID
with their new equivalents. The 'interesting' API changes are:
- testing.ProtocolSession tracks complete nodes, not just their IDs.
- adapters.NodeConfig has a new method to create a complete node.
These changes were needed to make swarm tests work.
Note that the NodeID change makes the code incompatible with old
simulation snapshots.
* whisper/whisperv5, whisper/whisperv6: port to p2p/enode
This port was easy because whisper uses []byte for node IDs and
URL strings in the API.
* eth: port to p2p/enode
Again, easy to port because eth uses strings for node IDs and doesn't
care about node information in any way.
* les: port to p2p/enode
Apart from replacing discover.NodeID with enode.ID, most changes are in
the server pool code. It now deals with complete nodes instead
of (Pubkey, IP, Port) triples. The database format is unchanged for now,
but we should probably change it to use the node database later.
* node: port to p2p/enode
This change simply replaces discover.Node and discover.NodeID with their
new equivalents.
* swarm/network: port to p2p/enode
Swarm has its own node address representation, BzzAddr, containing both
an overlay address (the hash of a secp256k1 public key) and an underlay
address (enode:// URL).
There are no changes to the BzzAddr format in this commit, but certain
operations such as creating a BzzAddr from a node ID are now impossible
because node IDs aren't public keys anymore.
Most swarm-related changes in the series remove uses of
NewAddrFromNodeID, replacing it with NewAddr which takes a complete node
as argument. ToOverlayAddr is removed because we can just use the node
ID directly.
2018-09-25 01:59:00 +03:00
|
|
|
rid := encodePubkey(&test.remotekey.PublicKey).id()
|
2015-03-25 17:45:53 +02:00
|
|
|
ns, err := test.udp.findnode(rid, test.remoteaddr, testTarget)
|
|
|
|
if err != nil && len(ns) == 0 {
|
|
|
|
errc <- err
|
|
|
|
} else {
|
|
|
|
resultc <- ns
|
|
|
|
}
|
|
|
|
}()
|
2015-01-27 15:33:26 +02:00
|
|
|
|
2015-03-25 17:45:53 +02:00
|
|
|
// wait for the findnode to be sent.
|
|
|
|
// after it is sent, the transport is waiting for a reply
|
2020-06-02 14:20:19 +03:00
|
|
|
test.waitPacketOut(func(p *v4wire.Findnode, to *net.UDPAddr, hash []byte) {
|
2015-03-25 17:45:53 +02:00
|
|
|
if p.Target != testTarget {
|
|
|
|
t.Errorf("wrong target: got %v, want %v", p.Target, testTarget)
|
|
|
|
}
|
|
|
|
})
|
2015-01-27 15:33:26 +02:00
|
|
|
|
2015-03-25 17:45:53 +02:00
|
|
|
// send the reply as two packets.
|
all: new p2p node representation (#17643)
Package p2p/enode provides a generalized representation of p2p nodes
which can contain arbitrary information in key/value pairs. It is also
the new home for the node database. The "v4" identity scheme is also
moved here from p2p/enr to remove the dependency on Ethereum crypto from
that package.
Record signature handling is changed significantly. The identity scheme
registry is removed and acceptable schemes must be passed to any method
that needs identity. This means records must now be validated explicitly
after decoding.
The enode API is designed to make signature handling easy and safe: most
APIs around the codebase work with enode.Node, which is a wrapper around
a valid record. Going from enr.Record to enode.Node requires a valid
signature.
* p2p/discover: port to p2p/enode
This ports the discovery code to the new node representation in
p2p/enode. The wire protocol is unchanged, this can be considered a
refactoring change. The Kademlia table can now deal with nodes using an
arbitrary identity scheme. This requires a few incompatible API changes:
- Table.Lookup is not available anymore. It used to take a public key
as argument because v4 protocol requires one. Its replacement is
LookupRandom.
- Table.Resolve takes *enode.Node instead of NodeID. This is also for
v4 protocol compatibility because nodes cannot be looked up by ID
alone.
- Types Node and NodeID are gone. Further commits in the series will be
fixes all over the the codebase to deal with those removals.
* p2p: port to p2p/enode and discovery changes
This adapts package p2p to the changes in p2p/discover. All uses of
discover.Node and discover.NodeID are replaced by their equivalents from
p2p/enode.
New API is added to retrieve the enode.Node instance of a peer. The
behavior of Server.Self with discovery disabled is improved. It now
tries much harder to report a working IP address, falling back to
127.0.0.1 if no suitable address can be determined through other means.
These changes were needed for tests of other packages later in the
series.
* p2p/simulations, p2p/testing: port to p2p/enode
No surprises here, mostly replacements of discover.Node, discover.NodeID
with their new equivalents. The 'interesting' API changes are:
- testing.ProtocolSession tracks complete nodes, not just their IDs.
- adapters.NodeConfig has a new method to create a complete node.
These changes were needed to make swarm tests work.
Note that the NodeID change makes the code incompatible with old
simulation snapshots.
* whisper/whisperv5, whisper/whisperv6: port to p2p/enode
This port was easy because whisper uses []byte for node IDs and
URL strings in the API.
* eth: port to p2p/enode
Again, easy to port because eth uses strings for node IDs and doesn't
care about node information in any way.
* les: port to p2p/enode
Apart from replacing discover.NodeID with enode.ID, most changes are in
the server pool code. It now deals with complete nodes instead
of (Pubkey, IP, Port) triples. The database format is unchanged for now,
but we should probably change it to use the node database later.
* node: port to p2p/enode
This change simply replaces discover.Node and discover.NodeID with their
new equivalents.
* swarm/network: port to p2p/enode
Swarm has its own node address representation, BzzAddr, containing both
an overlay address (the hash of a secp256k1 public key) and an underlay
address (enode:// URL).
There are no changes to the BzzAddr format in this commit, but certain
operations such as creating a BzzAddr from a node ID are now impossible
because node IDs aren't public keys anymore.
Most swarm-related changes in the series remove uses of
NewAddrFromNodeID, replacing it with NewAddr which takes a complete node
as argument. ToOverlayAddr is removed because we can just use the node
ID directly.
2018-09-25 01:59:00 +03:00
|
|
|
list := []*node{
|
2019-06-07 16:31:00 +03:00
|
|
|
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://9bffefd833d53fac8e652415f4973bee289e8b1a5c6c4cbe70abf817ce8a64cee11b823b66a987f51aaa9fba0d6a91b3e6bf0d5a5d1042de8e9eeea057b217f8@10.0.1.36:30301?discport=17")),
|
|
|
|
wrapNode(enode.MustParse("enode://1b5b4aa662d7cb44a7221bfba67302590b643028197a7d5214790f3bac7aaa4a3241be9e83c09cf1f6c69d007c634faae3dc1b1221793e8446c0b3a09de65960@10.0.1.16:30303")),
|
2015-01-27 15:33:26 +02:00
|
|
|
}
|
2020-06-02 14:20:19 +03:00
|
|
|
rpclist := make([]v4wire.Node, len(list))
|
2015-04-23 13:11:21 +03:00
|
|
|
for i := range list {
|
|
|
|
rpclist[i] = nodeToRPC(list[i])
|
|
|
|
}
|
2020-06-02 14:20:19 +03:00
|
|
|
test.packetIn(nil, &v4wire.Neighbors{Expiration: futureExp, Nodes: rpclist[:2]})
|
|
|
|
test.packetIn(nil, &v4wire.Neighbors{Expiration: futureExp, Nodes: rpclist[2:]})
|
2015-01-27 15:33:26 +02:00
|
|
|
|
2015-03-25 17:45:53 +02:00
|
|
|
// check that the sent neighbors are all returned by findnode
|
|
|
|
select {
|
|
|
|
case result := <-resultc:
|
2016-11-21 20:11:54 +03:00
|
|
|
want := append(list[:2], list[3:]...)
|
|
|
|
if !reflect.DeepEqual(result, want) {
|
|
|
|
t.Errorf("neighbors mismatch:\n got: %v\n want: %v", result, want)
|
2015-03-25 17:45:53 +02:00
|
|
|
}
|
|
|
|
case err := <-errc:
|
|
|
|
t.Errorf("findnode error: %v", err)
|
|
|
|
case <-time.After(5 * time.Second):
|
|
|
|
t.Error("findnode did not return within 5 seconds")
|
2015-01-27 15:33:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-15 07:47:45 +03:00
|
|
|
// This test checks that reply matching of pong verifies the ping hash.
|
2019-04-30 14:13:22 +03:00
|
|
|
func TestUDPv4_pingMatch(t *testing.T) {
|
2019-01-29 19:39:20 +03:00
|
|
|
test := newUDPTest(t)
|
|
|
|
defer test.close()
|
|
|
|
|
|
|
|
randToken := make([]byte, 32)
|
|
|
|
crand.Read(randToken)
|
|
|
|
|
2020-06-02 14:20:19 +03:00
|
|
|
test.packetIn(nil, &v4wire.Ping{From: testRemote, To: testLocalAnnounced, Version: 4, Expiration: futureExp})
|
|
|
|
test.waitPacketOut(func(*v4wire.Pong, *net.UDPAddr, []byte) {})
|
|
|
|
test.waitPacketOut(func(*v4wire.Ping, *net.UDPAddr, []byte) {})
|
|
|
|
test.packetIn(errUnsolicitedReply, &v4wire.Pong{ReplyTok: randToken, To: testLocalAnnounced, Expiration: futureExp})
|
2019-01-29 19:39:20 +03:00
|
|
|
}
|
|
|
|
|
2019-05-15 07:47:45 +03:00
|
|
|
// This test checks that reply matching of pong verifies the sender IP address.
|
2019-04-30 14:13:22 +03:00
|
|
|
func TestUDPv4_pingMatchIP(t *testing.T) {
|
2019-01-29 19:39:20 +03:00
|
|
|
test := newUDPTest(t)
|
|
|
|
defer test.close()
|
|
|
|
|
2020-06-02 14:20:19 +03:00
|
|
|
test.packetIn(nil, &v4wire.Ping{From: testRemote, To: testLocalAnnounced, Version: 4, Expiration: futureExp})
|
|
|
|
test.waitPacketOut(func(*v4wire.Pong, *net.UDPAddr, []byte) {})
|
2019-01-29 19:39:20 +03:00
|
|
|
|
2020-06-02 14:20:19 +03:00
|
|
|
test.waitPacketOut(func(p *v4wire.Ping, to *net.UDPAddr, hash []byte) {
|
2019-04-30 14:13:22 +03:00
|
|
|
wrongAddr := &net.UDPAddr{IP: net.IP{33, 44, 1, 2}, Port: 30000}
|
2020-06-02 14:20:19 +03:00
|
|
|
test.packetInFrom(errUnsolicitedReply, test.remotekey, wrongAddr, &v4wire.Pong{
|
2019-04-30 14:13:22 +03:00
|
|
|
ReplyTok: hash,
|
|
|
|
To: testLocalAnnounced,
|
|
|
|
Expiration: futureExp,
|
|
|
|
})
|
2019-01-29 19:39:20 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-04-30 14:13:22 +03:00
|
|
|
func TestUDPv4_successfulPing(t *testing.T) {
|
2015-03-25 17:45:53 +02:00
|
|
|
test := newUDPTest(t)
|
all: new p2p node representation (#17643)
Package p2p/enode provides a generalized representation of p2p nodes
which can contain arbitrary information in key/value pairs. It is also
the new home for the node database. The "v4" identity scheme is also
moved here from p2p/enr to remove the dependency on Ethereum crypto from
that package.
Record signature handling is changed significantly. The identity scheme
registry is removed and acceptable schemes must be passed to any method
that needs identity. This means records must now be validated explicitly
after decoding.
The enode API is designed to make signature handling easy and safe: most
APIs around the codebase work with enode.Node, which is a wrapper around
a valid record. Going from enr.Record to enode.Node requires a valid
signature.
* p2p/discover: port to p2p/enode
This ports the discovery code to the new node representation in
p2p/enode. The wire protocol is unchanged, this can be considered a
refactoring change. The Kademlia table can now deal with nodes using an
arbitrary identity scheme. This requires a few incompatible API changes:
- Table.Lookup is not available anymore. It used to take a public key
as argument because v4 protocol requires one. Its replacement is
LookupRandom.
- Table.Resolve takes *enode.Node instead of NodeID. This is also for
v4 protocol compatibility because nodes cannot be looked up by ID
alone.
- Types Node and NodeID are gone. Further commits in the series will be
fixes all over the the codebase to deal with those removals.
* p2p: port to p2p/enode and discovery changes
This adapts package p2p to the changes in p2p/discover. All uses of
discover.Node and discover.NodeID are replaced by their equivalents from
p2p/enode.
New API is added to retrieve the enode.Node instance of a peer. The
behavior of Server.Self with discovery disabled is improved. It now
tries much harder to report a working IP address, falling back to
127.0.0.1 if no suitable address can be determined through other means.
These changes were needed for tests of other packages later in the
series.
* p2p/simulations, p2p/testing: port to p2p/enode
No surprises here, mostly replacements of discover.Node, discover.NodeID
with their new equivalents. The 'interesting' API changes are:
- testing.ProtocolSession tracks complete nodes, not just their IDs.
- adapters.NodeConfig has a new method to create a complete node.
These changes were needed to make swarm tests work.
Note that the NodeID change makes the code incompatible with old
simulation snapshots.
* whisper/whisperv5, whisper/whisperv6: port to p2p/enode
This port was easy because whisper uses []byte for node IDs and
URL strings in the API.
* eth: port to p2p/enode
Again, easy to port because eth uses strings for node IDs and doesn't
care about node information in any way.
* les: port to p2p/enode
Apart from replacing discover.NodeID with enode.ID, most changes are in
the server pool code. It now deals with complete nodes instead
of (Pubkey, IP, Port) triples. The database format is unchanged for now,
but we should probably change it to use the node database later.
* node: port to p2p/enode
This change simply replaces discover.Node and discover.NodeID with their
new equivalents.
* swarm/network: port to p2p/enode
Swarm has its own node address representation, BzzAddr, containing both
an overlay address (the hash of a secp256k1 public key) and an underlay
address (enode:// URL).
There are no changes to the BzzAddr format in this commit, but certain
operations such as creating a BzzAddr from a node ID are now impossible
because node IDs aren't public keys anymore.
Most swarm-related changes in the series remove uses of
NewAddrFromNodeID, replacing it with NewAddr which takes a complete node
as argument. ToOverlayAddr is removed because we can just use the node
ID directly.
2018-09-25 01:59:00 +03:00
|
|
|
added := make(chan *node, 1)
|
|
|
|
test.table.nodeAddedHook = func(n *node) { added <- n }
|
2019-01-29 19:39:20 +03:00
|
|
|
defer test.close()
|
2015-01-27 15:33:26 +02:00
|
|
|
|
2015-06-10 14:07:30 +03:00
|
|
|
// The remote side sends a ping packet to initiate the exchange.
|
2020-06-02 14:20:19 +03:00
|
|
|
go test.packetIn(nil, &v4wire.Ping{From: testRemote, To: testLocalAnnounced, Version: 4, Expiration: futureExp})
|
2015-02-09 12:02:32 +02:00
|
|
|
|
2019-05-15 07:47:45 +03:00
|
|
|
// The ping is replied to.
|
2020-06-02 14:20:19 +03:00
|
|
|
test.waitPacketOut(func(p *v4wire.Pong, to *net.UDPAddr, hash []byte) {
|
|
|
|
pinghash := test.sent[0][:32]
|
2015-03-25 17:45:53 +02:00
|
|
|
if !bytes.Equal(p.ReplyTok, pinghash) {
|
2015-04-18 02:50:31 +03:00
|
|
|
t.Errorf("got pong.ReplyTok %x, want %x", p.ReplyTok, pinghash)
|
|
|
|
}
|
2020-06-02 14:20:19 +03:00
|
|
|
wantTo := v4wire.Endpoint{
|
2015-04-18 02:50:31 +03:00
|
|
|
// The mirrored UDP address is the UDP packet sender
|
|
|
|
IP: test.remoteaddr.IP, UDP: uint16(test.remoteaddr.Port),
|
|
|
|
// The mirrored TCP port is the one from the ping packet
|
|
|
|
TCP: testRemote.TCP,
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(p.To, wantTo) {
|
|
|
|
t.Errorf("got pong.To %v, want %v", p.To, wantTo)
|
2015-01-27 15:33:26 +02:00
|
|
|
}
|
2015-03-25 17:45:53 +02:00
|
|
|
})
|
2015-01-27 15:33:26 +02:00
|
|
|
|
2019-05-15 07:47:45 +03:00
|
|
|
// Remote is unknown, the table pings back.
|
2020-06-02 14:20:19 +03:00
|
|
|
test.waitPacketOut(func(p *v4wire.Ping, to *net.UDPAddr, hash []byte) {
|
2018-10-12 12:47:24 +03:00
|
|
|
if !reflect.DeepEqual(p.From, test.udp.ourEndpoint()) {
|
|
|
|
t.Errorf("got ping.From %#v, want %#v", p.From, test.udp.ourEndpoint())
|
2015-04-18 02:50:31 +03:00
|
|
|
}
|
2020-06-02 14:20:19 +03:00
|
|
|
wantTo := v4wire.Endpoint{
|
2015-04-18 02:50:31 +03:00
|
|
|
// The mirrored UDP address is the UDP packet sender.
|
2018-10-12 12:47:24 +03:00
|
|
|
IP: test.remoteaddr.IP,
|
|
|
|
UDP: uint16(test.remoteaddr.Port),
|
2015-04-18 02:50:31 +03:00
|
|
|
TCP: 0,
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(p.To, wantTo) {
|
|
|
|
t.Errorf("got ping.To %v, want %v", p.To, wantTo)
|
|
|
|
}
|
2020-06-02 14:20:19 +03:00
|
|
|
test.packetIn(nil, &v4wire.Pong{ReplyTok: hash, Expiration: futureExp})
|
2015-04-18 02:50:31 +03:00
|
|
|
})
|
2015-03-25 17:45:53 +02:00
|
|
|
|
2019-05-15 07:47:45 +03:00
|
|
|
// The node should be added to the table shortly after getting the
|
2015-06-10 14:07:30 +03:00
|
|
|
// pong packet.
|
|
|
|
select {
|
|
|
|
case n := <-added:
|
all: new p2p node representation (#17643)
Package p2p/enode provides a generalized representation of p2p nodes
which can contain arbitrary information in key/value pairs. It is also
the new home for the node database. The "v4" identity scheme is also
moved here from p2p/enr to remove the dependency on Ethereum crypto from
that package.
Record signature handling is changed significantly. The identity scheme
registry is removed and acceptable schemes must be passed to any method
that needs identity. This means records must now be validated explicitly
after decoding.
The enode API is designed to make signature handling easy and safe: most
APIs around the codebase work with enode.Node, which is a wrapper around
a valid record. Going from enr.Record to enode.Node requires a valid
signature.
* p2p/discover: port to p2p/enode
This ports the discovery code to the new node representation in
p2p/enode. The wire protocol is unchanged, this can be considered a
refactoring change. The Kademlia table can now deal with nodes using an
arbitrary identity scheme. This requires a few incompatible API changes:
- Table.Lookup is not available anymore. It used to take a public key
as argument because v4 protocol requires one. Its replacement is
LookupRandom.
- Table.Resolve takes *enode.Node instead of NodeID. This is also for
v4 protocol compatibility because nodes cannot be looked up by ID
alone.
- Types Node and NodeID are gone. Further commits in the series will be
fixes all over the the codebase to deal with those removals.
* p2p: port to p2p/enode and discovery changes
This adapts package p2p to the changes in p2p/discover. All uses of
discover.Node and discover.NodeID are replaced by their equivalents from
p2p/enode.
New API is added to retrieve the enode.Node instance of a peer. The
behavior of Server.Self with discovery disabled is improved. It now
tries much harder to report a working IP address, falling back to
127.0.0.1 if no suitable address can be determined through other means.
These changes were needed for tests of other packages later in the
series.
* p2p/simulations, p2p/testing: port to p2p/enode
No surprises here, mostly replacements of discover.Node, discover.NodeID
with their new equivalents. The 'interesting' API changes are:
- testing.ProtocolSession tracks complete nodes, not just their IDs.
- adapters.NodeConfig has a new method to create a complete node.
These changes were needed to make swarm tests work.
Note that the NodeID change makes the code incompatible with old
simulation snapshots.
* whisper/whisperv5, whisper/whisperv6: port to p2p/enode
This port was easy because whisper uses []byte for node IDs and
URL strings in the API.
* eth: port to p2p/enode
Again, easy to port because eth uses strings for node IDs and doesn't
care about node information in any way.
* les: port to p2p/enode
Apart from replacing discover.NodeID with enode.ID, most changes are in
the server pool code. It now deals with complete nodes instead
of (Pubkey, IP, Port) triples. The database format is unchanged for now,
but we should probably change it to use the node database later.
* node: port to p2p/enode
This change simply replaces discover.Node and discover.NodeID with their
new equivalents.
* swarm/network: port to p2p/enode
Swarm has its own node address representation, BzzAddr, containing both
an overlay address (the hash of a secp256k1 public key) and an underlay
address (enode:// URL).
There are no changes to the BzzAddr format in this commit, but certain
operations such as creating a BzzAddr from a node ID are now impossible
because node IDs aren't public keys anymore.
Most swarm-related changes in the series remove uses of
NewAddrFromNodeID, replacing it with NewAddr which takes a complete node
as argument. ToOverlayAddr is removed because we can just use the node
ID directly.
2018-09-25 01:59:00 +03:00
|
|
|
rid := encodePubkey(&test.remotekey.PublicKey).id()
|
|
|
|
if n.ID() != rid {
|
|
|
|
t.Errorf("node has wrong ID: got %v, want %v", n.ID(), rid)
|
2015-02-09 12:02:32 +02:00
|
|
|
}
|
all: new p2p node representation (#17643)
Package p2p/enode provides a generalized representation of p2p nodes
which can contain arbitrary information in key/value pairs. It is also
the new home for the node database. The "v4" identity scheme is also
moved here from p2p/enr to remove the dependency on Ethereum crypto from
that package.
Record signature handling is changed significantly. The identity scheme
registry is removed and acceptable schemes must be passed to any method
that needs identity. This means records must now be validated explicitly
after decoding.
The enode API is designed to make signature handling easy and safe: most
APIs around the codebase work with enode.Node, which is a wrapper around
a valid record. Going from enr.Record to enode.Node requires a valid
signature.
* p2p/discover: port to p2p/enode
This ports the discovery code to the new node representation in
p2p/enode. The wire protocol is unchanged, this can be considered a
refactoring change. The Kademlia table can now deal with nodes using an
arbitrary identity scheme. This requires a few incompatible API changes:
- Table.Lookup is not available anymore. It used to take a public key
as argument because v4 protocol requires one. Its replacement is
LookupRandom.
- Table.Resolve takes *enode.Node instead of NodeID. This is also for
v4 protocol compatibility because nodes cannot be looked up by ID
alone.
- Types Node and NodeID are gone. Further commits in the series will be
fixes all over the the codebase to deal with those removals.
* p2p: port to p2p/enode and discovery changes
This adapts package p2p to the changes in p2p/discover. All uses of
discover.Node and discover.NodeID are replaced by their equivalents from
p2p/enode.
New API is added to retrieve the enode.Node instance of a peer. The
behavior of Server.Self with discovery disabled is improved. It now
tries much harder to report a working IP address, falling back to
127.0.0.1 if no suitable address can be determined through other means.
These changes were needed for tests of other packages later in the
series.
* p2p/simulations, p2p/testing: port to p2p/enode
No surprises here, mostly replacements of discover.Node, discover.NodeID
with their new equivalents. The 'interesting' API changes are:
- testing.ProtocolSession tracks complete nodes, not just their IDs.
- adapters.NodeConfig has a new method to create a complete node.
These changes were needed to make swarm tests work.
Note that the NodeID change makes the code incompatible with old
simulation snapshots.
* whisper/whisperv5, whisper/whisperv6: port to p2p/enode
This port was easy because whisper uses []byte for node IDs and
URL strings in the API.
* eth: port to p2p/enode
Again, easy to port because eth uses strings for node IDs and doesn't
care about node information in any way.
* les: port to p2p/enode
Apart from replacing discover.NodeID with enode.ID, most changes are in
the server pool code. It now deals with complete nodes instead
of (Pubkey, IP, Port) triples. The database format is unchanged for now,
but we should probably change it to use the node database later.
* node: port to p2p/enode
This change simply replaces discover.Node and discover.NodeID with their
new equivalents.
* swarm/network: port to p2p/enode
Swarm has its own node address representation, BzzAddr, containing both
an overlay address (the hash of a secp256k1 public key) and an underlay
address (enode:// URL).
There are no changes to the BzzAddr format in this commit, but certain
operations such as creating a BzzAddr from a node ID are now impossible
because node IDs aren't public keys anymore.
Most swarm-related changes in the series remove uses of
NewAddrFromNodeID, replacing it with NewAddr which takes a complete node
as argument. ToOverlayAddr is removed because we can just use the node
ID directly.
2018-09-25 01:59:00 +03:00
|
|
|
if !n.IP().Equal(test.remoteaddr.IP) {
|
|
|
|
t.Errorf("node has wrong IP: got %v, want: %v", n.IP(), test.remoteaddr.IP)
|
2015-06-10 14:07:30 +03:00
|
|
|
}
|
2019-07-22 10:30:09 +03:00
|
|
|
if n.UDP() != test.remoteaddr.Port {
|
all: new p2p node representation (#17643)
Package p2p/enode provides a generalized representation of p2p nodes
which can contain arbitrary information in key/value pairs. It is also
the new home for the node database. The "v4" identity scheme is also
moved here from p2p/enr to remove the dependency on Ethereum crypto from
that package.
Record signature handling is changed significantly. The identity scheme
registry is removed and acceptable schemes must be passed to any method
that needs identity. This means records must now be validated explicitly
after decoding.
The enode API is designed to make signature handling easy and safe: most
APIs around the codebase work with enode.Node, which is a wrapper around
a valid record. Going from enr.Record to enode.Node requires a valid
signature.
* p2p/discover: port to p2p/enode
This ports the discovery code to the new node representation in
p2p/enode. The wire protocol is unchanged, this can be considered a
refactoring change. The Kademlia table can now deal with nodes using an
arbitrary identity scheme. This requires a few incompatible API changes:
- Table.Lookup is not available anymore. It used to take a public key
as argument because v4 protocol requires one. Its replacement is
LookupRandom.
- Table.Resolve takes *enode.Node instead of NodeID. This is also for
v4 protocol compatibility because nodes cannot be looked up by ID
alone.
- Types Node and NodeID are gone. Further commits in the series will be
fixes all over the the codebase to deal with those removals.
* p2p: port to p2p/enode and discovery changes
This adapts package p2p to the changes in p2p/discover. All uses of
discover.Node and discover.NodeID are replaced by their equivalents from
p2p/enode.
New API is added to retrieve the enode.Node instance of a peer. The
behavior of Server.Self with discovery disabled is improved. It now
tries much harder to report a working IP address, falling back to
127.0.0.1 if no suitable address can be determined through other means.
These changes were needed for tests of other packages later in the
series.
* p2p/simulations, p2p/testing: port to p2p/enode
No surprises here, mostly replacements of discover.Node, discover.NodeID
with their new equivalents. The 'interesting' API changes are:
- testing.ProtocolSession tracks complete nodes, not just their IDs.
- adapters.NodeConfig has a new method to create a complete node.
These changes were needed to make swarm tests work.
Note that the NodeID change makes the code incompatible with old
simulation snapshots.
* whisper/whisperv5, whisper/whisperv6: port to p2p/enode
This port was easy because whisper uses []byte for node IDs and
URL strings in the API.
* eth: port to p2p/enode
Again, easy to port because eth uses strings for node IDs and doesn't
care about node information in any way.
* les: port to p2p/enode
Apart from replacing discover.NodeID with enode.ID, most changes are in
the server pool code. It now deals with complete nodes instead
of (Pubkey, IP, Port) triples. The database format is unchanged for now,
but we should probably change it to use the node database later.
* node: port to p2p/enode
This change simply replaces discover.Node and discover.NodeID with their
new equivalents.
* swarm/network: port to p2p/enode
Swarm has its own node address representation, BzzAddr, containing both
an overlay address (the hash of a secp256k1 public key) and an underlay
address (enode:// URL).
There are no changes to the BzzAddr format in this commit, but certain
operations such as creating a BzzAddr from a node ID are now impossible
because node IDs aren't public keys anymore.
Most swarm-related changes in the series remove uses of
NewAddrFromNodeID, replacing it with NewAddr which takes a complete node
as argument. ToOverlayAddr is removed because we can just use the node
ID directly.
2018-09-25 01:59:00 +03:00
|
|
|
t.Errorf("node has wrong UDP port: got %v, want: %v", n.UDP(), test.remoteaddr.Port)
|
2015-06-10 14:07:30 +03:00
|
|
|
}
|
all: new p2p node representation (#17643)
Package p2p/enode provides a generalized representation of p2p nodes
which can contain arbitrary information in key/value pairs. It is also
the new home for the node database. The "v4" identity scheme is also
moved here from p2p/enr to remove the dependency on Ethereum crypto from
that package.
Record signature handling is changed significantly. The identity scheme
registry is removed and acceptable schemes must be passed to any method
that needs identity. This means records must now be validated explicitly
after decoding.
The enode API is designed to make signature handling easy and safe: most
APIs around the codebase work with enode.Node, which is a wrapper around
a valid record. Going from enr.Record to enode.Node requires a valid
signature.
* p2p/discover: port to p2p/enode
This ports the discovery code to the new node representation in
p2p/enode. The wire protocol is unchanged, this can be considered a
refactoring change. The Kademlia table can now deal with nodes using an
arbitrary identity scheme. This requires a few incompatible API changes:
- Table.Lookup is not available anymore. It used to take a public key
as argument because v4 protocol requires one. Its replacement is
LookupRandom.
- Table.Resolve takes *enode.Node instead of NodeID. This is also for
v4 protocol compatibility because nodes cannot be looked up by ID
alone.
- Types Node and NodeID are gone. Further commits in the series will be
fixes all over the the codebase to deal with those removals.
* p2p: port to p2p/enode and discovery changes
This adapts package p2p to the changes in p2p/discover. All uses of
discover.Node and discover.NodeID are replaced by their equivalents from
p2p/enode.
New API is added to retrieve the enode.Node instance of a peer. The
behavior of Server.Self with discovery disabled is improved. It now
tries much harder to report a working IP address, falling back to
127.0.0.1 if no suitable address can be determined through other means.
These changes were needed for tests of other packages later in the
series.
* p2p/simulations, p2p/testing: port to p2p/enode
No surprises here, mostly replacements of discover.Node, discover.NodeID
with their new equivalents. The 'interesting' API changes are:
- testing.ProtocolSession tracks complete nodes, not just their IDs.
- adapters.NodeConfig has a new method to create a complete node.
These changes were needed to make swarm tests work.
Note that the NodeID change makes the code incompatible with old
simulation snapshots.
* whisper/whisperv5, whisper/whisperv6: port to p2p/enode
This port was easy because whisper uses []byte for node IDs and
URL strings in the API.
* eth: port to p2p/enode
Again, easy to port because eth uses strings for node IDs and doesn't
care about node information in any way.
* les: port to p2p/enode
Apart from replacing discover.NodeID with enode.ID, most changes are in
the server pool code. It now deals with complete nodes instead
of (Pubkey, IP, Port) triples. The database format is unchanged for now,
but we should probably change it to use the node database later.
* node: port to p2p/enode
This change simply replaces discover.Node and discover.NodeID with their
new equivalents.
* swarm/network: port to p2p/enode
Swarm has its own node address representation, BzzAddr, containing both
an overlay address (the hash of a secp256k1 public key) and an underlay
address (enode:// URL).
There are no changes to the BzzAddr format in this commit, but certain
operations such as creating a BzzAddr from a node ID are now impossible
because node IDs aren't public keys anymore.
Most swarm-related changes in the series remove uses of
NewAddrFromNodeID, replacing it with NewAddr which takes a complete node
as argument. ToOverlayAddr is removed because we can just use the node
ID directly.
2018-09-25 01:59:00 +03:00
|
|
|
if n.TCP() != int(testRemote.TCP) {
|
|
|
|
t.Errorf("node has wrong TCP port: got %v, want: %v", n.TCP(), testRemote.TCP)
|
2015-06-10 14:07:30 +03:00
|
|
|
}
|
|
|
|
case <-time.After(2 * time.Second):
|
|
|
|
t.Errorf("node was not added within 2 seconds")
|
2015-01-27 15:33:26 +02:00
|
|
|
}
|
2015-02-09 12:02:32 +02:00
|
|
|
}
|
2015-01-27 15:33:26 +02:00
|
|
|
|
2019-05-15 07:47:45 +03:00
|
|
|
// This test checks that EIP-868 requests work.
|
|
|
|
func TestUDPv4_EIP868(t *testing.T) {
|
|
|
|
test := newUDPTest(t)
|
|
|
|
defer test.close()
|
|
|
|
|
|
|
|
test.udp.localNode.Set(enr.WithEntry("foo", "bar"))
|
|
|
|
wantNode := test.udp.localNode.Node()
|
|
|
|
|
|
|
|
// ENR requests aren't allowed before endpoint proof.
|
2020-06-02 14:20:19 +03:00
|
|
|
test.packetIn(errUnknownNode, &v4wire.ENRRequest{Expiration: futureExp})
|
2019-05-15 07:47:45 +03:00
|
|
|
|
|
|
|
// Perform endpoint proof and check for sequence number in packet tail.
|
2020-06-02 14:20:19 +03:00
|
|
|
test.packetIn(nil, &v4wire.Ping{Expiration: futureExp})
|
|
|
|
test.waitPacketOut(func(p *v4wire.Pong, addr *net.UDPAddr, hash []byte) {
|
2021-05-18 12:48:41 +03:00
|
|
|
if p.ENRSeq != wantNode.Seq() {
|
|
|
|
t.Errorf("wrong sequence number in pong: %d, want %d", p.ENRSeq, wantNode.Seq())
|
2019-05-15 07:47:45 +03:00
|
|
|
}
|
|
|
|
})
|
2020-06-02 14:20:19 +03:00
|
|
|
test.waitPacketOut(func(p *v4wire.Ping, addr *net.UDPAddr, hash []byte) {
|
2021-05-18 12:48:41 +03:00
|
|
|
if p.ENRSeq != wantNode.Seq() {
|
|
|
|
t.Errorf("wrong sequence number in ping: %d, want %d", p.ENRSeq, wantNode.Seq())
|
2019-05-15 07:47:45 +03:00
|
|
|
}
|
2020-06-02 14:20:19 +03:00
|
|
|
test.packetIn(nil, &v4wire.Pong{Expiration: futureExp, ReplyTok: hash})
|
2019-05-15 07:47:45 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
// Request should work now.
|
2020-06-02 14:20:19 +03:00
|
|
|
test.packetIn(nil, &v4wire.ENRRequest{Expiration: futureExp})
|
|
|
|
test.waitPacketOut(func(p *v4wire.ENRResponse, addr *net.UDPAddr, hash []byte) {
|
2019-05-15 07:47:45 +03:00
|
|
|
n, err := enode.New(enode.ValidSchemes, &p.Record)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("invalid record: %v", err)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(n, wantNode) {
|
2022-07-10 11:15:54 +03:00
|
|
|
t.Fatalf("wrong node in ENRResponse: %v", n)
|
2019-05-15 07:47:45 +03:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-08-24 15:42:39 +03:00
|
|
|
// This test verifies that a small network of nodes can boot up into a healthy state.
|
|
|
|
func TestUDPv4_smallNetConvergence(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
// Start the network.
|
|
|
|
nodes := make([]*UDPv4, 4)
|
|
|
|
for i := range nodes {
|
|
|
|
var cfg Config
|
|
|
|
if i > 0 {
|
|
|
|
bn := nodes[0].Self()
|
|
|
|
cfg.Bootnodes = []*enode.Node{bn}
|
|
|
|
}
|
|
|
|
nodes[i] = startLocalhostV4(t, cfg)
|
|
|
|
defer nodes[i].Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run through the iterator on all nodes until
|
|
|
|
// they have all found each other.
|
|
|
|
status := make(chan error, len(nodes))
|
|
|
|
for i := range nodes {
|
|
|
|
node := nodes[i]
|
|
|
|
go func() {
|
|
|
|
found := make(map[enode.ID]bool, len(nodes))
|
|
|
|
it := node.RandomNodes()
|
|
|
|
for it.Next() {
|
|
|
|
found[it.Node().ID()] = true
|
|
|
|
if len(found) == len(nodes) {
|
|
|
|
status <- nil
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
status <- fmt.Errorf("node %s didn't find all nodes", node.Self().ID().TerminalString())
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for all status reports.
|
|
|
|
timeout := time.NewTimer(30 * time.Second)
|
|
|
|
defer timeout.Stop()
|
|
|
|
for received := 0; received < len(nodes); {
|
|
|
|
select {
|
|
|
|
case <-timeout.C:
|
|
|
|
for _, node := range nodes {
|
|
|
|
node.Close()
|
|
|
|
}
|
|
|
|
case err := <-status:
|
|
|
|
received++
|
|
|
|
if err != nil {
|
|
|
|
t.Error("ERROR:", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func startLocalhostV4(t *testing.T, cfg Config) *UDPv4 {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
cfg.PrivateKey = newkey()
|
|
|
|
db, _ := enode.OpenDB("")
|
|
|
|
ln := enode.NewLocalNode(db, cfg.PrivateKey)
|
|
|
|
|
|
|
|
// Prefix logs with node ID.
|
|
|
|
lprefix := fmt.Sprintf("(%s)", ln.ID().TerminalString())
|
|
|
|
lfmt := log.TerminalFormat(false)
|
|
|
|
cfg.Log = testlog.Logger(t, log.LvlTrace)
|
|
|
|
cfg.Log.SetHandler(log.FuncHandler(func(r *log.Record) error {
|
|
|
|
t.Logf("%s %s", lprefix, lfmt.Format(r))
|
|
|
|
return nil
|
|
|
|
}))
|
|
|
|
|
|
|
|
// Listen.
|
|
|
|
socket, err := net.ListenUDP("udp4", &net.UDPAddr{IP: net.IP{127, 0, 0, 1}})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
realaddr := socket.LocalAddr().(*net.UDPAddr)
|
|
|
|
ln.SetStaticIP(realaddr.IP)
|
|
|
|
ln.SetFallbackUDP(realaddr.Port)
|
|
|
|
udp, err := ListenV4(socket, ln, cfg)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
return udp
|
|
|
|
}
|
|
|
|
|
2015-03-25 17:45:53 +02:00
|
|
|
// dgramPipe is a fake UDP socket. It queues all sent datagrams.
|
|
|
|
type dgramPipe struct {
|
|
|
|
mu *sync.Mutex
|
|
|
|
cond *sync.Cond
|
|
|
|
closing chan struct{}
|
|
|
|
closed bool
|
2019-01-29 19:39:20 +03:00
|
|
|
queue []dgram
|
|
|
|
}
|
|
|
|
|
|
|
|
type dgram struct {
|
|
|
|
to net.UDPAddr
|
|
|
|
data []byte
|
2015-03-25 17:45:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func newpipe() *dgramPipe {
|
|
|
|
mu := new(sync.Mutex)
|
|
|
|
return &dgramPipe{
|
|
|
|
closing: make(chan struct{}),
|
|
|
|
cond: &sync.Cond{L: mu},
|
|
|
|
mu: mu,
|
2015-01-27 15:33:26 +02:00
|
|
|
}
|
2015-03-25 17:45:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// WriteToUDP queues a datagram.
|
|
|
|
func (c *dgramPipe) WriteToUDP(b []byte, to *net.UDPAddr) (n int, err error) {
|
|
|
|
msg := make([]byte, len(b))
|
|
|
|
copy(msg, b)
|
|
|
|
c.mu.Lock()
|
|
|
|
defer c.mu.Unlock()
|
|
|
|
if c.closed {
|
|
|
|
return 0, errors.New("closed")
|
|
|
|
}
|
2019-01-29 19:39:20 +03:00
|
|
|
c.queue = append(c.queue, dgram{*to, b})
|
2015-03-25 17:45:53 +02:00
|
|
|
c.cond.Signal()
|
|
|
|
return len(b), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReadFromUDP just hangs until the pipe is closed.
|
|
|
|
func (c *dgramPipe) ReadFromUDP(b []byte) (n int, addr *net.UDPAddr, err error) {
|
|
|
|
<-c.closing
|
|
|
|
return 0, nil, io.EOF
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *dgramPipe) Close() error {
|
|
|
|
c.mu.Lock()
|
|
|
|
defer c.mu.Unlock()
|
|
|
|
if !c.closed {
|
|
|
|
close(c.closing)
|
|
|
|
c.closed = true
|
2015-01-27 15:33:26 +02:00
|
|
|
}
|
2019-04-30 14:13:22 +03:00
|
|
|
c.cond.Broadcast()
|
2015-02-09 12:02:32 +02:00
|
|
|
return nil
|
2015-01-27 15:33:26 +02:00
|
|
|
}
|
2015-03-25 17:45:53 +02:00
|
|
|
|
|
|
|
func (c *dgramPipe) LocalAddr() net.Addr {
|
2015-04-18 02:50:31 +03:00
|
|
|
return &net.UDPAddr{IP: testLocal.IP, Port: int(testLocal.UDP)}
|
2015-03-25 17:45:53 +02:00
|
|
|
}
|
|
|
|
|
2020-04-08 10:57:23 +03:00
|
|
|
func (c *dgramPipe) receive() (dgram, error) {
|
2015-03-25 17:45:53 +02:00
|
|
|
c.mu.Lock()
|
|
|
|
defer c.mu.Unlock()
|
2020-04-08 10:57:23 +03:00
|
|
|
|
|
|
|
var timedOut bool
|
|
|
|
timer := time.AfterFunc(3*time.Second, func() {
|
|
|
|
c.mu.Lock()
|
|
|
|
timedOut = true
|
|
|
|
c.mu.Unlock()
|
|
|
|
c.cond.Broadcast()
|
|
|
|
})
|
|
|
|
defer timer.Stop()
|
|
|
|
|
|
|
|
for len(c.queue) == 0 && !c.closed && !timedOut {
|
2015-03-25 17:45:53 +02:00
|
|
|
c.cond.Wait()
|
|
|
|
}
|
2019-04-30 14:13:22 +03:00
|
|
|
if c.closed {
|
2020-04-08 10:57:23 +03:00
|
|
|
return dgram{}, errClosed
|
|
|
|
}
|
|
|
|
if timedOut {
|
|
|
|
return dgram{}, errTimeout
|
2019-04-30 14:13:22 +03:00
|
|
|
}
|
2015-03-25 17:45:53 +02:00
|
|
|
p := c.queue[0]
|
|
|
|
copy(c.queue, c.queue[1:])
|
|
|
|
c.queue = c.queue[:len(c.queue)-1]
|
2020-04-08 10:57:23 +03:00
|
|
|
return p, nil
|
2015-03-25 17:45:53 +02:00
|
|
|
}
|