From 29b1bf4d694081d9ee8a29b880c5c61aaebb55b4 Mon Sep 17 00:00:00 2001 From: bernd-m <43466467+bermuell@users.noreply.github.com> Date: Thu, 29 Aug 2024 11:11:39 +0200 Subject: [PATCH] fix: permissionless added event + cli changes (#2185) * add consumer registration event * fix some queries, CLI and permissionless logic * cli changes * addressed review comments --- x/ccv/provider/client/cli/query.go | 12 ++--- x/ccv/provider/client/cli/tx.go | 80 ++++++++--------------------- x/ccv/provider/keeper/grpc_query.go | 2 +- x/ccv/provider/keeper/msg_server.go | 6 +++ x/ccv/types/events.go | 2 + 5 files changed, 37 insertions(+), 65 deletions(-) diff --git a/x/ccv/provider/client/cli/query.go b/x/ccv/provider/client/cli/query.go index fd5d5d7762..1d6e26350b 100644 --- a/x/ccv/provider/client/cli/query.go +++ b/x/ccv/provider/client/cli/query.go @@ -46,8 +46,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) @@ -170,12 +170,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, ), @@ -188,7 +188,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 { @@ -196,7 +196,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..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. @@ -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 } @@ -291,16 +270,16 @@ 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.). 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 { @@ -314,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 } @@ -418,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), @@ -466,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 { @@ -505,12 +469,12 @@ 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]. 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/grpc_query.go b/x/ccv/provider/keeper/grpc_query.go index d782d650f5..d47fd8fec9 100644 --- a/x/ccv/provider/keeper/grpc_query.go +++ b/x/ccv/provider/keeper/grpc_query.go @@ -210,7 +210,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 } diff --git a/x/ccv/provider/keeper/msg_server.go b/x/ccv/provider/keeper/msg_server.go index 28e0ef24ba..e78e8f8c9b 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.EventTypeConsumerCreation, + 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..0be5b1a7d8 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" + EventTypeConsumerCreation = "consumer_creation" 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"