Expose Tx method in the clients

This commit is contained in:
Ethan Frey 2017-04-13 21:49:21 +02:00
parent c848056438
commit 20458564b2
4 changed files with 42 additions and 13 deletions

View File

@ -160,6 +160,21 @@ func (c *HTTP) Commit(height int) (*ctypes.ResultCommit, error) {
return (*tmResult).(*ctypes.ResultCommit), nil return (*tmResult).(*ctypes.ResultCommit), nil
} }
func (c *HTTP) Tx(hash []byte, height, index int, prove bool) (*ctypes.ResultTx, error) {
tmResult := new(ctypes.TMResult)
query := map[string]interface{}{
"height": height,
"index": index,
"hash": hash,
"prove": prove,
}
_, err := c.rpc.Call("tx", query, tmResult)
if err != nil {
return nil, errors.Wrap(err, "Tx")
}
return (*tmResult).(*ctypes.ResultTx), nil
}
func (c *HTTP) Validators() (*ctypes.ResultValidators, error) { func (c *HTTP) Validators() (*ctypes.ResultValidators, error) {
tmResult := new(ctypes.TMResult) tmResult := new(ctypes.TMResult)
_, err := c.rpc.Call("validators", map[string]interface{}{}, tmResult) _, err := c.rpc.Call("validators", map[string]interface{}{}, tmResult)

View File

@ -44,6 +44,7 @@ type SignClient interface {
Block(height int) (*ctypes.ResultBlock, error) Block(height int) (*ctypes.ResultBlock, error)
Commit(height int) (*ctypes.ResultCommit, error) Commit(height int) (*ctypes.ResultCommit, error)
Validators() (*ctypes.ResultValidators, error) Validators() (*ctypes.ResultValidators, error)
Tx(hash []byte, height, index int, prove bool) (*ctypes.ResultTx, error)
} }
// HistoryClient shows us data from genesis to now in large chunks. // HistoryClient shows us data from genesis to now in large chunks.

View File

@ -103,3 +103,7 @@ func (c Local) Commit(height int) (*ctypes.ResultCommit, error) {
func (c Local) Validators() (*ctypes.ResultValidators, error) { func (c Local) Validators() (*ctypes.ResultValidators, error) {
return core.Validators() return core.Validators()
} }
func (c Local) Tx(hash []byte, height, index int, prove bool) (*ctypes.ResultTx, error) {
return core.Tx(hash, height, index, prove)
}

View File

@ -3,7 +3,6 @@ package client_test
import ( import (
"strings" "strings"
"testing" "testing"
"time"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
@ -11,6 +10,7 @@ import (
merktest "github.com/tendermint/merkleeyes/testutil" merktest "github.com/tendermint/merkleeyes/testutil"
"github.com/tendermint/tendermint/rpc/client" "github.com/tendermint/tendermint/rpc/client"
rpctest "github.com/tendermint/tendermint/rpc/test" rpctest "github.com/tendermint/tendermint/rpc/test"
"github.com/tendermint/tendermint/types"
) )
func getHTTPClient() *client.HTTP { func getHTTPClient() *client.HTTP {
@ -117,49 +117,58 @@ func TestAppCalls(t *testing.T) {
// write something // write something
k, v, tx := merktest.MakeTxKV() k, v, tx := merktest.MakeTxKV()
_, err = c.BroadcastTxCommit(tx) bres, err := c.BroadcastTxCommit(tx)
require.Nil(err, "%d: %+v", i, err) require.Nil(err, "%d: %+v", i, err)
require.True(bres.DeliverTx.GetCode().IsOK())
txh := bres.Height
apph := txh + 1 // this is where the tx will be applied to the state
// wait before querying // wait before querying
time.Sleep(time.Second * 1) client.WaitForHeight(c, apph, nil)
qres, err := c.ABCIQuery("/key", k, false) qres, err := c.ABCIQuery("/key", k, false)
if assert.Nil(err) && assert.True(qres.Response.Code.IsOK()) { if assert.Nil(err) && assert.True(qres.Response.Code.IsOK()) {
data := qres.Response data := qres.Response
// assert.Equal(k, data.GetKey()) // only returned for proofs // assert.Equal(k, data.GetKey()) // only returned for proofs
assert.Equal(v, data.GetValue()) assert.Equal(v, data.GetValue())
} }
// +/- 1 making my head hurt
h := int(qres.Response.Height) - 1 // make sure we can lookup the tx with proof
// ptx, err := c.Tx(bres.TxID, txh, 0, true)
ptx, err := c.Tx(bres.TxID, 0, 0, true)
require.Nil(err, "%d: %+v", i, err)
assert.Equal(txh, ptx.Height)
assert.Equal(types.Tx(tx), ptx.Tx)
// and we can even check the block is added // and we can even check the block is added
block, err := c.Block(h) block, err := c.Block(apph)
require.Nil(err, "%d: %+v", i, err) require.Nil(err, "%d: %+v", i, err)
appHash := block.BlockMeta.Header.AppHash appHash := block.BlockMeta.Header.AppHash
assert.True(len(appHash) > 0) assert.True(len(appHash) > 0)
assert.EqualValues(h, block.BlockMeta.Header.Height) assert.EqualValues(apph, block.BlockMeta.Header.Height)
// check blockchain info, now that we know there is info // check blockchain info, now that we know there is info
// TODO: is this commented somewhere that they are returned // TODO: is this commented somewhere that they are returned
// in order of descending height??? // in order of descending height???
info, err := c.BlockchainInfo(h-2, h) info, err := c.BlockchainInfo(apph, apph)
require.Nil(err, "%d: %+v", i, err) require.Nil(err, "%d: %+v", i, err)
assert.True(info.LastHeight > 2) assert.True(info.LastHeight >= apph)
if assert.Equal(3, len(info.BlockMetas)) { if assert.Equal(1, len(info.BlockMetas)) {
lastMeta := info.BlockMetas[0] lastMeta := info.BlockMetas[0]
assert.EqualValues(h, lastMeta.Header.Height) assert.EqualValues(apph, lastMeta.Header.Height)
bMeta := block.BlockMeta bMeta := block.BlockMeta
assert.Equal(bMeta.Header.AppHash, lastMeta.Header.AppHash) assert.Equal(bMeta.Header.AppHash, lastMeta.Header.AppHash)
assert.Equal(bMeta.BlockID, lastMeta.BlockID) assert.Equal(bMeta.BlockID, lastMeta.BlockID)
} }
// and get the corresponding commit with the same apphash // and get the corresponding commit with the same apphash
commit, err := c.Commit(h) commit, err := c.Commit(apph)
require.Nil(err, "%d: %+v", i, err) require.Nil(err, "%d: %+v", i, err)
cappHash := commit.Header.AppHash cappHash := commit.Header.AppHash
assert.Equal(appHash, cappHash) assert.Equal(appHash, cappHash)
assert.NotNil(commit.Commit) assert.NotNil(commit.Commit)
// compare the commits (note Commit(2) has commit from Block(3)) // compare the commits (note Commit(2) has commit from Block(3))
commit2, err := c.Commit(h - 1) commit2, err := c.Commit(apph - 1)
require.Nil(err, "%d: %+v", i, err) require.Nil(err, "%d: %+v", i, err)
assert.Equal(block.Block.LastCommit, commit2.Commit) assert.Equal(block.Block.LastCommit, commit2.Commit)