tendermint/lite/static_test.go

60 lines
1.6 KiB
Go
Raw Normal View History

2017-11-09 14:37:18 -08:00
package lite_test
2017-10-24 03:34:36 -07:00
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/tendermint/tendermint/types"
2017-11-09 14:37:18 -08:00
"github.com/tendermint/tendermint/lite"
liteErr "github.com/tendermint/tendermint/lite/errors"
2017-10-24 03:34:36 -07:00
)
func TestStaticCert(t *testing.T) {
// assert, require := assert.New(t), require.New(t)
assert := assert.New(t)
// require := require.New(t)
2017-11-09 14:37:18 -08:00
keys := lite.GenValKeys(4)
2017-10-24 03:34:36 -07:00
// 20, 30, 40, 50 - the first 3 don't have 2/3, the last 3 do!
vals := keys.ToValidators(20, 10)
// and a certifier based on our known set
chainID := "test-static"
2017-11-09 14:37:18 -08:00
cert := lite.NewStatic(chainID, vals)
2017-10-24 03:34:36 -07:00
cases := []struct {
2017-11-09 14:37:18 -08:00
keys lite.ValKeys
2017-10-24 03:34:36 -07:00
vals *types.ValidatorSet
2017-11-30 11:08:38 -08:00
height uint64
2017-10-24 03:34:36 -07:00
first, last int // who actually signs
proper bool // true -> expect no error
changed bool // true -> expect validator change error
}{
// perfect, signed by everyone
{keys, vals, 1, 0, len(keys), true, false},
// skip little guy is okay
{keys, vals, 2, 1, len(keys), true, false},
// but not the big guy
{keys, vals, 3, 0, len(keys) - 1, false, false},
// even changing the power a little bit breaks the static validator
// the sigs are enough, but the validator hash is unknown
{keys, keys.ToValidators(20, 11), 4, 0, len(keys), false, true},
}
for _, tc := range cases {
check := tc.keys.GenCommit(chainID, tc.height, nil, tc.vals,
[]byte("foo"), tc.first, tc.last)
err := cert.Certify(check)
if tc.proper {
assert.Nil(err, "%+v", err)
} else {
assert.NotNil(err)
if tc.changed {
2017-11-09 14:37:18 -08:00
assert.True(liteErr.IsValidatorsChangedErr(err), "%+v", err)
2017-10-24 03:34:36 -07:00
}
}
}
}