github.com/cosmos/cosmos-sdk@v0.50.10/x/staking/testutil/cmt.go (about)

     1  package testutil
     2  
     3  import (
     4  	cmtcrypto "github.com/cometbft/cometbft/crypto"
     5  	cmttypes "github.com/cometbft/cometbft/types"
     6  
     7  	"cosmossdk.io/math"
     8  
     9  	cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
    10  	"github.com/cosmos/cosmos-sdk/x/staking/types"
    11  )
    12  
    13  // GetCmtConsPubKey gets the validator's public key as a cmtcrypto.PubKey.
    14  func GetCmtConsPubKey(v types.Validator) (cmtcrypto.PubKey, error) {
    15  	pk, err := v.ConsPubKey()
    16  	if err != nil {
    17  		return nil, err
    18  	}
    19  
    20  	return cryptocodec.ToCmtPubKeyInterface(pk)
    21  }
    22  
    23  // ToCmtValidator casts an SDK validator to a CometBFT type Validator.
    24  func ToCmtValidator(v types.Validator, r math.Int) (*cmttypes.Validator, error) {
    25  	tmPk, err := GetCmtConsPubKey(v)
    26  	if err != nil {
    27  		return nil, err
    28  	}
    29  
    30  	return cmttypes.NewValidator(tmPk, v.ConsensusPower(r)), nil
    31  }
    32  
    33  // ToCmtValidators casts all validators to the corresponding CometBFT type.
    34  func ToCmtValidators(v types.Validators, r math.Int) ([]*cmttypes.Validator, error) {
    35  	validators := make([]*cmttypes.Validator, len(v.Validators))
    36  	var err error
    37  	for i, val := range v.Validators {
    38  		validators[i], err = ToCmtValidator(val, r)
    39  		if err != nil {
    40  			return nil, err
    41  		}
    42  	}
    43  
    44  	return validators, nil
    45  }