Skip to content

Commit

Permalink
CNS-450: implemented query that returns all the projects of a subscri…
Browse files Browse the repository at this point in the history
…ption + unit test
  • Loading branch information
oren-lava committed May 24, 2023
1 parent a920062 commit e61b925
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 5 deletions.
4 changes: 4 additions & 0 deletions x/projects/keeper/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,7 @@ func (k Keeper) SetProjectPolicy(ctx sdk.Context, projectIDs []string, policy *t

return nil
}

func (k Keeper) GetAllProjectsForSubscription(ctx sdk.Context, subscription string) []string {
return k.projectsFS.GetAllEntryIndicesWithPrefix(ctx, subscription)
}
1 change: 0 additions & 1 deletion x/subscription/client/cli/query_list_projects.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ func CmdListProjects() *cobra.Command {
queryClient := types.NewQueryClient(clientCtx)

params := &types.QueryListProjectsRequest{

Subscription: reqSubscription,
}

Expand Down
14 changes: 11 additions & 3 deletions x/subscription/keeper/grpc_query_list_projects.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"

sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/lavanet/lava/utils"
"github.com/lavanet/lava/x/subscription/types"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
Expand All @@ -16,8 +18,14 @@ func (k Keeper) ListProjects(goCtx context.Context, req *types.QueryListProjects

ctx := sdk.UnwrapSDKContext(goCtx)

// TODO: implement logic
_ = ctx
_, found := k.GetSubscription(ctx, req.Subscription)
if !found {
return nil, utils.LavaFormatWarning("subscription not found", sdkerrors.ErrKeyNotFound,
utils.Attribute{Key: "subscription", Value: req.Subscription},
)
}

projects := k.projectsKeeper.GetAllProjectsForSubscription(ctx, req.Subscription)

return &types.QueryListProjectsResponse{}, nil
return &types.QueryListProjectsResponse{Projects: projects}, nil
}
66 changes: 65 additions & 1 deletion x/subscription/keeper/subscription_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ type testStruct struct {
_ctx context.Context
ctx sdk.Context
keepers *keepertest.Keepers
servers *keepertest.Servers
plans []planstypes.Plan
}

Expand All @@ -118,7 +119,7 @@ func (ts *testStruct) expireSubscription(sub types.Subscription) types.Subscript
}

func setupTestStruct(t *testing.T, numPlans int) testStruct {
_, keepers, _ctx := keepertest.InitAllKeepers(t)
servers, keepers, _ctx := keepertest.InitAllKeepers(t)

_ctx = keepertest.AdvanceEpoch(_ctx, keepers)
ctx := sdk.UnwrapSDKContext(_ctx)
Expand All @@ -130,6 +131,7 @@ func setupTestStruct(t *testing.T, numPlans int) testStruct {
ctx: ctx,
keepers: keepers,
plans: plans,
servers: servers,
}

return ts
Expand Down Expand Up @@ -620,3 +622,65 @@ func TestAddProjectToSubscription(t *testing.T) {
})
}
}

func TestGetProjectsForSubscription(t *testing.T) {
ts := setupTestStruct(t, 1)
plan := ts.plans[0]

subAcc1 := common.CreateNewAccount(ts._ctx, *ts.keepers, 10000).Addr.String()
subAcc2 := common.CreateNewAccount(ts._ctx, *ts.keepers, 10000).Addr.String()

// buy two subscriptions
_, err := ts.servers.SubscriptionServer.Buy(ts._ctx, &types.MsgBuy{
Creator: subAcc1,
Consumer: subAcc1,
Index: plan.Index,
Duration: 1,
})
require.Nil(t, err)

_, err = ts.servers.SubscriptionServer.Buy(ts._ctx, &types.MsgBuy{
Creator: subAcc2,
Consumer: subAcc2,
Index: plan.Index,
Duration: 1,
})
require.Nil(t, err)

// add two projects to the first subscription
projData1 := projectstypes.ProjectData{
Name: "proj1",
Enabled: true,
Policy: &plan.PlanPolicy,
}
_, err = ts.servers.SubscriptionServer.AddProject(ts._ctx, &types.MsgAddProject{
Creator: subAcc1,
ProjectData: projData1,
})
require.Nil(t, err)

projData2 := projectstypes.ProjectData{
Name: "proj2",
Enabled: false,
Policy: &plan.PlanPolicy,
}
_, err = ts.servers.SubscriptionServer.AddProject(ts._ctx, &types.MsgAddProject{
Creator: subAcc1,
ProjectData: projData2,
})
require.Nil(t, err)

res1, err := ts.keepers.Subscription.ListProjects(ts._ctx, &types.QueryListProjectsRequest{
Subscription: subAcc1,
})
require.Nil(t, err)

res2, err := ts.keepers.Subscription.ListProjects(ts._ctx, &types.QueryListProjectsRequest{
Subscription: subAcc2,
})
require.Nil(t, err)

// check the number of projects are as expected (+1 because there's an auto-generated admin project)
require.Equal(t, 3, len(res1.Projects))
require.Equal(t, 1, len(res2.Projects))
}
1 change: 1 addition & 0 deletions x/subscription/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type ProjectsKeeper interface {
CreateProject(ctx sdk.Context, subscriptionAddress string, projectData projectstypes.ProjectData, plan planstypes.Plan) error
DeleteProject(ctx sdk.Context, index string) error
SnapshotSubscriptionProjects(ctx sdk.Context, subscriptionAddr string)
GetAllProjectsForSubscription(ctx sdk.Context, subscription string) []string
// Methods imported from projectskeeper should be defined here
}

Expand Down

0 comments on commit e61b925

Please sign in to comment.