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 a4f2b11
Show file tree
Hide file tree
Showing 13 changed files with 128 additions and 64 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
71 changes: 32 additions & 39 deletions .github/workflows/docker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,40 @@ 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

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

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

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

- name: Log in to the Container registry
uses: docker/login-action@v3
Expand All @@ -29,20 +45,17 @@ jobs:
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: Install solc
run: .github/scripts/install_solc.sh

- name: Fetch third-party dependencies
- name: Set up Docker builder
run: |
make fetch-thirdparty
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

- name: Setup Yarn cache - submodule "thirdparty/account-abstraction"
uses: actions/setup-node@v3
with:
Expand All @@ -57,23 +70,3 @@ jobs:
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: 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 }}
10 changes: 10 additions & 0 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,$(GIT_TAG),$(GIT_TAG))

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):$(1) \
--tag $(DOCKER_IMAGE_NAME):$(2) \
--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
2 changes: 1 addition & 1 deletion crates/p2p/src/rpc/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ pub struct ProtocolId {
/// The encoding of the RPC.
pub encoding: Encoding,

///
/// The protocol id.
protocol_id: String,
}

Expand Down
2 changes: 1 addition & 1 deletion crates/rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ ethers = { workspace = true }

# rpc
hyper = { version = "0.14.20" }
hyper-tls = { version = "0.5.0" }
hyper-tls = { version = "0.5.0", features = ["vendored"] }
jsonrpsee = { workspace = true }
tower = { version = "0.4.13" }
tower-http = { version = "0.4.0", features = ["cors"] }
Expand Down
2 changes: 1 addition & 1 deletion rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
[toolchain]
channel = "1.76.0"
channel = "1.78.0"

0 comments on commit a4f2b11

Please sign in to comment.