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

Out of gas handling #331

Merged
merged 13 commits into from
May 18, 2020
Merged

Conversation

reuvenpo
Copy link
Contributor

@reuvenpo reuvenpo commented May 13, 2020

This PR aims to fix CosmWasm/wasmvm#29 .
It adds all the support required in the cosmwasm-vm crate (which is a lot)

@reuvenpo reuvenpo force-pushed the out-of-gas-handling branch from c6f578a to 44ca77e Compare May 13, 2020 17:56
reuvenpo added a commit to CosmWasm/wasmvm that referenced this pull request May 13, 2020
Copy link
Contributor Author

@reuvenpo reuvenpo left a comment

Choose a reason for hiding this comment

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

Just went over my diff again and added a few notes. Most of these are for myself but one is for the other reviewers.

packages/vm/src/context.rs Outdated Show resolved Hide resolved
packages/vm/src/context.rs Outdated Show resolved Hide resolved
packages/vm/src/imports.rs Show resolved Hide resolved
packages/vm/src/imports.rs Outdated Show resolved Hide resolved
Copy link
Member

@webmaster128 webmaster128 left a comment

Choose a reason for hiding this comment

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

Very nice work, great points.

I guess it will be hard to update. Maybe we can split this PR a little bit into smaller chunks to get them in?

contracts/hackatom/tests/integration.rs Outdated Show resolved Hide resolved
contracts/hackatom/tests/integration.rs Show resolved Hide resolved
packages/std/src/errors/system_error.rs Outdated Show resolved Hide resolved
packages/vm/src/context.rs Outdated Show resolved Hide resolved
packages/vm/src/context.rs Outdated Show resolved Hide resolved
packages/vm/src/imports.rs Show resolved Hide resolved
packages/vm/src/imports.rs Show resolved Hide resolved
packages/vm/src/imports.rs Outdated Show resolved Hide resolved
packages/vm/src/instance.rs Show resolved Hide resolved
packages/vm/src/mock/storage.rs Show resolved Hide resolved
@reuvenpo
Copy link
Contributor Author

Very nice work, great points.

Thanks! :D It took a lot of time to figure out a lot of the subtleties here, as you've seen so far.

I guess it will be hard to update. Maybe we can split this PR a little bit into smaller chunks to get them in?

My plan for Sunday is to churn through all the conflicts i'll get after running git merge origin/master. Can't really see an easier way to move forward since i know how much more i need to do here anyways.

@webmaster128
Copy link
Member

webmaster128 commented May 15, 2020

Okay, let's try to get this in first. After it is merged there are a few action items (not necessarily for you) good to get done on Monday (for 0.8.0):

  • in cosmwasm-vm, rename MemoryStorage to MockStorage since it is not reused anymore
  • in cosmwasm-vm, export everything that is related to testing (such as the mock stuff) in the ::testing module, like we do in cosmwasm-std
  • move MemoryStorage from cosmwasm-std to cosmwasm-storage
  • update Use FnOnce in with_*_from_context #333

@reuvenpo
Copy link
Contributor Author

Cool 👍
Please keep in mind that i also have more items to work on in this PR before i would consider the gas issue fully fixed. I don't think that i'd be able to act on all the comments so far, merge master into this PR, and finish the gas handling all in one day.

@webmaster128
Copy link
Member

webmaster128 commented May 15, 2020

@reuvenpo no worries. Amazing work has been done already and my focus is to reduce the diff, allowing us to progress in parallel from there.

@reuvenpo
Copy link
Contributor Author

reuvenpo commented May 17, 2020

  • I (believe that I) fixed everything we discussed in this PR on Friday
  • I merged latest master into this branch and fixed all the tests. Clippy is complaining because of an unused function: it's a function you defined so i want your feedback on whether i can safely remove it.
  • Please comment on the few issue that are still left open above 🙏

packages/vm/src/errors.rs Outdated Show resolved Hide resolved
packages/std/src/errors/system_error.rs Outdated Show resolved Hide resolved
packages/vm/src/imports.rs Show resolved Hide resolved
packages/vm/src/imports.rs Outdated Show resolved Hide resolved
packages/vm/src/imports.rs Show resolved Hide resolved
@webmaster128
Copy link
Member

webmaster128 commented May 18, 2020

Clippy is complaining because of an unused function: it's a function you defined so i want your feedback on whether i can safely remove it.

make_backend_err becoming unused is a good sign. It means FfiError covers all cases where an error occurs when the VM calls into the backend (chain). Could you remove make_backend_err and VmError::BackendErr?

@reuvenpo
Copy link
Contributor Author

Could you remove make_backend_err and VmError::BackendErr?

Done :)

Copy link
Member

@ethanfrey ethanfrey 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 on the major overhaul. There seem to be multiple different concerns in this PR, but good stuff in general. Added some comments on structure.

packages/std/src/errors/system_error.rs Outdated Show resolved Hide resolved
packages/std/src/errors/system_error.rs Outdated Show resolved Hide resolved
packages/std/src/errors/system_error.rs Show resolved Hide resolved
packages/vm/src/context.rs Show resolved Hide resolved
},
}

impl FfiError {
Copy link
Member

Choose a reason for hiding this comment

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

What is this for? I generally don't like the idea of modifying errors.
Maybe just a simpler constructor ffi_error(message: S) -> FfiError that returns an FfiError::Other variant?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh, this isn't a constructor. you can see how i use it, it's kind of like the context method from snafu::ResultExt. it basically says "If the value is a specific variant of the type, please give it this additional data". In this case i say that if it can, please add this message with context information. It's used to give a specific error message when go has an unexpected error (not panic) and we have something useful to say.

Copy link
Member

Choose a reason for hiding this comment

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

Can this be implemented as a constructor, i.e. keep the error immutable? Or do we get the error from somewhere and need to add the information later on?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Uhh, i suppose it could be? Let me play around with it. If you look at the places this is used, we first convert a GoResult to Result<(), FfiError>, and then if we got Other we add an error message with this method. I can wrap that up into an associated function on GoResult, perhaps.

Copy link
Member

Choose a reason for hiding this comment

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

Not too important in my opinion. Wrote it down here: #336

packages/vm/src/instance.rs Show resolved Hide resolved
start: Option<&[u8]>,
end: Option<&[u8]>,
order: Order,
) -> FfiResult<Box<dyn Iterator<Item = FfiResult<KV>> + 'a>> {
Copy link
Member

Choose a reason for hiding this comment

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

Okay, so copying all this code is to give us the proper Result/Error type for the iterator (and other ops). It seems necessary to match the different function signatures, but it is also missing any code that would let use test the error cases. I would propose replacing this with the following:

  1. A wrapper that takes cosmwasm_std::Storage and converts to cosmwasm_vm::Storage (different return types). This would convert any StdError to FfiError::Other by stringifying it. It lets us easily reuse code for the success-case.
  2. An implementation that always returns an error, where you can set an FfiError to in the struct, and all calls will return this error (cloned). I think it is critical to test this error handling - which is a large part of the refactor

Copy link
Member

Choose a reason for hiding this comment

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

Another case for this is the fact that staking contract is failing.

https://app.circleci.com/pipelines/github/CosmWasm/cosmwasm/1049/workflows/6581f6b9-4460-42d5-83b6-3467606c2680/jobs/7649/steps

My integration test customizes the Querier (staking module), but that uses the cosmwasm_std::Extern. Having no (test-safe) translation to cosmwasm_vm::Extern makes life hard here (and a whole lot more code copying if I want to support this). I think some transforms allow the two to diverge, but let us easily re-use test helpers. Such transforms can be in the testing/mock modules to signify they should only be used for test code (like MockStorage, MockQuerier, etc)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Uhm, i just fixed the staking contract integration tests by changing an import and tweaking a function signature to reflect a change that was made after i forked. i just pushed it but everything's passing on my machine, so it should pass here in a few minutes as well.
As for the suggestion in your first comment on this thread, that sounds good, but i don't think there's a strong need to do this right now. there were already tests that checked for errors in a lot of the code i touched, and i had to modify those tests to reflect my changes. Most of what i did is already backed by tests. The only thing that i feel that i should write a test for, is the case where things run out of gas, but i don't want to do it before i've completed the full error flow for that scenario.

Copy link
Member

Choose a reason for hiding this comment

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

Yes, these were ideas for future enhancements rather than in this pr.

packages/vm/src/traits.rs Show resolved Hide resolved
packages/vm/src/imports.rs Show resolved Hide resolved
packages/vm/src/imports.rs Show resolved Hide resolved
Copy link
Member

@webmaster128 webmaster128 left a comment

Choose a reason for hiding this comment

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

Solid and strictly better than current master. Ready to merge from my point of view

@webmaster128 webmaster128 merged commit 701e10e into CosmWasm:master May 18, 2020
Expotential108 added a commit to Expotential108/Binding-cosmwasm that referenced this pull request Mar 12, 2023
delneg pushed a commit to SigmaGmbH/swisstronik-librustgo that referenced this pull request Jul 25, 2023
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.

Gas consumed
3 participants