Skip to content

Commit

Permalink
fix: permissionless added event + cli changes (#2185)
Browse files Browse the repository at this point in the history
* add consumer registration event

* fix some queries, CLI and permissionless logic

* cli changes

* addressed review comments
  • Loading branch information
bermuell committed Aug 29, 2024
1 parent 43ecf10 commit 29b1bf4
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 65 deletions.
12 changes: 6 additions & 6 deletions x/ccv/provider/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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,
),
Expand All @@ -188,15 +188,15 @@ $ %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 {
return err
}

req := &types.QueryValidatorProviderAddrRequest{
ChainId: consumerChainID,
ConsumerId: consumerID,
ConsumerAddress: addr.String(),
}
res, err := queryClient.QueryValidatorProviderAddr(cmd.Context(), req)
Expand Down
80 changes: 22 additions & 58 deletions x/ccv/provider/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,17 +215,17 @@ 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.
Note that the one that signs this message is the owner of this consumer chain. The owner can be later
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 {
Expand All @@ -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
}
Expand All @@ -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 {
Expand All @@ -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
}
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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),
Expand Down
2 changes: 1 addition & 1 deletion x/ccv/provider/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
6 changes: 6 additions & 0 deletions x/ccv/provider/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
2 changes: 2 additions & 0 deletions x/ccv/types/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const (
EventTypeExecuteConsumerChainSlash = "execute_consumer_chain_slash"
EventTypeFeeDistribution = "fee_distribution"
EventTypeConsumerSlashRequest = "consumer_slash_request"
EventTypeConsumerCreation = "consumer_creation"

AttributeKeyAckSuccess = "success"
AttributeKeyAck = "acknowledgement"
Expand All @@ -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"
Expand Down

0 comments on commit 29b1bf4

Please sign in to comment.