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

unify native and wasm vp #1093

Closed
wants to merge 15 commits into from
Closed

Conversation

tzemanovic
Copy link
Member

@tzemanovic tzemanovic commented May 10, 2022

closes anoma/namada#110

depends and based on #1243

To facilitate code re-use between native and WASM VPs, this PR adds trait VpEnv that is implemented in both. Similarly, it adds trait TxEnv for transaction, currently only implemented in WASM.

Because the native VPs use methods on Ctx, but in WASM the "context" is implicit (WASM source itself doesn't have a direct access to it and the host env functions are resolved by the runtime), we're adding a "fake" Ctx to both vp_prelude and tx_prelude to be able to fit them under the same interface.

Another key difference is that in native VPs, gas handling is explicit and the env method's Result type is used to return early on out of gas error, whereas in WASM VPs, gas handling is not included in the source, but rather injected into the WASM before it's executed on the ledger. To unify their host env interfaces, the methods in VpEnv (and in TxEnv) use an associated Error type. In native VPs, the Error is instantiated to include out of gas error, however, in WASM the host environment functions are essentially infallible at type level (std::convert::Infallible) and out of gas error (or any other runtime errors for that matter) instead panic the WASM execution. However, it presented an opportunity to add more seamless error-handling for txs and VPs, so in WASM the EnvResult can be extended with user defined errors and messages.

The EnvResult's Error type can be used to add a static message to errors and/or wrap any other error E where E: std::error::Error (bd3b901) and the errors can be printed in debug build via debug_log! macro (the WASM debug build recipes are added in #1243). Transactions' and VPs' results are handled in the macros (562267d).

The debug_log! macro is also improve here to just println! to stdout in non-WASM target, e.g. unit test (76e1458.

This is a breaking change for WASM transactions and VPs (e90da7c for the changes in the current tx and VPs), where

  • transactions now receive ctx: &mut Ctx as their first arg and return TxResult (TxResult = EnvResult<()>)
  • VPs receive ctx: &Ctx as their first arg return VpResult (VpResult = EnvResult<bool>)

Additionally, the tx and VPs library code from vm_env crate has been moved to the tx_prelude/vp_prelude crates, so that only thing left in vm_env is the low-level host env functions API and a small helper for 2-step buffer read for dynamically-sized data.

For WASM testing helpers, a vp_host_env::ctx() and tx_host_env::ctx() is provided (d9f0c0b).

@tzemanovic tzemanovic force-pushed the tomas/unify-native-and-wasm-vp branch 2 times, most recently from 4c822a8 to 29e026c Compare July 1, 2022 09:07
@tzemanovic tzemanovic force-pushed the tomas/unify-native-and-wasm-vp branch from 29e026c to e373a1e Compare July 8, 2022 17:22
@tzemanovic tzemanovic force-pushed the tomas/unify-native-and-wasm-vp branch from 2707a81 to e948791 Compare July 20, 2022 19:04
@tzemanovic tzemanovic force-pushed the tomas/unify-native-and-wasm-vp branch from e948791 to bd3b901 Compare July 21, 2022 10:46
@tzemanovic tzemanovic force-pushed the tomas/unify-native-and-wasm-vp branch from bd3b901 to 06fafdc Compare July 21, 2022 13:41
@tzemanovic
Copy link
Member Author

pls update wasm

@tzemanovic tzemanovic marked this pull request as ready for review July 21, 2022 13:55
@tzemanovic
Copy link
Member Author

moved to anoma/namada#256

@tzemanovic tzemanovic closed this Aug 2, 2022
@mariari mariari deleted the tomas/unify-native-and-wasm-vp branch October 9, 2024 11:46
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.

unify native and WASM VP host env interfaces
3 participants