From ea473ed3b1774c4200efeb49664716cf74fb7417 Mon Sep 17 00:00:00 2001 From: racytech Date: Mon, 25 Nov 2024 12:57:34 +0500 Subject: [PATCH] rlp: introducing pooled buff, remove allocations from basic types --- core/types/access_list_tx.go | 32 +++--- core/types/authorization.go | 18 ++-- core/types/blob_tx.go | 28 ++--- core/types/block.go | 39 ++++--- core/types/dynamic_fee_tx.go | 20 ++-- core/types/encdec_test.go | 192 ++++++++++++++++++++++++++++++----- core/types/legacy_tx.go | 18 ++-- core/types/set_code_tx.go | 20 ++-- core/types/withdrawal.go | 26 ++--- rlp/encode.go | 28 ++++- 10 files changed, 302 insertions(+), 119 deletions(-) diff --git a/core/types/access_list_tx.go b/core/types/access_list_tx.go index 24f09377caa..72238d332bc 100644 --- a/core/types/access_list_tx.go +++ b/core/types/access_list_tx.go @@ -168,26 +168,26 @@ func accessListSize(al AccessList) int { } func encodeAccessList(al AccessList, w io.Writer, b []byte) error { - for _, tuple := range al { + for i := 0; i < len(al); i++ { tupleLen := 21 // Each storage key takes 33 bytes - storageLen := 33 * len(tuple.StorageKeys) + storageLen := 33 * len(al[i].StorageKeys) tupleLen += rlp2.ListPrefixLen(storageLen) + storageLen if err := EncodeStructSizePrefix(tupleLen, w, b); err != nil { return err } - if err := rlp.EncodeOptionalAddress(&tuple.Address, w, b); err != nil { + 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 { return err } b[0] = 128 + 32 - for _, storageKey := range tuple.StorageKeys { + for idx := 0; idx < len(al[i].StorageKeys); idx++ { if _, err := w.Write(b[:1]); err != nil { return err } - if _, err := w.Write(storageKey.Bytes()); err != nil { + if _, err := w.Write(al[i].StorageKeys[idx][:]); err != nil { return err } } @@ -195,7 +195,7 @@ func encodeAccessList(al AccessList, w io.Writer, b []byte) error { return nil } -func EncodeStructSizePrefix(size int, w io.Writer, b []byte) error { +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)) @@ -217,7 +217,8 @@ func EncodeStructSizePrefix(size int, w io.Writer, b []byte) error { // transactions, it returns the type and payload. func (tx *AccessListTx) MarshalBinary(w io.Writer) error { payloadSize, nonceLen, gasLen, accessListLen := tx.payloadSize() - var b [33]byte + b := newEncodingBuf() + defer pooledBuf.Put(b) // encode TxType b[0] = AccessListTxType if _, err := w.Write(b[:1]); err != nil { @@ -235,7 +236,7 @@ func (tx *AccessListTx) encodePayload(w io.Writer, b []byte, payloadSize, nonceL return err } // encode ChainID - if err := tx.ChainID.EncodeRLP(w); err != nil { + if err := rlp.EncodeUint256(tx.ChainID, w, b); err != nil { return err } // encode Nonce @@ -243,7 +244,7 @@ func (tx *AccessListTx) encodePayload(w io.Writer, b []byte, payloadSize, nonceL return err } // encode GasPrice - if err := tx.GasPrice.EncodeRLP(w); err != nil { + if err := rlp.EncodeUint256(tx.GasPrice, w, b); err != nil { return err } // encode Gas @@ -260,12 +261,12 @@ func (tx *AccessListTx) encodePayload(w io.Writer, b []byte, payloadSize, nonceL return err } if tx.To != nil { - if _, err := w.Write(tx.To.Bytes()); err != nil { + if _, err := w.Write(tx.To[:]); err != nil { return err } } // encode Value - if err := tx.Value.EncodeRLP(w); err != nil { + if err := rlp.EncodeUint256(tx.Value, w, b); err != nil { return err } // encode Data @@ -281,15 +282,15 @@ func (tx *AccessListTx) encodePayload(w io.Writer, b []byte, payloadSize, nonceL return err } // encode V - if err := tx.V.EncodeRLP(w); err != nil { + if err := rlp.EncodeUint256(&tx.V, w, b); err != nil { return err } // encode R - if err := tx.R.EncodeRLP(w); err != nil { + if err := rlp.EncodeUint256(&tx.R, w, b); err != nil { return err } // encode S - if err := tx.S.EncodeRLP(w); err != nil { + if err := rlp.EncodeUint256(&tx.S, w, b); err != nil { return err } return nil @@ -301,7 +302,8 @@ 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 - var b [33]byte + b := newEncodingBuf() + defer pooledBuf.Put(b) // envelope if err := rlp.EncodeStringSizePrefix(envelopeSize, w, b[:]); err != nil { return err diff --git a/core/types/authorization.go b/core/types/authorization.go index ed9036b2db6..b928f5e2e6b 100644 --- a/core/types/authorization.go +++ b/core/types/authorization.go @@ -182,36 +182,34 @@ func decodeAuthorizations(auths *[]Authorization, s *rlp.Stream) error { } func encodeAuthorizations(authorizations []Authorization, w io.Writer, b []byte) error { - for _, auth := range authorizations { - // 0. encode length of individual Authorization - authLen := authorizationSize(auth) + for i := 0; i < len(authorizations); i++ { + authLen := authorizationSize(authorizations[i]) if err := EncodeStructSizePrefix(authLen, w, b); err != nil { return err } // 1. encode ChainId - if err := rlp.EncodeInt(auth.ChainID, w, b); err != nil { + if err := rlp.EncodeInt(authorizations[i].ChainID, w, b); err != nil { return err } // 2. encode Address - if err := rlp.EncodeOptionalAddress(&auth.Address, w, b); err != nil { + if err := rlp.EncodeOptionalAddress(&authorizations[i].Address, w, b); err != nil { return err } // 3. encode Nonce - if err := rlp.EncodeInt(auth.Nonce, w, b); err != nil { + if err := rlp.EncodeInt(authorizations[i].Nonce, w, b); err != nil { return err } // 4. encode YParity, R, S - if err := rlp.EncodeInt(uint64(auth.YParity), w, b); err != nil { + if err := rlp.EncodeInt(uint64(authorizations[i].YParity), w, b); err != nil { return err } - if err := auth.R.EncodeRLP(w); err != nil { + if err := rlp.EncodeUint256(&authorizations[i].R, w, b); err != nil { return err } - if err := auth.S.EncodeRLP(w); err != nil { + if err := rlp.EncodeUint256(&authorizations[i].S, w, b); err != nil { return err } } - return nil } diff --git a/core/types/blob_tx.go b/core/types/blob_tx.go index 72845624e3e..e453a019863 100644 --- a/core/types/blob_tx.go +++ b/core/types/blob_tx.go @@ -178,8 +178,8 @@ func blobVersionedHashesSize(hashes []libcommon.Hash) int { } func encodeBlobVersionedHashes(hashes []libcommon.Hash, w io.Writer, b []byte) error { - for _, h := range hashes { - if err := rlp.EncodeString(h[:], w, b); err != nil { + for i := 0; i < len(hashes); i++ { + if err := rlp.EncodeString(hashes[i][:], w, b); err != nil { return err } } @@ -192,7 +192,7 @@ func (stx *BlobTx) encodePayload(w io.Writer, b []byte, payloadSize, nonceLen, g return err } // encode ChainID - if err := stx.ChainID.EncodeRLP(w); err != nil { + if err := rlp.EncodeUint256(stx.ChainID, w, b); err != nil { return err } // encode Nonce @@ -200,11 +200,11 @@ func (stx *BlobTx) encodePayload(w io.Writer, b []byte, payloadSize, nonceLen, g return err } // encode MaxPriorityFeePerGas - if err := stx.Tip.EncodeRLP(w); err != nil { + if err := rlp.EncodeUint256(stx.Tip, w, b); err != nil { return err } // encode MaxFeePerGas - if err := stx.FeeCap.EncodeRLP(w); err != nil { + if err := rlp.EncodeUint256(stx.FeeCap, w, b); err != nil { return err } // encode Gas @@ -216,11 +216,11 @@ func (stx *BlobTx) encodePayload(w io.Writer, b []byte, payloadSize, nonceLen, g if _, err := w.Write(b[:1]); err != nil { return err } - if _, err := w.Write(stx.To.Bytes()); err != nil { + if _, err := w.Write(stx.To[:]); err != nil { return err } // encode Value - if err := stx.Value.EncodeRLP(w); err != nil { + if err := rlp.EncodeUint256(stx.Value, w, b); err != nil { return err } // encode Data @@ -236,7 +236,7 @@ func (stx *BlobTx) encodePayload(w io.Writer, b []byte, payloadSize, nonceLen, g return err } // encode MaxFeePerBlobGas - if err := stx.MaxFeePerBlobGas.EncodeRLP(w); err != nil { + if err := rlp.EncodeUint256(stx.MaxFeePerBlobGas, w, b); err != nil { return err } // prefix @@ -248,15 +248,15 @@ func (stx *BlobTx) encodePayload(w io.Writer, b []byte, payloadSize, nonceLen, g return err } // encode V - if err := stx.V.EncodeRLP(w); err != nil { + if err := rlp.EncodeUint256(&stx.V, w, b); err != nil { return err } // encode R - if err := stx.R.EncodeRLP(w); err != nil { + if err := rlp.EncodeUint256(&stx.R, w, b); err != nil { return err } // encode S - if err := stx.S.EncodeRLP(w); err != nil { + if err := rlp.EncodeUint256(&stx.S, w, b); err != nil { return err } return nil @@ -266,7 +266,8 @@ 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 - var b [33]byte + b := newEncodingBuf() + defer pooledBuf.Put(b) // envelope if err := rlp.EncodeStringSizePrefix(envelopeSize, w, b[:]); err != nil { return err @@ -284,7 +285,8 @@ func (stx *BlobTx) EncodeRLP(w io.Writer) error { func (stx *BlobTx) MarshalBinary(w io.Writer) error { payloadSize, nonceLen, gasLen, accessListLen, blobHashesLen := stx.payloadSize() - var b [33]byte + b := newEncodingBuf() + defer pooledBuf.Put(b) // encode TxType b[0] = BlobTxType if _, err := w.Write(b[:1]); err != nil { diff --git a/core/types/block.go b/core/types/block.go index 1a549caba0a..06f7ad77803 100644 --- a/core/types/block.go +++ b/core/types/block.go @@ -200,7 +200,8 @@ func (h *Header) EncodingSize() int { func (h *Header) EncodeRLP(w io.Writer) error { encodingSize := h.EncodingSize() - var b [33]byte + b := newEncodingBuf() + defer pooledBuf.Put(b) // Prefix if err := EncodeStructSizePrefix(encodingSize, w, b[:]); err != nil { return err @@ -209,39 +210,39 @@ func (h *Header) EncodeRLP(w io.Writer) error { if _, err := w.Write(b[:1]); err != nil { return err } - if _, err := w.Write(h.ParentHash.Bytes()); err != nil { + if _, err := w.Write(h.ParentHash[:]); err != nil { return err } if _, err := w.Write(b[:1]); err != nil { return err } - if _, err := w.Write(h.UncleHash.Bytes()); err != nil { + if _, err := w.Write(h.UncleHash[:]); err != nil { return err } b[0] = 128 + 20 if _, err := w.Write(b[:1]); err != nil { return err } - if _, err := w.Write(h.Coinbase.Bytes()); err != nil { + if _, err := w.Write(h.Coinbase[:]); err != nil { return err } b[0] = 128 + 32 if _, err := w.Write(b[:1]); err != nil { return err } - if _, err := w.Write(h.Root.Bytes()); err != nil { + if _, err := w.Write(h.Root[:]); err != nil { return err } if _, err := w.Write(b[:1]); err != nil { return err } - if _, err := w.Write(h.TxHash.Bytes()); err != nil { + if _, err := w.Write(h.TxHash[:]); err != nil { return err } if _, err := w.Write(b[:1]); err != nil { return err } - if _, err := w.Write(h.ReceiptHash.Bytes()); err != nil { + if _, err := w.Write(h.ReceiptHash[:]); err != nil { return err } b[0] = 183 + 2 @@ -250,7 +251,7 @@ func (h *Header) EncodeRLP(w io.Writer) error { if _, err := w.Write(b[:3]); err != nil { return err } - if _, err := w.Write(h.Bloom.Bytes()); err != nil { + if _, err := w.Write(h.Bloom[:]); err != nil { return err } if err := rlp.EncodeBigInt(h.Difficulty, w, b[:]); err != nil { @@ -284,7 +285,7 @@ func (h *Header) EncodeRLP(w io.Writer) error { if _, err := w.Write(b[:1]); err != nil { return err } - if _, err := w.Write(h.MixDigest.Bytes()); err != nil { + if _, err := w.Write(h.MixDigest[:]); err != nil { return err } b[0] = 128 + 8 @@ -307,7 +308,7 @@ func (h *Header) EncodeRLP(w io.Writer) error { if _, err := w.Write(b[:1]); err != nil { return err } - if _, err := w.Write(h.WithdrawalsHash.Bytes()); err != nil { + if _, err := w.Write(h.WithdrawalsHash[:]); err != nil { return err } } @@ -328,7 +329,7 @@ func (h *Header) EncodeRLP(w io.Writer) error { if _, err := w.Write(b[:1]); err != nil { return err } - if _, err := w.Write(h.ParentBeaconBlockRoot.Bytes()); err != nil { + if _, err := w.Write(h.ParentBeaconBlockRoot[:]); err != nil { return err } } @@ -338,7 +339,7 @@ func (h *Header) EncodeRLP(w io.Writer) error { if _, err := w.Write(b[:1]); err != nil { return err } - if _, err := w.Write(h.RequestsHash.Bytes()); err != nil { + if _, err := w.Write(h.RequestsHash[:]); err != nil { return err } } @@ -788,7 +789,8 @@ func (rb RawBody) payloadSize() (payloadSize, txsLen, unclesLen, withdrawalsLen func (rb RawBody) EncodeRLP(w io.Writer) error { payloadSize, txsLen, unclesLen, withdrawalsLen := rb.payloadSize() - var b [33]byte + b := newEncodingBuf() + defer pooledBuf.Put(b) // prefix if err := EncodeStructSizePrefix(payloadSize, w, b[:]); err != nil { return err @@ -873,7 +875,8 @@ func (bfs BodyForStorage) payloadSize() (payloadSize, unclesLen, withdrawalsLen func (bfs BodyForStorage) EncodeRLP(w io.Writer) error { payloadSize, unclesLen, withdrawalsLen := bfs.payloadSize() - var b [33]byte + b := newEncodingBuf() + defer pooledBuf.Put(b) // prefix if err := EncodeStructSizePrefix(payloadSize, w, b[:]); err != nil { @@ -956,7 +959,9 @@ func (bb Body) payloadSize() (payloadSize int, txsLen, unclesLen, withdrawalsLen func (bb Body) EncodeRLP(w io.Writer) error { payloadSize, txsLen, unclesLen, withdrawalsLen := bb.payloadSize() - var b [33]byte + + b := newEncodingBuf() + defer pooledBuf.Put(b) // prefix if err := EncodeStructSizePrefix(payloadSize, w, b[:]); err != nil { return err @@ -1201,7 +1206,9 @@ func (bb *Block) EncodingSize() int { // EncodeRLP serializes b into the Ethereum RLP block format. func (bb *Block) EncodeRLP(w io.Writer) error { payloadSize, txsLen, unclesLen, withdrawalsLen := bb.payloadSize() - var b [33]byte + + b := newEncodingBuf() + defer pooledBuf.Put(b) // prefix if err := EncodeStructSizePrefix(payloadSize, w, b[:]); err != nil { return err diff --git a/core/types/dynamic_fee_tx.go b/core/types/dynamic_fee_tx.go index 225467caa4d..78d7b8e0e3e 100644 --- a/core/types/dynamic_fee_tx.go +++ b/core/types/dynamic_fee_tx.go @@ -172,7 +172,8 @@ func (tx *DynamicFeeTransaction) WithSignature(signer Signer, sig []byte) (Trans // transactions, it returns the type and payload. func (tx *DynamicFeeTransaction) MarshalBinary(w io.Writer) error { payloadSize, nonceLen, gasLen, accessListLen := tx.payloadSize() - var b [33]byte + b := newEncodingBuf() + defer pooledBuf.Put(b) // encode TxType b[0] = DynamicFeeTxType if _, err := w.Write(b[:1]); err != nil { @@ -190,7 +191,7 @@ func (tx *DynamicFeeTransaction) encodePayload(w io.Writer, b []byte, payloadSiz return err } // encode ChainID - if err := tx.ChainID.EncodeRLP(w); err != nil { + if err := rlp.EncodeUint256(tx.ChainID, w, b); err != nil { return err } // encode Nonce @@ -198,11 +199,11 @@ func (tx *DynamicFeeTransaction) encodePayload(w io.Writer, b []byte, payloadSiz return err } // encode MaxPriorityFeePerGas - if err := tx.Tip.EncodeRLP(w); err != nil { + if err := rlp.EncodeUint256(tx.Tip, w, b); err != nil { return err } // encode MaxFeePerGas - if err := tx.FeeCap.EncodeRLP(w); err != nil { + if err := rlp.EncodeUint256(tx.FeeCap, w, b); err != nil { return err } // encode Gas @@ -214,7 +215,7 @@ func (tx *DynamicFeeTransaction) encodePayload(w io.Writer, b []byte, payloadSiz return err } // encode Value - if err := tx.Value.EncodeRLP(w); err != nil { + if err := rlp.EncodeUint256(tx.Value, w, b); err != nil { return err } // encode Data @@ -230,15 +231,15 @@ func (tx *DynamicFeeTransaction) encodePayload(w io.Writer, b []byte, payloadSiz return err } // encode V - if err := tx.V.EncodeRLP(w); err != nil { + if err := rlp.EncodeUint256(&tx.V, w, b); err != nil { return err } // encode R - if err := tx.R.EncodeRLP(w); err != nil { + if err := rlp.EncodeUint256(&tx.R, w, b); err != nil { return err } // encode S - if err := tx.S.EncodeRLP(w); err != nil { + if err := rlp.EncodeUint256(&tx.S, w, b); err != nil { return err } return nil @@ -248,7 +249,8 @@ func (tx *DynamicFeeTransaction) EncodeRLP(w io.Writer) error { payloadSize, nonceLen, gasLen, accessListLen := tx.payloadSize() // size of struct prefix and TxType envelopeSize := 1 + rlp2.ListPrefixLen(payloadSize) + payloadSize - var b [33]byte + b := newEncodingBuf() + defer pooledBuf.Put(b) // envelope if err := rlp.EncodeStringSizePrefix(envelopeSize, w, b[:]); err != nil { return err diff --git a/core/types/encdec_test.go b/core/types/encdec_test.go index c31e3d7be72..23ebd07bea8 100644 --- a/core/types/encdec_test.go +++ b/core/types/encdec_test.go @@ -28,10 +28,11 @@ import ( "github.com/holiman/uint256" libcommon "github.com/erigontech/erigon-lib/common" + rlp2 "github.com/erigontech/erigon-lib/rlp" "github.com/erigontech/erigon/rlp" ) -const RUNS = 100 // for local tests increase this number +const RUNS = 100000 // for local tests increase this number type TRand struct { rnd *rand.Rand @@ -147,8 +148,13 @@ func (tr *TRand) RandAuthorizations(size int) []Authorization { return auths } -func (tr *TRand) RandTransaction() Transaction { - txType := tr.RandIntInRange(0, 5) // LegacyTxType, AccessListTxType, DynamicFeeTxType, BlobTxType, SetCodeTxType +func (tr *TRand) RandTransaction(_type int) Transaction { + var txType int + if _type == -1 { + txType = tr.RandIntInRange(0, 5) // LegacyTxType, AccessListTxType, DynamicFeeTxType, BlobTxType, SetCodeTxType + } else { + txType = _type + } to := tr.RandAddress() commonTx := CommonTx{ Nonce: *tr.RandUint64(), @@ -224,7 +230,7 @@ func (tr *TRand) RandHashes(size int) []libcommon.Hash { func (tr *TRand) RandTransactions(size int) []Transaction { txns := make([]Transaction, size) for i := 0; i < size; i++ { - txns[i] = tr.RandTransaction() + txns[i] = tr.RandTransaction(-1) } return txns } @@ -232,7 +238,18 @@ func (tr *TRand) RandTransactions(size int) []Transaction { func (tr *TRand) RandRawTransactions(size int) [][]byte { txns := make([][]byte, size) for i := 0; i < size; i++ { - txns[i] = tr.RandBytes(tr.RandIntInRange(1, 1023)) + txns[i] = tr.RandBytes(tr.RandIntInRange(1, 512)) + } + return txns +} + +func (tr *TRand) RandRLPTransactions(size int) [][]byte { + txns := make([][]byte, size) + for i := 0; i < size; i++ { + txn := make([]byte, 512) + txSize := tr.RandIntInRange(1, 500) + encodedSize := rlp2.EncodeString(tr.RandBytes(txSize), txn) + txns[i] = txn[:encodedSize] } return txns } @@ -255,7 +272,7 @@ func (tr *TRand) RandWithdrawals(size int) []*Withdrawal { func (tr *TRand) RandRawBody() *RawBody { return &RawBody{ - Transactions: tr.RandRawTransactions(tr.RandIntInRange(1, 6)), + Transactions: tr.RandRLPTransactions(tr.RandIntInRange(1, 6)), Uncles: tr.RandHeaders(tr.RandIntInRange(1, 6)), Withdrawals: tr.RandWithdrawals(tr.RandIntInRange(1, 6)), } @@ -413,29 +430,71 @@ func compareBodies(t *testing.T, a, b *Body) error { return nil } -// func TestRawBodyEncodeDecodeRLP(t *testing.T) { -// tr := NewTRand() -// var buf bytes.Buffer -// for i := 0; i < RUNS; i++ { -// enc := tr.RandRawBody() -// buf.Reset() -// if err := enc.EncodeRLP(&buf); err != nil { -// t.Errorf("error: RawBody.EncodeRLP(): %v", err) -// } +func TestTransactionEncodeDecodeRLP(t *testing.T) { + tr := NewTRand() + var buf bytes.Buffer + for i := 0; i < RUNS; i++ { + enc := tr.RandTransaction(-1) + buf.Reset() + if err := enc.EncodeRLP(&buf); err != nil { + t.Errorf("error: RawBody.EncodeRLP(): %v", err) + } + + s := rlp.NewStream(bytes.NewReader(buf.Bytes()), 0) + + dec, err := DecodeRLPTransaction(s, false) + if err != nil { + t.Errorf("error: DecodeRLPTransaction: %v", err) + } + compareTransactions(t, enc, dec) + } +} + +func TestHeaderEncodeDecodeRLP(t *testing.T) { + tr := NewTRand() + var buf bytes.Buffer + for i := 0; i < RUNS; i++ { + enc := tr.RandHeader() + buf.Reset() + if err := enc.EncodeRLP(&buf); err != nil { + t.Errorf("error: Header.EncodeRLP(): %v", err) + } + + s := rlp.NewStream(bytes.NewReader(buf.Bytes()), 0) + + dec := &Header{} + if err := dec.DecodeRLP(s); err != nil { + t.Errorf("error: Header.DecodeRLP(): %v", err) + panic(err) + } + + checkHeaders(t, enc, dec) + } +} -// s := rlp.NewStream(bytes.NewReader(buf.Bytes()), 0) +func TestRawBodyEncodeDecodeRLP(t *testing.T) { + tr := NewTRand() + var buf bytes.Buffer + for i := 0; i < RUNS; i++ { + enc := tr.RandRawBody() + buf.Reset() + if err := enc.EncodeRLP(&buf); err != nil { + t.Errorf("error: RawBody.EncodeRLP(): %v", err) + } -// dec := &RawBody{} -// if err := dec.DecodeRLP(s); err != nil { -// t.Errorf("error: RawBody.DecodeRLP(): %v", err) -// panic(err) -// } + s := rlp.NewStream(bytes.NewReader(buf.Bytes()), 0) -// if err := compareRawBodies(t, enc, dec); err != nil { -// t.Errorf("error: compareRawBodies: %v", err) -// } -// } -// } + dec := &RawBody{} + if err := dec.DecodeRLP(s); err != nil { + t.Errorf("error: RawBody.DecodeRLP(): %v", err) + panic(err) + } + + if err := compareRawBodies(t, enc, dec); err != nil { + t.Errorf("error: compareRawBodies: %v", err) + } + } +} func TestBodyEncodeDecodeRLP(t *testing.T) { tr := NewTRand() @@ -480,3 +539,84 @@ func TestWithdrawalEncodeDecodeRLP(t *testing.T) { checkWithdrawals(t, enc, dec) } } + +/* + Benchmarks +*/ + +func BenchmarkHeaderRLP(b *testing.B) { + tr := NewTRand() + header := tr.RandHeader() + var buf bytes.Buffer + b.ResetTimer() + for i := 0; i < b.N; i++ { + buf.Reset() + header.EncodeRLP(&buf) + } +} + +func BenchmarkLegacyTxRLP(b *testing.B) { + tr := NewTRand() + txn := tr.RandTransaction(LegacyTxType) + var buf bytes.Buffer + b.ResetTimer() + for i := 0; i < b.N; i++ { + buf.Reset() + txn.EncodeRLP(&buf) + } +} + +func BenchmarkAccessListTxRLP(b *testing.B) { + tr := NewTRand() + txn := tr.RandTransaction(AccessListTxType) + var buf bytes.Buffer + b.ResetTimer() + for i := 0; i < b.N; i++ { + buf.Reset() + txn.EncodeRLP(&buf) + } +} + +func BenchmarkDynamicFeeTxRLP(b *testing.B) { + tr := NewTRand() + txn := tr.RandTransaction(DynamicFeeTxType) + var buf bytes.Buffer + b.ResetTimer() + for i := 0; i < b.N; i++ { + buf.Reset() + txn.EncodeRLP(&buf) + } +} + +func BenchmarkBlobTxRLP(b *testing.B) { + tr := NewTRand() + txn := tr.RandTransaction(BlobTxType) + var buf bytes.Buffer + b.ResetTimer() + for i := 0; i < b.N; i++ { + buf.Reset() + txn.EncodeRLP(&buf) + } +} + +func BenchmarkSetCodeTxRLP(b *testing.B) { + tr := NewTRand() + txn := tr.RandTransaction(SetCodeTxType) + var buf bytes.Buffer + b.ResetTimer() + for i := 0; i < b.N; i++ { + buf.Reset() + txn.EncodeRLP(&buf) + } +} + +func BenchmarkWithdrawalRLP(b *testing.B) { + tr := NewTRand() + w := tr.RandWithdrawal() + var buf bytes.Buffer + b.ResetTimer() + for i := 0; i < b.N; i++ { + buf.Reset() + w.EncodeRLP(&buf) + } +} diff --git a/core/types/legacy_tx.go b/core/types/legacy_tx.go index 1cffe818772..f7ed6776c3d 100644 --- a/core/types/legacy_tx.go +++ b/core/types/legacy_tx.go @@ -219,7 +219,8 @@ func (tx *LegacyTx) payloadSize() (payloadSize int, nonceLen, gasLen int) { func (tx *LegacyTx) MarshalBinary(w io.Writer) error { payloadSize, nonceLen, gasLen := tx.payloadSize() - var b [33]byte + b := newEncodingBuf() + defer pooledBuf.Put(b) if err := tx.encodePayload(w, b[:], payloadSize, nonceLen, gasLen); err != nil { return err } @@ -243,7 +244,7 @@ func (tx *LegacyTx) encodePayload(w io.Writer, b []byte, payloadSize, nonceLen, return err } } - if err := tx.GasPrice.EncodeRLP(w); err != nil { + if err := rlp.EncodeUint256(tx.GasPrice, w, b); err != nil { return err } if err := rlp.EncodeInt(tx.Gas, w, b); err != nil { @@ -258,23 +259,23 @@ func (tx *LegacyTx) encodePayload(w io.Writer, b []byte, payloadSize, nonceLen, return err } if tx.To != nil { - if _, err := w.Write(tx.To.Bytes()); err != nil { + if _, err := w.Write(tx.To[:]); err != nil { return err } } - if err := tx.Value.EncodeRLP(w); err != nil { + if err := rlp.EncodeUint256(tx.Value, w, b); err != nil { return err } if err := rlp.EncodeString(tx.Data, w, b); err != nil { return err } - if err := tx.V.EncodeRLP(w); err != nil { + if err := rlp.EncodeUint256(&tx.V, w, b); err != nil { return err } - if err := tx.R.EncodeRLP(w); err != nil { + if err := rlp.EncodeUint256(&tx.R, w, b); err != nil { return err } - if err := tx.S.EncodeRLP(w); err != nil { + if err := rlp.EncodeUint256(&tx.S, w, b); err != nil { return err } return nil @@ -283,7 +284,8 @@ func (tx *LegacyTx) encodePayload(w io.Writer, b []byte, payloadSize, nonceLen, func (tx *LegacyTx) EncodeRLP(w io.Writer) error { payloadSize, nonceLen, gasLen := tx.payloadSize() - var b [33]byte + b := newEncodingBuf() + defer pooledBuf.Put(b) if err := tx.encodePayload(w, b[:], payloadSize, nonceLen, gasLen); err != nil { return err } diff --git a/core/types/set_code_tx.go b/core/types/set_code_tx.go index fc6542f6375..8f68e462e21 100644 --- a/core/types/set_code_tx.go +++ b/core/types/set_code_tx.go @@ -99,7 +99,8 @@ func (tx *SetCodeTransaction) WithSignature(signer Signer, sig []byte) (Transact func (tx *SetCodeTransaction) MarshalBinary(w io.Writer) error { payloadSize, nonceLen, gasLen, accessListLen, authorizationsLen := tx.payloadSize() - var b [33]byte + b := newEncodingBuf() + defer pooledBuf.Put(b) // encode TxType b[0] = SetCodeTxType if _, err := w.Write(b[:1]); err != nil { @@ -203,7 +204,8 @@ func (tx *SetCodeTransaction) SigningHash(chainID *big.Int) libcommon.Hash { func (tx *SetCodeTransaction) EncodeRLP(w io.Writer) error { payloadSize, nonceLen, gasLen, accessListLen, authorizationsLen := tx.payloadSize() envelopSize := 1 + rlp2.ListPrefixLen(payloadSize) + payloadSize - var b [33]byte + b := newEncodingBuf() + defer pooledBuf.Put(b) // encode envelope size if err := rlp.EncodeStringSizePrefix(envelopSize, w, b[:]); err != nil { return err @@ -290,7 +292,7 @@ func (tx *SetCodeTransaction) encodePayload(w io.Writer, b []byte, payloadSize, return err } // encode ChainID - if err := tx.ChainID.EncodeRLP(w); err != nil { + if err := rlp.EncodeUint256(tx.ChainID, w, b); err != nil { return err } // encode Nonce @@ -298,11 +300,11 @@ func (tx *SetCodeTransaction) encodePayload(w io.Writer, b []byte, payloadSize, return err } // encode MaxPriorityFeePerGas - if err := tx.Tip.EncodeRLP(w); err != nil { + if err := rlp.EncodeUint256(tx.Tip, w, b); err != nil { return err } // encode MaxFeePerGas - if err := tx.FeeCap.EncodeRLP(w); err != nil { + if err := rlp.EncodeUint256(tx.FeeCap, w, b); err != nil { return err } // encode Gas @@ -314,7 +316,7 @@ func (tx *SetCodeTransaction) encodePayload(w io.Writer, b []byte, payloadSize, return err } // encode Value - if err := tx.Value.EncodeRLP(w); err != nil { + if err := rlp.EncodeUint256(tx.Value, w, b); err != nil { return err } // encode Data @@ -338,15 +340,15 @@ func (tx *SetCodeTransaction) encodePayload(w io.Writer, b []byte, payloadSize, return err } // encode V - if err := tx.V.EncodeRLP(w); err != nil { + if err := rlp.EncodeUint256(&tx.V, w, b); err != nil { return err } // encode R - if err := tx.R.EncodeRLP(w); err != nil { + if err := rlp.EncodeUint256(&tx.R, w, b); err != nil { return err } // encode S - if err := tx.S.EncodeRLP(w); err != nil { + if err := rlp.EncodeUint256(&tx.S, w, b); err != nil { return err } return nil diff --git a/core/types/withdrawal.go b/core/types/withdrawal.go index 96d18f19663..c009025118c 100644 --- a/core/types/withdrawal.go +++ b/core/types/withdrawal.go @@ -23,6 +23,7 @@ import ( "bytes" "fmt" "io" + "sync" "github.com/erigontech/erigon-lib/common/hexutil" @@ -31,18 +32,17 @@ import ( "github.com/erigontech/erigon/rlp" ) -// var buff = sync.Pool{ -// New: func() interface{} { return new([33]byte) }, -// } +type encodingBuf [33]byte -// func (b *buff) reset() { - -// } +var pooledBuf = sync.Pool{ + New: func() interface{} { return new(encodingBuf) }, +} -// func newEncBuff() [33]byte { -// buf := buff.Get().([33]byte) -// buf.reset() -// } +func newEncodingBuf() *encodingBuf { + b := pooledBuf.Get().(*encodingBuf) + *b = encodingBuf([33]byte{}) // reset, do we need to? + return b +} //go:generate gencodec -type Withdrawal -field-override withdrawalMarshaling -out gen_withdrawal_json.go @@ -70,7 +70,9 @@ func (obj *Withdrawal) EncodeRLP(w io.Writer) error { encodingSize := obj.EncodingSize() - b := make([]byte, 33) + b := newEncodingBuf() + defer pooledBuf.Put(b) + if err := EncodeStructSizePrefix(encodingSize, w, b[:]); err != nil { return err } @@ -86,7 +88,7 @@ func (obj *Withdrawal) EncodeRLP(w io.Writer) error { if _, err := w.Write(b[:1]); err != nil { return err } - if _, err := w.Write(obj.Address.Bytes()); err != nil { + if _, err := w.Write(obj.Address[:]); err != nil { return err } diff --git a/rlp/encode.go b/rlp/encode.go index 75c14a9913b..6c7abf4ea94 100644 --- a/rlp/encode.go +++ b/rlp/encode.go @@ -616,6 +616,32 @@ func EncodeBigInt(i *big.Int, w io.Writer, buffer []byte) error { return err } +func EncodeUint256(i *uint256.Int, w io.Writer, buffer []byte) error { + buffer[0] = 0x80 + if i == nil { + _, err := w.Write(buffer[:1]) + return err + } + nBits := i.BitLen() + if nBits == 0 { + _, err := w.Write(buffer[:1]) + return err + } + buffer[0] = byte(i[0]) + if nBits <= 7 { + _, err := w.Write(buffer[:1]) + return err + } + nBytes := byte((nBits + 7) / 8) + binary.BigEndian.PutUint64(buffer[1:9], i[3]) + binary.BigEndian.PutUint64(buffer[9:17], i[2]) + binary.BigEndian.PutUint64(buffer[17:25], i[1]) + binary.BigEndian.PutUint64(buffer[25:33], i[0]) + buffer[32-nBytes] = 0x80 + nBytes + _, err := w.Write(buffer[32-nBytes:]) + return err +} + func EncodeString(s []byte, w io.Writer, buffer []byte) error { switch len(s) { case 0: @@ -672,7 +698,7 @@ func EncodeOptionalAddress(addr *libcommon.Address, w io.Writer, buffer []byte) return err } if addr != nil { - if _, err := w.Write(addr.Bytes()); err != nil { + if _, err := w.Write(addr[:]); err != nil { return err } }