From 48fc8866d783963297100f78422e12fa525237e5 Mon Sep 17 00:00:00 2001 From: Riccardo Montagnin Date: Fri, 29 Jul 2022 14:33:55 +0200 Subject: [PATCH] fix: add missing subspaces authz migration (#981) ## 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) --- ...edc16e6f3b9dd6dfbf0cbb7406719d61285d8.yaml | 6 + app/app.go | 4 +- proto/desmos/subspaces/v2/authz/authz.proto | 21 + x/subspaces/keeper/migrations.go | 18 +- x/subspaces/legacy/v2/authz.go | 65 +++ x/subspaces/legacy/v2/authz.pb.go | 441 ++++++++++++++++++ x/subspaces/legacy/v2/codec.go | 14 + x/subspaces/legacy/v4/store.go | 32 ++ x/subspaces/legacy/v4/store_test.go | 92 ++++ x/subspaces/module.go | 20 +- 10 files changed, 702 insertions(+), 11 deletions(-) create mode 100644 .changeset/entries/949c3ef6e6aaea51b5bb253dc3eedc16e6f3b9dd6dfbf0cbb7406719d61285d8.yaml create mode 100644 proto/desmos/subspaces/v2/authz/authz.proto create mode 100644 x/subspaces/legacy/v2/authz.go create mode 100644 x/subspaces/legacy/v2/authz.pb.go create mode 100644 x/subspaces/legacy/v2/codec.go create mode 100644 x/subspaces/legacy/v4/store.go create mode 100644 x/subspaces/legacy/v4/store_test.go diff --git a/.changeset/entries/949c3ef6e6aaea51b5bb253dc3eedc16e6f3b9dd6dfbf0cbb7406719d61285d8.yaml b/.changeset/entries/949c3ef6e6aaea51b5bb253dc3eedc16e6f3b9dd6dfbf0cbb7406719d61285d8.yaml new file mode 100644 index 0000000000..c888c54229 --- /dev/null +++ b/.changeset/entries/949c3ef6e6aaea51b5bb253dc3eedc16e6f3b9dd6dfbf0cbb7406719d61285d8.yaml @@ -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 diff --git a/app/app.go b/app/app.go index 5616039c4c..1bae9f3929 100644 --- a/app/app.go +++ b/app/app.go @@ -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), @@ -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), diff --git a/proto/desmos/subspaces/v2/authz/authz.proto b/proto/desmos/subspaces/v2/authz/authz.proto new file mode 100644 index 0000000000..1efabffa23 --- /dev/null +++ b/proto/desmos/subspaces/v2/authz/authz.proto @@ -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; +} \ No newline at end of file diff --git a/x/subspaces/keeper/migrations.go b/x/subspaces/keeper/migrations.go index 7fd3fdc2c9..ffc5de143e 100644 --- a/x/subspaces/keeper/migrations.go +++ b/x/subspaces/keeper/migrations.go @@ -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, } } @@ -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) +} diff --git a/x/subspaces/legacy/v2/authz.go b/x/subspaces/legacy/v2/authz.go new file mode 100644 index 0000000000..88b62baeda --- /dev/null +++ b/x/subspaces/legacy/v2/authz.go @@ -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 +} diff --git a/x/subspaces/legacy/v2/authz.pb.go b/x/subspaces/legacy/v2/authz.pb.go new file mode 100644 index 0000000000..3988e3c60b --- /dev/null +++ b/x/subspaces/legacy/v2/authz.pb.go @@ -0,0 +1,441 @@ +// Code generated by protoc-gen-gogo. DO NOT EDIT. +// source: desmos/subspaces/v2/authz/authz.proto + +package v2 + +import ( + fmt "fmt" + _ "github.com/cosmos/cosmos-sdk/types" + _ "github.com/gogo/protobuf/gogoproto" + proto "github.com/gogo/protobuf/proto" + _ "github.com/regen-network/cosmos-proto" + io "io" + math "math" + math_bits "math/bits" +) + +// Reference imports to suppress errors if they are not otherwise used. +var _ = proto.Marshal +var _ = fmt.Errorf +var _ = math.Inf + +// This is a compile-time assertion to ensure that this generated file +// is compatible with the proto package it is being compiled against. +// A compilation error at this line likely means your copy of the +// proto package needs to be updated. +const _ = proto.GoGoProtoPackageIsVersion3 // please upgrade the proto package + +// GenericSubspaceAuthorization defines an authorization to perform any +// operation only inside a specific subspace. +type GenericSubspaceAuthorization struct { + // Ids of the subspaces inside which to grant the permission + SubspacesIDs []uint64 `protobuf:"varint,1,rep,packed,name=subspaces_ids,json=subspacesIds,proto3" json:"subspaces_ids,omitempty"` + // Msg, identified by it's type URL, to grant unrestricted permissions to + // execute within the subspace + Msg string `protobuf:"bytes,2,opt,name=msg,proto3" json:"msg,omitempty"` +} + +func (m *GenericSubspaceAuthorization) Reset() { *m = GenericSubspaceAuthorization{} } +func (m *GenericSubspaceAuthorization) String() string { return proto.CompactTextString(m) } +func (*GenericSubspaceAuthorization) ProtoMessage() {} +func (*GenericSubspaceAuthorization) Descriptor() ([]byte, []int) { + return fileDescriptor_21914b81942bd86e, []int{0} +} +func (m *GenericSubspaceAuthorization) XXX_Unmarshal(b []byte) error { + return m.Unmarshal(b) +} +func (m *GenericSubspaceAuthorization) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + if deterministic { + return xxx_messageInfo_GenericSubspaceAuthorization.Marshal(b, m, deterministic) + } else { + b = b[:cap(b)] + n, err := m.MarshalToSizedBuffer(b) + if err != nil { + return nil, err + } + return b[:n], nil + } +} +func (m *GenericSubspaceAuthorization) XXX_Merge(src proto.Message) { + xxx_messageInfo_GenericSubspaceAuthorization.Merge(m, src) +} +func (m *GenericSubspaceAuthorization) XXX_Size() int { + return m.Size() +} +func (m *GenericSubspaceAuthorization) XXX_DiscardUnknown() { + xxx_messageInfo_GenericSubspaceAuthorization.DiscardUnknown(m) +} + +var xxx_messageInfo_GenericSubspaceAuthorization proto.InternalMessageInfo + +func (m *GenericSubspaceAuthorization) GetSubspacesIDs() []uint64 { + if m != nil { + return m.SubspacesIDs + } + return nil +} + +func (m *GenericSubspaceAuthorization) GetMsg() string { + if m != nil { + return m.Msg + } + return "" +} + +func init() { + proto.RegisterType((*GenericSubspaceAuthorization)(nil), "desmos.subspaces.v2.authz.GenericSubspaceAuthorization") +} + +func init() { + proto.RegisterFile("desmos/subspaces/v2/authz/authz.proto", fileDescriptor_21914b81942bd86e) +} + +var fileDescriptor_21914b81942bd86e = []byte{ + // 278 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x52, 0x4d, 0x49, 0x2d, 0xce, + 0xcd, 0x2f, 0xd6, 0x2f, 0x2e, 0x4d, 0x2a, 0x2e, 0x48, 0x4c, 0x4e, 0x2d, 0xd6, 0x2f, 0x33, 0xd2, + 0x4f, 0x2c, 0x2d, 0xc9, 0xa8, 0x82, 0x90, 0x7a, 0x05, 0x45, 0xf9, 0x25, 0xf9, 0x42, 0x92, 0x10, + 0x65, 0x7a, 0x70, 0x65, 0x7a, 0x65, 0x46, 0x7a, 0x60, 0x05, 0x52, 0x22, 0xe9, 0xf9, 0xe9, 0xf9, + 0x60, 0x55, 0xfa, 0x20, 0x16, 0x44, 0x83, 0x94, 0x64, 0x72, 0x3e, 0x48, 0x43, 0x3c, 0x44, 0x02, + 0xc2, 0x81, 0x4a, 0xc9, 0x41, 0x78, 0xfa, 0x49, 0x89, 0xc5, 0xa9, 0xfa, 0x65, 0x86, 0x49, 0xa9, + 0x25, 0x89, 0x86, 0xfa, 0xc9, 0xf9, 0x99, 0x79, 0x10, 0x79, 0xa5, 0x2a, 0x2e, 0x19, 0xf7, 0xd4, + 0xbc, 0xd4, 0xa2, 0xcc, 0xe4, 0x60, 0xa8, 0x6d, 0x8e, 0xa5, 0x25, 0x19, 0xf9, 0x45, 0x99, 0x55, + 0x89, 0x25, 0x99, 0xf9, 0x79, 0x42, 0xa6, 0x5c, 0xbc, 0x70, 0x67, 0xc4, 0x67, 0xa6, 0x14, 0x4b, + 0x30, 0x2a, 0x30, 0x6b, 0xb0, 0x38, 0x09, 0x3c, 0xba, 0x27, 0xcf, 0x03, 0xd3, 0x51, 0xec, 0xe9, + 0x52, 0x1c, 0xc4, 0x03, 0x57, 0xe6, 0x99, 0x52, 0x2c, 0x24, 0xc0, 0xc5, 0x9c, 0x5b, 0x9c, 0x2e, + 0xc1, 0xa4, 0xc0, 0xa8, 0xc1, 0x19, 0x04, 0x62, 0x5a, 0x09, 0x5e, 0xda, 0xa2, 0xcb, 0x8b, 0x62, + 0xb6, 0x53, 0xc0, 0x89, 0x47, 0x72, 0x8c, 0x17, 0x1e, 0xc9, 0x31, 0x3e, 0x78, 0x24, 0xc7, 0x38, + 0xe1, 0xb1, 0x1c, 0xc3, 0x85, 0xc7, 0x72, 0x0c, 0x37, 0x1e, 0xcb, 0x31, 0x44, 0x99, 0xa5, 0x67, + 0x96, 0x64, 0x94, 0x26, 0xe9, 0x25, 0xe7, 0xe7, 0xea, 0x43, 0x02, 0x43, 0x37, 0x27, 0x31, 0xa9, + 0x18, 0xca, 0xd6, 0x2f, 0x33, 0xd1, 0xaf, 0x40, 0x0a, 0xc4, 0x9c, 0xd4, 0xf4, 0xc4, 0xe4, 0x4a, + 0xfd, 0x32, 0xa3, 0x24, 0x36, 0xb0, 0xa7, 0x8c, 0x01, 0x01, 0x00, 0x00, 0xff, 0xff, 0xe6, 0x1a, + 0x65, 0x9c, 0x69, 0x01, 0x00, 0x00, +} + +func (m *GenericSubspaceAuthorization) Marshal() (dAtA []byte, err error) { + size := m.Size() + dAtA = make([]byte, size) + n, err := m.MarshalToSizedBuffer(dAtA[:size]) + if err != nil { + return nil, err + } + return dAtA[:n], nil +} + +func (m *GenericSubspaceAuthorization) MarshalTo(dAtA []byte) (int, error) { + size := m.Size() + return m.MarshalToSizedBuffer(dAtA[:size]) +} + +func (m *GenericSubspaceAuthorization) MarshalToSizedBuffer(dAtA []byte) (int, error) { + i := len(dAtA) + _ = i + var l int + _ = l + if len(m.Msg) > 0 { + i -= len(m.Msg) + copy(dAtA[i:], m.Msg) + i = encodeVarintAuthz(dAtA, i, uint64(len(m.Msg))) + i-- + dAtA[i] = 0x12 + } + if len(m.SubspacesIDs) > 0 { + dAtA2 := make([]byte, len(m.SubspacesIDs)*10) + var j1 int + for _, num := range m.SubspacesIDs { + for num >= 1<<7 { + dAtA2[j1] = uint8(uint64(num)&0x7f | 0x80) + num >>= 7 + j1++ + } + dAtA2[j1] = uint8(num) + j1++ + } + i -= j1 + copy(dAtA[i:], dAtA2[:j1]) + i = encodeVarintAuthz(dAtA, i, uint64(j1)) + i-- + dAtA[i] = 0xa + } + return len(dAtA) - i, nil +} + +func encodeVarintAuthz(dAtA []byte, offset int, v uint64) int { + offset -= sovAuthz(v) + base := offset + for v >= 1<<7 { + dAtA[offset] = uint8(v&0x7f | 0x80) + v >>= 7 + offset++ + } + dAtA[offset] = uint8(v) + return base +} +func (m *GenericSubspaceAuthorization) Size() (n int) { + if m == nil { + return 0 + } + var l int + _ = l + if len(m.SubspacesIDs) > 0 { + l = 0 + for _, e := range m.SubspacesIDs { + l += sovAuthz(uint64(e)) + } + n += 1 + sovAuthz(uint64(l)) + l + } + l = len(m.Msg) + if l > 0 { + n += 1 + l + sovAuthz(uint64(l)) + } + return n +} + +func sovAuthz(x uint64) (n int) { + return (math_bits.Len64(x|1) + 6) / 7 +} +func sozAuthz(x uint64) (n int) { + return sovAuthz(uint64((x << 1) ^ uint64((int64(x) >> 63)))) +} +func (m *GenericSubspaceAuthorization) Unmarshal(dAtA []byte) error { + l := len(dAtA) + iNdEx := 0 + for iNdEx < l { + preIndex := iNdEx + var wire uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowAuthz + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + wire |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + fieldNum := int32(wire >> 3) + wireType := int(wire & 0x7) + if wireType == 4 { + return fmt.Errorf("proto: GenericSubspaceAuthorization: wiretype end group for non-group") + } + if fieldNum <= 0 { + return fmt.Errorf("proto: GenericSubspaceAuthorization: illegal tag %d (wire type %d)", fieldNum, wire) + } + switch fieldNum { + case 1: + if wireType == 0 { + var v uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowAuthz + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + v |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + m.SubspacesIDs = append(m.SubspacesIDs, v) + } else if wireType == 2 { + var packedLen int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowAuthz + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + packedLen |= int(b&0x7F) << shift + if b < 0x80 { + break + } + } + if packedLen < 0 { + return ErrInvalidLengthAuthz + } + postIndex := iNdEx + packedLen + if postIndex < 0 { + return ErrInvalidLengthAuthz + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + var elementCount int + var count int + for _, integer := range dAtA[iNdEx:postIndex] { + if integer < 128 { + count++ + } + } + elementCount = count + if elementCount != 0 && len(m.SubspacesIDs) == 0 { + m.SubspacesIDs = make([]uint64, 0, elementCount) + } + for iNdEx < postIndex { + var v uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowAuthz + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + v |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + m.SubspacesIDs = append(m.SubspacesIDs, v) + } + } else { + return fmt.Errorf("proto: wrong wireType = %d for field SubspacesIDs", wireType) + } + case 2: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Msg", wireType) + } + var stringLen uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowAuthz + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + stringLen |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + intStringLen := int(stringLen) + if intStringLen < 0 { + return ErrInvalidLengthAuthz + } + postIndex := iNdEx + intStringLen + if postIndex < 0 { + return ErrInvalidLengthAuthz + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.Msg = string(dAtA[iNdEx:postIndex]) + iNdEx = postIndex + default: + iNdEx = preIndex + skippy, err := skipAuthz(dAtA[iNdEx:]) + if err != nil { + return err + } + if (skippy < 0) || (iNdEx+skippy) < 0 { + return ErrInvalidLengthAuthz + } + if (iNdEx + skippy) > l { + return io.ErrUnexpectedEOF + } + iNdEx += skippy + } + } + + if iNdEx > l { + return io.ErrUnexpectedEOF + } + return nil +} +func skipAuthz(dAtA []byte) (n int, err error) { + l := len(dAtA) + iNdEx := 0 + depth := 0 + for iNdEx < l { + var wire uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return 0, ErrIntOverflowAuthz + } + if iNdEx >= l { + return 0, io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + wire |= (uint64(b) & 0x7F) << shift + if b < 0x80 { + break + } + } + wireType := int(wire & 0x7) + switch wireType { + case 0: + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return 0, ErrIntOverflowAuthz + } + if iNdEx >= l { + return 0, io.ErrUnexpectedEOF + } + iNdEx++ + if dAtA[iNdEx-1] < 0x80 { + break + } + } + case 1: + iNdEx += 8 + case 2: + var length int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return 0, ErrIntOverflowAuthz + } + if iNdEx >= l { + return 0, io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + length |= (int(b) & 0x7F) << shift + if b < 0x80 { + break + } + } + if length < 0 { + return 0, ErrInvalidLengthAuthz + } + iNdEx += length + case 3: + depth++ + case 4: + if depth == 0 { + return 0, ErrUnexpectedEndOfGroupAuthz + } + depth-- + case 5: + iNdEx += 4 + default: + return 0, fmt.Errorf("proto: illegal wireType %d", wireType) + } + if iNdEx < 0 { + return 0, ErrInvalidLengthAuthz + } + if depth == 0 { + return iNdEx, nil + } + } + return 0, io.ErrUnexpectedEOF +} + +var ( + ErrInvalidLengthAuthz = fmt.Errorf("proto: negative length found during unmarshaling") + ErrIntOverflowAuthz = fmt.Errorf("proto: integer overflow") + ErrUnexpectedEndOfGroupAuthz = fmt.Errorf("proto: unexpected end of group") +) diff --git a/x/subspaces/legacy/v2/codec.go b/x/subspaces/legacy/v2/codec.go new file mode 100644 index 0000000000..bae59ee418 --- /dev/null +++ b/x/subspaces/legacy/v2/codec.go @@ -0,0 +1,14 @@ +package v2 + +import ( + "github.com/cosmos/cosmos-sdk/codec/types" + "github.com/cosmos/cosmos-sdk/x/authz" +) + +// RegisterInterfaces registers the interfaces types with the interface registry +func RegisterInterfaces(registry types.InterfaceRegistry) { + registry.RegisterImplementations( + (*authz.Authorization)(nil), + &GenericSubspaceAuthorization{}, + ) +} diff --git a/x/subspaces/legacy/v4/store.go b/x/subspaces/legacy/v4/store.go new file mode 100644 index 0000000000..b96cca881f --- /dev/null +++ b/x/subspaces/legacy/v4/store.go @@ -0,0 +1,32 @@ +package v4 + +import ( + "github.com/cosmos/cosmos-sdk/codec" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/authz" + authzkeeper "github.com/cosmos/cosmos-sdk/x/authz/keeper" + + subspacesauthz "github.com/desmos-labs/desmos/v4/x/subspaces/authz" + v2 "github.com/desmos-labs/desmos/v4/x/subspaces/legacy/v2" +) + +// MigrateStore migrates the store from version 3 to version 4. +// The process performs the missing authz migration that was excluded from version 2 to version 3. +func MigrateStore(ctx sdk.Context, authzKeeper authzkeeper.Keeper, cdc codec.BinaryCodec) error { + authzKeeper.IterateGrants(ctx, func(granterAddr sdk.AccAddress, granteeAddr sdk.AccAddress, grant authz.Grant) bool { + if auth, ok := grant.Authorization.GetCachedValue().(*v2.GenericSubspaceAuthorization); ok { + // Convert the generic grant authorization + v3Auth := subspacesauthz.NewGenericSubspaceAuthorization(auth.SubspacesIDs, auth.Msg) + + // Store the new authorization + err := authzKeeper.SaveGrant(ctx, granteeAddr, granterAddr, v3Auth, grant.Expiration) + if err != nil { + panic(err) + } + } + + return false + }) + + return nil +} diff --git a/x/subspaces/legacy/v4/store_test.go b/x/subspaces/legacy/v4/store_test.go new file mode 100644 index 0000000000..f5e764e35b --- /dev/null +++ b/x/subspaces/legacy/v4/store_test.go @@ -0,0 +1,92 @@ +package v4_test + +import ( + "testing" + "time" + + authzkeeper "github.com/cosmos/cosmos-sdk/x/authz/keeper" + + subspacesauthz "github.com/desmos-labs/desmos/v4/x/subspaces/authz" + v4 "github.com/desmos-labs/desmos/v4/x/subspaces/legacy/v4" + + sdk "github.com/cosmos/cosmos-sdk/types" + capabilitytypes "github.com/cosmos/cosmos-sdk/x/capability/types" + paramstypes "github.com/cosmos/cosmos-sdk/x/params/types" + "github.com/stretchr/testify/require" + + "github.com/desmos-labs/desmos/v4/app" + "github.com/desmos-labs/desmos/v4/testutil/storetesting" + v2 "github.com/desmos-labs/desmos/v4/x/subspaces/legacy/v2" + "github.com/desmos-labs/desmos/v4/x/subspaces/types" +) + +func TestMigrateStore(t *testing.T) { + cdc, _ := app.MakeCodecs() + + // Build all the necessary keys + keys := sdk.NewKVStoreKeys(authzkeeper.StoreKey) + tKeys := sdk.NewTransientStoreKeys(paramstypes.TStoreKey) + memKeys := sdk.NewMemoryStoreKeys(capabilitytypes.MemStoreKey) + + // Build the authz keeper + authzKeeper := authzkeeper.NewKeeper(keys[authzkeeper.StoreKey], cdc, nil) + + testCases := []struct { + name string + store func(ctx sdk.Context) + shouldErr bool + check func(ctx sdk.Context) + }{ + { + name: "generic subspace authorization is migrated properly", + store: func(ctx sdk.Context) { + authorization := v2.NewGenericSubspaceAuthorization([]uint64{1, 2}, sdk.MsgTypeURL(&types.MsgEditSubspace{})) + expiration := time.Date(2020, 1, 1, 00, 00, 00, 000, time.UTC) + + granterAddr, err := sdk.AccAddressFromBech32("cosmos1d6j0q7akh0sg69qz7cz6y0e8kc6ljxz8p0ah2t") + require.NoError(t, err) + + granteeAddr, err := sdk.AccAddressFromBech32("cosmos1w6hk7984dpgtgxwqwuv5645yeq02c4svknv5ua") + require.NoError(t, err) + + err = authzKeeper.SaveGrant(ctx, granteeAddr, granterAddr, authorization, expiration) + require.NoError(t, err) + }, + check: func(ctx sdk.Context) { + granterAddr, err := sdk.AccAddressFromBech32("cosmos1d6j0q7akh0sg69qz7cz6y0e8kc6ljxz8p0ah2t") + require.NoError(t, err) + + granteeAddr, err := sdk.AccAddressFromBech32("cosmos1w6hk7984dpgtgxwqwuv5645yeq02c4svknv5ua") + require.NoError(t, err) + + authorizations := authzKeeper.GetAuthorizations(ctx, granteeAddr, granterAddr) + require.Len(t, authorizations, 1) + + require.Equal(t, subspacesauthz.NewGenericSubspaceAuthorization( + []uint64{1, 2}, + sdk.MsgTypeURL(&types.MsgEditSubspace{}), + ), authorizations[0]) + }, + }, + } + + for _, tc := range testCases { + tc := tc + t.Run(tc.name, func(t *testing.T) { + ctx := storetesting.BuildContext(keys, tKeys, memKeys) + if tc.store != nil { + tc.store(ctx) + } + + err := v4.MigrateStore(ctx, authzKeeper, cdc) + if tc.shouldErr { + require.Error(t, err) + } else { + require.NoError(t, err) + if tc.check != nil { + tc.check(ctx) + } + } + }) + } +} diff --git a/x/subspaces/module.go b/x/subspaces/module.go index cacf753142..14c2f78c23 100644 --- a/x/subspaces/module.go +++ b/x/subspaces/module.go @@ -6,6 +6,10 @@ import ( "fmt" "math/rand" + authzkeeper "github.com/cosmos/cosmos-sdk/x/authz/keeper" + + v2 "github.com/desmos-labs/desmos/v4/x/subspaces/legacy/v2" + "github.com/desmos-labs/desmos/v4/x/subspaces/authz" feeskeeper "github.com/desmos-labs/desmos/v4/x/fees/keeper" @@ -32,7 +36,7 @@ import ( ) const ( - consensusVersion = 3 + consensusVersion = 4 ) // type check to ensure the interface is properly implemented @@ -91,8 +95,9 @@ func (AppModuleBasic) GetQueryCmd() *cobra.Command { // RegisterInterfaces registers interfaces and implementations of the subspaces module. func (AppModuleBasic) RegisterInterfaces(registry codectypes.InterfaceRegistry) { - authz.RegisterInterfaces(registry) + v2.RegisterInterfaces(registry) types.RegisterInterfaces(registry) + authz.RegisterInterfaces(registry) } // -------------------------------------------------------------------------------------------------------------------- @@ -101,6 +106,7 @@ func (AppModuleBasic) RegisterInterfaces(registry codectypes.InterfaceRegistry) type AppModule struct { AppModuleBasic keeper keeper.Keeper + authzk authzkeeper.Keeper ak authkeeper.AccountKeeper bk bankkeeper.Keeper fk feeskeeper.Keeper @@ -111,7 +117,7 @@ func (am AppModule) RegisterServices(cfg module.Configurator) { types.RegisterMsgServer(cfg.MsgServer(), keeper.NewMsgServerImpl(am.keeper)) types.RegisterQueryServer(cfg.QueryServer(), am.keeper) - m := keeper.NewMigrator(am.keeper) + m := keeper.NewMigrator(am.keeper, am.authzk) err := cfg.RegisterMigration(types.ModuleName, 1, m.Migrate1to2) if err != nil { panic(err) @@ -120,15 +126,21 @@ func (am AppModule) RegisterServices(cfg module.Configurator) { if err != nil { panic(err) } + err = cfg.RegisterMigration(types.ModuleName, 3, m.Migrate3to4) + if err != nil { + panic(err) + } } // NewAppModule creates a new AppModule Object func NewAppModule( - cdc codec.Codec, keeper keeper.Keeper, ak authkeeper.AccountKeeper, bk bankkeeper.Keeper, fk feeskeeper.Keeper, + cdc codec.Codec, keeper keeper.Keeper, authzKeeper authzkeeper.Keeper, + ak authkeeper.AccountKeeper, bk bankkeeper.Keeper, fk feeskeeper.Keeper, ) AppModule { return AppModule{ AppModuleBasic: AppModuleBasic{cdc: cdc}, keeper: keeper, + authzk: authzKeeper, ak: ak, bk: bk, fk: fk,