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

Refactor 08-wasm with improvements after 02-client refactor #6088

Merged
merged 10 commits into from
Apr 15, 2024

Conversation

DimitrisJim
Copy link
Contributor

@DimitrisJim DimitrisJim commented Apr 3, 2024

Description

closes: #5833
closes: #5834


Before we can merge this PR, please make sure that all the following items have been
checked off. If any of the checklist items are not applicable, please leave them but
write a little note why.

  • Targeted PR against the correct branch (see CONTRIBUTING.md).
  • Linked to GitHub issue with discussion and accepted design, OR link to spec that describes this work.
  • Code follows the module structure standards and Go style guide.
  • Wrote unit and integration tests.
  • Updated relevant documentation (docs/).
  • Added relevant godoc comments.
  • Provide a conventional commit message to follow the repository standards.
  • Include a descriptive changelog entry when appropriate. This may be left to the discretion of the PR reviewers. (e.g. chores should be omitted from changelog)
  • Re-reviewed Files changed in the GitHub PR explorer.
  • Review SonarCloud Report in the comment section below once CI passes.

Summary by CodeRabbit

  • Refactor

    • Updated method implementations and imports in keeper.go for improved performance and code organization.
    • Enhanced code structure and error handling in keeper_vm.go for better reliability and maintainability.
    • Introduced a new gas registration variable in gas_register.go to optimize gas-related functionalities.
  • New Features

    • Added new methods and functionalities related to context, collections, and checksums in keeper.go.
    • Implemented panic checks and schema initialization in keeper_vm.go to enhance functionality.
    • Introduced a new gas registration variable VMGasRegister in gas_register.go for improved gas handling.
  • Bug Fixes

    • Adjusted method implementations in keeper.go for better error handling and code consistency.
    • Fixed issues related to query router and collections initialization in keeper_vm.go for enhanced stability.
    • Resolved gas registration inconsistencies in gas_register.go for improved performance.
  • Documentation

    • Updated documentation to reflect the changes in keeper.go, keeper_vm.go, and gas_register.go for better clarity and understanding.

Copy link
Contributor

coderabbitai bot commented Apr 3, 2024

Walkthrough

Walkthrough

The changes in the 08-wasm module involve refactoring and enhancing the management of light clients and WASM contracts. These changes aim to improve flexibility and maintainability by modifying query handling, migrations, and overall code structure.

Changes

Files Changes
modules/light-clients/08-wasm/keeper/keeper.go, modules/light-clients/08-wasm/keeper/keeper_vm.go Various updates and additions to imports, methods, and struct functionalities within the keeper files.
modules/light-clients/08-wasm/internal/ibcwasm/wasm.go Global variables moved to LightClientModule type following the merge of #5806.
modules/light-clients/08-wasm/types/store.go Function to obtain clientID through complex self-introspection will no longer be necessary post #5806 merge.

Assessment against linked issues

Objective Addressed Explanation
Remove 08-wasm globals (#5833) The globals have been moved to the LightClientModule type.
Remove complex self introspection to obtain clientID (#5834) The function for complex self-introspection is no longer needed post #5806 merge.

Possibly related issues


Recent Review Details

Configuration used: .coderabbit.yml

Commits Files that changed from the base of the PR and between fc3aeb5 and 7154767.
Files selected for processing (1)
  • modules/light-clients/08-wasm/types/gas_register.go (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • modules/light-clients/08-wasm/types/gas_register.go

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.

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.
  • 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.

* Move functionality from client state methods to light client module methods.

 - Yank all statements and delete functions as needed.
 - Make any required functions from types/ public.
 - Add the vm in the 08-wasm keeper.
 - Update documentation to include documentation of deleted functions.

* Remove global VM, introspection to retrieve client identifier.

 - Refactor signatures to explicitly pass vm, client identifier for time being.
 - Refactor tests to conform with new signatures.
 - Replace occurences of `ibcwasm.GetVM` in other keeper functions.

* Move migrateContract from clientState to keeper method.

- Matches moves for other functions.
- Move is required to address future circular dependency for state management (keeper would import types and types would import keeper)
- Rm migrate_contract(test).go files.

* Move state management to keeper.

- Remove globals for state (Checksums)
- Move access functions from types to keeper, move tests.
- Update calls using global to instead go through keeper.

* Make InitializePinnedCodes a method on the keeper.

* Remove pending todo in test for migrate contract.

This previously was testing a function that didn't set the client state after calling the contract (invoking through the keeper
always sets a client state after contract invocation). Hence, even if we don't set explicitly in the migrateFn callback, we _still_ get
a client state in the end.

* Move vm entrypoint functions in keeper/ package.

* feat: move vm entry points to keeper.

* chore: simplify signatures.

* test: add amended tests for contact keeper.

* refactor: move querier to keeper package.

* lint: you'll never fix me.

* refactor: remove queryRouter global variable

 - Move into the func for creating new plugins, pass it to acceptStargateQuery directly. This is probably what wasmd did (see they don't accept a query router now?).
   Since we don't use the get elsewhere, this was the smallest diff for a globa.
 - Preemptively ran make lint-fix, gotcha linting crybaby.

* refactor: remove global for query plugins.

* nit: rename to queryPlugins.

* refactor: remove QueryPluginsI interface.

* refactor: make queryHandler a private type.

* Make migrateContractCode a private function, clean up uneeded export in export_test.go for types/.

* refactor: Move vm entrypoints to keeper.

- Remove generic types and do unmarshalling in light client methods.
- Move all functions onto keeper and adjust call sites.

* chore: address some testing todos. Move testing unmarshal failure case to light client tests.

* chore: additional tiny nits.

- Consistently use wasmClientKeeper as local var name.
- Reorg keeper fields slightly.
- Rm 'vm.go' which was already empty.

* chore: restructure definitions to make diff more readable.

* d'oh: rm vm arg from instantiate.

* nit: Space out keeper fields. Remove TODO for method name.
@DimitrisJim
Copy link
Contributor Author

Will clean up some of the pending TODOs and then open for review. Diff is already large enough so don't want to add to it atm with additional diffs for the follow ups in #6103

return clientState.UpdateState(ctx, cdc, clientStore, clientMsg)
clientMessage, ok := clientMsg.(*types.ClientMessage)
if !ok {
panic(fmt.Errorf("expected type %T, got %T", &types.ClientMessage{}, clientMsg))

Check warning

Code scanning / CodeQL

Panic in BeginBock or EndBlock consensus methods Warning

Possible panics in BeginBock- or EndBlock-related consensus methods could cause a chain halt

res, err := l.keeper.WasmSudo(ctx, clientID, clientStore, clientState, payload)
if err != nil {
panic(err)

Check warning

Code scanning / CodeQL

Panic in BeginBock or EndBlock consensus methods Warning

Possible panics in BeginBock- or EndBlock-related consensus methods could cause a chain halt

var result types.UpdateStateResult
if err := json.Unmarshal(res, &result); err != nil {
panic(errorsmod.Wrap(types.ErrWasmInvalidResponseData, err.Error()))

Check warning

Code scanning / CodeQL

Panic in BeginBock or EndBlock consensus methods Warning

Possible panics in BeginBock- or EndBlock-related consensus methods could cause a chain halt
@@ -45,7 +45,6 @@ type queryHandler struct {
}

// newQueryHandler returns a default querier that can be used in the contract.
// TODO(jim): Make private and use export_test?
Copy link
Contributor Author

Choose a reason for hiding this comment

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

stale

@@ -328,13 +328,14 @@ func (suite *KeeperTestSuite) TestStargateQuery() {
// NOTE: we register query callbacks against: types.TimestampAtHeightMsg{}
// in practise, this can against any client state msg, however registering against types.StatusMsg{} introduces recursive loops
// due to test case: "success: verify membership query"
// TODO(Jim): Sanity check that it unmarshals correctly?
Copy link
Contributor Author

Choose a reason for hiding this comment

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

just checked the typical res is nil/not nil.

if !found {
return errorsmod.Wrap(clienttypes.ErrClientNotFound, substituteClientID)
}

substituteClientState := substituteClient
Copy link
Contributor Author

Choose a reason for hiding this comment

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

dunno why tf I had done this 😅

@@ -854,7 +854,9 @@ func (suite *WasmTestSuite) TestVerifyUpgradeAndUpdateState() {
},
clienttypes.ErrClientNotFound,
},
/* TODO(jim): Get back to these.
/* NOTE(jim): This can't fail on unmarshalling, it appears. Any consensus type
Copy link
Contributor Author

Choose a reason for hiding this comment

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

does it make sense to keep this in to note for covering this edge case?

Copy link
Member

Choose a reason for hiding this comment

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

is there something we should do about this commented out code for a follow up?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

unsure tbh. On the one hand I think we should be aware of this quirk, on the other hand, can't seem to best find a place to add it.

@@ -1462,24 +1463,20 @@ func (suite *WasmTestSuite) TestRecoverClient() {
},
clienttypes.ErrClientNotFound,
},
/* TODO(jim): We don't pass a client state directly now so we need to modify how this is tested.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

first test was removed, was already checked. Second test was amended to cover the case where the substitute has a diff checksum

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

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

Copy link
Contributor

@chatton chatton left a comment

Choose a reason for hiding this comment

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

LGTM! Amazing work, really clean for the scope of this refactor 👍

Copy link
Member

@damiannolan damiannolan left a comment

Choose a reason for hiding this comment

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

LGTM! I think I've reviewed all necessary files. Happy to merge this and create follow ups for anything outstanding.

Just left some nits on tests and such. Awesome work on refactoring this! Looks much better imo! 🚀

storeService: storeService,
clientKeeper: clientKeeper,
authority: authority,
}

_, err := sb.Build()
Copy link
Member

Choose a reason for hiding this comment

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

I wonder if we'd be better placed just keeping the schema around on the Keeper?
Avoid having to add it sometime when/if it becomes necessary.

I don't feel too strongly about it tho, happy to defer to your judgement on this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll leave it off the keeper for time being, have a slight pref for not adding it if it isn't doing something (and makes someone wonder why its there in the future)

testCases := []struct {
name string
malleate func()
malleate func(k *keeper.Keeper)
Copy link
Member

Choose a reason for hiding this comment

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

might be nice to change these tests to use GetSimApp(suite.chainA).WasmClientKeeper directly inside malleate instead of passing the arg. If only for consistencies sake with the rest of the codebase.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yup, part of one of the follow ups to refactor this!

@@ -132,17 +148,17 @@ func (suite *TypesTestSuite) TestStargateQuery() {

testCases := []struct {
name string
malleate func()
malleate func(k *keeper.Keeper)
Copy link
Member

Choose a reason for hiding this comment

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

ditto

modules/light-clients/08-wasm/types/gas_register.go Outdated Show resolved Hide resolved
@@ -854,7 +854,9 @@ func (suite *WasmTestSuite) TestVerifyUpgradeAndUpdateState() {
},
clienttypes.ErrClientNotFound,
},
/* TODO(jim): Get back to these.
/* NOTE(jim): This can't fail on unmarshalling, it appears. Any consensus type
Copy link
Member

Choose a reason for hiding this comment

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

is there something we should do about this commented out code for a follow up?

Co-authored-by: Damian Nolan <damiannolan@gmail.com>
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

Actionable comments outside the diff hunks (1)
modules/light-clients/08-wasm/types/gas_register.go (1)

Line range hint 3-11: Reorder imports according to the specified custom order to comply with the project's coding standards.

import (
+	wasmvmtypes "github.com/CosmWasm/wasmvm/v2/types"
+	errorsmod "cosmossdk.io/errors"
+	sdkmath "cosmossdk.io/math"
+	storetypes "cosmossdk.io/store/types"
+	sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
-	wasmvmtypes "github.com/CosmWasm/wasmvm/v2/types"
-	errorsmod "cosmossdk.io/errors"
-	sdkmath "cosmossdk.io/math"
-	storetypes "cosmossdk.io/store/types"
-	sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)

Denominator: 100,
}

VMGasRegister = NewDefaultWasmGasRegister()
Copy link
Contributor

Choose a reason for hiding this comment

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

Consider adding a blank line before the declaration of VMGasRegister for better readability.

+	
	VMGasRegister = NewDefaultWasmGasRegister()

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.

Suggested change
VMGasRegister = NewDefaultWasmGasRegister()
VMGasRegister = NewDefaultWasmGasRegister()

@damiannolan
Copy link
Member

damiannolan commented Apr 15, 2024

Linter complaining about import ordering I think 👀 edit: maybe it was my code suggestion

Copy link

sonarcloud bot commented Apr 15, 2024

Quality Gate Passed Quality Gate passed for 'ibc-go'

Issues
7 New issues
0 Accepted issues

Measures
0 Security Hotspots
No data about Coverage
No data about Duplication

See analysis details on SonarCloud

@DimitrisJim DimitrisJim merged commit ebabc1c into main Apr 15, 2024
85 of 86 checks passed
@DimitrisJim DimitrisJim deleted the 08-wasm-refactor branch April 15, 2024 17:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
08-wasm priority PRs that need prompt reviews
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Remove complex self introspection to obtain clientID Remove 08-wasm globals
3 participants