Skip to content

Commit

Permalink
Merge pull request lavanet#1065 from lavanet/CNS-749-allow-policy-del…
Browse files Browse the repository at this point in the history
…ete-in-project

CNS-749: allow policy delete in project
  • Loading branch information
Yaroms authored Dec 25, 2023
2 parents 6d4c5f5 + 6db1ca2 commit e563f6a
Show file tree
Hide file tree
Showing 16 changed files with 172 additions and 124 deletions.
4 changes: 2 additions & 2 deletions proto/lavanet/lava/projects/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ message MsgDelKeysResponse {
message MsgSetPolicy {
string creator = 1;
string project = 2;
lavanet.lava.plans.Policy policy = 3 [(gogoproto.nullable) = false];
lavanet.lava.plans.Policy policy = 3;
}

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

message MsgSetSubscriptionPolicyResponse {
Expand Down
4 changes: 2 additions & 2 deletions testutil/common/tester.go
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@ func (ts *Tester) TxProjectDelKeys(projectID, creator string, projectKeys ...pro
}

// TxProjectSetSubscriptionPolicy: implement 'tx project set-subscription-policy'
func (ts *Tester) TxProjectSetSubscriptionPolicy(projectID, subkey string, policy planstypes.Policy) (*projectstypes.MsgSetSubscriptionPolicyResponse, error) {
func (ts *Tester) TxProjectSetSubscriptionPolicy(projectID, subkey string, policy *planstypes.Policy) (*projectstypes.MsgSetSubscriptionPolicyResponse, error) {
msg := &projectstypes.MsgSetSubscriptionPolicy{
Creator: subkey,
Policy: policy,
Expand All @@ -525,7 +525,7 @@ func (ts *Tester) TxProjectSetSubscriptionPolicy(projectID, subkey string, polic
}

// TxProjectSetPolicy: implement 'tx project set-policy'
func (ts *Tester) TxProjectSetPolicy(projectID, subkey string, policy planstypes.Policy) (*projectstypes.MsgSetPolicyResponse, error) {
func (ts *Tester) TxProjectSetPolicy(projectID, subkey string, policy *planstypes.Policy) (*projectstypes.MsgSetPolicyResponse, error) {
msg := &projectstypes.MsgSetPolicy{
Creator: subkey,
Policy: policy,
Expand Down
6 changes: 3 additions & 3 deletions x/pairing/keeper/grpc_query_effective_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func TestEffectivePolicy(t *testing.T) {
MaxProvidersToPair: 3,
GeolocationProfile: 1,
}
_, err = ts.TxProjectSetPolicy(project.ProjectID, clientAddr, adminPolicy)
_, err = ts.TxProjectSetPolicy(project.ProjectID, clientAddr, &adminPolicy)
require.Nil(t, err)

subPolicy := planstypes.Policy{
Expand All @@ -45,7 +45,7 @@ func TestEffectivePolicy(t *testing.T) {
MaxProvidersToPair: 2,
GeolocationProfile: planstypes.Geolocation_value["GL"],
}
_, err = ts.TxProjectSetSubscriptionPolicy(project.ProjectID, clientAddr, subPolicy)
_, err = ts.TxProjectSetSubscriptionPolicy(project.ProjectID, clientAddr, &subPolicy)
require.Nil(t, err)

// the effective policy function calcaulates the effective chain policy within it
Expand All @@ -70,7 +70,7 @@ func TestEffectivePolicy(t *testing.T) {
require.Nil(t, res.PendingPolicy) // there should be no pending policy

// set a new policy without applying it (no advanceEpoch)
_, err = ts.TxProjectSetPolicy(project.ProjectID, clientAddr, planstypes.Policy{
_, err = ts.TxProjectSetPolicy(project.ProjectID, clientAddr, &planstypes.Policy{
TotalCuLimit: 80000,
EpochCuLimit: 5000,
MaxProvidersToPair: 3,
Expand Down
14 changes: 7 additions & 7 deletions x/pairing/keeper/pairing_subscription_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,12 +232,12 @@ func TestStrictestPolicyGeolocation(t *testing.T) {
MaxProvidersToPair: 2,
}

_, err = ts.TxProjectSetPolicy(projectID, client1Addr, *adminPolicy)
_, err = ts.TxProjectSetPolicy(projectID, client1Addr, adminPolicy)
require.Nil(t, err)

ts.AdvanceEpoch()

_, err = ts.TxProjectSetSubscriptionPolicy(projectID, client1Addr, *subscriptionPolicy)
_, err = ts.TxProjectSetSubscriptionPolicy(projectID, client1Addr, subscriptionPolicy)
require.Nil(t, err)

ts.AdvanceEpoch()
Expand Down Expand Up @@ -292,7 +292,7 @@ func TestStrictestPolicyProvidersToPair(t *testing.T) {
MaxProvidersToPair: tt.providersToPairSubPolicy,
}

_, err = ts.TxProjectSetPolicy(proj.Index, client1Addr, *adminPolicy)
_, err = ts.TxProjectSetPolicy(proj.Index, client1Addr, adminPolicy)
if !tt.adminPolicyValid {
require.NotNil(t, err)
return
Expand All @@ -302,7 +302,7 @@ func TestStrictestPolicyProvidersToPair(t *testing.T) {

ts.AdvanceEpoch()

_, err = ts.TxProjectSetSubscriptionPolicy(proj.Index, client1Addr, *subscriptionPolicy)
_, err = ts.TxProjectSetSubscriptionPolicy(proj.Index, client1Addr, subscriptionPolicy)
if !tt.subscriptionPolicyValid {
require.NotNil(t, err)
return
Expand Down Expand Up @@ -394,12 +394,12 @@ func TestStrictestPolicyCuPerEpoch(t *testing.T) {
MaxProvidersToPair: ts.plan.PlanPolicy.MaxProvidersToPair,
}

_, err = ts.TxProjectSetPolicy(proj.Index, client1Addr, *adminPolicy)
_, err = ts.TxProjectSetPolicy(proj.Index, client1Addr, adminPolicy)
require.Nil(t, err)

ts.AdvanceEpoch()

_, err = ts.TxProjectSetSubscriptionPolicy(proj.Index, client1Addr, *subscriptionPolicy)
_, err = ts.TxProjectSetSubscriptionPolicy(proj.Index, client1Addr, subscriptionPolicy)
require.Nil(t, err)

ts.AdvanceEpoch()
Expand Down Expand Up @@ -529,7 +529,7 @@ func TestAddProjectAfterPlanUpdate(t *testing.T) {
adminPolicy := ts.plan.PlanPolicy
adminPolicy.EpochCuLimit = oldEpochCuLimit - 30

_, err = ts.TxProjectSetPolicy(proj.Project.Index, dev1Addr, adminPolicy)
_, err = ts.TxProjectSetPolicy(proj.Project.Index, dev1Addr, &adminPolicy)
require.Nil(t, err)

// advance epoch to set the new policy
Expand Down
14 changes: 7 additions & 7 deletions x/pairing/keeper/pairing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ func TestAddonPairing(t *testing.T) {
if tt.projChainPolicy != nil {
projPolicy := defaultPolicy()
projPolicy.ChainPolicies = []planstypes.ChainPolicy{*tt.projChainPolicy}
_, err = ts.TxProjectSetPolicy(projectID, sub1Addr, projPolicy)
_, err = ts.TxProjectSetPolicy(projectID, sub1Addr, &projPolicy)
require.Nil(t, err)
}

Expand All @@ -542,7 +542,7 @@ func TestAddonPairing(t *testing.T) {
if tt.subscChainPolicy != nil {
subscPolicy := defaultPolicy()
subscPolicy.ChainPolicies = []planstypes.ChainPolicy{*tt.subscChainPolicy}
_, err = ts.TxProjectSetSubscriptionPolicy(projectID, sub1Addr, subscPolicy)
_, err = ts.TxProjectSetSubscriptionPolicy(projectID, sub1Addr, &subscPolicy)
require.Nil(t, err)
}

Expand Down Expand Up @@ -724,7 +724,7 @@ func TestSelectedProvidersPairing(t *testing.T) {
policy.SelectedProvidersMode = tt.projMode
policy.SelectedProviders = providersSet.projProviders

_, err = ts.TxProjectSetPolicy(project.Index, sub1Addr, *policy)
_, err = ts.TxProjectSetPolicy(project.Index, sub1Addr, policy)
require.Nil(t, err)

// skip epoch for the policy change to take effect
Expand All @@ -733,7 +733,7 @@ func TestSelectedProvidersPairing(t *testing.T) {
policy.SelectedProvidersMode = tt.subMode
policy.SelectedProviders = providersSet.subProviders

_, err = ts.TxProjectSetSubscriptionPolicy(project.Index, sub1Addr, *policy)
_, err = ts.TxProjectSetSubscriptionPolicy(project.Index, sub1Addr, policy)
require.Nil(t, err)

// skip epoch for the policy change to take effect
Expand Down Expand Up @@ -1051,7 +1051,7 @@ func TestGeolocationPairingScores(t *testing.T) {
if tt.changePolicy {
newPolicy = tt.planPolicy
newPolicy.GeolocationProfile = tt.newGeo
_, err = ts.TxProjectSetPolicy(projIndex, tt.dev.Addr.String(), newPolicy)
_, err = ts.TxProjectSetPolicy(projIndex, tt.dev.Addr.String(), &newPolicy)
require.Nil(t, err)
policies = append(policies, &newPolicy)
}
Expand Down Expand Up @@ -1942,7 +1942,7 @@ func TestExtensionAndAddonPairing(t *testing.T) {
if tt.projChainPolicy != nil {
projPolicy := defaultPolicy()
projPolicy.ChainPolicies = []planstypes.ChainPolicy{*tt.projChainPolicy}
_, err = ts.TxProjectSetPolicy(projectID, sub1Addr, projPolicy)
_, err = ts.TxProjectSetPolicy(projectID, sub1Addr, &projPolicy)
require.Nil(t, err)
}

Expand All @@ -1952,7 +1952,7 @@ func TestExtensionAndAddonPairing(t *testing.T) {
if tt.subscChainPolicy != nil {
subscPolicy := defaultPolicy()
subscPolicy.ChainPolicies = []planstypes.ChainPolicy{*tt.subscChainPolicy}
_, err = ts.TxProjectSetSubscriptionPolicy(projectID, sub1Addr, subscPolicy)
_, err = ts.TxProjectSetSubscriptionPolicy(projectID, sub1Addr, &subscPolicy)
require.Nil(t, err)
}

Expand Down
3 changes: 3 additions & 0 deletions x/plans/types/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ var policyDefaultValues = map[string]interface{}{
}

func (policy *Policy) ContainsChainID(chainID string) bool {
if policy == nil {
return false
}
if len(policy.ChainPolicies) == 0 {
// empty chainPolicies -> support all chains
return true
Expand Down
44 changes: 29 additions & 15 deletions x/projects/client/cli/tx_set_admin_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,44 +16,57 @@ import (

var _ = strconv.Itoa(0)

const DeletePolicyFlagName = "delete-policy"

func CmdSetPolicy() *cobra.Command {
cmd := &cobra.Command{
Use: "set-policy project-index [policy-file-path]",
Short: "set policy to a project",
Long: `The set-policy command allows a project admin to set a new policy to its project. The policy file is a YAML file (see cookbook/projects/example_policy.yml for reference). The new policy will be applied from the next epoch. To define a geolocation in the policy file, use the available geolocations: ` + planstypes.PrintGeolocations(),
Example: `required flags: --from <creator-address>
lavad tx project set-policy [project-index] [policy-file-path] --from <creator_address>
lavad tx project set-policy [policy-file-path] --from <creator_address> (use this for the default admin policy)`,
lavad tx project set-policy admin [policy-file-path] --from <creator_address> (use this for the default admin policy)
lavad tx project set-policy [project-index] --delete-policy --from <creator_address>`,
Args: cobra.RangeArgs(1, 2),
RunE: func(cmd *cobra.Command, args []string) (err error) {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
var projectId string
var adminPolicyFilePath string
if len(args) == 2 {
projectId = args[0]
adminPolicyFilePath = args[1]
} else {
adminPolicyFilePath = args[0]

projectId := args[0]
if projectId == "admin" {
projectId = clientCtx.GetFromAddress().String() + "-admin"
}

policy, err := planstypes.ParsePolicyFromYamlPath(adminPolicyFilePath)
if err != nil {
return err
// check if the command includes --delete-policy
deletePolicyFlag := cmd.Flags().Lookup(DeletePolicyFlagName)
if deletePolicyFlag == nil {
return fmt.Errorf("%s flag wasn't found", DeletePolicyFlagName)
}
deletePolicy := deletePolicyFlag.Changed

err = verifyChainPoliciesAreCorrectlySet(clientCtx, policy)
if err != nil {
return err
var policy *planstypes.Policy
if !deletePolicy {
if len(args) < 2 {
return fmt.Errorf("not enough arguments")
}
adminPolicyFilePath := args[1]
policy, err = planstypes.ParsePolicyFromYamlPath(adminPolicyFilePath)
if err != nil {
return err
}

err = verifyChainPoliciesAreCorrectlySet(clientCtx, policy)
if err != nil {
return err
}
}

msg := types.NewMsgSetPolicy(
clientCtx.GetFromAddress().String(),
projectId,
*policy,
policy,
)
if err := msg.ValidateBasic(); err != nil {
return err
Expand All @@ -64,6 +77,7 @@ func CmdSetPolicy() *cobra.Command {

flags.AddTxFlagsToCmd(cmd)
cmd.MarkFlagRequired(flags.FlagFrom)
cmd.Flags().Bool(DeletePolicyFlagName, false, "deletes the policy")

return cmd
}
Expand Down
29 changes: 22 additions & 7 deletions x/projects/client/cli/tx_set_subscription_policy.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cli

import (
"fmt"
"strconv"
"strings"

Expand All @@ -20,8 +21,9 @@ func CmdSetSubscriptionPolicy() *cobra.Command {
Short: "set subscription policy to a project",
Long: `The set-subscription-policy command allows the project's subscription consumer to set a new policy to its subscription which will affect some/all of the subscription's projects. The policy file is a YAML file (see cookbook/projects/example_policy.yml for reference). The new policy will be applied from the next epoch. To define a geolocation in the policy file, use the available geolocations: ` + planstypes.PrintGeolocations(),
Example: `required flags: --from <creator-address>
lavad tx project set-subscription-policy [project-indices] [policy-file-path] --from <creator_address>`,
Args: cobra.ExactArgs(2),
lavad tx project set-subscription-policy [project-indices] [policy-file-path] --from <creator_address>
lavad tx project set-subscription-policy [project-indices] --delete-policy --from <creator_address>`,
Args: cobra.RangeArgs(1, 2),
RunE: func(cmd *cobra.Command, args []string) (err error) {
argProjects := strings.Split(args[0], listSeparator)

Expand All @@ -30,17 +32,29 @@ func CmdSetSubscriptionPolicy() *cobra.Command {
return err
}

subscriptionPolicyFilePath := args[1]
// check if the command includes --delete-policy
deletePolicyFlag := cmd.Flags().Lookup(DeletePolicyFlagName)
if deletePolicyFlag == nil {
return fmt.Errorf("%s flag wasn't found", DeletePolicyFlagName)
}
deletePolicy := deletePolicyFlag.Changed

policy, err := planstypes.ParsePolicyFromYamlPath(subscriptionPolicyFilePath)
if err != nil {
return err
var policy *planstypes.Policy
if !deletePolicy {
if len(args) < 2 {
return fmt.Errorf("not enough arguments")
}
subscriptionPolicyFilePath := args[1]
policy, err = planstypes.ParsePolicyFromYamlPath(subscriptionPolicyFilePath)
if err != nil {
return err
}
}

msg := types.NewMsgSetSubscriptionPolicy(
clientCtx.GetFromAddress().String(),
argProjects,
*policy,
policy,
)
if err := msg.ValidateBasic(); err != nil {
return err
Expand All @@ -50,6 +64,7 @@ func CmdSetSubscriptionPolicy() *cobra.Command {
}

flags.AddTxFlagsToCmd(cmd)
cmd.Flags().Bool(DeletePolicyFlagName, false, "deletes the policy")

return cmd
}
11 changes: 6 additions & 5 deletions x/projects/keeper/msg_server_set_admin_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ func (k msgServer) SetPolicy(goCtx context.Context, msg *types.MsgSetPolicy) (*t

policy := msg.GetPolicy()

err := policy.ValidateBasicPolicy(false)
if err != nil {
return nil, err
if policy != nil {
err := policy.ValidateBasicPolicy(false)
if err != nil {
return nil, err
}
}

err = k.SetProjectPolicy(ctx, []string{msg.GetProject()}, &policy, msg.GetCreator(), types.SET_ADMIN_POLICY)

err := k.SetProjectPolicy(ctx, []string{msg.GetProject()}, policy, msg.GetCreator(), types.SET_ADMIN_POLICY)
if err != nil {
return nil, err
}
Expand Down
11 changes: 6 additions & 5 deletions x/projects/keeper/msg_server_set_subscription_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ func (k msgServer) SetSubscriptionPolicy(goCtx context.Context, msg *types.MsgSe

policy := msg.GetPolicy()

err := policy.ValidateBasicPolicy(false)
if err != nil {
return nil, err
if policy != nil {
err := policy.ValidateBasicPolicy(false)
if err != nil {
return nil, err
}
}

err = k.SetProjectPolicy(ctx, msg.GetProjects(), &policy, msg.GetCreator(), types.SET_SUBSCRIPTION_POLICY)

err := k.SetProjectPolicy(ctx, msg.GetProjects(), policy, msg.GetCreator(), types.SET_SUBSCRIPTION_POLICY)
if err != nil {
return nil, err
}
Expand Down
Loading

0 comments on commit e563f6a

Please sign in to comment.