From a5886732b9f0f51fd5662dced3bc46c8bd7aa58e Mon Sep 17 00:00:00 2001 From: Andrew Ashikhmin <34320705+yperbasis@users.noreply.github.com> Date: Wed, 14 Jun 2023 14:48:16 +0200 Subject: [PATCH] Use BitLenToByteLen from erigon-lib (#7735) --- core/types/access_list_tx.go | 17 +++++----- core/types/accounts/account.go | 29 +++++++++-------- core/types/blob_tx_wrapper.go | 16 ++++----- core/types/block.go | 59 ++++++++++++++++------------------ core/types/dynamic_fee_tx.go | 8 ++--- core/types/legacy_tx.go | 2 +- core/types/signed_blob_tx.go | 10 +++--- core/vm/gas_table.go | 5 +-- crypto/ecies/ecies.go | 4 ++- eth/protocols/eth/protocol.go | 16 ++++----- ethdb/kv_util.go | 3 +- go.mod | 2 +- go.sum | 6 ++-- rlp/encode.go | 12 ++++--- 14 files changed, 96 insertions(+), 93 deletions(-) diff --git a/core/types/access_list_tx.go b/core/types/access_list_tx.go index 0e7039cd2..562205d33 100644 --- a/core/types/access_list_tx.go +++ b/core/types/access_list_tx.go @@ -25,6 +25,7 @@ import ( "math/bits" "github.com/holiman/uint256" + "github.com/ledgerwatch/erigon-lib/chain" libcommon "github.com/ledgerwatch/erigon-lib/common" types2 "github.com/ledgerwatch/erigon-lib/types" @@ -95,7 +96,7 @@ func (tx AccessListTx) EncodingSize() int { envelopeSize := payloadSize // Add envelope size and type size if payloadSize >= 56 { - envelopeSize += (bits.Len(uint(payloadSize)) + 7) / 8 + envelopeSize += libcommon.BitLenToByteLen(bits.Len(uint(payloadSize))) } envelopeSize += 2 return envelopeSize @@ -135,7 +136,7 @@ func (tx AccessListTx) payloadSize() (payloadSize int, nonceLen, gasLen, accessL } default: if len(tx.Data) >= 56 { - payloadSize += (bits.Len(uint(len(tx.Data))) + 7) / 8 + payloadSize += libcommon.BitLenToByteLen(bits.Len(uint(len(tx.Data)))) } payloadSize += len(tx.Data) } @@ -143,7 +144,7 @@ func (tx AccessListTx) payloadSize() (payloadSize int, nonceLen, gasLen, accessL payloadSize++ accessListLen = accessListSize(tx.AccessList) if accessListLen >= 56 { - payloadSize += (bits.Len(uint(accessListLen)) + 7) / 8 + payloadSize += libcommon.BitLenToByteLen(bits.Len(uint(accessListLen))) } payloadSize += accessListLen // size of V @@ -167,12 +168,12 @@ func accessListSize(al types2.AccessList) int { // Each storage key takes 33 bytes storageLen := 33 * len(tuple.StorageKeys) if storageLen >= 56 { - tupleLen += (bits.Len(uint(storageLen)) + 7) / 8 // BE encoding of the length of the storage keys + tupleLen += libcommon.BitLenToByteLen(bits.Len(uint(storageLen))) // BE encoding of the length of the storage keys } tupleLen += storageLen accessListLen++ if tupleLen >= 56 { - accessListLen += (bits.Len(uint(tupleLen)) + 7) / 8 // BE encoding of the length of the storage keys + accessListLen += libcommon.BitLenToByteLen(bits.Len(uint(tupleLen))) // BE encoding of the length of the storage keys } accessListLen += tupleLen } @@ -186,7 +187,7 @@ func encodeAccessList(al types2.AccessList, w io.Writer, b []byte) error { // Each storage key takes 33 bytes storageLen := 33 * len(tuple.StorageKeys) if storageLen >= 56 { - tupleLen += (bits.Len(uint(storageLen)) + 7) / 8 // BE encoding of the length of the storage keys + 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 { @@ -217,7 +218,7 @@ func encodeAccessList(al types2.AccessList, w io.Writer, b []byte) error { func EncodeStructSizePrefix(size int, w io.Writer, b []byte) error { if size >= 56 { - beSize := (bits.Len(uint(size)) + 7) / 8 + 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 { @@ -321,7 +322,7 @@ func (tx AccessListTx) EncodeRLP(w io.Writer) error { payloadSize, nonceLen, gasLen, accessListLen := tx.payloadSize() envelopeSize := payloadSize if payloadSize >= 56 { - envelopeSize += (bits.Len(uint(payloadSize)) + 7) / 8 + envelopeSize += libcommon.BitLenToByteLen(bits.Len(uint(payloadSize))) } // size of struct prefix and TxType envelopeSize += 2 diff --git a/core/types/accounts/account.go b/core/types/accounts/account.go index 8801c7277..1953249c6 100644 --- a/core/types/accounts/account.go +++ b/core/types/accounts/account.go @@ -62,7 +62,7 @@ func (a *Account) EncodingLengthForStorage() uint { } if a.Nonce > 0 { - structLength += uint((bits.Len64(a.Nonce)+7)/8) + 1 + structLength += uint(libcommon.BitLenToByteLen(bits.Len64(a.Nonce))) + 1 } if !a.IsEmptyCodeHash() { @@ -70,7 +70,7 @@ func (a *Account) EncodingLengthForStorage() uint { } if a.Incarnation > 0 { - structLength += uint((bits.Len64(a.Incarnation)+7)/8) + 1 + structLength += uint(libcommon.BitLenToByteLen(bits.Len64(a.Incarnation))) + 1 } return structLength @@ -94,7 +94,7 @@ func (a *Account) EncodingLengthForHashing() uint { return 1 + structLength } - lengthBytes := (bits.Len(structLength) + 7) / 8 + lengthBytes := libcommon.BitLenToByteLen(bits.Len(structLength)) return uint(1+lengthBytes) + structLength } @@ -104,7 +104,7 @@ func (a *Account) EncodeForStorage(buffer []byte) { var pos = 1 if a.Nonce > 0 { fieldSet = 1 - nonceBytes := (bits.Len64(a.Nonce) + 7) / 8 + nonceBytes := libcommon.BitLenToByteLen(bits.Len64(a.Nonce)) buffer[pos] = byte(nonceBytes) var nonce = a.Nonce for i := nonceBytes; i > 0; i-- { @@ -126,7 +126,7 @@ func (a *Account) EncodeForStorage(buffer []byte) { if a.Incarnation > 0 { fieldSet |= 4 - incarnationBytes := (bits.Len64(a.Incarnation) + 7) / 8 + incarnationBytes := libcommon.BitLenToByteLen(bits.Len64(a.Incarnation)) buffer[pos] = byte(incarnationBytes) var incarnation = a.Incarnation for i := incarnationBytes; i > 0; i-- { @@ -215,7 +215,7 @@ func (a *Account) EncodeForHashing(buffer []byte) { buffer[0] = byte(192 + structLength) pos = 1 } else { - lengthBytes := (bits.Len(structLength) + 7) / 8 + lengthBytes := libcommon.BitLenToByteLen(bits.Len(structLength)) buffer[0] = byte(247 + lengthBytes) for i := lengthBytes; i > 0; i-- { @@ -663,7 +663,7 @@ func SerialiseV3(a *Account) []byte { var l int l++ if a.Nonce > 0 { - l += (bits.Len64(a.Nonce) + 7) / 8 + l += libcommon.BitLenToByteLen(bits.Len64(a.Nonce)) } l++ if !a.Balance.IsZero() { @@ -675,7 +675,7 @@ func SerialiseV3(a *Account) []byte { } l++ if a.Incarnation > 0 { - l += (bits.Len64(a.Incarnation) + 7) / 8 + l += libcommon.BitLenToByteLen(bits.Len64(a.Incarnation)) } value := make([]byte, l) pos := 0 @@ -683,7 +683,7 @@ func SerialiseV3(a *Account) []byte { value[pos] = 0 pos++ } else { - nonceBytes := (bits.Len64(a.Nonce) + 7) / 8 + nonceBytes := libcommon.BitLenToByteLen(bits.Len64(a.Nonce)) value[pos] = byte(nonceBytes) var nonce = a.Nonce for i := nonceBytes; i > 0; i-- { @@ -714,7 +714,7 @@ func SerialiseV3(a *Account) []byte { if a.Incarnation == 0 { value[pos] = 0 } else { - incBytes := (bits.Len64(a.Incarnation) + 7) / 8 + incBytes := libcommon.BitLenToByteLen(bits.Len64(a.Incarnation)) value[pos] = byte(incBytes) var inc = a.Incarnation for i := incBytes; i > 0; i-- { @@ -728,7 +728,7 @@ func SerialiseV3(a *Account) []byte { func SerialiseV3Len(a *Account) (l int) { l++ if a.Nonce > 0 { - l += (bits.Len64(a.Nonce) + 7) / 8 + l += libcommon.BitLenToByteLen(bits.Len64(a.Nonce)) } l++ if !a.Balance.IsZero() { @@ -740,17 +740,18 @@ func SerialiseV3Len(a *Account) (l int) { } l++ if a.Incarnation > 0 { - l += (bits.Len64(a.Incarnation) + 7) / 8 + l += libcommon.BitLenToByteLen(bits.Len64(a.Incarnation)) } return l } + func SerialiseV3To(a *Account, value []byte) { pos := 0 if a.Nonce == 0 { value[pos] = 0 pos++ } else { - nonceBytes := (bits.Len64(a.Nonce) + 7) / 8 + nonceBytes := libcommon.BitLenToByteLen(bits.Len64(a.Nonce)) value[pos] = byte(nonceBytes) var nonce = a.Nonce for i := nonceBytes; i > 0; i-- { @@ -781,7 +782,7 @@ func SerialiseV3To(a *Account, value []byte) { if a.Incarnation == 0 { value[pos] = 0 } else { - incBytes := (bits.Len64(a.Incarnation) + 7) / 8 + incBytes := libcommon.BitLenToByteLen(bits.Len64(a.Incarnation)) value[pos] = byte(incBytes) var inc = a.Incarnation for i := incBytes; i > 0; i-- { diff --git a/core/types/blob_tx_wrapper.go b/core/types/blob_tx_wrapper.go index 6e93061dd..974e96c30 100644 --- a/core/types/blob_tx_wrapper.go +++ b/core/types/blob_tx_wrapper.go @@ -40,9 +40,9 @@ type BlobTxWrapper struct { /* Blob methods */ func (b *Blob) payloadSize() int { - size := 1 // 0xb7 - size += (bits.Len(LEN_BLOB) + 7) / 8 // params.FieldElementsPerBlob * 32 = 131072 (length encoding size) - size += LEN_BLOB // byte_array it self + size := 1 // 0xb7 + size += libcommon.BitLenToByteLen(bits.Len(LEN_BLOB)) // params.FieldElementsPerBlob * 32 = 131072 (length encoding size) + size += LEN_BLOB // byte_array it self return size } @@ -57,7 +57,7 @@ func (li BlobKzgs) copy() BlobKzgs { func (li BlobKzgs) payloadSize() int { size := 49 * len(li) if size >= 56 { - size += (bits.Len(uint(size)) + 7) / 8 // BE encoding of the length of hashes + size += libcommon.BitLenToByteLen(bits.Len(uint(size))) // BE encoding of the length of hashes } return size } @@ -91,7 +91,7 @@ func (li KZGProofs) copy() KZGProofs { func (li KZGProofs) payloadSize() int { size := 49 * len(li) if size >= 56 { - size += (bits.Len(uint(size)) + 7) / 8 // BE encoding of the length of hashes + size += libcommon.BitLenToByteLen(bits.Len(uint(size))) // BE encoding of the length of hashes } return size } @@ -126,7 +126,7 @@ func (blobs Blobs) payloadSize() int { total := 0 if len(blobs) > 0 { total = len(blobs) * blobs[0].payloadSize() - total += (bits.Len(uint(total)) + 7) / 8 + total += libcommon.BitLenToByteLen(bits.Len(uint(total))) } return total } @@ -298,7 +298,7 @@ func (txw BlobTxWrapper) EncodingSize() int { envelopeSize := payloadSize // Add envelope size and type size if payloadSize >= 56 { - envelopeSize += (bits.Len(uint(payloadSize)) + 7) / 8 + envelopeSize += libcommon.BitLenToByteLen(bits.Len(uint(payloadSize))) } envelopeSize += 2 return envelopeSize @@ -343,7 +343,7 @@ func (txw BlobTxWrapper) EncodeRLP(w io.Writer) error { payloadSize := txSize + commitmentsSize + proofsSize + blobsSize envelopeSize := payloadSize if payloadSize >= 56 { - envelopeSize += (bits.Len(uint(payloadSize)) + 7) / 8 + envelopeSize += libcommon.BitLenToByteLen(bits.Len(uint(payloadSize))) } // size of struct prefix and TxType envelopeSize += 2 diff --git a/core/types/block.go b/core/types/block.go index 9feccd681..d7050e616 100644 --- a/core/types/block.go +++ b/core/types/block.go @@ -107,10 +107,6 @@ type Header struct { VerkleKeyVals []verkle.KeyValuePair } -func bitsToBytes(bitLen int) (byteLen int) { - return (bitLen + 7) / 8 -} - func (h *Header) EncodingSize() int { encodingSize := 33 /* ParentHash */ + 33 /* UncleHash */ + 21 /* Coinbase */ + 33 /* Root */ + 33 /* TxHash */ + 33 /* ReceiptHash */ + 259 /* Bloom */ @@ -139,7 +135,7 @@ func (h *Header) EncodingSize() int { } default: if len(h.Extra) >= 56 { - encodingSize += bitsToBytes(bits.Len(uint(len(h.Extra)))) + encodingSize += libcommon.BitLenToByteLen(bits.Len(uint(len(h.Extra)))) } encodingSize += len(h.Extra) } @@ -147,7 +143,7 @@ func (h *Header) EncodingSize() int { if len(h.AuRaSeal) != 0 { encodingSize += 1 + rlp.IntLenExcludingHead(h.AuRaStep) + 1 + len(h.AuRaSeal) if len(h.AuRaSeal) >= 56 { - encodingSize += bitsToBytes(bits.Len(uint(len(h.AuRaSeal)))) + encodingSize += libcommon.BitLenToByteLen(bits.Len(uint(len(h.AuRaSeal)))) } } else { encodingSize += 33 /* MixDigest */ + 9 /* BlockNonce */ @@ -182,7 +178,7 @@ func (h *Header) EncodingSize() int { } default: if len(h.VerkleProof) >= 56 { - encodingSize += bitsToBytes(bits.Len(uint(len(h.VerkleProof)))) + encodingSize += libcommon.BitLenToByteLen(bits.Len(uint(len(h.VerkleProof)))) } encodingSize += len(h.VerkleProof) } @@ -537,9 +533,10 @@ var headerSize = common.StorageSize(reflect.TypeOf(Header{}).Size()) // Size returns the approximate memory used by all internal contents. It is used // to approximate and limit the memory consumption of various caches. func (h *Header) Size() common.StorageSize { - s := headerSize + common.StorageSize(len(h.Extra)+bitsToBytes(h.Difficulty.BitLen())+bitsToBytes(h.Number.BitLen())) + s := headerSize + s += common.StorageSize(len(h.Extra) + libcommon.BitLenToByteLen(h.Difficulty.BitLen()) + libcommon.BitLenToByteLen(h.Number.BitLen())) if h.BaseFee != nil { - s += common.StorageSize(bitsToBytes(h.BaseFee.BitLen())) + s += common.StorageSize(libcommon.BitLenToByteLen(h.BaseFee.BitLen())) } if h.WithdrawalsHash != nil { s += common.StorageSize(32) @@ -653,7 +650,7 @@ func (rb RawBody) payloadSize() (payloadSize, txsLen, unclesLen, withdrawalsLen txsLen += len(tx) } if txsLen >= 56 { - payloadSize += bitsToBytes(bits.Len(uint(txsLen))) + payloadSize += libcommon.BitLenToByteLen(bits.Len(uint(txsLen))) } payloadSize += txsLen @@ -663,12 +660,12 @@ func (rb RawBody) payloadSize() (payloadSize, txsLen, unclesLen, withdrawalsLen unclesLen++ uncleLen := uncle.EncodingSize() if uncleLen >= 56 { - unclesLen += bitsToBytes(bits.Len(uint(uncleLen))) + unclesLen += libcommon.BitLenToByteLen(bits.Len(uint(uncleLen))) } unclesLen += uncleLen } if unclesLen >= 56 { - payloadSize += bitsToBytes(bits.Len(uint(unclesLen))) + payloadSize += libcommon.BitLenToByteLen(bits.Len(uint(unclesLen))) } payloadSize += unclesLen @@ -679,12 +676,12 @@ func (rb RawBody) payloadSize() (payloadSize, txsLen, unclesLen, withdrawalsLen withdrawalsLen++ withdrawalLen := withdrawal.EncodingSize() if withdrawalLen >= 56 { - withdrawalLen += bitsToBytes(bits.Len(uint(withdrawalLen))) + withdrawalLen += libcommon.BitLenToByteLen(bits.Len(uint(withdrawalLen))) } withdrawalsLen += withdrawalLen } if withdrawalsLen >= 56 { - payloadSize += bitsToBytes(bits.Len(uint(withdrawalsLen))) + payloadSize += libcommon.BitLenToByteLen(bits.Len(uint(withdrawalsLen))) } payloadSize += withdrawalsLen } @@ -817,12 +814,12 @@ func (bfs BodyForStorage) payloadSize() (payloadSize, unclesLen, withdrawalsLen unclesLen++ uncleLen := uncle.EncodingSize() if uncleLen >= 56 { - unclesLen += bitsToBytes(bits.Len(uint(uncleLen))) + unclesLen += libcommon.BitLenToByteLen(bits.Len(uint(uncleLen))) } unclesLen += uncleLen } if unclesLen >= 56 { - payloadSize += bitsToBytes(bits.Len(uint(unclesLen))) + payloadSize += libcommon.BitLenToByteLen(bits.Len(uint(unclesLen))) } payloadSize += unclesLen @@ -833,12 +830,12 @@ func (bfs BodyForStorage) payloadSize() (payloadSize, unclesLen, withdrawalsLen withdrawalsLen++ withdrawalLen := withdrawal.EncodingSize() if withdrawalLen >= 56 { - withdrawalLen += bitsToBytes(bits.Len(uint(withdrawalLen))) + withdrawalLen += libcommon.BitLenToByteLen(bits.Len(uint(withdrawalLen))) } withdrawalsLen += withdrawalLen } if withdrawalsLen >= 56 { - payloadSize += bitsToBytes(bits.Len(uint(withdrawalsLen))) + payloadSize += libcommon.BitLenToByteLen(bits.Len(uint(withdrawalsLen))) } payloadSize += withdrawalsLen } @@ -975,12 +972,12 @@ func (bb Body) payloadSize() (payloadSize int, txsLen, unclesLen, withdrawalsLen txLen = t.EncodingSize() } if txLen >= 56 { - txsLen += bitsToBytes(bits.Len(uint(txLen))) + txsLen += libcommon.BitLenToByteLen(bits.Len(uint(txLen))) } txsLen += txLen } if txsLen >= 56 { - payloadSize += bitsToBytes(bits.Len(uint(txsLen))) + payloadSize += libcommon.BitLenToByteLen(bits.Len(uint(txsLen))) } payloadSize += txsLen @@ -990,12 +987,12 @@ func (bb Body) payloadSize() (payloadSize int, txsLen, unclesLen, withdrawalsLen unclesLen++ uncleLen := uncle.EncodingSize() if uncleLen >= 56 { - unclesLen += bitsToBytes(bits.Len(uint(uncleLen))) + unclesLen += libcommon.BitLenToByteLen(bits.Len(uint(uncleLen))) } unclesLen += uncleLen } if unclesLen >= 56 { - payloadSize += bitsToBytes(bits.Len(uint(unclesLen))) + payloadSize += libcommon.BitLenToByteLen(bits.Len(uint(unclesLen))) } payloadSize += unclesLen @@ -1006,12 +1003,12 @@ func (bb Body) payloadSize() (payloadSize int, txsLen, unclesLen, withdrawalsLen withdrawalsLen++ withdrawalLen := withdrawal.EncodingSize() if withdrawalLen >= 56 { - withdrawalLen += bitsToBytes(bits.Len(uint(withdrawalLen))) + withdrawalLen += libcommon.BitLenToByteLen(bits.Len(uint(withdrawalLen))) } withdrawalsLen += withdrawalLen } if withdrawalsLen >= 56 { - payloadSize += bitsToBytes(bits.Len(uint(withdrawalsLen))) + payloadSize += libcommon.BitLenToByteLen(bits.Len(uint(withdrawalsLen))) } payloadSize += withdrawalsLen } @@ -1325,7 +1322,7 @@ func (bb Block) payloadSize() (payloadSize int, txsLen, unclesLen, withdrawalsLe payloadSize++ headerLen := bb.header.EncodingSize() if headerLen >= 56 { - payloadSize += bitsToBytes(bits.Len(uint(headerLen))) + payloadSize += libcommon.BitLenToByteLen(bits.Len(uint(headerLen))) } payloadSize += headerLen @@ -1335,12 +1332,12 @@ func (bb Block) payloadSize() (payloadSize int, txsLen, unclesLen, withdrawalsLe txsLen++ txLen := tx.EncodingSize() if txLen >= 56 { - txsLen += bitsToBytes(bits.Len(uint(txLen))) + txsLen += libcommon.BitLenToByteLen(bits.Len(uint(txLen))) } txsLen += txLen } if txsLen >= 56 { - payloadSize += bitsToBytes(bits.Len(uint(txsLen))) + payloadSize += libcommon.BitLenToByteLen(bits.Len(uint(txsLen))) } payloadSize += txsLen @@ -1350,12 +1347,12 @@ func (bb Block) payloadSize() (payloadSize int, txsLen, unclesLen, withdrawalsLe unclesLen++ uncleLen := uncle.EncodingSize() if uncleLen >= 56 { - unclesLen += bitsToBytes(bits.Len(uint(uncleLen))) + unclesLen += libcommon.BitLenToByteLen(bits.Len(uint(uncleLen))) } unclesLen += uncleLen } if unclesLen >= 56 { - payloadSize += bitsToBytes(bits.Len(uint(unclesLen))) + payloadSize += libcommon.BitLenToByteLen(bits.Len(uint(unclesLen))) } payloadSize += unclesLen @@ -1366,12 +1363,12 @@ func (bb Block) payloadSize() (payloadSize int, txsLen, unclesLen, withdrawalsLe withdrawalsLen++ withdrawalLen := withdrawal.EncodingSize() if withdrawalLen >= 56 { - withdrawalLen += bitsToBytes(bits.Len(uint(withdrawalLen))) + withdrawalLen += libcommon.BitLenToByteLen(bits.Len(uint(withdrawalLen))) } withdrawalsLen += withdrawalLen } if withdrawalsLen >= 56 { - payloadSize += bitsToBytes(bits.Len(uint(withdrawalsLen))) + payloadSize += libcommon.BitLenToByteLen(bits.Len(uint(withdrawalsLen))) } payloadSize += withdrawalsLen } diff --git a/core/types/dynamic_fee_tx.go b/core/types/dynamic_fee_tx.go index 04c2b4cd3..0fa8b8655 100644 --- a/core/types/dynamic_fee_tx.go +++ b/core/types/dynamic_fee_tx.go @@ -120,7 +120,7 @@ func (tx DynamicFeeTransaction) EncodingSize() int { envelopeSize := payloadSize // Add envelope size and type size if payloadSize >= 56 { - envelopeSize += (bits.Len(uint(payloadSize)) + 7) / 8 + envelopeSize += libcommon.BitLenToByteLen(bits.Len(uint(payloadSize))) } envelopeSize += 2 return envelopeSize @@ -162,7 +162,7 @@ func (tx DynamicFeeTransaction) payloadSize() (payloadSize int, nonceLen, gasLen } default: if len(tx.Data) >= 56 { - payloadSize += (bits.Len(uint(len(tx.Data))) + 7) / 8 + payloadSize += libcommon.BitLenToByteLen(bits.Len(uint(len(tx.Data)))) } payloadSize += len(tx.Data) } @@ -170,7 +170,7 @@ func (tx DynamicFeeTransaction) payloadSize() (payloadSize int, nonceLen, gasLen payloadSize++ accessListLen = accessListSize(tx.AccessList) if accessListLen >= 56 { - payloadSize += (bits.Len(uint(accessListLen)) + 7) / 8 + payloadSize += libcommon.BitLenToByteLen(bits.Len(uint(accessListLen))) } payloadSize += accessListLen // size of V @@ -298,7 +298,7 @@ func (tx DynamicFeeTransaction) EncodeRLP(w io.Writer) error { payloadSize, nonceLen, gasLen, accessListLen := tx.payloadSize() envelopeSize := payloadSize if payloadSize >= 56 { - envelopeSize += (bits.Len(uint(payloadSize)) + 7) / 8 + envelopeSize += libcommon.BitLenToByteLen(bits.Len(uint(payloadSize))) } // size of struct prefix and TxType envelopeSize += 2 diff --git a/core/types/legacy_tx.go b/core/types/legacy_tx.go index d6b0af324..e7606416c 100644 --- a/core/types/legacy_tx.go +++ b/core/types/legacy_tx.go @@ -228,7 +228,7 @@ func (tx LegacyTx) payloadSize() (payloadSize int, nonceLen, gasLen int) { } default: if len(tx.Data) >= 56 { - payloadSize += (bits.Len(uint(len(tx.Data))) + 7) / 8 + payloadSize += libcommon.BitLenToByteLen(bits.Len(uint(len(tx.Data)))) } payloadSize += len(tx.Data) } diff --git a/core/types/signed_blob_tx.go b/core/types/signed_blob_tx.go index c0d2f5bb2..3c1124301 100644 --- a/core/types/signed_blob_tx.go +++ b/core/types/signed_blob_tx.go @@ -247,7 +247,7 @@ func (stx SignedBlobTx) EncodingSize() int { envelopeSize := payloadSize // Add envelope size and type size if payloadSize >= 56 { - envelopeSize += (bits.Len(uint(payloadSize)) + 7) / 8 + envelopeSize += libcommon.BitLenToByteLen(bits.Len(uint(payloadSize))) } envelopeSize += 2 return envelopeSize @@ -288,7 +288,7 @@ func (stx SignedBlobTx) payloadSize() (payloadSize int, nonceLen, gasLen, access } default: if len(stx.Data) >= 56 { - payloadSize += (bits.Len(uint(len(stx.Data))) + 7) / 8 + payloadSize += libcommon.BitLenToByteLen(bits.Len(uint(len(stx.Data)))) } payloadSize += len(stx.Data) } @@ -296,7 +296,7 @@ func (stx SignedBlobTx) payloadSize() (payloadSize int, nonceLen, gasLen, access payloadSize++ accessListLen = accessListSize(stx.AccessList) if accessListLen >= 56 { - payloadSize += (bits.Len(uint(accessListLen)) + 7) / 8 + payloadSize += libcommon.BitLenToByteLen(bits.Len(uint(accessListLen))) } payloadSize += accessListLen // size of MaxFeePerDataGas @@ -306,7 +306,7 @@ func (stx SignedBlobTx) payloadSize() (payloadSize int, nonceLen, gasLen, access payloadSize++ blobHashesLen = blobVersionedHashesSize(stx.BlobVersionedHashes) if blobHashesLen >= 56 { - payloadSize += (bits.Len(uint(blobHashesLen)) + 7) / 8 + payloadSize += libcommon.BitLenToByteLen(bits.Len(uint(blobHashesLen))) } payloadSize += blobHashesLen // size of y_parity @@ -431,7 +431,7 @@ func (stx SignedBlobTx) EncodeRLP(w io.Writer) error { payloadSize, nonceLen, gasLen, accessListLen, blobHashesLen := stx.payloadSize() envelopeSize := payloadSize if payloadSize >= 56 { - envelopeSize += (bits.Len(uint(payloadSize)) + 7) / 8 + envelopeSize += libcommon.BitLenToByteLen(bits.Len(uint(payloadSize))) } // size of struct prefix and TxType envelopeSize += 2 diff --git a/core/vm/gas_table.go b/core/vm/gas_table.go index 60e94e40e..a48eb6c3c 100644 --- a/core/vm/gas_table.go +++ b/core/vm/gas_table.go @@ -20,6 +20,7 @@ import ( "errors" "github.com/holiman/uint256" + libcommon "github.com/ledgerwatch/erigon-lib/common" "github.com/ledgerwatch/erigon-lib/common/math" @@ -346,7 +347,7 @@ func gasCreate2Eip3860(_ VMInterpreter, contract *Contract, stack *stack.Stack, } func gasExpFrontier(_ VMInterpreter, contract *Contract, stack *stack.Stack, mem *Memory, memorySize uint64) (uint64, error) { - expByteLen := uint64((stack.Data[stack.Len()-2].BitLen() + 7) / 8) + expByteLen := uint64(libcommon.BitLenToByteLen(stack.Data[stack.Len()-2].BitLen())) var ( gas = expByteLen * params.ExpByteFrontier // no overflow check required. Max is 256 * ExpByte gas @@ -359,7 +360,7 @@ func gasExpFrontier(_ VMInterpreter, contract *Contract, stack *stack.Stack, mem } func gasExpEIP160(_ VMInterpreter, contract *Contract, stack *stack.Stack, mem *Memory, memorySize uint64) (uint64, error) { - expByteLen := uint64((stack.Data[stack.Len()-2].BitLen() + 7) / 8) + expByteLen := uint64(libcommon.BitLenToByteLen(stack.Data[stack.Len()-2].BitLen())) var ( gas = expByteLen * params.ExpByteEIP160 // no overflow check required. Max is 256 * ExpByte gas diff --git a/crypto/ecies/ecies.go b/crypto/ecies/ecies.go index 64b5a99d0..c50500953 100644 --- a/crypto/ecies/ecies.go +++ b/crypto/ecies/ecies.go @@ -40,6 +40,8 @@ import ( "hash" "io" "math/big" + + libcommon "github.com/ledgerwatch/erigon-lib/common" ) var ( @@ -114,7 +116,7 @@ func GenerateKey(rand io.Reader, curve elliptic.Curve, params *ECIESParams) (prv // MaxSharedKeyLength returns the maximum length of the shared key the // public key can produce. func MaxSharedKeyLength(pub *PublicKey) int { - return (pub.Curve.Params().BitSize + 7) / 8 + return libcommon.BitLenToByteLen(pub.Curve.Params().BitSize) } // ECDH key agreement method used to establish secret keys for encryption. diff --git a/eth/protocols/eth/protocol.go b/eth/protocols/eth/protocol.go index 464c54b79..cda5d7206 100644 --- a/eth/protocols/eth/protocol.go +++ b/eth/protocols/eth/protocol.go @@ -204,12 +204,12 @@ func (tp TransactionsPacket) EncodeRLP(w io.Writer) error { txLen = t.EncodingSize() } if txLen >= 56 { - txsLen += (bits.Len(uint(txLen)) + 7) / 8 + txsLen += libcommon.BitLenToByteLen(bits.Len(uint(txLen))) } txsLen += txLen } if txsLen >= 56 { - encodingSize += (bits.Len(uint(txsLen)) + 7) / 8 + encodingSize += libcommon.BitLenToByteLen(bits.Len(uint(txsLen))) } encodingSize += txsLen // encode Transactions @@ -322,7 +322,7 @@ func (nbp NewBlockPacket) EncodeRLP(w io.Writer) error { encodingSize++ blockLen := nbp.Block.EncodingSize() if blockLen >= 56 { - encodingSize += (bits.Len(uint(blockLen)) + 7) / 8 + encodingSize += libcommon.BitLenToByteLen(bits.Len(uint(blockLen))) } encodingSize += blockLen // size of TD @@ -331,7 +331,7 @@ func (nbp NewBlockPacket) EncodeRLP(w io.Writer) error { if nbp.TD != nil { tdBitLen = nbp.TD.BitLen() if tdBitLen >= 8 { - tdLen = (tdBitLen + 7) / 8 + tdLen = libcommon.BitLenToByteLen(tdBitLen) } } encodingSize += tdLen @@ -519,12 +519,12 @@ func (ptp PooledTransactionsPacket) EncodeRLP(w io.Writer) error { txLen = t.EncodingSize() } if txLen >= 56 { - txsLen += (bits.Len(uint(txLen)) + 7) / 8 + txsLen += libcommon.BitLenToByteLen(bits.Len(uint(txLen))) } txsLen += txLen } if txsLen >= 56 { - encodingSize += (bits.Len(uint(txsLen)) + 7) / 8 + encodingSize += libcommon.BitLenToByteLen(bits.Len(uint(txsLen))) } encodingSize += txsLen // encode Transactions @@ -593,12 +593,12 @@ func (ptp66 PooledTransactionsPacket66) EncodeRLP(w io.Writer) error { txLen = t.EncodingSize() } if txLen >= 56 { - txsLen += (bits.Len(uint(txLen)) + 7) / 8 + txsLen += libcommon.BitLenToByteLen(bits.Len(uint(txLen))) } txsLen += txLen } if txsLen >= 56 { - encodingSize += (bits.Len(uint(txsLen)) + 7) / 8 + encodingSize += libcommon.BitLenToByteLen(bits.Len(uint(txsLen))) } encodingSize += txsLen var b [33]byte diff --git a/ethdb/kv_util.go b/ethdb/kv_util.go index 430d9d738..796ad8fe2 100644 --- a/ethdb/kv_util.go +++ b/ethdb/kv_util.go @@ -3,6 +3,7 @@ package ethdb import ( "bytes" + libcommon "github.com/ledgerwatch/erigon-lib/common" "github.com/ledgerwatch/erigon-lib/kv" ) @@ -29,7 +30,7 @@ func Walk(c kv.Cursor, startkey []byte, fixedbits int, walker func(k, v []byte) } func Bytesmask(fixedbits int) (fixedbytes int, mask byte) { - fixedbytes = (fixedbits + 7) / 8 + fixedbytes = libcommon.BitLenToByteLen(fixedbits) shiftbits := fixedbits & 7 mask = byte(0xff) if shiftbits != 0 { diff --git a/go.mod b/go.mod index fb462bc17..3735fbd1f 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/ledgerwatch/erigon go 1.19 require ( - github.com/ledgerwatch/erigon-lib v0.0.0-20230614032101-ee8930fe2e70 + github.com/ledgerwatch/erigon-lib v0.0.0-20230614122213-f87b4cd9de58 github.com/ledgerwatch/erigon-snapshot v1.2.1-0.20230605042354-196538d42475 github.com/ledgerwatch/log/v3 v3.8.0 github.com/ledgerwatch/secp256k1 v1.0.0 diff --git a/go.sum b/go.sum index 412d2bb4b..a7ad82c2e 100644 --- a/go.sum +++ b/go.sum @@ -417,8 +417,8 @@ github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/kylelemons/godebug v0.0.0-20170224010052-a616ab194758 h1:0D5M2HQSGD3PYPwICLl+/9oulQauOuETfgFvhBDffs0= github.com/leanovate/gopter v0.2.9 h1:fQjYxZaynp97ozCzfOyOuAGOU4aU/z37zf/tOujFk7c= github.com/leanovate/gopter v0.2.9/go.mod h1:U2L/78B+KVFIx2VmW6onHJQzXtFb+p5y3y2Sh+Jxxv8= -github.com/ledgerwatch/erigon-lib v0.0.0-20230614032101-ee8930fe2e70 h1:Ucwk02M0TYSR3NF29FIbIGmBeKNbnzZjH/NN43MPMHA= -github.com/ledgerwatch/erigon-lib v0.0.0-20230614032101-ee8930fe2e70/go.mod h1:HsaEkkc6WIfOwN+5MdPFhUdANAMIRa0UcOWfdlV6gY0= +github.com/ledgerwatch/erigon-lib v0.0.0-20230614122213-f87b4cd9de58 h1:G9GM0eNxy8t2upW7h9y1rHl9h36IsM/f1MqA2IWlHEg= +github.com/ledgerwatch/erigon-lib v0.0.0-20230614122213-f87b4cd9de58/go.mod h1:HsaEkkc6WIfOwN+5MdPFhUdANAMIRa0UcOWfdlV6gY0= github.com/ledgerwatch/erigon-snapshot v1.2.1-0.20230605042354-196538d42475 h1:1BvWA6agTUS4RZUHx79f45HpvelMVv4iEddaURUYcC8= github.com/ledgerwatch/erigon-snapshot v1.2.1-0.20230605042354-196538d42475/go.mod h1:3AuPxZc85jkehh/HA9h8gabv5MSi3kb/ddtzBsTVJFo= github.com/ledgerwatch/log/v3 v3.8.0 h1:gCpp7uGtIerEz1jKVPeDnbIopFPud9ZnCpBLlLBGqPU= @@ -1100,8 +1100,6 @@ modernc.org/tcl v1.15.2 h1:C4ybAYCGJw968e+Me18oW55kD/FexcHbqH2xak1ROSY= modernc.org/token v1.1.0 h1:Xl7Ap9dKaEs5kLoOQeQmPWevfnk/DM5qcLcYlA8ys6Y= modernc.org/token v1.1.0/go.mod h1:UGzOrNV1mAFSEB63lOFHIpNRUVMvYTc6yu1SMY/XTDM= modernc.org/z v1.7.3 h1:zDJf6iHjrnB+WRD88stbXokugjyc0/pB91ri1gO6LZY= -nhooyr.io/websocket v1.8.7 h1:usjR2uOr/zjjkVMy0lW+PPohFok7PCow5sDjLgX4P4g= -nhooyr.io/websocket v1.8.7/go.mod h1:B70DZP8IakI65RVQ51MsWP/8jndNma26DVA/nFSCgW0= pgregory.net/rapid v1.0.0 h1:iQaM2w5PZ6xvt6x7hbd7tiDS+nk7YPp5uCaEba+T/F4= pgregory.net/rapid v1.0.0/go.mod h1:PY5XlDGj0+V1FCq0o192FdRhpKHGTRIWBgqjDBTrq04= rsc.io/tmplfunc v0.0.3 h1:53XFQh69AfOa8Tw0Jm7t+GV7KZhOi6jzsCzTtKbMvzU= diff --git a/rlp/encode.go b/rlp/encode.go index afa5281d2..f43b71670 100644 --- a/rlp/encode.go +++ b/rlp/encode.go @@ -26,6 +26,8 @@ import ( "sync" "github.com/holiman/uint256" + + libcommon "github.com/ledgerwatch/erigon-lib/common" ) // https://github.com/ethereum/wiki/wiki/RLP @@ -747,7 +749,7 @@ func putint(b []byte, i uint64) (size int) { // intsize computes the minimum number of bytes required to store i. func intsize(i uint64) (size int) { - return (bits.Len64(i) + 7) / 8 + return libcommon.BitLenToByteLen(bits.Len64(i)) } func IntLenExcludingHead(i uint64) int { @@ -762,7 +764,7 @@ func BigIntLenExcludingHead(i *big.Int) int { if bitLen < 8 { return 0 } - return (bitLen + 7) / 8 + return libcommon.BitLenToByteLen(bitLen) } func Uint256LenExcludingHead(i *uint256.Int) int { @@ -770,7 +772,7 @@ func Uint256LenExcludingHead(i *uint256.Int) int { if bitLen < 8 { return 0 } - return (bitLen + 7) / 8 + return libcommon.BitLenToByteLen(bitLen) } // precondition: len(buffer) >= 9 @@ -803,7 +805,7 @@ func EncodeBigInt(i *big.Int, w io.Writer, buffer []byte) error { return err } - size := (bitLen + 7) / 8 + size := libcommon.BitLenToByteLen(bitLen) buffer[0] = 0x80 + byte(size) i.FillBytes(buffer[1 : 1+size]) _, err := w.Write(buffer[:1+size]) @@ -840,7 +842,7 @@ func EncodeString(s []byte, w io.Writer, buffer []byte) error { func EncodeStringSizePrefix(size int, w io.Writer, buffer []byte) error { if size >= 56 { - beSize := (bits.Len(uint(size)) + 7) / 8 + beSize := libcommon.BitLenToByteLen(bits.Len(uint(size))) binary.BigEndian.PutUint64(buffer[1:], uint64(size)) buffer[8-beSize] = byte(beSize) + 183 if _, err := w.Write(buffer[8-beSize : 9]); err != nil {