tendermint/light/client/provider_test.go

62 lines
1.5 KiB
Go
Raw Normal View History

2017-11-12 00:43:16 +00:00
package client
2017-10-24 12:34:36 +02:00
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/tendermint/tendermint/light"
lightErr "github.com/tendermint/tendermint/light/errors"
2017-11-12 00:43:16 +00:00
rpcclient "github.com/tendermint/tendermint/rpc/client"
rpctest "github.com/tendermint/tendermint/rpc/test"
2017-10-24 12:34:36 +02:00
)
func TestProvider(t *testing.T) {
assert, require := assert.New(t), require.New(t)
cfg := rpctest.GetConfig()
rpcAddr := cfg.RPC.ListenAddress
chainID := cfg.ChainID
2017-11-12 00:43:16 +00:00
p := NewHTTPProvider(rpcAddr)
2017-10-24 12:34:36 +02:00
require.NotNil(t, p)
// let it produce some blocks
2017-11-12 00:43:16 +00:00
err := rpcclient.WaitForHeight(p.(*provider).node, 6, nil)
require.Nil(err)
2017-10-24 12:34:36 +02:00
// let's get the highest block
seed, err := p.LatestCommit()
require.Nil(err, "%+v", err)
sh := seed.Height()
vhash := seed.Header.ValidatorsHash
assert.True(sh < 5000)
// let's check this is valid somehow
assert.Nil(seed.ValidateBasic(chainID))
cert := light.NewStatic(chainID, seed.Validators)
2017-10-24 12:34:36 +02:00
// historical queries now work :)
lower := sh - 5
seed, err = p.GetByHeight(lower)
assert.Nil(err, "%+v", err)
assert.Equal(lower, seed.Height())
// also get by hash (given the match)
seed, err = p.GetByHash(vhash)
require.Nil(err, "%+v", err)
require.Equal(vhash, seed.Header.ValidatorsHash)
err = cert.Certify(seed.Commit)
assert.Nil(err, "%+v", err)
// get by hash fails without match
seed, err = p.GetByHash([]byte("foobar"))
assert.NotNil(err)
assert.True(lightErr.IsCommitNotFoundErr(err))
2017-10-24 12:34:36 +02:00
// storing the seed silently ignored
err = p.StoreCommit(seed)
assert.Nil(err, "%+v", err)
}