Skip to content

Commit

Permalink
Merge pull request lavanet#563 from lavanet/CNS-476-move-policy-to-pl…
Browse files Browse the repository at this point in the history
…an-proto

CNS-476: move policy to plan proto
  • Loading branch information
Yaroms authored Jun 21, 2023
2 parents c84999b + 71f4cf8 commit 6736400
Show file tree
Hide file tree
Showing 35 changed files with 1,091 additions and 1,080 deletions.
27 changes: 25 additions & 2 deletions proto/plans/plan.proto
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ option go_package = "github.com/lavanet/lava/x/plans/types";
option (gogoproto.equal_all) = true;
import "gogoproto/gogo.proto";
import "cosmos/base/v1beta1/coin.proto";
import "projects/project.proto";

message Plan {
reserved 10;
Expand All @@ -21,5 +20,29 @@ message Plan {
string description = 11; // plan description (for humans)
string type = 12; // plan type
uint64 annual_discount_percentage = 13; // discount for buying the plan for a year
lavanet.lava.projects.Policy plan_policy = 14 [(gogoproto.nullable) = false];
Policy plan_policy = 14 [(gogoproto.nullable) = false];
}

// the enum below determines the pairing algorithm's behaviour with the selected providers feature
enum SELECTED_PROVIDERS_MODE {
ALLOWED = 0; // no providers restrictions
MIXED = 1; // use the selected providers mixed with randomly chosen providers
EXCLUSIVE = 2; // use only the selected providers
DISABLED = 3; // selected providers feature is disabled
}

// protobuf expected in YAML format: used "moretags" to simplify parsing
message Policy {
repeated ChainPolicy chain_policies = 1 [(gogoproto.nullable) = false, (gogoproto.moretags) = "mapstructure:\"chain_policies\""];
uint64 geolocation_profile = 2 [(gogoproto.moretags) = "mapstructure:\"geolocation_profile\"", (gogoproto.jsontag) = "geolocation_profile"];
uint64 total_cu_limit = 3 [(gogoproto.moretags) = "mapstructure:\"total_cu_limit\"", (gogoproto.jsontag) = "total_cu_limit"];
uint64 epoch_cu_limit = 4 [(gogoproto.moretags) = "mapstructure:\"epoch_cu_limit\"", (gogoproto.jsontag) = "epoch_cu_limit"];
uint64 max_providers_to_pair = 5 [(gogoproto.jsontag) = "max_providers_to_pair", (gogoproto.moretags) = "mapstructure:\"max_providers_to_pair\""];
SELECTED_PROVIDERS_MODE selected_providers_mode = 6 [(gogoproto.moretags) = "mapstructure:\"selected_providers_mode\"", (gogoproto.jsontag) = "selected_providers_mode"];
repeated string selected_providers = 7 [(gogoproto.moretags) = "mapstructure:\"selected_providers\"", (gogoproto.jsontag) = "selected_providers"];
}

message ChainPolicy {
string chain_id = 1 [(gogoproto.moretags) = "mapstructure:\"chain_id\""];
repeated string apis = 2 [(gogoproto.nullable) = false, (gogoproto.moretags) = "mapstructure:\"apis\""];
}
31 changes: 4 additions & 27 deletions proto/projects/project.proto
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ syntax = "proto3";
package lavanet.lava.projects;

import "gogoproto/gogo.proto";
import "plans/plan.proto";
option (gogoproto.equal_all) = true;
option go_package = "github.com/lavanet/lava/x/projects/types";

Expand All @@ -11,9 +12,9 @@ message Project {
reserved 3;
bool enabled = 4; // enabled flag
repeated ProjectKey project_keys = 5 [(gogoproto.nullable) = false]; // list of the projects keys
Policy admin_policy = 6;
lavanet.lava.plans.Policy admin_policy = 6;
uint64 used_cu = 7;
Policy subscription_policy = 8;
lavanet.lava.plans.Policy subscription_policy = 8;
uint64 snapshot = 9; // snapshot id to uniquely identify snapshots
}

Expand All @@ -32,30 +33,6 @@ message ProjectKey {
uint32 kinds = 4;
}

// the enum below determines the pairing algorithm's behaviour with the selected providers feature
enum SELECTED_PROVIDERS_MODE {
ALLOWED = 0; // no providers restrictions
MIXED = 1; // use the selected providers mixed with randomly chosen providers
EXCLUSIVE = 2; // use only the selected providers
DISABLED = 3; // selected providers feature is disabled
}

// protobuf expected in YAML format: used "moretags" to simplify parsing
message Policy {
repeated ChainPolicy chain_policies = 1 [(gogoproto.nullable) = false, (gogoproto.moretags) = "mapstructure:\"chain_policies\""];
uint64 geolocation_profile = 2 [(gogoproto.moretags) = "mapstructure:\"geolocation_profile\"", (gogoproto.jsontag) = "geolocation_profile"];
uint64 total_cu_limit = 3 [(gogoproto.moretags) = "mapstructure:\"total_cu_limit\"", (gogoproto.jsontag) = "total_cu_limit"];
uint64 epoch_cu_limit = 4 [(gogoproto.moretags) = "mapstructure:\"epoch_cu_limit\"", (gogoproto.jsontag) = "epoch_cu_limit"];
uint64 max_providers_to_pair = 5 [(gogoproto.jsontag) = "max_providers_to_pair", (gogoproto.moretags) = "mapstructure:\"max_providers_to_pair\""];
SELECTED_PROVIDERS_MODE selected_providers_mode = 6 [(gogoproto.moretags) = "mapstructure:\"selected_providers_mode\"", (gogoproto.jsontag) = "selected_providers_mode"];
repeated string selected_providers = 7 [(gogoproto.moretags) = "mapstructure:\"selected_providers\"", (gogoproto.jsontag) = "selected_providers"];
}

message ChainPolicy {
string chain_id = 1 [(gogoproto.moretags) = "mapstructure:\"chain_id\""];
repeated string apis = 2 [(gogoproto.nullable) = false, (gogoproto.moretags) = "mapstructure:\"apis\""];
}

message ProtoDeveloperData {
string projectID = 1;
reserved 2;
Expand All @@ -67,5 +44,5 @@ message ProjectData {
reserved 2;
bool enabled = 3;
repeated ProjectKey projectKeys = 4 [(gogoproto.nullable) = false];
Policy policy = 5;
lavanet.lava.plans.Policy policy = 5;
}
5 changes: 3 additions & 2 deletions proto/projects/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ syntax = "proto3";
package lavanet.lava.projects;

import "projects/project.proto";
import "plans/plan.proto";
import "gogoproto/gogo.proto";

// this line is used by starport scaffolding # proto/tx/import
Expand Down Expand Up @@ -38,7 +39,7 @@ message MsgDelKeysResponse {
message MsgSetPolicy {
string creator = 1;
string project = 2;
Policy policy = 3 [(gogoproto.nullable) = false];
lavanet.lava.plans.Policy policy = 3 [(gogoproto.nullable) = false];
}

message MsgSetPolicyResponse {
Expand All @@ -47,7 +48,7 @@ message MsgSetPolicyResponse {
message MsgSetSubscriptionPolicy {
string creator = 1;
repeated string projects = 2;
Policy policy = 3 [(gogoproto.nullable) = false];
lavanet.lava.plans.Policy policy = 3 [(gogoproto.nullable) = false];
}

message MsgSetSubscriptionPolicyResponse {
Expand Down
3 changes: 1 addition & 2 deletions testutil/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
epochstoragetypes "github.com/lavanet/lava/x/epochstorage/types"
"github.com/lavanet/lava/x/pairing/types"
plantypes "github.com/lavanet/lava/x/plans/types"
projectstypes "github.com/lavanet/lava/x/projects/types"
spectypes "github.com/lavanet/lava/x/spec/types"
subscriptiontypes "github.com/lavanet/lava/x/subscription/types"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -41,7 +40,7 @@ func CreateMockSpec() spectypes.Spec {
}

func CreateMockPlan() plantypes.Plan {
policy := projectstypes.Policy{
policy := plantypes.Policy{
TotalCuLimit: 100000,
EpochCuLimit: 10000,
MaxProvidersToPair: 3,
Expand Down
8 changes: 4 additions & 4 deletions x/pairing/keeper/filters/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/lavanet/lava/utils"
epochstoragetypes "github.com/lavanet/lava/x/epochstorage/types"
projectstypes "github.com/lavanet/lava/x/projects/types"
planstypes "github.com/lavanet/lava/x/plans/types"
)

type Filter interface {
Filter(ctx sdk.Context, providers []epochstoragetypes.StakeEntry, currentEpoch uint64) []bool
InitFilter(strictestPolicy projectstypes.Policy) bool // return if filter is usable (by the policy)
InitFilter(strictestPolicy planstypes.Policy) bool // return if filter is usable (by the policy)
}

func GetAllFilters() []Filter {
Expand All @@ -23,7 +23,7 @@ func GetAllFilters() []Filter {
return filters
}

func initFilters(filters []Filter, strictestPolicy projectstypes.Policy) []Filter {
func initFilters(filters []Filter, strictestPolicy planstypes.Policy) []Filter {
activeFilters := []Filter{}

for _, filter := range filters {
Expand All @@ -36,7 +36,7 @@ func initFilters(filters []Filter, strictestPolicy projectstypes.Policy) []Filte
return activeFilters
}

func FilterProviders(ctx sdk.Context, filters []Filter, providers []epochstoragetypes.StakeEntry, strictestPolicy projectstypes.Policy, currentEpoch uint64) ([]epochstoragetypes.StakeEntry, error) {
func FilterProviders(ctx sdk.Context, filters []Filter, providers []epochstoragetypes.StakeEntry, strictestPolicy planstypes.Policy, currentEpoch uint64) ([]epochstoragetypes.StakeEntry, error) {
filters = initFilters(filters, strictestPolicy)

var filtersResult [][]bool
Expand Down
4 changes: 2 additions & 2 deletions x/pairing/keeper/filters/frozen_providers_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ package filters
import (
sdk "github.com/cosmos/cosmos-sdk/types"
epochstoragetypes "github.com/lavanet/lava/x/epochstorage/types"
projectstypes "github.com/lavanet/lava/x/projects/types"
planstypes "github.com/lavanet/lava/x/plans/types"
)

type FrozenProvidersFilter struct{}

func (f *FrozenProvidersFilter) InitFilter(strictestPolicy projectstypes.Policy) bool {
func (f *FrozenProvidersFilter) InitFilter(strictestPolicy planstypes.Policy) bool {
// frozen providers (or providers that their stake is not applied yet) can't be part of the pairing - this filter is always active
return true
}
Expand Down
8 changes: 4 additions & 4 deletions x/pairing/keeper/filters/geolocation_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package filters
import (
sdk "github.com/cosmos/cosmos-sdk/types"
epochstoragetypes "github.com/lavanet/lava/x/epochstorage/types"
projectstypes "github.com/lavanet/lava/x/projects/types"
planstypes "github.com/lavanet/lava/x/plans/types"
)

// TODO: This is a temp filter until the geolocation mechanism changes (this is not optimal)
Expand All @@ -12,9 +12,9 @@ type GeolocationFilter struct {
geolocation uint64
}

func (f *GeolocationFilter) InitFilter(strictestPolicy projectstypes.Policy) bool {
if strictestPolicy.SelectedProvidersMode == projectstypes.SELECTED_PROVIDERS_MODE_DISABLED ||
strictestPolicy.SelectedProvidersMode == projectstypes.SELECTED_PROVIDERS_MODE_ALLOWED {
func (f *GeolocationFilter) InitFilter(strictestPolicy planstypes.Policy) bool {
if strictestPolicy.SelectedProvidersMode == planstypes.SELECTED_PROVIDERS_MODE_DISABLED ||
strictestPolicy.SelectedProvidersMode == planstypes.SELECTED_PROVIDERS_MODE_ALLOWED {
f.geolocation = strictestPolicy.GeolocationProfile
return true
}
Expand Down
6 changes: 3 additions & 3 deletions x/pairing/keeper/filters/selected_providers_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ package filters
import (
sdk "github.com/cosmos/cosmos-sdk/types"
epochstoragetypes "github.com/lavanet/lava/x/epochstorage/types"
projectstypes "github.com/lavanet/lava/x/projects/types"
planstypes "github.com/lavanet/lava/x/plans/types"
)

type SelectedProvidersFilter struct {
selectedProviders []string
}

func (f *SelectedProvidersFilter) InitFilter(strictestPolicy projectstypes.Policy) bool {
func (f *SelectedProvidersFilter) InitFilter(strictestPolicy planstypes.Policy) bool {
switch strictestPolicy.SelectedProvidersMode {
case projectstypes.SELECTED_PROVIDERS_MODE_EXCLUSIVE, projectstypes.SELECTED_PROVIDERS_MODE_MIXED:
case planstypes.SELECTED_PROVIDERS_MODE_EXCLUSIVE, planstypes.SELECTED_PROVIDERS_MODE_MIXED:
f.selectedProviders = strictestPolicy.SelectedProviders
return true
}
Expand Down
6 changes: 3 additions & 3 deletions x/pairing/keeper/grpc_query_user_entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
epochstoragetypes "github.com/lavanet/lava/x/epochstorage/types"
"github.com/lavanet/lava/x/pairing/types"
projectstypes "github.com/lavanet/lava/x/projects/types"
planstypes "github.com/lavanet/lava/x/plans/types"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
Expand Down Expand Up @@ -41,7 +41,7 @@ func (k Keeper) UserEntry(goCtx context.Context, req *types.QueryUserEntryReques
}

planPolicy := plan.GetPlanPolicy()
policies := []*projectstypes.Policy{&planPolicy, project.AdminPolicy, project.SubscriptionPolicy}
policies := []*planstypes.Policy{&planPolicy, project.AdminPolicy, project.SubscriptionPolicy}
// geolocation is a bitmap. common denominator can be calculated with logical AND
geolocation := k.CalculateEffectiveGeolocationFromPolicies(policies)

Expand All @@ -51,7 +51,7 @@ func (k Keeper) UserEntry(goCtx context.Context, req *types.QueryUserEntryReques
}
allowedCU := k.CalculateEffectiveAllowedCuPerEpochFromPolicies(policies, project.GetUsedCu(), sub.GetMonthCuLeft())

if !projectstypes.VerifyTotalCuUsage(policies, project.GetUsedCu()) {
if !planstypes.VerifyTotalCuUsage(policies, project.GetUsedCu()) {
allowedCU = 0
}

Expand Down
6 changes: 3 additions & 3 deletions x/pairing/keeper/limitConsumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/lavanet/lava/utils"
projectstypes "github.com/lavanet/lava/x/projects/types"
planstypes "github.com/lavanet/lava/x/plans/types"
)

func (k Keeper) EnforceClientCUsUsageInEpoch(ctx sdk.Context, allowedCU uint64, totalCUInEpochForUserProvider uint64, clientAddr sdk.AccAddress, chainID string, epoch uint64) error {
Expand All @@ -19,8 +19,8 @@ func (k Keeper) EnforceClientCUsUsageInEpoch(ctx sdk.Context, allowedCU uint64,
}

planPolicy := plan.GetPlanPolicy()
policies := []*projectstypes.Policy{&planPolicy, project.AdminPolicy, project.SubscriptionPolicy}
if !projectstypes.VerifyTotalCuUsage(policies, project.GetUsedCu()) {
policies := []*planstypes.Policy{&planPolicy, project.AdminPolicy, project.SubscriptionPolicy}
if !planstypes.VerifyTotalCuUsage(policies, project.GetUsedCu()) {
return utils.LavaFormatError("total cu in epoch for consumer exceeded the allowed amount for the project", fmt.Errorf("consumer CU limit exceeded for project"), []utils.Attribute{{Key: "projectUsedCu", Value: project.GetUsedCu()}}...)
}

Expand Down
6 changes: 3 additions & 3 deletions x/pairing/keeper/msg_server_relay_payment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/lavanet/lava/utils/sigs"
epochstoragetypes "github.com/lavanet/lava/x/epochstorage/types"
"github.com/lavanet/lava/x/pairing/types"
plantypes "github.com/lavanet/lava/x/plans/types"
planstypes "github.com/lavanet/lava/x/plans/types"
projecttypes "github.com/lavanet/lava/x/projects/types"
spectypes "github.com/lavanet/lava/x/spec/types"
subscriptionypes "github.com/lavanet/lava/x/subscription/types"
Expand All @@ -30,7 +30,7 @@ type testStruct struct {
providers []*common.Account
clients []*common.Account
spec spectypes.Spec
plan plantypes.Plan
plan planstypes.Plan
}

func createStubRequest(relaySession *types.RelaySession) *types.RelayRequest {
Expand Down Expand Up @@ -726,7 +726,7 @@ func TestCuUsageInProjectsAndSubscription(t *testing.T) {
ProjectKeys: []projecttypes.ProjectKey{
projecttypes.ProjectDeveloperKey(projectAdmin1.String()),
},
Policy: &projecttypes.Policy{
Policy: &planstypes.Policy{
GeolocationProfile: uint64(1),
MaxProvidersToPair: 3,
TotalCuLimit: 1000,
Expand Down
Loading

0 comments on commit 6736400

Please sign in to comment.