Skip to content

Commit

Permalink
chore: Rename txAmount to txCount (#10713)
Browse files Browse the repository at this point in the history
To avoid confusion, this PR renames occurrences of `txAmount` and
`txsAmount` in the code base to `txCount`. The word amount has a
different meaning, in general. Before this PR, no meaning of the word is
being utilized with `txAmount` and can be confused with amount of gas
utilized.
  • Loading branch information
somnathb1 authored Jun 13, 2024
1 parent 194e198 commit f37e230
Show file tree
Hide file tree
Showing 13 changed files with 97 additions and 97 deletions.
16 changes: 8 additions & 8 deletions cmd/hack/hack.go
Original file line number Diff line number Diff line change
Expand Up @@ -541,11 +541,11 @@ func extractBodies(datadir string) error {
var lastBlockNum, lastBaseTxNum, lastAmount uint64
var prevBlockNum, prevBaseTxNum, prevAmount uint64
first := true
sn.Iterate(func(blockNum uint64, baseTxNum uint64, txAmount uint64) error {
sn.Iterate(func(blockNum uint64, baseTxNum uint64, txCount uint64) error {
if first {
firstBlockNum = blockNum
firstBaseTxNum = baseTxNum
firstAmount = txAmount
firstAmount = txCount
first = false
} else {
if blockNum != prevBlockNum+1 {
Expand All @@ -559,8 +559,8 @@ func extractBodies(datadir string) error {
lastBlockNum = blockNum
prevBaseTxNum = baseTxNum
lastBaseTxNum = baseTxNum
prevAmount = txAmount
lastAmount = txAmount
prevAmount = txCount
lastAmount = txCount
return nil
})
fmt.Printf("Seg: [%d, %d, %d] => [%d, %d, %d]\n", firstBlockNum, firstBaseTxNum, firstAmount, lastBlockNum, lastBaseTxNum, lastAmount)
Expand Down Expand Up @@ -594,8 +594,8 @@ func extractBodies(datadir string) error {
if hash, err = br.CanonicalHash(context.Background(), tx, blockNumber); err != nil {
return err
}
_, baseTxId, txAmount := rawdb.ReadBody(tx, blockHash, blockNumber)
fmt.Printf("Body %d %x: baseTxId %d, txAmount %d\n", blockNumber, blockHash, baseTxId, txAmount)
_, baseTxId, txCount := rawdb.ReadBody(tx, blockHash, blockNumber)
fmt.Printf("Body %d %x: baseTxId %d, txCount %d\n", blockNumber, blockHash, baseTxId, txCount)
if hash != blockHash {
fmt.Printf("Non-canonical\n")
continue
Expand All @@ -606,7 +606,7 @@ func extractBodies(datadir string) error {
fmt.Printf("Mismatch txId for block %d, txId = %d, baseTxId = %d\n", blockNumber, txId, baseTxId)
}
}
txId = baseTxId + uint64(txAmount) + 2
txId = baseTxId + uint64(txCount) + 2
if i == 50 {
break
}
Expand Down Expand Up @@ -901,7 +901,7 @@ func trimTxs(chaindata string) error {
return err
}
// Remove from the map
toDelete.RemoveRange(body.BaseTxId, body.BaseTxId+uint64(body.TxAmount))
toDelete.RemoveRange(body.BaseTxId, body.BaseTxId+uint64(body.TxCount))
}
fmt.Printf("Number of tx records to delete: %d\n", toDelete.GetCardinality())
// Takes 20min to iterate 1.4b
Expand Down
4 changes: 2 additions & 2 deletions cmd/rpcdaemon/rpcservices/eth_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ func (back *RemoteBackend) BlockWithSenders(ctx context.Context, tx kv.Getter, h
return back.blockReader.BlockWithSenders(ctx, tx, hash, blockNum)
}

func (back *RemoteBackend) IterateFrozenBodies(_ func(blockNum uint64, baseTxNum uint64, txAmount uint64) error) error {
func (back *RemoteBackend) IterateFrozenBodies(_ func(blockNum uint64, baseTxNum uint64, txCount uint64) error) error {
panic("not implemented")
}

Expand All @@ -266,7 +266,7 @@ func (back *RemoteBackend) BodyWithTransactions(ctx context.Context, tx kv.Gette
func (back *RemoteBackend) BodyRlp(ctx context.Context, tx kv.Getter, hash common.Hash, blockNum uint64) (bodyRlp rlp.RawValue, err error) {
return back.blockReader.BodyRlp(ctx, tx, hash, blockNum)
}
func (back *RemoteBackend) Body(ctx context.Context, tx kv.Getter, hash common.Hash, blockNum uint64) (body *types.Body, txAmount uint32, err error) {
func (back *RemoteBackend) Body(ctx context.Context, tx kv.Getter, hash common.Hash, blockNum uint64) (body *types.Body, txCount uint32, err error) {
return back.blockReader.Body(ctx, tx, hash, blockNum)
}
func (back *RemoteBackend) Header(ctx context.Context, tx kv.Getter, hash common.Hash, blockNum uint64) (*types.Header, error) {
Expand Down
28 changes: 14 additions & 14 deletions core/rawdb/accessors_chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -513,17 +513,17 @@ func ReadBodyByNumber(db kv.Tx, number uint64) (*types.Body, uint64, uint32, err
if hash == (common.Hash{}) {
return nil, 0, 0, nil
}
body, baseTxId, txAmount := ReadBody(db, hash, number)
return body, baseTxId, txAmount, nil
body, baseTxId, txCount := ReadBody(db, hash, number)
return body, baseTxId, txCount, nil
}

func ReadBodyWithTransactions(db kv.Getter, hash common.Hash, number uint64) (*types.Body, error) {
body, baseTxId, txAmount := ReadBody(db, hash, number)
body, baseTxId, txCount := ReadBody(db, hash, number)
if body == nil {
return nil, nil
}
var err error
body.Transactions, err = CanonicalTransactions(db, baseTxId, txAmount)
body.Transactions, err = CanonicalTransactions(db, baseTxId, txCount)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -552,13 +552,13 @@ func RawTransactionsRange(db kv.Getter, from, to uint64) (res [][]byte, err erro
if len(bodyRlp) == 0 {
continue
}
baseTxId, txAmount, err := types.DecodeOnlyTxMetadataFromBody(bodyRlp)
baseTxId, txCount, err := types.DecodeOnlyTxMetadataFromBody(bodyRlp)
if err != nil {
return nil, err
}

binary.BigEndian.PutUint64(encNum, baseTxId)
if err = db.ForAmount(kv.EthTx, encNum, txAmount, func(k, v []byte) error {
if err = db.ForAmount(kv.EthTx, encNum, txCount, func(k, v []byte) error {
res = append(res, v)
return nil
}); err != nil {
Expand Down Expand Up @@ -610,10 +610,10 @@ func ReadBody(db kv.Getter, hash common.Hash, number uint64) (*types.Body, uint6
body.Withdrawals = bodyForStorage.Withdrawals
body.Requests = bodyForStorage.Requests

if bodyForStorage.TxAmount < 2 {
panic(fmt.Sprintf("block body hash too few txs amount: %d, %d", number, bodyForStorage.TxAmount))
if bodyForStorage.TxCount < 2 {
panic(fmt.Sprintf("block body hash too few txs amount: %d, %d", number, bodyForStorage.TxCount))
}
return body, bodyForStorage.BaseTxId + 1, bodyForStorage.TxAmount - 2 // 1 system txn in the begining of block, and 1 at the end
return body, bodyForStorage.BaseTxId + 1, bodyForStorage.TxCount - 2 // 1 system txn in the begining of block, and 1 at the end
}

func HasSenders(db kv.Getter, hash common.Hash, number uint64) (bool, error) {
Expand Down Expand Up @@ -650,7 +650,7 @@ func WriteRawBody(db kv.RwTx, hash common.Hash, number uint64, body *types.RawBo
}
data := types.BodyForStorage{
BaseTxId: baseTxnID,
TxAmount: uint32(len(body.Transactions)) + 2, /*system txs*/
TxCount: uint32(len(body.Transactions)) + 2, /*system txs*/
Uncles: body.Uncles,
Withdrawals: body.Withdrawals,
Requests: body.Requests,
Expand All @@ -674,7 +674,7 @@ func WriteBody(db kv.RwTx, hash common.Hash, number uint64, body *types.Body) (e
}
data := types.BodyForStorage{
BaseTxId: baseTxId,
TxAmount: uint32(len(body.Transactions)) + 2,
TxCount: uint32(len(body.Transactions)) + 2,
Uncles: body.Uncles,
Withdrawals: body.Withdrawals,
Requests: body.Requests,
Expand Down Expand Up @@ -734,7 +734,7 @@ func AppendCanonicalTxNums(tx kv.RwTx, from uint64) (err error) {
return err
}

nextBaseTxNum += int(bodyForStorage.TxAmount)
nextBaseTxNum += int(bodyForStorage.TxCount)
err = rawdbv3.TxNums.Append(tx, blockNum, uint64(nextBaseTxNum-1))
if err != nil {
return err
Expand Down Expand Up @@ -1060,7 +1060,7 @@ func PruneBlocks(tx kv.RwTx, blockTo uint64, blocksDeleteLimit int) (deleted int
log.Debug("PruneBlocks: block body not found", "height", n)
} else {
txIDBytes := make([]byte, 8)
for txID := b.BaseTxId; txID < b.BaseTxId+uint64(b.TxAmount); txID++ {
for txID := b.BaseTxId; txID < b.BaseTxId+uint64(b.TxCount); txID++ {
binary.BigEndian.PutUint64(txIDBytes, txID)
if err = tx.Delete(kv.EthTx, txIDBytes); err != nil {
return deleted, err
Expand Down Expand Up @@ -1108,7 +1108,7 @@ func TruncateBlocks(ctx context.Context, tx kv.RwTx, blockFrom uint64) error {
}
if b != nil {
txIDBytes := make([]byte, 8)
for txID := b.BaseTxId; txID < b.BaseTxId+uint64(b.TxAmount); txID++ {
for txID := b.BaseTxId; txID < b.BaseTxId+uint64(b.TxCount); txID++ {
binary.BigEndian.PutUint64(txIDBytes, txID)
if err = tx.Delete(kv.EthTx, txIDBytes); err != nil {
return err
Expand Down
6 changes: 3 additions & 3 deletions core/rawdb/accessors_chain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -773,7 +773,7 @@ func TestPreShanghaiBodyForStorageNoPanicOnWithdrawals(t *testing.T) {
rlp.DecodeBytes(bstring, body)

require.Nil(body.Withdrawals)
require.Equal(uint32(2), body.TxAmount)
require.Equal(uint32(2), body.TxCount)
}

// Tests shanghai bodyForStorage to make sure withdrawals are present
Expand All @@ -789,7 +789,7 @@ func TestShanghaiBodyForStorageHasWithdrawals(t *testing.T) {

require.NotNil(body.Withdrawals)
require.Equal(2, len(body.Withdrawals))
require.Equal(uint32(2), body.TxAmount)
require.Equal(uint32(2), body.TxCount)
}

// Tests shanghai bodyForStorage to make sure when no withdrawals the slice is empty (not nil)
Expand All @@ -805,7 +805,7 @@ func TestShanghaiBodyForStorageNoWithdrawals(t *testing.T) {

require.NotNil(body.Withdrawals)
require.Equal(0, len(body.Withdrawals))
require.Equal(uint32(2), body.TxAmount)
require.Equal(uint32(2), body.TxCount)
}

func checkReceiptsRLP(have, want types.Receipts) error {
Expand Down
6 changes: 3 additions & 3 deletions core/rawdb/accessors_iter.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,10 @@ func (s *CanonicalTxnIds) advance() (err error) {

if s.orderAscend {
s.currentTxNum = int(body.BaseTxId)
s.endOfCurrentBlock = body.BaseTxId + uint64(body.TxAmount) // 2 system txs already included in TxAmount
s.endOfCurrentBlock = body.BaseTxId + uint64(body.TxCount) // 2 system txs already included in TxAmount
} else {
s.currentTxNum = int(body.BaseTxId) + int(body.TxAmount) - 1 // 2 system txs already included in TxAmount
s.endOfCurrentBlock = body.BaseTxId - 1 // and one of them is baseTxId
s.currentTxNum = int(body.BaseTxId) + int(body.TxCount) - 1 // 2 system txs already included in TxAmount
s.endOfCurrentBlock = body.BaseTxId - 1 // and one of them is baseTxId
}
return nil
}
Expand Down
4 changes: 2 additions & 2 deletions core/snaptype/block_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ var (
default:
}

for body.BaseTxId+uint64(body.TxAmount) <= firstTxID+i { // skip empty blocks
for body.BaseTxId+uint64(body.TxCount) <= firstTxID+i { // skip empty blocks
if !bodyGetter.HasNext() {
return fmt.Errorf("not enough bodies")
}
Expand Down Expand Up @@ -327,6 +327,6 @@ func txsAmountBasedOnBodiesSnapshots(bodiesSegment *seg.Decompressor, len uint64
return 0, 0, fmt.Errorf("negative txs count %s: lastBody.BaseTxId=%d < firstBody.BaseTxId=%d", bodiesSegment.FileName(), lastBody.BaseTxId, firstBody.BaseTxId)
}

expectedCount = int(lastBody.BaseTxId+uint64(lastBody.TxAmount)) - int(firstBody.BaseTxId)
expectedCount = int(lastBody.BaseTxId+uint64(lastBody.TxCount)) - int(firstBody.BaseTxId)
return
}
24 changes: 12 additions & 12 deletions core/types/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,7 @@ type RawBody struct {

type BodyForStorage struct {
BaseTxId uint64
TxAmount uint32
TxCount uint32
Uncles []*Header
Withdrawals []*Withdrawal
Requests Requests
Expand Down Expand Up @@ -812,10 +812,10 @@ func (rb *RawBody) DecodeRLP(s *rlp.Stream) error {

func (bfs BodyForStorage) payloadSize() (payloadSize, unclesLen, withdrawalsLen, requestsLen int) {
baseTxIdLen := 1 + rlp.IntLenExcludingHead(bfs.BaseTxId)
txAmountLen := 1 + rlp.IntLenExcludingHead(uint64(bfs.TxAmount))
txCountLen := 1 + rlp.IntLenExcludingHead(uint64(bfs.TxCount))

payloadSize += baseTxIdLen
payloadSize += txAmountLen
payloadSize += txCountLen

// size of Uncles
unclesLen += encodingSizeGeneric(bfs.Uncles)
Expand Down Expand Up @@ -850,8 +850,8 @@ func (bfs BodyForStorage) EncodeRLP(w io.Writer) error {
return err
}

// encode TxAmount
if err := rlp.Encode(w, bfs.TxAmount); err != nil {
// encode TxCount
if err := rlp.Encode(w, bfs.TxCount); err != nil {
return err
}

Expand Down Expand Up @@ -883,8 +883,8 @@ func (bfs *BodyForStorage) DecodeRLP(s *rlp.Stream) error {
if err = s.Decode(&bfs.BaseTxId); err != nil {
return err
}
// decode TxAmount
if err = s.Decode(&bfs.TxAmount); err != nil {
// decode TxCount
if err = s.Decode(&bfs.TxCount); err != nil {
return err
}
// decode Uncles
Expand Down Expand Up @@ -1496,18 +1496,18 @@ func (b *Block) Hash() libcommon.Hash {

type Blocks []*Block

func DecodeOnlyTxMetadataFromBody(payload []byte) (baseTxId uint64, txAmount uint32, err error) {
func DecodeOnlyTxMetadataFromBody(payload []byte) (baseTxId uint64, txCount uint32, err error) {
pos, _, err := rlp2.List(payload, 0)
if err != nil {
return baseTxId, txAmount, err
return baseTxId, txCount, err
}
pos, baseTxId, err = rlp2.U64(payload, pos)
if err != nil {
return baseTxId, txAmount, err
return baseTxId, txCount, err
}
_, txAmount, err = rlp2.U32(payload, pos)
_, txCount, err = rlp2.U32(payload, pos)
if err != nil {
return baseTxId, txAmount, err
return baseTxId, txCount, err
}
return
}
Expand Down
12 changes: 6 additions & 6 deletions turbo/jsonrpc/eth_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ func (api *APIImpl) GetBlockTransactionCountByNumber(ctx context.Context, blockN
return nil, nil
}

_, txAmount, err := api._blockReader.Body(ctx, tx, blockHash, blockNum)
_, txCount, err := api._blockReader.Body(ctx, tx, blockHash, blockNum)
if err != nil {
return nil, err
}
Expand All @@ -356,11 +356,11 @@ func (api *APIImpl) GetBlockTransactionCountByNumber(ctx context.Context, blockN
return nil, err
}
if ok {
txAmount++
txCount++
}
}

numOfTx := hexutil.Uint(txAmount)
numOfTx := hexutil.Uint(txCount)

return &numOfTx, nil
}
Expand All @@ -380,7 +380,7 @@ func (api *APIImpl) GetBlockTransactionCountByHash(ctx context.Context, blockHas
return nil, nil
}

_, txAmount, err := api._blockReader.Body(ctx, tx, blockHash, blockNum)
_, txCount, err := api._blockReader.Body(ctx, tx, blockHash, blockNum)
if err != nil {
return nil, err
}
Expand All @@ -397,11 +397,11 @@ func (api *APIImpl) GetBlockTransactionCountByHash(ctx context.Context, blockHas
return nil, err
}
if ok {
txAmount++
txCount++
}
}

numOfTx := hexutil.Uint(txAmount)
numOfTx := hexutil.Uint(txCount)

return &numOfTx, nil
}
Expand Down
16 changes: 8 additions & 8 deletions turbo/jsonrpc/eth_block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,12 +200,12 @@ func TestGetBlockTransactionCountByHash(t *testing.T) {

expectedAmount := hexutil.Uint(len(bodyWithTx.Transactions))

txAmount, err := api.GetBlockTransactionCountByHash(ctx, blockHash)
txCount, err := api.GetBlockTransactionCountByHash(ctx, blockHash)
if err != nil {
t.Errorf("failed getting the transaction count, err=%s", err)
}

assert.Equal(t, expectedAmount, *txAmount)
assert.Equal(t, expectedAmount, *txCount)
}

func TestGetBlockTransactionCountByHash_ZeroTx(t *testing.T) {
Expand All @@ -232,12 +232,12 @@ func TestGetBlockTransactionCountByHash_ZeroTx(t *testing.T) {

expectedAmount := hexutil.Uint(len(bodyWithTx.Transactions))

txAmount, err := api.GetBlockTransactionCountByHash(ctx, blockHash)
txCount, err := api.GetBlockTransactionCountByHash(ctx, blockHash)
if err != nil {
t.Errorf("failed getting the transaction count, err=%s", err)
}

assert.Equal(t, expectedAmount, *txAmount)
assert.Equal(t, expectedAmount, *txCount)
}

func TestGetBlockTransactionCountByNumber(t *testing.T) {
Expand All @@ -264,12 +264,12 @@ func TestGetBlockTransactionCountByNumber(t *testing.T) {

expectedAmount := hexutil.Uint(len(bodyWithTx.Transactions))

txAmount, err := api.GetBlockTransactionCountByNumber(ctx, rpc.BlockNumber(header.Number.Uint64()))
txCount, err := api.GetBlockTransactionCountByNumber(ctx, rpc.BlockNumber(header.Number.Uint64()))
if err != nil {
t.Errorf("failed getting the transaction count, err=%s", err)
}

assert.Equal(t, expectedAmount, *txAmount)
assert.Equal(t, expectedAmount, *txCount)
}

func TestGetBlockTransactionCountByNumber_ZeroTx(t *testing.T) {
Expand Down Expand Up @@ -297,10 +297,10 @@ func TestGetBlockTransactionCountByNumber_ZeroTx(t *testing.T) {

expectedAmount := hexutil.Uint(len(bodyWithTx.Transactions))

txAmount, err := api.GetBlockTransactionCountByNumber(ctx, rpc.BlockNumber(header.Number.Uint64()))
txCount, err := api.GetBlockTransactionCountByNumber(ctx, rpc.BlockNumber(header.Number.Uint64()))
if err != nil {
t.Errorf("failed getting the transaction count, err=%s", err)
}

assert.Equal(t, expectedAmount, *txAmount)
assert.Equal(t, expectedAmount, *txCount)
}
Loading

0 comments on commit f37e230

Please sign in to comment.