2020-11-09 08:01:43 -08:00
|
|
|
package teststaking
|
|
|
|
|
|
|
|
import (
|
|
|
|
tmcrypto "github.com/tendermint/tendermint/crypto"
|
|
|
|
tmtypes "github.com/tendermint/tendermint/types"
|
|
|
|
|
|
|
|
cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
|
2021-01-27 02:24:00 -08:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2020-11-09 08:01:43 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/staking/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
// GetTmConsPubKey gets the validator's public key as a tmcrypto.PubKey.
|
|
|
|
func GetTmConsPubKey(v types.Validator) (tmcrypto.PubKey, error) {
|
|
|
|
pk, err := v.ConsPubKey()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return cryptocodec.ToTmPubKeyInterface(pk)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToTmValidator casts an SDK validator to a tendermint type Validator.
|
2021-02-03 01:59:37 -08:00
|
|
|
func ToTmValidator(v types.Validator, r sdk.Int) (*tmtypes.Validator, error) {
|
2020-11-09 08:01:43 -08:00
|
|
|
tmPk, err := GetTmConsPubKey(v)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-02-03 01:59:37 -08:00
|
|
|
return tmtypes.NewValidator(tmPk, v.ConsensusPower(r)), nil
|
2020-11-09 08:01:43 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// ToTmValidators casts all validators to the corresponding tendermint type.
|
2021-02-03 01:59:37 -08:00
|
|
|
func ToTmValidators(v types.Validators, r sdk.Int) ([]*tmtypes.Validator, error) {
|
2020-11-09 08:01:43 -08:00
|
|
|
validators := make([]*tmtypes.Validator, len(v))
|
|
|
|
var err error
|
|
|
|
for i, val := range v {
|
2021-02-03 01:59:37 -08:00
|
|
|
validators[i], err = ToTmValidator(val, r)
|
2020-11-09 08:01:43 -08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return validators, nil
|
|
|
|
}
|