Skip to content

Commit

Permalink
CM-292 Added lease support
Browse files Browse the repository at this point in the history
  • Loading branch information
ebruck committed Mar 25, 2020
1 parent 3450307 commit d07bddb
Show file tree
Hide file tree
Showing 22 changed files with 673 additions and 264 deletions.
26 changes: 14 additions & 12 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,12 @@ import (
"time"
)

const crudModuleEntry = "bluzelle_crud"
const maxKeysSize = uint64(102400)
const maxKeyValuesSize = uint64(102400)
const (
crudModuleEntry = "bluzelle_crud"
maxKeysSize = uint64(102400)
maxKeyValuesSize = uint64(102400)
DefaultLeaseBlockHeight = int64(10 * 86400 / 5) // (10 days of blocks * seconds/day) / 5
)

var (
// default home directories for the application CLI
Expand Down Expand Up @@ -136,7 +139,7 @@ func NewCRUDApp(
bApp.SetAppVersion(version.Version)

keys := sdk.NewKVStoreKeys(bam.MainStoreKey, auth.StoreKey, staking.StoreKey,
supply.StoreKey, distr.StoreKey, slashing.StoreKey, params.StoreKey, crud.StoreKey, faucet.StoreKey)
supply.StoreKey, distr.StoreKey, slashing.StoreKey, params.StoreKey, crud.StoreKey, faucet.StoreKey, crud.LeaseKey)

tkeys := sdk.NewTransientStoreKeys(staking.TStoreKey, params.TStoreKey)

Expand Down Expand Up @@ -219,19 +222,16 @@ func NewCRUDApp(
app.crudKeeper = crud.NewKeeper(
app.bankKeeper,
keys[crud.StoreKey],
keys[crud.LeaseKey],
app.cdc,
crud.MaxKeeperSizes{MaxKeysSize: maxKeysSize, MaxKeyValuesSize: maxKeyValuesSize},
crud.MaxKeeperSizes{MaxKeysSize: maxKeysSize, MaxKeyValuesSize: maxKeyValuesSize, MaxDefaultLeaseBlocks: DefaultLeaseBlockHeight},
)

// Example use of the faucet:
//
// blzcli tx faucet mintfor $(blzcli keys show neeraj -a) --from vuser --gas-prices 0.01bnt

app.faucetKeeper = faucet.NewKeeper(
app.supplyKeeper,
app.stakingKeeper,
2000000000, // How many tokens to mint out for every successful request
5*time.Minute, // Time you have to wait before you can use the faucet again for a given beneficiary address
2000000000,
5*time.Minute,
keys[faucet.StoreKey],
app.cdc)

Expand Down Expand Up @@ -320,7 +320,9 @@ func (app *CRUDApp) BeginBlocker(ctx sdk.Context, req abci.RequestBeginBlock) ab
}

func (app *CRUDApp) EndBlocker(ctx sdk.Context, req abci.RequestEndBlock) abci.ResponseEndBlock {
return app.mm.EndBlock(ctx, req)
r := app.mm.EndBlock(ctx, req)
app.crudKeeper.ProcessLeasesAtBlockHeight(ctx, app.crudKeeper.GetKVStore(ctx), app.crudKeeper.GetLeaseStore(ctx), ctx.BlockHeight())
return r
}

func (app *CRUDApp) LoadHeight(height int64) error {
Expand Down
5 changes: 3 additions & 2 deletions docs/commands/useful.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,9 @@ request transaction
}

***
## query distribution validator-outstanding-rewards
>Query distribution outstanding (un-withdrawn) rewards for a validator and all their delegations
# query distribution validator-outstanding-rewards
>Query distribution outstanding (un-withdrawn) rewards for a validator and all
their delegations

blzcli query distribution validator-outstanding-rewards [validator] [flags]

Expand Down
2 changes: 1 addition & 1 deletion docs/setup/deploy.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ document.
this can be done from the command line with

sed -i -e '/^##### advanced configuration options #####/a \
output = "json"' ~/.blzd/config/config.toml
output = "json"' ~/.blzd/config/config.toml

4. Edit “.blzd/config/genesis.json” to change bond_denom from “stake” to
“ubnt”. This genesis.json file will be used to initialize the blockchain
Expand Down
1 change: 1 addition & 0 deletions x/crud/alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const (
ModuleName = types.ModuleName
RouterKey = types.RouterKey
StoreKey = types.StoreKey
LeaseKey = types.LeaseKey
)

var (
Expand Down
2 changes: 1 addition & 1 deletion x/crud/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func GetCmdQRead(queryRoute string, cdc *codec.Codec) *cobra.Command {
res, _, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/read/%s/%s", queryRoute, UUID, key), nil)

if err != nil {
fmt.Printf("could not read key - %s : %s", UUID, key)
fmt.Printf("could not read key - %s : %s\n", UUID, key)
return nil
}
var out types.QueryResultRead
Expand Down
16 changes: 11 additions & 5 deletions x/crud/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import (
"github.com/spf13/cobra"
)

var leaseValue int64

func GetTxCmd(_ string, cdc *codec.Codec) *cobra.Command {
crudTxCmd := &cobra.Command{
Use: types.ModuleName,
Expand All @@ -54,7 +56,7 @@ func GetTxCmd(_ string, cdc *codec.Codec) *cobra.Command {
}

func GetCmdCreate(cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
cc := cobra.Command{
Use: "create [UUID] [key] [value]",
Short: "create a new entry in the database",
Args: cobra.ExactArgs(3),
Expand All @@ -63,16 +65,17 @@ func GetCmdCreate(cdc *codec.Codec) *cobra.Command {
inBuf := bufio.NewReader(cmd.InOrStdin())
txBldr := auth.NewTxBuilderFromCLI(inBuf).WithTxEncoder(utils.GetTxEncoder(cdc))

msg := types.NewMsgCreate(args[0], args[1], args[2], cliCtx.GetFromAddress())
msg := types.NewMsgCreate(args[0], args[1], args[2], leaseValue, 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
}

func GetCmdRead(cdc *codec.Codec) *cobra.Command {
Expand All @@ -97,15 +100,15 @@ func GetCmdRead(cdc *codec.Codec) *cobra.Command {
}

func GetCmdUpdate(cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
cc := cobra.Command{
Use: "update [UUID] [key] [value]",
Short: "update an existing entry in the database",
Args: cobra.ExactArgs(3),
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.NewMsgUpdate(args[0], args[1], args[2], cliCtx.GetFromAddress())
msg := types.MsgUpdate{UUID: args[0], Key: args[1], Value: args[2], Lease: leaseValue, Owner: cliCtx.GetFromAddress()}

err := msg.ValidateBasic()
if err != nil {
Expand All @@ -115,6 +118,9 @@ func GetCmdUpdate(cdc *codec.Codec) *cobra.Command {
return utils.GenerateOrBroadcastMsgs(cliCtx, txBldr, []sdk.Msg{msg})
},
}

cc.PersistentFlags().Int64Var(&leaseValue, "lease", 0, "lease in blocks (default 0 (no change))")
return &cc
}

func GetCmdDelete(cdc *codec.Codec) *cobra.Command {
Expand Down
6 changes: 4 additions & 2 deletions x/crud/client/rest/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type createReq struct {
UUID string
Key string
Value string
Lease int64
Owner string
}

Expand All @@ -53,7 +54,7 @@ func BlzCreateHandler(cliCtx context.CLIContext) http.HandlerFunc {
return
}

msg := types.NewMsgCreate(req.UUID, req.Key, req.Value, addr)
msg := types.NewMsgCreate(req.UUID, req.Key, req.Value, req.Lease, addr)
err = msg.ValidateBasic()
if err != nil {
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
Expand Down Expand Up @@ -112,6 +113,7 @@ type updateReq struct {
UUID string
Key string
Value string
Lease int64
Owner string
}

Expand All @@ -135,7 +137,7 @@ func BlzUpdateHandler(cliCtx context.CLIContext) http.HandlerFunc {
return
}

msg := types.NewMsgUpdate(req.UUID, req.Key, req.Value, addr)
msg := types.MsgUpdate{req.UUID, req.Key, req.Value, req.Lease, addr}
err = msg.ValidateBasic()
if err != nil {
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
Expand Down
6 changes: 3 additions & 3 deletions x/crud/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ func TestValidateGenesis(t *testing.T) {

assert.Nil(t, ValidateGenesis(genesisState))

blzValues = append(blzValues, types.BLZValue{"test", []byte("notnilowner")})
blzValues = append(blzValues, types.BLZValue{"test", 0, 0, []byte("notnilowner")})

assert.Nil(t, ValidateGenesis(genesisState))

blzValues = append(blzValues, types.BLZValue{"test", nil})
blzValues = append(blzValues, types.BLZValue{"test", 0, 0, nil})

assert.Nil(t, ValidateGenesis(genesisState))
}
Expand All @@ -65,7 +65,7 @@ func TestInitGenesis(t *testing.T) {
data.BlzValues = append(data.BlzValues, types.BLZValue{Value: "test", Owner: owner})

mockKeeper.EXPECT().
SetBLZValue(ctx, nil, "UUID-Genesis", "Key-Genesis",
SetValue(ctx, nil, "UUID-Genesis", "Key-Genesis",
types.BLZValue{Value: "test", Owner: owner})

mockKeeper.EXPECT().
Expand Down
42 changes: 37 additions & 5 deletions x/crud/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,21 @@ func handleMsgCreate(ctx sdk.Context, keeper keeper.IKeeper, msg types.MsgCreate
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "Key already exists")
}

// default lease...
if msg.Lease == 0 {
msg.Lease = keeper.GetDefaultLeaseBlocks()
}

keeper.SetValue(ctx, keeper.GetKVStore(ctx), msg.UUID, msg.Key, types.BLZValue{
Value: msg.Value,
Owner: msg.Owner,
Value: msg.Value,
Owner: msg.Owner,
Lease: msg.Lease,
Height: ctx.BlockHeight(),
})

leaseCtx := ctx.WithGasMeter(sdk.NewInfiniteGasMeter())
keeper.SetLease(keeper.GetLeaseStore(leaseCtx), msg.UUID, msg.Key, ctx.BlockHeight(), msg.Lease)

return &sdk.Result{}, nil
}

Expand All @@ -80,7 +91,8 @@ func handleMsgRead(ctx sdk.Context, keeper keeper.IKeeper, msg types.MsgRead) (*
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "Key does not exist")
}

json_data, err := json.Marshal(types.QueryResultRead{msg.UUID, msg.Key, keeper.GetValue(ctx, keeper.GetKVStore(ctx), msg.UUID, msg.Key).Value})
blzValue := keeper.GetValue(ctx, keeper.GetKVStore(ctx), msg.UUID, msg.Key)
json_data, err := json.Marshal(types.QueryResultRead{msg.UUID, msg.Key, blzValue.Value, blzValue.Height, blzValue.Lease})
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "could not marshal result to JSON")
}
Expand All @@ -102,8 +114,27 @@ func handleMsgUpdate(ctx sdk.Context, keeper keeper.IKeeper, msg types.MsgUpdate
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "Incorrect Owner")
}

keeper.SetValue(ctx, keeper.GetKVStore(ctx), msg.UUID, msg.Key, types.BLZValue{Value: msg.Value, Owner: msg.Owner})
oldBlzValue := keeper.GetValue(ctx, keeper.GetKVStore(ctx), msg.UUID, msg.Key)

if msg.Lease != 0 { // 0 means no change to lease
newLease := oldBlzValue.Lease + msg.Lease
if newLease <= 0 {
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "Invalid lease")
}

if (oldBlzValue.Height + newLease) <= ctx.BlockHeight() {
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "Invalid lease")
}

keeper.SetValue(ctx, keeper.GetKVStore(ctx), msg.UUID, msg.Key, types.BLZValue{Value: msg.Value, Lease: newLease, Height: oldBlzValue.Height, Owner: msg.Owner})

leaseCtx := ctx.WithGasMeter(sdk.NewInfiniteGasMeter())
keeper.DeleteLease(keeper.GetLeaseStore(leaseCtx), msg.UUID, msg.Key, oldBlzValue.Height, oldBlzValue.Lease)
keeper.SetLease(keeper.GetLeaseStore(leaseCtx), msg.UUID, msg.Key, oldBlzValue.Height, newLease)
} else {
keeper.SetValue(ctx, keeper.GetKVStore(ctx), msg.UUID, msg.Key, types.BLZValue{Value: msg.Value, Lease: oldBlzValue.Lease,
Owner: msg.Owner, Height: oldBlzValue.Height})
}
return &sdk.Result{}, nil
}

Expand All @@ -121,7 +152,8 @@ func handleMsgDelete(ctx sdk.Context, keeper keeper.IKeeper, msg types.MsgDelete
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "Incorrect Owner")
}

keeper.DeleteValue(ctx, keeper.GetKVStore(ctx), msg.UUID, msg.Key)
newCtx := ctx.WithGasMeter(sdk.NewInfiniteGasMeter())
keeper.DeleteValue(ctx, keeper.GetKVStore(ctx), keeper.GetLeaseStore(newCtx), msg.UUID, msg.Key)

return &sdk.Result{}, nil
}
Expand Down
Loading

0 comments on commit d07bddb

Please sign in to comment.