Skip to content

Commit

Permalink
CNS-213: made the selected mode be written explictly in the policy file
Browse files Browse the repository at this point in the history
  • Loading branch information
oren-lava committed Jun 11, 2023
1 parent a3597b8 commit 33a8a61
Show file tree
Hide file tree
Showing 11 changed files with 157 additions and 131 deletions.
2 changes: 1 addition & 1 deletion cookbook/plans/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"total_cu_limit": "1000000",
"epoch_cu_limit": "100000",
"max_providers_to_pair": "3",
"selected_providers_mode": 0,
"selected_providers_mode": "ALLOWED",
"selected_providers": []
}
}
Expand Down
2 changes: 1 addition & 1 deletion cookbook/projects/example_policy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Policy:
total_cu_limit: 1000
epoch_cu_limit: 100
max_providers_to_pair: 2
selected_providers_mode: 1
selected_providers_mode: EXCLUSIVE
selected_providers:
- lava@1kgd936x3tlz2er9untunk7texfanmaud8yp9kf
- lava@18puklmhr7u2f9g524tttm24ttf4ud842wtfcna
Expand Down
18 changes: 9 additions & 9 deletions proto/projects/project.proto
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,22 @@ 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 {
// the enum below determines the pairing algorithm's behaviour with the selected providers feature
enum selected_providers_mode_enum {
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
}

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_enum selected_providers_mode = 6 [(gogoproto.moretags) = "mapstructure:\"selected_providers_mode\"", (gogoproto.jsontag) = "selected_providers_mode"];
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"];
}

Expand Down
4 changes: 2 additions & 2 deletions x/pairing/keeper/filters/geolocation_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ type GeolocationFilter struct {
}

func (f *GeolocationFilter) InitFilter(strictestPolicy projectstypes.Policy) bool {
if strictestPolicy.SelectedProvidersMode == projectstypes.Policy_DISABLED ||
strictestPolicy.SelectedProvidersMode == projectstypes.Policy_ALLOWED {
if strictestPolicy.SelectedProvidersMode == projectstypes.SELECTED_PROVIDERS_MODE_DISABLED ||
strictestPolicy.SelectedProvidersMode == projectstypes.SELECTED_PROVIDERS_MODE_ALLOWED {
f.geolocation = strictestPolicy.GeolocationProfile
return true
}
Expand Down
2 changes: 1 addition & 1 deletion x/pairing/keeper/filters/selected_providers_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type SelectedProvidersFilter struct {

func (f *SelectedProvidersFilter) InitFilter(strictestPolicy projectstypes.Policy) bool {
switch strictestPolicy.SelectedProvidersMode {
case projectstypes.Policy_EXCLUSIVE, projectstypes.Policy_MIXED:
case projectstypes.SELECTED_PROVIDERS_MODE_EXCLUSIVE, projectstypes.SELECTED_PROVIDERS_MODE_MIXED:
f.selectedProviders = strictestPolicy.SelectedProviders
return true
}
Expand Down
2 changes: 1 addition & 1 deletion x/pairing/keeper/grpc_query_static_providers_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func (k Keeper) StaticProvidersList(goCtx context.Context, req *types.QueryStati
var geolocationFilter pairingfilters.GeolocationFilter
policy := projectstypes.Policy{
GeolocationProfile: uint64(1),
SelectedProvidersMode: projectstypes.Policy_DISABLED,
SelectedProvidersMode: projectstypes.SELECTED_PROVIDERS_MODE_DISABLED,
}
geoFilterActive := geolocationFilter.InitFilter(policy)
if !geoFilterActive {
Expand Down
6 changes: 3 additions & 3 deletions x/pairing/keeper/pairing.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,12 +207,12 @@ func (k Keeper) getProjectStrictestPolicy(ctx sdk.Context, project projectstypes
return strictestPolicy, allowedCU, nil
}

func (k Keeper) CalculateEffectiveSelectedProviders(policies []*projectstypes.Policy) (projectstypes.PolicySelectedProvidersModeEnum, []string) {
selectedProvidersModeList := []projectstypes.PolicySelectedProvidersModeEnum{}
func (k Keeper) CalculateEffectiveSelectedProviders(policies []*projectstypes.Policy) (projectstypes.SELECTED_PROVIDERS_MODE, []string) {
selectedProvidersModeList := []projectstypes.SELECTED_PROVIDERS_MODE{}
selectedProvidersList := [][]string{}
for _, p := range policies {
selectedProvidersModeList = append(selectedProvidersModeList, p.SelectedProvidersMode)
if p.SelectedProvidersMode == projectstypes.Policy_EXCLUSIVE || p.SelectedProvidersMode == projectstypes.Policy_MIXED {
if p.SelectedProvidersMode == projectstypes.SELECTED_PROVIDERS_MODE_EXCLUSIVE || p.SelectedProvidersMode == projectstypes.SELECTED_PROVIDERS_MODE_MIXED {
if len(p.SelectedProviders) != 0 {
selectedProvidersList = append(selectedProvidersList, p.SelectedProviders)
}
Expand Down
12 changes: 6 additions & 6 deletions x/pairing/keeper/pairing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,9 +320,9 @@ func TestSelectedProvidersPairing(t *testing.T) {
err := ts.addProvider(200)
require.Nil(t, err)

allowed := projectstypes.Policy_ALLOWED
exclusive := projectstypes.Policy_EXCLUSIVE
disabled := projectstypes.Policy_DISABLED
allowed := projectstypes.SELECTED_PROVIDERS_MODE_ALLOWED
exclusive := projectstypes.SELECTED_PROVIDERS_MODE_EXCLUSIVE
disabled := projectstypes.SELECTED_PROVIDERS_MODE_DISABLED

maxProvidersToPair := ts.keepers.Pairing.CalculateEffectiveProvidersToPairFromPolicies(
[]*projectstypes.Policy{&ts.plan.PlanPolicy, projPolicy},
Expand Down Expand Up @@ -364,9 +364,9 @@ func TestSelectedProvidersPairing(t *testing.T) {
// TODO: add mixed mode test cases (once implemented)
templates := []struct {
name string
planMode projectstypes.PolicySelectedProvidersModeEnum
subMode projectstypes.PolicySelectedProvidersModeEnum
projMode projectstypes.PolicySelectedProvidersModeEnum
planMode projectstypes.SELECTED_PROVIDERS_MODE
subMode projectstypes.SELECTED_PROVIDERS_MODE
projMode projectstypes.SELECTED_PROVIDERS_MODE
providersSet int
expectedProviders int
}{
Expand Down
14 changes: 7 additions & 7 deletions x/projects/keeper/project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -599,10 +599,10 @@ func TestSetPolicySelectedProviders(t *testing.T) {
subAddr := projectData.ProjectKeys[0].Key
projPolicy := projectData.Policy

allowed := types.Policy_ALLOWED
mixed := types.Policy_MIXED
exclusive := types.Policy_EXCLUSIVE
disabled := types.Policy_DISABLED
allowed := types.SELECTED_PROVIDERS_MODE_ALLOWED
mixed := types.SELECTED_PROVIDERS_MODE_MIXED
exclusive := types.SELECTED_PROVIDERS_MODE_EXCLUSIVE
disabled := types.SELECTED_PROVIDERS_MODE_DISABLED

providersSets := []struct {
planProviders []string
Expand All @@ -619,9 +619,9 @@ func TestSetPolicySelectedProviders(t *testing.T) {

templates := []struct {
name string
planMode types.PolicySelectedProvidersModeEnum
subMode types.PolicySelectedProvidersModeEnum
projMode types.PolicySelectedProvidersModeEnum
planMode types.SELECTED_PROVIDERS_MODE
subMode types.SELECTED_PROVIDERS_MODE
projMode types.SELECTED_PROVIDERS_MODE
providerSet int
planPolicyValid bool
subPolicyValid bool
Expand Down
Loading

0 comments on commit 33a8a61

Please sign in to comment.