good morning!!!!

Skip to content
Snippets Groups Projects
Unverified Commit fc196d4b authored by Alex Sharov's avatar Alex Sharov Committed by GitHub
Browse files

pending txs methods (#1869)

parent 8c9a76d3
Branches
Tags
No related merge requests found
......@@ -121,8 +121,11 @@ The following table shows the current implementation status of turbo-geth's RPC
| eth_getUncleCountByBlockNumber | Yes | |
| | | |
| eth_getTransactionByHash | Yes | |
| eth_getRawTransactionByHash | Yes | |
| eth_getTransactionByBlockHashAndIndex | Yes | |
| eth_retRawTransactionByBlockHashAndIndex| Yes | |
| eth_getTransactionByBlockNumberAndIndex | Yes | |
| eth_retRawTransactionByBlockNumberAndIndex| Yes | |
| eth_getTransactionReceipt | Yes | |
| eth_getBlockReceipts | Yes | |
| | | |
......
package commands
import (
"bytes"
"context"
"math/big"
"sync"
......@@ -34,6 +35,9 @@ type EthAPI interface {
GetTransactionByHash(ctx context.Context, hash common.Hash) (*RPCTransaction, error)
GetTransactionByBlockHashAndIndex(ctx context.Context, blockHash common.Hash, txIndex hexutil.Uint64) (*RPCTransaction, error)
GetTransactionByBlockNumberAndIndex(ctx context.Context, blockNr rpc.BlockNumber, txIndex hexutil.Uint) (*RPCTransaction, error)
GetRawTransactionByBlockNumberAndIndex(ctx context.Context, blockNr rpc.BlockNumber, index hexutil.Uint) (hexutil.Bytes, error)
GetRawTransactionByBlockHashAndIndex(ctx context.Context, blockHash common.Hash, index hexutil.Uint) (hexutil.Bytes, error)
GetRawTransactionByHash(ctx context.Context, hash common.Hash) (hexutil.Bytes, error)
// Receipt related (see ./eth_receipts.go)
GetTransactionReceipt(ctx context.Context, hash common.Hash) (map[string]interface{}, error)
......@@ -230,3 +234,19 @@ func newRPCTransaction(tx types.Transaction, blockHash common.Hash, blockNumber
}
return result
}
// newRPCPendingTransaction returns a pending transaction that will serialize to the RPC representation
func newRPCPendingTransaction(tx types.Transaction) *RPCTransaction {
return newRPCTransaction(tx, common.Hash{}, 0, 0)
}
// newRPCRawTransactionFromBlockIndex returns the bytes of a transaction given a block and a transaction index.
func newRPCRawTransactionFromBlockIndex(b *types.Block, index uint64) (hexutil.Bytes, error) {
txs := b.Transactions()
if index >= uint64(len(txs)) {
return nil, nil
}
var buf bytes.Buffer
err := txs[index].MarshalBinary(&buf)
return buf.Bytes(), err
}
package commands
import (
"bytes"
"context"
"fmt"
"github.com/ledgerwatch/turbo-geth/common"
"github.com/ledgerwatch/turbo-geth/common/hexutil"
"github.com/ledgerwatch/turbo-geth/core/rawdb"
types2 "github.com/ledgerwatch/turbo-geth/core/types"
"github.com/ledgerwatch/turbo-geth/gointerfaces"
"github.com/ledgerwatch/turbo-geth/gointerfaces/txpool"
"github.com/ledgerwatch/turbo-geth/gointerfaces/types"
"github.com/ledgerwatch/turbo-geth/rpc"
)
......@@ -23,9 +28,57 @@ func (api *APIImpl) GetTransactionByHash(ctx context.Context, hash common.Hash)
if txn == nil {
return nil, nil // not error, see https://github.com/ledgerwatch/turbo-geth/issues/1645
}
if tx != nil {
return newRPCTransaction(txn, blockHash, blockNumber, txIndex), nil
}
// No finalized transaction, try to retrieve it from the pool
reply, err := api.txPool.Transactions(ctx, &txpool.TransactionsRequest{Hashes: []*types.H256{gointerfaces.ConvertHashToH256(hash)}})
if err != nil {
return nil, err
}
if reply.RlpTxs[0] != nil {
txn, err = types2.UnmarshalTransactionFromBinary(reply.RlpTxs[0])
if err != nil {
return nil, err
}
return newRPCPendingTransaction(txn), nil
}
// Transaction unknown, return as such
return nil, nil
}
// GetRawTransactionByHash returns the bytes of the transaction for the given hash.
func (api *APIImpl) GetRawTransactionByHash(ctx context.Context, hash common.Hash) (hexutil.Bytes, error) {
tx, err := api.db.BeginRo(ctx)
if err != nil {
return nil, err
}
defer tx.Rollback()
// https://infura.io/docs/ethereum/json-rpc/eth-getTransactionByHash
txn, _, _, _ := rawdb.ReadTransaction(tx, hash)
if txn == nil {
return nil, nil // not error, see https://github.com/ledgerwatch/turbo-geth/issues/1645
}
if tx != nil {
var buf bytes.Buffer
err = txn.MarshalBinary(&buf)
return buf.Bytes(), err
}
// No finalized transaction, try to retrieve it from the pool
reply, err := api.txPool.Transactions(ctx, &txpool.TransactionsRequest{Hashes: []*types.H256{gointerfaces.ConvertHashToH256(hash)}})
if err != nil {
return nil, err
}
if reply.RlpTxs[0] != nil {
return reply.RlpTxs[0], nil
}
return nil, nil
}
// GetTransactionByBlockHashAndIndex implements eth_getTransactionByBlockHashAndIndex. Returns information about a transaction given the block's hash and a transaction index.
func (api *APIImpl) GetTransactionByBlockHashAndIndex(ctx context.Context, blockHash common.Hash, txIndex hexutil.Uint64) (*RPCTransaction, error) {
tx, err := api.db.BeginRo(ctx)
......@@ -51,6 +104,26 @@ func (api *APIImpl) GetTransactionByBlockHashAndIndex(ctx context.Context, block
return newRPCTransaction(txs[txIndex], block.Hash(), block.NumberU64(), uint64(txIndex)), nil
}
// GetRawTransactionByBlockHashAndIndex returns the bytes of the transaction for the given block hash and index.
func (api *APIImpl) GetRawTransactionByBlockHashAndIndex(ctx context.Context, blockHash common.Hash, index hexutil.Uint) (hexutil.Bytes, error) {
tx, err := api.db.BeginRo(ctx)
if err != nil {
return nil, err
}
defer tx.Rollback()
// https://infura.io/docs/ethereum/json-rpc/eth-getRawTransactionByBlockHashAndIndex
block, err := rawdb.ReadBlockByHash(tx, blockHash)
if err != nil {
return nil, err
}
if block == nil {
return nil, nil // not error, see https://github.com/ledgerwatch/turbo-geth/issues/1645
}
return newRPCRawTransactionFromBlockIndex(block, uint64(index))
}
// GetTransactionByBlockNumberAndIndex implements eth_getTransactionByBlockNumberAndIndex. Returns information about a transaction given a block number and transaction index.
func (api *APIImpl) GetTransactionByBlockNumberAndIndex(ctx context.Context, blockNr rpc.BlockNumber, txIndex hexutil.Uint) (*RPCTransaction, error) {
tx, err := api.db.BeginRo(ctx)
......@@ -80,3 +153,28 @@ func (api *APIImpl) GetTransactionByBlockNumberAndIndex(ctx context.Context, blo
return newRPCTransaction(txs[txIndex], block.Hash(), block.NumberU64(), uint64(txIndex)), nil
}
// GetRawTransactionByBlockNumberAndIndex returns the bytes of the transaction for the given block number and index.
func (api *APIImpl) GetRawTransactionByBlockNumberAndIndex(ctx context.Context, blockNr rpc.BlockNumber, index hexutil.Uint) (hexutil.Bytes, error) {
tx, err := api.db.BeginRo(ctx)
if err != nil {
return nil, err
}
defer tx.Rollback()
// https://infura.io/docs/ethereum/json-rpc/eth-getRawTransactionByBlockNumberAndIndex
blockNum, err := getBlockNumber(blockNr, tx)
if err != nil {
return nil, err
}
block, err := rawdb.ReadBlockByNumber(tx, blockNum)
if err != nil {
return nil, err
}
if block == nil {
return nil, nil // not error, see https://github.com/ledgerwatch/turbo-geth/issues/1645
}
return newRPCRawTransactionFromBlockIndex(block, uint64(index))
}
......@@ -189,6 +189,37 @@ func UnmarshalTransactionFromBinary(data []byte) (Transaction, error) {
}
}
func MarshalTransactionsBinary(txs Transactions) ([][]byte, error) {
var err error
var buf bytes.Buffer
result := make([][]byte, len(txs))
for i := range txs {
if txs[i] == nil {
result[i] = nil
continue
}
buf.Reset()
err = txs[i].MarshalBinary(&buf)
if err != nil {
return nil, err
}
result[i] = common.CopyBytes(buf.Bytes())
}
return result, nil
}
func UnmarshalTransactionsFromBinary(txs [][]byte) ([]Transaction, error) {
result := make([]Transaction, len(txs))
var err error
for i := range txs {
result[i], err = UnmarshalTransactionFromBinary(txs[i])
if err != nil {
return nil, err
}
}
return result, nil
}
func sanityCheckSignature(v *uint256.Int, r *uint256.Int, s *uint256.Int, maybeProtected bool) error {
if isProtectedV(v) && !maybeProtected {
return ErrUnexpectedProtection
......
......@@ -53,7 +53,7 @@ func (s *TxPoolServer) FindUnknown(ctx context.Context, in *proto_txpool.TxHashe
func (s *TxPoolServer) Add(ctx context.Context, in *proto_txpool.AddRequest) (*proto_txpool.AddReply, error) {
reply := &proto_txpool.AddReply{Imported: make([]proto_txpool.ImportResult, len(in.RlpTxs)), Errors: make([]string, len(in.RlpTxs))}
txs, err := UnmarshalTxs(in.RlpTxs)
txs, err := types.UnmarshalTransactionsFromBinary(in.RlpTxs)
if err != nil {
return nil, err
}
......@@ -120,34 +120,3 @@ func (s *TxPoolServer) Transactions(ctx context.Context, in *proto_txpool.Transa
return reply, nil
}
func MarshalTxs(txs types.Transactions) ([][]byte, error) {
var err error
var buf bytes.Buffer
result := make([][]byte, len(txs))
for i := range txs {
if txs[i] == nil {
result[i] = nil
continue
}
buf.Reset()
err = txs[i].MarshalBinary(&buf)
if err != nil {
return nil, err
}
result[i] = common.CopyBytes(buf.Bytes())
}
return result, nil
}
func UnmarshalTxs(txs [][]byte) ([]types.Transaction, error) {
result := make([]types.Transaction, len(txs))
var err error
for i := range txs {
result[i], err = types.UnmarshalTransactionFromBinary(txs[i])
if err != nil {
return nil, err
}
}
return result, nil
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment