Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[wip] Why we even need txNum for statesyncTx? Yep. In RPC we needn't it. #14086

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion core/rawdb/accessors_indexes.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func ReadTxLookupEntry(db kv.Getter, txnHash libcommon.Hash) (blockNumber *uint6
return nil, nil, nil
}
numberBlockNum := binary.BigEndian.Uint64(data[:8])
numberTxNum := binary.BigEndian.Uint64(data[8:]) + 1
numberTxNum := binary.BigEndian.Uint64(data[8:])

return &numberBlockNum, &numberTxNum, nil
}
Expand Down
2 changes: 1 addition & 1 deletion core/rawdb/accessors_indexes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func TestLookupStorage(t *testing.T) {
if txn.Hash() != txn2.Hash() {
t.Fatalf("txn #%d [%x]: transaction mismatch: have %v, want %v", i, txn.Hash(), txn, txn2)
}
if txNum != txNumMin+uint64(i)+2 {
if txNum != txNumMin+uint64(i)+1 {
t.Fatalf("txn #%d [%x]: txnum mismatch: have %d, want %d", i, txn.Hash(), txNum, txNumMin+uint64(i)+1)
}
}
Expand Down
14 changes: 4 additions & 10 deletions turbo/jsonrpc/debug_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -512,16 +512,10 @@ func (api *PrivateDebugAPIImpl) GetRawTransaction(ctx context.Context, txnHash c
txNumsReader := rawdbv3.TxNums.WithCustomReadTxNumFunc(freezeblocks.ReadTxNumFuncFromBlockReader(ctx, api._blockReader))

// Private API returns 0 if transaction is not found.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't understand why need rely on blockNum == 0 if txnLookup returns ok

if blockNum == 0 && chainConfig.Bor != nil {
isBorStateSyncTx := blockNum == 0 && chainConfig.Bor != nil
if isBorStateSyncTx {
if api.useBridgeReader {
blockNum, ok, err = api.bridgeReader.EventTxnLookup(ctx, txnHash)
if ok {
txNumNextBlock, err := txNumsReader.Min(tx, blockNum+1)
if err != nil {
return nil, err
}
txNum = txNumNextBlock
}
} else {
blockNum, ok, err = api._blockReader.EventLookup(ctx, tx, txnHash)
}
Expand All @@ -540,11 +534,11 @@ func (api *PrivateDebugAPIImpl) GetRawTransaction(ctx context.Context, txnHash c
return nil, err
}

if txNumMin+2 > txNum {
if txNumMin+1 > txNum && !isBorStateSyncTx {
return nil, fmt.Errorf("uint underflow txnums error txNum: %d, txNumMin: %d, blockNum: %d", txNum, txNumMin, blockNum)
}

var txnIndex uint64 = txNum - txNumMin - 2
var txnIndex = txNum - txNumMin - 1

txn, err := api._txnReader.TxnByIdxInBlock(ctx, tx, blockNum, int(txnIndex))
if err != nil {
Expand Down
33 changes: 16 additions & 17 deletions turbo/jsonrpc/eth_receipts.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,10 @@ func (api *BaseAPI) getLogsV3(ctx context.Context, tx kv.TemporalTx, begin, end
return logs, err
}

if len(events) == 0 {
continue
}

borLogs, err := api.borReceiptGenerator.GenerateBorLogs(ctx, events, txNumsReader, tx, header, chainConfig, txIndex, len(logs))
if err != nil {
return logs, err
Expand Down Expand Up @@ -470,19 +474,14 @@ func (api *APIImpl) GetTransactionReceipt(ctx context.Context, txnHash common.Ha
}

// Private API returns 0 if transaction is not found.
if blockNum == 0 && chainConfig.Bor != nil {
isBorStateSyncTxn := blockNum == 0 && chainConfig.Bor != nil

if isBorStateSyncTxn {
if api.useBridgeReader {
blockNum, ok, err = api.bridgeReader.EventTxnLookup(ctx, txnHash)
if err != nil {
return nil, err
}
if ok {
txNumNextBlock, err := txNumsReader.Min(tx, blockNum+1)
if err != nil {
return nil, err
}
txNum = txNumNextBlock
}
} else {
blockNum, ok, err = api._blockReader.EventLookup(ctx, tx, txnHash)
}
Expand All @@ -495,7 +494,7 @@ func (api *APIImpl) GetTransactionReceipt(ctx context.Context, txnHash common.Ha
return nil, nil
}

if txNumMin+2 > txNum { //TODO: what a magic is this "2" and how to avoid it
if txNumMin+1 > txNum && !isBorStateSyncTxn { //TODO: what a magic is this "2" and how to avoid it
return nil, fmt.Errorf("uint underflow txnums error txNum: %d, txNumMin: %d, blockNum: %d", txNum, txNumMin, blockNum)
}

Expand All @@ -504,14 +503,7 @@ func (api *APIImpl) GetTransactionReceipt(ctx context.Context, txnHash common.Ha
return nil, err
}

var txnIndex = int(txNum - txNumMin - 2)

txn, err := api._blockReader.TxnByIdxInBlock(ctx, tx, header.Number.Uint64(), txnIndex)
if err != nil {
return nil, err
}

if txn == nil && chainConfig.Bor != nil {
if isBorStateSyncTxn {
block, err := api.blockByNumberWithSenders(ctx, tx, blockNum)
if err != nil {
return nil, err
Expand All @@ -537,6 +529,13 @@ func (api *APIImpl) GetTransactionReceipt(ctx context.Context, txnHash common.Ha
return ethutils.MarshalReceipt(borReceipt, bortypes.NewBorTransaction(), chainConfig, block.HeaderNoCopy(), txnHash, false), nil
}

var txnIndex = int(txNum - txNumMin - 1)

txn, err := api._blockReader.TxnByIdxInBlock(ctx, tx, header.Number.Uint64(), txnIndex)
if err != nil {
return nil, err
}

receipt, err := api.getReceipt(ctx, chainConfig, tx, header, txn, txnIndex, txNum)
if err != nil {
return nil, fmt.Errorf("getReceipt error: %w", err)
Expand Down
31 changes: 11 additions & 20 deletions turbo/jsonrpc/eth_txs.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,10 @@ func (api *APIImpl) GetTransactionByHash(ctx context.Context, txnHash common.Has
txNumsReader := rawdbv3.TxNums.WithCustomReadTxNumFunc(freezeblocks.ReadTxNumFuncFromBlockReader(ctx, api._blockReader))

// Private API returns 0 if transaction is not found.
if blockNum == 0 && chainConfig.Bor != nil {
isBorStateSyncTxn := blockNum == 0 && chainConfig.Bor != nil
if isBorStateSyncTxn {
if api.useBridgeReader {
blockNum, ok, err = api.bridgeReader.EventTxnLookup(ctx, txnHash)
if ok {
txNumNextBlock, err := txNumsReader.Min(tx, blockNum+1)
if err != nil {
return nil, err
}
txNum = txNumNextBlock
}
} else {
blockNum, ok, err = api._blockReader.EventLookup(ctx, tx, txnHash)
}
Expand All @@ -82,17 +76,10 @@ func (api *APIImpl) GetTransactionByHash(ctx context.Context, txnHash common.Has
return nil, err
}

if txNumMin+2 > txNum { //TODO: what a magic is this "2" and how to avoid it
if txNumMin+1 > txNum && !isBorStateSyncTxn {
return nil, fmt.Errorf("uint underflow txnums error txNum: %d, txNumMin: %d, blockNum: %d", txNum, txNumMin, blockNum)
}

var txnIndex uint64 = txNum - txNumMin - 2

txn, err := api._txnReader.TxnByIdxInBlock(ctx, tx, blockNum, int(txnIndex))
if err != nil {
return nil, err
}

header, err := api._blockReader.HeaderByNumber(ctx, tx, blockNum)
if err != nil {
return nil, err
Expand All @@ -110,10 +97,7 @@ func (api *APIImpl) GetTransactionByHash(ctx context.Context, txnHash common.Has
}

// if no transaction was found then we return nil
if txn == nil {
if chainConfig.Bor == nil {
return nil, nil
}
if isBorStateSyncTxn {
borTx := bortypes.NewBorTransaction()
_, txCount, err := api._blockReader.Body(ctx, tx, blockHash, blockNum)
if err != nil {
Expand All @@ -122,6 +106,13 @@ func (api *APIImpl) GetTransactionByHash(ctx context.Context, txnHash common.Has
return ethapi.NewRPCBorTransaction(borTx, txnHash, blockHash, blockNum, uint64(txCount), chainConfig.ChainID), nil
}

var txnIndex = txNum - txNumMin - 1

txn, err := api._txnReader.TxnByIdxInBlock(ctx, tx, blockNum, int(txnIndex))
if err != nil {
return nil, err
}

return ethapi.NewRPCTransaction(txn, blockHash, blockNum, txnIndex, baseFee), nil
}

Expand Down
11 changes: 2 additions & 9 deletions turbo/jsonrpc/trace_adhoc.go
Original file line number Diff line number Diff line change
Expand Up @@ -833,13 +833,6 @@ func (api *TraceAPIImpl) ReplayTransaction(ctx context.Context, txHash libcommon
// otherwise this may be a bor state sync transaction - check
if api.useBridgeReader {
blockNum, ok, err = api.bridgeReader.EventTxnLookup(ctx, txHash)
if ok {
txNumNextBlock, err := txNumsReader.Min(tx, blockNum+1)
if err != nil {
return nil, err
}
txNum = txNumNextBlock
}
} else {
blockNum, ok, err = api._blockReader.EventLookup(ctx, tx, txHash)
}
Expand All @@ -864,11 +857,11 @@ func (api *TraceAPIImpl) ReplayTransaction(ctx context.Context, txHash libcommon
return nil, err
}

if txNumMin+2 > txNum {
if txNumMin+1 > txNum && !isBorStateSyncTxn {
return nil, fmt.Errorf("uint underflow txnums error txNum: %d, txNumMin: %d, blockNum: %d", txNum, txNumMin, blockNum)
}

var txnIndex = int(txNum - txNumMin - 2)
var txnIndex = int(txNum - txNumMin - 1)

if isBorStateSyncTxn {
txnIndex = -1
Expand Down
11 changes: 2 additions & 9 deletions turbo/jsonrpc/trace_filtering.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,6 @@ func (api *TraceAPIImpl) Transaction(ctx context.Context, txHash common.Hash, ga
// otherwise this may be a bor state sync transaction - check
if api.useBridgeReader {
blockNumber, ok, err = api.bridgeReader.EventTxnLookup(ctx, txHash)
if ok {
txNumNextBlock, err := txNumsReader.Min(tx, blockNumber+1)
if err != nil {
return nil, err
}
txNum = txNumNextBlock
}
} else {
blockNumber, ok, err = api._blockReader.EventLookup(ctx, tx, txHash)
}
Expand All @@ -115,11 +108,11 @@ func (api *TraceAPIImpl) Transaction(ctx context.Context, txHash common.Hash, ga
return nil, err
}

if txNumMin+2 > txNum {
if txNumMin+1 > txNum && !isBorStateSyncTxn {
return nil, fmt.Errorf("uint underflow txnums error txNum: %d, txNumMin: %d, blockNum: %d", txNum, txNumMin, blockNumber)
}

var txIndex = int(txNum - txNumMin - 2)
var txIndex = int(txNum - txNumMin - 1)

if isBorStateSyncTxn {
txIndex = -1
Expand Down
2 changes: 1 addition & 1 deletion turbo/snapshotsync/freezeblocks/block_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -1129,7 +1129,7 @@ func (r *BlockReader) txnByHash(txnHash common.Hash, segments []*snapshotsync.Vi

// final txnHash check - completely avoid false-positives
if txn.Hash() == txnHash {
return txn, blockNum, idxTxnHash.BaseDataID() + txNumInFile + 1, true, nil
return txn, blockNum, idxTxnHash.BaseDataID() + txNumInFile, true, nil
}
}

Expand Down
Loading