internal/ethapi: return null for missing raw transactions

Change GetRawTransactionByHash, GetRawTransactionByBlockNumberAndIndex,
GetRawTransactionByBlockHashAndIndex and the helper
newRPCRawTransactionFromBlockIndex to return *hexutil.Bytes instead of
hexutil.Bytes so that a nil pointer serialises to JSON null rather than
"0x" when the requested transaction is not found.
This commit is contained in:
ManuelArto 2026-06-25 17:48:26 +02:00
parent 4387f433c9
commit bd40458a1f

View file

@ -1220,13 +1220,14 @@ func newRPCTransactionFromBlockIndex(b *types.Block, index uint64, config *param
} }
// newRPCRawTransactionFromBlockIndex returns the bytes of a transaction given a block and a transaction index. // newRPCRawTransactionFromBlockIndex returns the bytes of a transaction given a block and a transaction index.
func newRPCRawTransactionFromBlockIndex(b *types.Block, index uint64) hexutil.Bytes { func newRPCRawTransactionFromBlockIndex(b *types.Block, index uint64) *hexutil.Bytes {
txs := b.Transactions() txs := b.Transactions()
if index >= uint64(len(txs)) { if index >= uint64(len(txs)) {
return nil return nil
} }
blob, _ := txs[index].MarshalBinary() blob, _ := txs[index].MarshalBinary()
return blob b2 := hexutil.Bytes(blob)
return &b2
} }
// accessListResult returns an optional accesslist // accessListResult returns an optional accesslist
@ -1485,7 +1486,7 @@ func (api *TransactionAPI) GetTransactionByBlockHashAndIndex(ctx context.Context
} }
// GetRawTransactionByBlockNumberAndIndex returns the bytes of the transaction for the given block number and index. // GetRawTransactionByBlockNumberAndIndex returns the bytes of the transaction for the given block number and index.
func (api *TransactionAPI) GetRawTransactionByBlockNumberAndIndex(ctx context.Context, blockNr rpc.BlockNumber, index hexutil.Uint) hexutil.Bytes { func (api *TransactionAPI) GetRawTransactionByBlockNumberAndIndex(ctx context.Context, blockNr rpc.BlockNumber, index hexutil.Uint) *hexutil.Bytes {
if block, _ := api.b.BlockByNumber(ctx, blockNr); block != nil { if block, _ := api.b.BlockByNumber(ctx, blockNr); block != nil {
return newRPCRawTransactionFromBlockIndex(block, uint64(index)) return newRPCRawTransactionFromBlockIndex(block, uint64(index))
} }
@ -1493,7 +1494,7 @@ func (api *TransactionAPI) GetRawTransactionByBlockNumberAndIndex(ctx context.Co
} }
// GetRawTransactionByBlockHashAndIndex returns the bytes of the transaction for the given block hash and index. // GetRawTransactionByBlockHashAndIndex returns the bytes of the transaction for the given block hash and index.
func (api *TransactionAPI) GetRawTransactionByBlockHashAndIndex(ctx context.Context, blockHash common.Hash, index hexutil.Uint) hexutil.Bytes { func (api *TransactionAPI) GetRawTransactionByBlockHashAndIndex(ctx context.Context, blockHash common.Hash, index hexutil.Uint) *hexutil.Bytes {
if block, _ := api.b.BlockByHash(ctx, blockHash); block != nil { if block, _ := api.b.BlockByHash(ctx, blockHash); block != nil {
return newRPCRawTransactionFromBlockIndex(block, uint64(index)) return newRPCRawTransactionFromBlockIndex(block, uint64(index))
} }
@ -1545,12 +1546,17 @@ func (api *TransactionAPI) GetTransactionByHash(ctx context.Context, hash common
} }
// GetRawTransactionByHash returns the bytes of the transaction for the given hash. // GetRawTransactionByHash returns the bytes of the transaction for the given hash.
func (api *TransactionAPI) GetRawTransactionByHash(ctx context.Context, hash common.Hash) (hexutil.Bytes, error) { func (api *TransactionAPI) GetRawTransactionByHash(ctx context.Context, hash common.Hash) (*hexutil.Bytes, error) {
// Retrieve a finalized transaction, or a pooled otherwise // Retrieve a finalized transaction, or a pooled otherwise
found, tx, _, _, _ := api.b.GetCanonicalTransaction(hash) found, tx, _, _, _ := api.b.GetCanonicalTransaction(hash)
if !found { if !found {
if tx = api.b.GetPoolTransaction(hash); tx != nil { if tx = api.b.GetPoolTransaction(hash); tx != nil {
return tx.MarshalBinary() blob, err := tx.MarshalBinary()
if err != nil {
return nil, err
}
b := hexutil.Bytes(blob)
return &b, nil
} }
// If also not in the pool there is a chance the tx indexer is still in progress. // If also not in the pool there is a chance the tx indexer is still in progress.
if !api.b.TxIndexDone() { if !api.b.TxIndexDone() {
@ -1559,7 +1565,12 @@ func (api *TransactionAPI) GetRawTransactionByHash(ctx context.Context, hash com
// If the transaction is not found in the pool and the indexer is done, return nil // If the transaction is not found in the pool and the indexer is done, return nil
return nil, nil return nil, nil
} }
return tx.MarshalBinary() blob, err := tx.MarshalBinary()
if err != nil {
return nil, err
}
b := hexutil.Bytes(blob)
return &b, nil
} }
// GetTransactionReceipt returns the transaction receipt for the given transaction hash. // GetTransactionReceipt returns the transaction receipt for the given transaction hash.