mirror of
https://github.com/tendermint/tendermint.git
synced 2026-02-10 14:00:33 +00:00
69 lines
1.9 KiB
Go
69 lines
1.9 KiB
Go
package consensus_test
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/tendermint/tendermint/crypto"
|
|
"github.com/tendermint/tendermint/crypto/ed25519"
|
|
cryptoenc "github.com/tendermint/tendermint/crypto/encoding"
|
|
"github.com/tendermint/tendermint/pkg/abci"
|
|
"github.com/tendermint/tendermint/pkg/consensus"
|
|
)
|
|
|
|
func TestABCIPubKey(t *testing.T) {
|
|
pkEd := ed25519.GenPrivKey().PubKey()
|
|
err := testABCIPubKey(t, pkEd, consensus.ABCIPubKeyTypeEd25519)
|
|
assert.NoError(t, err)
|
|
}
|
|
|
|
func testABCIPubKey(t *testing.T, pk crypto.PubKey, typeStr string) error {
|
|
abciPubKey, err := cryptoenc.PubKeyToProto(pk)
|
|
require.NoError(t, err)
|
|
pk2, err := cryptoenc.PubKeyFromProto(abciPubKey)
|
|
require.NoError(t, err)
|
|
require.Equal(t, pk, pk2)
|
|
return nil
|
|
}
|
|
|
|
func TestABCIValidators(t *testing.T) {
|
|
pkEd := ed25519.GenPrivKey().PubKey()
|
|
|
|
// correct validator
|
|
tmValExpected := consensus.NewValidator(pkEd, 10)
|
|
|
|
tmVal := consensus.NewValidator(pkEd, 10)
|
|
|
|
abciVal := consensus.TM2PB.ValidatorUpdate(tmVal)
|
|
tmVals, err := consensus.PB2TM.ValidatorUpdates([]abci.ValidatorUpdate{abciVal})
|
|
assert.Nil(t, err)
|
|
assert.Equal(t, tmValExpected, tmVals[0])
|
|
|
|
abciVals := consensus.TM2PB.ValidatorUpdates(consensus.NewValidatorSet(tmVals))
|
|
assert.Equal(t, []abci.ValidatorUpdate{abciVal}, abciVals)
|
|
|
|
// val with address
|
|
tmVal.Address = pkEd.Address()
|
|
|
|
abciVal = consensus.TM2PB.ValidatorUpdate(tmVal)
|
|
tmVals, err = consensus.PB2TM.ValidatorUpdates([]abci.ValidatorUpdate{abciVal})
|
|
assert.Nil(t, err)
|
|
assert.Equal(t, tmValExpected, tmVals[0])
|
|
}
|
|
|
|
func TestABCIValidatorWithoutPubKey(t *testing.T) {
|
|
pkEd := ed25519.GenPrivKey().PubKey()
|
|
|
|
abciVal := consensus.TM2PB.Validator(consensus.NewValidator(pkEd, 10))
|
|
|
|
// pubkey must be nil
|
|
tmValExpected := abci.Validator{
|
|
Address: pkEd.Address(),
|
|
Power: 10,
|
|
}
|
|
|
|
assert.Equal(t, tmValExpected, abciVal)
|
|
}
|