rawdb: don't decode the full block body in ReadTransaction

This commit is contained in:
Ömer Faruk IRMAK 2025-06-11 00:14:09 +03:00
parent ece4b48d84
commit de7fe6a24a
2 changed files with 41 additions and 6 deletions

View file

@ -24,6 +24,7 @@ import (
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/params" "github.com/ethereum/go-ethereum/params"
@ -139,15 +140,41 @@ func ReadTransaction(db ethdb.Reader, hash common.Hash) (*types.Transaction, com
if blockHash == (common.Hash{}) { if blockHash == (common.Hash{}) {
return nil, common.Hash{}, 0, 0 return nil, common.Hash{}, 0, 0
} }
body := ReadBody(db, blockHash, *blockNumber) bodyRLP := ReadBodyRLP(db, blockHash, *blockNumber)
if body == nil { if bodyRLP == nil {
log.Error("Transaction referenced missing", "number", *blockNumber, "hash", blockHash) log.Error("Transaction referenced missing", "number", *blockNumber, "hash", blockHash)
return nil, common.Hash{}, 0, 0 return nil, common.Hash{}, 0, 0
} }
for txIndex, tx := range body.Transactions { txnListRLP, _, err := rlp.SplitList(bodyRLP)
if tx.Hash() == hash { if err != nil {
return tx, blockHash, *blockNumber, uint64(txIndex) return nil, common.Hash{}, 0, 0
}
txnIterator, err := rlp.NewListIterator(txnListRLP)
if err != nil {
return nil, common.Hash{}, 0, 0
}
txIndex := 0
for txnIterator.Next() && txnIterator.Err() == nil {
txnRLP := txnIterator.Value()
// Figure out if this a legacy transaction or not
rlpKind, hashPreimage, _, err := rlp.Split(txnRLP)
if err != nil {
return nil, common.Hash{}, 0, 0
} }
if rlpKind == rlp.List {
hashPreimage = txnRLP
}
if crypto.Keccak256Hash(hashPreimage) == hash {
var tx types.Transaction
if err := rlp.DecodeBytes(txnRLP, &tx); err != nil {
return nil, common.Hash{}, 0, 0
}
return &tx, blockHash, *blockNumber, uint64(txIndex)
}
txIndex++
} }
log.Error("Transaction not found", "number", *blockNumber, "hash", blockHash, "txhash", hash) log.Error("Transaction not found", "number", *blockNumber, "hash", blockHash, "txhash", hash)
return nil, common.Hash{}, 0, 0 return nil, common.Hash{}, 0, 0

View file

@ -72,7 +72,15 @@ func TestLookupStorage(t *testing.T) {
tx1 := types.NewTransaction(1, common.BytesToAddress([]byte{0x11}), big.NewInt(111), 1111, big.NewInt(11111), []byte{0x11, 0x11, 0x11}) tx1 := types.NewTransaction(1, common.BytesToAddress([]byte{0x11}), big.NewInt(111), 1111, big.NewInt(11111), []byte{0x11, 0x11, 0x11})
tx2 := types.NewTransaction(2, common.BytesToAddress([]byte{0x22}), big.NewInt(222), 2222, big.NewInt(22222), []byte{0x22, 0x22, 0x22}) tx2 := types.NewTransaction(2, common.BytesToAddress([]byte{0x22}), big.NewInt(222), 2222, big.NewInt(22222), []byte{0x22, 0x22, 0x22})
tx3 := types.NewTransaction(3, common.BytesToAddress([]byte{0x33}), big.NewInt(333), 3333, big.NewInt(33333), []byte{0x33, 0x33, 0x33}) tx3 := types.NewTransaction(3, common.BytesToAddress([]byte{0x33}), big.NewInt(333), 3333, big.NewInt(33333), []byte{0x33, 0x33, 0x33})
txs := []*types.Transaction{tx1, tx2, tx3} tx4 := types.NewTx(&types.DynamicFeeTx{
To: new(common.Address),
Nonce: 5,
Value: big.NewInt(5),
Gas: 5,
GasTipCap: big.NewInt(55),
GasFeeCap: big.NewInt(1055),
})
txs := []*types.Transaction{tx1, tx2, tx3, tx4}
block := types.NewBlock(&types.Header{Number: big.NewInt(314)}, &types.Body{Transactions: txs}, nil, newTestHasher()) block := types.NewBlock(&types.Header{Number: big.NewInt(314)}, &types.Body{Transactions: txs}, nil, newTestHasher())