Skip to content

Commit

Permalink
Merge pull request #2 from paxosglobal/fix-transactionbyhash
Browse files Browse the repository at this point in the history
Fixing the Simulated and Adding a new Fn TransactionByHashWithBlockNum
  • Loading branch information
piyuvyas authored Jun 13, 2019
2 parents 6da3643 + 8f4905b commit 817dd6c
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
9 changes: 8 additions & 1 deletion accounts/abi/bind/backends/simulated.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"errors"
"fmt"
"github.com/paxosglobal/go-ethereum/common/hexutil"
"math/big"
"sync"
"time"
Expand Down Expand Up @@ -183,7 +184,13 @@ func (b *SimulatedBackend) TransactionReceipt(ctx context.Context, txHash common
// 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) {
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, blockHexString string, err error) {
transaction, _, blockNumber, _ := rawdb.ReadTransaction(b.database, txHash)
return transaction, hexutil.EncodeUint64(blockNumber), nil
}

func (b *SimulatedBackend) BlockByHash(ctx context.Context, hash common.Hash) (*types.Block, error) {
Expand Down
25 changes: 25 additions & 0 deletions ethclient/ethclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/paxosglobal/go-ethereum"
"math/big"

"github.com/ethereum/go-ethereum"
Expand Down Expand Up @@ -188,6 +189,30 @@ func (tx *rpcTransaction) UnmarshalJSON(msg []byte) error {
return json.Unmarshal(msg, &tx.txExtraInfo)
}

// TransactionByHash returns the transaction with the given hash.
// The code is exactly copied from the function TransactionByHash
// but this returns the blockHexString instead of json.BlockNumber == nil
func (ec *Client) TransactionByHashWithBlockNum(ctx context.Context, hash common.Hash) (tx *types.Transaction, blockHexString string, err error) {
var json *rpcTransaction
err = ec.c.CallContext(ctx, &json, "eth_getTransactionByHash", hash)
if err != nil {
return nil, "0x0", err
} else if json == nil {
return nil, "0x0", ethereum.NotFound
} else if _, r, _ := json.tx.RawSignatureValues(); r == nil {
return nil, "0x0", 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, "0x0", nil
}

return json.tx, *json.BlockNumber, nil
}

// 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) {
var json *rpcTransaction
Expand Down
3 changes: 3 additions & 0 deletions interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ type TransactionReader interface {
// transaction may not be included in the current canonical chain even if a receipt
// exists.
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, blockHexString string, err error)
}

// ChainStateReader wraps access to the state trie of the canonical blockchain. Note that
Expand Down

0 comments on commit 817dd6c

Please sign in to comment.