mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-05-18 22:09:26 +00:00
p2p: use slices.Clone (#32428)
Replaces a helper method with slices.Clone
This commit is contained in:
parent
a9ac275588
commit
9b2e8e7ce3
1 changed files with 4 additions and 9 deletions
|
|
@ -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
|
|
||||||
}
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue