Skip to content

Commit

Permalink
Use libcommon.CopyBytes instead of bytesCopy (#13406)
Browse files Browse the repository at this point in the history
  • Loading branch information
yperbasis authored Jan 13, 2025
1 parent 0af73b7 commit be1c034
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 33 deletions.
21 changes: 6 additions & 15 deletions erigon-lib/kv/remotedbserver/remotedbserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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--
Expand Down
13 changes: 4 additions & 9 deletions p2p/discover/v5wire/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
15 changes: 6 additions & 9 deletions turbo/jsonrpc/eth_call.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,23 @@ 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"
"github.com/erigontech/erigon-lib/crypto"
"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"
Expand Down Expand Up @@ -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
}

Expand Down

0 comments on commit be1c034

Please sign in to comment.