Skip to content

Commit

Permalink
Add consensus address to validator query response
Browse files Browse the repository at this point in the history
  • Loading branch information
lucaslopezf committed May 21, 2024
1 parent 9fd3d13 commit 2463cec
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 0 deletions.
17 changes: 17 additions & 0 deletions x/staking/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package keeper
import (
"context"
"errors"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
"strings"

"google.golang.org/grpc/codes"
Expand Down Expand Up @@ -48,6 +49,7 @@ func (k Querier) Validators(ctx context.Context, req *types.QueryValidatorsReque
return nil, nil
}

setConsensusAddress(val)
return val, nil
}, func() *types.Validator {
return &types.Validator{}
Expand Down Expand Up @@ -84,6 +86,7 @@ func (k Querier) Validator(ctx context.Context, req *types.QueryValidatorRequest
return nil, status.Errorf(codes.NotFound, "validator %s not found", req.ValidatorAddr)
}

setConsensusAddress(&validator)
return &types.QueryValidatorResponse{Validator: validator}, nil
}

Expand Down Expand Up @@ -649,3 +652,17 @@ func redelegationsToRedelegationResponses(ctx context.Context, k *Keeper, redels

return resp, nil
}

// setConsensusAddress sets the ConsensusAddress field for the given validator
func setConsensusAddress(validator *types.Validator) {
if validator == nil {
return
}

cpk, ok := validator.ConsensusPubkey.GetCachedValue().(cryptotypes.PubKey)
// Best-effort way
if ok {
consAddr := sdk.ConsAddress(cpk.Address())
validator.ConsensusAddress = consAddr.String()
}
}
3 changes: 3 additions & 0 deletions x/staking/proto/cosmos/staking/v1beta1/staking.proto
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@ message Validator {

// list of unbonding ids, each uniquely identifying an unbonding of this validator
repeated uint64 unbonding_ids = 13;

// consensus_address defines the address of the validator's consensus
string consensus_address = 14 [(cosmos_proto.scalar) = "cosmos.AddressString"];
}

// BondStatus is the status of a validator.
Expand Down
1 change: 1 addition & 0 deletions x/staking/types/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func NewValidator(operator string, pubKey cryptotypes.PubKey, description Descri

return Validator{
OperatorAddress: operator,
ConsensusAddress: sdk.ConsAddress(pubKey.Address()).String(),
ConsensusPubkey: pkAny,
Jailed: false,
Status: Unbonded,
Expand Down
5 changes: 5 additions & 0 deletions x/staking/types/validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,11 @@ func TestBondStatus(t *testing.T) {
require.Equal(t, types.BondStatusUnbonding, types.Unbonding.String())
}

func TestValidatorSetConsensusAddress(t *testing.T) {
validator := newValidator(t, valAddr1, pk1)
require.Equal(t, sdk.ConsAddress(pk1.Address()).String(), validator.ConsensusAddress)
}

func mkValidator(tokens int64, shares math.LegacyDec) types.Validator {
vAddr1, _ := codectestutil.CodecOptions{}.GetValidatorCodec().BytesToString(valAddr1)
return types.Validator{
Expand Down

0 comments on commit 2463cec

Please sign in to comment.