Skip to content

Commit

Permalink
fix: add missing subspaces authz migration
Browse files Browse the repository at this point in the history
Signed-off-by: Riccardo Montagnin <riccardo.montagnin@gmail.com>
  • Loading branch information
RiccardoM committed Jul 29, 2022
1 parent da714ce commit b79173e
Show file tree
Hide file tree
Showing 8 changed files with 694 additions and 4 deletions.
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;
}
9 changes: 7 additions & 2 deletions x/subspaces/keeper/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package keeper
import (
sdk "github.com/cosmos/cosmos-sdk/types"

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

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
Expand All @@ -31,3 +31,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.keeper.storeKey, 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 b79173e

Please sign in to comment.