Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: comet show-validator command #2131

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

dezzeus
Copy link
Contributor

@dezzeus dezzeus commented Nov 6, 2024

The show-validator sub-command of the comet one was broken due to the fact that the the codebase no longer seems to provide a cosmos-sdk/codec (in the Cosmos context that acts as a service locator).

This patch by-pass such codec and provides a custom function for marshalling the public key in JSON.

Please note that the printed type has changed from cometbft/PubKeyBls12_381 to a more generic bls12_381, due to the internal representation of such information.

Let me know if it's enough or if I should attempt to restore the codec for keeping the same serialization format.

This fixes the issue #2123.

Summary by CodeRabbit

  • New Features

    • Introduced a new function for JSON serialization of public key data, enhancing public key management.
    • Updated the command for displaying validator information, simplifying the process of retrieving and presenting public keys.
  • Bug Fixes

    • Improved error handling and output formatting for public key retrieval in the validator command.

Copy link
Contributor

coderabbitai bot commented Nov 6, 2024

Walkthrough

The changes primarily involve modifications to the commands.go file in the mod/consensus/pkg/cometbft/cli package. A new function, marshalPublicKey, is introduced for JSON serialization of public key data. The ShowValidatorCmd function is updated to simplify public key retrieval by directly calling pvm.LoadFilePV, removing previous error handling and conversion steps. The StatusCommand function remains mostly unchanged. Overall, the modifications enhance public key management and output formatting.

Changes

File Change Summary
mod/consensus/pkg/cometbft/cli/commands.go - Added marshalPublicKey function for JSON serialization of public keys.
- Updated ShowValidatorCmd to simplify public key retrieval and output.
- StatusCommand remains largely unchanged.

Suggested labels

Ready for Review

Suggested reviewers

  • itsdevbear
  • chuck-bear
  • abi87

Poem

In the meadow where bunnies play,
A new command brightens the day.
With keys that dance in JSON's light,
We hop along, all feels just right!
So let’s cheer for changes, oh so grand,
In our code, a joyful band! 🐇✨


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

codecov bot commented Nov 6, 2024

Codecov Report

Attention: Patch coverage is 0% with 19 lines in your changes missing coverage. Please review.

Project coverage is 26.23%. Comparing base (a0f84fb) to head (efce8f1).

Files with missing lines Patch % Lines
mod/consensus/pkg/cometbft/cli/commands.go 0.00% 19 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #2131      +/-   ##
==========================================
- Coverage   26.24%   26.23%   -0.02%     
==========================================
  Files         360      360              
  Lines       16241    16248       +7     
  Branches       12       12              
==========================================
  Hits         4263     4263              
- Misses      11709    11716       +7     
  Partials      269      269              
Files with missing lines Coverage Δ
mod/consensus/pkg/cometbft/cli/commands.go 0.00% <0.00%> (ø)

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

📜 Review details

Configuration used: CodeRabbit UI
Review profile: ASSERTIVE

📥 Commits

Reviewing files that changed from the base of the PR and between 1601517 and 249b38b.

📒 Files selected for processing (1)
  • mod/consensus/pkg/cometbft/cli/commands.go (3 hunks)
🔇 Additional comments (2)
mod/consensus/pkg/cometbft/cli/commands.go (2)

25-26: LGTM: Import additions are appropriate.

The new imports support the custom JSON marshaling functionality required for the public key serialization.

Also applies to: 36-36


167-182: Verify the JSON output format.

Let's verify that the JSON output matches the expected format mentioned in the PR description, particularly the change from cometbft/PubKeyBls12_381 to bls12_381.

✅ Verification successful

Let me gather more information about the JSON serialization format by checking the actual implementation and tests.


Let me check one more thing to verify the actual BLS public key serialization format by looking at tests.


Based on the gathered information, I can now provide the final response:

The JSON output format is consistent with the codebase standards

The implementation in marshalPublicKey correctly uses the BLS key type bls12_381 as defined by CometBLSType constant in mod/primitives/pkg/crypto/bls.go. The JSON structure with type and value fields matches the standard format used across the codebase, as evidenced by similar structures in other parts of the code.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Search for any hardcoded public key type strings in the codebase
# to ensure consistency with the new format.

echo "Searching for public key type strings..."
rg -i "PubKeyBls12_381|bls12_381" --type go

echo "Searching for other public key type definitions..."
ast-grep --pattern 'Type: $_ // public key type string'

Length of output: 366


Script:

#!/bin/bash
# Check the BLS public key implementation and tests
echo "Checking BLS public key implementation..."
rg -A 10 "func.*Type\(\)" mod/primitives/pkg/crypto/bls.go

echo "Checking test files for JSON serialization..."
rg -l "json.*marshal.*test" --type go | xargs rg -A 10 "Marshal.*bls"

echo "Looking for JSON struct tags in public key related code..."
ast-grep --pattern 'struct {
  $$$
  Type string `json:"type"`
  $$$
}'

Length of output: 1393


Script:

#!/bin/bash
# Check BLS-specific test files and implementation
echo "Checking BLS test files..."
fd -e go test | xargs rg -B 5 -A 10 "json.*Marshal.*bls|bls.*json"

echo "Looking for BLS key type constant usage..."
rg -B 3 -A 3 "CometBLSType" --type go

echo "Checking for any BLS key serialization tests..."
rg -l "TestBLS.*JSON|JSON.*BLS" --type go

Length of output: 2415

mod/consensus/pkg/cometbft/cli/commands.go Show resolved Hide resolved
mod/consensus/pkg/cometbft/cli/commands.go Show resolved Hide resolved
@dezzeus
Copy link
Contributor Author

dezzeus commented Nov 6, 2024

As a note, if the previous type representation (cometbft/PubKeyBls12_381) is a strict one in value, please note that, with my current understanding of the project, I was not able to restore it.

The previous implementation relied on a dependency-injected codec.Codec of which I cannot find the initialization (in the previous codebase).

I've tried to restore the old serialization by replacing the new marshalPublicKey function with the following:

import (
        // ...
	"github.com/cosmos/cosmos-sdk/codec"
	addresscodec "github.com/cosmos/cosmos-sdk/codec/address"
	cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
)

func marshalPublicKey(pk cmtcrypto.PubKey) (string, error) {
	addrCdc := addresscodec.NewBech32Codec("bera")
	reg, _, err := codec.ProvideInterfaceRegistry(addrCdc, addrCdc, nil)
	if err != nil {
		return "", err
	}
	codec := codec.NewProtoCodec(reg)
	sdkPK, err := cryptocodec.FromCmtPubKeyInterface(pk)
	if err != nil {
		return "", err
	}

	info, err := codec.MarshalInterfaceJSON(sdkPK)
	if err != nil {
		return "", err
	}

	return string(info), nil
}

But the marshaling fails due to the missing PK interface in the registry that I have no clue on how to initialize it…

Copy link
Collaborator

@abi87 abi87 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not a complete solution but I think it's a reasonable patch to unlock some broken functionalities. Approving and planning to revisit it in the next future to fully fix this

Comment on lines +148 to +152

pk := pvm.LoadFilePV(
cfg.PrivValidatorKeyFile(),
cfg.PrivValidatorStateFile(),
)
pk, err := privValidator.GetPubKey()
if err != nil {
return err
}

sdkPK, err := cryptocodec.FromCmtPubKeyInterface(pk)
if err != nil {
return err
}
).Key.PubKey
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall looks good for the fix. I was curious, do we see any benefit in doing this ? I think it wasn't necessary.

@fridrik01
Copy link
Contributor

@berachain/core reasons not to merge this in now?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants