Skip to content

Commit

Permalink
Merge 7592942 into 4fed597
Browse files Browse the repository at this point in the history
  • Loading branch information
rnistuk committed Mar 5, 2020
2 parents 4fed597 + 7592942 commit 394893e
Show file tree
Hide file tree
Showing 5 changed files with 181 additions and 13 deletions.
2 changes: 2 additions & 0 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import (
)

const crudModuleEntry = "bluzelle_crud"
const maxKeysSize = uint64(102400)

var (
// default home directories for the application CLI
Expand Down Expand Up @@ -239,6 +240,7 @@ func NewCRUDApp(
app.bankKeeper,
keys[crud.StoreKey],
app.cdc,
crud.MaxKeeperSizes{MaxKeysSize: maxKeysSize},
)

app.mm = module.NewManager(
Expand Down
1 change: 1 addition & 0 deletions x/crud/alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ var (

type (
Keeper = keeper.Keeper
MaxKeeperSizes = keeper.MaxKeeperSizes
MsgBLZCreate = types.MsgBLZCreate
MsgBLZRead = types.MsgBLZRead
MsgBLZUpdate = types.MsgBLZUpdate
Expand Down
24 changes: 21 additions & 3 deletions x/crud/internal/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// along with this program. If not, see <http://www.gnu.org/licenses/>.

//go:generate mockgen -destination=../../mocks/mock_keeper.go -package=mocks github.com/bluzelle/curium/x/crud/internal/keeper IKeeper
//go:generate mockgen -destination=../../mocks/mock_gas.go -package=mocks github.com/cosmos/cosmos-sdk/store/types GasMeter
package keeper

import (
Expand All @@ -22,6 +23,10 @@ import (
"github.com/cosmos/cosmos-sdk/x/bank"
)

type MaxKeeperSizes struct {
MaxKeysSize uint64
}

type IKeeper interface {
SetBLZValue(ctx sdk.Context, store sdk.KVStore, UUID string, key string, value types.BLZValue)
GetBLZValue(ctx sdk.Context, store sdk.KVStore, UUID string, key string) types.BLZValue
Expand All @@ -38,18 +43,20 @@ type Keeper struct {
CoinKeeper bank.Keeper
storeKey sdk.StoreKey
cdc *codec.Codec
mks MaxKeeperSizes
}

// Note: MakeMetaKey is used in query.go and keeper.go
func MakeMetaKey(UUID string, key string) string {
return UUID + "\x00" + key
}

func NewKeeper(coinKeeper bank.Keeper, storeKey sdk.StoreKey, cdc *codec.Codec) Keeper {
func NewKeeper(coinKeeper bank.Keeper, storeKey sdk.StoreKey, cdc *codec.Codec, mks MaxKeeperSizes) Keeper {
return Keeper{
CoinKeeper: coinKeeper,
storeKey: storeKey,
cdc: cdc,
mks: mks,
}
}

Expand Down Expand Up @@ -96,10 +103,21 @@ func (k Keeper) GetKeys(ctx sdk.Context, store sdk.KVStore, UUID string) types.Q
prefix := UUID + "\x00"
iterator := sdk.KVStorePrefixIterator(store, []byte(prefix))
keys := types.QueryResultKeys{UUID: UUID, Keys: make([]string, 0)}

defer iterator.Close()

keysSize := uint64(0)
for ; iterator.Valid(); iterator.Next() {
keys.Keys = append(keys.Keys, string(iterator.Key())[len(prefix):])
key := string(iterator.Key())[len(prefix):]
keysSize = uint64(len(key)) + keysSize
if ctx.GasMeter().IsPastLimit() {
return types.QueryResultKeys{UUID: UUID, Keys: make([]string, 0)}
}

if keysSize < k.mks.MaxKeysSize {
keys.Keys = append(keys.Keys, key)
} else {
return keys
}
}
return keys
}
Expand Down
52 changes: 42 additions & 10 deletions x/crud/internal/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,22 @@ package keeper

import (
"github.com/bluzelle/curium/x/crud/internal/types"
"github.com/bluzelle/curium/x/crud/mocks"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/store/cachekv"
"github.com/cosmos/cosmos-sdk/store/dbadapter"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
abci "github.com/tendermint/tendermint/abci/types"
dbm "github.com/tendermint/tm-db"
"reflect"
"testing"
)

func initKeeperTest(t *testing.T) (sdk.Context, sdk.KVStore, []byte, *codec.Codec) {
return sdk.Context{},

return sdk.NewContext(nil, abci.Header{}, false, nil),
cachekv.NewStore(dbadapter.Store{dbm.NewMemDB()}),
[]byte("bluzelle1t0ywtmrduldf6h4wqrnnpyp9wr6law2u5jwa23"),
codec.New()
Expand All @@ -44,7 +48,7 @@ func Test_makeMetaKey(t *testing.T) {
func TestKeeper_SetBLZValue(t *testing.T) {
ctx, testStore, owner, cdc := initKeeperTest(t)

keeper := NewKeeper(nil, nil, cdc)
keeper := NewKeeper(nil, nil, cdc, MaxKeeperSizes{})

acceptedValue := types.BLZValue{
Value: "value",
Expand All @@ -63,7 +67,7 @@ func TestKeeper_SetBLZValue(t *testing.T) {

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

// test value not found
result := keeper.GetBLZValue(ctx, testStore, "uuid", "key")
Expand All @@ -84,7 +88,7 @@ func TestKeeper_GetBLZValue(t *testing.T) {

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

keeper.SetBLZValue(ctx, testStore, "uuid", "key", types.BLZValue{
Value: "value",
Expand All @@ -99,7 +103,7 @@ func TestKeeper_DeleteBLZValue(t *testing.T) {

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

assert.False(t, keeper.IsKeyPresent(ctx, testStore, "uuid", "key"))

Expand All @@ -113,7 +117,7 @@ func TestKeeper_IsKeyPresent(t *testing.T) {

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

result := keeper.GetValuesIterator(ctx, testStore)

Expand All @@ -135,7 +139,7 @@ func TestKeeper_GetValuesIterator(t *testing.T) {

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

keys := keeper.GetKeys(ctx, testStore, "uuid")

Expand Down Expand Up @@ -164,9 +168,37 @@ func TestKeeper_GetKeys(t *testing.T) {
assert.Len(t, keys.Keys, 1)
}

func TestKeeper_GetKeys_MaxSize(t *testing.T) {
ctx, testStore, owner, cdc := initKeeperTest(t)

// test max keys size
{
keeper := NewKeeper(nil, nil, cdc, MaxKeeperSizes{MaxKeysSize: 9})
keeper.SetBLZValue(ctx, testStore, "uuid", "key0", types.BLZValue{Value: "value", Owner: owner})
keeper.SetBLZValue(ctx, testStore, "uuid", "key1", types.BLZValue{Value: "value", Owner: owner})
keeper.SetBLZValue(ctx, testStore, "uuid", "key2", types.BLZValue{Value: "value", Owner: owner})
keeper.SetBLZValue(ctx, testStore, "uuid", "key3", types.BLZValue{Value: "value", Owner: owner})

keys := keeper.GetKeys(ctx, testStore, "uuid")

assert.Len(t, keys.Keys, 2)
}

// test out of gas
{
mockCtrl := gomock.NewController(t)
mockGasMeter := mocks.NewMockGasMeter(mockCtrl)
keeper := NewKeeper(nil, nil, cdc, MaxKeeperSizes{MaxKeysSize: 1024})
mockGasMeter.EXPECT().IsPastLimit().Return(true)
keys := keeper.GetKeys(ctx.WithGasMeter(mockGasMeter), testStore, "uuid")

assert.Len(t, keys.Keys, 0)
}
}

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

keeper.SetBLZValue(ctx, testStore, "uuid", "key0", types.BLZValue{Value: "value", Owner: owner})

Expand All @@ -179,7 +211,7 @@ func TestKeeper_GetOwner(t *testing.T) {
func TestKeeper_RenameBLZKey(t *testing.T) {
ctx, testStore, owner, cdc := initKeeperTest(t)

keeper := NewKeeper(nil, nil, cdc)
keeper := NewKeeper(nil, nil, cdc, MaxKeeperSizes{})

keeper.SetBLZValue(ctx, testStore, "uuid", "key", types.BLZValue{
Value: "a value",
Expand All @@ -190,7 +222,7 @@ func TestKeeper_RenameBLZKey(t *testing.T) {

assert.True(t, keeper.RenameBLZKey(ctx, testStore, "uuid", "key", "newkey"))

assert.True(t, reflect.DeepEqual(keeper.GetBLZValue(ctx,testStore, "uuid", "newkey"), types.BLZValue{
assert.True(t, reflect.DeepEqual(keeper.GetBLZValue(ctx, testStore, "uuid", "newkey"), types.BLZValue{
Value: "a value",
Owner: owner,
}))
Expand Down
115 changes: 115 additions & 0 deletions x/crud/mocks/mock_gas.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 394893e

Please sign in to comment.