From fbfb4083e108085f3921ba23567ade9adaee7a17 Mon Sep 17 00:00:00 2001 From: Chen Kai <281165273grape@gmail.com> Date: Tue, 10 Dec 2024 16:16:40 +0800 Subject: [PATCH] revert some change Signed-off-by: Chen Kai <281165273grape@gmail.com> --- cmd/devp2p/internal/v4test/discv4tests.go | 8 ++--- p2p/discover/v5_udp.go | 23 ++++++--------- p2p/discover/v5_udp_test.go | 36 +++++++++++------------ p2p/discover/v5wire/encoding.go | 4 +-- 4 files changed, 33 insertions(+), 38 deletions(-) diff --git a/cmd/devp2p/internal/v4test/discv4tests.go b/cmd/devp2p/internal/v4test/discv4tests.go index ca556851b4..164f510187 100644 --- a/cmd/devp2p/internal/v4test/discv4tests.go +++ b/cmd/devp2p/internal/v4test/discv4tests.go @@ -510,10 +510,10 @@ var AllTests = []utesting.Test{ {Name: "Ping/PastExpiration", Fn: PingPastExpiration}, {Name: "Ping/WrongPacketType", Fn: WrongPacketType}, {Name: "Ping/BondThenPingWithWrongFrom", Fn: BondThenPingWithWrongFrom}, - {Name: "Findnode/WithoutEndpointProof", Fn: FindnodeWithoutEndpointProof}, - {Name: "Findnode/BasicFindnode", Fn: BasicFindnode}, - {Name: "Findnode/UnsolicitedNeighbors", Fn: UnsolicitedNeighbors}, - {Name: "Findnode/PastExpiration", Fn: FindnodePastExpiration}, + {Name: "findnode/WithoutEndpointProof", Fn: FindnodeWithoutEndpointProof}, + {Name: "findnode/BasicFindnode", Fn: BasicFindnode}, + {Name: "findnode/UnsolicitedNeighbors", Fn: UnsolicitedNeighbors}, + {Name: "findnode/PastExpiration", Fn: FindnodePastExpiration}, {Name: "Amplification/InvalidPongHash", Fn: FindnodeAmplificationInvalidPongHash}, {Name: "Amplification/WrongIP", Fn: FindnodeAmplificationWrongIP}, } diff --git a/p2p/discover/v5_udp.go b/p2p/discover/v5_udp.go index 30c7adbdb9..c08e036445 100644 --- a/p2p/discover/v5_udp.go +++ b/p2p/discover/v5_udp.go @@ -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. func (t *UDPv5) lookupWorker(destNode *enode.Node, target enode.ID) ([]*enode.Node, error) { var ( - dists = LookupDistances(target, destNode.ID()) + dists = lookupDistances(target, destNode.ID()) nodes = nodesByDistance{target: target} err error ) var r []*enode.Node - r, err = t.Findnode(destNode, dists) + r, err = t.findnode(destNode, dists) if errors.Is(err, errClosed) { return nil, err } @@ -386,10 +386,10 @@ func (t *UDPv5) lookupWorker(destNode *enode.Node, target enode.ID) ([]*enode.No 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 // 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) dists = append(dists, uint(td)) 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. 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 { return nil, err } @@ -439,8 +439,8 @@ func (t *UDPv5) RequestENR(n *enode.Node) (*enode.Node, error) { return nodes[0], nil } -// Findnode calls FINDNODE on a node and waits for responses. -func (t *UDPv5) Findnode(n *enode.Node, distances []uint) ([]*enode.Node, error) { +// findnode calls FINDNODE on a node and waits for responses. +func (t *UDPv5) findnode(n *enode.Node, distances []uint) ([]*enode.Node, error) { resp := t.callToNode(n, v5wire.NodesMsg, &v5wire.Findnode{Distances: 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) enc, nonce, err := t.codec.Encode(toID, addr, packet, c) - t.logcontext = append(t.logcontext, "nonce", fmt.Sprintf("%x", nonce[:])) if err != nil { t.logcontext = append(t.logcontext, "err", err) 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) { c, err := t.matchWithCall(fromID, p.Nonce) 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 } @@ -890,7 +889,7 @@ func (t *UDPv5) handleWhoareyou(p *v5wire.Whoareyou, fromID enode.ID, fromAddr n return } // 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.challenge = p 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) { return t.cachedAddrNode.Get(addr) } - -func (t *UDPv5) Table() *Table { - return t.tab -} diff --git a/p2p/discover/v5_udp_test.go b/p2p/discover/v5_udp_test.go index ac00eef72d..8631b918ff 100644 --- a/p2p/discover/v5_udp_test.go +++ b/p2p/discover/v5_udp_test.go @@ -79,7 +79,7 @@ func startLocalhostV5(t *testing.T, cfg Config) *UDPv5 { db, _ := enode.OpenDB("") ln := enode.NewLocalNode(db, cfg.PrivateKey) - // Prefix logs with Node ID. + // Prefix logs with node ID. lprefix := fmt.Sprintf("(%s)", ln.ID().TerminalString()) 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.waitPacketOut(func(p *v5wire.Whoareyou, addr netip.AddrPort, _ v5wire.Nonce) { check(p, 0) }) - // Make Node known. + // Make node known. n := test.getNode(test.remotekey, test.remoteaddr).Node() test.table.addFoundNode(n, false) @@ -165,7 +165,7 @@ func TestUDPv5_findnodeHandling(t *testing.T) { fillTable(test.table, nodes249, 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.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) want := nodeSet[n.ID()] 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) { test.t.Fatalf("wrong record in response: %v", n) @@ -288,7 +288,7 @@ func TestUDPv5_findnodeCall(t *testing.T) { ) go func() { var err error - response, err = test.udp.Findnode(remote, distances) + response, err = test.udp.findnode(remote, distances) done <- err }() @@ -398,7 +398,7 @@ func TestUDPv5_callTimeoutReset(t *testing.T) { done = make(chan error, 1) ) go func() { - _, err := test.udp.Findnode(remote, []uint{distance}) + _, err := test.udp.findnode(remote, []uint{distance}) 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) { test := newUDPV5Test(t) lnID := test.table.self().ID() t.Run("target distance of 1", func(t *testing.T) { node := nodeAtDistance(lnID, 1, intIP(0)) - dists := LookupDistances(lnID, node.ID()) + dists := lookupDistances(lnID, node.ID()) require.Equal(t, []uint{1, 2, 3}, dists) }) t.Run("target distance of 2", func(t *testing.T) { node := nodeAtDistance(lnID, 2, intIP(0)) - dists := LookupDistances(lnID, node.ID()) + dists := lookupDistances(lnID, node.ID()) require.Equal(t, []uint{2, 3, 1}, dists) }) t.Run("target distance of 128", func(t *testing.T) { node := nodeAtDistance(lnID, 128, intIP(0)) - dists := LookupDistances(lnID, node.ID()) + dists := lookupDistances(lnID, node.ID()) require.Equal(t, []uint{128, 129, 127}, dists) }) t.Run("target distance of 255", func(t *testing.T) { node := nodeAtDistance(lnID, 255, intIP(0)) - dists := LookupDistances(lnID, node.ID()) + dists := lookupDistances(lnID, node.ID()) require.Equal(t, []uint{255, 256, 254}, dists) }) t.Run("target distance of 256", func(t *testing.T) { node := nodeAtDistance(lnID, 256, intIP(0)) - dists := LookupDistances(lnID, node.ID()) + dists := lookupDistances(lnID, node.ID()) 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) 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}) case *v5wire.Findnode: if asked[recipient.ID()] { - t.Error("Asked Node", recipient.ID(), "twice") + t.Error("Asked node", recipient.ID(), "twice") } asked[recipient.ID()] = true nodes := lookupTestnet.neighborsAtDistances(recipient, p.Distances, 16) @@ -628,7 +628,7 @@ func TestUDPv5_lookup(t *testing.T) { 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) { t.Parallel() var cfg Config @@ -636,7 +636,7 @@ func TestUDPv5_LocalNode(t *testing.T) { defer node.Close() localNd := node.LocalNode() - // set value in Node's local record + // set value in node's local record testVal := [4]byte{'A', 'B', 'C', 'D'} localNd.Set(enr.WithEntry("testing", &testVal)) @@ -826,7 +826,7 @@ func (test *udpV5Test) waitPacketOut(validate interface{}) (closed bool) { } ln := test.nodesByIP[dgram.to.Addr()] 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 } codec := &testCodec{test: test, id: ln.ID()} diff --git a/p2p/discover/v5wire/encoding.go b/p2p/discover/v5wire/encoding.go index 6f33aa831c..a6cc278bba 100644 --- a/p2p/discover/v5wire/encoding.go +++ b/p2p/discover/v5wire/encoding.go @@ -94,7 +94,7 @@ const ( // Should reject packets smaller than minPacketSize. minPacketSize = 63 - MaxPacketSize = 1280 + maxPacketSize = 1280 minMessageSize = 48 // this refers to data after static headers randomPacketMsgSize = 20 @@ -169,7 +169,7 @@ func NewCodec(ln *enode.LocalNode, key *ecdsa.PrivateKey, clock mclock.Clock, pr privkey: key, sc: NewSessionCache(1024, clock), protocolID: DefaultProtocolID, - decbuf: make([]byte, MaxPacketSize), + decbuf: make([]byte, maxPacketSize), } if protocolID != nil { c.protocolID = *protocolID