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: pass initial version at immutable tree creation #880

Merged
merged 3 commits into from
Feb 10, 2024

Conversation

beer-1
Copy link
Contributor

@beer-1 beer-1 commented Feb 8, 2024

In previous iavl, the version is not affecting to working hash. but in current iavl, the version is a part of working hash, so the InitialVersion should be used when you make working hash to newly added moules.

iavl/node.go

Line 435 in af7ae13

func (node *Node) hashWithCount(version int64) []byte {

The problem is happening when you adding an new module packetforward, you can easily reproduce the consensus breaking with software upgrade proposal. You should run more than two nodes and after upgrade restart validator node and see other nodes.

We deeply debugged it and found the working hash for the stores, which are added via upgrade, are same whatever the height you did upgrade ( [164 128 39 101 194 146 82 214 50 145 131 89 83 171 212 207 142 176 239 6 10 8 254 1 95 237 121 132 93 24 213 34] for packetforward). but it should be differ in current iavl because it is using version to make working hash. and also found it is using tree.version, which is 0, when a new module firstly added by upgrade module. This wrong store hash is kept only in runtime. When you restart the node, it makes different WorkingHash and then consensus broken.

@beer-1 beer-1 requested a review from a team as a code owner February 8, 2024 04:17
Copy link

coderabbitai bot commented Feb 8, 2024

Walkthrough

The update primarily focuses on enhancing the ImmutableTree data structure within the mutable_tree.go file by incorporating an initialization step for the version field. This initialization sets the version to an int64 value, which is obtained from opts.InitialVersion. This modification aims to ensure that the ImmutableTree starts with a specific version, enhancing its version management capabilities from the moment of its creation.

Changes

File(s) Change Summary
mutable_tree.go Added initialization of version field in ImmutableTree to opts.InitialVersion.

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:

Note: Auto-reply has been disabled for this repository by the repository owner. The CodeRabbit bot will not respond to your comments unless it is explicitly tagged.

  • 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 tests 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 from git and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit tests.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

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 a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @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.
  • The JSON schema for the configuration file is available here.
  • 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/coderabbit-overrides.v2.json

CodeRabbit Discord Community

Join our Discord Community to get help, request features, and share feedback.

@beer-1 beer-1 changed the title pass initial version at immutable tree creation fix: pass initial version at immutable tree creation Feb 8, 2024
@cool-develope
Copy link
Collaborator

Thanks for reporting this issue, it seems like the InitialVersion option is not used properly. Tree.version should be determined in LoadVersion so in this case, we should consider the InitialVersion while LoadVersion

@beer-1
Copy link
Contributor Author

beer-1 commented Feb 8, 2024

Thanks for reporting this issue, it seems like the InitialVersion option is not used properly. Tree.version should be determined in LoadVersion so in this case, we should consider the InitialVersion while LoadVersion.

Not sure this is correct way to fix it. Maybe result should be same.

If you like this more, I can fix the PR .

	if firstVersion == 0 {
		if targetVersion <= 0 {
+			tree.version = int64(tree.ndb.opts.InitialVersion)

			if !tree.skipFastStorageUpgrade {
				tree.mtx.Lock()
				defer tree.mtx.Unlock()
				_, err := tree.enableFastStorageAndCommitIfNotEnabled()
				return 0, err
			}
			return 0, nil
		}
		return 0, fmt.Errorf("no versions found while trying to load %v", targetVersion)
	}

iavl/mutable_tree.go

Lines 461 to 472 in af7ae13

if firstVersion == 0 {
if targetVersion <= 0 {
if !tree.skipFastStorageUpgrade {
tree.mtx.Lock()
defer tree.mtx.Unlock()
_, err := tree.enableFastStorageAndCommitIfNotEnabled()
return 0, err
}
return 0, nil
}
return 0, fmt.Errorf("no versions found while trying to load %v", targetVersion)
}

mutable_tree.go Outdated
@@ -56,7 +56,7 @@ func NewMutableTree(db dbm.DB, cacheSize int, skipFastStorageUpgrade bool, lg lo
}

ndb := newNodeDB(db, cacheSize, opts, lg)
head := &ImmutableTree{ndb: ndb, skipFastStorageUpgrade: skipFastStorageUpgrade}
head := &ImmutableTree{ndb: ndb, skipFastStorageUpgrade: skipFastStorageUpgrade, version: int64(opts.InitialVersion)}
Copy link
Collaborator

@cool-develope cool-develope Feb 8, 2024

Choose a reason for hiding this comment

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

I know the result is same.
The happy case is to load the current version while constructing the tree in LoadVersion, so we don't need to set the InitialVersion option. The InitialVersion is just optional considering the above case like new module.
But if we force set the version here, it can be misleading the meaning of InitialVersion.

Copy link
Contributor Author

@beer-1 beer-1 Feb 8, 2024

Choose a reason for hiding this comment

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

I don't have strong opinion on this, so happy to follow your way.

Is the above solution (in comment) is good? or should we make other changes on LoadVersion function?
I have tested, and the above solution is also working.

Copy link
Collaborator

Choose a reason for hiding this comment

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

no, we should move this part to the LoadVersion, your commit will be not working when set the initial version by SetInitialVersion without opts.

Copy link
Contributor Author

@beer-1 beer-1 Feb 8, 2024

Choose a reason for hiding this comment

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

okay, I moved the fix to LoadVersion and tested it. It's also working well :)
@cool-develope

@cool-develope cool-develope enabled auto-merge (squash) February 9, 2024 12:44
@cool-develope cool-develope merged commit 4bd2593 into cosmos:master Feb 10, 2024
7 checks passed
beer-1 added a commit to initia-labs/iavl that referenced this pull request Apr 15, 2024
beer-1 added a commit to initia-labs/iavl that referenced this pull request Apr 16, 2024
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.

3 participants