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 #256

Merged
merged 14 commits into from
Oct 13, 2022
Merged

unify native and wasm VP #256

merged 14 commits into from
Oct 13, 2022

Conversation

tzemanovic
Copy link
Member

@tzemanovic tzemanovic commented Aug 2, 2022

moved from anoma/anoma#1093

closes #110 and #87

depends and based on #351

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 (anoma/anoma@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 (anoma/anoma@562267d).

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

This is a breaking change for WASM transactions and VPs (anoma/anoma@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 (anoma/anoma@d9f0c0b).

@tzemanovic tzemanovic marked this pull request as ready for review August 2, 2022 12:00
@tzemanovic tzemanovic force-pushed the tomas/unify-native-and-wasm-vp branch 2 times, most recently from 52ff433 to 13623c0 Compare August 11, 2022 16:40
@tzemanovic tzemanovic force-pushed the tomas/unify-native-and-wasm-vp branch 2 times, most recently from c039262 to 1e6c868 Compare August 17, 2022 15:57
@tzemanovic tzemanovic force-pushed the tomas/unify-native-and-wasm-vp branch from 1e6c868 to 14f638a Compare August 17, 2022 16:39
@tzemanovic
Copy link
Member Author

pls update wasm

@tzemanovic tzemanovic mentioned this pull request Aug 18, 2022
35 tasks
tzemanovic added a commit that referenced this pull request Aug 24, 2022
* tomas/unify-native-and-wasm-vp:
  [ci skip] wasm checksums update
  update wasm checksums
  wasm_for_tests: make
  Update shared/src/ledger/vp_env.rs
  changelog: add #1093
  wasm: improve error handling
  macros: add error handling to transaction and validity_predicate macros
  wasm: update for VM API changes
  tests: update for VM API changes
  VM: move vm_env sub-mod into tx/vp_prelude and add Tx/VpEnv and Ctx
  shared: update native_vp implementations for VpEnv methods
  shared/ledger/native_vp: implement VpEnv
  shared/vm: rename host_env TxEnv/VpEnv to TxVmEnv/VpVmEnv
  ledger: add tx and VP traits with host env functions
@tzemanovic tzemanovic mentioned this pull request Aug 24, 2022
tzemanovic added a commit that referenced this pull request Sep 23, 2022
- Updated tx_bond, tx_unbond and tx_withdraw and their tests to the new tx API

* tomas/unify-native-and-wasm-vp:
  [ci skip] wasm checksums update
  update wasm checksums
  wasm_for_tests: make
  Update shared/src/ledger/vp_env.rs
  changelog: add #1093
  wasm: improve error handling
  macros: add error handling to transaction and validity_predicate macros
  wasm: update for VM API changes
  tests: update for VM API changes
  VM: move vm_env sub-mod into tx/vp_prelude and add Tx/VpEnv and Ctx
  shared: update native_vp implementations for VpEnv methods
  shared/ledger/native_vp: implement VpEnv
  shared/vm: rename host_env TxEnv/VpEnv to TxVmEnv/VpVmEnv
  ledger: add tx and VP traits with host env functions
@juped juped merged commit 1d793ae into main Oct 13, 2022
@juped juped deleted the tomas/unify-native-and-wasm-vp branch October 13, 2022 06:51
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
2 participants