Skip to content

Commit 6952fe3

Browse files
authored
Merge pull request ethereum#3100 from kobigurk/develop
internal/ethapi, internal/web3ext: adds raw tx retrieval methods
2 parents b19b7c3 + 1a6682c commit 6952fe3

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

internal/ethapi/api.go

+42-1
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,7 @@ func (s *PublicBlockChainAPI) rpcOutputBlock(b *types.Block, inclTx bool, fullTx
597597
"gasUsed": rpc.NewHexNumber(head.GasUsed),
598598
"timestamp": rpc.NewHexNumber(head.Time),
599599
"transactionsRoot": head.TxHash,
600-
"receiptsRoot": head.ReceiptHash,
600+
"receiptsRoot": head.ReceiptHash,
601601
}
602602

603603
if inclTx {
@@ -699,6 +699,16 @@ func newRPCTransactionFromBlockIndex(b *types.Block, txIndex int) (*RPCTransacti
699699
return nil, nil
700700
}
701701

702+
// newRPCRawTransactionFromBlockIndex returns the bytes of a transaction given a block and a transaction index.
703+
func newRPCRawTransactionFromBlockIndex(b *types.Block, txIndex int) (rpc.HexBytes, error) {
704+
if txIndex >= 0 && txIndex < len(b.Transactions()) {
705+
tx := b.Transactions()[txIndex]
706+
return rlp.EncodeToBytes(tx)
707+
}
708+
709+
return nil, nil
710+
}
711+
702712
// newRPCTransaction returns a transaction that will serialize to the RPC representation.
703713
func newRPCTransaction(b *types.Block, txHash common.Hash) (*RPCTransaction, error) {
704714
for idx, tx := range b.Transactions() {
@@ -770,6 +780,22 @@ func (s *PublicTransactionPoolAPI) GetTransactionByBlockHashAndIndex(ctx context
770780
return nil, nil
771781
}
772782

783+
// GetRawTransactionByBlockNumberAndIndex returns the bytes of the transaction for the given block number and index.
784+
func (s *PublicTransactionPoolAPI) GetRawTransactionByBlockNumberAndIndex(ctx context.Context, blockNr rpc.BlockNumber, index rpc.HexNumber) (rpc.HexBytes, error) {
785+
if block, _ := s.b.BlockByNumber(ctx, blockNr); block != nil {
786+
return newRPCRawTransactionFromBlockIndex(block, index.Int())
787+
}
788+
return nil, nil
789+
}
790+
791+
// GetRawTransactionByBlockHashAndIndex returns the bytes of the transaction for the given block hash and index.
792+
func (s *PublicTransactionPoolAPI) GetRawTransactionByBlockHashAndIndex(ctx context.Context, blockHash common.Hash, index rpc.HexNumber) (rpc.HexBytes, error) {
793+
if block, _ := s.b.GetBlock(ctx, blockHash); block != nil {
794+
return newRPCRawTransactionFromBlockIndex(block, index.Int())
795+
}
796+
return nil, nil
797+
}
798+
773799
// GetTransactionCount returns the number of transactions the given address has sent for the given block number
774800
func (s *PublicTransactionPoolAPI) GetTransactionCount(ctx context.Context, address common.Address, blockNr rpc.BlockNumber) (*rpc.HexNumber, error) {
775801
state, _, err := s.b.StateAndHeaderByNumber(blockNr)
@@ -835,6 +861,21 @@ func (s *PublicTransactionPoolAPI) GetTransactionByHash(ctx context.Context, txH
835861
return nil, nil
836862
}
837863

864+
// GetRawTransactionByHash returns the bytes of the transaction for the given hash.
865+
func (s *PublicTransactionPoolAPI) GetRawTransactionByHash(ctx context.Context, txHash common.Hash) (rpc.HexBytes, error) {
866+
var tx *types.Transaction
867+
var err error
868+
869+
if tx, _, err = getTransaction(s.b.ChainDb(), s.b, txHash); err != nil {
870+
glog.V(logger.Debug).Infof("%v\n", err)
871+
return nil, nil
872+
} else if tx == nil {
873+
return nil, nil
874+
}
875+
876+
return rlp.EncodeToBytes(tx)
877+
}
878+
838879
// GetTransactionReceipt returns the transaction receipt for the given transaction hash.
839880
func (s *PublicTransactionPoolAPI) GetTransactionReceipt(txHash common.Hash) (map[string]interface{}, error) {
840881
receipt := core.GetReceipt(s.b.ChainDb(), txHash)

internal/web3ext/web3ext.go

+13
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,19 @@ web3._extend({
468468
call: 'eth_submitTransaction',
469469
params: 1,
470470
inputFormatter: [web3._extend.formatters.inputTransactionFormatter]
471+
}),
472+
new web3._extend.Method({
473+
name: 'getRawTransaction',
474+
call: 'eth_getRawTransactionByHash',
475+
params: 1
476+
}),
477+
new web3._extend.Method({
478+
name: 'getRawTransactionFromBlock',
479+
call: function(args) {
480+
return (web3._extend.utils.isString(args[0]) && args[0].indexOf('0x') === 0) ? 'eth_getRawTransactionByBlockHashAndIndex' : 'eth_getRawTransactionByBlockNumberAndIndex';
481+
},
482+
params: 2,
483+
inputFormatter: [web3._extend.formatters.inputBlockNumberFormatter, web3._extend.utils.toHex]
471484
})
472485
],
473486
properties:

0 commit comments

Comments
 (0)