revert some change

Signed-off-by: Chen Kai <281165273grape@gmail.com>
This commit is contained in:
Chen Kai 2024-12-10 16:16:40 +08:00
parent dc433b38e4
commit fbfb4083e1
4 changed files with 33 additions and 38 deletions

View file

@ -510,10 +510,10 @@ var AllTests = []utesting.Test{
{Name: "Ping/PastExpiration", Fn: PingPastExpiration}, {Name: "Ping/PastExpiration", Fn: PingPastExpiration},
{Name: "Ping/WrongPacketType", Fn: WrongPacketType}, {Name: "Ping/WrongPacketType", Fn: WrongPacketType},
{Name: "Ping/BondThenPingWithWrongFrom", Fn: BondThenPingWithWrongFrom}, {Name: "Ping/BondThenPingWithWrongFrom", Fn: BondThenPingWithWrongFrom},
{Name: "Findnode/WithoutEndpointProof", Fn: FindnodeWithoutEndpointProof}, {Name: "findnode/WithoutEndpointProof", Fn: FindnodeWithoutEndpointProof},
{Name: "Findnode/BasicFindnode", Fn: BasicFindnode}, {Name: "findnode/BasicFindnode", Fn: BasicFindnode},
{Name: "Findnode/UnsolicitedNeighbors", Fn: UnsolicitedNeighbors}, {Name: "findnode/UnsolicitedNeighbors", Fn: UnsolicitedNeighbors},
{Name: "Findnode/PastExpiration", Fn: FindnodePastExpiration}, {Name: "findnode/PastExpiration", Fn: FindnodePastExpiration},
{Name: "Amplification/InvalidPongHash", Fn: FindnodeAmplificationInvalidPongHash}, {Name: "Amplification/InvalidPongHash", Fn: FindnodeAmplificationInvalidPongHash},
{Name: "Amplification/WrongIP", Fn: FindnodeAmplificationWrongIP}, {Name: "Amplification/WrongIP", Fn: FindnodeAmplificationWrongIP},
} }

View file

@ -369,12 +369,12 @@ func (t *UDPv5) newLookup(ctx context.Context, target enode.ID) *lookup {
// lookupWorker performs FINDNODE calls against a single node during lookup. // lookupWorker performs FINDNODE calls against a single node during lookup.
func (t *UDPv5) lookupWorker(destNode *enode.Node, target enode.ID) ([]*enode.Node, error) { func (t *UDPv5) lookupWorker(destNode *enode.Node, target enode.ID) ([]*enode.Node, error) {
var ( var (
dists = LookupDistances(target, destNode.ID()) dists = lookupDistances(target, destNode.ID())
nodes = nodesByDistance{target: target} nodes = nodesByDistance{target: target}
err error err error
) )
var r []*enode.Node var r []*enode.Node
r, err = t.Findnode(destNode, dists) r, err = t.findnode(destNode, dists)
if errors.Is(err, errClosed) { if errors.Is(err, errClosed) {
return nil, err return nil, err
} }
@ -386,10 +386,10 @@ func (t *UDPv5) lookupWorker(destNode *enode.Node, target enode.ID) ([]*enode.No
return nodes.entries, err return nodes.entries, err
} }
// LookupDistances computes the distance parameter for FINDNODE calls to dest. // lookupDistances computes the distance parameter for FINDNODE calls to dest.
// It chooses distances adjacent to logdist(target, dest), e.g. for a target // It chooses distances adjacent to logdist(target, dest), e.g. for a target
// with logdist(target, dest) = 255 the result is [255, 256, 254]. // with logdist(target, dest) = 255 the result is [255, 256, 254].
func LookupDistances(target, dest enode.ID) (dists []uint) { func lookupDistances(target, dest enode.ID) (dists []uint) {
td := enode.LogDist(target, dest) td := enode.LogDist(target, dest)
dists = append(dists, uint(td)) dists = append(dists, uint(td))
for i := 1; len(dists) < lookupRequestLimit; i++ { for i := 1; len(dists) < lookupRequestLimit; i++ {
@ -429,7 +429,7 @@ func (t *UDPv5) PingWithResp(n *enode.Node) (*v5wire.Pong, error) {
// RequestENR requests n's record. // RequestENR requests n's record.
func (t *UDPv5) RequestENR(n *enode.Node) (*enode.Node, error) { func (t *UDPv5) RequestENR(n *enode.Node) (*enode.Node, error) {
nodes, err := t.Findnode(n, []uint{0}) nodes, err := t.findnode(n, []uint{0})
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -439,8 +439,8 @@ func (t *UDPv5) RequestENR(n *enode.Node) (*enode.Node, error) {
return nodes[0], nil return nodes[0], nil
} }
// Findnode calls FINDNODE on a node and waits for responses. // findnode calls FINDNODE on a node and waits for responses.
func (t *UDPv5) Findnode(n *enode.Node, distances []uint) ([]*enode.Node, error) { func (t *UDPv5) findnode(n *enode.Node, distances []uint) ([]*enode.Node, error) {
resp := t.callToNode(n, v5wire.NodesMsg, &v5wire.Findnode{Distances: distances}) resp := t.callToNode(n, v5wire.NodesMsg, &v5wire.Findnode{Distances: distances})
return t.waitForNodes(resp, distances) return t.waitForNodes(resp, distances)
} }
@ -721,7 +721,6 @@ func (t *UDPv5) send(toID enode.ID, toAddr netip.AddrPort, packet v5wire.Packet,
t.logcontext = packet.AppendLogInfo(t.logcontext) t.logcontext = packet.AppendLogInfo(t.logcontext)
enc, nonce, err := t.codec.Encode(toID, addr, packet, c) enc, nonce, err := t.codec.Encode(toID, addr, packet, c)
t.logcontext = append(t.logcontext, "nonce", fmt.Sprintf("%x", nonce[:]))
if err != nil { if err != nil {
t.logcontext = append(t.logcontext, "err", err) t.logcontext = append(t.logcontext, "err", err)
t.log.Warn(">> "+packet.Name(), t.logcontext...) t.log.Warn(">> "+packet.Name(), t.logcontext...)
@ -879,7 +878,7 @@ var (
func (t *UDPv5) handleWhoareyou(p *v5wire.Whoareyou, fromID enode.ID, fromAddr netip.AddrPort) { func (t *UDPv5) handleWhoareyou(p *v5wire.Whoareyou, fromID enode.ID, fromAddr netip.AddrPort) {
c, err := t.matchWithCall(fromID, p.Nonce) c, err := t.matchWithCall(fromID, p.Nonce)
if err != nil { if err != nil {
t.log.Debug("Invalid "+p.Name(), "addr", fromAddr, "nonce", fmt.Sprintf("%x", p.Nonce[:]), "err", err) t.log.Debug("Invalid "+p.Name(), "addr", fromAddr, "err", err)
return return
} }
@ -890,7 +889,7 @@ func (t *UDPv5) handleWhoareyou(p *v5wire.Whoareyou, fromID enode.ID, fromAddr n
return return
} }
// Resend the call that was answered by WHOAREYOU. // Resend the call that was answered by WHOAREYOU.
t.log.Trace("<< "+p.Name(), "id", c.node.ID(), "addr", fromAddr, "nonce", fmt.Sprintf("%x", p.Nonce[:])) t.log.Trace("<< "+p.Name(), "id", c.node.ID(), "addr", fromAddr)
c.handshakeCount++ c.handshakeCount++
c.challenge = p c.challenge = p
p.Node = c.node p.Node = c.node
@ -1007,7 +1006,3 @@ func (t *UDPv5) putCache(addr string, node *enode.Node) {
func (t *UDPv5) GetCachedNode(addr string) (*enode.Node, bool) { func (t *UDPv5) GetCachedNode(addr string) (*enode.Node, bool) {
return t.cachedAddrNode.Get(addr) return t.cachedAddrNode.Get(addr)
} }
func (t *UDPv5) Table() *Table {
return t.tab
}

View file

@ -79,7 +79,7 @@ func startLocalhostV5(t *testing.T, cfg Config) *UDPv5 {
db, _ := enode.OpenDB("") db, _ := enode.OpenDB("")
ln := enode.NewLocalNode(db, cfg.PrivateKey) ln := enode.NewLocalNode(db, cfg.PrivateKey)
// Prefix logs with Node ID. // Prefix logs with node ID.
lprefix := fmt.Sprintf("(%s)", ln.ID().TerminalString()) lprefix := fmt.Sprintf("(%s)", ln.ID().TerminalString())
cfg.Log = testlog.Logger(t, log.LevelTrace).With("node-id", lprefix) cfg.Log = testlog.Logger(t, log.LevelTrace).With("node-id", lprefix)
@ -135,13 +135,13 @@ func TestUDPv5_unknownPacket(t *testing.T) {
} }
} }
// Unknown packet from unknown Node. // Unknown packet from unknown node.
test.packetIn(&v5wire.Unknown{Nonce: nonce}) test.packetIn(&v5wire.Unknown{Nonce: nonce})
test.waitPacketOut(func(p *v5wire.Whoareyou, addr netip.AddrPort, _ v5wire.Nonce) { test.waitPacketOut(func(p *v5wire.Whoareyou, addr netip.AddrPort, _ v5wire.Nonce) {
check(p, 0) check(p, 0)
}) })
// Make Node known. // Make node known.
n := test.getNode(test.remotekey, test.remoteaddr).Node() n := test.getNode(test.remotekey, test.remoteaddr).Node()
test.table.addFoundNode(n, false) test.table.addFoundNode(n, false)
@ -165,7 +165,7 @@ func TestUDPv5_findnodeHandling(t *testing.T) {
fillTable(test.table, nodes249, true) fillTable(test.table, nodes249, true)
fillTable(test.table, nodes248, true) fillTable(test.table, nodes248, true)
// Requesting with distance zero should return the Node's own record. // Requesting with distance zero should return the node's own record.
test.packetIn(&v5wire.Findnode{ReqID: []byte{0}, Distances: []uint{0}}) test.packetIn(&v5wire.Findnode{ReqID: []byte{0}, Distances: []uint{0}})
test.expectNodes([]byte{0}, 1, []*enode.Node{test.udp.Self()}) test.expectNodes([]byte{0}, 1, []*enode.Node{test.udp.Self()})
@ -212,7 +212,7 @@ func (test *udpV5Test) expectNodes(wantReqID []byte, wantTotal uint8, wantNodes
n, _ := enode.New(enode.ValidSchemesForTesting, record) n, _ := enode.New(enode.ValidSchemesForTesting, record)
want := nodeSet[n.ID()] want := nodeSet[n.ID()]
if want == nil { if want == nil {
test.t.Fatalf("unexpected Node in response: %v", n) test.t.Fatalf("unexpected node in response: %v", n)
} }
if !reflect.DeepEqual(record, want) { if !reflect.DeepEqual(record, want) {
test.t.Fatalf("wrong record in response: %v", n) test.t.Fatalf("wrong record in response: %v", n)
@ -288,7 +288,7 @@ func TestUDPv5_findnodeCall(t *testing.T) {
) )
go func() { go func() {
var err error var err error
response, err = test.udp.Findnode(remote, distances) response, err = test.udp.findnode(remote, distances)
done <- err done <- err
}() }()
@ -398,7 +398,7 @@ func TestUDPv5_callTimeoutReset(t *testing.T) {
done = make(chan error, 1) done = make(chan error, 1)
) )
go func() { go func() {
_, err := test.udp.Findnode(remote, []uint{distance}) _, err := test.udp.findnode(remote, []uint{distance})
done <- err done <- err
}() }()
@ -535,38 +535,38 @@ func TestUDPv5_talkRequest(t *testing.T) {
} }
} }
// This test checks that LookupDistances works. // This test checks that lookupDistances works.
func TestUDPv5_lookupDistances(t *testing.T) { func TestUDPv5_lookupDistances(t *testing.T) {
test := newUDPV5Test(t) test := newUDPV5Test(t)
lnID := test.table.self().ID() lnID := test.table.self().ID()
t.Run("target distance of 1", func(t *testing.T) { t.Run("target distance of 1", func(t *testing.T) {
node := nodeAtDistance(lnID, 1, intIP(0)) node := nodeAtDistance(lnID, 1, intIP(0))
dists := LookupDistances(lnID, node.ID()) dists := lookupDistances(lnID, node.ID())
require.Equal(t, []uint{1, 2, 3}, dists) require.Equal(t, []uint{1, 2, 3}, dists)
}) })
t.Run("target distance of 2", func(t *testing.T) { t.Run("target distance of 2", func(t *testing.T) {
node := nodeAtDistance(lnID, 2, intIP(0)) node := nodeAtDistance(lnID, 2, intIP(0))
dists := LookupDistances(lnID, node.ID()) dists := lookupDistances(lnID, node.ID())
require.Equal(t, []uint{2, 3, 1}, dists) require.Equal(t, []uint{2, 3, 1}, dists)
}) })
t.Run("target distance of 128", func(t *testing.T) { t.Run("target distance of 128", func(t *testing.T) {
node := nodeAtDistance(lnID, 128, intIP(0)) node := nodeAtDistance(lnID, 128, intIP(0))
dists := LookupDistances(lnID, node.ID()) dists := lookupDistances(lnID, node.ID())
require.Equal(t, []uint{128, 129, 127}, dists) require.Equal(t, []uint{128, 129, 127}, dists)
}) })
t.Run("target distance of 255", func(t *testing.T) { t.Run("target distance of 255", func(t *testing.T) {
node := nodeAtDistance(lnID, 255, intIP(0)) node := nodeAtDistance(lnID, 255, intIP(0))
dists := LookupDistances(lnID, node.ID()) dists := lookupDistances(lnID, node.ID())
require.Equal(t, []uint{255, 256, 254}, dists) require.Equal(t, []uint{255, 256, 254}, dists)
}) })
t.Run("target distance of 256", func(t *testing.T) { t.Run("target distance of 256", func(t *testing.T) {
node := nodeAtDistance(lnID, 256, intIP(0)) node := nodeAtDistance(lnID, 256, intIP(0))
dists := LookupDistances(lnID, node.ID()) dists := lookupDistances(lnID, node.ID())
require.Equal(t, []uint{256, 255, 254}, dists) require.Equal(t, []uint{256, 255, 254}, dists)
}) })
} }
@ -590,7 +590,7 @@ func TestUDPv5_lookup(t *testing.T) {
} }
} }
// Seed table with initial Node. // Seed table with initial node.
initialNode := lookupTestnet.node(256, 0) initialNode := lookupTestnet.node(256, 0)
fillTable(test.table, []*enode.Node{initialNode}, true) fillTable(test.table, []*enode.Node{initialNode}, true)
@ -611,7 +611,7 @@ func TestUDPv5_lookup(t *testing.T) {
test.packetInFrom(key, to, &v5wire.Pong{ReqID: p.ReqID}) test.packetInFrom(key, to, &v5wire.Pong{ReqID: p.ReqID})
case *v5wire.Findnode: case *v5wire.Findnode:
if asked[recipient.ID()] { if asked[recipient.ID()] {
t.Error("Asked Node", recipient.ID(), "twice") t.Error("Asked node", recipient.ID(), "twice")
} }
asked[recipient.ID()] = true asked[recipient.ID()] = true
nodes := lookupTestnet.neighborsAtDistances(recipient, p.Distances, 16) nodes := lookupTestnet.neighborsAtDistances(recipient, p.Distances, 16)
@ -628,7 +628,7 @@ func TestUDPv5_lookup(t *testing.T) {
checkLookupResults(t, lookupTestnet, results) checkLookupResults(t, lookupTestnet, results)
} }
// This test checks the local Node can be utilised to set key-values. // This test checks the local node can be utilised to set key-values.
func TestUDPv5_LocalNode(t *testing.T) { func TestUDPv5_LocalNode(t *testing.T) {
t.Parallel() t.Parallel()
var cfg Config var cfg Config
@ -636,7 +636,7 @@ func TestUDPv5_LocalNode(t *testing.T) {
defer node.Close() defer node.Close()
localNd := node.LocalNode() localNd := node.LocalNode()
// set value in Node's local record // set value in node's local record
testVal := [4]byte{'A', 'B', 'C', 'D'} testVal := [4]byte{'A', 'B', 'C', 'D'}
localNd.Set(enr.WithEntry("testing", &testVal)) localNd.Set(enr.WithEntry("testing", &testVal))
@ -826,7 +826,7 @@ func (test *udpV5Test) waitPacketOut(validate interface{}) (closed bool) {
} }
ln := test.nodesByIP[dgram.to.Addr()] ln := test.nodesByIP[dgram.to.Addr()]
if ln == nil { if ln == nil {
test.t.Fatalf("attempt to send to non-existing Node %v", &dgram.to) test.t.Fatalf("attempt to send to non-existing node %v", &dgram.to)
return false return false
} }
codec := &testCodec{test: test, id: ln.ID()} codec := &testCodec{test: test, id: ln.ID()}

View file

@ -94,7 +94,7 @@ const (
// Should reject packets smaller than minPacketSize. // Should reject packets smaller than minPacketSize.
minPacketSize = 63 minPacketSize = 63
MaxPacketSize = 1280 maxPacketSize = 1280
minMessageSize = 48 // this refers to data after static headers minMessageSize = 48 // this refers to data after static headers
randomPacketMsgSize = 20 randomPacketMsgSize = 20
@ -169,7 +169,7 @@ func NewCodec(ln *enode.LocalNode, key *ecdsa.PrivateKey, clock mclock.Clock, pr
privkey: key, privkey: key,
sc: NewSessionCache(1024, clock), sc: NewSessionCache(1024, clock),
protocolID: DefaultProtocolID, protocolID: DefaultProtocolID,
decbuf: make([]byte, MaxPacketSize), decbuf: make([]byte, maxPacketSize),
} }
if protocolID != nil { if protocolID != nil {
c.protocolID = *protocolID c.protocolID = *protocolID