Skip to content

Commit

Permalink
Merge pull request #11 from bandprotocol/update-structure
Browse files Browse the repository at this point in the history
Update go version, project structure
  • Loading branch information
RogerKSI authored Aug 11, 2022
2 parents 0d55409 + 5b36779 commit 8404067
Show file tree
Hide file tree
Showing 29 changed files with 243 additions and 179 deletions.
1 change: 0 additions & 1 deletion .dockerignore

This file was deleted.

10 changes: 5 additions & 5 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ jobs:
- name: Install Go
uses: actions/setup-go@v2
with:
go-version: "^1.14.4"
go-version: '^1.18.3'

- name: Install Wabt (wat2wasm)
run: |
wget https://github.com/WebAssembly/wabt/releases/download/1.0.17/wabt-1.0.17-ubuntu.tar.gz
tar -zxf wabt-1.0.17-ubuntu.tar.gz
sudo cp wabt-1.0.17/bin/wat2wasm /usr/local/bin
wget https://github.com/WebAssembly/wabt/releases/download/1.0.29/wabt-1.0.29-ubuntu.tar.gz
tar -zxf wabt-1.0.29-ubuntu.tar.gz
sudo cp wabt-1.0.29/bin/wat2wasm /usr/local/bin
- name: Check go mod cache
uses: actions/cache@v1
Expand All @@ -46,7 +46,7 @@ jobs:
uses: actions-rs/cargo@v1
with:
command: test
args: --manifest-path Cargo.toml --all
args: --manifest-path libgo_owasm/Cargo.toml --all

- name: Run go tests
run: go test ./...
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
/target
.vscode

libgo_owasm/target
10 changes: 0 additions & 10 deletions Dockerfile.linux.dockerignore

This file was deleted.

10 changes: 0 additions & 10 deletions Dockerfile.osx.dockerignore

This file was deleted.

20 changes: 14 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,24 @@ USER_ID := $(shell id -u)
USER_GROUP = $(shell id -g)

docker-image-linux:
DOCKER_BUILDKIT=1 docker build .. -t owasm/go-ext-builder:$(DOCKER_TAG)-linux -f Dockerfile.linux
DOCKER_BUILDKIT=1 docker build .. -t owasm/go-ext-builder:$(DOCKER_TAG)-linux -f build/Dockerfile.linux

docker-image-osx:
DOCKER_BUILDKIT=1 docker build .. -t owasm/go-ext-builder:$(DOCKER_TAG)-osx -f Dockerfile.osx
DOCKER_BUILDKIT=1 docker build .. -t owasm/go-ext-builder:$(DOCKER_TAG)-osx -f build/Dockerfile.osx

docker-images: docker-image-linux docker-image-osx

release-linux:
rm -rf libgo_owasm/target/release
docker run --rm -u $(USER_ID):$(USER_GROUP) -v $(shell pwd):/code/go-owasm owasm/go-ext-builder:$(DOCKER_TAG)-linux

release-osx:
rm -rf libgo_owasm/target/release
docker run --rm -u $(USER_ID):$(USER_GROUP) -v $(shell pwd):/code/go-owasm owasm/go-ext-builder:$(DOCKER_TAG)-osx

# and use them to compile release builds
release:
rm -rf target/release
docker run --rm -u $(USER_ID):$(USER_GROUP) -v $(shell pwd)/..:/code owasm/go-ext-builder:$(DOCKER_TAG)-osx
rm -rf target/release
docker run --rm -u $(USER_ID):$(USER_GROUP) -v $(shell pwd)/..:/code owasm/go-ext-builder:$(DOCKER_TAG)-linux
rm -rf libgo_owasm/target/release
docker run --rm -u $(USER_ID):$(USER_GROUP) -v $(shell pwd):/code/go-owasm owasm/go-ext-builder:$(DOCKER_TAG)-linux
rm -rf libgo_owasm/target/release
docker run --rm -u $(USER_ID):$(USER_GROUP) -v $(shell pwd):/code/go-owasm owasm/go-ext-builder:$(DOCKER_TAG)-osx
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
## Generate cbindgen for go
## Generate bindings.h for go

````
cbindgen --config cbindgen.toml --output api/bindings.h
```
cd libgo_owasm
cargo build --release
```

## Build shared object library

Currently, we support only build on linux. Osx build will be available.

- run `make docker-images` to pre-built dependencies
- run `make release` to build current code to `libgo_owasm.so`
````
- run `make release` to build current code to `libgo_owasm.so` and `libgo_owasm.dylib`
49 changes: 24 additions & 25 deletions api/bindings.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* (c) 2020 Band protocol. Licensed under Apache-2.0 */

/* Generated with cbindgen:0.14.2 */
/* Generated with cbindgen:0.18.0 */

/* Warning, this file is autogenerated by cbindgen. Don't modify this manually. */

Expand Down Expand Up @@ -36,6 +36,10 @@ enum Error {
};
typedef int32_t Error;

typedef struct cache_t {

} cache_t;

/**
* A `span` is a lightweight struct used to refer to a section of memory. The memory
* section is not owned by the span, similar to C++'s std::span. The `span`'s creator is
Expand All @@ -56,42 +60,37 @@ typedef struct env_t {
} env_t;

typedef struct EnvDispatcher {
Error (*get_calldata)(env_t*, Span *calldata);
Error (*set_return_data)(env_t*, Span data);
int64_t (*get_ask_count)(env_t*);
int64_t (*get_min_count)(env_t*);
int64_t (*get_prepare_time)(env_t*);
Error (*get_execute_time)(env_t*, int64_t*);
Error (*get_ans_count)(env_t*, int64_t*);
Error (*ask_external_data)(env_t*, int64_t eid, int64_t did, Span data);
Error (*get_external_data_status)(env_t*, int64_t eid, int64_t vid, int64_t *status);
Error (*get_external_data)(env_t*, int64_t eid, int64_t vid, Span *data);
Error (*get_calldata)(struct env_t*, struct Span *calldata);
Error (*set_return_data)(struct env_t*, struct Span data);
int64_t (*get_ask_count)(struct env_t*);
int64_t (*get_min_count)(struct env_t*);
int64_t (*get_prepare_time)(struct env_t*);
Error (*get_execute_time)(struct env_t*, int64_t*);
Error (*get_ans_count)(struct env_t*, int64_t*);
Error (*ask_external_data)(struct env_t*, int64_t eid, int64_t did, struct Span data);
Error (*get_external_data_status)(struct env_t*, int64_t eid, int64_t vid, int64_t *status);
Error (*get_external_data)(struct env_t*, int64_t eid, int64_t vid, struct Span *data);
} EnvDispatcher;

typedef struct Env {
env_t *env;
EnvDispatcher dis;
struct env_t *env;
struct EnvDispatcher dis;
} Env;

typedef struct RunOutput {
uint32_t gas_used;
} RunOutput;

// cache
typedef struct cache_t {
struct cache_t *init_cache(uint32_t size);

} cache_t;
void release_cache(struct cache_t *cache);

Error do_compile(Span input, Span *output);
Error do_compile(struct Span input, struct Span *output);

Error do_run(cache_t *cache,
Span code,
Error do_run(struct cache_t *cache,
struct Span code,
uint32_t gas_limit,
int64_t span_size,
bool is_prepare,
Env env,
RunOutput *output);

cache_t *init_cache(uint32_t size);

void release_cache(cache_t *cache);
struct Env env,
struct RunOutput *output);
49 changes: 25 additions & 24 deletions api/lib_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,11 @@ func TestInvaildSignature(t *testing.T) {
wasm := wat2wasm([]byte(`(module
(func (param i64 i64 i32 i64)
(local $idx i32)
(set_local $idx (i32.const 0))
(local.set $idx (i32.const 0))
(block
(loop
(set_local $idx (get_local $idx) (i32.const 1) (i32.add) )
(br_if 0 (i32.lt_u (get_local $idx) (i32.const 10000)))
(local.set $idx (local.get $idx) (i32.const 1) (i32.add) )
(br_if 0 (i32.lt_u (local.get $idx) (i32.const 10000)))
)
))
(func)
Expand All @@ -110,11 +110,11 @@ func TestGasLimit(t *testing.T) {
(type (func (param i64 i64 i32 i64) (result i64)))
(func
(local $idx i32)
(set_local $idx (i32.const 0))
(local.set $idx (i32.const 0))
(block
(loop
(set_local $idx (get_local $idx) (i32.const 1) (i32.add) )
(br_if 0 (i32.lt_u (get_local $idx) (i32.const 10000)))
(local.set $idx (local.get $idx) (i32.const 1) (i32.add) )
(br_if 0 (i32.lt_u (local.get $idx) (i32.const 10000)))
)
))
(func)
Expand All @@ -123,6 +123,7 @@ func TestGasLimit(t *testing.T) {
(export "execute" (func 1)))
`))
code, err := vm.Compile(wasm, spanSize)
require.NoError(t, err)
output, err := vm.Prepare(code, 100000, 1024, NewMockEnv([]byte("")))
require.NoError(t, err)
require.Equal(t, RunOutput{GasUsed: 80004}, output)
Expand All @@ -139,11 +140,11 @@ func TestCompileErrorNoMemory(t *testing.T) {
(type (func (param i64 i64 i32 i64) (result i64)))
(func
(local $idx i32)
(set_local $idx (i32.const 0))
(local.set $idx (i32.const 0))
(block
(loop
(set_local $idx (get_local $idx) (i32.const 1) (i32.add) )
(br_if 0 (i32.lt_u (get_local $idx) (i32.const 10000)))
(local.set $idx (local.get $idx) (i32.const 1) (i32.add) )
(br_if 0 (i32.lt_u (local.get $idx) (i32.const 10000)))
)
))
(func)
Expand All @@ -165,11 +166,11 @@ func TestCompileErrorMinimumMemoryExceed(t *testing.T) {
(type (func (param i64 i64 i32 i64) (result i64)))
(func
(local $idx i32)
(set_local $idx (i32.const 0))
(local.set $idx (i32.const 0))
(block
(loop
(set_local $idx (get_local $idx) (i32.const 1) (i32.add) )
(br_if 0 (i32.lt_u (get_local $idx) (i32.const 10000)))
(local.set $idx (local.get $idx) (i32.const 1) (i32.add) )
(br_if 0 (i32.lt_u (local.get $idx) (i32.const 10000)))
)
))
(func)
Expand All @@ -184,11 +185,11 @@ func TestCompileErrorMinimumMemoryExceed(t *testing.T) {
(type (func (param i64 i64 i32 i64) (result i64)))
(func
(local $idx i32)
(set_local $idx (i32.const 0))
(local.set $idx (i32.const 0))
(block
(loop
(set_local $idx (get_local $idx) (i32.const 1) (i32.add) )
(br_if 0 (i32.lt_u (get_local $idx) (i32.const 10000)))
(local.set $idx (local.get $idx) (i32.const 1) (i32.add) )
(br_if 0 (i32.lt_u (local.get $idx) (i32.const 10000)))
)
))
(func)
Expand All @@ -210,11 +211,11 @@ func TestCompileErrorSetMaximumMemory(t *testing.T) {
(type (func (param i64 i64 i32 i64) (result i64)))
(func
(local $idx i32)
(set_local $idx (i32.const 0))
(local.set $idx (i32.const 0))
(block
(loop
(set_local $idx (get_local $idx) (i32.const 1) (i32.add) )
(br_if 0 (i32.lt_u (get_local $idx) (i32.const 10000)))
(local.set $idx (local.get $idx) (i32.const 1) (i32.add) )
(br_if 0 (i32.lt_u (local.get $idx) (i32.const 10000)))
)
))
(func)
Expand All @@ -238,11 +239,11 @@ func TestCompileErrorCheckWasmImports(t *testing.T) {
(import "env" "beeb" (func (type 0)))
(func
(local $idx i32)
(set_local $idx (i32.const 0))
(local.set $idx (i32.const 0))
(block
(loop
(set_local $idx (get_local $idx) (i32.const 1) (i32.add) )
(br_if 0 (i32.lt_u (get_local $idx) (i32.const 1000000000)))
(local.set $idx (local.get $idx) (i32.const 1) (i32.add) )
(br_if 0 (i32.lt_u (local.get $idx) (i32.const 1000000000)))
)
)
)
Expand All @@ -267,11 +268,11 @@ func TestCompileErrorCheckWasmExports(t *testing.T) {
(import "env" "ask_external_data" (func (type 0)))
(func
(local $idx i32)
(set_local $idx (i32.const 0))
(local.set $idx (i32.const 0))
(block
(loop
(set_local $idx (get_local $idx) (i32.const 1) (i32.add) )
(br_if 0 (i32.lt_u (get_local $idx) (i32.const 1000000000)))
(local.set $idx (local.get $idx) (i32.const 1) (i32.add) )
(br_if 0 (i32.lt_u (local.get $idx) (i32.const 1000000000)))
)
)
)
Expand Down
Binary file modified api/libgo_owasm.dylib
Binary file not shown.
Binary file modified api/libgo_owasm.so
Binary file not shown.
4 changes: 1 addition & 3 deletions Dockerfile.linux → build/Dockerfile.linux
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,11 @@ RUN url="https://static.rust-lang.org/rustup/dist/x86_64-unknown-linux-gnu/rustu

# PRE-FETCH MANY DEPS
WORKDIR /scratch
COPY owasm /scratch/owasm
COPY go-owasm /scratch/go-owasm
RUN cd go-owasm && cargo fetch
RUN cd go-owasm/libgo_owasm && cargo fetch
# allow non-root user to download more deps later
RUN chmod -R 777 /usr/local/cargo


## COPY BUILD SCRIPTS

WORKDIR /code
Expand Down
3 changes: 1 addition & 2 deletions Dockerfile.osx → build/Dockerfile.osx
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,8 @@ RUN chmod -R +rx /opt/osxcross/target/bin

# PRE-FETCH MANY DEPS
WORKDIR /scratch
COPY owasm /scratch/owasm
COPY go-owasm /scratch/go-owasm
RUN cd go-owasm && cargo fetch
RUN cd go-owasm/libgo_owasm && cargo fetch
# allow non-root user to download more deps later
RUN chmod -R 777 /usr/local/cargo
RUN chmod -R 777 /usr/local/rustup
Expand Down
4 changes: 2 additions & 2 deletions build/build_linux.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/bin/bash

cd go-owasm
cd go-owasm/libgo_owasm
cargo build --release
cp target/release/deps/libgo_owasm.so api
cp target/release/deps/libgo_owasm.so ./../api
4 changes: 2 additions & 2 deletions build/build_osx.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export LIBZ_SYS_STATIC=1
export CC=o64-clang
export CXX=o64-clang++

cd go-owasm
cd go-owasm/libgo_owasm
rustup target add x86_64-apple-darwin
cargo build --release --target x86_64-apple-darwin
cp target/x86_64-apple-darwin/release/deps/libgo_owasm.dylib api
cp target/x86_64-apple-darwin/release/deps/libgo_owasm.dylib ./../api
8 changes: 7 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
module github.com/bandprotocol/go-owasm

go 1.13
go 1.18

require github.com/stretchr/testify v1.6.1

require (
github.com/davecgh/go-spew v1.1.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
)
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Loading

0 comments on commit 8404067

Please sign in to comment.