p2p: use slices.Clone (#32428)
Some checks are pending
/ Linux Build (push) Waiting to run
/ Linux Build (arm) (push) Waiting to run
/ Windows Build (push) Waiting to run
/ Docker Image (push) Waiting to run

Replaces a helper method with slices.Clone
This commit is contained in:
cui 2025-08-25 17:30:51 +08:00 committed by GitHub
parent a9ac275588
commit 9b2e8e7ce3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -27,6 +27,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"hash" "hash"
"slices"
"github.com/ethereum/go-ethereum/common/mclock" "github.com/ethereum/go-ethereum/common/mclock"
"github.com/ethereum/go-ethereum/p2p/enode" "github.com/ethereum/go-ethereum/p2p/enode"
@ -222,7 +223,7 @@ func (c *Codec) Encode(id enode.ID, addr string, packet Packet, challenge *Whoar
// Store sent WHOAREYOU challenges. // Store sent WHOAREYOU challenges.
if challenge, ok := packet.(*Whoareyou); ok { if challenge, ok := packet.(*Whoareyou); ok {
challenge.ChallengeData = bytesCopy(&c.buf) challenge.ChallengeData = slices.Clone(c.buf.Bytes())
enc, err := c.EncodeRaw(id, head, msgData) enc, err := c.EncodeRaw(id, head, msgData)
if err != nil { if err != nil {
return nil, Nonce{}, err return nil, Nonce{}, err
@ -325,7 +326,7 @@ func (c *Codec) encodeWhoareyou(toID enode.ID, packet *Whoareyou) (Header, error
// Create header. // Create header.
head := c.makeHeader(toID, flagWhoareyou, 0) head := c.makeHeader(toID, flagWhoareyou, 0)
head.AuthData = bytesCopy(&c.buf) head.AuthData = slices.Clone(c.buf.Bytes())
head.Nonce = packet.Nonce head.Nonce = packet.Nonce
// Encode auth data. // Encode auth data.
@ -430,7 +431,7 @@ func (c *Codec) encodeMessageHeader(toID enode.ID, s *session) (Header, error) {
auth := messageAuthData{SrcID: c.localnode.ID()} auth := messageAuthData{SrcID: c.localnode.ID()}
c.buf.Reset() c.buf.Reset()
binary.Write(&c.buf, binary.BigEndian, &auth) binary.Write(&c.buf, binary.BigEndian, &auth)
head.AuthData = bytesCopy(&c.buf) head.AuthData = slices.Clone(c.buf.Bytes())
head.Nonce = nonce head.Nonce = nonce
return head, err return head, err
} }
@ -686,9 +687,3 @@ func (h *Header) mask(destID enode.ID) cipher.Stream {
} }
return cipher.NewCTR(block, h.IV[:]) return cipher.NewCTR(block, h.IV[:])
} }
func bytesCopy(r *bytes.Buffer) []byte {
b := make([]byte, r.Len())
copy(b, r.Bytes())
return b
}