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

chore: set empty hash to hash of nothing #20775

Merged
merged 9 commits into from
Jul 2, 2024
Merged

chore: set empty hash to hash of nothing #20775

merged 9 commits into from
Jul 2, 2024

Conversation

tac0turtle
Copy link
Member

@tac0turtle tac0turtle commented Jun 25, 2024

Description

Closes: #20513

in server v1 we always set an empty hash when its needed via the lastcommitid check. this pr aims to recreate this


Author Checklist

All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.

I have...

  • included the correct type prefix in the PR title, you can find examples of the prefixes below:
  • confirmed ! in the type prefix if API or client breaking change
  • targeted the correct branch (see PR Targeting)
  • provided a link to the relevant issue or specification
  • reviewed "Files changed" and left comments if necessary
  • included the necessary unit and integration tests
  • added a changelog entry to CHANGELOG.md
  • updated the relevant documentation or specification, including comments for documenting Go code
  • confirmed all CI checks have passed

Reviewers Checklist

All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.

Please see Pull Request Reviewer section in the contributing guide for more information on how to review a pull request.

I have...

  • confirmed the correct type prefix in the PR title
  • confirmed all author checklist items have been addressed
  • reviewed state machine logic, API design and naming, documentation is accurate, tests and test coverage

Summary by CodeRabbit

  • New Features

    • Optimized the handling of header information and state management for better performance and reliability.
  • Bug Fixes

    • Fixed the initialization of Hash and AppHash to ensure they are non-nil, improving stability.
  • Refactor

    • Replaced abci struct declarations and return types with abciproto for better code clarity and maintenance.
  • Chores

    • Added imports for crypto/sha256 to enhance security-related computations.

Copy link
Contributor

coderabbitai bot commented Jun 25, 2024

Walkthrough

The changes focus on improving hash and response handling across multiple files in the repository. Key updates include initializing hashes with non-nil values, transitioning from abci to abciproto structures, and fixing app hash handling. Additionally, new functions were introduced for manipulating header information within the state, ensuring better abstraction and management of header data.

Changes

File Path Change Summary
core/header/service.go Removed empty hash logic, fixed AppHash length check, updated AppHash encoding in Bytes function.
server/v2/cometbft/abci.go Imported crypto/sha256, replaced abci with abciproto, updated hash initializations and response types.
server/v2/stf/core_header_service.go Added import for store, introduced constant headerInfoPrefix, added setHeaderInfo and getHeaderInfo functions.
server/v2/stf/stf.go Removed setHeaderInfo and getHeaderInfo functions, recommended higher-level abstraction.
store/v2/root/store.go Imported crypto/sha256, added SHA256 hash calculation for empty byte slice in LastCommitID function.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant CometBFT
    participant Application

    Client->>CometBFT: SubmitTx()
    CometBFT->>Application: CheckTx()
    Application->>CometBFT: CheckTxResponse{CheckTxResponse using abciproto}
    CometBFT->>Client: RespondTxStatus()

    Client->>CometBFT: QueryInfo()
    CometBFT->>Application: Info()
    Application->>CometBFT: InfoResponse{InfoResponse using abciproto}
    CometBFT->>Client: SendInfo()
Loading

Assessment against linked issues

Objective Addressed Explanation
Ensure server/v2 produces non-empty hash values where appropriate (#20513)

Tip

Early access features: enabled

We are currently testing the following features in early access:

  • OpenAI gpt-4o model for code reviews and chat: OpenAI claims that this model is better at understanding and generating code than the previous models. We seek your feedback over the next few weeks before making it generally available.

Note:

  • You can enable or disable early access features from the CodeRabbit UI or by updating the CodeRabbit configuration file.
  • Please join our Discord Community to provide feedback and report issues.
  • OSS projects are currently opted into early access features by default.

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>.
    • 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 generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @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 as 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.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration 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.

@tac0turtle tac0turtle marked this pull request as ready for review June 28, 2024 14:26
@tac0turtle tac0turtle requested a review from a team as a code owner June 28, 2024 14:26
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: 1

Review details

Configuration used: .coderabbit.yml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between c7375f7 and 3f3f09c.

Files selected for processing (5)
  • core/header/service.go (1 hunks)
  • server/v2/cometbft/abci.go (9 hunks)
  • server/v2/stf/core_header_service.go (2 hunks)
  • server/v2/stf/stf.go (1 hunks)
  • store/v2/root/store.go (2 hunks)
Additional context used
Path-based instructions (5)
server/v2/stf/core_header_service.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

core/header/service.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

store/v2/root/store.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

server/v2/cometbft/abci.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

server/v2/stf/stf.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

Additional comments not posted (9)
server/v2/stf/core_header_service.go (3)

7-7: Approved addition of store import.

The addition of "cosmossdk.io/core/store" aligns with the new functions introduced for setting and getting header info, which interact with the store.


39-55: Validate error handling and null checks in getHeaderInfo.

Proper error handling and null checks are in place. However, consider adding a comment on line 49 to explain the return of an empty header.Info when the value is nil.

+ // Return an empty header.Info if the stored value is nil, indicating no previous header info was set.

18-18: Check the use of a constant for headerInfoPrefix.

Verification successful

The usage of the headerInfoPrefix constant is consistent within the server/v2/stf/core_header_service.go file.

  • server/v2/stf/core_header_service.go: The constant is used in Set and Get methods.
Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify the usage of `headerInfoPrefix` to ensure it's used correctly throughout the codebase.

# Test: Search for the constant usage. Expect: Consistent and correct usage.
rg --type go $'headerInfoPrefix'

Length of output: 280

core/header/service.go (2)

40-40: Ensure hash size validation.

The validation of i.Hash length against hashSize is crucial for maintaining data integrity and preventing errors during encoding.


49-49: Ensure app hash size validation.

Similar to the hash size validation, validating i.AppHash against hashSize is essential for consistency and error prevention.

store/v2/root/store.go (2)

5-5: Approved addition of crypto/sha256 import.

The addition of the SHA256 library is necessary for the new functionality to compute hashes of empty byte slices, as seen in the LastCommitID method.


152-155: Verify handling of empty hashes in LastCommitID.

The logic to handle empty hashes by returning a SHA256 hash of an empty byte slice when latestVersion is 0 is a crucial update for consistency. Ensure this behavior is correctly implemented across all relevant parts of the application.

server/v2/cometbft/abci.go (2)

5-5: Approved addition of crypto/sha256 import.

The addition of the SHA256 library is necessary for the new functionality to compute hashes, as seen in the InitChain and other methods.


107-107: Review changes in response structures and hash handling in various methods.

The modifications in response structures and hash handling across multiple methods such as CheckTx, Info, InitChain, PrepareProposal, ProcessProposal, VerifyVoteExtension, and ExtendVote are crucial for aligning with the new architectural changes. Ensure these changes do not introduce any regressions or inconsistencies.

Also applies to: 141-141, 235-235, 283-283, 329-329, 362-362, 368-368, 541-541, 577-577

Comment on lines +20 to +37
// setHeaderInfo sets the header info in the state to be used by queries in the future.
func (s STF[T]) setHeaderInfo(state store.WriterMap, headerInfo header.Info) error {
// TODO storing header info is too low level here, stf should be stateless.
// We should have a keeper that does this.
runtimeStore, err := state.GetWriter(Identity)
if err != nil {
return err
}
bz, err := headerInfo.Bytes()
if err != nil {
return err
}
err = runtimeStore.Set([]byte{headerInfoPrefix}, bz)
if err != nil {
return err
}
return nil
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Ensure statelessness in STF design.

The comment on line 22 suggests that storing header info at this level might not be ideal, indicating a potential architectural concern. Consider refactoring to use a dedicated keeper for state management.

- // TODO storing header info is too low level here, stf should be stateless.
- // We should have a keeper that does this.
+ // Refactor to use a dedicated keeper for managing header info.
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
// setHeaderInfo sets the header info in the state to be used by queries in the future.
func (s STF[T]) setHeaderInfo(state store.WriterMap, headerInfo header.Info) error {
// TODO storing header info is too low level here, stf should be stateless.
// We should have a keeper that does this.
runtimeStore, err := state.GetWriter(Identity)
if err != nil {
return err
}
bz, err := headerInfo.Bytes()
if err != nil {
return err
}
err = runtimeStore.Set([]byte{headerInfoPrefix}, bz)
if err != nil {
return err
}
return nil
}
// setHeaderInfo sets the header info in the state to be used by queries in the future.
func (s STF[T]) setHeaderInfo(state store.WriterMap, headerInfo header.Info) error {
// Refactor to use a dedicated keeper for managing header info.
runtimeStore, err := state.GetWriter(Identity)
if err != nil {
return err
}
bz, err := headerInfo.Bytes()
if err != nil {
return err
}
err = runtimeStore.Set([]byte{headerInfoPrefix}, bz)
if err != nil {
return err
}
return nil
}

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: 0

Review details

Configuration used: .coderabbit.yml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 3f3f09c and a60bcdb.

Files selected for processing (1)
  • server/v2/cometbft/abci.go (9 hunks)
Files skipped from review as they are similar to previous changes (1)
  • server/v2/cometbft/abci.go

@tac0turtle tac0turtle added this pull request to the merge queue Jul 2, 2024
Merged via the queue into main with commit 4e65074 Jul 2, 2024
71 of 72 checks passed
@tac0turtle tac0turtle deleted the marko/20513 branch July 2, 2024 14:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

server/v1 may produce a hash of empty string instead of nil, should server/v2?
5 participants