Skip to content

Commit

Permalink
fix!: wasm compatibility for fuels-types (#839)
Browse files Browse the repository at this point in the history
closes: #837 
closes: #854

A big thank you to @hal3e for help with the CI. 

The wasm offending functionality is now hidden behind the feature `std`
in `fuels-types` and in `fuels-core` as well.

### Breakage:
@digorithm 
It is part of the crate's default features, so most users shouldn't feel
the change -- especially since there weren't other other features that
would have made somebody do `default-features = false` in their
`Cargo.toml`.

But if they did for whatever reason, then they might see parts of
`fuels-types` disappear since the `std` feature would not be enabled. So
that makes it a theoretically breaking change.

Also `fuels` had two feature flags: `fuel-core` and `fuel-core-lib`.
`fuel-core` is removed since it was implicitly added because the
optional dependency `fuel-core` had not been referred to by prepending
`dep:` to it.

`fuel-core` used to enable the fuel-core-lib only partially and was not
the right flag to use.

### About the implementation
Cargo features should be additive by design. This means that you cannot
remove dependencies by adding features.

This further means that wasm support is generally implemented by
*hiding* wasm-offending code behind a feature (such as `std`).

In our case, if we want to be WASM compatible, we must not enable the
`std` feature on any `fuels-*` crates our project might depend on.

Feature unification makes this a bit difficult. If there is even one
dependency that enabled `std` on some `fuels-*` crate, all other crates
that are part of that compilation will also have the `std` feature
enabled for that crate.

Because of this, I've made it default for our workspace dependencies to
start off without defaults (and thus without the `std` flag).

If any of our crates need it, they will have to enable the feature
explicitly.
  • Loading branch information
segfault-magnet committed Feb 23, 2023
1 parent dbf142f commit 43e907d
Show file tree
Hide file tree
Showing 24 changed files with 142 additions and 94 deletions.
73 changes: 37 additions & 36 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -113,18 +113,24 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
command: [check]
cargo_command: [check]
args: [--all-features]
package: ${{fromJSON(needs.get-workspace-members.outputs.members)}}
include:
- command: fmt
- cargo_command: fmt
args: --all --verbose -- --check
- command: clippy
- cargo_command: clippy
args: --all-targets --all-features
- command: test
- cargo_command: test
args: --all-targets --all-features --workspace
- command: test
- cargo_command: test
args: --all-targets --workspace
- command: test_wasm
args:
- command: check_doc_anchors_valid
args:
- command: check_doc_unresolved_links
args:
steps:
- name: Checkout repository
uses: actions/checkout@v3
Expand All @@ -140,61 +146,56 @@ jobs:
- uses: Swatinem/rust-cache@v2.0.1
continue-on-error: true
with:
key: "${{ matrix.command }} ${{ matrix.args }} ${{ matrix.package }}"

- name: Add WASM target
if: ${{ matrix.command == 'test' }}
run: rustup target add wasm32-unknown-unknown

- name: Install WebAssembly Test harness
if: ${{ matrix.command == 'test' }}
uses: baptiste0928/cargo-install@v1
with:
crate: webassembly-test-runner
cache-key: "${{ matrix.command }} ${{ matrix.args }} ${{ matrix.package }}"

# TODO: Enable WASM tests
# - name: Test WASM package
# if: ${{ matrix.command == 'test' }}
# run: |
# cd packages/wasm-tests
# cargo test --target wasm32-unknown-unknown --all-targets --all-features
# cargo test --target wasm32-unknown-unknown --all-targets --no-default-features
key: "${{ matrix.cargo_command }} ${{ matrix.args }} ${{ matrix.package }}"

- name: Install Fuel Core
if: ${{ matrix.command == 'test' }}
if: ${{ matrix.cargo_command == 'test' }}
run: |
curl -sSLf https://github.com/FuelLabs/fuel-core/releases/download/v${{ env.FUEL_CORE_VERSION }}/fuel-core-${{ env.FUEL_CORE_VERSION }}-x86_64-unknown-linux-gnu.tar.gz -L -o fuel-core.tar.gz
tar -xvf fuel-core.tar.gz
chmod +x fuel-core-${{ env.FUEL_CORE_VERSION }}-x86_64-unknown-linux-gnu/fuel-core
mv fuel-core-${{ env.FUEL_CORE_VERSION }}-x86_64-unknown-linux-gnu/fuel-core /usr/local/bin/fuel-core
- name: Download sway example artifacts
if: ${{ matrix.command == 'test' || matrix.command == 'clippy' }}
if: ${{ matrix.cargo_command == 'test' || matrix.cargo_command == 'clippy' }}
uses: actions/download-artifact@v3
with:
name: sway-examples
path: packages/fuels/tests/

- name: Cargo (workspace-level)
if: ${{ !matrix.package }}
run: cargo ${{ matrix.command }} ${{ matrix.args }}
if: ${{ matrix.cargo_command && !matrix.package }}
run: cargo ${{ matrix.cargo_command }} ${{ matrix.args }}

- name: Cargo (package-level)
if: ${{ matrix.package }}
run: cargo ${{ matrix.command }} -p ${{ matrix.package }} ${{ matrix.args }}
if: ${{ matrix.cargo_command && matrix.package }}
run: cargo ${{ matrix.cargo_command }} -p ${{ matrix.package }} ${{ matrix.args }}

- name: Check Docs. Validity
uses: actions-rs/cargo@v1
- name: Install NodeJS for WASM testing
if: ${{ matrix.command == 'test_wasm' }}
uses: actions/setup-node@v3
with:
command: run
args: --bin check-docs
node-version: 18

- name: Check Docs. Warnings
- name: Test WASM
if: ${{ matrix.command == 'test_wasm' }}
run: |
rustup target add wasm32-unknown-unknown
curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh
cd packages/wasm-tests
wasm-pack test --node
- name: Check for invalid documentation anchors
if: ${{ matrix.command == 'check_doc_anchors_valid' }}
run: cargo run --bin check-docs

- name: Check for unresolved documentation links
if: ${{ matrix.command == 'check_doc_unresolved_links' }}
run: |
! cargo doc |& grep -A 6 "warning: unresolved link to"
# Ensure workspace is publishable
publish-crates-check:
runs-on: ubuntu-latest
Expand Down
14 changes: 7 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ fuel-core-chain-config = "0.17"
fuel-core-types = "0.17"
fuel-vm = "0.26"
fuels = { version = "0.36.1", path = "./packages/fuels" }
fuels-code-gen = { version = "0.36.1", path = "./packages/fuels-code-gen" }
fuels-core = { version = "0.36.1", path = "./packages/fuels-core" }
fuels-macros = { version = "0.36.1", path = "./packages/fuels-macros" }
fuels-programs = { version = "0.36.1", path = "./packages/fuels-programs" }
fuels-signers = { version = "0.36.1", path = "./packages/fuels-signers" }
fuels-test-helpers = { version = "0.36.1", path = "./packages/fuels-test-helpers" }
fuels-types = { version = "0.36.1", path = "./packages/fuels-types" }
fuels-code-gen = { version = "0.36.1", path = "./packages/fuels-code-gen", default-features = false }
fuels-core = { version = "0.36.1", path = "./packages/fuels-core", default-features = false }
fuels-macros = { version = "0.36.1", path = "./packages/fuels-macros", default-features = false }
fuels-programs = { version = "0.36.1", path = "./packages/fuels-programs", default-features = false }
fuels-signers = { version = "0.36.1", path = "./packages/fuels-signers", default-features = false }
fuels-test-helpers = { version = "0.36.1", path = "./packages/fuels-test-helpers", default-features = false }
fuels-types = { version = "0.36.1", path = "./packages/fuels-types", default-features = false }
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,17 @@ You can also run specific tests. The following example will run all integration
```shell
cargo test --test types in_vector -- --show-output
```
### How to run WASM tests?
You need to have wasm32 as a target, if you don't already:
```shell
rustup target add wasm32-unknown-unknown
```
You also need `wasm-pack`, if you don't already:
```shell
cargo install wasm-pack
```

Navigate to `packages/wasm-tests` and run `wasm-pack test`.
### What to do if my tests are failing on `master`

Before doing anything else, try all these commands:
Expand Down
1 change: 0 additions & 1 deletion packages/fuels-code-gen/src/program_bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ mod abigen;
mod custom_types;
mod generated_code;
mod resolved_type;
mod source;
mod utils;

pub use abigen::{Abigen, AbigenTarget, ProgramType};
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ use std::convert::TryFrom;

use crate::{
error::{Error, Result},
program_bindings::{abi_types::FullProgramABI, source::Source},
program_bindings::abi_types::FullProgramABI,
utils::Source,
};

#[derive(Debug, Clone)]
Expand Down
2 changes: 2 additions & 0 deletions packages/fuels-code-gen/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
pub use ident::{ident, safe_ident};
pub use source::Source;
pub use type_path::TypePath;

mod ident;
mod source;
mod type_path;
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::error::{error, Error, Result};

/// A source of a Truffle artifact JSON.
#[derive(Clone, Debug, Eq, PartialEq)]
pub(crate) enum Source {
pub enum Source {
/// A raw ABI string
String(String),

Expand Down
7 changes: 7 additions & 0 deletions packages/fuels-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,10 @@ sha2 = "0.9.5"

[dev-dependencies]
fuels-test-helpers = { workspace = true }

[features]
default = ["std"]
std = [
"fuels-test-helpers/std",
"fuels-types/std",
]
9 changes: 9 additions & 0 deletions packages/fuels-programs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,12 @@ tokio = "1.12"

[dev-dependencies]
fuels-test-helpers = { path = "../fuels-test-helpers" }

[features]
default = ["std"]
std = [
"fuels-core/std",
"fuels-signers/std",
"fuels-test-helpers/std",
"fuels-types/std",
]
6 changes: 6 additions & 0 deletions packages/fuels-signers/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,10 @@ hex = { version = "0.4.3", default-features = false, features = ["std"] }
tempfile = "3.3.0"

[features]
default = ["std"]
std = [
"fuels-core/std",
"fuels-test-helpers/std",
"fuels-types/std",
]
test-helpers = ["fuel-core"]
8 changes: 7 additions & 1 deletion packages/fuels-test-helpers/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,11 @@ futures = "0.3.26"
which = { version = "4.3", default-features = false }

[features]
default = ["fuels-signers"]
default = ["fuels-signers", "std"]
std = [
"fuels-programs/std",
"fuels-core/std",
"fuels-signers?/std",
"fuels-types/std",
]
fuel-core-lib = ["fuel-core"]
6 changes: 4 additions & 2 deletions packages/fuels-types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ fuel-asm = { workspace = true }
fuel-abi-types = { workspace = true }
fuel-core = { workspace = true, default-features = false, optional = true }
fuel-core-chain-config = { workspace = true, default-features = false }
fuel-core-client = { workspace = true, default-features = false }
fuel-core-client = { workspace = true, default-features = false, optional = true }
hex = { version = "0.4.3", features = ["std"] }
proc-macro2 = "1.0"
serde = { version = "1.0", features = ["derive"] }
Expand All @@ -29,6 +29,8 @@ strum = "0.21"
strum_macros = "0.21"
itertools = "0.10.5"
thiserror = { version = "1.0.26", default-features = false }
tokio = "1.15"
fuels-macros = { workspace = true }

[features]
default = ["std"]
std = ["dep:fuel-core-client"]
2 changes: 2 additions & 0 deletions packages/fuels-types/src/block.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![cfg(feature = "std")]

use chrono::{DateTime, NaiveDateTime, Utc};
use fuel_core_client::client::schema::block::{Block as ClientBlock, Header as ClientHeader};
use fuel_tx::Bytes32;
Expand Down
2 changes: 2 additions & 0 deletions packages/fuels-types/src/chain_info.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![cfg(feature = "std")]

use fuel_core_client::client::schema::chain::ChainInfo as ClientChainInfo;
use fuel_tx::ConsensusParameters;

Expand Down
2 changes: 2 additions & 0 deletions packages/fuels-types/src/coin.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![cfg(feature = "std")]

use fuel_core_chain_config::CoinConfig;
use fuel_core_client::client::schema::coin::{Coin as ClientCoin, CoinStatus as ClientCoinStatus};
use fuel_tx::{AssetId, UtxoId};
Expand Down
2 changes: 2 additions & 0 deletions packages/fuels-types/src/message.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![cfg(feature = "std")]

use fuel_core_chain_config::MessageConfig;
use fuel_core_client::client::schema::message::{
Message as ClientMessage, MessageStatus as ClientMessageStatus,
Expand Down
2 changes: 2 additions & 0 deletions packages/fuels-types/src/message_proof.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![cfg(feature = "std")]

use fuel_core_client::client::schema::message::MessageProof as ClientMessageProof;
use fuel_tx::{Bytes32, Bytes64};

Expand Down
2 changes: 2 additions & 0 deletions packages/fuels-types/src/node_info.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![cfg(feature = "std")]

use fuel_core_client::client::schema::node_info::NodeInfo as ClientNodeInfo;

#[derive(Debug)]
Expand Down
2 changes: 2 additions & 0 deletions packages/fuels-types/src/resource.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![cfg(feature = "std")]

use fuel_core_client::client::schema::resource::Resource as ClientResource;

use crate::{coin::Coin, message::Message};
Expand Down
2 changes: 2 additions & 0 deletions packages/fuels-types/src/transaction_response.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![cfg(feature = "std")]

use std::str::FromStr;

use chrono::{DateTime, NaiveDateTime, Utc};
Expand Down
10 changes: 9 additions & 1 deletion packages/fuels/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,12 @@ sha2 = "0.9.5"
tokio = "1.15.0"

[features]
fuel-core-lib = ["fuels-test-helpers/fuel-core-lib", "fuel-core"]
default = ["std", "fuels-test-helpers/fuels-signers"]
std = [
"fuels-programs/std",
"fuels-core/std",
"fuels-signers/std",
"fuels-test-helpers/std",
"fuels-types/std",
]
fuel-core-lib = ["fuels-test-helpers/fuel-core-lib", "dep:fuel-core"]
2 changes: 1 addition & 1 deletion packages/wasm-tests/.cargo/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
target = "wasm32-unknown-unknown"

[target.wasm32-unknown-unknown]
runner = "webassembly-test-runner"
runner = "wasm-bindgen-test-runner"
2 changes: 1 addition & 1 deletion packages/wasm-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ fuels-macros = { workspace = true }
fuels-core = { workspace = true }

[dev-dependencies]
webassembly-test = "0.1"
wasm-bindgen-test = "0.3.34"
Loading

0 comments on commit 43e907d

Please sign in to comment.