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