From be1c034dddc68700f840da63178c7b1ff27afd25 Mon Sep 17 00:00:00 2001 From: Andrew Ashikhmin <34320705+yperbasis@users.noreply.github.com> Date: Mon, 13 Jan 2025 13:58:48 +0100 Subject: [PATCH] Use libcommon.CopyBytes instead of bytesCopy (#13406) --- .../kv/remotedbserver/remotedbserver.go | 21 ++++++------------- p2p/discover/v5wire/encoding.go | 13 ++++-------- turbo/jsonrpc/eth_call.go | 15 ++++++------- 3 files changed, 16 insertions(+), 33 deletions(-) diff --git a/erigon-lib/kv/remotedbserver/remotedbserver.go b/erigon-lib/kv/remotedbserver/remotedbserver.go index b7e46c598f0..7316e92fbf4 100644 --- a/erigon-lib/kv/remotedbserver/remotedbserver.go +++ b/erigon-lib/kv/remotedbserver/remotedbserver.go @@ -260,8 +260,8 @@ func (s *KvServer) Tx(stream remote.KV_TxServer) error { if err != nil { return fmt.Errorf("kvserver: %w", err) } - c.k = bytesCopy(k) - c.v = bytesCopy(v) + c.k = common.CopyBytes(k) + c.v = common.CopyBytes(v) } if err := s.renew(stream.Context(), id); err != nil { @@ -421,15 +421,6 @@ func handleOp(c kv.Cursor, stream remote.KV_TxServer, in *remote.Cursor) error { return nil } -func bytesCopy(b []byte) []byte { - if b == nil { - return nil - } - copiedBytes := make([]byte, len(b)) - copy(copiedBytes, b) - return copiedBytes -} - func (s *KvServer) StateChanges(_ *remote.StateChangeRequest, server remote.KV_StateChangesServer) error { ch, remove := s.stateChangeStreams.Sub() defer remove() @@ -647,8 +638,8 @@ func (s *KvServer) HistoryRange(_ context.Context, req *remote.HistoryRangeReq) if err != nil { return err } - key := bytesCopy(k) - value := bytesCopy(v) + key := common.CopyBytes(k) + value := common.CopyBytes(v) reply.Keys = append(reply.Keys, key) reply.Values = append(reply.Values, value) } @@ -692,8 +683,8 @@ func (s *KvServer) RangeAsOf(_ context.Context, req *remote.RangeAsOfReq) (*remo if err != nil { return err } - key := bytesCopy(k) - value := bytesCopy(v) + key := common.CopyBytes(k) + value := common.CopyBytes(v) reply.Keys = append(reply.Keys, key) reply.Values = append(reply.Values, value) limit-- diff --git a/p2p/discover/v5wire/encoding.go b/p2p/discover/v5wire/encoding.go index 5db2fa4d5d3..af5170729e4 100644 --- a/p2p/discover/v5wire/encoding.go +++ b/p2p/discover/v5wire/encoding.go @@ -31,6 +31,7 @@ import ( "fmt" "hash" + libcommon "github.com/erigontech/erigon-lib/common" "github.com/erigontech/erigon-lib/common/mclock" "github.com/erigontech/erigon-lib/rlp" "github.com/erigontech/erigon/p2p/enode" @@ -199,7 +200,7 @@ func (c *Codec) Encode(id enode.ID, addr string, packet Packet, challenge *Whoar // Store sent WHOAREYOU challenges. if challenge, ok := packet.(*Whoareyou); ok { - challenge.ChallengeData = bytesCopy(&c.buf) + challenge.ChallengeData = libcommon.CopyBytes(c.buf.Bytes()) c.sc.storeSentHandshake(id, addr, challenge) } else if msgData == nil { headerData := c.buf.Bytes() @@ -293,7 +294,7 @@ func (c *Codec) encodeWhoareyou(toID enode.ID, packet *Whoareyou) (Header, error // Create header. head := c.makeHeader(toID, flagWhoareyou, 0) - head.AuthData = bytesCopy(&c.buf) + head.AuthData = libcommon.CopyBytes(c.buf.Bytes()) head.Nonce = packet.Nonce // Encode auth data. @@ -400,7 +401,7 @@ func (c *Codec) encodeMessageHeader(toID enode.ID, s *session) (Header, error) { auth := messageAuthData{SrcID: c.localnode.ID()} c.buf.Reset() binary.Write(&c.buf, binary.BigEndian, &auth) //nolint:errcheck - head.AuthData = bytesCopy(&c.buf) + head.AuthData = libcommon.CopyBytes(c.buf.Bytes()) head.Nonce = nonce return head, err } @@ -649,9 +650,3 @@ func (h *Header) mask(destID enode.ID) cipher.Stream { } return cipher.NewCTR(block, h.IV[:]) } - -func bytesCopy(r *bytes.Buffer) []byte { - b := make([]byte, r.Len()) - copy(b, r.Bytes()) - return b -} diff --git a/turbo/jsonrpc/eth_call.go b/turbo/jsonrpc/eth_call.go index eb4c18780be..1b8f9a7f46d 100644 --- a/turbo/jsonrpc/eth_call.go +++ b/turbo/jsonrpc/eth_call.go @@ -23,16 +23,10 @@ import ( "fmt" "math/big" - "github.com/erigontech/erigon-lib/kv/dbutils" - "github.com/erigontech/erigon-lib/trie" - - "github.com/erigontech/erigon-lib/commitment" - libstate "github.com/erigontech/erigon-lib/state" "github.com/holiman/uint256" "google.golang.org/grpc" - "github.com/erigontech/erigon-lib/kv/membatchwithdb" - + "github.com/erigontech/erigon-lib/commitment" libcommon "github.com/erigontech/erigon-lib/common" "github.com/erigontech/erigon-lib/common/hexutil" "github.com/erigontech/erigon-lib/common/hexutility" @@ -40,8 +34,12 @@ import ( "github.com/erigontech/erigon-lib/gointerfaces" txpool_proto "github.com/erigontech/erigon-lib/gointerfaces/txpoolproto" "github.com/erigontech/erigon-lib/kv" + "github.com/erigontech/erigon-lib/kv/dbutils" + "github.com/erigontech/erigon-lib/kv/membatchwithdb" "github.com/erigontech/erigon-lib/kv/rawdbv3" "github.com/erigontech/erigon-lib/log/v3" + libstate "github.com/erigontech/erigon-lib/state" + "github.com/erigontech/erigon-lib/trie" "github.com/erigontech/erigon-lib/types/accounts" "github.com/erigontech/erigon/consensus" "github.com/erigontech/erigon/core" @@ -636,8 +634,7 @@ func (api *BaseAPI) getWitness(ctx context.Context, db kv.RoDB, blockNrOrHash rp fmt.Printf("state root mismatch after stateless execution actual(%x) != expected(%x)\n", newStateRoot.Bytes(), block.Root().Bytes()) } witnessBufBytes := witnessBuffer.Bytes() - witnessBufBytesCopy := make([]byte, len(witnessBufBytes)) - copy(witnessBufBytesCopy, witnessBufBytes) + witnessBufBytesCopy := libcommon.CopyBytes(witnessBufBytes) return witnessBufBytesCopy, nil }