Skip to content

Commit

Permalink
revert some change
Browse files Browse the repository at this point in the history
Signed-off-by: Chen Kai <[email protected]>
  • Loading branch information
GrapeBaBa committed Dec 10, 2024
1 parent dc433b3 commit fbfb408
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 38 deletions.
8 changes: 4 additions & 4 deletions cmd/devp2p/internal/v4test/discv4tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},
}
23 changes: 9 additions & 14 deletions p2p/discover/v5_udp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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++ {
Expand Down Expand Up @@ -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
}
Expand All @@ -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)
}
Expand Down Expand Up @@ -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...)
Expand Down Expand Up @@ -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
}

Expand All @@ -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
Expand Down Expand Up @@ -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
}
36 changes: 18 additions & 18 deletions p2p/discover/v5_udp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand All @@ -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()})

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
}()

Expand Down Expand Up @@ -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
}()

Expand Down Expand Up @@ -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)
})
}
Expand All @@ -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)

Expand All @@ -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)
Expand All @@ -628,15 +628,15 @@ 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
node := startLocalhostV5(t, cfg)
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))

Expand Down Expand Up @@ -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()}
Expand Down
4 changes: 2 additions & 2 deletions p2p/discover/v5wire/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit fbfb408

Please sign in to comment.