Skip to content

Commit

Permalink
fix: add missing subspaces authz migration (#981)
Browse files Browse the repository at this point in the history
## Description

This PR adds the `x/subspaces` authorization migrations that was missing from `v4.2.0`. 



---

### Author Checklist

*All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.*

I have...

- [x] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] added `!` to the type prefix if API or client breaking change
- [x] targeted the correct branch (see [PR Targeting](https://github.com/desmos-labs/desmos/blob/master/CONTRIBUTING.md#pr-targeting))
- [ ] provided a link to the relevant issue or specification
- [ ] followed the guidelines for [building modules](https://docs.cosmos.network/v0.44/building-modules/intro.html)
- [x] included the necessary unit and integration [tests](https://github.com/desmos-labs/desmos/blob/master/CONTRIBUTING.md#testing)
- [x] added a changelog entry to `CHANGELOG.md`
- [ ] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [ ] updated the relevant documentation or specification
- [x] reviewed "Files changed" and left comments if necessary
- [x] confirmed all CI checks have passed

### Reviewers Checklist

*All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.*

I have...

- [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] confirmed `!` in the type prefix if API or client breaking change
- [ ] confirmed all author checklist items have been addressed
- [ ] reviewed state machine logic
- [ ] reviewed API design and naming
- [ ] reviewed documentation is accurate
- [ ] reviewed tests and test coverage
- [ ] manually tested (if applicable)
  • Loading branch information
RiccardoM authored Jul 29, 2022
1 parent e407869 commit 48fc886
Show file tree
Hide file tree
Showing 10 changed files with 702 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
type: fix
module: x/subspaces
pull_request: 981
description: Added missing subspaces authorizations migration
backward_compatible: true
date: 2022-07-29T08:13:00.050919588Z
4 changes: 2 additions & 2 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -678,7 +678,7 @@ func NewDesmosApp(

// Custom modules
fees.NewAppModule(app.appCodec, app.FeesKeeper),
subspaces.NewAppModule(appCodec, app.SubspacesKeeper, app.AccountKeeper, app.BankKeeper, app.FeesKeeper),
subspaces.NewAppModule(appCodec, app.SubspacesKeeper, app.AuthzKeeper, app.AccountKeeper, app.BankKeeper, app.FeesKeeper),
profilesModule,
relationships.NewAppModule(appCodec, app.RelationshipsKeeper, app.SubspacesKeeper, profilesv4.NewKeeper(keys[profilestypes.StoreKey], appCodec), app.AccountKeeper, app.BankKeeper, app.FeesKeeper),
posts.NewAppModule(appCodec, app.PostsKeeper, app.SubspacesKeeper, app.AccountKeeper, app.BankKeeper, app.FeesKeeper),
Expand Down Expand Up @@ -862,7 +862,7 @@ func NewDesmosApp(
// Custom modules
fees.NewAppModule(appCodec, app.FeesKeeper),
supply.NewAppModule(appCodec, legacyAmino, app.SupplyKeeper),
subspaces.NewAppModule(appCodec, app.SubspacesKeeper, app.AccountKeeper, app.BankKeeper, app.FeesKeeper),
subspaces.NewAppModule(appCodec, app.SubspacesKeeper, app.AuthzKeeper, app.AccountKeeper, app.BankKeeper, app.FeesKeeper),
profilesModule,
relationships.NewAppModule(appCodec, app.RelationshipsKeeper, app.SubspacesKeeper, profilesv4.NewKeeper(keys[profilestypes.StoreKey], appCodec), app.AccountKeeper, app.BankKeeper, app.FeesKeeper),
posts.NewAppModule(appCodec, app.PostsKeeper, app.SubspacesKeeper, app.AccountKeeper, app.BankKeeper, app.FeesKeeper),
Expand Down
21 changes: 21 additions & 0 deletions proto/desmos/subspaces/v2/authz/authz.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
syntax = "proto3";
package desmos.subspaces.v2.authz;

import "gogoproto/gogo.proto";
import "cosmos_proto/cosmos.proto";
import "cosmos/base/v1beta1/coin.proto";

option go_package = "github.com/desmos-labs/desmos/v4/x/subspaces/legacy/v2";

// GenericSubspaceAuthorization defines an authorization to perform any
// operation only inside a specific subspace.
message GenericSubspaceAuthorization {
option (cosmos_proto.implements_interface) = "Authorization";

// Ids of the subspaces inside which to grant the permission
repeated uint64 subspaces_ids = 1 [ (gogoproto.customname) = "SubspacesIDs" ];

// Msg, identified by it's type URL, to grant unrestricted permissions to
// execute within the subspace
string msg = 2;
}
18 changes: 13 additions & 5 deletions x/subspaces/keeper/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,26 @@ package keeper

import (
sdk "github.com/cosmos/cosmos-sdk/types"

v3 "github.com/desmos-labs/desmos/v4/x/subspaces/legacy/v3"
authzkeeper "github.com/cosmos/cosmos-sdk/x/authz/keeper"

v2 "github.com/desmos-labs/desmos/v4/x/subspaces/legacy/v2"
v3 "github.com/desmos-labs/desmos/v4/x/subspaces/legacy/v3"
v4 "github.com/desmos-labs/desmos/v4/x/subspaces/legacy/v4"
)

// DONTCOVER

// Migrator is a struct for handling in-place store migrations.
type Migrator struct {
keeper Keeper
keeper Keeper
authzKeeper authzkeeper.Keeper
}

// NewMigrator returns a new Migrator
func NewMigrator(keeper Keeper) Migrator {
func NewMigrator(keeper Keeper, authzKeeper authzkeeper.Keeper) Migrator {
return Migrator{
keeper: keeper,
keeper: keeper,
authzKeeper: authzKeeper,
}
}

Expand All @@ -31,3 +34,8 @@ func (m Migrator) Migrate1to2(ctx sdk.Context) error {
func (m Migrator) Migrate2to3(ctx sdk.Context) error {
return v3.MigrateStore(ctx, m.keeper.storeKey, m.keeper.cdc)
}

// Migrate3to4 migrates from version 3 to 4.
func (m Migrator) Migrate3to4(ctx sdk.Context) error {
return v4.MigrateStore(ctx, m.authzKeeper, m.keeper.cdc)
}
65 changes: 65 additions & 0 deletions x/subspaces/legacy/v2/authz.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package v2

// DONTCOVER

import (
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/x/authz"

"github.com/desmos-labs/desmos/v4/x/subspaces/types"
)

// TODO: Revisit this once we have proper gas fee framework.
// Tracking issues https://github.com/cosmos/cosmos-sdk/issues/9054, https://github.com/cosmos/cosmos-sdk/discussions/9072
const gasCostPerIteration = uint64(10)

var (
_ authz.Authorization = &GenericSubspaceAuthorization{}
)

// NewGenericSubspaceAuthorization creates a new GenericSubspaceAuthorization object.
func NewGenericSubspaceAuthorization(subspacesIDs []uint64, msgTypeURL string) *GenericSubspaceAuthorization {
return &GenericSubspaceAuthorization{
SubspacesIDs: subspacesIDs,
Msg: msgTypeURL,
}
}

// MsgTypeURL implements Authorization.MsgTypeURL.
func (a GenericSubspaceAuthorization) MsgTypeURL() string {
return a.Msg
}

// Accept implements Authorization.Accept.
func (a GenericSubspaceAuthorization) Accept(ctx sdk.Context, msg sdk.Msg) (authz.AcceptResponse, error) {
switch msg := msg.(type) {

case types.SubspaceMsg:
for _, subspaceID := range a.SubspacesIDs {
ctx.GasMeter().ConsumeGas(gasCostPerIteration, "generic subspace authorization")
if subspaceID == msg.GetSubspaceID() {
return authz.AcceptResponse{Accept: true}, nil
}
}
return authz.AcceptResponse{Accept: false}, nil

default:
return authz.AcceptResponse{}, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "unknown msg type")
}
}

// ValidateBasic implements Authorization.ValidateBasic.
func (a GenericSubspaceAuthorization) ValidateBasic() error {
if a.SubspacesIDs == nil {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "at least one subspace id is required")
}

for _, subspaceID := range a.SubspacesIDs {
if subspaceID == 0 {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "invalid subspace id")
}
}

return nil
}
Loading

0 comments on commit 48fc886

Please sign in to comment.