-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: add missing subspaces authz migration
Signed-off-by: Riccardo Montagnin <riccardo.montagnin@gmail.com>
- Loading branch information
Showing
8 changed files
with
694 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.