-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of github.com:desmos-labs/desmos into riccardo/…
…fix-permission-messages � Conflicts: � x/subspaces/keeper/msg_server.go � x/subspaces/keeper/msg_server_test.go
- Loading branch information
Showing
11 changed files
with
411 additions
and
32 deletions.
There are no files selected for viewing
6 changes: 6 additions & 0 deletions
6
.changeset/entries/6e78e6f7f9486799935df288f3d1467a646116fddc2952e79e559b1d4cc58d79.yaml
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,6 @@ | ||
type: fix | ||
module: x/subspaces | ||
pull_request: 801 | ||
description: Added permissions sanitization | ||
backward_compatible: true | ||
date: 2022-04-01T09:09:14.889652713Z |
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,32 @@ | ||
package testutil | ||
|
||
import ( | ||
"github.com/cosmos/cosmos-sdk/store" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/tendermint/tendermint/libs/log" | ||
tmproto "github.com/tendermint/tendermint/proto/tendermint/types" | ||
dbm "github.com/tendermint/tm-db" | ||
) | ||
|
||
func BuildContext( | ||
keys map[string]*sdk.KVStoreKey, tKeys map[string]*sdk.TransientStoreKey, memKeys map[string]*sdk.MemoryStoreKey, | ||
) sdk.Context { | ||
db := dbm.NewMemDB() | ||
cms := store.NewCommitMultiStore(db) | ||
for _, key := range keys { | ||
cms.MountStoreWithDB(key, sdk.StoreTypeIAVL, db) | ||
} | ||
for _, tKey := range tKeys { | ||
cms.MountStoreWithDB(tKey, sdk.StoreTypeTransient, db) | ||
} | ||
for _, memKey := range memKeys { | ||
cms.MountStoreWithDB(memKey, sdk.StoreTypeMemory, nil) | ||
} | ||
|
||
err := cms.LoadLatestVersion() | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
return sdk.NewContext(cms, tmproto.Header{}, false, log.NewNopLogger()) | ||
} |
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,26 @@ | ||
package keeper | ||
|
||
import ( | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
|
||
v2 "github.com/desmos-labs/desmos/v3/x/subspaces/legacy/v2" | ||
) | ||
|
||
// DONTCOVER | ||
|
||
// Migrator is a struct for handling in-place store migrations. | ||
type Migrator struct { | ||
keeper Keeper | ||
} | ||
|
||
// NewMigrator returns a new Migrator | ||
func NewMigrator(keeper Keeper) Migrator { | ||
return Migrator{ | ||
keeper: keeper, | ||
} | ||
} | ||
|
||
// Migrate1to2 migrates from version 1 to 2. | ||
func (m Migrator) Migrate1to2(ctx sdk.Context) error { | ||
return v2.MigrateStore(ctx, m.keeper.storeKey, m.keeper.cdc) | ||
} |
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
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,87 @@ | ||
package v2 | ||
|
||
import ( | ||
"github.com/cosmos/cosmos-sdk/codec" | ||
"github.com/cosmos/cosmos-sdk/store/prefix" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
|
||
"github.com/desmos-labs/desmos/v3/x/subspaces/types" | ||
) | ||
|
||
func MigrateStore(ctx sdk.Context, storeKey sdk.StoreKey, cdc codec.BinaryCodec) error { | ||
store := ctx.KVStore(storeKey) | ||
|
||
err := fixGroupsPermissions(store, cdc) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
fixUsersPermissions(store) | ||
|
||
return nil | ||
} | ||
|
||
func fixGroupsPermissions(store sdk.KVStore, cdc codec.BinaryCodec) error { | ||
groupsStore := prefix.NewStore(store, types.GroupsPrefix) | ||
iterator := groupsStore.Iterator(nil, nil) | ||
|
||
var groups []types.UserGroup | ||
for ; iterator.Valid(); iterator.Next() { | ||
var group types.UserGroup | ||
err := cdc.Unmarshal(iterator.Value(), &group) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Sanitize the permissions | ||
group.Permissions = types.SanitizePermission(group.Permissions) | ||
groups = append(groups, group) | ||
} | ||
|
||
iterator.Close() | ||
|
||
// Store the new groups | ||
for i, group := range groups { | ||
bz, err := cdc.Marshal(&groups[i]) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
store.Set(types.GroupStoreKey(group.SubspaceID, group.ID), bz) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
type userPermissionDetails struct { | ||
subspaceID uint64 | ||
user sdk.AccAddress | ||
permissions types.Permission | ||
} | ||
|
||
func fixUsersPermissions(store sdk.KVStore) { | ||
permissionsStore := prefix.NewStore(store, types.UserPermissionsStorePrefix) | ||
iterator := permissionsStore.Iterator(nil, nil) | ||
|
||
var permissions []userPermissionDetails | ||
for ; iterator.Valid(); iterator.Next() { | ||
// The first 8 bytes are the subspace id (uint64 takes up 8 bytes) | ||
// The remaining bytes are the user address | ||
subspaceBz, addressBz := iterator.Key()[:8], iterator.Key()[8:] | ||
|
||
permissions = append(permissions, userPermissionDetails{ | ||
subspaceID: types.GetSubspaceIDFromBytes(subspaceBz), | ||
user: types.GetAddressBytes(addressBz), | ||
|
||
// Sanitize the permission | ||
permissions: types.SanitizePermission(types.UnmarshalPermission(iterator.Value())), | ||
}) | ||
} | ||
|
||
iterator.Close() | ||
|
||
// Store the new permissions | ||
for _, entry := range permissions { | ||
store.Set(types.UserPermissionStoreKey(entry.subspaceID, entry.user), types.MarshalPermission(entry.permissions)) | ||
} | ||
} |
Oops, something went wrong.