diff --git a/rpc/core/routes.go b/rpc/core/routes.go index 38e60960..4dc5aef8 100644 --- a/rpc/core/routes.go +++ b/rpc/core/routes.go @@ -19,7 +19,7 @@ var Routes = map[string]*rpc.RPCFunc{ "genesis": rpc.NewRPCFunc(GenesisResult, ""), "block": rpc.NewRPCFunc(BlockResult, "height"), "commit": rpc.NewRPCFunc(CommitResult, "height"), - "tx": rpc.NewRPCFunc(TxResult, "hash,prove"), + "tx": rpc.NewRPCFunc(TxResult, "hash,height,index,prove"), "validators": rpc.NewRPCFunc(ValidatorsResult, ""), "dump_consensus_state": rpc.NewRPCFunc(DumpConsensusStateResult, ""), "unconfirmed_txs": rpc.NewRPCFunc(UnconfirmedTxsResult, ""), @@ -100,8 +100,8 @@ func NumUnconfirmedTxsResult() (ctypes.TMResult, error) { // Tx allow user to query the transaction results. `nil` could mean the // transaction is in the mempool, invalidated, or was not send in the first // place. -func TxResult(hash []byte, prove bool) (ctypes.TMResult, error) { - return Tx(hash, prove) +func TxResult(hash []byte, height, index int, prove bool) (ctypes.TMResult, error) { + return Tx(hash, height, index, prove) } func BroadcastTxCommitResult(tx []byte) (ctypes.TMResult, error) { diff --git a/rpc/core/tx.go b/rpc/core/tx.go index 14f87ece..e6e4e3df 100644 --- a/rpc/core/tx.go +++ b/rpc/core/tx.go @@ -3,32 +3,48 @@ package core import ( "fmt" + abci "github.com/tendermint/abci/types" ctypes "github.com/tendermint/tendermint/rpc/core/types" "github.com/tendermint/tendermint/types" ) -func Tx(hash []byte, prove bool) (*ctypes.ResultTx, error) { - r, err := txIndexer.Tx(hash) - if err != nil { - return nil, err +func Tx(hash []byte, height, index int, prove bool) (*ctypes.ResultTx, error) { + var deliverTx abci.ResponseDeliverTx + if len(hash) > 0 { + if height != 0 || index != 0 { + return nil, fmt.Errorf("Invalid args. If hash is provided, height and index should not be") + } + + r, err := txIndexer.Tx(hash) + if err != nil { + return nil, err + } + + if r == nil { + return &ctypes.ResultTx{}, fmt.Errorf("Tx (%X) not found", hash) + } + + height = int(r.Height) // XXX + index = int(r.Index) + deliverTx = r.DeliverTx } - if r == nil { - return &ctypes.ResultTx{}, fmt.Errorf("Tx (%X) not found", hash) - } + block := blockStore.LoadBlock(height) - block := blockStore.LoadBlock(int(r.Height)) - tx := block.Data.Txs[int(r.Index)] + if index >= len(block.Data.Txs) { + return nil, fmt.Errorf("Index (%d) is out of range for block (%d) with %d txs", index, height, len(block.Data.Txs)) + } + tx := block.Data.Txs[index] var proof types.TxProof if prove { - proof = block.Data.Txs.Proof(int(r.Index)) + proof = block.Data.Txs.Proof(index) } return &ctypes.ResultTx{ - Height: r.Height, - Index: r.Index, - DeliverTx: r.DeliverTx, + Height: height, + Index: index, + DeliverTx: deliverTx, Tx: tx, Proof: proof, }, nil