Skip to content

Commit

Permalink
CM-278 Implement RenewLeaseAll
Browse files Browse the repository at this point in the history
  • Loading branch information
rnistuk committed Apr 2, 2020
1 parent ce96088 commit 6484125
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 1 deletion.
30 changes: 30 additions & 0 deletions x/crud/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ func GetTxCmd(_ string, cdc *codec.Codec) *cobra.Command {
GetCmdRead(cdc),
GetCmdRename(cdc),
GetCmdRenewLease(cdc),
GetCmdRenewLeaseAll(cdc),
GetCmdUpdate(cdc),
)...)

Expand Down Expand Up @@ -385,3 +386,32 @@ func GetCmdRenewLease(cdc *codec.Codec) *cobra.Command {
cc.PersistentFlags().Int64Var(&leaseValue, "lease", 0, "lease in blocks (default 172800 (10 days))")
return &cc
}

func GetCmdRenewLeaseAll(cdc *codec.Codec) *cobra.Command {
cc := cobra.Command{
Use: "renewleaseall [UUID]",
Short: "renew the lease of all existing entries in the database",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc)
inBuf := bufio.NewReader(cmd.InOrStdin())
txBldr := auth.NewTxBuilderFromCLI(inBuf).WithTxEncoder(utils.GetTxEncoder(cdc))

msg := types.MsgRenewLeaseAll{
UUID: args[0],
Lease: leaseValue,
Owner: cliCtx.GetFromAddress(),
}

err := msg.ValidateBasic()
if err != nil {
return err
}

return utils.GenerateOrBroadcastMsgs(cliCtx, txBldr, []sdk.Msg{msg})
},
}

cc.PersistentFlags().Int64Var(&leaseValue, "lease", 0, "lease in blocks (default 172800 (10 days))")
return &cc
}
1 change: 1 addition & 0 deletions x/crud/client/rest/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,5 @@ func RegisterRoutes(cliCtx context.CLIContext, r *mux.Router, storeName string)
r.HandleFunc(fmt.Sprintf("/%s/update", storeName), BlzUpdateHandler(cliCtx)).Methods("POST")
r.HandleFunc(fmt.Sprintf("/%s/version", storeName), BlzQVersionHandler(cliCtx, storeName)).Methods("GET")
r.HandleFunc(fmt.Sprintf("/%s/renewlease", storeName), BlzRenewLease(cliCtx)).Methods("POST")
r.HandleFunc(fmt.Sprintf("/%s/renewleaseall", storeName), BlzRenewLeaseAll(cliCtx)).Methods("POST")
}
45 changes: 45 additions & 0 deletions x/crud/client/rest/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -585,3 +585,48 @@ func BlzRenewLease(cliCtx context.CLIContext) http.HandlerFunc {
utils.WriteGenerateStdTxResponse(w, cliCtx, baseReq, []sdk.Msg{msg})
}
}

///////////////////////////////////////////////////////////////////////////////
// Renew Lease
type RenewLeaseAllReq struct {
BaseReq rest.BaseReq
UUID string
Lease int64
Owner string
}

func BlzRenewLeaseAll(cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req RenewLeaseAllReq

if !rest.ReadRESTReq(w, r, cliCtx.Codec, &req) {
rest.WriteErrorResponse(w, http.StatusBadRequest, "failed to parse request")
return
}

baseReq := req.BaseReq.Sanitize()
if !baseReq.ValidateBasic(w) {
return
}

addr, err := sdk.AccAddressFromBech32(req.Owner)
if err != nil {
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}

// create the message
msg := types.MsgRenewLeaseAll{
UUID: req.UUID,
Lease: req.Lease,
Owner: addr,
}
err = msg.ValidateBasic()
if err != nil {
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}

utils.WriteGenerateStdTxResponse(w, cliCtx, baseReq, []sdk.Msg{msg})
}
}
5 changes: 5 additions & 0 deletions x/crud/internal/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -521,4 +521,9 @@ func TestKeeper_GetNShortestLease(t *testing.T) {
response = keeper.GetNShortestLease(newCtx, testStore, "wronguuid", owner, 5)
assert.Equal(t, "wronguuid", response.UUID)
assert.Equal(t, 0, len(response.KeyLeases))

response = keeper.GetNShortestLease(newCtx, testStore, "uuid", owner, 11)
assert.Equal(t, "uuid", response.UUID)
assert.Equal(t, 10, len(response.KeyLeases))

}
2 changes: 1 addition & 1 deletion x/crud/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func TestAppModuleBasic_GetTxCmd(t *testing.T) {
assert.Equal(t, 2, cmd.SuggestionsMinimumDistance)

commands := cmd.Commands()
assert.Len(t, commands, 14)
assert.Len(t, commands, 15)
}
}

Expand Down

0 comments on commit 6484125

Please sign in to comment.