Skip to content

Commit

Permalink
#13 Added cli and REST query support, cancel upgrade proposal to cons…
Browse files Browse the repository at this point in the history
…ortium module
  • Loading branch information
aaronc committed Mar 26, 2019
1 parent a87f722 commit 52beb32
Show file tree
Hide file tree
Showing 10 changed files with 131 additions and 15 deletions.
6 changes: 6 additions & 0 deletions cmd/xrncli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ import (
geoclient "github.com/regen-network/regen-ledger/x/geo/client"
agentclient "github.com/regen-network/regen-ledger/x/group/client"
proposalclient "github.com/regen-network/regen-ledger/x/proposal/client"
upgradecli "github.com/regen-network/regen-ledger/x/upgrade/client/cli"
upgraderest "github.com/regen-network/regen-ledger/x/upgrade/client/rest"
cmn "github.com/tendermint/tendermint/libs/common"
)

Expand All @@ -38,6 +40,7 @@ const (
storeData = "data"
storeAgent = "group"
storeProposal = "proposal"
storeUpgrade = "upgrade"
)

var defaultCLIHome = os.ExpandEnv("$HOME/.xrncli")
Expand Down Expand Up @@ -178,6 +181,7 @@ func registerRoutes(rs *lcd.RestServer) {
auth.RegisterRoutes(rs.CliCtx, rs.Mux, rs.Cdc, storeAcc)
bank.RegisterRoutes(rs.CliCtx, rs.Mux, rs.Cdc, rs.KeyBase)
datarest.RegisterRoutes(rs.CliCtx, rs.Mux, rs.Cdc, storeData)
upgraderest.RegisterRoutes(rs.CliCtx, rs.Mux, rs.Cdc, "upgrade-plan", storeUpgrade)
}

func queryCmd(cdc *amino.Codec, mc []sdk.ModuleClients) *cobra.Command {
Expand All @@ -200,6 +204,8 @@ func queryCmd(cdc *amino.Codec, mc []sdk.ModuleClients) *cobra.Command {
queryCmd.AddCommand(m.GetQueryCmd())
}

queryCmd.AddCommand(upgradecli.GetQueryCmd("upgrade-plan", storeUpgrade, cdc))

addNodeFlags(queryCmd)

return queryCmd
Expand Down
25 changes: 16 additions & 9 deletions x/consortium/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@ func GetCmdProposeUpgrade(cdc *codec.Codec) *cobra.Command {
if err != nil {
return nil, fmt.Errorf("error parsing time: %+v", err)
}

if height != 0 {
return nil, fmt.Errorf("only one of --time or --height should be specified")
}
}

info := make(map[string]interface{})
Expand All @@ -52,10 +48,21 @@ func GetCmdProposeUpgrade(cdc *codec.Codec) *cobra.Command {
})

cmd.Args = cobra.ExactArgs(1)
cmd.Use = "propose-upgrade <name> [--time <time> | --height <height>] [--commit <commit-hash>]"
cmd.Short = "Propose an ESP version"
cmd.Flags().StringVar(&timeStr, "time", "", "The time after which the upgrade must happen in ISO8601/RFC3339 format (omit if using --upgrade-height)")
cmd.Flags().Int64Var(&height, "height", 0, "The height at which the upgrade must happen (omit if using --upgrade-time)")
cmd.Flags().StringVar(&commit, "commit", "", "The git commit hash of the version of the software to upgrade to")
cmd.Use = "propose-upgrade <name> [--upgrade-time <time> | --upgrade-height <height>] [--upgrade-commit <commit-hash>]"
cmd.Short = "Propose a software upgrade"
cmd.Flags().StringVar(&timeStr, "upgrade-time", "", "The time after which the upgrade must happen in ISO8601/RFC3339 format (omit if using --upgrade-height)")
cmd.Flags().Int64Var(&height, "upgrade-height", 0, "The height at which the upgrade must happen (omit if using --upgrade-time)")
cmd.Flags().StringVar(&commit, "upgrade-commit", "", "The git commit hash of the version of the software to upgrade to")
return cmd
}

func GetCmdProposeCancelUpgrade(cdc *codec.Codec) *cobra.Command {
cmd := proposalcli.GetCmdPropose(cdc, func(cmd *cobra.Command, args []string) (action proposal.ProposalAction, e error) {
return consortium.ActionCancelUpgrade{}, nil
})

cmd.Args = cobra.ExactArgs(0)
cmd.Use = "propose-cancel-upgrade <name> [--upgrade-time <time> | --upgrade-height <height>] [--upgrade-commit <commit-hash>]"
cmd.Short = "Propose that a pending software upgrade be cancelled"
return cmd
}
1 change: 1 addition & 0 deletions x/consortium/client/module_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func (mc ModuleClient) GetTxCmd() *cobra.Command {

consortiumTxCmd.AddCommand(client.PostCommands(
consortiumcmd.GetCmdProposeUpgrade(mc.cdc),
consortiumcmd.GetCmdProposeCancelUpgrade(mc.cdc),
)...)

return consortiumTxCmd
Expand Down
3 changes: 2 additions & 1 deletion x/consortium/codec.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package consortium

import (
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/codec"
)

// RegisterCodec registers concrete types on the Amino codec
func RegisterCodec(cdc *codec.Codec) {
cdc.RegisterConcrete(ActionScheduleUpgrade{}, "consortium/ActionScheduleUpgrade", nil)
cdc.RegisterConcrete(ActionCancelUpgrade{}, "consortium/ActionCancelUpgrade", nil)
cdc.RegisterConcrete(ActionChangeValidatorSet{}, "consortium/ActionChangeValidatorSet", nil)
}
1 change: 1 addition & 0 deletions x/proposal/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ func (keeper Keeper) TryExecute(ctx sdk.Context, proposalId []byte) sdk.Result {
if res.Code == sdk.CodeOK {
store := ctx.KVStore(keeper.storeKey)
store.Delete(proposalId)
res.Tags = res.Tags.AppendTag("action", proposal.Action.Type())
}

return res
Expand Down
36 changes: 36 additions & 0 deletions x/upgrade/client/cli/query.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package cli

import (
"fmt"
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/regen-network/regen-ledger/x/upgrade"
"github.com/spf13/cobra"
)

func GetQueryCmd(cmdName string, storeName string, cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: cmdName,
Short: "get upgrade plan (if one exists)",
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc)

res, err := cliCtx.QueryStore([]byte(upgrade.PlanKey), storeName)
if err != nil {
return err
}

if len(res) == 0 {
return fmt.Errorf("no upgrade scheduled")
}

var plan upgrade.Plan
err = cdc.UnmarshalBinaryBare(res, &plan)
if err != nil {
return err
}
return cliCtx.PrintOutput(plan)
},
}
}
39 changes: 39 additions & 0 deletions x/upgrade/client/rest/rest.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package rest

import (
"fmt"
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/types/rest"
"github.com/gorilla/mux"
"github.com/regen-network/regen-ledger/x/upgrade"
"net/http"
)

func RegisterRoutes(cliCtx context.CLIContext, r *mux.Router, cdc *codec.Codec, routeName string, storeName string) {
r.HandleFunc(fmt.Sprintf("/%s", routeName), getUpgradePlanHandler(cdc, cliCtx, storeName)).Methods("GET")
}

func getUpgradePlanHandler(cdc *codec.Codec, cliCtx context.CLIContext, storeName string) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, request *http.Request) {
res, err := cliCtx.QueryStore([]byte(upgrade.PlanKey), storeName)
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}

if len(res) == 0 {
http.NotFound(w, request)
return
}

var plan upgrade.Plan
err = cdc.UnmarshalBinaryBare(res, &plan)
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}

rest.PostProcessResponse(w, cdc, plan, cliCtx.Indent)
}
}
5 changes: 5 additions & 0 deletions x/upgrade/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package upgrade

/*
Package upgrade
*/
11 changes: 7 additions & 4 deletions x/upgrade/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type Keeper struct {
}

const (
planKey = "plan"
PlanKey = "plan"
)

// NewKeeper constructs an upgrade keeper
Expand Down Expand Up @@ -45,6 +45,9 @@ func (keeper Keeper) ScheduleUpgrade(ctx sdk.Context, plan Plan) sdk.Error {
if !plan.Time.After(ctx.BlockHeader().Time) {
return sdk.ErrUnknownRequest("Upgrade cannot be scheduled in the past")
}
if plan.Height != 0 {
return sdk.ErrUnknownRequest("Only one of Time or Height should be specified")
}
} else {
if plan.Height <= ctx.BlockHeight() {
return sdk.ErrUnknownRequest("Upgrade cannot be scheduled in the past")
Expand All @@ -55,14 +58,14 @@ func (keeper Keeper) ScheduleUpgrade(ctx sdk.Context, plan Plan) sdk.Error {
return sdk.ErrUnknownRequest(fmt.Sprintf("Upgrade with name %s has already been completed", plan.Name))
}
bz := keeper.cdc.MustMarshalBinaryBare(plan)
store.Set([]byte(planKey), bz)
store.Set([]byte(PlanKey), bz)
return nil
}

// ClearUpgradePlan clears any schedule upgrade
func (keeper Keeper) ClearUpgradePlan(ctx sdk.Context) {
store := ctx.KVStore(keeper.storeKey)
store.Delete([]byte(planKey))
store.Delete([]byte(PlanKey))
}

// ValidateBasic does basic validation of an Plan
Expand All @@ -78,7 +81,7 @@ func (plan Plan) ValidateBasic() sdk.Error {
// upgrade or false if there is none
func (keeper Keeper) GetUpgradePlan(ctx sdk.Context) (plan Plan, havePlan bool) {
store := ctx.KVStore(keeper.storeKey)
bz := store.Get([]byte(planKey))
bz := store.Get([]byte(PlanKey))
if bz == nil {
return plan, false
}
Expand Down
19 changes: 18 additions & 1 deletion x/upgrade/types.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package upgrade

import "time"
import (
"fmt"
"time"
)
import sdk "github.com/cosmos/cosmos-sdk/types"

// Plan specifies information about a planned upgrade and when it should occur
Expand All @@ -27,3 +30,17 @@ type Plan struct {

// UpgradeHandler specifies the type of function that is called when an upgrade is applied
type Handler func(ctx sdk.Context, plan Plan)

func (plan Plan) String() string {
if !plan.Time.IsZero() {
return fmt.Sprintf(`Upgrade Plan
Name: %s
Time: %s
Info: %s`, plan.Name, plan.Time.Format(time.RFC3339), plan.Info)
} else {
return fmt.Sprintf(`Upgrade Plan
Name: %s
Height: %d
Info: %s`, plan.Name, plan.Height, plan.Info)
}
}

0 comments on commit 52beb32

Please sign in to comment.