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

feat(08-wasm): use 'clientID' as contract address #4939

Merged
merged 21 commits into from
Oct 27, 2023

Conversation

srdtrk
Copy link
Member

@srdtrk srdtrk commented Oct 24, 2023

Description

closes: #4928

Commit Message / Changelog Entry

feat(08-wasm): use clientID as contract address

see the guidelines for commit messages. (view raw markdown for examples)


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 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/) or specification (x/<module>/spec/).
  • Added relevant godoc comments.
  • Provide a commit message to be used for the changelog entry in the PR description for review.
  • Re-reviewed Files changed in the Github PR explorer.
  • Review Codecov Report in the comment section below once CI passes.

@srdtrk srdtrk added the 08-wasm label Oct 24, 2023
@srdtrk srdtrk marked this pull request as ready for review October 24, 2023 11:28
@srdtrk srdtrk linked an issue Oct 24, 2023 that may be closed by this pull request
modules/light-clients/08-wasm/types/store.go Show resolved Hide resolved
clientStore = upws.subjectStore
}

store, ok := clientStore.(storeprefix.Store)
Copy link
Member Author

Choose a reason for hiding this comment

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

We are using this Store, this might help the reviewers

modules/light-clients/08-wasm/types/vm.go Show resolved Hide resolved
Comment on lines -10 to -16
// these fields are exported aliases for the payload fields passed to the wasm vm.
// these are used to specify callback functions to handle specific queries in the mock vm.
type (
// CallbackFn types
QueryFn = queryFn
SudoFn = sudoFn
)
Copy link
Member Author

Choose a reason for hiding this comment

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

I've removed the usage of these in my test in another PR but forgot to remove them from export_test.go. So I'm doing this here

Copy link
Contributor

@crodriguezvega crodriguezvega left a comment

Choose a reason for hiding this comment

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

Thank you, @srdtrk. Nice tricks to get the client ID from the store!

Comment on lines 157 to 171
lastSlash := strings.LastIndex(prefixStr, "/")
if lastSlash == -1 {
return "", errorsmod.Wrapf(ErrRetrieveClientID, "prefix does not contain a slash")
}
secondLastSlash := strings.LastIndex(prefixStr[:lastSlash], "/")
if secondLastSlash == -1 {
return "", errorsmod.Wrapf(ErrRetrieveClientID, "prefix does not contain a second slash")
}

clientID := prefixStr[secondLastSlash+1 : lastSlash]

isClientID := strings.HasPrefix(clientID, exported.Wasm)
if !isClientID {
return "", errorsmod.Wrapf(ErrRetrieveClientID, "prefix does not contain a 08-wasm clientID")
}
Copy link
Contributor

@crodriguezvega crodriguezvega Oct 26, 2023

Choose a reason for hiding this comment

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

I know we tend to prefer doing string parsing manually, but I think in this particular scenario a regex would probably make this code more readable. I was just playing a bit around with it and something like ^[ -~]*\/(08-wasm\-\d*){1}\/$ could do the trick. This could even be a separate function to test it more easily. [ -~] matches all ASCII characters, but we can tune this better to follow the spec.

Copy link
Member Author

Choose a reason for hiding this comment

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

I'll move this validation steps for clientID to a validation helper in a follow up PR. Because we need the validation for MsgMigrateContract too. Can we discuss this again in that PR?

Copy link
Contributor

Choose a reason for hiding this comment

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

Sounds good!

modules/light-clients/08-wasm/types/errors.go Outdated Show resolved Hide resolved
Copy link
Contributor

@colin-axner colin-axner left a comment

Choose a reason for hiding this comment

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

Nice work @srdtrk! I am leaving my general approval. I think the parsing of the client ID code could be improved a bit readability wise + some more comments/docs, but otherwise looks good to me :)

env := getEnv(ctx)
clientID, err := getClientID(clientStore)
if err != nil {
return nil, errorsmod.Wrapf(err, "failed to retrieve clientID for wasm contract instantiation")
Copy link
Contributor

Choose a reason for hiding this comment

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

should a test case be added for each of these errors?

Copy link
Member Author

Choose a reason for hiding this comment

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

Added a test case in TestInitialize (Instantiate), TestVerifyClientMessage (Query), and TestUpdateState (Sudo)

modules/light-clients/08-wasm/types/store.go Show resolved Hide resolved
modules/light-clients/08-wasm/types/store.go Show resolved Hide resolved
modules/light-clients/08-wasm/types/store.go Outdated Show resolved Hide resolved
Comment on lines 157 to 171
lastSlash := strings.LastIndex(prefixStr, "/")
if lastSlash == -1 {
return "", errorsmod.Wrapf(ErrRetrieveClientID, "prefix does not contain a slash")
}
secondLastSlash := strings.LastIndex(prefixStr[:lastSlash], "/")
if secondLastSlash == -1 {
return "", errorsmod.Wrapf(ErrRetrieveClientID, "prefix does not contain a second slash")
}

clientID := prefixStr[secondLastSlash+1 : lastSlash]

isClientID := strings.HasPrefix(clientID, exported.Wasm)
if !isClientID {
return "", errorsmod.Wrapf(ErrRetrieveClientID, "prefix does not contain a %s clientID", exported.Wasm)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this code could be simplified with strings.Split or something similar? I believe @chatton had some ideas.

Comments should indicate the expected format of the prefix

Copy link
Member Author

Choose a reason for hiding this comment

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

Carlos also had the idea of using regexps. Should we create an issue for this?

Copy link
Member Author

Choose a reason for hiding this comment

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

I made some improvements

@srdtrk srdtrk requested a review from colin-axner October 27, 2023 07:22
@srdtrk srdtrk merged commit 95a1237 into feat/wasm-clients Oct 27, 2023
58 of 59 checks passed
@srdtrk srdtrk deleted the serdar/issue#4928-use-contract-address branch October 27, 2023 08:43
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.

Use a contract address in every call to wasmVM.Instantiate
4 participants