Skip to content

Commit

Permalink
Add a flag to make lighthouse portable across machines (#1423)
Browse files Browse the repository at this point in the history
## Issue Addressed

Closes #1395

## Proposed Changes

* Add a feature to `lighthouse` and `lcli` called `portable` which enables the `portable` feature on our fork of BLST. This feature turns off the `-march=native` C compiler flag that produces binaries highly targeted to the host CPU's instruction set.
* Tweak the `Makefile` so that when the `PORTABLE` environment variable is set to `true`, it compiles with this feature.
* Temporarily enable `PORTABLE=true` in the Docker build so that the image on Docker Hub is portable. Eventually I think we should enable `PORTABLE=true` _only on Docker Hub_, so that users building locally can take advantage of the tasty compiler magic. This seems to be possible by setting a Docker Hub environment variable: https://docs.docker.com/docker-hub/builds/#environment-variables-for-builds

## Additional Info

Tested by compiling on a very new CPU (Intel Core i7-8550U) and copying the binary to a very old CPU (Intel Core i3 530). Before the portability fix, this produced the SIGILL crash described in #1395, and after the fix, it worked smoothly.

I'm in the process of testing the Docker build and running some benches to confirm that the performance penalty isn't too severe.
  • Loading branch information
michaelsproul committed Jul 31, 2020
1 parent 2ede9ca commit 7d8acc2
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 6 deletions.
3 changes: 2 additions & 1 deletion Cargo.lock

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

6 changes: 4 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
FROM rust:1.44.1 AS builder
FROM rust:1.45.1 AS builder
RUN apt-get update && apt-get install -y cmake
COPY . lighthouse
ARG PORTABLE
ENV PORTABLE $PORTABLE
RUN cd lighthouse && make
RUN cd lighthouse && cargo install --path lcli --locked
RUN cd lighthouse && make install-lcli

FROM debian:buster-slim
RUN apt-get update && apt-get install -y --no-install-recommends \
Expand Down
8 changes: 8 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,19 @@ STATE_TRANSITION_VECTORS = "testing/state_transition_vectors"
#
# Binaries will most likely be found in `./target/release`
install:
ifeq ($(PORTABLE), true)
cargo install --path lighthouse --force --locked --features portable
else
cargo install --path lighthouse --force --locked
endif

# Builds the lcli binary in release (optimized).
install-lcli:
ifeq ($(PORTABLE), true)
cargo install --path lcli --force --locked --features portable
else
cargo install --path lcli --force --locked
endif

# Runs the full workspace tests in **release**, without downloading any additional
# test vectors.
Expand Down
4 changes: 4 additions & 0 deletions book/src/docker.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ $ docker run sigp/lighthouse lighthouse --help
> Note: when you're running the Docker Hub image you're relying upon a
> pre-built binary instead of building from source.
> Note: due to the Docker Hub image being compiled to work on arbitrary machines, it isn't as highly
> optimized as an image built from source. We're working to improve this, but for now if you want
> the absolute best performance, please build the image yourself.
### Building the Docker Image

To build the image from source, navigate to
Expand Down
3 changes: 2 additions & 1 deletion crypto/bls/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ eth2_hashing = "0.1.0"
ethereum-types = "0.9.1"
arbitrary = { version = "0.4.4", features = ["derive"], optional = true }
zeroize = { version = "1.0.0", features = ["zeroize_derive"] }
blst = { git = "https://github.com/sigp/blst.git", rev = "968c846a2dc46e836e407bbdbac1a38a597ebc46" }
blst = { git = "https://github.com/sigp/blst.git", rev = "dad1ad0cd22861e5773bee177bee4e1684792605" }

[features]
default = ["supranational"]
fake_crypto = []
milagro = []
supranational = []
supranational-portable = ["supranational", "blst/portable"]
4 changes: 4 additions & 0 deletions hooks/build
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash

# Build hook to run on Docker Hub to ensure that the image is built with `PORTABLE=true`.
docker build --build-arg PORTABLE=true -f $DOCKERFILE_PATH -t $IMAGE_NAME .
4 changes: 3 additions & 1 deletion lcli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ version = "0.2.0"
authors = ["Paul Hauner <[email protected]>"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[features]
portable = ["bls/supranational-portable"]

[dependencies]
bls = { path = "../crypto/bls" }
clap = "2.33.0"
hex = "0.4.2"
log = "0.4.8"
Expand Down
6 changes: 5 additions & 1 deletion lighthouse/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@ authors = ["Sigma Prime <[email protected]>"]
edition = "2018"

[features]
write_ssz_files = ["beacon_node/write_ssz_files"] # Writes debugging .ssz files to /tmp during block processing.
# Writes debugging .ssz files to /tmp during block processing.
write_ssz_files = ["beacon_node/write_ssz_files"]
# Compiles the BLS crypto code so that the binary is portable across machines.
portable = ["bls/supranational-portable"]

[dependencies]
beacon_node = { "path" = "../beacon_node" }
tokio = "0.2.21"
slog = { version = "2.5.2", features = ["max_level_trace"] }
sloggers = "1.0.0"
types = { "path" = "../consensus/types" }
bls = { path = "../crypto/bls" }
clap = "2.33.0"
env_logger = "0.7.1"
logging = { path = "../common/logging" }
Expand Down

0 comments on commit 7d8acc2

Please sign in to comment.