Skip to content

Commit

Permalink
feat: introduce cross for cross-platform docker image
Browse files Browse the repository at this point in the history
  • Loading branch information
Vid201 committed Jun 6, 2024
1 parent fcbc70f commit 356ea8a
Show file tree
Hide file tree
Showing 15 changed files with 149 additions and 74 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/book.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ name: book and docs

jobs:
build:
name: build book and docs
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4

- uses: dtolnay/rust-toolchain@nightly

- name: Install mdbook
run: |
mkdir mdbook
Expand Down Expand Up @@ -96,6 +98,7 @@ jobs:
if-no-files-found: error

deploy:
name: deploy book and docs
runs-on: ubuntu-latest
needs: [build]

Expand Down
8 changes: 4 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ name: ci

jobs:
build_and_test:
name: Silius - ERC-4337 bundler in Rust
name: build and test Silius
runs-on: ubuntu-latest
steps:
# github action worker got about 14GB available space for building which is not enough
Expand All @@ -22,8 +22,8 @@ jobs:
- uses: actions/checkout@v4

- name: Setup Rust toolchain (stable)
uses: dtolnay/rust-toolchain@1.76.0
- name: Setup Rust toolchain
uses: dtolnay/rust-toolchain@1.78.0
with:
components: clippy

Expand Down Expand Up @@ -94,7 +94,7 @@ jobs:
make lint
bundler_spec_tests:
name: Run bundler spec tests
name: bundler spec tests
runs-on: ubuntu-latest
needs: build_and_test
steps:
Expand Down
72 changes: 35 additions & 37 deletions .github/workflows/docker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,77 +3,75 @@ on:
branches:
- main

name: Create and publish a Docker image
name: docker

env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}

jobs:
build_and_push_docker_image:
build:
name: build and push docker image
runs-on: ubuntu-latest
permissions:
contents: read
packages: write

contents: read
steps:
- name: Checkout repository
uses: actions/checkout@v4
# github action worker got about 14GB available space for building which is not enough
# remove some unused binary in the image to get more spaces
- name: Make more space to compilation
run: |
sudo rm -rf /usr/share/dotnet
sudo rm -rf /opt/ghc
sudo rm -rf "/usr/local/share/boost"
sudo rm -rf "$AGENT_TOOLSDIRECTORY"
- uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Setup Rust toolchain
uses: dtolnay/[email protected]

- name: Setup Rust cache
uses: Swatinem/rust-cache@v2
with:
cache-on-failure: true

- uses: taiki-e/install-action@cross

- name: Log in to the Container registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Get Git Tag
run: echo "tags=$(git describe --tags)" >> $GITHUB_OUTPUT
id: tag

- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}

- name: Fetch third-party dependencies
run: |
make fetch-thirdparty
- name: Setup Yarn cache - submodule "thirdparty/account-abstraction"
uses: actions/setup-node@v3
with:
node-version: '16.17'
cache: 'yarn'
cache-dependency-path: crates/contracts/thirdparty/account-abstraction

- name: Setup Yarn cache - submodule "thirdparty/bundler"
uses: actions/setup-node@v3
with:
node-version: '16.17'
cache: 'yarn'
cache-dependency-path: tests/thirdparty/bundler

- name: Setup third-party dependencies
run: |
make setup-thirdparty

- name: Setup Docker Buildx
uses: docker/setup-buildx-action@v3
with:
platforms: linux/amd64,linux/arm64
- name: Install solc
run: .github/scripts/install_solc.sh

- name: Build and push Docker image
uses: docker/build-push-action@v5
with:
context: .
push: true
platforms: linux/amd64,linux/arm64
provenance: false
cache-from: type=gha
cache-to: type=gha,mode=max
tags: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ steps.tag.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
- name: Set up Docker builder
run: |
docker run --privileged --rm tonistiigi/binfmt --install arm64,amd64
docker buildx create --use --name cross-builder
- name: Build and push Silius image
run: make docker-build-push
26 changes: 18 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ authors = ["Vid Kersic <[email protected]>"]
edition = "2021"
license = "MIT OR Apache-2.0"
repository = "https://github.com/silius-rs/silius"
rust-version = "1.76.0"
rust-version = "1.78.0"
keywords = [
"silius",
"primitives",
Expand Down
10 changes: 10 additions & 0 deletions Cross.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[build]
pre-build = [
"apt-get update && apt-get -y upgrade && apt-get install -y wget pkg-config llvm-dev libclang-6.0-dev clang-6.0 libssl-dev",
"wget -c https://github.com/ethereum/solidity/releases/download/v0.8.20/solc-static-linux && mv solc-static-linux /usr/local/bin/solc && chmod a+x /usr/local/bin/solc"
]

[build.env]
passthrough = [
"JEMALLOC_SYS_WITH_LG_PAGE",
]
30 changes: 15 additions & 15 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
FROM lukemathwalker/cargo-chef:latest-rust-1.76.0 AS chef
FROM lukemathwalker/cargo-chef:latest-rust-1.78.0 AS chef
WORKDIR /app

LABEL org.opencontainers.image.source=https://github.com/silius-rs/silius
LABEL org.opencontainers.image.description="Silius - ERC-4337 (Account Abstraction) bundler implementation in Rust."
LABEL org.opencontainers.image.licenses="MIT OR Apache-2.0"

# Install system dependencies
RUN apt-get update && apt-get -y upgrade && apt-get install -y wget pkg-config libclang-dev libssl-dev

# Install solc
RUN wget -c "https://github.com/ethereum/solidity/releases/download/v0.8.20/solc-static-linux"
RUN mv solc-static-linux /usr/local/bin/solc
RUN chmod a+x /usr/local/bin/solc

# build cargo-chef plan
FROM chef AS planner
COPY . .
Expand All @@ -17,30 +25,22 @@ COPY --from=planner /app/recipe.json recipe.json
ARG BUILD_PROFILE=release
ENV BUILD_PROFILE $BUILD_PROFILE

# Set the build target platform
ARG TARGETPLATFORM

# Install system dependencies
RUN apt-get update && apt-get -y upgrade && apt-get install -y pkg-config libclang-dev libssl-dev

# Install solc
RUN wget -c "https://github.com/ethereum/solidity/releases/download/v0.8.20/solc-static-linux"
RUN mv solc-static-linux /usr/local/bin/solc
RUN chmod a+x /usr/local/bin/solc
# Extra Cargo features
ARG FEATURES=""
ENV FEATURES $FEATURES

# Builds dependencies
RUN cargo chef cook --profile $BUILD_PROFILE --recipe-path recipe.json
RUN cargo chef cook --profile $BUILD_PROFILE --features "$FEATURES" --recipe-path recipe.json

# Build application
COPY . .
RUN if [ "$TARGETPLATFORM" = "linux/arm64" ]; then JEMALLOC_SYS_WITH_LG_PAGE=16 ; fi && \
cargo build --profile $BUILD_PROFILE --locked
RUN cargo build --profile $BUILD_PROFILE --features "$FEATURES" --locked

# Copy application
RUN cp /app/target/$BUILD_PROFILE/silius /app/silius

# Use ubuntu as a runtime image
FROM --platform=$TARGETPLATFORM ubuntu AS runtime
FROM ubuntu:22.04 AS runtime

# Create data folder
RUN mkdir -p /data/silius
Expand Down
13 changes: 13 additions & 0 deletions Dockerfile.cross
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
FROM --platform=$TARGETPLATFORM ubuntu:22.04

LABEL org.opencontainers.image.source=https://github.com/silius-rs/silius
LABEL org.opencontainers.image.description="Silius - ERC-4337 (Account Abstraction) bundler implementation in Rust."
LABEL org.opencontainers.image.licenses="MIT OR Apache-2.0"

ARG TARGETARCH

COPY ./dist/bin/$TARGETARCH/silius /usr/local/bin/silius

EXPOSE 3000 3001

ENTRYPOINT ["/usr/local/bin/silius"]
35 changes: 35 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,38 @@ clean:
cd crates/contracts/thirdparty/account-abstraction && yarn clean && cd ../..
cd tests/thirdparty/bundler && yarn clear && cd ../..
cargo clean

# Heavily inspired by Lighthouse: https://github.com/sigp/lighthouse/blob/693886b94176faa4cb450f024696cb69cda2fe58/Makefile
# docker image cross-platform build

GIT_TAG = $(shell git describe --tags)
BIN_DIR = dist/bin
BUILD_PATH = target
PROFILE = release

DOCKER_IMAGE_NAME = ghcr.io/silius-rs/silius

docker-build-push:
$(call docker_build_push)

define docker_build_push
$(MAKE) fetch-thirdparty
$(MAKE) setup-thirdparty

RUSTFLAGS="-C link-arg=-lgcc -Clink-arg=-static-libgcc" \
cross build --target x86_64-unknown-linux-gnu --features "" --profile "$(PROFILE)"
mkdir -p $(BIN_DIR)/amd64
cp $(BUILD_PATH)/x86_64-unknown-linux-gnu/$(PROFILE)/silius $(BIN_DIR)/amd64/silius

RUSTFLAGS="-C link-arg=-lgcc -Clink-arg=-static-libgcc" JEMALLOC_SYS_WITH_LG_PAGE=16 \
cross build --target aarch64-unknown-linux-gnu --features "" --profile "$(PROFILE)"
mkdir -p $(BIN_DIR)/arm64
cp $(BUILD_PATH)/aarch64-unknown-linux-gnu/$(PROFILE)/silius $(BIN_DIR)/arm64/silius

docker buildx build --file ./Dockerfile.cross . \
--platform linux/amd64,linux/arm64 \
--tag $(DOCKER_IMAGE_NAME):$(GIT_TAG) \
--tag $(DOCKER_IMAGE_NAME):$(GIT_TAG) \
--provenance=false \
--push
endef
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ For more information: <https://hackmd.io/@Vid201/aa-bundler-rust>

**Prerequisites:**

Rust version: 1.76.0
Rust version: 1.78.0

1. `libclang-dev`, `pkg-config` and `libssl-dev` on Debian/Ubuntu.
2. Ethereum execution client JSON-RPC API with enabled [`debug_traceCall`](https://geth.ethereum.org/docs/interacting-with-geth/rpc/ns-debug#debug_tracecall). For production, you can use [Geth](https://github.com/ethereum/go-ethereum) or [Erigon](https://github.com/ledgerwatch/erigon). For testing, we are using Geth dev mode (tested with [v1.12.0](https://github.com/ethereum/go-ethereum/releases/tag/v1.12.0)); so you need to install [Geth](https://geth.ethereum.org/docs/getting-started/installing-geth) for running tests.
Expand Down
10 changes: 8 additions & 2 deletions crates/grpc/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,14 @@ fn config() -> prost_build::Config {
fn make_protos(protos: &[&str]) {
let out_dir = PathBuf::from(env::var("OUT_DIR").expect("OUT_DIT not set"));
tonic_build::configure()
.server_mod_attribute("uopool", r#"#[allow(clippy::unwrap_used)]"#)
.server_mod_attribute("bundler", r#"#[allow(clippy::unwrap_used)]"#)
.server_mod_attribute(
"uopool",
r#"#[allow(clippy::unwrap_used, clippy::mixed_attributes_style)]"#,
)
.server_mod_attribute(
"bundler",
r#"#[allow(clippy::unwrap_used, clippy::mixed_attributes_style)]"#,
)
.file_descriptor_set_path(out_dir.join("descriptor.bin"))
.compile_with_config(config(), protos, &["./src/protos"])
.expect("Failed to compile protos.");
Expand Down
Loading

0 comments on commit 356ea8a

Please sign in to comment.