Skip to content

Commit

Permalink
Merge branch 'main' into facu/impl-secp256r1-customtype
Browse files Browse the repository at this point in the history
  • Loading branch information
tac0turtle authored Apr 12, 2024
2 parents ae57709 + ce373b6 commit 3e04a7c
Show file tree
Hide file tree
Showing 15 changed files with 88 additions and 38 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ Every module contains its own CHANGELOG.md. Please refer to the module you are i
* [#19851](https://github.com/cosmos/cosmos-sdk/pull/19851) Fix some places in which we call Remove inside a Walk (x/staking and x/gov).
* (baseapp) [#19970](https://github.com/cosmos/cosmos-sdk/pull/19970) Fix default config values to use no-op mempool as default.
* (crypto) [#20027](https://github.com/cosmos/cosmos-sdk/pull/20027) secp256r1 keys now implement gogoproto's customtype interface.
* (x/bank) [#20028](https://github.com/cosmos/cosmos-sdk/pull/20028) Align query with multi denoms for send-enabled.

### API Breaking Changes

Expand Down
2 changes: 1 addition & 1 deletion x/bank/autocli.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (am AppModule) AutoCLIOptions() *autocliv1.ModuleOptions {
To look up one or more specific denoms, supply them as arguments to this command.
To look up all denoms, do not provide any arguments.`,
),
PositionalArgs: []*autocliv1.PositionalArgDescriptor{{ProtoField: "denoms"}},
PositionalArgs: []*autocliv1.PositionalArgDescriptor{{ProtoField: "denoms", Varargs: true}},
},
},
},
Expand Down
2 changes: 1 addition & 1 deletion x/epochs/simulation/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (

// GenCommunityTax randomized CommunityTax
func GenDuration(r *rand.Rand) time.Duration {
return time.Hour * time.Duration(r.Intn(168)) // limit 1 week
return time.Hour * time.Duration(r.Intn(168)+1) // between 1 hour to 1 week
}

func RandomizedEpochs(r *rand.Rand) []types.EpochInfo {
Expand Down
1 change: 1 addition & 0 deletions x/evidence/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Ref: https://keepachangelog.com/en/1.0.0/

### Api Breaking Changes

* [#20016](https://github.com/cosmos/cosmos-sdk/pull/20016) `NewMsgSubmitEvidence` now takes a string as argument instead of an `AccAddress`.
* [#19482](https://github.com/cosmos/cosmos-sdk/pull/19482) `appmodule.Environment` is passed to `NewKeeper` instead of individual services
* [#19627](https://github.com/cosmos/cosmos-sdk/pull/19627) `NewAppModule` now takes in a `codec.Codec` as its first argument

Expand Down
25 changes: 20 additions & 5 deletions x/evidence/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/stretchr/testify/suite"

"cosmossdk.io/collections"
coreaddress "cosmossdk.io/core/address"
"cosmossdk.io/core/header"
"cosmossdk.io/log"
storetypes "cosmossdk.io/store/types"
Expand Down Expand Up @@ -74,6 +75,9 @@ type KeeperTestSuite struct {

ctx sdk.Context

addressCodec coreaddress.Codec
consAddressCodec coreaddress.ConsensusAddressCodec

evidenceKeeper keeper.Keeper
bankKeeper *evidencetestutil.MockBankKeeper
accountKeeper *evidencetestutil.MockAccountKeeper
Expand All @@ -91,6 +95,8 @@ func (suite *KeeperTestSuite) SetupTest() {
tkey := storetypes.NewTransientStoreKey("evidence_transient_store")
testCtx := testutil.DefaultContextWithDB(suite.T(), key, tkey)
suite.ctx = testCtx.Ctx
suite.addressCodec = address.NewBech32Codec("cosmos")
suite.consAddressCodec = address.NewBech32Codec("cosmosvalcons")

ctrl := gomock.NewController(suite.T())

Expand Down Expand Up @@ -137,11 +143,14 @@ func (suite *KeeperTestSuite) populateEvidence(ctx sdk.Context, numEvidence int)
for i := 0; i < numEvidence; i++ {
pk := ed25519.GenPrivKey()

consAddr, err := suite.consAddressCodec.BytesToString(pk.PubKey().Address())
suite.Require().NoError(err)

evidence[i] = &types.Equivocation{
Height: 11,
Power: 100,
Time: time.Now().UTC(),
ConsensusAddress: sdk.ConsAddress(pk.PubKey().Address().Bytes()).String(),
ConsensusAddress: consAddr,
}

suite.Nil(suite.evidenceKeeper.SubmitEvidence(ctx, evidence[i]))
Expand All @@ -153,12 +162,14 @@ func (suite *KeeperTestSuite) populateEvidence(ctx sdk.Context, numEvidence int)
func (suite *KeeperTestSuite) TestSubmitValidEvidence() {
ctx := suite.ctx.WithIsCheckTx(false)
pk := ed25519.GenPrivKey()
consAddr, err := suite.consAddressCodec.BytesToString(pk.PubKey().Address())
suite.Require().NoError(err)

e := &types.Equivocation{
Height: 1,
Power: 100,
Time: time.Now().UTC(),
ConsensusAddress: sdk.ConsAddress(pk.PubKey().Address().Bytes()).String(),
ConsensusAddress: consAddr,
}

suite.Nil(suite.evidenceKeeper.SubmitEvidence(ctx, e))
Expand All @@ -171,12 +182,14 @@ func (suite *KeeperTestSuite) TestSubmitValidEvidence() {
func (suite *KeeperTestSuite) TestSubmitValidEvidence_Duplicate() {
ctx := suite.ctx.WithIsCheckTx(false)
pk := ed25519.GenPrivKey()
consAddr, err := suite.consAddressCodec.BytesToString(pk.PubKey().Address())
suite.Require().NoError(err)

e := &types.Equivocation{
Height: 1,
Power: 100,
Time: time.Now().UTC(),
ConsensusAddress: sdk.ConsAddress(pk.PubKey().Address().Bytes()).String(),
ConsensusAddress: consAddr,
}

suite.Nil(suite.evidenceKeeper.SubmitEvidence(ctx, e))
Expand All @@ -190,14 +203,16 @@ func (suite *KeeperTestSuite) TestSubmitValidEvidence_Duplicate() {
func (suite *KeeperTestSuite) TestSubmitInvalidEvidence() {
ctx := suite.ctx.WithIsCheckTx(false)
pk := ed25519.GenPrivKey()
consAddr, err := suite.consAddressCodec.BytesToString(pk.PubKey().Address())
suite.Require().NoError(err)
e := &types.Equivocation{
Height: 0,
Power: 100,
Time: time.Now().UTC(),
ConsensusAddress: sdk.ConsAddress(pk.PubKey().Address().Bytes()).String(),
ConsensusAddress: consAddr,
}

err := suite.evidenceKeeper.SubmitEvidence(ctx, e)
err = suite.evidenceKeeper.SubmitEvidence(ctx, e)
suite.ErrorIs(err, types.ErrInvalidEvidence)

res, err := suite.evidenceKeeper.Evidences.Get(ctx, e.Hash())
Expand Down
16 changes: 10 additions & 6 deletions x/evidence/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,34 @@ import (
"cosmossdk.io/x/evidence/types"

"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
sdk "github.com/cosmos/cosmos-sdk/types"
)

func (s *KeeperTestSuite) TestSubmitEvidence() {
pk := ed25519.GenPrivKey()
consAddr, err := s.consAddressCodec.BytesToString(pk.PubKey().Address())
s.Require().NoError(err)

e := &types.Equivocation{
Height: 1,
Power: 100,
Time: time.Now().UTC(),
ConsensusAddress: sdk.ConsAddress(pk.PubKey().Address().Bytes()).String(),
ConsensusAddress: consAddr,
}

validEvidence, err := types.NewMsgSubmitEvidence(sdk.AccAddress(valAddress), e)
accAddr, err := s.addressCodec.BytesToString(valAddress)
s.Require().NoError(err)

validEvidence, err := types.NewMsgSubmitEvidence(accAddr, e)
s.Require().NoError(err)

e2 := &types.Equivocation{
Height: 0,
Power: 100,
Time: time.Now().UTC(),
ConsensusAddress: sdk.ConsAddress(pk.PubKey().Address().Bytes()).String(),
ConsensusAddress: consAddr,
}

invalidEvidence, err := types.NewMsgSubmitEvidence(sdk.AccAddress(valAddress), e2)
invalidEvidence, err := types.NewMsgSubmitEvidence(accAddr, e2)
s.Require().NoError(err)

testCases := []struct {
Expand All @@ -47,7 +51,7 @@ func (s *KeeperTestSuite) TestSubmitEvidence() {
{
name: "missing evidence",
req: &types.MsgSubmitEvidence{
Submitter: sdk.AccAddress(valAddress).String(),
Submitter: accAddr,
},
expErr: true,
expErrMsg: "missing evidence: invalid evidence",
Expand Down
23 changes: 14 additions & 9 deletions x/evidence/types/evidence_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,24 @@ import (
)

func TestEquivocation_Valid(t *testing.T) {
consCodec := address.NewBech32Codec("cosmosvalcons")
n, _ := time.Parse(time.RFC3339, "2006-01-02T15:04:05Z")
addr := sdk.ConsAddress("foo_________________")
addr, err := consCodec.BytesToString(sdk.ConsAddress("foo_________________"))
require.NoError(t, err)

e := types.Equivocation{
Height: 100,
Time: n,
Power: 1000000,
ConsensusAddress: addr.String(),
ConsensusAddress: addr,
}

consAddr, err := consCodec.BytesToString(e.GetConsensusAddress(consCodec))
require.NoError(t, err)
require.Equal(t, e.GetTotalPower(), int64(0))
require.Equal(t, e.GetValidatorPower(), e.Power)
require.Equal(t, e.GetTime(), e.Time)
require.Equal(t, e.GetConsensusAddress(address.NewBech32Codec("cosmosvalcons")).String(), e.ConsensusAddress)
require.Equal(t, consAddr, e.ConsensusAddress)
require.Equal(t, e.GetHeight(), e.Height)
require.Equal(t, e.Route(), types.RouteEquivocation)
require.Equal(t, strings.ToUpper(hex.EncodeToString(e.Hash())), "1E10F9267BEA3A9A4AB5302C2C510CC1AFD7C54E232DA5B2E3360DFAFACF7A76")
Expand All @@ -39,7 +43,7 @@ func TestEquivocation_Valid(t *testing.T) {
require.Equal(t, int64(0), e.GetTotalPower())
require.Equal(t, e.Power, e.GetValidatorPower())
require.Equal(t, e.Time, e.GetTime())
require.Equal(t, e.ConsensusAddress, e.GetConsensusAddress(address.NewBech32Codec("cosmosvalcons")).String())
require.Equal(t, e.ConsensusAddress, consAddr)
require.Equal(t, e.Height, e.GetHeight())
require.Equal(t, types.RouteEquivocation, e.Route())
require.Equal(t, "1E10F9267BEA3A9A4AB5302C2C510CC1AFD7C54E232DA5B2E3360DFAFACF7A76", strings.ToUpper(hex.EncodeToString(e.Hash())))
Expand All @@ -49,18 +53,19 @@ func TestEquivocation_Valid(t *testing.T) {

func TestEquivocationValidateBasic(t *testing.T) {
var zeroTime time.Time
addr := sdk.ConsAddress("foo_________________")
addr, err := address.NewBech32Codec("cosmosvalcons").BytesToString(sdk.ConsAddress("foo_________________"))
require.NoError(t, err)

n, _ := time.Parse(time.RFC3339, "2006-01-02T15:04:05Z")
testCases := []struct {
name string
e types.Equivocation
expectErr bool
}{
{"valid", types.Equivocation{100, n, 1000000, addr.String()}, false},
{"invalid time", types.Equivocation{100, zeroTime, 1000000, addr.String()}, true},
{"invalid height", types.Equivocation{0, n, 1000000, addr.String()}, true},
{"invalid power", types.Equivocation{100, n, 0, addr.String()}, true},
{"valid", types.Equivocation{100, n, 1000000, addr}, false},
{"invalid time", types.Equivocation{100, zeroTime, 1000000, addr}, true},
{"invalid height", types.Equivocation{0, n, 1000000, addr}, true},
{"invalid power", types.Equivocation{100, n, 0, addr}, true},
{"invalid address", types.Equivocation{100, n, 1000000, ""}, true},
}

Expand Down
4 changes: 2 additions & 2 deletions x/evidence/types/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ var (
)

// NewMsgSubmitEvidence returns a new MsgSubmitEvidence with a signer/submitter.
func NewMsgSubmitEvidence(s sdk.AccAddress, evi exported.Evidence) (*MsgSubmitEvidence, error) {
func NewMsgSubmitEvidence(s string, evi exported.Evidence) (*MsgSubmitEvidence, error) {
msg, ok := evi.(proto.Message)
if !ok {
return nil, fmt.Errorf("cannot proto marshal %T", evi)
Expand All @@ -27,7 +27,7 @@ func NewMsgSubmitEvidence(s sdk.AccAddress, evi exported.Evidence) (*MsgSubmitEv
if err != nil {
return nil, err
}
return &MsgSubmitEvidence{Submitter: s.String(), Evidence: any}, nil
return &MsgSubmitEvidence{Submitter: s, Evidence: any}, nil
}

func (m MsgSubmitEvidence) GetEvidence() exported.Evidence {
Expand Down
3 changes: 3 additions & 0 deletions x/slashing/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ Ref: https://keepachangelog.com/en/1.0.0/

### API Breaking Changes

* [#20026](https://github.com/cosmos/cosmos-sdk/pull/20026) Removal of the Address.String() method and related changes:
* `Migrate` now takes a `ValidatorAddressCodec` as argument.
* `Migrator` has a new field of `ValidatorAddressCodec` type.
* [#16441](https://github.com/cosmos/cosmos-sdk/pull/16441) Params state is migrated to collections. `GetParams` has been removed.
* [#17023](https://github.com/cosmos/cosmos-sdk/pull/17023) Use collections for `ValidatorSigningInfo`:
* remove `Keeper`: `SetValidatorSigningInfo`, `GetValidatorSigningInfo`, `IterateValidatorSigningInfos`
Expand Down
13 changes: 9 additions & 4 deletions x/slashing/keeper/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,24 @@ package keeper
import (
"context"

"cosmossdk.io/core/address"
v4 "cosmossdk.io/x/slashing/migrations/v4"

"github.com/cosmos/cosmos-sdk/runtime"
)

// Migrator is a struct for handling in-place store migrations.
type Migrator struct {
keeper Keeper
keeper Keeper
valCodec address.ValidatorAddressCodec
}

// NewMigrator returns a new Migrator.
func NewMigrator(keeper Keeper) Migrator {
return Migrator{keeper: keeper}
func NewMigrator(keeper Keeper, valCodec address.ValidatorAddressCodec) Migrator {
return Migrator{
keeper: keeper,
valCodec: valCodec,
}
}

// Migrate1to2 migrates from version 1 to 2.
Expand All @@ -40,5 +45,5 @@ func (m Migrator) Migrate3to4(ctx context.Context) error {
if err != nil {
return err
}
return v4.Migrate(ctx, m.keeper.cdc, store, params)
return v4.Migrate(ctx, m.keeper.cdc, store, params, m.valCodec)
}
8 changes: 6 additions & 2 deletions x/slashing/migrations/v4/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/bits-and-blooms/bitset"
gogotypes "github.com/cosmos/gogoproto/types"

"cosmossdk.io/core/address"
"cosmossdk.io/errors"
storetypes "cosmossdk.io/store/types"
"cosmossdk.io/x/slashing/types"
Expand All @@ -17,12 +18,15 @@ import (
// Migrate migrates state to consensus version 4. Specifically, the migration
// deletes all existing validator bitmap entries and replaces them with a real
// "chunked" bitmap.
func Migrate(ctx context.Context, cdc codec.BinaryCodec, store storetypes.KVStore, params types.Params) error {
func Migrate(ctx context.Context, cdc codec.BinaryCodec, store storetypes.KVStore, params types.Params, addressCodec address.ValidatorAddressCodec) error {
// Get all the missed blocks for each validator, based on the existing signing
// info.
var missedBlocks []types.ValidatorMissedBlocks
iterateValidatorSigningInfos(ctx, cdc, store, func(addr sdk.ConsAddress, info types.ValidatorSigningInfo) (stop bool) {
bechAddr := addr.String()
bechAddr, err := addressCodec.BytesToString(addr)
if err != nil {
return true
}
localMissedBlocks := GetValidatorMissedBlocks(ctx, cdc, store, addr, params)

missedBlocks = append(missedBlocks, types.ValidatorMissedBlocks{
Expand Down
8 changes: 6 additions & 2 deletions x/slashing/migrations/v4/migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
v4 "cosmossdk.io/x/slashing/migrations/v4"
slashingtypes "cosmossdk.io/x/slashing/types"

"github.com/cosmos/cosmos-sdk/codec/address"
codectestutil "github.com/cosmos/cosmos-sdk/codec/testutil"
"github.com/cosmos/cosmos-sdk/testutil"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand All @@ -27,9 +28,12 @@ func TestMigrate(t *testing.T) {
ctx := testutil.DefaultContext(storeKey, tKey)
store := ctx.KVStore(storeKey)
params := slashingtypes.Params{SignedBlocksWindow: 100}
valCodec := address.NewBech32Codec("cosmosvalcons")
consStrAddr, err := valCodec.BytesToString(consAddr)
require.NoError(t, err)

// store old signing info and bitmap entries
bz := cdc.MustMarshal(&slashingtypes.ValidatorSigningInfo{Address: consAddr.String()})
bz := cdc.MustMarshal(&slashingtypes.ValidatorSigningInfo{Address: consStrAddr})
store.Set(v4.ValidatorSigningInfoKey(consAddr), bz)

for i := int64(0); i < params.SignedBlocksWindow; i++ {
Expand All @@ -39,7 +43,7 @@ func TestMigrate(t *testing.T) {
store.Set(v4.ValidatorMissedBlockBitArrayKey(consAddr, i), bz)
}

err := v4.Migrate(ctx, cdc, store, params)
err = v4.Migrate(ctx, cdc, store, params, valCodec)
require.NoError(t, err)

for i := int64(0); i < params.SignedBlocksWindow; i++ {
Expand Down
2 changes: 1 addition & 1 deletion x/slashing/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func (am AppModule) RegisterServices(registrar grpc.ServiceRegistrar) error {

// RegisterMigrations registers module migrations.
func (am AppModule) RegisterMigrations(mr appmodule.MigrationRegistrar) error {
m := keeper.NewMigrator(am.keeper)
m := keeper.NewMigrator(am.keeper, am.stakingKeeper.ValidatorAddressCodec())

if err := mr.Register(types.ModuleName, 1, m.Migrate1to2); err != nil {
return fmt.Errorf("failed to migrate x/%s from version 1 to 2: %w", types.ModuleName, err)
Expand Down
9 changes: 7 additions & 2 deletions x/slashing/simulation/proposals.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,15 @@ func ProposalMsgs() []simtypes.WeightedProposalMsg {
}

// SimulateMsgUpdateParams returns a random MsgUpdateParams
func SimulateMsgUpdateParams(r *rand.Rand, _ []simtypes.Account, _ coreaddress.Codec) (sdk.Msg, error) {
func SimulateMsgUpdateParams(r *rand.Rand, _ []simtypes.Account, ac coreaddress.Codec) (sdk.Msg, error) {
// use the default gov module account address as authority
var authority sdk.AccAddress = address.Module("gov")

authorityAddr, err := ac.BytesToString(authority)
if err != nil {
return nil, err
}

params := types.DefaultParams()
params.DowntimeJailDuration = time.Duration(simtypes.RandTimestamp(r).UnixNano())
params.SignedBlocksWindow = int64(simtypes.RandIntBetween(r, 1, 1000))
Expand All @@ -45,7 +50,7 @@ func SimulateMsgUpdateParams(r *rand.Rand, _ []simtypes.Account, _ coreaddress.C
params.SlashFractionDowntime = sdkmath.LegacyNewDecWithPrec(int64(simtypes.RandIntBetween(r, 1, 100)), 2)

return &types.MsgUpdateParams{
Authority: authority.String(),
Authority: authorityAddr,
Params: params,
}, nil
}
Loading

0 comments on commit 3e04a7c

Please sign in to comment.