Skip to content

Commit

Permalink
Add address codec to validator
Browse files Browse the repository at this point in the history
  • Loading branch information
lucaslopezf committed May 23, 2024
1 parent 1d7cb9a commit 424b463
Show file tree
Hide file tree
Showing 22 changed files with 1,188 additions and 1,172 deletions.
1 change: 0 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ Every module contains its own CHANGELOG.md. Please refer to the module you are i
* (crypto/keyring) [#20212](https://github.com/cosmos/cosmos-sdk/pull/20212) Expose the db keyring used in the keystore.
* (genutil) [#19971](https://github.com/cosmos/cosmos-sdk/pull/19971) Allow manually setting the consensus key type in genesis
* (debug) [#20328](https://github.com/cosmos/cosmos-sdk/pull/20328) Add consensus address for debug cmd.
* (x/staking) [#20434](https://github.com/cosmos/cosmos-sdk/pull/20434) Add consensus address to validator query response

### Improvements

Expand Down
565 changes: 283 additions & 282 deletions api/cosmos/staking/v1beta1/staking.pulsar.go

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion tests/integration/distribution/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ func TestMsgWithdrawDelegatorReward(t *testing.T) {
}

// setup staking validator
validator, err := stakingtypes.NewValidator(f.valAddr.String(), PKS[0], stakingtypes.Description{})
validator, err := stakingtypes.NewValidator(f.valAddr.String(), PKS[0], stakingtypes.Description{}, addresscodec.NewBech32Codec(sdk.Bech32PrefixConsAddr))
assert.NilError(t, err)
commission := stakingtypes.NewCommission(math.LegacyZeroDec(), math.LegacyOneDec(), math.LegacyOneDec())
validator, err = validator.SetInitialCommission(commission)
Expand Down
7 changes: 4 additions & 3 deletions tests/integration/gov/keeper/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"cosmossdk.io/x/gov/types/v1beta1"
stakingtypes "cosmossdk.io/x/staking/types"

addresscodec "github.com/cosmos/cosmos-sdk/codec/address"
simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
sdk "github.com/cosmos/cosmos-sdk/types"
)
Expand Down Expand Up @@ -41,11 +42,11 @@ func createValidators(t *testing.T, f *fixture, powers []int64) ([]sdk.AccAddres
valAddrs := simtestutil.ConvertAddrsToValAddrs(addrs)
pks := simtestutil.CreateTestPubKeys(5)

val1, err := stakingtypes.NewValidator(valAddrs[0].String(), pks[0], stakingtypes.Description{})
val1, err := stakingtypes.NewValidator(valAddrs[0].String(), pks[0], stakingtypes.Description{}, addresscodec.NewBech32Codec(sdk.Bech32PrefixConsAddr))
assert.NilError(t, err)
val2, err := stakingtypes.NewValidator(valAddrs[1].String(), pks[1], stakingtypes.Description{})
val2, err := stakingtypes.NewValidator(valAddrs[1].String(), pks[1], stakingtypes.Description{}, addresscodec.NewBech32Codec(sdk.Bech32PrefixConsAddr))
assert.NilError(t, err)
val3, err := stakingtypes.NewValidator(valAddrs[2].String(), pks[2], stakingtypes.Description{})
val3, err := stakingtypes.NewValidator(valAddrs[2].String(), pks[2], stakingtypes.Description{}, addresscodec.NewBech32Codec(sdk.Bech32PrefixConsAddr))
assert.NilError(t, err)

assert.NilError(t, f.stakingKeeper.SetValidator(f.ctx, val1))
Expand Down
2 changes: 2 additions & 0 deletions tests/integration/staking/keeper/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"cosmossdk.io/x/staking"
"cosmossdk.io/x/staking/types"

addresscodec "github.com/cosmos/cosmos-sdk/codec/address"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
Expand Down Expand Up @@ -196,6 +197,7 @@ func TestInitGenesisLargeValidatorSet(t *testing.T) {
sdk.ValAddress(addrs[i]).String(),
PKs[i],
types.NewDescription(fmt.Sprintf("#%d", i), "", "", "", ""),
addresscodec.NewBech32Codec(sdk.Bech32PrefixConsAddr),
)
assert.NilError(t, err)
validators[i].Status = types.Bonded
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/staking/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func TestCancelUnbondingDelegation(t *testing.T) {
delegatorAddr := addrs[1]

// setup a new validator with bonded status
validator, err := types.NewValidator(valAddr.String(), PKs[0], types.NewDescription("Validator", "", "", "", ""))
validator, err := types.NewValidator(valAddr.String(), PKs[0], types.NewDescription("Validator", "", "", "", ""), address.NewBech32Codec(sdk.Bech32PrefixConsAddr))
validator.Status = types.Bonded
assert.NilError(t, err)
assert.NilError(t, f.stakingKeeper.SetValidator(ctx, validator))
Expand Down
3 changes: 2 additions & 1 deletion tests/integration/staking/keeper/validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"cosmossdk.io/x/staking/testutil"
"cosmossdk.io/x/staking/types"

addresscodec "github.com/cosmos/cosmos-sdk/codec/address"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand All @@ -20,7 +21,7 @@ import (

func newMonikerValidator(tb testing.TB, operator sdk.ValAddress, pubKey cryptotypes.PubKey, moniker string) types.Validator {
tb.Helper()
v, err := types.NewValidator(operator.String(), pubKey, types.Description{Moniker: moniker})
v, err := types.NewValidator(operator.String(), pubKey, types.Description{Moniker: moniker}, addresscodec.NewBech32Codec(sdk.Bech32PrefixConsAddr))
assert.NilError(tb, err)
return v
}
Expand Down
3 changes: 1 addition & 2 deletions tests/sims/distribution/operations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,7 @@ func (suite *SimTestSuite) getTestingValidator(accounts []simtypes.Account, comm
account := accounts[n]
valPubKey := account.PubKey
valAddr := sdk.ValAddress(account.PubKey.Address().Bytes())
validator, err := stakingtypes.NewValidator(valAddr.String(), valPubKey, stakingtypes.
Description{})
validator, err := stakingtypes.NewValidator(valAddr.String(), valPubKey, stakingtypes.Description{}, address.NewBech32Codec(sdk.Bech32PrefixConsAddr))
require.NoError(err)
validator, err = validator.SetInitialCommission(commission)
require.NoError(err)
Expand Down
3 changes: 2 additions & 1 deletion tests/sims/slashing/operations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
addresscodec "github.com/cosmos/cosmos-sdk/codec/address"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
"github.com/cosmos/cosmos-sdk/runtime"
Expand Down Expand Up @@ -207,7 +208,7 @@ func getTestingValidator(ctx sdk.Context, stakingKeeper *stakingkeeper.Keeper, a
account := accounts[n]
valPubKey := account.ConsKey.PubKey()
valAddr := sdk.ValAddress(account.PubKey.Address().Bytes())
validator, err := stakingtypes.NewValidator(valAddr.String(), valPubKey, stakingtypes.Description{})
validator, err := stakingtypes.NewValidator(valAddr.String(), valPubKey, stakingtypes.Description{}, addresscodec.NewBech32Codec(sdk.Bech32PrefixConsAddr))
if err != nil {
return stakingtypes.Validator{}, fmt.Errorf("failed to create validator: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion x/distribution/keeper/allocation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ func TestAllocateTokensTruncation(t *testing.T) {
valAddr2 := sdk.ValAddress(valConsAddr2)
valAddr2Str, err := cdcOpts.GetValidatorCodec().BytesToString(valAddr2)
require.NoError(t, err)
val2, err := stakingtypes.NewValidator(valAddr2Str, valConsPk1, stakingtypes.Description{})
val2, err := stakingtypes.NewValidator(valAddr2Str, valConsPk1, stakingtypes.Description{}, address.NewBech32Codec(sdk.Bech32PrefixConsAddr))
require.NoError(t, err)
val2.Commission = stakingtypes.NewCommission(math.LegacyNewDecWithPrec(1, 1), math.LegacyNewDecWithPrec(1, 1), math.LegacyNewDec(0))
stakingKeeper.EXPECT().ValidatorByConsAddr(gomock.Any(), sdk.GetConsAddress(valConsPk2)).Return(val2, nil).AnyTimes()
Expand Down
3 changes: 2 additions & 1 deletion x/distribution/testutil/staking_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ import (
"cosmossdk.io/x/distribution/keeper"
stakingtypes "cosmossdk.io/x/staking/types"

addresscodec "github.com/cosmos/cosmos-sdk/codec/address"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)

func CreateValidator(pk cryptotypes.PubKey, operator string, stake math.Int) (stakingtypes.Validator, error) {
val, err := stakingtypes.NewValidator(operator, pk, stakingtypes.Description{Moniker: "TestValidator"})
val, err := stakingtypes.NewValidator(operator, pk, stakingtypes.Description{Moniker: "TestValidator"}, addresscodec.NewBech32Codec(sdk.Bech32PrefixConsAddr))

Check warning

Code scanning / CodeQL

Directly using the bech32 constants Warning test

Directly using the bech32 constants instead of the configuration values
val.Tokens = stake
val.DelegatorShares = math.LegacyNewDecFromInt(val.Tokens)
return val, err
Expand Down
3 changes: 2 additions & 1 deletion x/slashing/keeper/hooks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package keeper_test
import (
stakingtypes "cosmossdk.io/x/staking/types"

addresscodec "github.com/cosmos/cosmos-sdk/codec/address"
"github.com/cosmos/cosmos-sdk/testutil/testdata"
sdk "github.com/cosmos/cosmos-sdk/types"
)
Expand All @@ -27,7 +28,7 @@ func (s *KeeperTestSuite) TestAfterValidatorCreatedOrRemoved() {

addStr, err := s.stakingKeeper.ValidatorAddressCodec().BytesToString(addr)
require.NoError(err)
validator, err := stakingtypes.NewValidator(addStr, pubKey, stakingtypes.Description{})
validator, err := stakingtypes.NewValidator(addStr, pubKey, stakingtypes.Description{}, addresscodec.NewBech32Codec(sdk.Bech32PrefixConsAddr))
require.NoError(err)

s.stakingKeeper.EXPECT().Validator(ctx, valAddr).Return(validator, nil)
Expand Down
10 changes: 5 additions & 5 deletions x/slashing/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ func (s *KeeperTestSuite) TestUnjail() {
valStr, err := s.stakingKeeper.ValidatorAddressCodec().BytesToString(addr)
s.Require().NoError(err)

val, err := types.NewValidator(valStr, pubKey, types.Description{Moniker: "test"})
val, err := types.NewValidator(valStr, pubKey, types.Description{Moniker: "test"}, addresscodec.NewBech32Codec(sdk.Bech32PrefixConsAddr))
s.Require().NoError(err)

s.stakingKeeper.EXPECT().Validator(s.ctx, valAddr).Return(val, nil)
Expand Down Expand Up @@ -214,7 +214,7 @@ func (s *KeeperTestSuite) TestUnjail() {
consStr, err := s.stakingKeeper.ConsensusAddressCodec().BytesToString(addr)
s.Require().NoError(err)

val, err := types.NewValidator(valStr, pubKey, types.Description{Moniker: "test"})
val, err := types.NewValidator(valStr, pubKey, types.Description{Moniker: "test"}, addresscodec.NewBech32Codec(sdk.Bech32PrefixConsAddr))
val.Tokens = sdkmath.NewInt(1000)
val.DelegatorShares = sdkmath.LegacyNewDec(1)
val.Jailed = false
Expand Down Expand Up @@ -249,7 +249,7 @@ func (s *KeeperTestSuite) TestUnjail() {
consStr, err := s.stakingKeeper.ConsensusAddressCodec().BytesToString(addr)
s.Require().NoError(err)

val, err := types.NewValidator(valStr, pubKey, types.Description{Moniker: "test"})
val, err := types.NewValidator(valStr, pubKey, types.Description{Moniker: "test"}, addresscodec.NewBech32Codec(sdk.Bech32PrefixConsAddr))
val.Tokens = sdkmath.NewInt(1000)
val.DelegatorShares = sdkmath.LegacyNewDec(1)
val.Jailed = true
Expand Down Expand Up @@ -284,7 +284,7 @@ func (s *KeeperTestSuite) TestUnjail() {
addrStr, err := ac.BytesToString(addr)
s.Require().NoError(err)

val, err := types.NewValidator(valStr, pubKey, types.Description{Moniker: "test"})
val, err := types.NewValidator(valStr, pubKey, types.Description{Moniker: "test"}, addresscodec.NewBech32Codec(sdk.Bech32PrefixConsAddr))
val.Tokens = sdkmath.NewInt(1000)
val.DelegatorShares = sdkmath.LegacyNewDec(1)
val.Jailed = true
Expand Down Expand Up @@ -319,7 +319,7 @@ func (s *KeeperTestSuite) TestUnjail() {
consStr, err := s.stakingKeeper.ConsensusAddressCodec().BytesToString(addr)
s.Require().NoError(err)

val, err := types.NewValidator(valStr, pubKey, types.Description{Moniker: "test"})
val, err := types.NewValidator(valStr, pubKey, types.Description{Moniker: "test"}, addresscodec.NewBech32Codec(sdk.Bech32PrefixConsAddr))
val.Tokens = sdkmath.NewInt(1000)
val.DelegatorShares = sdkmath.LegacyNewDec(1)

Expand Down
1 change: 1 addition & 0 deletions x/staking/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
### Features

* [#19537](https://github.com/cosmos/cosmos-sdk/pull/19537) Changing `MinCommissionRate` in `MsgUpdateParams` now updates the minimum commission rate for all validators.
* [#20434](https://github.com/cosmos/cosmos-sdk/pull/20434) Add consensus address to validator query response

### Improvements

Expand Down
8 changes: 4 additions & 4 deletions x/staking/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (k Querier) Validators(ctx context.Context, req *types.QueryValidatorsReque
return nil, nil
}

setConsensusAddress(val)
k.setConsensusAddress(val)
return val, nil
}, func() *types.Validator {
return &types.Validator{}
Expand Down Expand Up @@ -86,7 +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)
k.setConsensusAddress(&validator)
return &types.QueryValidatorResponse{Validator: validator}, nil
}

Expand Down Expand Up @@ -654,7 +654,7 @@ func redelegationsToRedelegationResponses(ctx context.Context, k *Keeper, redels
}

// setConsensusAddress sets the ConsensusAddress field for the given validator
func setConsensusAddress(validator *types.Validator) {
func (k Querier) setConsensusAddress(validator *types.Validator) {
if validator == nil {
return
}
Expand All @@ -663,6 +663,6 @@ func setConsensusAddress(validator *types.Validator) {
// Best-effort way
if ok {
consAddr := sdk.ConsAddress(cpk.Address())
validator.ConsensusAddress = consAddr.String()
validator.ConsensusAddress, _ = k.consensusAddressCodec.BytesToString(consAddr)
}
}
3 changes: 2 additions & 1 deletion x/staking/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
consensusv1 "cosmossdk.io/x/consensus/types"
"cosmossdk.io/x/staking/types"

addresscodec "github.com/cosmos/cosmos-sdk/codec/address"
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
"github.com/cosmos/cosmos-sdk/telemetry"
Expand Down Expand Up @@ -108,7 +109,7 @@ func (k msgServer) CreateValidator(ctx context.Context, msg *types.MsgCreateVali
return nil, err
}

validator, err := types.NewValidator(msg.ValidatorAddress, pk, msg.Description)
validator, err := types.NewValidator(msg.ValidatorAddress, pk, msg.Description, addresscodec.NewBech32Codec(sdk.Bech32PrefixConsAddr))

Check warning

Code scanning / CodeQL

Directly using the bech32 constants Warning

Directly using the bech32 constants instead of the configuration values
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion x/staking/proto/cosmos/staking/v1beta1/staking.proto
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ message 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"];
string consensus_address = 14 [(cosmos_proto.scalar) = "cosmos.ConsensusAddressString"];
}

// BondStatus is the status of a validator.
Expand Down
3 changes: 2 additions & 1 deletion x/staking/simulation/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
sdkmath "cosmossdk.io/math"
"cosmossdk.io/x/staking/types"

addresscodec "github.com/cosmos/cosmos-sdk/codec/address"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
"github.com/cosmos/cosmos-sdk/types/simulation"
Expand Down Expand Up @@ -89,7 +90,7 @@ func RandomizedGenState(simState *module.SimulationState) {
simulation.RandomDecAmount(simState.Rand, maxCommission),
)

validator, err := types.NewValidator(ValStrAddress, simState.Accounts[i].ConsKey.PubKey(), types.Description{})
validator, err := types.NewValidator(ValStrAddress, simState.Accounts[i].ConsKey.PubKey(), types.Description{}, addresscodec.NewBech32Codec(sdk.Bech32PrefixConsAddr))

Check warning

Code scanning / CodeQL

Directly using the bech32 constants Warning

Directly using the bech32 constants instead of the configuration values
if err != nil {
panic(err)
}
Expand Down
3 changes: 2 additions & 1 deletion x/staking/testutil/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"cosmossdk.io/x/staking/types"

addresscodec "github.com/cosmos/cosmos-sdk/codec/address"
codectestutil "github.com/cosmos/cosmos-sdk/codec/testutil"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand All @@ -17,7 +18,7 @@ func NewValidator(tb testing.TB, operator sdk.ValAddress, pubKey cryptotypes.Pub
tb.Helper()
operatorAddr, err := codectestutil.CodecOptions{}.GetValidatorCodec().BytesToString(operator)
require.NoError(tb, err)
v, err := types.NewValidator(operatorAddr, pubKey, types.Description{})
v, err := types.NewValidator(operatorAddr, pubKey, types.Description{}, addresscodec.NewBech32Codec(sdk.Bech32PrefixConsAddr))

Check warning

Code scanning / CodeQL

Directly using the bech32 constants Warning test

Directly using the bech32 constants instead of the configuration values
require.NoError(tb, err)
return v
}
Loading

0 comments on commit 424b463

Please sign in to comment.