CosmWasm / cosmwasm-go

Enabling CosmWasm smart contracts in Go using TinyGo
Apache License 2.0
29 stars 8 forks source link

WIP:Build Proof of Concept(POC) of contract demo on go language #2

Closed KamiD closed 4 years ago

KamiD commented 4 years ago

Summary

we must build a poc of contract which dev on go language, it need accord with required exports :

make sure that Demo can be loaded and compiled without any errors

Proposal


For Admin Use

ethanfrey commented 4 years ago

We will release 0.9 with cosmwasm_vm_version_2 later this week. It has quite a few breaking changes and I would wait for that to begin work on this issue

ethanfrey commented 4 years ago

I would also say that this demo doesn't need storage or anything else. For an mvp, it should just parse the input message (json) , do some math on it and return a result (json).

This ensures we can call into it and that dynamic data is properly transfered

KamiD commented 4 years ago

Hi ethan, : ) that's great news, so that means we have a stable version of api ?
Yeah, I agree that just for an MVP. so, my progress : now, the poc can be loaded and initialized by cosmwasm-vm correctly .
but also, some problem occurred during building, I just do some changes in my local repo of cosmwasm-vm :

ethanfrey commented 4 years ago

Nice update.

Can you post your code here?

I will see about renaming init->initialize. Time is now or never

ethanfrey commented 4 years ago

As to the extra imports and such, I looked at tiny go and I think the approach is to change the defaults imports they compile with. This was some months ago, but I can take a look again

webmaster128 commented 4 years ago

in default, tinyGo to wasm will add import_table which named wasi_unstable during building, when cosmwasm-vm try to compile target byte-code to instance, it failed. so i add code in function from_module as follow:

There must be a way to disable WASI in TinyGo. If this is not the case, then this is a show stopper. Unfortunately I did not find any kind of flag for that and https://github.com/tinygo-org/tinygo/blob/master/targets/wasm.json does not look good as well.

ethanfrey commented 4 years ago

This is doable, but adding a custom compile option to tinygo.

I did this some months ago.... need to look for the code

ethanfrey commented 4 years ago

Okay, first of all the wasmi stuff....

I hacked around with tinygo some months ago to strip out excess exports. You can see what I did here: https://github.com/confio/tinygo/commit/95a4707853f8c8522f6a51db14c8306727a3776f

(Also trimming down the docker file for faster wasm builds)

Neither should be committed, but show some techniques. We can maintain a separate trimmed docker file rather than editing the master one. Also, we could add a separate runtime for cosmwasm, I would copy runtime_wasm and then edit.

ethanfrey commented 4 years ago

Okay, I have some code up on https://github.com/ethanfrey/tinygo-demo which shows my build process and getting a simple code exporting init (using go:exports to rename it). This is not a contract, just a test of the build tooling.

ethanfrey commented 4 years ago

I have now patched https://github.com/confio/tinygo/tree/cosmwasm-v2

It is up to the most recent release (0.13.1) with a smaller wasm-only Dockerfile (for quick test turnaround) and a special cosmwasm mode for wasm code that doesn't require any imports

Please check out the README in https://github.com/ethanfrey/tinygo-demo (which includes building Dockerfile.wasm from the linked repo and compiling a sample contract). You can see how we:

I would suggest using that image for all CosmWasm-related go contracts and making a PR there if necessary. When we are happy with it, I would like to upstream this.

I have also pushed cosmwasm/tinygo:latest to DockerHub, so no need to build locally, if you have docker installed, just try make build-cosmwasm

ethanfrey commented 4 years ago

@KamiD can you push your code to this repo so I can review/debug it?

This is still a private repo (open to your team, Simon and I) so it is not like publishing to the world, and I think I can help get a few key pieces working.

KamiD commented 4 years ago

Great !, I will try it now~ The code will uploaded in a few hours

KamiD commented 4 years ago

Hi Ethan ~ I used your code and docker image to test my code, every thing looks good, so I roll back local cosmwasm-vm repo to v0.8.0 Also i fixed get_region failure, very powerful support~~, I will upload my code sooner , thanks for your help !

KamiD commented 4 years ago

Hi Ethan, I upload my test code in this repo~, its working imperfect, only testing on init, next working are in progress~

ethanfrey commented 4 years ago

I took a look. Interesting stuff there with all the mallocs. I made a simple update to the makefile so I can view the generated wasm better.

I also noted that go test doesn't run (only the tinygo compile). Not a problem at this stage, but odd. (And I modified the paths with go mod and saw tinygo complained... I'm sure you spent some time debugging this).

$ go test ./src/
src/lib.go:5:2: local import "../std" in non-local package
ethanfrey commented 4 years ago

If you could make a (draft) PR with future work on this, it is easier to comment on the code.

This is a good start to have a demo on master, but I am happy to review PRs as they come in. Also, it would be cool if you added step-by-step testing procedures with cosmwasm-simulate in the readme. I already see the power of such tool :smile:

KamiD commented 4 years ago

If you could make a (draft) PR with future work on this, it is easier to comment on the code.

This is a good start to have a demo on master, but I am happy to review PRs as they come in. Also, it would be cool if you added step-by-step testing procedures with cosmwasm-simulate in the readme. I already see the power of such tool 😄

Sure, I will update Readme later~ also, the next update will pushing by PR for more easier to review~

ethanfrey commented 4 years ago

:+1: