evmole icon indicating copy to clipboard operation
evmole copied to clipboard

Add go bindings via C static library

Open ngotchac opened this issue 9 months ago • 2 comments

Might fix https://github.com/cdump/evmole/issues/5 ?

This adds the necessary bits on the Rust part to expose a very simple C library, which only exposes the evmole_contract_info method with a EvmoleContractInfoOptions struct for options. It reads the bytecode as a hex-encoded string, and outputs the result JSON-encoded.

On the go part, it links via CGO the built static libraries files, and wraps the C methods in a usable API, unmarshaling the JSON result into proper Go structs.

A test has been added which simply checks that it works correctly:

cd go
go test -count=1 -v ./test

I added the prebuilt static libraries, built via cargo-zigbuild so I could compile them for MacOS and Windows as well (I only have a Linux machine). I added the commands to a Makefile. I could only test for Linux on amd64.

Regarding the building of the static libraries, I'm not sure what the best approach is. They need to be comited to the Git repo (AFAIK), since Go is cloning it when adding new packages; there's no post-install step that could fetch them from releases from example. I don't know how it could be done in the CI, unless it commits some artifacts in the build process? Another solution might be to release the libraries in this repo, and have another one for the go version that commits those files?

ngotchac avatar Apr 06 '25 23:04 ngotchac

I took a look at two of your approaches and I have to say, I like https://github.com/cdump/evmole/pull/10 much more. It seems to have more pros than cons compared to the current cgo variant.

I tried updating your https://github.com/cdump/evmole/pull/10 to the new API with contract_info, and it looks like it's working. I'll probably add this to the backlog and include it here.

Thanks for showing two different approaches - it really helps to understand the pros and cons of each one.

cdump avatar Apr 25 '25 12:04 cdump

I actually found that using the C-bindings was much simpler than I expected. My only gripe is that the static libs are quite big, and I'm not sure whether this could be improved. I tried adding no_std capibilities to evmole, but the size didn't reduce by much... I'll let you choose though, let me know if/how I can help :)

ngotchac avatar Apr 25 '25 20:04 ngotchac