Skip to content

Commit

Permalink
fix: storage module migration
Browse files Browse the repository at this point in the history
  • Loading branch information
j75689 committed Jul 28, 2023
1 parent f77bbcf commit 9f0f902
Show file tree
Hide file tree
Showing 12 changed files with 410 additions and 45 deletions.
2 changes: 1 addition & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ var (
paymentmodule.AppModuleBasic{},
permissionmodule.AppModuleBasic{},
virtualgroupmodule.AppModuleBasic{},
storagemodule.AppModuleBasic{},
&storagemodule.AppModuleBasic{},
challengemodule.AppModuleBasic{},
)

Expand Down
16 changes: 5 additions & 11 deletions app/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

storagemodule "github.com/bnb-chain/greenfield/x/storage"
storagetypesV1 "github.com/bnb-chain/greenfield/x/storage/types"
storagetypesV2 "github.com/bnb-chain/greenfield/x/storage/types/v2"
serverconfig "github.com/cosmos/cosmos-sdk/server/config"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
Expand Down Expand Up @@ -48,27 +49,20 @@ func (app *App) registerBEP1001UpgradeHandler() {
// Register the upgrade handler
app.UpgradeKeeper.SetUpgradeHandler(upgradetypes.BEP1001,
func(ctx sdk.Context, plan upgradetypes.Plan, fromVM module.VersionMap) (module.VersionMap, error) {
app.Logger().Info("upgrade to ", plan.Name)
storageModule, ok := app.mm.Modules[storagetypesV1.ModuleName].(storagemodule.AppModule)
if !ok {
return nil, fmt.Errorf("storage module not found")
}
err := storageModule.MigrateToV2(app.configurator)
if err != nil {
return nil, err
}
app.Logger().Info("processing upgrade handler", "name", plan.Name, "info", plan.Info)
fromVM[storagetypesV1.ModuleName] = storagetypesV2.ModuleVersion
return fromVM, nil
})

// Register the upgrade initializer
app.UpgradeKeeper.SetUpgradeInitializer(upgradetypes.BEP1001,
func() error {
app.Logger().Info("Init", upgradetypes.BEP1001, "upgrade")
app.Logger().Info("processing upgrade initializer", "name", upgradetypes.BEP1001)
storageModule, ok := app.mm.Modules[storagetypesV1.ModuleName].(storagemodule.AppModule)
if !ok {
return fmt.Errorf("storage module not found")
}
err := storageModule.MigrateToV2(app.configurator)
err := storageModule.MigrateToV2(app.configurator, app.InterfaceRegistry())
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ replace (
github.com/cometbft/cometbft-db => github.com/bnb-chain/greenfield-cometbft-db v0.8.1-alpha.1
github.com/confio/ics23/go => github.com/cosmos/cosmos-sdk/ics23/go v0.8.0
// TODO: bump to the official version after the release
github.com/cosmos/cosmos-sdk => github.com/j75689/greenfield-cosmos-sdk v0.0.0-20230727234701-4a1b74288c1c //github.com/bnb-chain/greenfield-cosmos-sdk v0.2.3-alpha.3.0.20230725020809-dbb91ace4379
github.com/cosmos/cosmos-sdk => github.com/j75689/greenfield-cosmos-sdk v0.0.0-20230728101132-bf4bb854669e //github.com/bnb-chain/greenfield-cosmos-sdk v0.2.3-alpha.3.0.20230725020809-dbb91ace4379
github.com/cosmos/iavl => github.com/bnb-chain/greenfield-iavl v0.20.1-alpha.1
github.com/syndtr/goleveldb => github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -702,8 +702,8 @@ github.com/ipfs/go-log/v2 v2.1.1/go.mod h1:2v2nsGfZsvvAJz13SyFzf9ObaqwHiHxsPLEHn
github.com/ipfs/go-log/v2 v2.1.3/go.mod h1:/8d0SH3Su5Ooc31QlL1WysJhvyOTDCjcCZ9Axpmri6g=
github.com/ipfs/go-log/v2 v2.3.0/go.mod h1:QqGoj30OTpnKaG/LKTGTxoP2mmQtjVMEnK72gynbe/g=
github.com/ipfs/go-log/v2 v2.4.0/go.mod h1:nPZnh7Cj7lwS3LpRU5Mwr2ol1c2gXIEXuF6aywqrtmo=
github.com/j75689/greenfield-cosmos-sdk v0.0.0-20230727234701-4a1b74288c1c h1:Uq/nrWVSAeF+ctfq8KduwMxBIRxxRzmF/sGCarJNRpE=
github.com/j75689/greenfield-cosmos-sdk v0.0.0-20230727234701-4a1b74288c1c/go.mod h1:EghjYxFg4NRNMfTJ6g9rVhjImhXQm+tuboknHqeGmJA=
github.com/j75689/greenfield-cosmos-sdk v0.0.0-20230728101132-bf4bb854669e h1:9wxAyBxQfREavAVP91+IyuepYmH8M3WdzhA/+Z8/+YM=
github.com/j75689/greenfield-cosmos-sdk v0.0.0-20230728101132-bf4bb854669e/go.mod h1:EghjYxFg4NRNMfTJ6g9rVhjImhXQm+tuboknHqeGmJA=
github.com/jackpal/go-nat-pmp v1.0.2-0.20160603034137-1fa385a6f458/go.mod h1:QPH045xvCAeXUZOxsnwmrtiCoxIr9eob+4orBN1SBKc=
github.com/jackpal/go-nat-pmp v1.0.2/go.mod h1:QPH045xvCAeXUZOxsnwmrtiCoxIr9eob+4orBN1SBKc=
github.com/jbenet/go-cienv v0.1.0/go.mod h1:TqNnHUmJgXau0nCzC7kXWeotg3J9W34CUv5Djy1+FlA=
Expand Down
36 changes: 36 additions & 0 deletions x/storage/client/cli/v2/query.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package v2

import (
"fmt"

"github.com/bnb-chain/greenfield/x/storage/client/cli"
"github.com/bnb-chain/greenfield/x/storage/types"
"github.com/cosmos/cosmos-sdk/client"
"github.com/spf13/cobra"
)

// GetQueryCmd returns the cli query commands for this module
func GetQueryCmd(queryRoute string) *cobra.Command {
// Group storage queries under a subcommand
storageQueryCmd := &cobra.Command{
Use: types.ModuleName,
Short: fmt.Sprintf("Querying commands for the %s module", types.ModuleName),
DisableFlagParsing: true,
SuggestionsMinimumDistance: 2,
RunE: client.ValidateCmd,
}

storageQueryCmd.AddCommand(
cli.CmdQueryParams(),
cli.CmdHeadBucket(),
cli.CmdHeadObject(),
cli.CmdListBuckets(),
cli.CmdListObjects(),
cli.CmdVerifyPermission(),
cli.CmdHeadGroup(),
cli.CmdListGroup(),
// request body did not change, so we can reuse the same command
cli.CmdHeadGroupMember())

return storageQueryCmd
}
158 changes: 158 additions & 0 deletions x/storage/client/cli/v2/tx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
package v2

import (
"fmt"
"strings"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/tx"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/spf13/cobra"

"github.com/bnb-chain/greenfield/x/storage/client/cli"
"github.com/bnb-chain/greenfield/x/storage/types"
)

// GetTxCmd returns the transaction commands for this module
func GetTxCmd() *cobra.Command {
cmd := &cobra.Command{
Use: types.ModuleName,
Short: fmt.Sprintf("%s transactions subcommands", types.ModuleName),
DisableFlagParsing: true,
SuggestionsMinimumDistance: 2,
RunE: client.ValidateCmd,
}

cmd.AddCommand(
cli.CmdCreateBucket(),
cli.CmdDeleteBucket(),
cli.CmdUpdateBucketInfo(),
cli.CmdMirrorBucket(),
cli.CmdDiscontinueBucket(),
)

cmd.AddCommand(
cli.CmdCreateObject(),
cli.CmdDeleteObject(),
cli.CmdCancelCreateObject(),
cli.CmdCopyObject(),
cli.CmdMirrorObject(),
cli.CmdDiscontinueObject(),
cli.CmdUpdateObjectInfo(),
)

cmd.AddCommand(
CmdCreateGroup(),
cli.CmdDeleteGroup(),
CmdUpdateGroupMember(),
cli.CmdUpdateGroupExtra(),
cli.CmdLeaveGroup(),
cli.CmdMirrorGroup(),
)

cmd.AddCommand(
cli.CmdPutPolicy(),
cli.CmdDeletePolicy(),
)

cmd.AddCommand(cli.CmdCancelMigrateBucket())
return cmd
}

func CmdCreateGroup() *cobra.Command {
cmd := &cobra.Command{
Use: "create-group [group-name]",
Short: "Create a new group with optional members, split member addresses by ','",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) (err error) {
argGroupName := args[0]
argMemberList, _ := cmd.Flags().GetString(cli.FlagMemberList)
extra, _ := cmd.Flags().GetString(cli.FlagExtra)

clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}

var memberAddrs []sdk.AccAddress
if argMemberList != "" {
members := strings.Split(argMemberList, ",")
for _, member := range members {
memberAddr, err := sdk.AccAddressFromHexUnsafe(member)
if err != nil {
return err
}
memberAddrs = append(memberAddrs, memberAddr)
}
}
msg := types.NewMsgCreateGroup(
clientCtx.GetFromAddress(),
argGroupName,
memberAddrs,
extra,
)
if err := msg.ValidateBasic(); err != nil {
return err
}
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}

cmd.Flags().String(cli.FlagExtra, "", "extra info for the group")
cmd.Flags().String(cli.FlagMemberList, "", "init members of the group")
flags.AddTxFlagsToCmd(cmd)

return cmd
}

func CmdUpdateGroupMember() *cobra.Command {
cmd := &cobra.Command{
Use: "update-group-member [group-name] [member-to-add] [member-to-delete]",
Short: "Update the member of the group you own, split member addresses by ,",
Args: cobra.ExactArgs(3),
RunE: func(cmd *cobra.Command, args []string) (err error) {
argGroupName := args[0]
argMemberToAdd := args[1]
argMemberToDelete := args[2]

clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
var memberAddrsToAdd []sdk.AccAddress
membersToAdd := strings.Split(argMemberToAdd, ",")
for _, member := range membersToAdd {
memberAddr, err := sdk.AccAddressFromHexUnsafe(member)
if err != nil {
return err
}
memberAddrsToAdd = append(memberAddrsToAdd, memberAddr)
}
var memberAddrsToDelete []sdk.AccAddress
membersToDelete := strings.Split(argMemberToDelete, ",")
for _, member := range membersToDelete {
memberAddr, err := sdk.AccAddressFromHexUnsafe(member)
if err != nil {
return err
}
memberAddrsToDelete = append(memberAddrsToDelete, memberAddr)
}
msg := types.NewMsgUpdateGroupMember(
clientCtx.GetFromAddress(),
clientCtx.GetFromAddress(),
argGroupName,
memberAddrsToAdd,
memberAddrsToDelete,
)
if err := msg.ValidateBasic(); err != nil {
return err
}
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}

flags.AddTxFlagsToCmd(cmd)

return cmd
}
Loading

0 comments on commit 9f0f902

Please sign in to comment.