Skip to content

Commit

Permalink
CM-286 getnshortestlease
Browse files Browse the repository at this point in the history
  • Loading branch information
rnistuk committed Mar 27, 2020
1 parent 3f0c71a commit 3467c58
Show file tree
Hide file tree
Showing 9 changed files with 191 additions and 1 deletion.
8 changes: 8 additions & 0 deletions x/crud/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,3 +291,11 @@ func handleMsgGetLease(ctx sdk.Context, keeper keeper.IKeeper, msg types.MsgGetL

return &sdk.Result{Data: json_data}, nil
}

/*
func handleMsgGetNShortestLease(ctx sdk.Context, keeper keeper.IKeeper, UUID string, owner sdk.AccAddress, n uint64) (*sdk.Result, error) {
}*/
19 changes: 19 additions & 0 deletions x/crud/internal/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/bank"
"sort"
"strconv"
)

Expand Down Expand Up @@ -281,3 +282,21 @@ func (k Keeper) ProcessLeasesAtBlockHeight(ctx sdk.Context, store sdk.KVStore, l
leaseStore.Delete(iterator.Key())
}
}

func (k Keeper) GetNShortestLease(ctx sdk.Context, store sdk.KVStore, UUID string, owner sdk.AccAddress, n uint64) types.QueryResultNShortestLeaseKeys {
keys := k.GetKeys(ctx, store, UUID, owner)

if len(keys.Keys) == 0 {
return types.QueryResultNShortestLeaseKeys{UUID: UUID, KeyLeases: make([]types.KeyLease, 0)}
}

var keyLeases = []types.KeyLease{}
for i := range keys.Keys {
value := k.GetValue(ctx, store, UUID, keys.Keys[i])
keyLeases = append(keyLeases, types.KeyLease{Key: keys.Keys[i], Lease: value.Lease + value.Height - ctx.BlockHeight()})
}

sort.Sort(types.KeyLeases(keyLeases))

return types.QueryResultNShortestLeaseKeys{UUID: UUID, KeyLeases: keyLeases[:n]}
}
34 changes: 33 additions & 1 deletion x/crud/internal/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package keeper

import (
"fmt"
"github.com/bluzelle/curium/x/crud/internal/types"
"github.com/bluzelle/curium/x/crud/mocks"
"github.com/cosmos/cosmos-sdk/codec"
Expand Down Expand Up @@ -161,7 +162,6 @@ func TestKeeper_GetValuesIterator(t *testing.T) {
}

func TestKeeper_GetKeys(t *testing.T) {
// TODO: ensure that we only get keys associated with the owner
ctx, testStore, owner, cdc := initKeeperTest(t)
keeper := NewKeeper(nil, nil, nil, cdc, MaxKeeperSizes{MaxKeysSize: 1024})

Expand Down Expand Up @@ -490,3 +490,35 @@ func TestKeeper_GetCdc(t *testing.T) {

assert.Equal(t, cdc, keeper.GetCdc())
}

func TestKeeper_GetNShortestLease(t *testing.T) {
ctx, testStore, owner, cdc := initKeeperTest(t)
keeper := NewKeeper(nil, nil, nil, cdc, MaxKeeperSizes{MaxKeysSize: 1024})

currentBlockHeight := int64(1)

for i := 0; i < 10; i++ {
l := int64(10000 - 10*i)
value := types.BLZValue{
Value: "value",
Lease: l,
Height: 1000,
Owner: owner,
}
testStore.Set([]byte(MakeMetaKey("uuid", fmt.Sprintf("key%d", l))), cdc.MustMarshalBinaryBare(value))
}

// there are at least 10 keys
newCtx := ctx.WithBlockHeight(currentBlockHeight)
response := keeper.GetNShortestLease(newCtx, testStore, "uuid", owner, 5)

assert.Equal(t, "uuid", response.UUID)
assert.Equal(t, 5, len(response.KeyLeases))

assert.Equal(t, "key9910", response.KeyLeases[0].Key)
assert.Equal(t, int64(9910+1000-currentBlockHeight), response.KeyLeases[0].Lease)

response = keeper.GetNShortestLease(newCtx, testStore, "wronguuid", owner, 5)
assert.Equal(t, "wronguuid", response.UUID)
assert.Equal(t, 0, len(response.KeyLeases))
}
1 change: 1 addition & 0 deletions x/crud/internal/types/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,5 @@ func RegisterCodec(cdc *codec.Codec) {
cdc.RegisterConcrete(MsgDeleteAll{}, "crud/deleteall", nil)
cdc.RegisterConcrete(MsgMultiUpdate{}, "crud/multiupdate", nil)
cdc.RegisterConcrete(MsgGetLease{}, "crud/getlease", nil)
cdc.RegisterConcrete(MsgGetNShortestLease{}, "crud/getnshortestlease", nil)
}
36 changes: 36 additions & 0 deletions x/crud/internal/types/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -500,3 +500,39 @@ func (msg MsgGetLease) GetSignBytes() []byte {
func (msg MsgGetLease) GetSigners() []sdk.AccAddress {
return []sdk.AccAddress{msg.Owner}
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// GetLease
type MsgGetNShortestLease struct {
UUID string
N uint64
Owner sdk.AccAddress
}

func (msg MsgGetNShortestLease) Route() string { return RouterKey }

func (msg MsgGetNShortestLease) Type() string { return "getnshortestlease" }

func (msg MsgGetNShortestLease) ValidateBasic() error {
if msg.Owner.Empty() {
return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, msg.Owner.String())
}

if len(msg.UUID) == 0 {
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "UUID empty")
}

if msg.N == 0 {
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "N must be larger than 0")
}

return nil
}

func (msg MsgGetNShortestLease) GetSignBytes() []byte {
return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(msg))
}

func (msg MsgGetNShortestLease) GetSigners() []sdk.AccAddress {
return []sdk.AccAddress{msg.Owner}
}
49 changes: 49 additions & 0 deletions x/crud/internal/types/msgs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -612,3 +612,52 @@ func TestMsgGetLease_GetSigners(t *testing.T) {
Equal(t, sut.GetSigners(), []sdk.AccAddress{sut.Owner})

}

/////////////////////////////////////////////////////////////////////////////////
func TestMsgGetNShortestLease_Route(t *testing.T) {
Equal(t, "crud", MsgGetNShortestLease{}.Route())
}

func TestMsgGetNShortestLease_Type(t *testing.T) {
Equal(t, "getnshortestlease", MsgGetNShortestLease{}.Type())
}

func TestMsgGetNShortestLease_ValidateBasic(t *testing.T) {
sut := MsgGetNShortestLease{
UUID: "uuid",
N: 10,
Owner: []byte("bluzelle1t0ywtmrduldf6h4wqrnnpyp9wr6law2u5jwa23"),
}

Nil(t, sut.ValidateBasic())

sut.UUID = ""
Equal(t, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "UUID empty").Error(), sut.ValidateBasic().Error())

sut.UUID = "uuid"
sut.N = 0
Equal(t, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "N must be larger than 0").Error(), sut.ValidateBasic().Error())

sut.Owner = nil
sut.N = 596740
Equal(t, sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, "").Error(), sut.ValidateBasic().Error())

}

func TestMsgGetNShortestLease_GetSignBytes(t *testing.T) {
sut := MsgGetNShortestLease{
UUID: "uuid",
N: 10,
Owner: []byte("bluzelle1t0ywtmrduldf6h4wqrnnpyp9wr6law2u5jwa23"),
}
Equal(t, "{\"type\":\"crud/getnshortestlease\",\"value\":{\"N\":\"10\",\"Owner\":\"cosmos1vfk827n9d3kx2vt5xpuhwardwfj82mryvcmxsdrhw9exumns09crjamjxekxzaejw56k5ampxgeslhg4h3\",\"UUID\":\"uuid\"}}", string(sut.GetSignBytes()))
}

func TestMsgGetNShortestLease_GetSigners(t *testing.T) {
sut := MsgGetNShortestLease{
UUID: "uuid",
N: 10,
Owner: []byte("bluzelle1t0ywtmrduldf6h4wqrnnpyp9wr6law2u5jwa23"),
}
Equal(t, sut.GetSigners(), []sdk.AccAddress{sut.Owner})
}
5 changes: 5 additions & 0 deletions x/crud/internal/types/querier.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,8 @@ type QueryResultLease struct {
Key string `json:"key"`
Lease int64 `json:"lease,string"`
}

type QueryResultNShortestLeaseKeys struct {
UUID string `json:"uuid"`
KeyLeases []KeyLease `json:"keyleases"`
}
11 changes: 11 additions & 0 deletions x/crud/internal/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,14 @@ type KeyValue struct {
Key string `json:"key"`
Value string `json:"value"`
}

type KeyLease struct {
Key string `json:"key"`
Lease int64 `json:"lease,string"`
}

type KeyLeases []KeyLease

func (a KeyLeases) Len() int { return len(a) }
func (a KeyLeases) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a KeyLeases) Less(i, j int) bool { return a[i].Lease < a[j].Lease }
29 changes: 29 additions & 0 deletions x/crud/internal/types/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
cc "github.com/cosmos/cosmos-sdk/codec"
"github.com/stretchr/testify/assert"
"reflect"
"sort"
"testing"
)

Expand Down Expand Up @@ -47,3 +48,31 @@ func TestBLZValue_String(t *testing.T) {
}
assert.Equal(t, value.String(), "Value: value Owner: <empty>")
}

func TestKeyLeases_Sort(t *testing.T) {
keyLeases := KeyLeases{}
keyLeases = append(keyLeases, KeyLease{
Key: "three",
Lease: 3,
})

keyLeases = append(keyLeases, KeyLease{
Key: "one",
Lease: 1,
})

keyLeases = append(keyLeases, KeyLease{
Key: "zero",
Lease: 0,
})

keyLeases = append(keyLeases, KeyLease{
Key: "two",
Lease: 2,
})

sort.Sort(keyLeases)

assert.Equal(t, int64(0), keyLeases[0].Lease)
assert.Equal(t, int64(3), keyLeases[len(keyLeases)-1].Lease)
}

0 comments on commit 3467c58

Please sign in to comment.