cosmos-sdk/client/commands/query/query_test.go

128 lines
3.7 KiB
Go
Raw Normal View History

2017-08-07 08:15:16 -07:00
package query
import (
"os"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/tendermint/go-wire"
2017-08-09 04:13:16 -07:00
lc "github.com/tendermint/light-client"
2017-08-07 08:15:16 -07:00
"github.com/tendermint/light-client/certifiers"
certclient "github.com/tendermint/light-client/certifiers/client"
nm "github.com/tendermint/tendermint/node"
"github.com/tendermint/tendermint/rpc/client"
rpctest "github.com/tendermint/tendermint/rpc/test"
2017-08-10 05:25:17 -07:00
"github.com/tendermint/tendermint/types"
2017-08-07 08:15:16 -07:00
"github.com/tendermint/tmlibs/log"
"github.com/tendermint/basecoin/app"
2017-08-07 10:24:58 -07:00
"github.com/tendermint/basecoin/modules/eyes"
2017-08-07 08:15:16 -07:00
)
var node *nm.Node
func TestMain(m *testing.M) {
logger := log.TestingLogger()
store, err := app.NewStore("", 0, logger)
if err != nil {
panic(err)
}
2017-08-07 10:24:58 -07:00
app := app.NewBasecoin(eyes.NewHandler(), store, logger)
2017-08-07 08:15:16 -07:00
node = rpctest.StartTendermint(app)
code := m.Run()
node.Stop()
node.Wait()
os.Exit(code)
}
func TestAppProofs(t *testing.T) {
assert, require := assert.New(t), require.New(t)
cl := client.NewLocal(node)
client.WaitForHeight(cl, 1, nil)
2017-08-07 08:15:16 -07:00
k := []byte("my-key")
v := []byte("my-value")
2017-08-07 10:24:58 -07:00
tx := eyes.SetTx{Key: k, Value: v}.Wrap()
2017-08-07 08:15:16 -07:00
btx := wire.BinaryBytes(tx)
br, err := cl.BroadcastTxCommit(btx)
require.Nil(err, "%+v", err)
require.EqualValues(0, br.CheckTx.Code, "%#v", br.CheckTx)
require.EqualValues(0, br.DeliverTx.Code)
2017-08-08 09:06:14 -07:00
// This sets up our trust on the node based on some past point.
2017-08-07 08:15:16 -07:00
source := certclient.New(cl)
seed, err := source.GetByHeight(br.Height - 2)
require.Nil(err, "%+v", err)
2017-08-08 09:06:14 -07:00
cert := certifiers.NewStatic("my-chain", seed.Validators)
2017-08-07 08:15:16 -07:00
// Test existing key.
2017-08-08 09:06:14 -07:00
var data eyes.Data
2017-08-07 08:15:16 -07:00
2017-08-11 04:57:38 -07:00
bs, height, proofExists, _, err := getWithProof(k, cl, cert)
2017-08-07 08:15:16 -07:00
require.Nil(err, "%+v", err)
2017-08-08 02:51:15 -07:00
require.NotNil(proofExists)
2017-08-11 04:57:38 -07:00
require.True(uint64(br.Height) < height)
2017-08-07 08:15:16 -07:00
err = wire.ReadBinaryBytes(bs, &data)
require.Nil(err, "%+v", err)
assert.EqualValues(v, data.Value)
2017-08-08 03:27:40 -07:00
err = proofExists.Verify(k, bs, proofExists.RootHash)
assert.Nil(err, "%+v", err)
2017-08-07 08:15:16 -07:00
// Test non-existing key.
missing := []byte("my-missing-key")
2017-08-08 03:20:21 -07:00
bs, _, proofExists, proofNotExists, err := getWithProof(missing, cl, cert)
2017-08-09 04:13:16 -07:00
require.True(lc.IsNoDataErr(err))
2017-08-07 08:15:16 -07:00
require.Nil(bs)
2017-08-08 02:51:15 -07:00
require.Nil(proofExists)
require.NotNil(proofNotExists)
2017-08-08 03:27:40 -07:00
err = proofNotExists.Verify(missing, proofNotExists.RootHash)
assert.Nil(err, "%+v", err)
err = proofNotExists.Verify(k, proofNotExists.RootHash)
assert.NotNil(err)
2017-08-07 08:15:16 -07:00
}
2017-08-10 05:25:17 -07:00
func TestTxProofs(t *testing.T) {
assert, require := assert.New(t), require.New(t)
cl := client.NewLocal(node)
client.WaitForHeight(cl, 1, nil)
tx := eyes.SetTx{Key: []byte("key-a"), Value: []byte("value-a")}.Wrap()
btx := types.Tx(wire.BinaryBytes(tx))
br, err := cl.BroadcastTxCommit(btx)
require.Nil(err, "%+v", err)
require.EqualValues(0, br.CheckTx.Code, "%#v", br.CheckTx)
require.EqualValues(0, br.DeliverTx.Code)
source := certclient.New(cl)
seed, err := source.GetByHeight(br.Height - 2)
require.Nil(err, "%+v", err)
cert := certifiers.NewStatic("my-chain", seed.Validators)
// First let's make sure a bogus transaction hash returns a valid non-existence proof.
key := types.Tx([]byte("bogus")).Hash()
bs, _, proofExists, proofNotExists, err := getWithProof(key, cl, cert)
assert.Nil(bs, "value should be nil")
require.True(lc.IsNoDataErr(err), "error should signal 'no data'")
assert.Nil(proofExists, "existence proof should be nil")
require.NotNil(proofNotExists, "non-existence proof shouldn't be nil")
err = proofNotExists.Verify(key, proofNotExists.RootHash)
require.Nil(err, "%+v", err)
// Now let's check with the real tx hash.
key = btx.Hash()
res, err := cl.Tx(key, true)
require.Nil(err, "%+v", err)
require.NotNil(res)
err = res.Proof.Validate(key)
assert.Nil(err, "%+v", err)
}