Skip to content

Commit 93770f5

Browse files
authored
refactor(x/slashing): remove global bech32 usage (#18115)
1 parent 0fc1089 commit 93770f5

21 files changed

+177
-84
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
166166
* (x/auth) [#17985](https://github.com/cosmos/cosmos-sdk/pull/17985) Remove `StdTxConfig`
167167
* Remove depreacted `MakeTestingEncodingParams` from `simapp/params`
168168
* (x/consensus) [#18041](https://github.com/cosmos/cosmos-sdk/pull/18041) `ToProtoConsensusParams()` returns an error
169+
* (x/slashing) [#18115](https://github.com/cosmos/cosmos-sdk/pull/18115) `NewValidatorSigningInfo` takes strings instead of `sdk.AccAddress`
169170

170171
### CLI Breaking Changes
171172

tests/integration/evidence/keeper/infraction_test.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,9 @@ func TestHandleDoubleSign(t *testing.T) {
194194

195195
assert.NilError(t, f.slashingKeeper.AddrPubkeyRelation.Set(f.sdkCtx, valpubkey.Address(), valpubkey))
196196

197-
info := slashingtypes.NewValidatorSigningInfo(sdk.ConsAddress(valpubkey.Address()), f.sdkCtx.BlockHeight(), int64(0), time.Unix(0, 0), false, int64(0))
197+
consaddrStr, err := f.stakingKeeper.ConsensusAddressCodec().BytesToString(valpubkey.Address())
198+
assert.NilError(t, err)
199+
info := slashingtypes.NewValidatorSigningInfo(consaddrStr, f.sdkCtx.BlockHeight(), int64(0), time.Unix(0, 0), false, int64(0))
198200
err = f.slashingKeeper.ValidatorSigningInfo.Set(f.sdkCtx, sdk.ConsAddress(valpubkey.Address()), info)
199201
assert.NilError(t, err)
200202
// handle a signature to set signing info

tests/integration/slashing/keeper/keeper_test.go

+21-7
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,13 @@ func initFixture(tb testing.TB) *fixture {
120120
addrDels := simtestutil.AddTestAddrsIncremental(bankKeeper, stakingKeeper, sdkCtx, 6, stakingKeeper.TokensFromConsensusPower(sdkCtx, 200))
121121
valAddrs := simtestutil.ConvertAddrsToValAddrs(addrDels)
122122

123-
info1 := slashingtypes.NewValidatorSigningInfo(sdk.ConsAddress(addrDels[0]), int64(4), int64(3), time.Unix(2, 0), false, int64(10))
124-
info2 := slashingtypes.NewValidatorSigningInfo(sdk.ConsAddress(addrDels[1]), int64(5), int64(4), time.Unix(2, 0), false, int64(10))
123+
consaddr0, err := stakingKeeper.ConsensusAddressCodec().BytesToString(addrDels[0])
124+
assert.NilError(tb, err)
125+
consaddr1, err := stakingKeeper.ConsensusAddressCodec().BytesToString(addrDels[1])
126+
assert.NilError(tb, err)
127+
128+
info1 := slashingtypes.NewValidatorSigningInfo(consaddr0, int64(4), int64(3), time.Unix(2, 0), false, int64(10))
129+
info2 := slashingtypes.NewValidatorSigningInfo(consaddr1, int64(5), int64(4), time.Unix(2, 0), false, int64(10))
125130

126131
err = slashingKeeper.ValidatorSigningInfo.Set(sdkCtx, sdk.ConsAddress(addrDels[0]), info1)
127132
assert.NilError(tb, err)
@@ -234,7 +239,10 @@ func TestHandleNewValidator(t *testing.T) {
234239

235240
assert.NilError(t, f.slashingKeeper.AddrPubkeyRelation.Set(f.ctx, pks[0].Address(), pks[0]))
236241

237-
info := slashingtypes.NewValidatorSigningInfo(sdk.ConsAddress(valpubkey.Address()), f.ctx.BlockHeight(), int64(0), time.Unix(0, 0), false, int64(0))
242+
consaddr, err := f.stakingKeeper.ConsensusAddressCodec().BytesToString(valpubkey.Address())
243+
assert.NilError(t, err)
244+
245+
info := slashingtypes.NewValidatorSigningInfo(consaddr, f.ctx.BlockHeight(), int64(0), time.Unix(0, 0), false, int64(0))
238246
assert.NilError(t, f.slashingKeeper.ValidatorSigningInfo.Set(f.ctx, sdk.ConsAddress(valpubkey.Address()), info))
239247

240248
// Validator created
@@ -289,7 +297,10 @@ func TestHandleAlreadyJailed(t *testing.T) {
289297
err := f.slashingKeeper.AddrPubkeyRelation.Set(f.ctx, pks[0].Address(), pks[0])
290298
assert.NilError(t, err)
291299

292-
info := slashingtypes.NewValidatorSigningInfo(sdk.ConsAddress(val.Address()), f.ctx.BlockHeight(), int64(0), time.Unix(0, 0), false, int64(0))
300+
consaddr, err := f.stakingKeeper.ConsensusAddressCodec().BytesToString(val.Address())
301+
assert.NilError(t, err)
302+
303+
info := slashingtypes.NewValidatorSigningInfo(consaddr, f.ctx.BlockHeight(), int64(0), time.Unix(0, 0), false, int64(0))
293304
assert.NilError(t, f.slashingKeeper.ValidatorSigningInfo.Set(f.ctx, sdk.ConsAddress(val.Address()), info))
294305

295306
amt := tstaking.CreateValidatorWithValPower(addr, val, power, true)
@@ -363,7 +374,10 @@ func TestValidatorDippingInAndOut(t *testing.T) {
363374

364375
assert.NilError(t, f.slashingKeeper.AddrPubkeyRelation.Set(f.ctx, pks[0].Address(), pks[0]))
365376

366-
info := slashingtypes.NewValidatorSigningInfo(consAddr, f.ctx.BlockHeight(), int64(0), time.Unix(0, 0), false, int64(0))
377+
consaddrStr, err := f.stakingKeeper.ConsensusAddressCodec().BytesToString(addr)
378+
assert.NilError(t, err)
379+
380+
info := slashingtypes.NewValidatorSigningInfo(consaddrStr, f.ctx.BlockHeight(), int64(0), time.Unix(0, 0), false, int64(0))
367381
assert.NilError(t, f.slashingKeeper.ValidatorSigningInfo.Set(f.ctx, consAddr, info))
368382

369383
tstaking.CreateValidatorWithValPower(valAddr, val, power, true)
@@ -424,7 +438,7 @@ func TestValidatorDippingInAndOut(t *testing.T) {
424438
assert.NilError(t, err)
425439
tstaking.CheckValidator(valAddr, stakingtypes.Unbonding, true)
426440

427-
info = slashingtypes.NewValidatorSigningInfo(consAddr, f.ctx.BlockHeight(), int64(0), time.Unix(0, 0), false, int64(0))
441+
info = slashingtypes.NewValidatorSigningInfo(consaddrStr, f.ctx.BlockHeight(), int64(0), time.Unix(0, 0), false, int64(0))
428442
err = f.slashingKeeper.ValidatorSigningInfo.Set(f.ctx, consAddr, info)
429443
assert.NilError(t, err)
430444

@@ -439,7 +453,7 @@ func TestValidatorDippingInAndOut(t *testing.T) {
439453
height = int64(5000)
440454
f.ctx = f.ctx.WithBlockHeight(height)
441455

442-
info = slashingtypes.NewValidatorSigningInfo(consAddr, f.ctx.BlockHeight(), int64(0), time.Unix(0, 0), false, int64(0))
456+
info = slashingtypes.NewValidatorSigningInfo(consaddrStr, f.ctx.BlockHeight(), int64(0), time.Unix(0, 0), false, int64(0))
443457
err = f.slashingKeeper.ValidatorSigningInfo.Set(f.ctx, consAddr, info)
444458
assert.NilError(t, err)
445459

x/slashing/app_test.go

+13-6
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"cosmossdk.io/log"
1313
"cosmossdk.io/math"
1414

15+
codecaddress "github.com/cosmos/cosmos-sdk/codec/address"
1516
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
1617
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
1718
"github.com/cosmos/cosmos-sdk/testutil/configurator"
@@ -27,8 +28,10 @@ import (
2728
)
2829

2930
var (
30-
priv1 = secp256k1.GenPrivKey()
31-
addr1 = sdk.AccAddress(priv1.PubKey().Address())
31+
priv1 = secp256k1.GenPrivKey()
32+
addr1 = sdk.AccAddress(priv1.PubKey().Address())
33+
addrCodec = codecaddress.NewBech32Codec("cosmos")
34+
valaddrCodec = codecaddress.NewBech32Codec("cosmosvaloper")
3235

3336
valKey = ed25519.GenPrivKey()
3437
valAddr = sdk.AccAddress(valKey.PubKey().Address())
@@ -40,8 +43,10 @@ func TestSlashingMsgs(t *testing.T) {
4043
genCoin := sdk.NewCoin(sdk.DefaultBondDenom, genTokens)
4144
bondCoin := sdk.NewCoin(sdk.DefaultBondDenom, bondTokens)
4245

46+
addrStr, err := addrCodec.BytesToString(addr1)
47+
require.NoError(t, err)
4348
acc1 := &authtypes.BaseAccount{
44-
Address: addr1.String(),
49+
Address: addrStr,
4550
}
4651
accs := []sims.GenesisAccount{{GenesisAccount: acc1, Coins: sdk.Coins{genCoin}}}
4752

@@ -79,8 +84,10 @@ func TestSlashingMsgs(t *testing.T) {
7984
description := stakingtypes.NewDescription("foo_moniker", "", "", "", "")
8085
commission := stakingtypes.NewCommissionRates(math.LegacyZeroDec(), math.LegacyZeroDec(), math.LegacyZeroDec())
8186

87+
addrStrVal, err := valaddrCodec.BytesToString(addr1)
88+
require.NoError(t, err)
8289
createValidatorMsg, err := stakingtypes.NewMsgCreateValidator(
83-
sdk.ValAddress(addr1).String(), valKey.PubKey(), bondCoin, description, commission, math.OneInt(),
90+
addrStrVal, valKey.PubKey(), bondCoin, description, commission, math.OneInt(),
8491
)
8592
require.NoError(t, err)
8693

@@ -96,10 +103,10 @@ func TestSlashingMsgs(t *testing.T) {
96103
validator, err := stakingKeeper.GetValidator(ctxCheck, sdk.ValAddress(addr1))
97104
require.NoError(t, err)
98105

99-
require.Equal(t, sdk.ValAddress(addr1).String(), validator.OperatorAddress)
106+
require.Equal(t, addrStrVal, validator.OperatorAddress)
100107
require.Equal(t, stakingtypes.Bonded, validator.Status)
101108
require.True(math.IntEq(t, bondTokens, validator.BondedTokens()))
102-
unjailMsg := &types.MsgUnjail{ValidatorAddr: sdk.ValAddress(addr1).String()}
109+
unjailMsg := &types.MsgUnjail{ValidatorAddr: addrStrVal}
103110

104111
ctxCheck = app.BaseApp.NewContext(true)
105112
_, err = slashingKeeper.ValidatorSigningInfo.Get(ctxCheck, sdk.ConsAddress(valAddr))

x/slashing/keeper/genesis.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,10 @@ func (keeper Keeper) ExportGenesis(ctx context.Context) (data *types.GenesisStat
6969
signingInfos := make([]types.SigningInfo, 0)
7070
missedBlocks := make([]types.ValidatorMissedBlocks, 0)
7171
err = keeper.ValidatorSigningInfo.Walk(ctx, nil, func(address sdk.ConsAddress, info types.ValidatorSigningInfo) (stop bool, err error) {
72-
bechAddr := address.String()
72+
bechAddr, err := keeper.sk.ConsensusAddressCodec().BytesToString(address)
73+
if err != nil {
74+
panic(err)
75+
}
7376
signingInfos = append(signingInfos, types.SigningInfo{
7477
Address: bechAddr,
7578
ValidatorSigningInfo: info,

x/slashing/keeper/genesis_test.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,14 @@ func (s *KeeperTestSuite) TestExportAndInitGenesis() {
1717
s.Require().NoError(err)
1818
consAddr1 := sdk.ConsAddress(([]byte("addr1_______________")))
1919
consAddr2 := sdk.ConsAddress(([]byte("addr2_______________")))
20+
consStr1, err := s.stakingKeeper.ConsensusAddressCodec().BytesToString(consAddr1)
21+
require.NoError(err)
22+
consStr2, err := s.stakingKeeper.ConsensusAddressCodec().BytesToString(consAddr2)
23+
require.NoError(err)
2024

21-
info1 := types.NewValidatorSigningInfo(consAddr1, int64(4), int64(3),
25+
info1 := types.NewValidatorSigningInfo(consStr1, int64(4), int64(3),
2226
time.Now().UTC().Add(100000000000), false, int64(10))
23-
info2 := types.NewValidatorSigningInfo(consAddr2, int64(5), int64(4),
27+
info2 := types.NewValidatorSigningInfo(consStr2, int64(5), int64(4),
2428
time.Now().UTC().Add(10000000000), false, int64(10))
2529

2630
s.Require().NoError(keeper.ValidatorSigningInfo.Set(ctx, consAddr1, info1))

x/slashing/keeper/grpc_query_test.go

+11-4
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,11 @@ func (s *KeeperTestSuite) TestGRPCSigningInfo() {
2929
require.ErrorContains(err, "invalid request")
3030
require.Nil(infoResp)
3131

32+
consStr, err := s.stakingKeeper.ConsensusAddressCodec().BytesToString(consAddr)
33+
require.NoError(err)
34+
3235
signingInfo := slashingtypes.NewValidatorSigningInfo(
33-
consAddr,
36+
consStr,
3437
0,
3538
int64(0),
3639
time.Unix(2, 0),
@@ -42,8 +45,10 @@ func (s *KeeperTestSuite) TestGRPCSigningInfo() {
4245
info, err := keeper.ValidatorSigningInfo.Get(ctx, consAddr)
4346
require.NoError(err)
4447

48+
consAddrStr, err := s.stakingKeeper.ConsensusAddressCodec().BytesToString(consAddr)
49+
require.NoError(err)
4550
infoResp, err = queryClient.SigningInfo(gocontext.Background(),
46-
&slashingtypes.QuerySigningInfoRequest{ConsAddress: consAddr.String()})
51+
&slashingtypes.QuerySigningInfoRequest{ConsAddress: consAddrStr})
4752
require.NoError(err)
4853
require.Equal(info, infoResp.ValSigningInfo)
4954
}
@@ -54,9 +59,11 @@ func (s *KeeperTestSuite) TestGRPCSigningInfos() {
5459

5560
// set two validator signing information
5661
consAddr1 := sdk.ConsAddress(sdk.AccAddress([]byte("addr1_______________")))
62+
consStr1, err := s.stakingKeeper.ConsensusAddressCodec().BytesToString(consAddr1)
63+
require.NoError(err)
5764
consAddr2 := sdk.ConsAddress(sdk.AccAddress([]byte("addr2_______________")))
5865
signingInfo := slashingtypes.NewValidatorSigningInfo(
59-
consAddr1,
66+
consStr1,
6067
0,
6168
int64(0),
6269
time.Unix(2, 0),
@@ -69,7 +76,7 @@ func (s *KeeperTestSuite) TestGRPCSigningInfos() {
6976
require.NoError(keeper.ValidatorSigningInfo.Set(ctx, consAddr2, signingInfo))
7077
var signingInfos []slashingtypes.ValidatorSigningInfo
7178

72-
err := keeper.ValidatorSigningInfo.Walk(ctx, nil, func(consAddr sdk.ConsAddress, info slashingtypes.ValidatorSigningInfo) (stop bool, err error) {
79+
err = keeper.ValidatorSigningInfo.Walk(ctx, nil, func(consAddr sdk.ConsAddress, info slashingtypes.ValidatorSigningInfo) (stop bool, err error) {
7380
signingInfos = append(signingInfos, info)
7481
return false, nil
7582
})

x/slashing/keeper/hooks.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,12 @@ func (h Hooks) AfterValidatorBonded(ctx context.Context, consAddr sdk.ConsAddres
3131
if err == nil {
3232
signingInfo.StartHeight = sdkCtx.BlockHeight()
3333
} else {
34+
consStr, err := h.k.sk.ConsensusAddressCodec().BytesToString(consAddr)
35+
if err != nil {
36+
return err
37+
}
3438
signingInfo = types.NewValidatorSigningInfo(
35-
consAddr,
39+
consStr,
3640
sdkCtx.BlockHeight(),
3741
0,
3842
time.Unix(0, 0),

x/slashing/keeper/hooks_test.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ func (s *KeeperTestSuite) TestAfterValidatorCreatedOrRemoved() {
2424
_, pubKey, addr := testdata.KeyTestPubAddr()
2525
valAddr := sdk.ValAddress(addr)
2626

27-
validator, err := stakingtypes.NewValidator(sdk.ValAddress(addr).String(), pubKey, stakingtypes.Description{})
27+
addStr, err := s.stakingKeeper.ValidatorAddressCodec().BytesToString(addr)
28+
require.NoError(err)
29+
validator, err := stakingtypes.NewValidator(addStr, pubKey, stakingtypes.Description{})
2830
require.NoError(err)
2931

3032
s.stakingKeeper.EXPECT().Validator(ctx, valAddr).Return(validator, nil)

x/slashing/keeper/infractions.go

+11-6
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,16 @@ func (k Keeper) HandleValidatorSignature(ctx context.Context, addr cryptotypes.A
8787
return err
8888
}
8989

90+
consStr, err := k.sk.ConsensusAddressCodec().BytesToString(consAddr)
91+
if err != nil {
92+
return err
93+
}
94+
9095
if missed {
9196
sdkCtx.EventManager().EmitEvent(
9297
sdk.NewEvent(
9398
types.EventTypeLiveness,
94-
sdk.NewAttribute(types.AttributeKeyAddress, consAddr.String()),
99+
sdk.NewAttribute(types.AttributeKeyAddress, consStr),
95100
sdk.NewAttribute(types.AttributeKeyMissedBlocks, fmt.Sprintf("%d", signInfo.MissedBlocksCounter)),
96101
sdk.NewAttribute(types.AttributeKeyHeight, fmt.Sprintf("%d", height)),
97102
),
@@ -100,7 +105,7 @@ func (k Keeper) HandleValidatorSignature(ctx context.Context, addr cryptotypes.A
100105
logger.Debug(
101106
"absent validator",
102107
"height", height,
103-
"validator", consAddr.String(),
108+
"validator", consStr,
104109
"missed", signInfo.MissedBlocksCounter,
105110
"threshold", minSignedPerWindow,
106111
)
@@ -137,10 +142,10 @@ func (k Keeper) HandleValidatorSignature(ctx context.Context, addr cryptotypes.A
137142
sdkCtx.EventManager().EmitEvent(
138143
sdk.NewEvent(
139144
types.EventTypeSlash,
140-
sdk.NewAttribute(types.AttributeKeyAddress, consAddr.String()),
145+
sdk.NewAttribute(types.AttributeKeyAddress, consStr),
141146
sdk.NewAttribute(types.AttributeKeyPower, fmt.Sprintf("%d", power)),
142147
sdk.NewAttribute(types.AttributeKeyReason, types.AttributeValueMissingSignature),
143-
sdk.NewAttribute(types.AttributeKeyJailed, consAddr.String()),
148+
sdk.NewAttribute(types.AttributeKeyJailed, consStr),
144149
sdk.NewAttribute(types.AttributeKeyBurnedCoins, coinsBurned.String()),
145150
),
146151
)
@@ -166,7 +171,7 @@ func (k Keeper) HandleValidatorSignature(ctx context.Context, addr cryptotypes.A
166171
logger.Info(
167172
"slashing and jailing validator due to liveness fault",
168173
"height", height,
169-
"validator", consAddr.String(),
174+
"validator", consStr,
170175
"min_height", minHeight,
171176
"threshold", minSignedPerWindow,
172177
"slashed", slashFractionDowntime.String(),
@@ -176,7 +181,7 @@ func (k Keeper) HandleValidatorSignature(ctx context.Context, addr cryptotypes.A
176181
// validator was (a) not found or (b) already jailed so we do not slash
177182
logger.Info(
178183
"validator would have been slashed for downtime, but was either not found in store or already jailed",
179-
"validator", consAddr.String(),
184+
"validator", consStr,
180185
)
181186
}
182187
}

x/slashing/keeper/keeper.go

+12-2
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,16 @@ func (k Keeper) SlashWithInfractionReason(ctx context.Context, consAddr sdk.Cons
115115
reasonAttr = sdk.NewAttribute(types.AttributeKeyReason, types.AttributeValueMissingSignature)
116116
}
117117

118+
consStr, err := k.sk.ConsensusAddressCodec().BytesToString(consAddr)
119+
if err != nil {
120+
return err
121+
}
122+
118123
sdkCtx := sdk.UnwrapSDKContext(ctx)
119124
sdkCtx.EventManager().EmitEvent(
120125
sdk.NewEvent(
121126
types.EventTypeSlash,
122-
sdk.NewAttribute(types.AttributeKeyAddress, consAddr.String()),
127+
sdk.NewAttribute(types.AttributeKeyAddress, consStr),
123128
sdk.NewAttribute(types.AttributeKeyPower, fmt.Sprintf("%d", power)),
124129
reasonAttr,
125130
sdk.NewAttribute(types.AttributeKeyBurnedCoins, coinsBurned.String()),
@@ -136,10 +141,15 @@ func (k Keeper) Jail(ctx context.Context, consAddr sdk.ConsAddress) error {
136141
if err != nil {
137142
return err
138143
}
144+
consStr, err := k.sk.ConsensusAddressCodec().BytesToString(consAddr)
145+
if err != nil {
146+
return err
147+
}
148+
139149
sdkCtx.EventManager().EmitEvent(
140150
sdk.NewEvent(
141151
types.EventTypeSlash,
142-
sdk.NewAttribute(types.AttributeKeyJailed, consAddr.String()),
152+
sdk.NewAttribute(types.AttributeKeyJailed, consStr),
143153
),
144154
)
145155
return nil

x/slashing/keeper/keeper_test.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,19 @@ func (s *KeeperTestSuite) SetupTest() {
5555
s.stakingKeeper.EXPECT().ValidatorAddressCodec().Return(address.NewBech32Codec("cosmosvaloper")).AnyTimes()
5656
s.stakingKeeper.EXPECT().ConsensusAddressCodec().Return(address.NewBech32Codec("cosmosvalcons")).AnyTimes()
5757

58+
authStr, err := address.NewBech32Codec("cosmos").BytesToString(authtypes.NewModuleAddress(govtypes.ModuleName))
59+
s.Require().NoError(err)
60+
5861
s.ctx = ctx
5962
s.slashingKeeper = slashingkeeper.NewKeeper(
6063
encCfg.Codec,
6164
encCfg.Amino,
6265
storeService,
6366
s.stakingKeeper,
64-
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
67+
authStr,
6568
)
6669
// set test params
67-
err := s.slashingKeeper.Params.Set(ctx, slashingtestutil.TestParams())
70+
err = s.slashingKeeper.Params.Set(ctx, slashingtestutil.TestParams())
6871
s.Require().NoError(err)
6972
slashingtypes.RegisterInterfaces(encCfg.InterfaceRegistry)
7073
queryHelper := baseapp.NewQueryServerTestHelper(ctx, encCfg.InterfaceRegistry)

0 commit comments

Comments
 (0)