Skip to content

Commit

Permalink
Merge rlp and rlp2 (#13095)
Browse files Browse the repository at this point in the history
Since `rlp` and `rlp2` both had `EncodeString` function, I decided to
change function name in `rlp` package to `EncodeBytes` to avoid name
collision.
Functions in `parse.go` file `List`, `String`, `U256`, `U64`, `U32` were
changed to `ParseList`, `ParseString`, `ParseU256` etc.
Function `EncodeStructSizePrefix` was moved from types package to `rlp`
package which is more semantically suitable
  • Loading branch information
racytech authored Dec 13, 2024
1 parent 69e00d6 commit 89a4a1e
Show file tree
Hide file tree
Showing 27 changed files with 194 additions and 232 deletions.
42 changes: 11 additions & 31 deletions core/types/access_list_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,16 @@
package types

import (
"encoding/binary"
"errors"
"fmt"
"io"
"math/big"
"math/bits"

"github.com/holiman/uint256"

"github.com/erigontech/erigon-lib/chain"
libcommon "github.com/erigontech/erigon-lib/common"
"github.com/erigontech/erigon-lib/rlp"
rlp2 "github.com/erigontech/erigon-lib/rlp2"
)

// AccessTuple is the element type of an access list.
Expand Down Expand Up @@ -110,7 +107,7 @@ func (tx *AccessListTx) Unwrap() Transaction {
func (tx *AccessListTx) EncodingSize() int {
payloadSize, _, _, _ := tx.payloadSize()
// Add envelope size and type size
return 1 + rlp2.ListPrefixLen(payloadSize) + payloadSize
return 1 + rlp.ListPrefixLen(payloadSize) + payloadSize
}

// payloadSize calculates the RLP encoding size of transaction, without TxType and envelope
Expand Down Expand Up @@ -138,10 +135,10 @@ func (tx *AccessListTx) payloadSize() (payloadSize int, nonceLen, gasLen, access
payloadSize++
payloadSize += rlp.Uint256LenExcludingHead(tx.Value)
// size of Data
payloadSize += rlp2.StringLen(tx.Data)
payloadSize += rlp.StringLen(tx.Data)
// size of AccessList
accessListLen = accessListSize(tx.AccessList)
payloadSize += rlp2.ListPrefixLen(accessListLen) + accessListLen
payloadSize += rlp.ListPrefixLen(accessListLen) + accessListLen
// size of V
payloadSize++
payloadSize += rlp.Uint256LenExcludingHead(&tx.V)
Expand All @@ -161,8 +158,8 @@ func accessListSize(al AccessList) int {
// size of StorageKeys
// Each storage key takes 33 bytes
storageLen := 33 * len(tuple.StorageKeys)
tupleLen += rlp2.ListPrefixLen(storageLen) + storageLen
accessListLen += rlp2.ListPrefixLen(tupleLen) + tupleLen
tupleLen += rlp.ListPrefixLen(storageLen) + storageLen
accessListLen += rlp.ListPrefixLen(tupleLen) + tupleLen
}
return accessListLen
}
Expand All @@ -172,14 +169,14 @@ func encodeAccessList(al AccessList, w io.Writer, b []byte) error {
tupleLen := 21
// Each storage key takes 33 bytes
storageLen := 33 * len(al[i].StorageKeys)
tupleLen += rlp2.ListPrefixLen(storageLen) + storageLen
if err := EncodeStructSizePrefix(tupleLen, w, b); err != nil {
tupleLen += rlp.ListPrefixLen(storageLen) + storageLen
if err := rlp.EncodeStructSizePrefix(tupleLen, w, b); err != nil {
return err
}
if err := rlp.EncodeOptionalAddress(&al[i].Address, w, b); err != nil { // TODO(racytech): change addr to []byte?
return err
}
if err := EncodeStructSizePrefix(storageLen, w, b); err != nil {
if err := rlp.EncodeStructSizePrefix(storageLen, w, b); err != nil {
return err
}
b[0] = 128 + 32
Expand All @@ -195,23 +192,6 @@ func encodeAccessList(al AccessList, w io.Writer, b []byte) error {
return nil
}

func EncodeStructSizePrefix(size int, w io.Writer, b []byte) error { // TODO(racytech): move it to rlp package?
if size >= 56 {
beSize := libcommon.BitLenToByteLen(bits.Len(uint(size)))
binary.BigEndian.PutUint64(b[1:], uint64(size))
b[8-beSize] = byte(beSize) + 247
if _, err := w.Write(b[8-beSize : 9]); err != nil {
return err
}
} else {
b[0] = byte(size) + 192
if _, err := w.Write(b[:1]); err != nil {
return err
}
}
return nil
}

// MarshalBinary returns the canonical encoding of the transaction.
// For legacy transactions, it returns the RLP encoding. For EIP-2718 typed
// transactions, it returns the type and payload.
Expand All @@ -232,7 +212,7 @@ func (tx *AccessListTx) MarshalBinary(w io.Writer) error {

func (tx *AccessListTx) encodePayload(w io.Writer, b []byte, payloadSize, nonceLen, gasLen, accessListLen int) error {
// prefix
if err := EncodeStructSizePrefix(payloadSize, w, b); err != nil {
if err := rlp.EncodeStructSizePrefix(payloadSize, w, b); err != nil {
return err
}
// encode ChainID
Expand Down Expand Up @@ -274,7 +254,7 @@ func (tx *AccessListTx) encodePayload(w io.Writer, b []byte, payloadSize, nonceL
return err
}
// prefix
if err := EncodeStructSizePrefix(accessListLen, w, b); err != nil {
if err := rlp.EncodeStructSizePrefix(accessListLen, w, b); err != nil {
return err
}
// encode AccessList
Expand All @@ -301,7 +281,7 @@ func (tx *AccessListTx) encodePayload(w io.Writer, b []byte, payloadSize, nonceL
func (tx *AccessListTx) EncodeRLP(w io.Writer) error {
payloadSize, nonceLen, gasLen, accessListLen := tx.payloadSize()
// size of struct prefix and TxType
envelopeSize := 1 + rlp2.ListPrefixLen(payloadSize) + payloadSize
envelopeSize := 1 + rlp.ListPrefixLen(payloadSize) + payloadSize
b := newEncodingBuf()
defer pooledBuf.Put(b)
// envelope
Expand Down
17 changes: 8 additions & 9 deletions core/types/authorization.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"github.com/erigontech/erigon-lib/common/length"
"github.com/erigontech/erigon-lib/crypto"
"github.com/erigontech/erigon-lib/rlp"
rlp2 "github.com/erigontech/erigon-lib/rlp2"
"github.com/erigontech/erigon/params"
)

Expand All @@ -37,11 +36,11 @@ func (ath *Authorization) copy() *Authorization {
}

func (ath *Authorization) RecoverSigner(data *bytes.Buffer, b []byte) (*libcommon.Address, error) {
authLen := rlp2.U64Len(ath.ChainID)
authLen := rlp.U64Len(ath.ChainID)
authLen += (1 + length.Addr)
authLen += rlp2.U64Len(ath.Nonce)
authLen += rlp.U64Len(ath.Nonce)

if err := EncodeStructSizePrefix(authLen, data, b); err != nil {
if err := rlp.EncodeStructSizePrefix(authLen, data, b); err != nil {
return nil, err
}

Expand Down Expand Up @@ -95,19 +94,19 @@ func (ath *Authorization) RecoverSigner(data *bytes.Buffer, b []byte) (*libcommo
}

func authorizationSize(auth Authorization) (authLen int) {
authLen = rlp2.U64Len(auth.ChainID)
authLen += rlp2.U64Len(auth.Nonce)
authLen = rlp.U64Len(auth.ChainID)
authLen += rlp.U64Len(auth.Nonce)
authLen += (1 + length.Addr)

authLen += rlp2.U64Len(uint64(auth.YParity)) + (1 + rlp.Uint256LenExcludingHead(&auth.R)) + (1 + rlp.Uint256LenExcludingHead(&auth.S))
authLen += rlp.U64Len(uint64(auth.YParity)) + (1 + rlp.Uint256LenExcludingHead(&auth.R)) + (1 + rlp.Uint256LenExcludingHead(&auth.S))

return
}

func authorizationsSize(authorizations []Authorization) (totalSize int) {
for _, auth := range authorizations {
authLen := authorizationSize(auth)
totalSize += rlp2.ListPrefixLen(authLen) + authLen
totalSize += rlp.ListPrefixLen(authLen) + authLen
}

return
Expand Down Expand Up @@ -184,7 +183,7 @@ func decodeAuthorizations(auths *[]Authorization, s *rlp.Stream) error {
func encodeAuthorizations(authorizations []Authorization, w io.Writer, b []byte) error {
for i := 0; i < len(authorizations); i++ {
authLen := authorizationSize(authorizations[i])
if err := EncodeStructSizePrefix(authLen, w, b); err != nil {
if err := rlp.EncodeStructSizePrefix(authLen, w, b); err != nil {
return err
}

Expand Down
13 changes: 6 additions & 7 deletions core/types/blob_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import (
libcommon "github.com/erigontech/erigon-lib/common"
"github.com/erigontech/erigon-lib/common/fixedgas"
"github.com/erigontech/erigon-lib/rlp"
rlp2 "github.com/erigontech/erigon-lib/rlp2"
)

type BlobTx struct {
Expand Down Expand Up @@ -159,7 +158,7 @@ func (stx *BlobTx) SigningHash(chainID *big.Int) libcommon.Hash {
func (stx *BlobTx) EncodingSize() int {
payloadSize, _, _, _, _ := stx.payloadSize()
// Add envelope size and type size
return 1 + rlp2.ListPrefixLen(payloadSize) + payloadSize
return 1 + rlp.ListPrefixLen(payloadSize) + payloadSize
}

func (stx *BlobTx) payloadSize() (payloadSize, nonceLen, gasLen, accessListLen, blobHashesLen int) {
Expand All @@ -169,7 +168,7 @@ func (stx *BlobTx) payloadSize() (payloadSize, nonceLen, gasLen, accessListLen,
payloadSize += rlp.Uint256LenExcludingHead(stx.MaxFeePerBlobGas)
// size of BlobVersionedHashes
blobHashesLen = blobVersionedHashesSize(stx.BlobVersionedHashes)
payloadSize += rlp2.ListPrefixLen(blobHashesLen) + blobHashesLen
payloadSize += rlp.ListPrefixLen(blobHashesLen) + blobHashesLen
return
}

Expand All @@ -188,7 +187,7 @@ func encodeBlobVersionedHashes(hashes []libcommon.Hash, w io.Writer, b []byte) e

func (stx *BlobTx) encodePayload(w io.Writer, b []byte, payloadSize, nonceLen, gasLen, accessListLen, blobHashesLen int) error {
// prefix
if err := EncodeStructSizePrefix(payloadSize, w, b); err != nil {
if err := rlp.EncodeStructSizePrefix(payloadSize, w, b); err != nil {
return err
}
// encode ChainID
Expand Down Expand Up @@ -228,7 +227,7 @@ func (stx *BlobTx) encodePayload(w io.Writer, b []byte, payloadSize, nonceLen, g
return err
}
// prefix
if err := EncodeStructSizePrefix(accessListLen, w, b); err != nil {
if err := rlp.EncodeStructSizePrefix(accessListLen, w, b); err != nil {
return err
}
// encode AccessList
Expand All @@ -240,7 +239,7 @@ func (stx *BlobTx) encodePayload(w io.Writer, b []byte, payloadSize, nonceLen, g
return err
}
// prefix
if err := EncodeStructSizePrefix(blobHashesLen, w, b); err != nil {
if err := rlp.EncodeStructSizePrefix(blobHashesLen, w, b); err != nil {
return err
}
// encode BlobVersionedHashes
Expand All @@ -265,7 +264,7 @@ func (stx *BlobTx) encodePayload(w io.Writer, b []byte, payloadSize, nonceLen, g
func (stx *BlobTx) EncodeRLP(w io.Writer) error {
payloadSize, nonceLen, gasLen, accessListLen, blobHashesLen := stx.payloadSize()
// size of struct prefix and TxType
envelopeSize := 1 + rlp2.ListPrefixLen(payloadSize) + payloadSize
envelopeSize := 1 + rlp.ListPrefixLen(payloadSize) + payloadSize
b := newEncodingBuf()
defer pooledBuf.Put(b)
// envelope
Expand Down
6 changes: 3 additions & 3 deletions core/types/blob_tx_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func (li BlobKzgs) payloadSize() int {

func (li BlobKzgs) encodePayload(w io.Writer, b []byte, payloadSize int) error {
// prefix
if err := EncodeStructSizePrefix(payloadSize, w, b); err != nil {
if err := rlp.EncodeStructSizePrefix(payloadSize, w, b); err != nil {
return err
}

Expand Down Expand Up @@ -125,7 +125,7 @@ func (li KZGProofs) payloadSize() int {

func (li KZGProofs) encodePayload(w io.Writer, b []byte, payloadSize int) error {
// prefix
if err := EncodeStructSizePrefix(payloadSize, w, b); err != nil {
if err := rlp.EncodeStructSizePrefix(payloadSize, w, b); err != nil {
return err
}

Expand Down Expand Up @@ -179,7 +179,7 @@ func (blobs Blobs) payloadSize() int {

func (blobs Blobs) encodePayload(w io.Writer, b []byte, payloadSize int) error {
// prefix
if err := EncodeStructSizePrefix(payloadSize, w, b); err != nil {
if err := rlp.EncodeStructSizePrefix(payloadSize, w, b); err != nil {
return err
}

Expand Down
Loading

0 comments on commit 89a4a1e

Please sign in to comment.