From b0178714e1e7d39e08fc03569b45e9e0ad6c7c5e Mon Sep 17 00:00:00 2001
From: Bernd Mueller <bernd@informal.systems>
Date: Thu, 15 Aug 2024 12:03:27 +0200
Subject: [PATCH 1/4] add consumer registration event

---
 x/ccv/provider/keeper/msg_server.go | 6 ++++++
 x/ccv/types/events.go               | 2 ++
 2 files changed, 8 insertions(+)

diff --git a/x/ccv/provider/keeper/msg_server.go b/x/ccv/provider/keeper/msg_server.go
index 28e0ef24ba..f64aed58e7 100644
--- a/x/ccv/provider/keeper/msg_server.go
+++ b/x/ccv/provider/keeper/msg_server.go
@@ -351,6 +351,12 @@ func (k msgServer) CreateConsumer(goCtx context.Context, msg *types.MsgCreateCon
 		}
 	}
 
+	ctx.EventManager().EmitEvents(sdk.Events{
+		sdk.NewEvent(ccvtypes.EventTypeConsumerRegistration,
+			sdk.NewAttribute(ccvtypes.AttributeConsumerID, consumerId),
+		),
+	})
+
 	return &types.MsgCreateConsumerResponse{ConsumerId: consumerId}, nil
 }
 
diff --git a/x/ccv/types/events.go b/x/ccv/types/events.go
index 3c63fd7954..2f6da88098 100644
--- a/x/ccv/types/events.go
+++ b/x/ccv/types/events.go
@@ -15,6 +15,7 @@ const (
 	EventTypeExecuteConsumerChainSlash  = "execute_consumer_chain_slash"
 	EventTypeFeeDistribution            = "fee_distribution"
 	EventTypeConsumerSlashRequest       = "consumer_slash_request"
+	EventTypeConsumerRegistration       = "consumer_registration"
 
 	AttributeKeyAckSuccess            = "success"
 	AttributeKeyAck                   = "acknowledgement"
@@ -34,6 +35,7 @@ const (
 	AttributeMisbehaviourHeight1      = "misbehaviour_height_1"
 	AttributeMisbehaviourHeight2      = "misbehaviour_height_2"
 	AttributeConsumerDoubleVoting     = "consumer_double_voting"
+	AttributeConsumerID               = "consumer_id"
 	AttributeChainID                  = "chain_id"
 	AttributeValidatorAddress         = "validator_address"
 	AttributeInfractionType           = "infraction_type"

From 8db24236bfa5bd78801eb12da7a929f444dfb744 Mon Sep 17 00:00:00 2001
From: Bernd Mueller <bernd@informal.systems>
Date: Wed, 21 Aug 2024 17:39:07 +0200
Subject: [PATCH 2/4] fix some queries, CLI and permissionless logic

---
 x/ccv/provider/client/cli/query.go  | 12 +++++-----
 x/ccv/provider/client/cli/tx.go     | 35 ++++++-----------------------
 x/ccv/provider/keeper/grpc_query.go |  2 +-
 3 files changed, 14 insertions(+), 35 deletions(-)

diff --git a/x/ccv/provider/client/cli/query.go b/x/ccv/provider/client/cli/query.go
index 34d715583c..1a351e2b20 100644
--- a/x/ccv/provider/client/cli/query.go
+++ b/x/ccv/provider/client/cli/query.go
@@ -48,8 +48,8 @@ func NewQueryCmd() *cobra.Command {
 // parameters managed by the x/params module.
 func CmdConsumerGenesis() *cobra.Command {
 	cmd := &cobra.Command{
-		Use:   "consumer-genesis [chainid]",
-		Short: "Query for consumer chain genesis state by chain id",
+		Use:   "consumer-genesis [consumer-id]",
+		Short: "Query for consumer chain genesis state by consumer id",
 		Args:  cobra.ExactArgs(1),
 		RunE: func(cmd *cobra.Command, args []string) error {
 			clientCtx, err := client.GetClientQueryContext(cmd)
@@ -241,12 +241,12 @@ $ %s query provider validator-consumer-key foochain %s1gghjut3ccd8ay0zduzj64hwre
 func CmdProviderValidatorKey() *cobra.Command {
 	bech32PrefixConsAddr := sdk.GetConfig().GetBech32ConsensusAddrPrefix()
 	cmd := &cobra.Command{
-		Use:   "validator-provider-key [chainid] [consumer-validator-address]",
+		Use:   "validator-provider-key [consumer-id] [consumer-validator-address]",
 		Short: "Query validator consensus public key for the provider chain",
 		Long: strings.TrimSpace(
 			fmt.Sprintf(`Returns the currently assigned validator consensus public key for the provider chain.
 Example:
-$ %s query provider validator-provider-key foochain %s1gghjut3ccd8ay0zduzj64hwre2fxs9ldmqhffj
+$ %s query provider validator-provider-key 333 %s1gghjut3ccd8ay0zduzj64hwre2fxs9ldmqhffj
 `,
 				version.AppName, bech32PrefixConsAddr,
 			),
@@ -259,7 +259,7 @@ $ %s query provider validator-provider-key foochain %s1gghjut3ccd8ay0zduzj64hwre
 			}
 			queryClient := types.NewQueryClient(clientCtx)
 
-			consumerChainID := args[0]
+			consumerID := args[0]
 
 			addr, err := sdk.ConsAddressFromBech32(args[1])
 			if err != nil {
@@ -267,7 +267,7 @@ $ %s query provider validator-provider-key foochain %s1gghjut3ccd8ay0zduzj64hwre
 			}
 
 			req := &types.QueryValidatorProviderAddrRequest{
-				ChainId:         consumerChainID,
+				ConsumerId:      consumerID,
 				ConsumerAddress: addr.String(),
 			}
 			res, err := queryClient.QueryValidatorProviderAddr(cmd.Context(), req)
diff --git a/x/ccv/provider/client/cli/tx.go b/x/ccv/provider/client/cli/tx.go
index 4a7cef864f..15cd8d69da 100644
--- a/x/ccv/provider/client/cli/tx.go
+++ b/x/ccv/provider/client/cli/tx.go
@@ -223,9 +223,9 @@ Note that the one that signs this message is the owner of this consumer chain. T
 changed by updating the consumer chain.
 
 Example:
-%s tx provider create-consumer [chain-id] [path/to/metadata.json] [path/to/initialization-parameters.json] [path/to/power-shaping-parameters.json] --from node0 --home ../node0 --chain-id $CID
+%s tx provider create-consumer [path/to/create_consumer.json] --from node0 --home ../node0 --chain-id $CID
 `, version.AppName)),
-		Args: cobra.ExactArgs(4),
+		Args: cobra.ExactArgs(1),
 		RunE: func(cmd *cobra.Command, args []string) error {
 			clientCtx, err := client.GetClientTxContext(cmd)
 			if err != nil {
@@ -240,37 +240,16 @@ Example:
 
 			signer := clientCtx.GetFromAddress().String()
 
-			chainId := args[0]
-
-			metadata := types.ConsumerMetadata{}
-			metadataJson, err := os.ReadFile(args[1])
-			if err != nil {
-				return err
-			}
-			if err = json.Unmarshal(metadataJson, &metadata); err != nil {
-				return fmt.Errorf("metadata unmarshalling failed: %w", err)
-			}
-
-			initializationParameters := types.ConsumerInitializationParameters{}
-			initializationParametersJson, err := os.ReadFile(args[2])
-			if err != nil {
-				return err
-			}
-			if err = json.Unmarshal(initializationParametersJson, &initializationParameters); err != nil {
-				return fmt.Errorf("initialization parameters unmarshalling failed: %w", err)
-			}
-
-			powerShapingParameters := types.PowerShapingParameters{}
-
-			powerShapingParametersJson, err := os.ReadFile(args[3])
+			consCreateJson, err := os.ReadFile(args[0])
 			if err != nil {
 				return err
 			}
-			if err = json.Unmarshal(powerShapingParametersJson, &powerShapingParameters); err != nil {
-				return fmt.Errorf("power-shaping parameters unmarshalling failed: %w", err)
+			consCreate := types.MsgCreateConsumer{}
+			if err = json.Unmarshal(consCreateJson, &consCreate); err != nil {
+				return fmt.Errorf("consumer data unmarshalling failed: %w", err)
 			}
 
-			msg, err := types.NewMsgCreateConsumer(signer, chainId, metadata, &initializationParameters, &powerShapingParameters)
+			msg, err := types.NewMsgCreateConsumer(signer, consCreate.ChainId, consCreate.Metadata, consCreate.InitializationParameters, consCreate.PowerShapingParameters)
 			if err != nil {
 				return err
 			}
diff --git a/x/ccv/provider/keeper/grpc_query.go b/x/ccv/provider/keeper/grpc_query.go
index 4f2763e0b9..4f2cafa6b2 100644
--- a/x/ccv/provider/keeper/grpc_query.go
+++ b/x/ccv/provider/keeper/grpc_query.go
@@ -201,7 +201,7 @@ func (k Keeper) QueryValidatorProviderAddr(goCtx context.Context, req *types.Que
 	}
 	consumerAddr := types.NewConsumerConsAddress(consumerAddrTmp)
 
-	providerAddr, found := k.GetValidatorByConsumerAddr(ctx, req.ChainId, consumerAddr)
+	providerAddr, found := k.GetValidatorByConsumerAddr(ctx, req.ConsumerId, consumerAddr)
 	if !found {
 		return &types.QueryValidatorProviderAddrResponse{}, nil
 	}

From 0173c0dac80d1c309618b9fdb168eee8c4c0233b Mon Sep 17 00:00:00 2001
From: Bernd Mueller <bernd@informal.systems>
Date: Thu, 29 Aug 2024 08:57:01 +0200
Subject: [PATCH 3/4] cli changes

---
 x/ccv/provider/client/cli/tx.go | 39 ++++++++++-----------------------
 1 file changed, 12 insertions(+), 27 deletions(-)

diff --git a/x/ccv/provider/client/cli/tx.go b/x/ccv/provider/client/cli/tx.go
index 15cd8d69da..c6e31f799e 100644
--- a/x/ccv/provider/client/cli/tx.go
+++ b/x/ccv/provider/client/cli/tx.go
@@ -277,9 +277,9 @@ func NewUpdateConsumerCmd() *cobra.Command {
 Note that only the owner of the chain can initialize it.
 
 Example:
-%s tx provider update-consumer [consumer-id] [owner-address] [path/to/metadata.json] [path/to/initialization-parameters.json] [path/to/power-shaping-parameters.json] --from node0 --home ../node0 --chain-id $CID
+	%s tx provider update-consumer [path/to/consumer-update.json] --from node0 --home ../node0 --chain-id $CID
 `, version.AppName)),
-		Args: cobra.ExactArgs(5),
+		Args: cobra.ExactArgs(1),
 		RunE: func(cmd *cobra.Command, args []string) error {
 			clientCtx, err := client.GetClientTxContext(cmd)
 			if err != nil {
@@ -293,37 +293,22 @@ Example:
 			txf = txf.WithTxConfig(clientCtx.TxConfig).WithAccountRetriever(clientCtx.AccountRetriever)
 
 			signer := clientCtx.GetFromAddress().String()
-			consumerId := args[0]
-			ownerAddress := args[1]
 
-			metadata := types.ConsumerMetadata{}
-			metadataJson, err := os.ReadFile(args[2])
+			consUpdateJson, err := os.ReadFile(args[0])
 			if err != nil {
 				return err
 			}
-			if err = json.Unmarshal(metadataJson, &metadata); err != nil {
-				return fmt.Errorf("metadata unmarshalling failed: %w", err)
-			}
 
-			initializationParameters := types.ConsumerInitializationParameters{}
-			initializationParametersJson, err := os.ReadFile(args[3])
-			if err != nil {
-				return err
-			}
-			if err = json.Unmarshal(initializationParametersJson, &initializationParameters); err != nil {
-				return fmt.Errorf("initialization parameters unmarshalling failed: %w", err)
+			consUpdate := types.MsgUpdateConsumer{}
+			if err = json.Unmarshal(consUpdateJson, &consUpdate); err != nil {
+				return fmt.Errorf("consumer data unmarshalling failed: %w", err)
 			}
 
-			powerShapingParameters := types.PowerShapingParameters{}
-			powerShapingParametersJson, err := os.ReadFile(args[4])
-			if err != nil {
-				return err
-			}
-			if err = json.Unmarshal(powerShapingParametersJson, &powerShapingParameters); err != nil {
-				return fmt.Errorf("power-shaping parameters unmarshalling failed: %w", err)
+			if strings.TrimSpace(consUpdate.ConsumerId) == "" {
+				return fmt.Errorf("consumer_id can't be empty")
 			}
 
-			msg, err := types.NewMsgUpdateConsumer(signer, consumerId, ownerAddress, &metadata, &initializationParameters, &powerShapingParameters)
+			msg, err := types.NewMsgUpdateConsumer(signer, consUpdate.ConsumerId, consUpdate.NewOwnerAddress, consUpdate.Metadata, consUpdate.InitializationParameters, consUpdate.PowerShapingParameters)
 			if err != nil {
 				return err
 			}
@@ -397,7 +382,7 @@ Example:
 
 func NewOptInCmd() *cobra.Command {
 	cmd := &cobra.Command{
-		Use: "opt-in [consumer-chain-id] [consumer-pubkey]",
+		Use: "opt-in [consumer-id] [consumer-pubkey]",
 		Short: "opts in validator to the consumer chain, and if given uses the " +
 			"provided consensus public key for this consumer chain",
 		Args: cobra.RangeArgs(1, 2),
@@ -445,7 +430,7 @@ func NewOptInCmd() *cobra.Command {
 
 func NewOptOutCmd() *cobra.Command {
 	cmd := &cobra.Command{
-		Use:   "opt-out [consumer-chain-id]",
+		Use:   "opt-out [consumer-id]",
 		Short: "opts out validator from this consumer chain",
 		Args:  cobra.ExactArgs(1),
 		RunE: func(cmd *cobra.Command, args []string) error {
@@ -484,7 +469,7 @@ func NewOptOutCmd() *cobra.Command {
 
 func NewSetConsumerCommissionRateCmd() *cobra.Command {
 	cmd := &cobra.Command{
-		Use:   "set-consumer-commission-rate [consumer-chain-id] [commission-rate]",
+		Use:   "set-consumer-commission-rate [consumer-id] [commission-rate]",
 		Short: "set a per-consumer chain commission",
 		Long: strings.TrimSpace(
 			fmt.Sprintf(`Note that the "commission-rate" argument is a fraction and should be in the range [0,1].

From 525afc0a7c224ef10542df22226069b97d0a4fef Mon Sep 17 00:00:00 2001
From: Bernd Mueller <bernd@informal.systems>
Date: Thu, 29 Aug 2024 10:23:37 +0200
Subject: [PATCH 4/4] addressed review comments

---
 x/ccv/provider/client/cli/tx.go     | 6 +++---
 x/ccv/provider/keeper/msg_server.go | 2 +-
 x/ccv/types/events.go               | 2 +-
 3 files changed, 5 insertions(+), 5 deletions(-)

diff --git a/x/ccv/provider/client/cli/tx.go b/x/ccv/provider/client/cli/tx.go
index c6e31f799e..4251ef50e1 100644
--- a/x/ccv/provider/client/cli/tx.go
+++ b/x/ccv/provider/client/cli/tx.go
@@ -215,7 +215,7 @@ Example:
 
 func NewCreateConsumerCmd() *cobra.Command {
 	cmd := &cobra.Command{
-		Use:   "create-consumer [chain-id] [metadata] [initialization-parameters] [power-shaping-parameters]",
+		Use:   "create-consumer [consumer-parameters]",
 		Short: "create a consumer chain",
 		Long: strings.TrimSpace(
 			fmt.Sprintf(`Create a consumer chain and get the assigned consumer id of this chain.
@@ -270,7 +270,7 @@ Example:
 
 func NewUpdateConsumerCmd() *cobra.Command {
 	cmd := &cobra.Command{
-		Use:   "update-consumer [consumer-id] [owner-address] [metadata] [initialization-parameters] [power-shaping-parameters]",
+		Use:   "update-consumer [consumer-parameters]",
 		Short: "update a consumer chain",
 		Long: strings.TrimSpace(
 			fmt.Sprintf(`Update a consumer chain to change its parameters (e.g., spawn time, allow list, etc.).
@@ -474,7 +474,7 @@ func NewSetConsumerCommissionRateCmd() *cobra.Command {
 		Long: strings.TrimSpace(
 			fmt.Sprintf(`Note that the "commission-rate" argument is a fraction and should be in the range [0,1].
 			Example:
-			%s set-consumer-commission-rate consumer-1 0.5 --from node0 --home ../node0`,
+			%s set-consumer-commission-rate 123 0.5 --from node0 --home ../node0`,
 				version.AppName),
 		),
 		Args: cobra.ExactArgs(2),
diff --git a/x/ccv/provider/keeper/msg_server.go b/x/ccv/provider/keeper/msg_server.go
index f64aed58e7..e78e8f8c9b 100644
--- a/x/ccv/provider/keeper/msg_server.go
+++ b/x/ccv/provider/keeper/msg_server.go
@@ -352,7 +352,7 @@ func (k msgServer) CreateConsumer(goCtx context.Context, msg *types.MsgCreateCon
 	}
 
 	ctx.EventManager().EmitEvents(sdk.Events{
-		sdk.NewEvent(ccvtypes.EventTypeConsumerRegistration,
+		sdk.NewEvent(ccvtypes.EventTypeConsumerCreation,
 			sdk.NewAttribute(ccvtypes.AttributeConsumerID, consumerId),
 		),
 	})
diff --git a/x/ccv/types/events.go b/x/ccv/types/events.go
index 2f6da88098..0be5b1a7d8 100644
--- a/x/ccv/types/events.go
+++ b/x/ccv/types/events.go
@@ -15,7 +15,7 @@ const (
 	EventTypeExecuteConsumerChainSlash  = "execute_consumer_chain_slash"
 	EventTypeFeeDistribution            = "fee_distribution"
 	EventTypeConsumerSlashRequest       = "consumer_slash_request"
-	EventTypeConsumerRegistration       = "consumer_registration"
+	EventTypeConsumerCreation           = "consumer_creation"
 
 	AttributeKeyAckSuccess            = "success"
 	AttributeKeyAck                   = "acknowledgement"