Skip to content

Commit 194b0c5

Browse files
Make test wasms build more consistently (#1576)
### What Remove the rssdkver (Rust SDK Version) meta entry from the builds of the test wasms in this repository. And build the test wasms once with the minimum supported rust version (msrv) and use that build for test runs. ### Why When building contracts with the soroban-sdk imported, the Rust SDK Version is embedded in a meta entry. For the test wasms in this repository this makes their builds unstable, and inconsistent. Every commit on the repository results in a slightly different build given that the revision is included and is forever changing. This makes it challenging to use the wasms in some places where the hash or the raw bytes get captured, such as test snapshots, and soon expanded generated code (#1572). The simplest way to remove this problem is to remove the meta entry just when building the local test vectors. Each version of rust can also cause differences in the wasm build, so for tests, only testing against the wasms built with the msrv keeps the wasm binaries stable for the longest period of time. This change required some refactoring of the ci process, to separate building and testing. That refactor also moved us away from using a separate set of commands in ci vs locally, now ci just runs make commands. The local Makefile is updated to build the wasms with the msrv as well for convenience.
1 parent dbae94d commit 194b0c5

File tree

16 files changed

+622
-87
lines changed

16 files changed

+622
-87
lines changed

.github/workflows/rust.yml

Lines changed: 72 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ concurrency:
1010
group: ${{ github.workflow }}-${{ github.ref_protected == 'true' && github.sha || github.ref }}-{{ github.event_name }}
1111
cancel-in-progress: true
1212

13+
env:
14+
ARTIFACT_RETENTION_DAYS_FOR_TEST_WASMS: 7
15+
1316
defaults:
1417
run:
1518
shell: bash
@@ -18,7 +21,7 @@ jobs:
1821

1922
complete:
2023
if: always()
21-
needs: [fmt, check-git-rev-deps, semver-checks, build-and-test, build-fuzz, docs, readme, migration-docs]
24+
needs: [fmt, check-git-rev-deps, semver-checks, build, test, build-fuzz, docs, readme, migration-docs]
2225
runs-on: ubuntu-latest
2326
steps:
2427
- if: contains(needs.*.result, 'failure') || contains(needs.*.result, 'cancelled')
@@ -57,7 +60,7 @@ jobs:
5760
steps:
5861
- uses: actions/checkout@v3
5962
- run: rustup update
60-
- uses: stellar/binaries@v41
63+
- uses: stellar/binaries@v45
6164
with:
6265
name: cargo-semver-checks
6366
version: 0.44.0
@@ -67,26 +70,68 @@ jobs:
6770
--exclude soroban-token-spec
6871
--exclude stellar-asset-spec
6972
70-
build-and-test:
73+
build:
7174
strategy:
7275
fail-fast: false
7376
matrix:
7477
rust: [msrv, latest]
7578
sys:
7679
- os: ubuntu-latest
7780
target: x86_64-unknown-linux-gnu
81+
- os: ubuntu-24.04-arm
82+
target: aarch64-unknown-linux-gnu
83+
- os: macos-15-intel
84+
target: x86_64-apple-darwin
85+
- os: macos-latest
86+
target: aarch64-apple-darwin
87+
- os: windows-latest
88+
target: x86_64-pc-windows-msvc
89+
runs-on: ${{ matrix.sys.os }}
90+
steps:
91+
- uses: actions/checkout@v3
92+
- uses: stellar/actions/rust-cache@main
93+
- name: Use the minimum supported Rust version
94+
if: matrix.rust == 'msrv'
95+
run: |
96+
msrv="$(cargo metadata --format-version 1 --no-deps | jq -r '.packages | map(.rust_version) | map(values) | min')"
97+
rustup override set $msrv
98+
rustup component add clippy rustfmt
99+
- name: Error on warnings only for msrv
100+
if: matrix.rust == 'msrv'
101+
run: echo RUSTFLAGS='-Dwarnings' >> $GITHUB_ENV
102+
- run: rustup update
103+
- run: cargo version
104+
- run: rustup target add ${{ matrix.sys.target }}
105+
- run: rustup target add wasm32v1-none
106+
- run: echo CARGO_BUILD_TARGET='${{ matrix.sys.target }}' >> $GITHUB_ENV
107+
- uses: stellar/binaries@v45
108+
with:
109+
name: cargo-hack
110+
version: 0.5.28
111+
- run: make build-libs
112+
- run: make build-test-wasms TEST_CRATES_RUSTUP_TOOLCHAIN=
113+
- name: Upload test wasms
114+
uses: actions/upload-artifact@v4
115+
with:
116+
name: test-wasms-${{ matrix.rust }}-${{ matrix.sys.target }}
117+
path: target/wasm32v1-none/release/test_*.wasm
118+
retention-days: ${{ env.ARTIFACT_RETENTION_DAYS_FOR_TEST_WASMS }}
119+
120+
test:
121+
needs: build
122+
strategy:
123+
fail-fast: false
124+
matrix:
125+
rust: [msrv, latest]
126+
sys:
78127
- os: ubuntu-latest
128+
target: x86_64-unknown-linux-gnu
129+
- os: ubuntu-24.04-arm
79130
target: aarch64-unknown-linux-gnu
80-
# Fix #1011 by avoiding the use of crate-type = ["cdylib"] in native
81-
# builds that cross-compile. It causes linker errors. The root cause
82-
# is rust-lang/cargo#4133. This workaround uses rustc to build for a
83-
# specific crate-type, lib, such that the Cargo.toml crate-type is
84-
# ignored.
85-
cdylib-cross-compile-workaround: true
86-
# - os: macos-latest
87-
# target: x86_64-apple-darwin
88-
# - os: macos-latest
89-
# target: aarch64-apple-darwin
131+
- os: macos-15-intel
132+
target: x86_64-apple-darwin
133+
- os: macos-latest
134+
target: aarch64-apple-darwin
90135
- os: windows-latest
91136
target: x86_64-pc-windows-msvc
92137
runs-on: ${{ matrix.sys.os }}
@@ -98,42 +143,28 @@ jobs:
98143
run: |
99144
msrv="$(cargo metadata --format-version 1 --no-deps | jq -r '.packages | map(.rust_version) | map(values) | min')"
100145
rustup override set $msrv
101-
rustup component add clippy --toolchain $msrv
102-
- name: Error on warnings and clippy checks
103-
# Only error on warnings and checks for the msrv, because new versions of
104-
# Rust will frequently add new warnings and checks.
146+
rustup component add clippy rustfmt
147+
- name: Error on warnings only for msrv
105148
if: matrix.rust == 'msrv'
106149
run: echo RUSTFLAGS='-Dwarnings' >> $GITHUB_ENV
107150
- run: rustup update
108151
- run: cargo version
109152
- run: rustup target add ${{ matrix.sys.target }}
110153
- run: rustup target add wasm32v1-none
111-
- uses: stellar/binaries@v37
154+
- run: echo CARGO_BUILD_TARGET='${{ matrix.sys.target }}' >> $GITHUB_ENV
155+
- uses: stellar/binaries@v45
112156
with:
113157
name: cargo-hack
114158
version: 0.5.28
115-
- if: startsWith(matrix.sys.target, 'x86_64')
116-
name: Clear test snapshots for checking no diffs exists after test run
159+
- name: Restore test wasms for tests from one of the msrv builds
160+
uses: actions/download-artifact@v5
161+
with:
162+
name: test-wasms-msrv-x86_64-unknown-linux-gnu
163+
path: target/wasm32v1-none/release/
164+
- name: Clear test snapshots for checking no diffs exists after test run
117165
run: rm -fr **/test_snapshots
118-
- name: Build for wasm
119-
run: cargo-hack hack build --target wasm32v1-none --profile release --workspace --exclude soroban-meta --exclude soroban-spec --exclude soroban-spec-rust --exclude soroban-ledger-snapshot
120-
- name: Wasm Size
121-
run: |
122-
cd target/wasm32v1-none/release/ && \
123-
for i in *.wasm ; do \
124-
ls -l "$i"; \
125-
done
126-
- if: "!matrix.sys.cdylib-cross-compile-workaround"
127-
name: Build for native
128-
run: cargo-hack hack --feature-powerset --exclude-features docs build --target ${{ matrix.sys.target }}
129-
- if: "matrix.sys.cdylib-cross-compile-workaround"
130-
name: Build for native
131-
run: cargo-hack hack --feature-powerset --exclude-features docs rustc --workspace --exclude soroban-sdk-macros --exclude proc_macros --crate-type lib --target ${{ matrix.sys.target }}
132-
- if: startsWith(matrix.sys.target, 'x86_64')
133-
name: Run tests
134-
run: cargo-hack hack --feature-powerset --ignore-unknown-features --features testutils --exclude-features docs test --target ${{ matrix.sys.target }}
135-
- if: startsWith(matrix.sys.target, 'x86_64')
136-
name: Check no diffs exist
166+
- run: make test
167+
- name: Check no diffs exist
137168
run: git add -N . && git diff HEAD --exit-code
138169

139170
build-fuzz:
@@ -142,10 +173,10 @@ jobs:
142173
- uses: actions/checkout@v3
143174
- uses: stellar/actions/rust-cache@main
144175
- run: rustup install nightly
145-
- uses: stellar/binaries@v37
176+
- uses: stellar/binaries@v45
146177
with:
147178
name: cargo-fuzz
148-
version: 0.12.0
179+
version: 0.13.1
149180
- run: make build-fuzz
150181
- name: Check no diffs exist
151182
run: git add -N . && git diff HEAD --exit-code

Makefile

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,38 @@
1+
LIB_CRATES = $(shell cargo metadata --no-deps --format-version 1 | jq -r '.packages[] | select(.name | startswith("test_") | not) | .name' | tr '\n' ' ')
2+
TEST_CRATES = $(shell cargo metadata --no-deps --format-version 1 | jq -r '.packages[] | select(.name | startswith("test_")) | .name' | tr '\n' ' ')
3+
4+
MSRV = $(shell cargo metadata --no-deps --format-version 1 | jq -r '.packages[] | select(.name == "soroban-sdk") | .rust_version')
5+
TEST_CRATES_RUSTUP_TOOLCHAIN?=$(MSRV)
6+
17
all: check test
28

39
export RUSTFLAGS=-Dwarnings
410

511
CARGO_DOC_ARGS?=--open
6-
NATIVE_ONLY_CRATES:=soroban-meta soroban-spec soroban-spec-rust soroban-ledger-snapshot
7-
NATIVE_PACKAGE_ARGS:=$(foreach i,$(NATIVE_ONLY_CRATES), --package $(i))
8-
WASM_EXCLUDE_ARGS:=$(foreach i,$(NATIVE_ONLY_CRATES), --exclude $(i))
912

1013
doc: fmt
11-
cargo test --doc -p soroban-sdk -p soroban-sdk-macros --features testutils,hazmat
12-
cargo +nightly doc -p soroban-sdk --no-deps --all-features $(CARGO_DOC_ARGS)
14+
cargo test --doc $(foreach c,$(LIB_CRATES),--package $(c)) --features testutils,alloc,hazmat
15+
cargo +nightly doc --no-deps $(foreach c,$(LIB_CRATES),--package $(c)) --all-features $(CARGO_DOC_ARGS)
1316

14-
test: fmt build
17+
test: fmt build-test-wasms
1518
cargo hack --feature-powerset --ignore-unknown-features --features testutils --exclude-features docs test
1619

17-
build: fmt
18-
cargo hack build --release $(NATIVE_PACKAGE_ARGS)
19-
cargo hack build --target wasm32v1-none --release --workspace $(WASM_EXCLUDE_ARGS)
20+
build: build-libs build-test-wasms
21+
22+
build-libs: fmt
23+
cargo hack build --release $(foreach c,$(LIB_CRATES),--package $(c))
24+
25+
build-test-wasms: fmt
26+
# Build the test wasms with MSRV by default, with some meta disabled for
27+
# binary stability for tests.
28+
RUSTUP_TOOLCHAIN=$(TEST_CRATES_RUSTUP_TOOLCHAIN) \
29+
RUSTFLAGS='--cfg soroban_sdk_internal_no_rssdkver_meta' \
30+
cargo hack build --release --target wasm32v1-none $(foreach c,$(TEST_CRATES),--package $(c)) ; \
2031
cd target/wasm32v1-none/release/ && \
2132
for i in *.wasm ; do \
2233
ls -l "$$i"; \
2334
done
2435

25-
check: build fmt
26-
cargo hack --feature-powerset --exclude-features docs check
27-
cargo hack check --release --target wasm32v1-none --workspace $(WASM_EXCLUDE_ARGS)
28-
2936
build-fuzz:
3037
cd tests/fuzz/fuzz && cargo +nightly fuzz check
3138

soroban-meta/src/tests.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ fn test_from_wasm() {
1212
ScMetaEntry::ScMetaV0(v0) => v0.key.to_string(),
1313
})
1414
.collect::<Vec<_>>();
15-
assert_eq!(keys, ["rsver", "rssdkver"]);
15+
assert_eq!(keys, ["rsver"]);
1616
}
1717

1818
#[test]
@@ -58,5 +58,5 @@ fn test_multiple_metadata_sections() {
5858
ScMetaEntry::ScMetaV0(v0) => v0.key.to_string(),
5959
})
6060
.collect::<Vec<_>>();
61-
assert_eq!(keys, ["rsver", "rssdkver", "mykey"]);
61+
assert_eq!(keys, ["rsver", "mykey"]);
6262
}

soroban-sdk/build.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
pub fn main() {
2+
// Inform the compiler that the soroban_sdk_internal_no_rssdkver_meta cfg is valid.
3+
// The cfg is used when building the test vectors in this repository, to disable the embedding
4+
// of the rssdkver meta to increase the stability of the build wasms and therefore their wasm
5+
// hash.
6+
println!("cargo::rustc-check-cfg=cfg(soroban_sdk_internal_no_rssdkver_meta)");
7+
28
#[cfg(all(target_family = "wasm", target_os = "unknown"))]
39
if let Ok(version) = rustc_version::version() {
410
if version.major == 1 && version.minor >= 82 {

soroban-sdk/src/lib.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,12 @@ const _: () = {
103103
// Rustc version.
104104
contractmeta!(key = "rsver", val = env!("RUSTC_VERSION"),);
105105

106-
// Rust Soroban SDK version.
106+
// Rust Soroban SDK version. Don't emit when the cfg is set. The cfg is set when building test
107+
// wasms in this repository, so that every commit in this repo does not cause the test wasms in
108+
// this repo to have a new hash due to the revision being embedded. The wasm hash gets embedded
109+
// into a few places, such as test snapshots, or get used in test themselves where if they are
110+
// constantly changing creates repetitive diffs.
111+
#[cfg(not(soroban_sdk_internal_no_rssdkver_meta))]
107112
contractmeta!(
108113
key = "rssdkver",
109114
val = concat!(env!("CARGO_PKG_VERSION"), "#", env!("GIT_REVISION")),

soroban-sdk/src/tests/address.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use crate::{self as soroban_sdk};
22
use sha2::{Digest, Sha256};
33
use soroban_sdk::{
4-
address::Executable, contract, env::EnvTestConfig, testutils::Address as _, Address, Bytes,
5-
BytesN, Env, String, TryIntoVal,
4+
address::Executable, contract, testutils::Address as _, Address, Bytes, BytesN, Env, String,
5+
TryIntoVal,
66
};
77

88
#[contract]
@@ -86,12 +86,7 @@ fn test_get_existing_contract_address_executable_wasm() {
8686
const EXAMPLE_WASM: &[u8] =
8787
include_bytes!("../../../target/wasm32v1-none/release/test_udt.wasm");
8888

89-
let env = Env::new_with_config(EnvTestConfig {
90-
// Disable test snapshots because the tests in this repo will run across
91-
// multiple hosts, and this test uses a wasm file that won't build consistently
92-
// across different hosts.
93-
capture_snapshot_at_drop: false,
94-
});
89+
let env = Env::default();
9590

9691
let contract_address = env.register(EXAMPLE_WASM, ());
9792
let contract_executable = contract_address.executable();

soroban-sdk/src/tests/crypto_bls12_381.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use crate::{self as soroban_sdk};
22
use soroban_sdk::{
33
bytes, bytesn, contract, contractimpl,
44
crypto::bls12_381::{Bls12_381, Fp, Fp2, Fr, G1Affine, G2Affine},
5-
env::EnvTestConfig,
65
vec, Address, Bytes, BytesN, Env, Vec, U256,
76
};
87

@@ -254,12 +253,7 @@ impl Contract {
254253

255254
#[test]
256255
fn test_invoke_contract() {
257-
let e = Env::new_with_config(EnvTestConfig {
258-
// Disable test snapshots because the tests in this repo will run across
259-
// multiple hosts, and this test uses a wasm file that won't build consistently
260-
// across different hosts.
261-
capture_snapshot_at_drop: false,
262-
});
256+
let e = Env::default();
263257

264258
let bls_contract_id = e.register(blscontract::WASM, ());
265259

0 commit comments

Comments
 (0)