Skip to content

Commit

Permalink
Now deletes owner index when keys expire
Browse files Browse the repository at this point in the history
  • Loading branch information
scottburch committed Nov 8, 2020
1 parent 2d74105 commit 1d88a8f
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 6 deletions.
2 changes: 1 addition & 1 deletion app.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ func (app *CRUDApp) BeginBlocker(ctx sdk.Context, req abci.RequestBeginBlock) ab

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

Expand Down
3 changes: 3 additions & 0 deletions setup/dev-setup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
rm -rf ~/.blz*

blzd init dev --chain-id bluzelle
13 changes: 13 additions & 0 deletions x/crud/client/rest/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,19 @@ func BlzQCountHandler(cliCtx context.CLIContext, storeName string) http.HandlerF
}
}

func BlzQMyKeysHandler(cliCtx context.CLIContext, storeName string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)

res, _, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/mykeys/%s/%s", storeName, vars["owner"], vars["UUID"]), nil)
if err != nil {
rest.WriteErrorResponse(w, http.StatusNotFound, err.Error())
return
}
rest.PostProcessResponse(w, cliCtx, res)
}
}

func BlzQGetLeaseHandler(cliCtx context.CLIContext, storeName string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
Expand Down
1 change: 1 addition & 0 deletions x/crud/client/rest/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,5 @@ func RegisterRoutes(cliCtx context.CLIContext, r *mux.Router, storeName string)
r.HandleFunc(fmt.Sprintf("/%s/upsert", storeName), BlzUpsertHandler(cliCtx)).Methods("POST")
r.HandleFunc(fmt.Sprintf("/%s/renewlease", storeName), BlzRenewLease(cliCtx)).Methods("POST")
r.HandleFunc(fmt.Sprintf("/%s/renewleaseall", storeName), BlzRenewLeaseAll(cliCtx)).Methods("POST")
r.HandleFunc(fmt.Sprintf("/%s/mykeys/{owner}/{UUID}", storeName), BlzQMyKeysHandler(cliCtx, storeName)).Methods("GET")
}
2 changes: 1 addition & 1 deletion x/crud/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,8 @@ func handleMsgDelete(ctx sdk.Context, keeper keeper.IKeeper, msg types.MsgDelete
}

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

return &sdk.Result{}, nil
}
Expand Down
24 changes: 20 additions & 4 deletions x/crud/internal/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@
package keeper

import (
"bytes"
"fmt"
"github.com/bluzelle/curium/x/crud/internal/types"
"github.com/cosmos/cosmos-sdk/codec"
cosmosTypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/bank"
"sort"
"strconv"
cosmosTypes "github.com/cosmos/cosmos-sdk/store/types"
)

type MaxKeeperSizes struct {
Expand Down Expand Up @@ -53,7 +54,7 @@ type IKeeper interface {
GetValue(ctx sdk.Context, store sdk.KVStore, UUID string, key string) types.BLZValue
GetValuesIterator(ctx sdk.Context, store sdk.KVStore) sdk.Iterator
IsKeyPresent(ctx sdk.Context, store sdk.KVStore, UUID string, key string) bool
ProcessLeasesAtBlockHeight(ctx sdk.Context, store sdk.KVStore, leaseStore sdk.KVStore, lease int64)
ProcessLeasesAtBlockHeight(ctx sdk.Context, store sdk.KVStore, leaseStore sdk.KVStore, ownerStore sdk.KVStore, lease int64)
RenameKey(ctx sdk.Context, store sdk.KVStore, UUID string, key string, newkey string) bool
SetLease(leaseStore sdk.KVStore, UUID string, key string, blockHeight int64, lease int64)
SetValue(ctx sdk.Context, store sdk.KVStore, UUID string, key string, value types.BLZValue)
Expand Down Expand Up @@ -346,12 +347,17 @@ func (k Keeper) SetOwner(store sdk.KVStore, ownerStore sdk.KVStore, UUID string,
}

func (k Keeper) DeleteOwner(store sdk.KVStore, ownerStore sdk.KVStore, UUID string, key string) {

metaKey := MakeMetaKey(UUID, key)
var bz = store.Get([]byte(metaKey))
var value types.BLZValue
k.cdc.MustUnmarshalBinaryBare(bz, &value)

ownerStore.Delete([]byte(MakeOwnerKey(value.Owner, UUID, key)))
ownerKey := MakeOwnerKey(value.Owner, UUID, key)
oldKey := ownerStore.Get([]byte(ownerKey))
fmt.Println(oldKey)

ownerStore.Delete([]byte(ownerKey))
}

func (k Keeper) SetLease(leaseStore sdk.KVStore, UUID string, key string, blockHeight int64, leaseBlocks int64) {
Expand All @@ -366,12 +372,22 @@ func (k Keeper) DeleteLease(leaseStore sdk.KVStore, UUID string, key string, blo
leaseStore.Delete([]byte(MakeLeaseKey(blockHeight+leaseBlocks, UUID, key)))
}

func (k Keeper) ProcessLeasesAtBlockHeight(_ sdk.Context, store sdk.KVStore, leaseStore sdk.KVStore, lease int64) {
func (k Keeper) ProcessLeasesAtBlockHeight(ctx sdk.Context, store sdk.KVStore, leaseStore sdk.KVStore, ownerStore sdk.KVStore, lease int64) {
prefix := strconv.FormatInt(lease, 10) + "\x00"
iterator := sdk.KVStorePrefixIterator(leaseStore, []byte(prefix))
defer iterator.Close()

for ; iterator.Valid(); iterator.Next() {

// Delete from owner index
parts := bytes.Split(iterator.Key()[len(prefix):], []byte("\x00"))
uuid := string(parts[0])
key := string(parts[1])

k.DeleteOwner(store, ownerStore, uuid, key)


// Delete lease
fmt.Printf("\n\tdeleting %s, %s\n", prefix, string(iterator.Key()))
store.Delete(iterator.Key()[len(prefix):])
leaseStore.Delete(iterator.Key())
Expand Down

0 comments on commit 1d88a8f

Please sign in to comment.