Skip to content

Commit

Permalink
refactor(x/slashing): remove global bech32 usage (#18115)
Browse files Browse the repository at this point in the history
  • Loading branch information
tac0turtle authored Oct 17, 2023
1 parent 0fc1089 commit 93770f5
Show file tree
Hide file tree
Showing 21 changed files with 177 additions and 84 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
* (x/auth) [#17985](https://github.com/cosmos/cosmos-sdk/pull/17985) Remove `StdTxConfig`
* Remove depreacted `MakeTestingEncodingParams` from `simapp/params`
* (x/consensus) [#18041](https://github.com/cosmos/cosmos-sdk/pull/18041) `ToProtoConsensusParams()` returns an error
* (x/slashing) [#18115](https://github.com/cosmos/cosmos-sdk/pull/18115) `NewValidatorSigningInfo` takes strings instead of `sdk.AccAddress`

### CLI Breaking Changes

Expand Down
4 changes: 3 additions & 1 deletion tests/integration/evidence/keeper/infraction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,9 @@ func TestHandleDoubleSign(t *testing.T) {

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

info := slashingtypes.NewValidatorSigningInfo(sdk.ConsAddress(valpubkey.Address()), f.sdkCtx.BlockHeight(), int64(0), time.Unix(0, 0), false, int64(0))
consaddrStr, err := f.stakingKeeper.ConsensusAddressCodec().BytesToString(valpubkey.Address())
assert.NilError(t, err)
info := slashingtypes.NewValidatorSigningInfo(consaddrStr, f.sdkCtx.BlockHeight(), int64(0), time.Unix(0, 0), false, int64(0))
err = f.slashingKeeper.ValidatorSigningInfo.Set(f.sdkCtx, sdk.ConsAddress(valpubkey.Address()), info)
assert.NilError(t, err)
// handle a signature to set signing info
Expand Down
28 changes: 21 additions & 7 deletions tests/integration/slashing/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,13 @@ func initFixture(tb testing.TB) *fixture {
addrDels := simtestutil.AddTestAddrsIncremental(bankKeeper, stakingKeeper, sdkCtx, 6, stakingKeeper.TokensFromConsensusPower(sdkCtx, 200))
valAddrs := simtestutil.ConvertAddrsToValAddrs(addrDels)

info1 := slashingtypes.NewValidatorSigningInfo(sdk.ConsAddress(addrDels[0]), int64(4), int64(3), time.Unix(2, 0), false, int64(10))
info2 := slashingtypes.NewValidatorSigningInfo(sdk.ConsAddress(addrDels[1]), int64(5), int64(4), time.Unix(2, 0), false, int64(10))
consaddr0, err := stakingKeeper.ConsensusAddressCodec().BytesToString(addrDels[0])
assert.NilError(tb, err)
consaddr1, err := stakingKeeper.ConsensusAddressCodec().BytesToString(addrDels[1])
assert.NilError(tb, err)

info1 := slashingtypes.NewValidatorSigningInfo(consaddr0, int64(4), int64(3), time.Unix(2, 0), false, int64(10))
info2 := slashingtypes.NewValidatorSigningInfo(consaddr1, int64(5), int64(4), time.Unix(2, 0), false, int64(10))

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

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

info := slashingtypes.NewValidatorSigningInfo(sdk.ConsAddress(valpubkey.Address()), f.ctx.BlockHeight(), int64(0), time.Unix(0, 0), false, int64(0))
consaddr, err := f.stakingKeeper.ConsensusAddressCodec().BytesToString(valpubkey.Address())
assert.NilError(t, err)

info := slashingtypes.NewValidatorSigningInfo(consaddr, f.ctx.BlockHeight(), int64(0), time.Unix(0, 0), false, int64(0))
assert.NilError(t, f.slashingKeeper.ValidatorSigningInfo.Set(f.ctx, sdk.ConsAddress(valpubkey.Address()), info))

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

info := slashingtypes.NewValidatorSigningInfo(sdk.ConsAddress(val.Address()), f.ctx.BlockHeight(), int64(0), time.Unix(0, 0), false, int64(0))
consaddr, err := f.stakingKeeper.ConsensusAddressCodec().BytesToString(val.Address())
assert.NilError(t, err)

info := slashingtypes.NewValidatorSigningInfo(consaddr, f.ctx.BlockHeight(), int64(0), time.Unix(0, 0), false, int64(0))
assert.NilError(t, f.slashingKeeper.ValidatorSigningInfo.Set(f.ctx, sdk.ConsAddress(val.Address()), info))

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

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

info := slashingtypes.NewValidatorSigningInfo(consAddr, f.ctx.BlockHeight(), int64(0), time.Unix(0, 0), false, int64(0))
consaddrStr, err := f.stakingKeeper.ConsensusAddressCodec().BytesToString(addr)
assert.NilError(t, err)

info := slashingtypes.NewValidatorSigningInfo(consaddrStr, f.ctx.BlockHeight(), int64(0), time.Unix(0, 0), false, int64(0))
assert.NilError(t, f.slashingKeeper.ValidatorSigningInfo.Set(f.ctx, consAddr, info))

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

info = slashingtypes.NewValidatorSigningInfo(consAddr, f.ctx.BlockHeight(), int64(0), time.Unix(0, 0), false, int64(0))
info = slashingtypes.NewValidatorSigningInfo(consaddrStr, f.ctx.BlockHeight(), int64(0), time.Unix(0, 0), false, int64(0))
err = f.slashingKeeper.ValidatorSigningInfo.Set(f.ctx, consAddr, info)
assert.NilError(t, err)

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

info = slashingtypes.NewValidatorSigningInfo(consAddr, f.ctx.BlockHeight(), int64(0), time.Unix(0, 0), false, int64(0))
info = slashingtypes.NewValidatorSigningInfo(consaddrStr, f.ctx.BlockHeight(), int64(0), time.Unix(0, 0), false, int64(0))
err = f.slashingKeeper.ValidatorSigningInfo.Set(f.ctx, consAddr, info)
assert.NilError(t, err)

Expand Down
19 changes: 13 additions & 6 deletions x/slashing/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"cosmossdk.io/log"
"cosmossdk.io/math"

codecaddress "github.com/cosmos/cosmos-sdk/codec/address"
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
"github.com/cosmos/cosmos-sdk/testutil/configurator"
Expand All @@ -27,8 +28,10 @@ import (
)

var (
priv1 = secp256k1.GenPrivKey()
addr1 = sdk.AccAddress(priv1.PubKey().Address())
priv1 = secp256k1.GenPrivKey()
addr1 = sdk.AccAddress(priv1.PubKey().Address())
addrCodec = codecaddress.NewBech32Codec("cosmos")
valaddrCodec = codecaddress.NewBech32Codec("cosmosvaloper")

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

addrStr, err := addrCodec.BytesToString(addr1)
require.NoError(t, err)
acc1 := &authtypes.BaseAccount{
Address: addr1.String(),
Address: addrStr,
}
accs := []sims.GenesisAccount{{GenesisAccount: acc1, Coins: sdk.Coins{genCoin}}}

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

addrStrVal, err := valaddrCodec.BytesToString(addr1)
require.NoError(t, err)
createValidatorMsg, err := stakingtypes.NewMsgCreateValidator(
sdk.ValAddress(addr1).String(), valKey.PubKey(), bondCoin, description, commission, math.OneInt(),
addrStrVal, valKey.PubKey(), bondCoin, description, commission, math.OneInt(),
)
require.NoError(t, err)

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

require.Equal(t, sdk.ValAddress(addr1).String(), validator.OperatorAddress)
require.Equal(t, addrStrVal, validator.OperatorAddress)
require.Equal(t, stakingtypes.Bonded, validator.Status)
require.True(math.IntEq(t, bondTokens, validator.BondedTokens()))
unjailMsg := &types.MsgUnjail{ValidatorAddr: sdk.ValAddress(addr1).String()}
unjailMsg := &types.MsgUnjail{ValidatorAddr: addrStrVal}

ctxCheck = app.BaseApp.NewContext(true)
_, err = slashingKeeper.ValidatorSigningInfo.Get(ctxCheck, sdk.ConsAddress(valAddr))
Expand Down
5 changes: 4 additions & 1 deletion x/slashing/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,10 @@ func (keeper Keeper) ExportGenesis(ctx context.Context) (data *types.GenesisStat
signingInfos := make([]types.SigningInfo, 0)
missedBlocks := make([]types.ValidatorMissedBlocks, 0)
err = keeper.ValidatorSigningInfo.Walk(ctx, nil, func(address sdk.ConsAddress, info types.ValidatorSigningInfo) (stop bool, err error) {
bechAddr := address.String()
bechAddr, err := keeper.sk.ConsensusAddressCodec().BytesToString(address)
if err != nil {
panic(err)
}
signingInfos = append(signingInfos, types.SigningInfo{
Address: bechAddr,
ValidatorSigningInfo: info,
Expand Down
8 changes: 6 additions & 2 deletions x/slashing/keeper/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@ func (s *KeeperTestSuite) TestExportAndInitGenesis() {
s.Require().NoError(err)
consAddr1 := sdk.ConsAddress(([]byte("addr1_______________")))
consAddr2 := sdk.ConsAddress(([]byte("addr2_______________")))
consStr1, err := s.stakingKeeper.ConsensusAddressCodec().BytesToString(consAddr1)
require.NoError(err)
consStr2, err := s.stakingKeeper.ConsensusAddressCodec().BytesToString(consAddr2)
require.NoError(err)

info1 := types.NewValidatorSigningInfo(consAddr1, int64(4), int64(3),
info1 := types.NewValidatorSigningInfo(consStr1, int64(4), int64(3),
time.Now().UTC().Add(100000000000), false, int64(10))
info2 := types.NewValidatorSigningInfo(consAddr2, int64(5), int64(4),
info2 := types.NewValidatorSigningInfo(consStr2, int64(5), int64(4),
time.Now().UTC().Add(10000000000), false, int64(10))

s.Require().NoError(keeper.ValidatorSigningInfo.Set(ctx, consAddr1, info1))
Expand Down
15 changes: 11 additions & 4 deletions x/slashing/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,11 @@ func (s *KeeperTestSuite) TestGRPCSigningInfo() {
require.ErrorContains(err, "invalid request")
require.Nil(infoResp)

consStr, err := s.stakingKeeper.ConsensusAddressCodec().BytesToString(consAddr)
require.NoError(err)

signingInfo := slashingtypes.NewValidatorSigningInfo(
consAddr,
consStr,
0,
int64(0),
time.Unix(2, 0),
Expand All @@ -42,8 +45,10 @@ func (s *KeeperTestSuite) TestGRPCSigningInfo() {
info, err := keeper.ValidatorSigningInfo.Get(ctx, consAddr)
require.NoError(err)

consAddrStr, err := s.stakingKeeper.ConsensusAddressCodec().BytesToString(consAddr)
require.NoError(err)
infoResp, err = queryClient.SigningInfo(gocontext.Background(),
&slashingtypes.QuerySigningInfoRequest{ConsAddress: consAddr.String()})
&slashingtypes.QuerySigningInfoRequest{ConsAddress: consAddrStr})
require.NoError(err)
require.Equal(info, infoResp.ValSigningInfo)
}
Expand All @@ -54,9 +59,11 @@ func (s *KeeperTestSuite) TestGRPCSigningInfos() {

// set two validator signing information
consAddr1 := sdk.ConsAddress(sdk.AccAddress([]byte("addr1_______________")))
consStr1, err := s.stakingKeeper.ConsensusAddressCodec().BytesToString(consAddr1)
require.NoError(err)
consAddr2 := sdk.ConsAddress(sdk.AccAddress([]byte("addr2_______________")))
signingInfo := slashingtypes.NewValidatorSigningInfo(
consAddr1,
consStr1,
0,
int64(0),
time.Unix(2, 0),
Expand All @@ -69,7 +76,7 @@ func (s *KeeperTestSuite) TestGRPCSigningInfos() {
require.NoError(keeper.ValidatorSigningInfo.Set(ctx, consAddr2, signingInfo))
var signingInfos []slashingtypes.ValidatorSigningInfo

err := keeper.ValidatorSigningInfo.Walk(ctx, nil, func(consAddr sdk.ConsAddress, info slashingtypes.ValidatorSigningInfo) (stop bool, err error) {
err = keeper.ValidatorSigningInfo.Walk(ctx, nil, func(consAddr sdk.ConsAddress, info slashingtypes.ValidatorSigningInfo) (stop bool, err error) {
signingInfos = append(signingInfos, info)
return false, nil
})
Expand Down
6 changes: 5 additions & 1 deletion x/slashing/keeper/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,12 @@ func (h Hooks) AfterValidatorBonded(ctx context.Context, consAddr sdk.ConsAddres
if err == nil {
signingInfo.StartHeight = sdkCtx.BlockHeight()
} else {
consStr, err := h.k.sk.ConsensusAddressCodec().BytesToString(consAddr)
if err != nil {
return err
}
signingInfo = types.NewValidatorSigningInfo(
consAddr,
consStr,
sdkCtx.BlockHeight(),
0,
time.Unix(0, 0),
Expand Down
4 changes: 3 additions & 1 deletion x/slashing/keeper/hooks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ func (s *KeeperTestSuite) TestAfterValidatorCreatedOrRemoved() {
_, pubKey, addr := testdata.KeyTestPubAddr()
valAddr := sdk.ValAddress(addr)

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

s.stakingKeeper.EXPECT().Validator(ctx, valAddr).Return(validator, nil)
Expand Down
17 changes: 11 additions & 6 deletions x/slashing/keeper/infractions.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,16 @@ func (k Keeper) HandleValidatorSignature(ctx context.Context, addr cryptotypes.A
return err
}

consStr, err := k.sk.ConsensusAddressCodec().BytesToString(consAddr)
if err != nil {
return err
}

if missed {
sdkCtx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeLiveness,
sdk.NewAttribute(types.AttributeKeyAddress, consAddr.String()),
sdk.NewAttribute(types.AttributeKeyAddress, consStr),
sdk.NewAttribute(types.AttributeKeyMissedBlocks, fmt.Sprintf("%d", signInfo.MissedBlocksCounter)),
sdk.NewAttribute(types.AttributeKeyHeight, fmt.Sprintf("%d", height)),
),
Expand All @@ -100,7 +105,7 @@ func (k Keeper) HandleValidatorSignature(ctx context.Context, addr cryptotypes.A
logger.Debug(
"absent validator",
"height", height,
"validator", consAddr.String(),
"validator", consStr,
"missed", signInfo.MissedBlocksCounter,
"threshold", minSignedPerWindow,
)
Expand Down Expand Up @@ -137,10 +142,10 @@ func (k Keeper) HandleValidatorSignature(ctx context.Context, addr cryptotypes.A
sdkCtx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeSlash,
sdk.NewAttribute(types.AttributeKeyAddress, consAddr.String()),
sdk.NewAttribute(types.AttributeKeyAddress, consStr),
sdk.NewAttribute(types.AttributeKeyPower, fmt.Sprintf("%d", power)),
sdk.NewAttribute(types.AttributeKeyReason, types.AttributeValueMissingSignature),
sdk.NewAttribute(types.AttributeKeyJailed, consAddr.String()),
sdk.NewAttribute(types.AttributeKeyJailed, consStr),
sdk.NewAttribute(types.AttributeKeyBurnedCoins, coinsBurned.String()),
),
)
Expand All @@ -166,7 +171,7 @@ func (k Keeper) HandleValidatorSignature(ctx context.Context, addr cryptotypes.A
logger.Info(
"slashing and jailing validator due to liveness fault",
"height", height,
"validator", consAddr.String(),
"validator", consStr,
"min_height", minHeight,
"threshold", minSignedPerWindow,
"slashed", slashFractionDowntime.String(),
Expand All @@ -176,7 +181,7 @@ func (k Keeper) HandleValidatorSignature(ctx context.Context, addr cryptotypes.A
// validator was (a) not found or (b) already jailed so we do not slash
logger.Info(
"validator would have been slashed for downtime, but was either not found in store or already jailed",
"validator", consAddr.String(),
"validator", consStr,
)
}
}
Expand Down
14 changes: 12 additions & 2 deletions x/slashing/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,16 @@ func (k Keeper) SlashWithInfractionReason(ctx context.Context, consAddr sdk.Cons
reasonAttr = sdk.NewAttribute(types.AttributeKeyReason, types.AttributeValueMissingSignature)
}

consStr, err := k.sk.ConsensusAddressCodec().BytesToString(consAddr)
if err != nil {
return err
}

sdkCtx := sdk.UnwrapSDKContext(ctx)
sdkCtx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeSlash,
sdk.NewAttribute(types.AttributeKeyAddress, consAddr.String()),
sdk.NewAttribute(types.AttributeKeyAddress, consStr),
sdk.NewAttribute(types.AttributeKeyPower, fmt.Sprintf("%d", power)),
reasonAttr,
sdk.NewAttribute(types.AttributeKeyBurnedCoins, coinsBurned.String()),
Expand All @@ -136,10 +141,15 @@ func (k Keeper) Jail(ctx context.Context, consAddr sdk.ConsAddress) error {
if err != nil {
return err
}
consStr, err := k.sk.ConsensusAddressCodec().BytesToString(consAddr)
if err != nil {
return err
}

sdkCtx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeSlash,
sdk.NewAttribute(types.AttributeKeyJailed, consAddr.String()),
sdk.NewAttribute(types.AttributeKeyJailed, consStr),
),
)
return nil
Expand Down
7 changes: 5 additions & 2 deletions x/slashing/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,19 @@ func (s *KeeperTestSuite) SetupTest() {
s.stakingKeeper.EXPECT().ValidatorAddressCodec().Return(address.NewBech32Codec("cosmosvaloper")).AnyTimes()
s.stakingKeeper.EXPECT().ConsensusAddressCodec().Return(address.NewBech32Codec("cosmosvalcons")).AnyTimes()

authStr, err := address.NewBech32Codec("cosmos").BytesToString(authtypes.NewModuleAddress(govtypes.ModuleName))
s.Require().NoError(err)

s.ctx = ctx
s.slashingKeeper = slashingkeeper.NewKeeper(
encCfg.Codec,
encCfg.Amino,
storeService,
s.stakingKeeper,
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
authStr,
)
// set test params
err := s.slashingKeeper.Params.Set(ctx, slashingtestutil.TestParams())
err = s.slashingKeeper.Params.Set(ctx, slashingtestutil.TestParams())
s.Require().NoError(err)
slashingtypes.RegisterInterfaces(encCfg.InterfaceRegistry)
queryHelper := baseapp.NewQueryServerTestHelper(ctx, encCfg.InterfaceRegistry)
Expand Down
Loading

0 comments on commit 93770f5

Please sign in to comment.