Fixing the Simulated and Adding a new Fn TransactionByHashWithBlockNum

This commit is contained in:
Priyadarshan Vyas 2019-05-28 17:20:03 -04:00
parent de06789c9a
commit 020746bcba
3 changed files with 34 additions and 1 deletions

View file

@ -20,6 +20,7 @@ import (
"context" "context"
"errors" "errors"
"fmt" "fmt"
"github.com/paxosglobal/go-ethereum"
"math/big" "math/big"
"sync" "sync"
"time" "time"
@ -182,7 +183,13 @@ func (b *SimulatedBackend) TransactionReceipt(ctx context.Context, txHash common
// TransactionByHash returns the transaction with the given hash. // TransactionByHash returns the transaction with the given hash.
func (b *SimulatedBackend) TransactionByHash(ctx context.Context, txHash common.Hash) (tx *types.Transaction, isPending bool, err error) { func (b *SimulatedBackend) TransactionByHash(ctx context.Context, txHash common.Hash) (tx *types.Transaction, isPending bool, err error) {
transaction, _, blockNumber, _ := rawdb.ReadTransaction(b.database, txHash) transaction, _, blockNumber, _ := rawdb.ReadTransaction(b.database, txHash)
return transaction, blockNumber != 0, nil return transaction, blockNumber == 0, nil
}
// TransactionByHash returns the transaction with the given hash.
func (b *SimulatedBackend) TransactionByHashWithBlockNum(ctx context.Context, txHash common.Hash) (tx *types.Transaction, isPending string, err error) {
transaction, _, blockNumber, _ := rawdb.ReadTransaction(b.database, txHash)
return transaction, string(blockNumber), nil
} }
// BlockByNumber retrieves a block from the database by number, caching it // BlockByNumber retrieves a block from the database by number, caching it

View file

@ -22,6 +22,7 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"github.com/paxosglobal/go-ethereum"
"math/big" "math/big"
"github.com/ethereum/go-ethereum" "github.com/ethereum/go-ethereum"
@ -188,6 +189,28 @@ func (tx *rpcTransaction) UnmarshalJSON(msg []byte) error {
return json.Unmarshal(msg, &tx.txExtraInfo) return json.Unmarshal(msg, &tx.txExtraInfo)
} }
// TransactionByHash returns the transaction with the given hash.
func (ec *Client) TransactionByHashWithBlockNum(ctx context.Context, hash common.Hash) (tx *types.Transaction, BlockNumber string, err error) {
var json *rpcTransaction
err = ec.c.CallContext(ctx, &json, "eth_getTransactionByHash", hash)
if err != nil {
return nil, false, err
} else if json == nil {
return nil, false, ethereum.NotFound
} else if _, r, _ := json.tx.RawSignatureValues(); r == nil {
return nil, false, fmt.Errorf("server returned transaction without signature")
}
if json.From != nil && json.BlockHash != nil {
setSenderFromServer(json.tx, *json.From, *json.BlockHash)
}
if json.BlockNumber == nil {
return json.tx, "0", nil
}
return json.tx, *json.BlockNumber, nil
}
// TransactionByHash returns the transaction with the given hash. // TransactionByHash returns the transaction with the given hash.
func (ec *Client) TransactionByHash(ctx context.Context, hash common.Hash) (tx *types.Transaction, isPending bool, err error) { func (ec *Client) TransactionByHash(ctx context.Context, hash common.Hash) (tx *types.Transaction, isPending bool, err error) {
var json *rpcTransaction var json *rpcTransaction

View file

@ -83,6 +83,9 @@ type TransactionReader interface {
// transaction may not be included in the current canonical chain even if a receipt // transaction may not be included in the current canonical chain even if a receipt
// exists. // exists.
TransactionReceipt(ctx context.Context, txHash common.Hash) (*types.Receipt, error) TransactionReceipt(ctx context.Context, txHash common.Hash) (*types.Receipt, error)
// Same as TransactionByHash but returns the BlockNum
TransactionByHashWithBlockNum(ctx context.Context, hash common.Hash) (tx *types.Transaction, BlockNumber string, err error)
} }
// ChainStateReader wraps access to the state trie of the canonical blockchain. Note that // ChainStateReader wraps access to the state trie of the canonical blockchain. Note that