mirror of
https://gitlab.com/pulsechaincom/erigon-pulse.git
synced 2024-12-22 03:30:37 +00:00
Simplify RLP size calculations (#8506)
Use `ListPrefixLen` & `StringLen` from erigon-lib more
This commit is contained in:
parent
33d5399436
commit
f26be2c4c3
@ -28,6 +28,7 @@ import (
|
|||||||
|
|
||||||
"github.com/ledgerwatch/erigon-lib/chain"
|
"github.com/ledgerwatch/erigon-lib/chain"
|
||||||
libcommon "github.com/ledgerwatch/erigon-lib/common"
|
libcommon "github.com/ledgerwatch/erigon-lib/common"
|
||||||
|
rlp2 "github.com/ledgerwatch/erigon-lib/rlp"
|
||||||
types2 "github.com/ledgerwatch/erigon-lib/types"
|
types2 "github.com/ledgerwatch/erigon-lib/types"
|
||||||
|
|
||||||
"github.com/ledgerwatch/erigon/common"
|
"github.com/ledgerwatch/erigon/common"
|
||||||
@ -93,13 +94,8 @@ func (tx *AccessListTx) Unwrap() Transaction {
|
|||||||
// EncodingSize returns the RLP encoding size of the whole transaction envelope
|
// EncodingSize returns the RLP encoding size of the whole transaction envelope
|
||||||
func (tx AccessListTx) EncodingSize() int {
|
func (tx AccessListTx) EncodingSize() int {
|
||||||
payloadSize, _, _, _ := tx.payloadSize()
|
payloadSize, _, _, _ := tx.payloadSize()
|
||||||
envelopeSize := payloadSize
|
|
||||||
// Add envelope size and type size
|
// Add envelope size and type size
|
||||||
if payloadSize >= 56 {
|
return 1 + rlp2.ListPrefixLen(payloadSize) + payloadSize
|
||||||
envelopeSize += libcommon.BitLenToByteLen(bits.Len(uint(payloadSize)))
|
|
||||||
}
|
|
||||||
envelopeSize += 2
|
|
||||||
return envelopeSize
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// payloadSize calculates the RLP encoding size of transaction, without TxType and envelope
|
// payloadSize calculates the RLP encoding size of transaction, without TxType and envelope
|
||||||
@ -127,26 +123,10 @@ func (tx AccessListTx) payloadSize() (payloadSize int, nonceLen, gasLen, accessL
|
|||||||
payloadSize++
|
payloadSize++
|
||||||
payloadSize += rlp.Uint256LenExcludingHead(tx.Value)
|
payloadSize += rlp.Uint256LenExcludingHead(tx.Value)
|
||||||
// size of Data
|
// size of Data
|
||||||
payloadSize++
|
payloadSize += rlp2.StringLen(tx.Data)
|
||||||
switch len(tx.Data) {
|
|
||||||
case 0:
|
|
||||||
case 1:
|
|
||||||
if tx.Data[0] >= 128 {
|
|
||||||
payloadSize++
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
if len(tx.Data) >= 56 {
|
|
||||||
payloadSize += libcommon.BitLenToByteLen(bits.Len(uint(len(tx.Data))))
|
|
||||||
}
|
|
||||||
payloadSize += len(tx.Data)
|
|
||||||
}
|
|
||||||
// size of AccessList
|
// size of AccessList
|
||||||
payloadSize++
|
|
||||||
accessListLen = accessListSize(tx.AccessList)
|
accessListLen = accessListSize(tx.AccessList)
|
||||||
if accessListLen >= 56 {
|
payloadSize += rlp2.ListPrefixLen(accessListLen) + accessListLen
|
||||||
payloadSize += libcommon.BitLenToByteLen(bits.Len(uint(accessListLen)))
|
|
||||||
}
|
|
||||||
payloadSize += accessListLen
|
|
||||||
// size of V
|
// size of V
|
||||||
payloadSize++
|
payloadSize++
|
||||||
payloadSize += rlp.Uint256LenExcludingHead(&tx.V)
|
payloadSize += rlp.Uint256LenExcludingHead(&tx.V)
|
||||||
@ -164,18 +144,10 @@ func accessListSize(al types2.AccessList) int {
|
|||||||
for _, tuple := range al {
|
for _, tuple := range al {
|
||||||
tupleLen := 21 // For the address
|
tupleLen := 21 // For the address
|
||||||
// size of StorageKeys
|
// size of StorageKeys
|
||||||
tupleLen++
|
|
||||||
// Each storage key takes 33 bytes
|
// Each storage key takes 33 bytes
|
||||||
storageLen := 33 * len(tuple.StorageKeys)
|
storageLen := 33 * len(tuple.StorageKeys)
|
||||||
if storageLen >= 56 {
|
tupleLen += rlp2.ListPrefixLen(storageLen) + storageLen
|
||||||
tupleLen += libcommon.BitLenToByteLen(bits.Len(uint(storageLen))) // BE encoding of the length of the storage keys
|
accessListLen += rlp2.ListPrefixLen(tupleLen) + tupleLen
|
||||||
}
|
|
||||||
tupleLen += storageLen
|
|
||||||
accessListLen++
|
|
||||||
if tupleLen >= 56 {
|
|
||||||
accessListLen += libcommon.BitLenToByteLen(bits.Len(uint(tupleLen))) // BE encoding of the length of the storage keys
|
|
||||||
}
|
|
||||||
accessListLen += tupleLen
|
|
||||||
}
|
}
|
||||||
return accessListLen
|
return accessListLen
|
||||||
}
|
}
|
||||||
@ -183,13 +155,9 @@ func accessListSize(al types2.AccessList) int {
|
|||||||
func encodeAccessList(al types2.AccessList, w io.Writer, b []byte) error {
|
func encodeAccessList(al types2.AccessList, w io.Writer, b []byte) error {
|
||||||
for _, tuple := range al {
|
for _, tuple := range al {
|
||||||
tupleLen := 21
|
tupleLen := 21
|
||||||
tupleLen++
|
|
||||||
// Each storage key takes 33 bytes
|
// Each storage key takes 33 bytes
|
||||||
storageLen := 33 * len(tuple.StorageKeys)
|
storageLen := 33 * len(tuple.StorageKeys)
|
||||||
if storageLen >= 56 {
|
tupleLen += rlp2.ListPrefixLen(storageLen) + storageLen
|
||||||
tupleLen += libcommon.BitLenToByteLen(bits.Len(uint(storageLen))) // BE encoding of the length of the storage keys
|
|
||||||
}
|
|
||||||
tupleLen += storageLen
|
|
||||||
if err := EncodeStructSizePrefix(tupleLen, w, b); err != nil {
|
if err := EncodeStructSizePrefix(tupleLen, w, b); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -320,12 +288,8 @@ func (tx AccessListTx) encodePayload(w io.Writer, b []byte, payloadSize, nonceLe
|
|||||||
// EncodeRLP implements rlp.Encoder
|
// EncodeRLP implements rlp.Encoder
|
||||||
func (tx AccessListTx) EncodeRLP(w io.Writer) error {
|
func (tx AccessListTx) EncodeRLP(w io.Writer) error {
|
||||||
payloadSize, nonceLen, gasLen, accessListLen := tx.payloadSize()
|
payloadSize, nonceLen, gasLen, accessListLen := tx.payloadSize()
|
||||||
envelopeSize := payloadSize
|
|
||||||
if payloadSize >= 56 {
|
|
||||||
envelopeSize += libcommon.BitLenToByteLen(bits.Len(uint(payloadSize)))
|
|
||||||
}
|
|
||||||
// size of struct prefix and TxType
|
// size of struct prefix and TxType
|
||||||
envelopeSize += 2
|
envelopeSize := 1 + rlp2.ListPrefixLen(payloadSize) + payloadSize
|
||||||
var b [33]byte
|
var b [33]byte
|
||||||
// envelope
|
// envelope
|
||||||
if err := rlp.EncodeStringSizePrefix(envelopeSize, w, b[:]); err != nil {
|
if err := rlp.EncodeStringSizePrefix(envelopeSize, w, b[:]); err != nil {
|
||||||
|
@ -8,6 +8,7 @@ import (
|
|||||||
|
|
||||||
"github.com/holiman/uint256"
|
"github.com/holiman/uint256"
|
||||||
libcommon "github.com/ledgerwatch/erigon-lib/common"
|
libcommon "github.com/ledgerwatch/erigon-lib/common"
|
||||||
|
rlp2 "github.com/ledgerwatch/erigon-lib/rlp"
|
||||||
|
|
||||||
"github.com/ledgerwatch/erigon/crypto"
|
"github.com/ledgerwatch/erigon/crypto"
|
||||||
"github.com/ledgerwatch/erigon/rlp"
|
"github.com/ledgerwatch/erigon/rlp"
|
||||||
@ -77,8 +78,6 @@ func (a *Account) EncodingLengthForStorage() uint {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (a *Account) EncodingLengthForHashing() uint {
|
func (a *Account) EncodingLengthForHashing() uint {
|
||||||
var structLength uint
|
|
||||||
|
|
||||||
balanceBytes := 0
|
balanceBytes := 0
|
||||||
if !a.Balance.LtUint64(128) {
|
if !a.Balance.LtUint64(128) {
|
||||||
balanceBytes = a.Balance.ByteLen()
|
balanceBytes = a.Balance.ByteLen()
|
||||||
@ -86,17 +85,11 @@ func (a *Account) EncodingLengthForHashing() uint {
|
|||||||
|
|
||||||
nonceBytes := rlp.IntLenExcludingHead(a.Nonce)
|
nonceBytes := rlp.IntLenExcludingHead(a.Nonce)
|
||||||
|
|
||||||
structLength += uint(balanceBytes + nonceBytes + 2)
|
structLength := balanceBytes + nonceBytes + 2
|
||||||
|
|
||||||
structLength += 66 // Two 32-byte arrays + 2 prefixes
|
structLength += 66 // Two 32-byte arrays + 2 prefixes
|
||||||
|
|
||||||
if structLength < 56 {
|
return uint(rlp2.ListPrefixLen(structLength) + structLength)
|
||||||
return 1 + structLength
|
|
||||||
}
|
|
||||||
|
|
||||||
lengthBytes := libcommon.BitLenToByteLen(bits.Len(structLength))
|
|
||||||
|
|
||||||
return uint(1+lengthBytes) + structLength
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *Account) EncodeForStorage(buffer []byte) {
|
func (a *Account) EncodeForStorage(buffer []byte) {
|
||||||
|
@ -4,13 +4,13 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"math/big"
|
"math/big"
|
||||||
"math/bits"
|
|
||||||
|
|
||||||
"github.com/holiman/uint256"
|
"github.com/holiman/uint256"
|
||||||
|
|
||||||
"github.com/ledgerwatch/erigon-lib/chain"
|
"github.com/ledgerwatch/erigon-lib/chain"
|
||||||
libcommon "github.com/ledgerwatch/erigon-lib/common"
|
libcommon "github.com/ledgerwatch/erigon-lib/common"
|
||||||
"github.com/ledgerwatch/erigon-lib/common/fixedgas"
|
"github.com/ledgerwatch/erigon-lib/common/fixedgas"
|
||||||
|
rlp2 "github.com/ledgerwatch/erigon-lib/rlp"
|
||||||
types2 "github.com/ledgerwatch/erigon-lib/types"
|
types2 "github.com/ledgerwatch/erigon-lib/types"
|
||||||
|
|
||||||
"github.com/ledgerwatch/erigon/rlp"
|
"github.com/ledgerwatch/erigon/rlp"
|
||||||
@ -102,12 +102,8 @@ func (stx BlobTx) payloadSize() (payloadSize, nonceLen, gasLen, accessListLen, b
|
|||||||
payloadSize++
|
payloadSize++
|
||||||
payloadSize += rlp.Uint256LenExcludingHead(stx.MaxFeePerBlobGas)
|
payloadSize += rlp.Uint256LenExcludingHead(stx.MaxFeePerBlobGas)
|
||||||
// size of BlobVersionedHashes
|
// size of BlobVersionedHashes
|
||||||
payloadSize++
|
|
||||||
blobHashesLen = blobVersionedHashesSize(stx.BlobVersionedHashes)
|
blobHashesLen = blobVersionedHashesSize(stx.BlobVersionedHashes)
|
||||||
if blobHashesLen >= 56 {
|
payloadSize += rlp2.ListPrefixLen(blobHashesLen) + blobHashesLen
|
||||||
payloadSize += libcommon.BitLenToByteLen(bits.Len(uint(blobHashesLen)))
|
|
||||||
}
|
|
||||||
payloadSize += blobHashesLen
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -202,12 +198,8 @@ func (stx BlobTx) encodePayload(w io.Writer, b []byte, payloadSize, nonceLen, ga
|
|||||||
|
|
||||||
func (stx BlobTx) EncodeRLP(w io.Writer) error {
|
func (stx BlobTx) EncodeRLP(w io.Writer) error {
|
||||||
payloadSize, nonceLen, gasLen, accessListLen, blobHashesLen := stx.payloadSize()
|
payloadSize, nonceLen, gasLen, accessListLen, blobHashesLen := stx.payloadSize()
|
||||||
envelopeSize := payloadSize
|
|
||||||
if payloadSize >= 56 {
|
|
||||||
envelopeSize += libcommon.BitLenToByteLen(bits.Len(uint(payloadSize)))
|
|
||||||
}
|
|
||||||
// size of struct prefix and TxType
|
// size of struct prefix and TxType
|
||||||
envelopeSize += 2
|
envelopeSize := 1 + rlp2.ListPrefixLen(payloadSize) + payloadSize
|
||||||
var b [33]byte
|
var b [33]byte
|
||||||
// envelope
|
// envelope
|
||||||
if err := rlp.EncodeStringSizePrefix(envelopeSize, w, b[:]); err != nil {
|
if err := rlp.EncodeStringSizePrefix(envelopeSize, w, b[:]); err != nil {
|
||||||
|
@ -24,7 +24,6 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"math/big"
|
"math/big"
|
||||||
"math/bits"
|
|
||||||
"reflect"
|
"reflect"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
|
|
||||||
@ -128,25 +127,11 @@ func (h *Header) EncodingSize() int {
|
|||||||
encodingSize++
|
encodingSize++
|
||||||
encodingSize += rlp.IntLenExcludingHead(h.Time)
|
encodingSize += rlp.IntLenExcludingHead(h.Time)
|
||||||
// size of Extra
|
// size of Extra
|
||||||
encodingSize++
|
encodingSize += rlp2.StringLen(h.Extra)
|
||||||
switch len(h.Extra) {
|
|
||||||
case 0:
|
|
||||||
case 1:
|
|
||||||
if h.Extra[0] >= 128 {
|
|
||||||
encodingSize++
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
if len(h.Extra) >= 56 {
|
|
||||||
encodingSize += libcommon.BitLenToByteLen(bits.Len(uint(len(h.Extra))))
|
|
||||||
}
|
|
||||||
encodingSize += len(h.Extra)
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(h.AuRaSeal) != 0 {
|
if len(h.AuRaSeal) != 0 {
|
||||||
encodingSize += 1 + rlp.IntLenExcludingHead(h.AuRaStep) + 1 + len(h.AuRaSeal)
|
encodingSize += 1 + rlp.IntLenExcludingHead(h.AuRaStep)
|
||||||
if len(h.AuRaSeal) >= 56 {
|
encodingSize += rlp2.ListPrefixLen(len(h.AuRaSeal)) + len(h.AuRaSeal)
|
||||||
encodingSize += libcommon.BitLenToByteLen(bits.Len(uint(len(h.AuRaSeal))))
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
encodingSize += 33 /* MixDigest */ + 9 /* BlockNonce */
|
encodingSize += 33 /* MixDigest */ + 9 /* BlockNonce */
|
||||||
}
|
}
|
||||||
@ -175,26 +160,12 @@ func (h *Header) EncodingSize() int {
|
|||||||
|
|
||||||
if h.Verkle {
|
if h.Verkle {
|
||||||
// Encoding of Verkle Proof
|
// Encoding of Verkle Proof
|
||||||
encodingSize++
|
encodingSize += rlp2.StringLen(h.VerkleProof)
|
||||||
switch len(h.VerkleProof) {
|
|
||||||
case 0:
|
|
||||||
case 1:
|
|
||||||
if h.VerkleProof[0] >= 128 {
|
|
||||||
encodingSize++
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
if len(h.VerkleProof) >= 56 {
|
|
||||||
encodingSize += libcommon.BitLenToByteLen(bits.Len(uint(len(h.VerkleProof))))
|
|
||||||
}
|
|
||||||
encodingSize += len(h.VerkleProof)
|
|
||||||
}
|
|
||||||
encodingSize++
|
|
||||||
|
|
||||||
var tmpBuffer bytes.Buffer
|
var tmpBuffer bytes.Buffer
|
||||||
if err := rlp.Encode(&tmpBuffer, h.VerkleKeyVals); err != nil {
|
if err := rlp.Encode(&tmpBuffer, h.VerkleKeyVals); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
encodingSize += tmpBuffer.Len()
|
encodingSize += rlp2.ListPrefixLen(tmpBuffer.Len()) + tmpBuffer.Len()
|
||||||
}
|
}
|
||||||
|
|
||||||
return encodingSize
|
return encodingSize
|
||||||
@ -698,45 +669,25 @@ func (rb RawBody) EncodingSize() int {
|
|||||||
|
|
||||||
func (rb RawBody) payloadSize() (payloadSize, txsLen, unclesLen, withdrawalsLen int) {
|
func (rb RawBody) payloadSize() (payloadSize, txsLen, unclesLen, withdrawalsLen int) {
|
||||||
// size of Transactions
|
// size of Transactions
|
||||||
payloadSize++
|
|
||||||
for _, tx := range rb.Transactions {
|
for _, tx := range rb.Transactions {
|
||||||
txsLen += len(tx)
|
txsLen += len(tx)
|
||||||
}
|
}
|
||||||
if txsLen >= 56 {
|
payloadSize += rlp2.ListPrefixLen(txsLen) + txsLen
|
||||||
payloadSize += libcommon.BitLenToByteLen(bits.Len(uint(txsLen)))
|
|
||||||
}
|
|
||||||
payloadSize += txsLen
|
|
||||||
|
|
||||||
// size of Uncles
|
// size of Uncles
|
||||||
payloadSize++
|
|
||||||
for _, uncle := range rb.Uncles {
|
for _, uncle := range rb.Uncles {
|
||||||
unclesLen++
|
|
||||||
uncleLen := uncle.EncodingSize()
|
uncleLen := uncle.EncodingSize()
|
||||||
if uncleLen >= 56 {
|
unclesLen += rlp2.ListPrefixLen(uncleLen) + uncleLen
|
||||||
unclesLen += libcommon.BitLenToByteLen(bits.Len(uint(uncleLen)))
|
|
||||||
}
|
|
||||||
unclesLen += uncleLen
|
|
||||||
}
|
}
|
||||||
if unclesLen >= 56 {
|
payloadSize += rlp2.ListPrefixLen(unclesLen) + unclesLen
|
||||||
payloadSize += libcommon.BitLenToByteLen(bits.Len(uint(unclesLen)))
|
|
||||||
}
|
|
||||||
payloadSize += unclesLen
|
|
||||||
|
|
||||||
// size of Withdrawals
|
// size of Withdrawals
|
||||||
if rb.Withdrawals != nil {
|
if rb.Withdrawals != nil {
|
||||||
payloadSize++
|
|
||||||
for _, withdrawal := range rb.Withdrawals {
|
for _, withdrawal := range rb.Withdrawals {
|
||||||
withdrawalsLen++
|
|
||||||
withdrawalLen := withdrawal.EncodingSize()
|
withdrawalLen := withdrawal.EncodingSize()
|
||||||
if withdrawalLen >= 56 {
|
withdrawalsLen += rlp2.ListPrefixLen(withdrawalLen) + withdrawalLen
|
||||||
withdrawalLen += libcommon.BitLenToByteLen(bits.Len(uint(withdrawalLen)))
|
|
||||||
}
|
|
||||||
withdrawalsLen += withdrawalLen
|
|
||||||
}
|
}
|
||||||
if withdrawalsLen >= 56 {
|
payloadSize += rlp2.ListPrefixLen(withdrawalsLen) + withdrawalsLen
|
||||||
payloadSize += libcommon.BitLenToByteLen(bits.Len(uint(withdrawalsLen)))
|
|
||||||
}
|
|
||||||
payloadSize += withdrawalsLen
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return payloadSize, txsLen, unclesLen, withdrawalsLen
|
return payloadSize, txsLen, unclesLen, withdrawalsLen
|
||||||
@ -853,9 +804,6 @@ func (rb *RawBody) DecodeRLP(s *rlp.Stream) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (bfs BodyForStorage) payloadSize() (payloadSize, unclesLen, withdrawalsLen int) {
|
func (bfs BodyForStorage) payloadSize() (payloadSize, unclesLen, withdrawalsLen int) {
|
||||||
|
|
||||||
payloadSize++
|
|
||||||
|
|
||||||
baseTxIdLen := 1 + rlp.IntLenExcludingHead(bfs.BaseTxId)
|
baseTxIdLen := 1 + rlp.IntLenExcludingHead(bfs.BaseTxId)
|
||||||
txAmountLen := 1 + rlp.IntLenExcludingHead(uint64(bfs.TxAmount))
|
txAmountLen := 1 + rlp.IntLenExcludingHead(uint64(bfs.TxAmount))
|
||||||
|
|
||||||
@ -864,33 +812,18 @@ func (bfs BodyForStorage) payloadSize() (payloadSize, unclesLen, withdrawalsLen
|
|||||||
|
|
||||||
// size of Uncles
|
// size of Uncles
|
||||||
for _, uncle := range bfs.Uncles {
|
for _, uncle := range bfs.Uncles {
|
||||||
unclesLen++
|
|
||||||
uncleLen := uncle.EncodingSize()
|
uncleLen := uncle.EncodingSize()
|
||||||
if uncleLen >= 56 {
|
unclesLen += rlp2.ListPrefixLen(uncleLen) + uncleLen
|
||||||
unclesLen += libcommon.BitLenToByteLen(bits.Len(uint(uncleLen)))
|
|
||||||
}
|
|
||||||
unclesLen += uncleLen
|
|
||||||
}
|
}
|
||||||
if unclesLen >= 56 {
|
payloadSize += rlp2.ListPrefixLen(unclesLen) + unclesLen
|
||||||
payloadSize += libcommon.BitLenToByteLen(bits.Len(uint(unclesLen)))
|
|
||||||
}
|
|
||||||
payloadSize += unclesLen
|
|
||||||
|
|
||||||
// size of Withdrawals
|
// size of Withdrawals
|
||||||
if bfs.Withdrawals != nil {
|
if bfs.Withdrawals != nil {
|
||||||
payloadSize++
|
|
||||||
for _, withdrawal := range bfs.Withdrawals {
|
for _, withdrawal := range bfs.Withdrawals {
|
||||||
withdrawalsLen++
|
|
||||||
withdrawalLen := withdrawal.EncodingSize()
|
withdrawalLen := withdrawal.EncodingSize()
|
||||||
if withdrawalLen >= 56 {
|
withdrawalsLen += rlp2.ListPrefixLen(withdrawalLen) + withdrawalLen
|
||||||
withdrawalLen += libcommon.BitLenToByteLen(bits.Len(uint(withdrawalLen)))
|
|
||||||
}
|
|
||||||
withdrawalsLen += withdrawalLen
|
|
||||||
}
|
}
|
||||||
if withdrawalsLen >= 56 {
|
payloadSize += rlp2.ListPrefixLen(withdrawalsLen) + withdrawalsLen
|
||||||
payloadSize += libcommon.BitLenToByteLen(bits.Len(uint(withdrawalsLen)))
|
|
||||||
}
|
|
||||||
payloadSize += withdrawalsLen
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return payloadSize, unclesLen, withdrawalsLen
|
return payloadSize, unclesLen, withdrawalsLen
|
||||||
@ -1012,50 +945,26 @@ func (bb Body) EncodingSize() int {
|
|||||||
|
|
||||||
func (bb Body) payloadSize() (payloadSize int, txsLen, unclesLen, withdrawalsLen int) {
|
func (bb Body) payloadSize() (payloadSize int, txsLen, unclesLen, withdrawalsLen int) {
|
||||||
// size of Transactions
|
// size of Transactions
|
||||||
payloadSize++
|
|
||||||
for _, tx := range bb.Transactions {
|
for _, tx := range bb.Transactions {
|
||||||
txsLen++
|
|
||||||
txLen := tx.EncodingSize()
|
txLen := tx.EncodingSize()
|
||||||
if txLen >= 56 {
|
txsLen += rlp2.ListPrefixLen(txLen) + txLen
|
||||||
txsLen += libcommon.BitLenToByteLen(bits.Len(uint(txLen)))
|
|
||||||
}
|
|
||||||
txsLen += txLen
|
|
||||||
}
|
}
|
||||||
if txsLen >= 56 {
|
payloadSize += rlp2.ListPrefixLen(txsLen) + txsLen
|
||||||
payloadSize += libcommon.BitLenToByteLen(bits.Len(uint(txsLen)))
|
|
||||||
}
|
|
||||||
payloadSize += txsLen
|
|
||||||
|
|
||||||
// size of Uncles
|
// size of Uncles
|
||||||
payloadSize++
|
|
||||||
for _, uncle := range bb.Uncles {
|
for _, uncle := range bb.Uncles {
|
||||||
unclesLen++
|
|
||||||
uncleLen := uncle.EncodingSize()
|
uncleLen := uncle.EncodingSize()
|
||||||
if uncleLen >= 56 {
|
unclesLen += rlp2.ListPrefixLen(uncleLen) + uncleLen
|
||||||
unclesLen += libcommon.BitLenToByteLen(bits.Len(uint(uncleLen)))
|
|
||||||
}
|
|
||||||
unclesLen += uncleLen
|
|
||||||
}
|
}
|
||||||
if unclesLen >= 56 {
|
payloadSize += rlp2.ListPrefixLen(unclesLen) + unclesLen
|
||||||
payloadSize += libcommon.BitLenToByteLen(bits.Len(uint(unclesLen)))
|
|
||||||
}
|
|
||||||
payloadSize += unclesLen
|
|
||||||
|
|
||||||
// size of Withdrawals
|
// size of Withdrawals
|
||||||
if bb.Withdrawals != nil {
|
if bb.Withdrawals != nil {
|
||||||
payloadSize++
|
|
||||||
for _, withdrawal := range bb.Withdrawals {
|
for _, withdrawal := range bb.Withdrawals {
|
||||||
withdrawalsLen++
|
|
||||||
withdrawalLen := withdrawal.EncodingSize()
|
withdrawalLen := withdrawal.EncodingSize()
|
||||||
if withdrawalLen >= 56 {
|
withdrawalsLen += rlp2.ListPrefixLen(withdrawalLen) + withdrawalLen
|
||||||
withdrawalLen += libcommon.BitLenToByteLen(bits.Len(uint(withdrawalLen)))
|
|
||||||
}
|
|
||||||
withdrawalsLen += withdrawalLen
|
|
||||||
}
|
}
|
||||||
if withdrawalsLen >= 56 {
|
payloadSize += rlp2.ListPrefixLen(withdrawalsLen) + withdrawalsLen
|
||||||
payloadSize += libcommon.BitLenToByteLen(bits.Len(uint(withdrawalsLen)))
|
|
||||||
}
|
|
||||||
payloadSize += withdrawalsLen
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return payloadSize, txsLen, unclesLen, withdrawalsLen
|
return payloadSize, txsLen, unclesLen, withdrawalsLen
|
||||||
@ -1359,58 +1268,30 @@ func (bb *Block) DecodeRLP(s *rlp.Stream) error {
|
|||||||
|
|
||||||
func (bb Block) payloadSize() (payloadSize int, txsLen, unclesLen, withdrawalsLen int) {
|
func (bb Block) payloadSize() (payloadSize int, txsLen, unclesLen, withdrawalsLen int) {
|
||||||
// size of Header
|
// size of Header
|
||||||
payloadSize++
|
|
||||||
headerLen := bb.header.EncodingSize()
|
headerLen := bb.header.EncodingSize()
|
||||||
if headerLen >= 56 {
|
payloadSize += rlp2.ListPrefixLen(headerLen) + headerLen
|
||||||
payloadSize += libcommon.BitLenToByteLen(bits.Len(uint(headerLen)))
|
|
||||||
}
|
|
||||||
payloadSize += headerLen
|
|
||||||
|
|
||||||
// size of Transactions
|
// size of Transactions
|
||||||
payloadSize++
|
|
||||||
for _, tx := range bb.transactions {
|
for _, tx := range bb.transactions {
|
||||||
txsLen++
|
|
||||||
txLen := tx.EncodingSize()
|
txLen := tx.EncodingSize()
|
||||||
if txLen >= 56 {
|
txsLen += rlp2.ListPrefixLen(txLen) + txLen
|
||||||
txsLen += libcommon.BitLenToByteLen(bits.Len(uint(txLen)))
|
|
||||||
}
|
|
||||||
txsLen += txLen
|
|
||||||
}
|
}
|
||||||
if txsLen >= 56 {
|
payloadSize += rlp2.ListPrefixLen(txsLen) + txsLen
|
||||||
payloadSize += libcommon.BitLenToByteLen(bits.Len(uint(txsLen)))
|
|
||||||
}
|
|
||||||
payloadSize += txsLen
|
|
||||||
|
|
||||||
// size of Uncles
|
// size of Uncles
|
||||||
payloadSize++
|
|
||||||
for _, uncle := range bb.uncles {
|
for _, uncle := range bb.uncles {
|
||||||
unclesLen++
|
|
||||||
uncleLen := uncle.EncodingSize()
|
uncleLen := uncle.EncodingSize()
|
||||||
if uncleLen >= 56 {
|
unclesLen += rlp2.ListPrefixLen(uncleLen) + uncleLen
|
||||||
unclesLen += libcommon.BitLenToByteLen(bits.Len(uint(uncleLen)))
|
|
||||||
}
|
|
||||||
unclesLen += uncleLen
|
|
||||||
}
|
}
|
||||||
if unclesLen >= 56 {
|
payloadSize += rlp2.ListPrefixLen(unclesLen) + unclesLen
|
||||||
payloadSize += libcommon.BitLenToByteLen(bits.Len(uint(unclesLen)))
|
|
||||||
}
|
|
||||||
payloadSize += unclesLen
|
|
||||||
|
|
||||||
// size of Withdrawals
|
// size of Withdrawals
|
||||||
if bb.withdrawals != nil {
|
if bb.withdrawals != nil {
|
||||||
payloadSize++
|
|
||||||
for _, withdrawal := range bb.withdrawals {
|
for _, withdrawal := range bb.withdrawals {
|
||||||
withdrawalsLen++
|
|
||||||
withdrawalLen := withdrawal.EncodingSize()
|
withdrawalLen := withdrawal.EncodingSize()
|
||||||
if withdrawalLen >= 56 {
|
withdrawalsLen += rlp2.ListPrefixLen(withdrawalLen) + withdrawalLen
|
||||||
withdrawalLen += libcommon.BitLenToByteLen(bits.Len(uint(withdrawalLen)))
|
|
||||||
}
|
|
||||||
withdrawalsLen += withdrawalLen
|
|
||||||
}
|
}
|
||||||
if withdrawalsLen >= 56 {
|
payloadSize += rlp2.ListPrefixLen(withdrawalsLen) + withdrawalsLen
|
||||||
payloadSize += libcommon.BitLenToByteLen(bits.Len(uint(withdrawalsLen)))
|
|
||||||
}
|
|
||||||
payloadSize += withdrawalsLen
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return payloadSize, txsLen, unclesLen, withdrawalsLen
|
return payloadSize, txsLen, unclesLen, withdrawalsLen
|
||||||
|
@ -21,12 +21,12 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"math/big"
|
"math/big"
|
||||||
"math/bits"
|
|
||||||
|
|
||||||
"github.com/holiman/uint256"
|
"github.com/holiman/uint256"
|
||||||
|
|
||||||
"github.com/ledgerwatch/erigon-lib/chain"
|
"github.com/ledgerwatch/erigon-lib/chain"
|
||||||
libcommon "github.com/ledgerwatch/erigon-lib/common"
|
libcommon "github.com/ledgerwatch/erigon-lib/common"
|
||||||
|
rlp2 "github.com/ledgerwatch/erigon-lib/rlp"
|
||||||
types2 "github.com/ledgerwatch/erigon-lib/types"
|
types2 "github.com/ledgerwatch/erigon-lib/types"
|
||||||
|
|
||||||
"github.com/ledgerwatch/erigon/common"
|
"github.com/ledgerwatch/erigon/common"
|
||||||
@ -117,13 +117,8 @@ func (tx DynamicFeeTransaction) GetAccessList() types2.AccessList {
|
|||||||
|
|
||||||
func (tx DynamicFeeTransaction) EncodingSize() int {
|
func (tx DynamicFeeTransaction) EncodingSize() int {
|
||||||
payloadSize, _, _, _ := tx.payloadSize()
|
payloadSize, _, _, _ := tx.payloadSize()
|
||||||
envelopeSize := payloadSize
|
|
||||||
// Add envelope size and type size
|
// Add envelope size and type size
|
||||||
if payloadSize >= 56 {
|
return 1 + rlp2.ListPrefixLen(payloadSize) + payloadSize
|
||||||
envelopeSize += libcommon.BitLenToByteLen(bits.Len(uint(payloadSize)))
|
|
||||||
}
|
|
||||||
envelopeSize += 2
|
|
||||||
return envelopeSize
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tx DynamicFeeTransaction) payloadSize() (payloadSize int, nonceLen, gasLen, accessListLen int) {
|
func (tx DynamicFeeTransaction) payloadSize() (payloadSize int, nonceLen, gasLen, accessListLen int) {
|
||||||
@ -153,26 +148,10 @@ func (tx DynamicFeeTransaction) payloadSize() (payloadSize int, nonceLen, gasLen
|
|||||||
payloadSize++
|
payloadSize++
|
||||||
payloadSize += rlp.Uint256LenExcludingHead(tx.Value)
|
payloadSize += rlp.Uint256LenExcludingHead(tx.Value)
|
||||||
// size of Data
|
// size of Data
|
||||||
payloadSize++
|
payloadSize += rlp2.StringLen(tx.Data)
|
||||||
switch len(tx.Data) {
|
|
||||||
case 0:
|
|
||||||
case 1:
|
|
||||||
if tx.Data[0] >= 128 {
|
|
||||||
payloadSize++
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
if len(tx.Data) >= 56 {
|
|
||||||
payloadSize += libcommon.BitLenToByteLen(bits.Len(uint(len(tx.Data))))
|
|
||||||
}
|
|
||||||
payloadSize += len(tx.Data)
|
|
||||||
}
|
|
||||||
// size of AccessList
|
// size of AccessList
|
||||||
payloadSize++
|
|
||||||
accessListLen = accessListSize(tx.AccessList)
|
accessListLen = accessListSize(tx.AccessList)
|
||||||
if accessListLen >= 56 {
|
payloadSize += rlp2.ListPrefixLen(accessListLen) + accessListLen
|
||||||
payloadSize += libcommon.BitLenToByteLen(bits.Len(uint(accessListLen)))
|
|
||||||
}
|
|
||||||
payloadSize += accessListLen
|
|
||||||
// size of V
|
// size of V
|
||||||
payloadSize++
|
payloadSize++
|
||||||
payloadSize += rlp.Uint256LenExcludingHead(&tx.V)
|
payloadSize += rlp.Uint256LenExcludingHead(&tx.V)
|
||||||
@ -296,12 +275,8 @@ func (tx DynamicFeeTransaction) encodePayload(w io.Writer, b []byte, payloadSize
|
|||||||
|
|
||||||
func (tx DynamicFeeTransaction) EncodeRLP(w io.Writer) error {
|
func (tx DynamicFeeTransaction) EncodeRLP(w io.Writer) error {
|
||||||
payloadSize, nonceLen, gasLen, accessListLen := tx.payloadSize()
|
payloadSize, nonceLen, gasLen, accessListLen := tx.payloadSize()
|
||||||
envelopeSize := payloadSize
|
|
||||||
if payloadSize >= 56 {
|
|
||||||
envelopeSize += libcommon.BitLenToByteLen(bits.Len(uint(payloadSize)))
|
|
||||||
}
|
|
||||||
// size of struct prefix and TxType
|
// size of struct prefix and TxType
|
||||||
envelopeSize += 2
|
envelopeSize := 1 + rlp2.ListPrefixLen(payloadSize) + payloadSize
|
||||||
var b [33]byte
|
var b [33]byte
|
||||||
// envelope
|
// envelope
|
||||||
if err := rlp.EncodeStringSizePrefix(envelopeSize, w, b[:]); err != nil {
|
if err := rlp.EncodeStringSizePrefix(envelopeSize, w, b[:]); err != nil {
|
||||||
|
@ -21,11 +21,11 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"math/big"
|
"math/big"
|
||||||
"math/bits"
|
|
||||||
|
|
||||||
"github.com/holiman/uint256"
|
"github.com/holiman/uint256"
|
||||||
"github.com/ledgerwatch/erigon-lib/chain"
|
"github.com/ledgerwatch/erigon-lib/chain"
|
||||||
libcommon "github.com/ledgerwatch/erigon-lib/common"
|
libcommon "github.com/ledgerwatch/erigon-lib/common"
|
||||||
|
rlp2 "github.com/ledgerwatch/erigon-lib/rlp"
|
||||||
types2 "github.com/ledgerwatch/erigon-lib/types"
|
types2 "github.com/ledgerwatch/erigon-lib/types"
|
||||||
|
|
||||||
"github.com/ledgerwatch/erigon/common"
|
"github.com/ledgerwatch/erigon/common"
|
||||||
@ -215,19 +215,7 @@ func (tx LegacyTx) payloadSize() (payloadSize int, nonceLen, gasLen int) {
|
|||||||
payloadSize++
|
payloadSize++
|
||||||
payloadSize += rlp.Uint256LenExcludingHead(tx.Value)
|
payloadSize += rlp.Uint256LenExcludingHead(tx.Value)
|
||||||
// size of Data
|
// size of Data
|
||||||
payloadSize++
|
payloadSize += rlp2.StringLen(tx.Data)
|
||||||
switch len(tx.Data) {
|
|
||||||
case 0:
|
|
||||||
case 1:
|
|
||||||
if tx.Data[0] >= 128 {
|
|
||||||
payloadSize++
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
if len(tx.Data) >= 56 {
|
|
||||||
payloadSize += libcommon.BitLenToByteLen(bits.Len(uint(len(tx.Data))))
|
|
||||||
}
|
|
||||||
payloadSize += len(tx.Data)
|
|
||||||
}
|
|
||||||
// size of V
|
// size of V
|
||||||
payloadSize++
|
payloadSize++
|
||||||
payloadSize += rlp.Uint256LenExcludingHead(&tx.V)
|
payloadSize += rlp.Uint256LenExcludingHead(&tx.V)
|
||||||
|
@ -20,11 +20,11 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"math/big"
|
"math/big"
|
||||||
"math/bits"
|
|
||||||
|
|
||||||
libcommon "github.com/ledgerwatch/erigon-lib/common"
|
libcommon "github.com/ledgerwatch/erigon-lib/common"
|
||||||
"github.com/ledgerwatch/erigon-lib/direct"
|
"github.com/ledgerwatch/erigon-lib/direct"
|
||||||
proto_sentry "github.com/ledgerwatch/erigon-lib/gointerfaces/sentry"
|
proto_sentry "github.com/ledgerwatch/erigon-lib/gointerfaces/sentry"
|
||||||
|
rlp2 "github.com/ledgerwatch/erigon-lib/rlp"
|
||||||
|
|
||||||
"github.com/ledgerwatch/erigon/core/forkid"
|
"github.com/ledgerwatch/erigon/core/forkid"
|
||||||
"github.com/ledgerwatch/erigon/core/types"
|
"github.com/ledgerwatch/erigon/core/types"
|
||||||
@ -250,12 +250,8 @@ type NewBlockPacket struct {
|
|||||||
func (nbp NewBlockPacket) EncodeRLP(w io.Writer) error {
|
func (nbp NewBlockPacket) EncodeRLP(w io.Writer) error {
|
||||||
encodingSize := 0
|
encodingSize := 0
|
||||||
// size of Block
|
// size of Block
|
||||||
encodingSize++
|
|
||||||
blockLen := nbp.Block.EncodingSize()
|
blockLen := nbp.Block.EncodingSize()
|
||||||
if blockLen >= 56 {
|
encodingSize += rlp2.ListPrefixLen(blockLen) + blockLen
|
||||||
encodingSize += libcommon.BitLenToByteLen(bits.Len(uint(blockLen)))
|
|
||||||
}
|
|
||||||
encodingSize += blockLen
|
|
||||||
// size of TD
|
// size of TD
|
||||||
encodingSize++
|
encodingSize++
|
||||||
var tdBitLen, tdLen int
|
var tdBitLen, tdLen int
|
||||||
|
Loading…
Reference in New Issue
Block a user