Giter Club home page Giter Club logo

c_ewasm_contracts's Introduction

This repository has some Ewasm contracts written in C.

  • src directory has source code for contracts.
  • wasm and wat directories have output .wasm and .wat files.
  • tests directory contains scout yaml files.
  • old_ewasm_1_stuff directory contains old things which have not yet been ported to scout.

Dependencies

Dependencies are executables: wabt (wasm2wat, wat2wasm), binaryen (wasm-opt), llvm8+ (clang, opt, llc, wasm-ld), and optionally scout (scout). If you don't have any of these, download and compile these locally into this directory as follows, or just copy that recipe from the Makefile.

Wabt

git clone https://github.com/webassembly/wabt.git
cd wabt; mkdir wabt/build; cd wabt/build
cmake -DCMAKE_BUILD_TYPE=Release -DBUILD_TESTS=OFF ..
make -j4
cd ../..

Binaryen

git clone https://github.com/WebAssembly/binaryen.git
cd binaryen; mkdir build; cd build
cmake -DCMAKE_BUILD_TYPE=Release ..
make -j4
cd ../..

LLVM

git clone https://github.com/llvm/llvm-project.git	# big download
cd llvm-project; mkdir build; cd build
cmake -G 'Unix Makefiles' -DLLVM_ENABLE_PROJECTS="clang;libcxx;libcxxabi;lld" ../llvm
make -j4	# warning: long compilation time, need lots of ram and disk, may crash and need to be restarted

LLVM builtins -- Needed for things like __multi3() which do multiplciation of two 64-bit integers to a 128-bit integer.

wget https://github.com/CraneStation/wasi-sdk/releases/download/wasi-sdk-5/libclang_rt.builtins-wasm32-wasi-5.0.tar.gz
tar -xvzf libclang_rt.builtins-wasm32-wasi-5.0.tar.gz

Scout (optional)

git clone https://github.com/poemm/scout.cpp.git
cd scout.c; mkdir build; cd build
cmake ..
make
cd ../..
cp scout.cpp/build/scout.exec .

Compile src to Wasm

Update paths to dependencies in Makefile variables LLVM_DIR, WABT_DIR, BINARYEN_DIR, and SCOUT_DIR. Or just keep those variables empty and update $PATH to the directories with the above executables. Or pass those paths as arguments to make.

# compile blake2b reference implementation to Wasm
make blake2b_ref

# same as above, but with custom paths to tools, if you don't want to set paths in Makefile
make blake2b_ref WABT_DIR=wabt/build/ BINARYAN_DIR=binaryen/build/bin/ LLVM_DIR=llvm-project/build/bin/

# compile a custom my_contract.c
make PROJECT=my_contract SRC_DIR=path/to/my_contract/src/dir/

# run a test in scout, assuming scout is installed to the location using above: make scoutcpp-install
cd wasm
../scout.exec ../tests/helloworld.yaml
cd ..

Some notes

WARNING: Ewasm is not yet finalized. This toolchain is changing. There are experiments in the src directory.

NOTE ABOUT C++: Basic things like std::vector require memory managment, which most standard C++ libraries implement as system calls. Ewasm doesn't support system calls, so memory management must be done in Wasm. So far, a custom malloc() is in ewasm.h, but other things are still needed before we can try to compile against C++ standard library without requiring system calls. Anyway, std::vector may be expensive to use on-chain.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.