This repository has been archived by the owner on Sep 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 55
GSoC 2020: Webassembly backend for the Ergo compiler #777
Closed
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Signed-off-by: Jerome Simeon <[email protected]>
Signed-off-by: Jerome Simeon <[email protected]>
Signed-off-by: Jerome Simeon <[email protected]>
Signed-off-by: Jerome Simeon <[email protected]>
Signed-off-by: Jerome Simeon <[email protected]>
Signed-off-by: Patrik Keller <[email protected]>
Signed-off-by: Jerome Simeon <[email protected]>
Signed-off-by: Jerome Simeon <[email protected]>
Signed-off-by: Jerome Simeon <[email protected]>
Signed-off-by: Jerome Simeon <[email protected]>
… execution Signed-off-by: Jerome Simeon <[email protected]>
Signed-off-by: Jerome Simeon <[email protected]>
… target Signed-off-by: Jerome Simeon <[email protected]>
Signed-off-by: Jerome Simeon <[email protected]>
Signed-off-by: Jerome Simeon <[email protected]>
Enables true ergo -> wasm -> invoke/trigger pipeline. Signed-off-by: Patrik Keller <[email protected]>
Signed-off-by: Patrik Keller <[email protected]>
Signed-off-by: Jerome Simeon <[email protected]>
Signed-off-by: Jerome Simeon <[email protected]>
Signed-off-by: Jerome Simeon <[email protected]>
Signed-off-by: Jerome Simeon <[email protected]>
Signed-off-by: Jerome Simeon <[email protected]>
Signed-off-by: Jerome Simeon <[email protected]>
… how it is being distributed Signed-off-by: Jerome Simeon <[email protected]>
Signed-off-by: Jerome Simeon <[email protected]>
Signed-off-by: Jerome Simeon <[email protected]>
Signed-off-by: Jerome Simeon <[email protected]>
Signed-off-by: Jerome Simeon <[email protected]>
Signed-off-by: Jerome Simeon <[email protected]>
Signed-off-by: Jerome Simeon <[email protected]>
pkel
changed the title
GSoC 2020: Webassembly Backend for the Ergo compiler
GSoC 2020: Webassembly backend for the Ergo compiler
Aug 30, 2020
@pkel thanks for a great contribution to Accord Project! (and for the great time this summer). We are planning to review this work / merge and test it further and release it in the next major iteration of the Ergo compiler and Accord Project templating system. 🥇 Some notes on this PR:
|
Closing this PR (now that GSoC 2020 is far behind us) in favour of #773. Will keep the branch around as a documentation to the GSoC work. |
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
GSoC 2020: Webassembly Backend for the Ergo compiler
I worked on translating Ergo smart contracts to Webassembly as part of Google Summer of Code (GSoC) 2020. This PR documents
I will submit a link to this PR for final evaluation of my GSoC project. We probably want to freeze this PR on Monday 8/31/2020 and keep it around for future reference. Future additions should go to Jerome's PR #773.
Goals
Webassembly (Wasm) is a novel binary instruction format designed as portable compilation target for higher programming languages. It was primarily designed for efficient, fast, and safe execution on the web stack. Unlike previous attempts for bringing portable executables to the web (Flash, Java Applets, ActiveX), Wasm succeeded in being adopted in all major browsers. Additionally, Wasm is increasingly used outside the browser, e.g.:
Wasm as a compilation target is interesting for the Accord project because it provides safe execution and access to wider range of platforms (relative to the currently used Javascript backend). I provide a more detailed argument in this Tech-WG call recording.
The goal of this GSoC project was to investigate how Ergo smart contracts can be translated to and executed as Wasm. A complete compiler backend and execution engine seemed unrealistic from the beginning. We thus settled on working towards a proof-of-concept implementation that can compile and run basic Ergo contracts.
Technical Overview
Ergo is based on the verified query compiler Q*cert which is implemented in Coq. Q*cert translates from various input query languages such as SQL to Spark, Java, and Javascript. Accord projects adds an additional frontend for their smart contract language Ergo. Naturally, we implement the Wasm backend for Ergo by adding an additional compiler backend in Q*cert. Consequently, most of my work happened in the Q*cert repository. I document the technical details over there.
In a nutshell: Q*cert translates Ergo to an imperative intermediate representation (Imp) which has only basic control flow constructs, few operators, and operates on JSON-like values. Q*cert gives strong correctness guarantees about this part of the compilation: evaluation based equality of Ergo input and Imp output is proven in the proof assistant Coq. The translation step between Imp and Javascript is relatively small. This last part of Ergo's existing compilation not verified and implemented in OCaml.
For the Wasm backend, I start from the same Imp. I translate the Imp control flow to Wasm using OCaml. I directly target the abstract syntax tree (AST) provided with the Wasm reference implementation (also OCaml). This allows to reuse their serialization of the AST into the Wasm binary and text formats. It also enables testing against the reference Wasm interpreter.
The generated Wasm binaries rely on a Wasm runtime for execution. This runtime supplies a runtime encoding for Imp's JSON-like values and implements Imp's operators. The runtime is implemented in Assemblyscript. This setup allows us to reuse existing implementations for memory allocation, garbage collection, strings, arrays and dictionaries. Unfortunately, we also lose control about the exact memory representation of Imp values. In order to supply arguments to and read results from the Wasm contract, I developed a binary encoding of Imp's JSON values. This encoding is heavily inspired by MessagePack but I omit some features and optimization for easier implementation.
Execution of ergo smart contracts on the new Wasm backend goes as follows.
logic.wasm
that describes the clauses of as smart contract as Wasm functions.logic.wasm
andruntime.wasm
.logic.wasm
which includes the following steps.runtime.wasm
I provide execution engines for OCaml and NodeJS. The former is used for testing in the Q*cert repository. The latter enables debugging with Chrome DevTools. This PR integrates the NodeJS engine with the Ergo command line utility (mostly my supervisor's @jeromesimeon work since he is familiar with the code base).
Achievements
We can compile basic Ergo smart contracts to Wasm and execute them using the Ergo command line utility. You can watch a recorded demo in my final presentation of the project on the TechWG call (21m25s into the video). If you want to try it yourself, you can follow these steps.
This also works on non-trivial tests like
volumediscount
,helloworldenforce
, andintegertest
. However, not all Ergo concepts are supported.Future Work
In order to turn this PoC into a usable piece of software, some steps are missing.
Essentials:
Improvements:
Integration:
cicero archive --target wasm
)