From 200ddf71ef92496ffcc66e668c8c18bdff6f06b6 Mon Sep 17 00:00:00 2001 From: JkLondon Date: Wed, 5 Mar 2025 19:14:36 +0100 Subject: [PATCH 1/5] commit --- turbo/jsonrpc/debug_api.go | 14 +++++--------- turbo/jsonrpc/eth_receipts.go | 29 ++++++++++++----------------- turbo/jsonrpc/eth_txs.go | 31 +++++++++++-------------------- turbo/jsonrpc/trace_adhoc.go | 9 +-------- turbo/jsonrpc/trace_filtering.go | 9 +-------- 5 files changed, 30 insertions(+), 62 deletions(-) diff --git a/turbo/jsonrpc/debug_api.go b/turbo/jsonrpc/debug_api.go index d867bb659da..34509704853 100644 --- a/turbo/jsonrpc/debug_api.go +++ b/turbo/jsonrpc/debug_api.go @@ -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. + isBorStateSyncTx := false if blockNum == 0 && chainConfig.Bor != nil { 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) } @@ -529,6 +523,8 @@ func (api *PrivateDebugAPIImpl) GetRawTransaction(ctx context.Context, txnHash c if err != nil { return nil, err } + + isBorStateSyncTx = true } if !ok { @@ -540,11 +536,11 @@ func (api *PrivateDebugAPIImpl) GetRawTransaction(ctx context.Context, txnHash c return nil, err } - if txNumMin+2 > txNum { + if txNumMin+2 > 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 - 2 txn, err := api._txnReader.TxnByIdxInBlock(ctx, tx, blockNum, int(txnIndex)) if err != nil { diff --git a/turbo/jsonrpc/eth_receipts.go b/turbo/jsonrpc/eth_receipts.go index 2e8cb6fe919..afd5459dd25 100644 --- a/turbo/jsonrpc/eth_receipts.go +++ b/turbo/jsonrpc/eth_receipts.go @@ -470,19 +470,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 { + isStateSync := blockNum == 0 && chainConfig.Bor != nil + + if isStateSync { 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) } @@ -495,7 +490,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+2 > txNum && !isStateSync { //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) } @@ -504,14 +499,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 isStateSync { block, err := api.blockByNumberWithSenders(ctx, tx, blockNum) if err != nil { return nil, err @@ -537,6 +525,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 - 2) + + 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) diff --git a/turbo/jsonrpc/eth_txs.go b/turbo/jsonrpc/eth_txs.go index 3de286fc931..0703280ed8d 100644 --- a/turbo/jsonrpc/eth_txs.go +++ b/turbo/jsonrpc/eth_txs.go @@ -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 { + isStateSyncTx := blockNum == 0 && chainConfig.Bor != nil + if isStateSyncTx { 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) } @@ -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+2 > txNum && !isStateSyncTx { //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) } - 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 @@ -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 isStateSyncTx { borTx := bortypes.NewBorTransaction() _, txCount, err := api._blockReader.Body(ctx, tx, blockHash, blockNum) if err != nil { @@ -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 - 2 + + 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 } diff --git a/turbo/jsonrpc/trace_adhoc.go b/turbo/jsonrpc/trace_adhoc.go index bdcfc82e3e7..54b0944fa71 100644 --- a/turbo/jsonrpc/trace_adhoc.go +++ b/turbo/jsonrpc/trace_adhoc.go @@ -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) } @@ -864,7 +857,7 @@ func (api *TraceAPIImpl) ReplayTransaction(ctx context.Context, txHash libcommon return nil, err } - if txNumMin+2 > txNum { + if txNumMin+2 > txNum && !isBorStateSyncTxn { return nil, fmt.Errorf("uint underflow txnums error txNum: %d, txNumMin: %d, blockNum: %d", txNum, txNumMin, blockNum) } diff --git a/turbo/jsonrpc/trace_filtering.go b/turbo/jsonrpc/trace_filtering.go index e9022f58776..2a2718e9c3f 100644 --- a/turbo/jsonrpc/trace_filtering.go +++ b/turbo/jsonrpc/trace_filtering.go @@ -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) } @@ -115,7 +108,7 @@ func (api *TraceAPIImpl) Transaction(ctx context.Context, txHash common.Hash, ga return nil, err } - if txNumMin+2 > txNum { + if txNumMin+2 > txNum && !isBorStateSyncTxn { return nil, fmt.Errorf("uint underflow txnums error txNum: %d, txNumMin: %d, blockNum: %d", txNum, txNumMin, blockNumber) } From db4130e75f3c25ee6ea3c5a64ab83d13374d9c13 Mon Sep 17 00:00:00 2001 From: "alex.sharov" Date: Thu, 6 Mar 2025 10:32:28 +0700 Subject: [PATCH 2/5] save --- turbo/jsonrpc/eth_receipts.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/turbo/jsonrpc/eth_receipts.go b/turbo/jsonrpc/eth_receipts.go index 2e8cb6fe919..a940e288d88 100644 --- a/turbo/jsonrpc/eth_receipts.go +++ b/turbo/jsonrpc/eth_receipts.go @@ -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 From f621c346aa9f53a8e4e4258f1a29f19710079fad Mon Sep 17 00:00:00 2001 From: JkLondon Date: Thu, 6 Mar 2025 09:37:16 +0100 Subject: [PATCH 3/5] commit --- core/rawdb/accessors_indexes.go | 2 +- turbo/jsonrpc/debug_api.go | 4 ++-- turbo/jsonrpc/eth_receipts.go | 10 +++++----- turbo/jsonrpc/eth_txs.go | 10 +++++----- turbo/jsonrpc/trace_adhoc.go | 4 ++-- turbo/jsonrpc/trace_filtering.go | 4 ++-- 6 files changed, 17 insertions(+), 17 deletions(-) diff --git a/core/rawdb/accessors_indexes.go b/core/rawdb/accessors_indexes.go index 6a4d5bdb537..b276b94a721 100644 --- a/core/rawdb/accessors_indexes.go +++ b/core/rawdb/accessors_indexes.go @@ -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 } diff --git a/turbo/jsonrpc/debug_api.go b/turbo/jsonrpc/debug_api.go index 34509704853..72d6d8e0bb0 100644 --- a/turbo/jsonrpc/debug_api.go +++ b/turbo/jsonrpc/debug_api.go @@ -536,11 +536,11 @@ func (api *PrivateDebugAPIImpl) GetRawTransaction(ctx context.Context, txnHash c return nil, err } - if txNumMin+2 > txNum && !isBorStateSyncTx { + if txNumMin+1 > txNum && !isBorStateSyncTx { return nil, fmt.Errorf("uint underflow txnums error txNum: %d, txNumMin: %d, blockNum: %d", txNum, txNumMin, blockNum) } - var txnIndex = txNum - txNumMin - 2 + var txnIndex = txNum - txNumMin - 1 txn, err := api._txnReader.TxnByIdxInBlock(ctx, tx, blockNum, int(txnIndex)) if err != nil { diff --git a/turbo/jsonrpc/eth_receipts.go b/turbo/jsonrpc/eth_receipts.go index afd5459dd25..ecc40463bd0 100644 --- a/turbo/jsonrpc/eth_receipts.go +++ b/turbo/jsonrpc/eth_receipts.go @@ -470,9 +470,9 @@ func (api *APIImpl) GetTransactionReceipt(ctx context.Context, txnHash common.Ha } // Private API returns 0 if transaction is not found. - isStateSync := blockNum == 0 && chainConfig.Bor != nil + isBorStateSyncTxn := blockNum == 0 && chainConfig.Bor != nil - if isStateSync { + if isBorStateSyncTxn { if api.useBridgeReader { blockNum, ok, err = api.bridgeReader.EventTxnLookup(ctx, txnHash) if err != nil { @@ -490,7 +490,7 @@ func (api *APIImpl) GetTransactionReceipt(ctx context.Context, txnHash common.Ha return nil, nil } - if txNumMin+2 > txNum && !isStateSync { //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) } @@ -499,7 +499,7 @@ func (api *APIImpl) GetTransactionReceipt(ctx context.Context, txnHash common.Ha return nil, err } - if isStateSync { + if isBorStateSyncTxn { block, err := api.blockByNumberWithSenders(ctx, tx, blockNum) if err != nil { return nil, err @@ -525,7 +525,7 @@ 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 - 2) + var txnIndex = int(txNum - txNumMin - 1) txn, err := api._blockReader.TxnByIdxInBlock(ctx, tx, header.Number.Uint64(), txnIndex) if err != nil { diff --git a/turbo/jsonrpc/eth_txs.go b/turbo/jsonrpc/eth_txs.go index 0703280ed8d..386005b5fca 100644 --- a/turbo/jsonrpc/eth_txs.go +++ b/turbo/jsonrpc/eth_txs.go @@ -58,8 +58,8 @@ 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. - isStateSyncTx := blockNum == 0 && chainConfig.Bor != nil - if isStateSyncTx { + isBorStateSyncTxn := blockNum == 0 && chainConfig.Bor != nil + if isBorStateSyncTxn { if api.useBridgeReader { blockNum, ok, err = api.bridgeReader.EventTxnLookup(ctx, txnHash) } else { @@ -76,7 +76,7 @@ func (api *APIImpl) GetTransactionByHash(ctx context.Context, txnHash common.Has return nil, err } - if txNumMin+2 > txNum && !isStateSyncTx { //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) } @@ -97,7 +97,7 @@ func (api *APIImpl) GetTransactionByHash(ctx context.Context, txnHash common.Has } // if no transaction was found then we return nil - if isStateSyncTx { + if isBorStateSyncTxn { borTx := bortypes.NewBorTransaction() _, txCount, err := api._blockReader.Body(ctx, tx, blockHash, blockNum) if err != nil { @@ -106,7 +106,7 @@ 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 - 2 + var txnIndex = txNum - txNumMin - 1 txn, err := api._txnReader.TxnByIdxInBlock(ctx, tx, blockNum, int(txnIndex)) if err != nil { diff --git a/turbo/jsonrpc/trace_adhoc.go b/turbo/jsonrpc/trace_adhoc.go index 54b0944fa71..e3a3208d483 100644 --- a/turbo/jsonrpc/trace_adhoc.go +++ b/turbo/jsonrpc/trace_adhoc.go @@ -857,11 +857,11 @@ func (api *TraceAPIImpl) ReplayTransaction(ctx context.Context, txHash libcommon return nil, err } - if txNumMin+2 > txNum && !isBorStateSyncTxn { + 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 diff --git a/turbo/jsonrpc/trace_filtering.go b/turbo/jsonrpc/trace_filtering.go index 2a2718e9c3f..a9450de8554 100644 --- a/turbo/jsonrpc/trace_filtering.go +++ b/turbo/jsonrpc/trace_filtering.go @@ -108,11 +108,11 @@ func (api *TraceAPIImpl) Transaction(ctx context.Context, txHash common.Hash, ga return nil, err } - if txNumMin+2 > txNum && !isBorStateSyncTxn { + 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 From 46e89747b5ba7d64199d679de9516c338fa834a2 Mon Sep 17 00:00:00 2001 From: JkLondon Date: Thu, 6 Mar 2025 09:56:13 +0100 Subject: [PATCH 4/5] commit --- turbo/jsonrpc/debug_api.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/turbo/jsonrpc/debug_api.go b/turbo/jsonrpc/debug_api.go index 72d6d8e0bb0..430e7b1f41c 100644 --- a/turbo/jsonrpc/debug_api.go +++ b/turbo/jsonrpc/debug_api.go @@ -512,8 +512,8 @@ 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. - isBorStateSyncTx := false - 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) } else { @@ -523,8 +523,6 @@ func (api *PrivateDebugAPIImpl) GetRawTransaction(ctx context.Context, txnHash c if err != nil { return nil, err } - - isBorStateSyncTx = true } if !ok { From b9c5b790053a24c53d1ee01401d8e7fc6c0e367d Mon Sep 17 00:00:00 2001 From: JkLondon Date: Thu, 6 Mar 2025 13:34:42 +0100 Subject: [PATCH 5/5] commit --- core/rawdb/accessors_indexes_test.go | 2 +- turbo/snapshotsync/freezeblocks/block_reader.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/core/rawdb/accessors_indexes_test.go b/core/rawdb/accessors_indexes_test.go index a0ba8bb95f4..17141aabcaf 100644 --- a/core/rawdb/accessors_indexes_test.go +++ b/core/rawdb/accessors_indexes_test.go @@ -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) } } diff --git a/turbo/snapshotsync/freezeblocks/block_reader.go b/turbo/snapshotsync/freezeblocks/block_reader.go index 98e93495d22..9e07cadebb8 100644 --- a/turbo/snapshotsync/freezeblocks/block_reader.go +++ b/turbo/snapshotsync/freezeblocks/block_reader.go @@ -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 } }