tendermint/rpc/core/tx.go

52 lines
1.1 KiB
Go
Raw Normal View History

package core
import (
2017-04-12 15:18:17 -07:00
"fmt"
2017-04-13 10:47:48 -07:00
abci "github.com/tendermint/abci/types"
ctypes "github.com/tendermint/tendermint/rpc/core/types"
2017-04-12 15:55:00 -07:00
"github.com/tendermint/tendermint/types"
)
2017-04-13 10:47:48 -07:00
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)
}
2017-04-12 15:18:17 -07:00
2017-04-13 10:47:48 -07:00
height = int(r.Height) // XXX
index = int(r.Index)
deliverTx = r.DeliverTx
2017-04-12 15:18:17 -07:00
}
2017-04-13 10:47:48 -07:00
block := blockStore.LoadBlock(height)
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]
2017-04-12 15:18:17 -07:00
2017-04-12 15:55:00 -07:00
var proof types.TxProof
if prove {
2017-04-13 10:47:48 -07:00
proof = block.Data.Txs.Proof(index)
2017-04-12 15:55:00 -07:00
}
2017-04-12 15:18:17 -07:00
return &ctypes.ResultTx{
2017-04-13 10:47:48 -07:00
Height: height,
Index: index,
DeliverTx: deliverTx,
2017-04-12 15:18:17 -07:00
Tx: tx,
2017-04-12 15:55:00 -07:00
Proof: proof,
2017-04-12 15:18:17 -07:00
}, nil
}