entropyxyz / programs

Source, toolchain, and examples for using programs on Entropy
https://docs.entropy.xyz/concepts/programs/
GNU Affero General Public License v3.0
19 stars 3 forks source link

Dockerfile and cargo-generate template for building programs #57

Closed ameba23 closed 6 months ago

ameba23 commented 7 months ago

This adds two things:

If this is merged, it will be possible to get started building a new program with cargo generate entropyxyz/programs. To try it out right now, you can do cargo generate --path <local path of this repo> This assumes cargo-generate is installed: cargo install cargo-generate.

Developers can then build their program 'deterministically' with docker build --output=binary-dir ., which will build the program and put the .wasm file in ./binary-dir.

The name of the docker image used to build the program is included as additional metadata in the program's Cargo.toml file. This means anyone with access to the program's source code can independently verify the program binary (or hash) by building it with the same image.

To build the examples in this repo using docker you can also do: docker build --build-arg PACKAGE=<example name> --output=example-binary . from the top level of this repo.

vercel[bot] commented 7 months ago

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
programs ❌ Failed (Inspect) Feb 14, 2024 10:28am
jakehemmerle commented 7 months ago

I like this idea a lot.

Can you add the instructions for use to the readme?

ameba23 commented 7 months ago

Can you add the instructions for use to the readme?

will do. but this still needs some work first.

Currently, it is outputting the binary as part of the docker image build process. This means every time we compile a program we have to go through the whole process of installing rust and compiling wasm-tools etc. I am gonna change it to be able to build programs when running the container.

ameba23 commented 7 months ago

Im a bit stuck on this. I've tried making a separate docker image with everything already installed, and using it as the base of this script.

That is, i have a Dockerfile which looks like this:

FROM rust:1.75 AS base

RUN rustup target add wasm32-unknown-unknown
RUN rustup target add wasm32-wasi
RUN cargo install cargo-component --version 0.2.0
RUN cargo install wasm-tools

and i have published that to dockerhub and using it as the base image to build a program like this:

FROM pegpegpeg/build-entropy-programs:latest AS base
ARG PACKAGE=template-barebones

WORKDIR /usr/src/programs
COPY . .

RUN cargo component build --release -p $PACKAGE --target wasm32-unknown-unknown

FROM scratch AS binary
COPY --from=base /usr/src/programs/target/wasm32-unknown-unknown/release/*.wasm /

But for some reason it still seems to install the rust toolchain every time i build a program, and building the example programs takes almost 2 minutes. Its as if cargo component is internally installing everything a second time.

jakehemmerle commented 7 months ago

@ameba23 if it's trying to download extra stuff, did you try throwing in a cargo component check or something into the image generation script to see that triggers it? just an idea. I'm not sure if it installs extra things

ameba23 commented 6 months ago

@ameba23 if it's trying to download extra stuff, did you try throwing in a cargo component check or something into the image generation script to see that triggers it? just an idea. I'm not sure if it installs extra things

In the end it seems this problem goes away if i build a 'standalone' program rather than one of the examples in this repo. I think this might be because of there being a rust-toolchain.toml file here - but still strange because i am using the latest stable version of rust in the dockerfile. It could also be related to there being a Cargo-component.lock file.

Either way, i now have one dockerfile for building the examples here, and one for building standalone programs, which can be started by doing cargo generate entropyxyz/programs.