ton-society / grants-and-bounties

TON Foundation invites talent to imagine and realize projects that have the potential to integrate with the daily lives of users.
https://ton.org/grants
292 stars 132 forks source link

Compliler and emulator libraries bindings fo Golang via WASM #423

Open mr-tron opened 8 months ago

mr-tron commented 8 months ago

Summary

Make Dockerfile and script for compiling funcfiftlib and tvm_emulator to wasm binaries and golang wrapper via wazero (important!) for running basic library functions (compile funC code, runing TVM or emulator).

Context

At this moment we have 3 developer toolsets:

  1. Native C++. Difficult to install and use.
  2. Python wrapper from disintar team (running via docker or require to install a lot of C libraries). Also not so friendly.
  3. Blueprint. Nice, but JS :) Also we have bindings for running tvm and transactions emulator from Golang but also not very friendly (require downloading binary and run them without isolation) and it doesn't support funcfiftlib.

Problems:

  1. Current WASMs for blueprint compiled with threading extension for wasm which doesn't implemented in wazero.
  2. There is old builds from Stepan Korshakov but they have some problems with naming modules (or maybe I am stupid).

References

https://github.com/tonkeeper/tongo/blob/master/tvm/tvmExecutor.go - current bindings using C-shared library https://github.com/tact-lang/ton-wasm - wasm build without threads. https://github.com/ton-community/func-js - current js bindings

Estimate suggested reward

$1000

Naltox commented 8 months ago

LGTM

tvorogme commented 8 months ago

Python wrapper from disintar team (running via docker or require to install a lot of C libraries). Also not so friendly.

pip install tonpy on any system, no need of any external packages. Very friendly (but good to have for golang)

We compile ton sources to each system and each python version: https://pypi.org/project/tonpy/#files

delovoyhomie commented 7 months ago

@mr-tron, how is it going?

mr-tron commented 7 months ago

Why are you think I am making it? I am waiting somebody who makes this task.

hrmon commented 6 months ago

Hi. I would like to do this task. But what API for BoC and Cells should be used? The API from tonkeeper/tongo?

Pickausernaame commented 6 months ago

@mr-tron I think this script can be useful https://github.com/ton-blockchain/ton/blob/master/assembly/wasm/fift-func-wasm-build-ubuntu.sh

mr-tron commented 6 months ago

Hi. I would like to do this task. But what API for BoC and Cells should be used? The API from tonkeeper/tongo?

doesn't matter. tongo or tonutils-go. parameters to wasm are serialized to base64 BoCs

hrmon commented 6 months ago

An update!

As @mr-tron mentioned the lack of threads support in wazero, I started by updating build scripts to cover that. But I faced some other road blocks:

TON code base uses c++ exceptions. However, wazero runtime still does not support them. There has been some efforts [1] to implement it, but not in the mainstream yet.

The main TON repo uses emscripten for porting to WASM and build options specifically target web/js [2]. For instance, the WASM builds are dependent on nodefs for filesystem api [3].

Since we want to target golang we may have to rely on WASI [4]. Emscripten support for WASI is no complete and wasi-sdk does not support c++ exceptions.

Therefore, I think accomplishing this task mostly involve contributions to upstream tools and the workload goes beyond the time and budget considered.

What do you think?

[1] https://github.com/tetratelabs/wazero/compare/main...jerbob92:wazero:feature/implement-emscripten-cpp-exceptions [2] https://github.com/ton-blockchain/ton/blob/200508cf8fca3ce5aa8d566105e32aea38601e93/emulator/CMakeLists.txt#L58 [3] https://github.com/ton-blockchain/ton/blob/200508cf8fca3ce5aa8d566105e32aea38601e93/crypto/CMakeLists.txt#L402 [4] https://wasi.dev

mr-tron commented 6 months ago

Thank you for your investigation. I hoped what it can be easily. Maybe I will try it by myself later. I saw this post few days ago - https://agniva.me/wasi/2023/12/09/whisper-wasi.html But didn't have enough time to check. Looks like very similar task.

hrmon commented 6 months ago

Without exception handling support, the first throw instruction will abrupt the process. As I compared, the use of throw in Whisper is minimal. You do not face it in the common case and with proper input. On the other hand, in TON exceptions are used extensively. For instance, TVM errors are translated into C++ exceptions. However, there is a chance we can handle this using host-provided functions, similar to the case with JS (where emscripten provides them automatically in the wrapper.) I have some more time to try this.

I guess TON use of filesystem API is minimal and we can handle it similar to [1]. If so, it should be doable.

[1] https://agniva.me/wasi/2023/12/09/whisper-wasi.html