Skip to content

Commit 9b2e8e7

Browse files
authored
p2p: use slices.Clone (#32428)
Replaces a helper method with slices.Clone
1 parent a9ac275 commit 9b2e8e7

File tree

1 file changed

+4
-9
lines changed

1 file changed

+4
-9
lines changed

p2p/discover/v5wire/encoding.go

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"errors"
2828
"fmt"
2929
"hash"
30+
"slices"
3031

3132
"github.com/ethereum/go-ethereum/common/mclock"
3233
"github.com/ethereum/go-ethereum/p2p/enode"
@@ -222,7 +223,7 @@ func (c *Codec) Encode(id enode.ID, addr string, packet Packet, challenge *Whoar
222223

223224
// Store sent WHOAREYOU challenges.
224225
if challenge, ok := packet.(*Whoareyou); ok {
225-
challenge.ChallengeData = bytesCopy(&c.buf)
226+
challenge.ChallengeData = slices.Clone(c.buf.Bytes())
226227
enc, err := c.EncodeRaw(id, head, msgData)
227228
if err != nil {
228229
return nil, Nonce{}, err
@@ -325,7 +326,7 @@ func (c *Codec) encodeWhoareyou(toID enode.ID, packet *Whoareyou) (Header, error
325326

326327
// Create header.
327328
head := c.makeHeader(toID, flagWhoareyou, 0)
328-
head.AuthData = bytesCopy(&c.buf)
329+
head.AuthData = slices.Clone(c.buf.Bytes())
329330
head.Nonce = packet.Nonce
330331

331332
// Encode auth data.
@@ -430,7 +431,7 @@ func (c *Codec) encodeMessageHeader(toID enode.ID, s *session) (Header, error) {
430431
auth := messageAuthData{SrcID: c.localnode.ID()}
431432
c.buf.Reset()
432433
binary.Write(&c.buf, binary.BigEndian, &auth)
433-
head.AuthData = bytesCopy(&c.buf)
434+
head.AuthData = slices.Clone(c.buf.Bytes())
434435
head.Nonce = nonce
435436
return head, err
436437
}
@@ -686,9 +687,3 @@ func (h *Header) mask(destID enode.ID) cipher.Stream {
686687
}
687688
return cipher.NewCTR(block, h.IV[:])
688689
}
689-
690-
func bytesCopy(r *bytes.Buffer) []byte {
691-
b := make([]byte, r.Len())
692-
copy(b, r.Bytes())
693-
return b
694-
}

0 commit comments

Comments
 (0)