diff --git a/.github/workflows/coverage-examples.yml b/.github/workflows/coverage-examples.yml new file mode 100644 index 000000000..d284e3dd4 --- /dev/null +++ b/.github/workflows/coverage-examples.yml @@ -0,0 +1,67 @@ +name: Coverage examples +on: + push: + branches: + - master + pull_request: +env: + RUSTFLAGS: "-D warnings" + +jobs: + coverage: + runs-on: ${{ matrix.platform }} + name: "${{ matrix.example }} - ${{ matrix.platform }}" + strategy: + matrix: + platform: [macos-latest] + toolchain: [nightly-2024-05-01-aarch64-apple-darwin] + steps: + - uses: actions/checkout@v3 + - name: Install Homebrew + run: | + /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" + - name: Install LLVM + run: brew install llvm + - name: Add LLVM to PATH + run: echo "/opt/homebrew/opt/llvm/bin" >> $GITHUB_PATH + - name: Llvm version + run: llvm-config --version + - name: Clang version + run: clang --version + - name: "${{ matrix.toolchain }} with rustfmt, and wasm32" + uses: actions-rs/toolchain@v1 + with: + profile: minimal + toolchain: ${{ matrix.toolchain }} + target: wasm32-unknown-unknown + - name: Set Default Toolchain + run: rustup override set ${{ matrix.toolchain }} + - name: Rust version + run: rustc --version --verbose + - name: Verify Rust Toolchain + run: rustup show + - name: Add rust-src component + run: rustup component add rust-src --toolchain ${{ matrix.toolchain }} + - name: Install wasmcov + run: cargo +${{ matrix.toolchain }} install wasmcov + - name: Create wasmcov directory + run: mkdir -p ${{ github.workspace }}/wasmcov + - name: Set WASMCOV_DIR environment variable + run: echo "WASMCOV_DIR=${{ github.workspace }}/wasmcov" >> $GITHUB_ENV + - name: Set up cache for near_sandbox directory + uses: actions/cache@v3 + with: + path: ${{ github.workspace }}/wasmcov/near_sandbox + key: ${{ runner.os }}-near-sandbox-${{ github.sha }} + restore-keys: | + ${{ runner.os }}-near-sandbox-${{ github.sha }} + - name: time version + run: cargo tree | grep time + - name: Build examples + env: + RUSTFLAGS: '-C link-arg=-s' + run: cd ./examples && ./build_wasm_test_all.sh + - name: Merge profdata files + env: + RUSTFLAGS: '-C link-arg=-s' + run: cd ./examples && ./merge_profdata.sh \ No newline at end of file diff --git a/examples/adder/Cargo.toml b/examples/adder/Cargo.toml index bec5bafb9..d64044e70 100644 --- a/examples/adder/Cargo.toml +++ b/examples/adder/Cargo.toml @@ -9,6 +9,7 @@ crate-type = ["cdylib"] [dependencies] near-sdk = { path = "../../near-sdk" } +wasmcov = { version = "0.2", optional = true } [dev-dependencies] near-workspaces = "0.11.1" @@ -18,6 +19,9 @@ near-abi = "0.4.0" zstd = "0.13" near-sdk = { path = "../../near-sdk", features = ["unit-testing"] } +[features] +wasmcov = ["dep:wasmcov"] + [profile.release] codegen-units = 1 # Tell `rustc` to optimize for small code size. diff --git a/examples/adder/build.sh b/examples/adder/build.sh index 2b82f897e..e3a4e35d1 100755 --- a/examples/adder/build.sh +++ b/examples/adder/build.sh @@ -1,8 +1,15 @@ #!/bin/bash -TARGET="${CARGO_TARGET_DIR:-../../target}" + +if [ -n "$WASMCOV_DIR" ]; then + TARGET="${WASMCOV_DIR}/target" + BUILD_COMMAND="cargo wasmcov build -- --features wasmcov" +else + TARGET="${CARGO_TARGET_DIR:-../../target}" + BUILD_COMMAND="cargo build" +fi + set -e cd "$(dirname $0)" - -cargo build --target wasm32-unknown-unknown --release +$BUILD_COMMAND --target wasm32-unknown-unknown --release cp $TARGET/wasm32-unknown-unknown/release/adder.wasm ./res/ -#wasm-opt -Oz --output ./res/status_message.wasm ./res/status_message.wasm +#wasm-opt -Oz --output ./res/status_message.wasm ./res/status_message.wasm \ No newline at end of file diff --git a/examples/adder/src/lib.rs b/examples/adder/src/lib.rs index 079bcca8c..ba98141a9 100644 --- a/examples/adder/src/lib.rs +++ b/examples/adder/src/lib.rs @@ -1,5 +1,8 @@ use near_sdk::near; +#[cfg(all(feature = "wasmcov", target_family = "wasm"))] +wasmcov::near::add_coverage!(); + #[near(serializers=[borsh, json])] pub struct Pair(u32, u32); @@ -88,4 +91,4 @@ mod tests { Ok(()) } -} +} \ No newline at end of file diff --git a/examples/build_wasm_test_all.sh b/examples/build_wasm_test_all.sh new file mode 100755 index 000000000..f2c3f4c93 --- /dev/null +++ b/examples/build_wasm_test_all.sh @@ -0,0 +1,19 @@ +#!/bin/bash + +for d in "status-message" $(ls -d */ | grep -v -e "status-message\/$"); do + if [ -d "$d" ]; then + echo "Processing directory $d" + echo "Building" + cd $d; + pwd + ./build.sh + echo "Testing" + cargo wasmcov test --near=1.40.0 + cd .. + echo "End of processing of directory $d" + fi +done + + + + diff --git a/examples/callback-results/Cargo.toml b/examples/callback-results/Cargo.toml index 52fafda73..87a01e2cd 100644 --- a/examples/callback-results/Cargo.toml +++ b/examples/callback-results/Cargo.toml @@ -9,6 +9,7 @@ crate-type = ["cdylib"] [dependencies] near-sdk = { path = "../../near-sdk" } +wasmcov = { version = "0.2", optional = true } [dev-dependencies] near-workspaces = "0.11.1" @@ -16,6 +17,9 @@ tokio = { version = "1.14", features = ["full"] } anyhow = "1.0" near-sdk = { path = "../../near-sdk", features = ["unit-testing"] } +[features] +wasmcov = ["dep:wasmcov"] + [profile.release] codegen-units = 1 # Tell `rustc` to optimize for small code size. diff --git a/examples/callback-results/build.sh b/examples/callback-results/build.sh index 969b4a995..a5b5f9bc3 100755 --- a/examples/callback-results/build.sh +++ b/examples/callback-results/build.sh @@ -1,6 +1,14 @@ #!/bin/bash -TARGET="${CARGO_TARGET_DIR:-../../target}" + +if [ -n "$WASMCOV_DIR" ]; then + TARGET="${WASMCOV_DIR}/target" + BUILD_COMMAND="cargo wasmcov build -- --features wasmcov" +else + TARGET="${CARGO_TARGET_DIR:-../../target}" + BUILD_COMMAND="cargo build" +fi + set -e cd "$(dirname $0)" -cargo build --target wasm32-unknown-unknown --release -cp $TARGET/wasm32-unknown-unknown/release/callback_results.wasm ./res/ +$BUILD_COMMAND --target wasm32-unknown-unknown --release +cp $TARGET/wasm32-unknown-unknown/release/callback_results.wasm ./res/ \ No newline at end of file diff --git a/examples/callback-results/src/lib.rs b/examples/callback-results/src/lib.rs index 1faa13e46..8b12056f1 100644 --- a/examples/callback-results/src/lib.rs +++ b/examples/callback-results/src/lib.rs @@ -1,6 +1,9 @@ use near_sdk::require; use near_sdk::{env, near, Promise, PromiseError}; +#[cfg(all(feature = "wasmcov", target_family = "wasm"))] +wasmcov::near::add_coverage!(); + const A_VALUE: u8 = 8; #[near(contract_state)] @@ -122,4 +125,4 @@ mod tests { Ok(()) } -} +} \ No newline at end of file diff --git a/examples/cross-contract-calls/Cargo.toml b/examples/cross-contract-calls/Cargo.toml index 118779daf..372270001 100644 --- a/examples/cross-contract-calls/Cargo.toml +++ b/examples/cross-contract-calls/Cargo.toml @@ -4,6 +4,9 @@ version = "0.1.0" authors = ["Near Inc "] edition = "2021" +[dependencies] +wasmcov = { version = "0.2", optional = true } + [dev-dependencies] anyhow = "1.0" near-sdk = { path = "../../near-sdk", features = ["default", "unit-testing"] } @@ -22,5 +25,8 @@ lto = true debug = false panic = "abort" +[features] +wasmcov = ["dep:wasmcov"] + [workspace] members = ["high-level", "low-level"] diff --git a/examples/cross-contract-calls/build.sh b/examples/cross-contract-calls/build.sh index 4beeb0907..f892efd56 100755 --- a/examples/cross-contract-calls/build.sh +++ b/examples/cross-contract-calls/build.sh @@ -1,8 +1,15 @@ #!/bin/bash -TARGET="${CARGO_TARGET_DIR:-../../target}" + +if [ -n "$WASMCOV_DIR" ]; then + TARGET="${WASMCOV_DIR}/target" + BUILD_COMMAND="cargo wasmcov build -- --features wasmcov" +else + TARGET="${CARGO_TARGET_DIR:-../../target}" + BUILD_COMMAND="cargo build" +fi + set -e cd "$(dirname $0)" - -cargo build --all --target wasm32-unknown-unknown --release +$BUILD_COMMAND --all --target wasm32-unknown-unknown --release cp $TARGET/wasm32-unknown-unknown/release/cross_contract_high_level.wasm ./res/ -cp $TARGET/wasm32-unknown-unknown/release/cross_contract_low_level.wasm ./res/ +cp $TARGET/wasm32-unknown-unknown/release/cross_contract_low_level.wasm ./res/ \ No newline at end of file diff --git a/examples/cross-contract-calls/high-level/Cargo.toml b/examples/cross-contract-calls/high-level/Cargo.toml index 9db83ff40..9ac9b7abd 100644 --- a/examples/cross-contract-calls/high-level/Cargo.toml +++ b/examples/cross-contract-calls/high-level/Cargo.toml @@ -9,3 +9,7 @@ crate-type = ["cdylib"] [dependencies] near-sdk = { path = "../../../near-sdk" , features = ["default", "unit-testing"] } +wasmcov = { version = "0.2", optional = true } + +[features] +wasmcov = ["dep:wasmcov"] \ No newline at end of file diff --git a/examples/cross-contract-calls/high-level/src/lib.rs b/examples/cross-contract-calls/high-level/src/lib.rs index 3f20bf058..caaf1734d 100644 --- a/examples/cross-contract-calls/high-level/src/lib.rs +++ b/examples/cross-contract-calls/high-level/src/lib.rs @@ -1,6 +1,9 @@ use near_sdk::env; use near_sdk::{log, near, PromiseOrValue}; +#[cfg(all(feature = "wasmcov", target_family = "wasm"))] +wasmcov::near::add_coverage!(); + #[derive(Default)] #[near(contract_state)] pub struct CrossContract {} diff --git a/examples/cross-contract-calls/low-level/Cargo.toml b/examples/cross-contract-calls/low-level/Cargo.toml index f2e635e31..c3df46277 100644 --- a/examples/cross-contract-calls/low-level/Cargo.toml +++ b/examples/cross-contract-calls/low-level/Cargo.toml @@ -9,3 +9,7 @@ crate-type = ["cdylib"] [dependencies] near-sdk = { path = "../../../near-sdk" , features = ["default", "unit-testing"] } +wasmcov = { version = "0.2", optional = true } + +[features] +wasmcov = ["dep:wasmcov"] \ No newline at end of file diff --git a/examples/cross-contract-calls/low-level/src/lib.rs b/examples/cross-contract-calls/low-level/src/lib.rs index 0a5e1533b..a70c2da5a 100644 --- a/examples/cross-contract-calls/low-level/src/lib.rs +++ b/examples/cross-contract-calls/low-level/src/lib.rs @@ -1,6 +1,9 @@ use near_sdk::serde_json; use near_sdk::{env, near, require, Gas, NearToken, PromiseResult}; +#[cfg(all(feature = "wasmcov", target_family = "wasm"))] +wasmcov::near::add_coverage!(); + // Prepaid gas for a single (not inclusive of recursion) `factorial` call. const FACTORIAL_CALL_GAS: Gas = Gas::from_tgas(20); diff --git a/examples/factory-contract/Cargo.toml b/examples/factory-contract/Cargo.toml index 7fca0329d..e771a7d13 100644 --- a/examples/factory-contract/Cargo.toml +++ b/examples/factory-contract/Cargo.toml @@ -4,6 +4,9 @@ version = "0.1.0" authors = ["Near Inc "] edition = "2021" +[dependencies] +wasmcov = { version = "0.2", optional = true } + [dev-dependencies] anyhow = "1.0" test-case = "2.0" @@ -11,6 +14,9 @@ tokio = { version = "1.14", features = ["full"] } near-workspaces = "0.11.1" near-sdk = { path = "../../near-sdk", features = ["unit-testing"] } +[features] +wasmcov = ["dep:wasmcov"] + [profile.release] codegen-units = 1 # Tell `rustc` to optimize for small code size. diff --git a/examples/factory-contract/build.sh b/examples/factory-contract/build.sh index c8e450d18..cd1940492 100755 --- a/examples/factory-contract/build.sh +++ b/examples/factory-contract/build.sh @@ -1,8 +1,16 @@ #!/bin/bash -TARGET="${CARGO_TARGET_DIR:-../../target}" + +if [ -n "$WASMCOV_DIR" ]; then + TARGET="${WASMCOV_DIR}/target" + BUILD_COMMAND="cargo wasmcov build -- --features wasmcov" +else + TARGET="${CARGO_TARGET_DIR:-../../target}" + BUILD_COMMAND="cargo build" +fi + set -e cd "$(dirname $0)" -cargo build --all --target wasm32-unknown-unknown --release +$BUILD_COMMAND --all --target wasm32-unknown-unknown --release cp $TARGET/wasm32-unknown-unknown/release/factory_contract_high_level.wasm ./res/ -cp $TARGET/wasm32-unknown-unknown/release/factory_contract_low_level.wasm ./res/ +cp $TARGET/wasm32-unknown-unknown/release/factory_contract_low_level.wasm ./res/ \ No newline at end of file diff --git a/examples/factory-contract/high-level/Cargo.toml b/examples/factory-contract/high-level/Cargo.toml index 3225e6ce5..57bae0682 100644 --- a/examples/factory-contract/high-level/Cargo.toml +++ b/examples/factory-contract/high-level/Cargo.toml @@ -9,6 +9,10 @@ crate-type = ["cdylib"] [dependencies] near-sdk = { path = "../../../near-sdk" } +wasmcov = { version = "0.2", optional = true } [dev-dependencies] near-sdk = { path = "../../../near-sdk", features = ["unit-testing"] } + +[features] +wasmcov = ["dep:wasmcov"] diff --git a/examples/factory-contract/high-level/src/lib.rs b/examples/factory-contract/high-level/src/lib.rs index 7b1167ff6..30dc56dcd 100644 --- a/examples/factory-contract/high-level/src/lib.rs +++ b/examples/factory-contract/high-level/src/lib.rs @@ -1,6 +1,9 @@ use near_sdk::PromiseError; use near_sdk::{env, ext_contract, near, AccountId, NearToken, Promise}; +#[cfg(all(feature = "wasmcov", target_family = "wasm"))] +wasmcov::near::add_coverage!(); + #[derive(Default)] #[near(contract_state)] pub struct FactoryContract {} diff --git a/examples/factory-contract/low-level/Cargo.toml b/examples/factory-contract/low-level/Cargo.toml index 2807fb8c2..5e538a4b1 100644 --- a/examples/factory-contract/low-level/Cargo.toml +++ b/examples/factory-contract/low-level/Cargo.toml @@ -9,6 +9,10 @@ crate-type = ["cdylib"] [dependencies] near-sdk = { path = "../../../near-sdk" } +wasmcov = { version = "0.2", optional = true } [dev-dependencies] near-sdk = { path = "../../../near-sdk", features = ["unit-testing"] } + +[features] +wasmcov = ["dep:wasmcov"] \ No newline at end of file diff --git a/examples/factory-contract/low-level/src/lib.rs b/examples/factory-contract/low-level/src/lib.rs index d2bda474e..a5b8dd37c 100644 --- a/examples/factory-contract/low-level/src/lib.rs +++ b/examples/factory-contract/low-level/src/lib.rs @@ -2,6 +2,9 @@ use near_sdk::json_types::U128; use near_sdk::serde_json; use near_sdk::{env, near, AccountId, Gas, NearToken, PromiseResult}; +#[cfg(all(feature = "wasmcov", target_family = "wasm"))] +wasmcov::near::add_coverage!(); + // Prepaid gas for making a single simple call. const SINGLE_CALL_GAS: Gas = Gas::from_tgas(20); diff --git a/examples/fungible-token/Cargo.toml b/examples/fungible-token/Cargo.toml index b2a546724..50e2d2851 100644 --- a/examples/fungible-token/Cargo.toml +++ b/examples/fungible-token/Cargo.toml @@ -4,12 +4,18 @@ version = "0.0.2" authors = ["Near Inc "] edition = "2021" +[dependencies] +wasmcov = { version = "0.2", optional = true } + [dev-dependencies] anyhow = "1.0" near-sdk = { path = "../../near-sdk", features = ["unit-testing"] } tokio = { version = "1.14", features = ["full"] } near-workspaces = "0.11.1" +[features] +wasmcov = ["dep:wasmcov"] + [profile.release] codegen-units = 1 # Tell `rustc` to optimize for small code size. diff --git a/examples/fungible-token/build.sh b/examples/fungible-token/build.sh index 0c31bd39d..7dd6172e3 100755 --- a/examples/fungible-token/build.sh +++ b/examples/fungible-token/build.sh @@ -1,8 +1,16 @@ #!/bin/bash -TARGET="${CARGO_TARGET_DIR:-../../target}" + +if [ -n "$WASMCOV_DIR" ]; then + TARGET="${WASMCOV_DIR}/target" + BUILD_COMMAND="cargo wasmcov build -- --features wasmcov" +else + TARGET="${CARGO_TARGET_DIR:-../../target}" + BUILD_COMMAND="cargo build" +fi + set -e cd "$(dirname $0)" -cargo build --all --target wasm32-unknown-unknown --release +$BUILD_COMMAND --all --target wasm32-unknown-unknown --release cp $TARGET/wasm32-unknown-unknown/release/defi.wasm ./res/ -cp $TARGET/wasm32-unknown-unknown/release/fungible_token.wasm ./res/ +cp $TARGET/wasm32-unknown-unknown/release/fungible_token.wasm ./res/ \ No newline at end of file diff --git a/examples/fungible-token/ft/Cargo.toml b/examples/fungible-token/ft/Cargo.toml index 652900622..b18166e0a 100644 --- a/examples/fungible-token/ft/Cargo.toml +++ b/examples/fungible-token/ft/Cargo.toml @@ -10,6 +10,10 @@ crate-type = ["cdylib"] [dependencies] near-sdk = { path = "../../../near-sdk" } near-contract-standards = { path = "../../../near-contract-standards" } +wasmcov = { version = "0.2", optional = true } [dev-dependencies] -near-sdk = { path = "../../../near-sdk", features = ["unit-testing"] } \ No newline at end of file +near-sdk = { path = "../../../near-sdk", features = ["unit-testing"] } + +[features] +wasmcov = ["dep:wasmcov"] \ No newline at end of file diff --git a/examples/fungible-token/ft/src/lib.rs b/examples/fungible-token/ft/src/lib.rs index a6be56935..cc55c4598 100644 --- a/examples/fungible-token/ft/src/lib.rs +++ b/examples/fungible-token/ft/src/lib.rs @@ -31,6 +31,9 @@ use near_sdk::{ env, log, near, require, AccountId, BorshStorageKey, NearToken, PanicOnDefault, PromiseOrValue, }; +#[cfg(all(feature = "wasmcov", target_family = "wasm"))] +wasmcov::near::add_coverage!(); + #[derive(PanicOnDefault)] #[near(contract_state)] pub struct Contract { diff --git a/examples/fungible-token/test-contract-defi/Cargo.toml b/examples/fungible-token/test-contract-defi/Cargo.toml index cc314dba7..4ae23344d 100644 --- a/examples/fungible-token/test-contract-defi/Cargo.toml +++ b/examples/fungible-token/test-contract-defi/Cargo.toml @@ -10,6 +10,10 @@ crate-type = ["cdylib"] [dependencies] near-sdk = { path = "../../../near-sdk" } near-contract-standards = { path = "../../../near-contract-standards" } +wasmcov = { version = "0.2", optional = true } [dev-dependencies] -near-sdk = { path = "../../../near-sdk", features = ["unit-testing"] } \ No newline at end of file +near-sdk = { path = "../../../near-sdk", features = ["unit-testing"] } + +[features] +wasmcov = ["dep:wasmcov"] \ No newline at end of file diff --git a/examples/fungible-token/test-contract-defi/src/lib.rs b/examples/fungible-token/test-contract-defi/src/lib.rs index 83671a1e8..8aaced549 100644 --- a/examples/fungible-token/test-contract-defi/src/lib.rs +++ b/examples/fungible-token/test-contract-defi/src/lib.rs @@ -5,6 +5,9 @@ use near_contract_standards::fungible_token::{receiver::FungibleTokenReceiver, B use near_sdk::json_types::U128; use near_sdk::{env, log, near, require, AccountId, Gas, PanicOnDefault, PromiseOrValue}; +#[cfg(all(feature = "wasmcov", target_family = "wasm"))] +wasmcov::near::add_coverage!(); + const BASE_GAS: u64 = 5_000_000_000_000; const PROMISE_CALL: u64 = 5_000_000_000_000; const GAS_FOR_FT_ON_TRANSFER: Gas = Gas::from_gas(BASE_GAS + PROMISE_CALL); diff --git a/examples/fungible-token/tests/workspaces.rs b/examples/fungible-token/tests/workspaces.rs index 2652a3cf2..2b364faa7 100644 --- a/examples/fungible-token/tests/workspaces.rs +++ b/examples/fungible-token/tests/workspaces.rs @@ -107,7 +107,7 @@ async fn test_storage_deposit_not_enough_deposit() -> anyhow::Result<()> { contract.view_account().await?.balance.saturating_sub(contract_balance_before_deposit); // contract receives a gas rewards for the function call, so it should gain some NEAR assert!(contract_balance_diff > NearToken::from_near(0)); - assert!(contract_balance_diff < NearToken::from_yoctonear(30_000_000_000_000_000_000)); + assert!(contract_balance_diff < NearToken::from_yoctonear(100_000_000_000_000_000_000)); Ok(()) } @@ -144,7 +144,7 @@ async fn test_storage_deposit_minimal_deposit() -> anyhow::Result<()> { // new_account is charged the transaction fee, so it should loose a bit more than minimal_deposit assert!(new_account_balance_diff > minimal_deposit); assert!( - new_account_balance_diff < minimal_deposit.saturating_add(NearToken::from_millinear(1)) + new_account_balance_diff < minimal_deposit.saturating_add(NearToken::from_yoctonear(2_000_000_000_000_000_000_000)) ); let contract_balance_diff = @@ -154,9 +154,8 @@ async fn test_storage_deposit_minimal_deposit() -> anyhow::Result<()> { // adjust the upper limit of the assertion to be more flexible for small variations in the gas reward received assert!( contract_balance_diff - < minimal_deposit.saturating_add(NearToken::from_yoctonear(50_000_000_000_000_000_000)) + < minimal_deposit.saturating_add(NearToken::from_yoctonear(2_000_000_000_000_000_000_000)) ); - Ok(()) } @@ -246,7 +245,7 @@ async fn test_storage_deposit_refunds_excessive_deposit() -> anyhow::Result<()> assert!(contract_balance_diff > minimal_deposit); assert!( contract_balance_diff - < minimal_deposit.saturating_add(NearToken::from_yoctonear(50_000_000_000_000_000_000)) + < minimal_deposit.saturating_add(NearToken::from_yoctonear(150_000_000_000_000_000_000)) ); Ok(()) diff --git a/examples/lockable-fungible-token/Cargo.toml b/examples/lockable-fungible-token/Cargo.toml index 01f9982e9..cc38038f7 100644 --- a/examples/lockable-fungible-token/Cargo.toml +++ b/examples/lockable-fungible-token/Cargo.toml @@ -9,6 +9,7 @@ crate-type = ["cdylib"] [dependencies] near-sdk = { path = "../../near-sdk", features = ["legacy"] } +wasmcov = { version = "0.2", optional = true } [dev-dependencies] anyhow = "1.0" @@ -16,6 +17,9 @@ tokio = { version = "1.14", features = ["full"] } near-sdk = { path = "../../near-sdk", features = ["unit-testing"] } near-workspaces = "0.11.1" +[features] +wasmcov = ["dep:wasmcov"] + [profile.release] codegen-units = 1 # Tell `rustc` to optimize for small code size. diff --git a/examples/lockable-fungible-token/build.sh b/examples/lockable-fungible-token/build.sh index 374e0c05e..b719942fc 100755 --- a/examples/lockable-fungible-token/build.sh +++ b/examples/lockable-fungible-token/build.sh @@ -1,6 +1,14 @@ #!/bin/bash -TARGET="${CARGO_TARGET_DIR:-../../target}" + +if [ -n "$WASMCOV_DIR" ]; then + TARGET="${WASMCOV_DIR}/target" + BUILD_COMMAND="cargo wasmcov build -- --features wasmcov" +else + TARGET="${CARGO_TARGET_DIR:-../../target}" + BUILD_COMMAND="cargo build" +fi + set -e cd "$(dirname $0)" -cargo build --target wasm32-unknown-unknown --release -cp $TARGET/wasm32-unknown-unknown/release/lockable_fungible_token.wasm ./res/ +$BUILD_COMMAND --target wasm32-unknown-unknown --release +cp $TARGET/wasm32-unknown-unknown/release/lockable_fungible_token.wasm ./res/ \ No newline at end of file diff --git a/examples/lockable-fungible-token/src/lib.rs b/examples/lockable-fungible-token/src/lib.rs index 824858dcf..c0df4fd2a 100644 --- a/examples/lockable-fungible-token/src/lib.rs +++ b/examples/lockable-fungible-token/src/lib.rs @@ -2,6 +2,9 @@ use near_sdk::collections::UnorderedMap; use near_sdk::{env, json_types::U128, near, AccountId, PanicOnDefault}; use std::collections::HashMap; +#[cfg(all(feature = "wasmcov", target_family = "wasm"))] +wasmcov::near::add_coverage!(); + type Balance = u128; #[derive(Default)] diff --git a/examples/merge_profdata.sh b/examples/merge_profdata.sh new file mode 100755 index 000000000..b82718703 --- /dev/null +++ b/examples/merge_profdata.sh @@ -0,0 +1,25 @@ +#!/bin/bash + +echo "Merging .profraw files" + +cargo wasmcov merge +cd $WASMCOV_DIR +mkdir lcov +ls profdata + +for file in "$WASMCOV_DIR"/profdata/*.profdata; do + profdata_file_name=$(basename "$file") + # replacing '-' by '_' and changing extension .profdata by .o + object_file_name=$(echo "$profdata_file_name" | sed 's/-/_/g' | sed 's/.profdata$/.o/') + + lcov_file_name=$(echo "$profdata_file_name" | sed 's/-/_/g' | sed 's/.profdata$/.lcov/') + + echo "Base profdata: $profdata_file_name" + echo "Object file name: $object_file_name" + echo "Lcov file name: $lcov_file_name" + echo "$WASMCOV_DIR/profdata/$profdata_file_name" + echo "$WASMCOV_DIR/target/$object_file_name" + echo "$WASMCOV_DIR/lcov/$lcov_file_name" + echo "------------" + llvm-cov export --instr-profile="$WASMCOV_DIR/profdata/$profdata_file_name" "$WASMCOV_DIR/target/$object_file_name" > "$WASMCOV_DIR/lcov/$lcov_file_name" +done \ No newline at end of file diff --git a/examples/mission-control/Cargo.toml b/examples/mission-control/Cargo.toml index d4f9d29b4..dd68982c8 100644 --- a/examples/mission-control/Cargo.toml +++ b/examples/mission-control/Cargo.toml @@ -9,10 +9,14 @@ crate-type = ["cdylib"] [dependencies] near-sdk = { path = "../../near-sdk" } +wasmcov = { version = "0.2", optional = true } [dev-dependencies] near-sdk = { path = "../../near-sdk", features = ["unit-testing"] } +[features] +wasmcov = ["dep:wasmcov"] + [profile.release] codegen-units = 1 # Tell `rustc` to optimize for small code size. diff --git a/examples/mission-control/build.sh b/examples/mission-control/build.sh index c5aa8b735..6ae795aae 100755 --- a/examples/mission-control/build.sh +++ b/examples/mission-control/build.sh @@ -1,7 +1,15 @@ #!/bin/bash -TARGET="${CARGO_TARGET_DIR:-../../target}" + +if [ -n "$WASMCOV_DIR" ]; then + TARGET="${WASMCOV_DIR}/target" + BUILD_COMMAND="cargo wasmcov build -- --features wasmcov" +else + TARGET="${CARGO_TARGET_DIR:-../../target}" + BUILD_COMMAND="cargo build" +fi + set -e cd "$(dirname $0)" -cargo build --target wasm32-unknown-unknown --release +$BUILD_COMMAND --target wasm32-unknown-unknown --release cp $TARGET/wasm32-unknown-unknown/release/mission_control.wasm ./res/ -#wasm-opt -Oz --output ./res/mission_control.wasm ./res/mission_control.wasm +#wasm-opt -Oz --output ./res/mission_control.wasm ./res/mission_control.wasm \ No newline at end of file diff --git a/examples/mission-control/src/account.rs b/examples/mission-control/src/account.rs index 15da4a915..3006b7347 100644 --- a/examples/mission-control/src/account.rs +++ b/examples/mission-control/src/account.rs @@ -5,15 +5,7 @@ use std::ops; use near_sdk::near; -#[derive( - PartialEq, - Eq, - PartialOrd, - Hash, - Clone, - Copy, - Debug, -)] +#[derive(PartialEq, Eq, PartialOrd, Hash, Clone, Copy, Debug)] #[near(serializers = [json, borsh])] pub struct Quantity(pub i32); diff --git a/examples/mission-control/src/agent.rs b/examples/mission-control/src/agent.rs index 81de55de4..06205e885 100644 --- a/examples/mission-control/src/agent.rs +++ b/examples/mission-control/src/agent.rs @@ -1,8 +1,8 @@ use crate::account::*; use crate::asset::*; use crate::rate::*; -use std::collections::HashMap; use near_sdk::near; +use std::collections::HashMap; #[derive(Clone)] #[near(serializers=[json, borsh])] diff --git a/examples/mission-control/src/asset.rs b/examples/mission-control/src/asset.rs index 87a65fcdf..1144c2b75 100644 --- a/examples/mission-control/src/asset.rs +++ b/examples/mission-control/src/asset.rs @@ -1,14 +1,6 @@ use near_sdk::near; -#[derive( - PartialEq, - Eq, - PartialOrd, - Ord, - Hash, - Clone, - Copy, -)] +#[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy)] #[near(serializers = [json, borsh])] pub enum Resource { Battery, @@ -17,15 +9,7 @@ pub enum Resource { PoseEstimation, } -#[derive( - PartialEq, - Eq, - PartialOrd, - Ord, - Hash, - Clone, - Copy, -)] +#[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy)] #[near(serializers = [json, borsh])] pub enum Reward { Score, @@ -35,15 +19,7 @@ pub enum Reward { Policy, } -#[derive( - PartialEq, - Eq, - PartialOrd, - Ord, - Hash, - Clone, - Copy, -)] +#[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy)] #[near(serializers = [json, borsh])] pub enum Asset { Resource(Resource), @@ -52,13 +28,7 @@ pub enum Asset { Trust, } -#[derive( - PartialEq, - Eq, - Hash, - PartialOrd, - Ord, -)] +#[derive(PartialEq, Eq, Hash, PartialOrd, Ord)] #[near(serializers = [json, borsh])] pub enum Exchange { MissionTimeWithResource, diff --git a/examples/mission-control/src/lib.rs b/examples/mission-control/src/lib.rs index 61522e7bc..e07efc09f 100644 --- a/examples/mission-control/src/lib.rs +++ b/examples/mission-control/src/lib.rs @@ -5,3 +5,6 @@ mod asset; mod macros; mod mission_control; mod rate; + +#[cfg(all(feature = "wasmcov", target_family = "wasm"))] +wasmcov::near::add_coverage!(); diff --git a/examples/mission-control/src/mission_control.rs b/examples/mission-control/src/mission_control.rs index 74f141a42..28c0808c0 100644 --- a/examples/mission-control/src/mission_control.rs +++ b/examples/mission-control/src/mission_control.rs @@ -2,11 +2,10 @@ use crate::account::*; use crate::agent::Agent; use crate::asset::*; use crate::rate::*; -use near_sdk::AccountId; use near_sdk::env; -use std::collections::HashMap; use near_sdk::near; - +use near_sdk::AccountId; +use std::collections::HashMap; #[near(serializers=[json, borsh], contract_state)] pub struct MissionControl { diff --git a/examples/mission-control/src/rate.rs b/examples/mission-control/src/rate.rs index 7390d69f4..6c1ad3549 100644 --- a/examples/mission-control/src/rate.rs +++ b/examples/mission-control/src/rate.rs @@ -1,7 +1,7 @@ use crate::account::*; use crate::asset::*; -use std::collections::HashMap; use near_sdk::near; +use std::collections::HashMap; #[derive(PartialEq, Eq)] #[near(serializers = [json, borsh])] diff --git a/examples/non-fungible-token/Cargo.toml b/examples/non-fungible-token/Cargo.toml index dcdeee7d9..acc4f35f8 100644 --- a/examples/non-fungible-token/Cargo.toml +++ b/examples/non-fungible-token/Cargo.toml @@ -4,6 +4,9 @@ version = "0.0.2" authors = ["Near Inc "] edition = "2021" +[dependencies] +wasmcov = { version = "0.2", optional = true } + [dev-dependencies] anyhow = "1.0" near-contract-standards = { path = "../../near-contract-standards" } @@ -23,3 +26,6 @@ overflow-checks = true [workspace] # remember to include a member for each contract members = ["nft", "test-approval-receiver", "test-token-receiver"] + +[features] +wasmcov = ["dep:wasmcov"] \ No newline at end of file diff --git a/examples/non-fungible-token/build.sh b/examples/non-fungible-token/build.sh index 361e33e71..b339c3845 100755 --- a/examples/non-fungible-token/build.sh +++ b/examples/non-fungible-token/build.sh @@ -1,8 +1,16 @@ #!/bin/bash -TARGET="${CARGO_TARGET_DIR:-../../target}" + +if [ -n "$WASMCOV_DIR" ]; then + TARGET="${WASMCOV_DIR}/target" + BUILD_COMMAND="cargo wasmcov build -- --features wasmcov" +else + TARGET="${CARGO_TARGET_DIR:-../../target}" + BUILD_COMMAND="cargo build" +fi + set -e cd "$(dirname $0)" -cargo build --all --target wasm32-unknown-unknown --release +$BUILD_COMMAND --all --target wasm32-unknown-unknown --release cp $TARGET/wasm32-unknown-unknown/release/approval_receiver.wasm ./res/ cp $TARGET/wasm32-unknown-unknown/release/non_fungible_token.wasm ./res/ -cp $TARGET/wasm32-unknown-unknown/release/token_receiver.wasm ./res/ +cp $TARGET/wasm32-unknown-unknown/release/token_receiver.wasm ./res/ \ No newline at end of file diff --git a/examples/non-fungible-token/nft/Cargo.toml b/examples/non-fungible-token/nft/Cargo.toml index 672e5e113..489456eb5 100644 --- a/examples/non-fungible-token/nft/Cargo.toml +++ b/examples/non-fungible-token/nft/Cargo.toml @@ -10,6 +10,10 @@ crate-type = ["cdylib"] [dependencies] near-sdk = { path = "../../../near-sdk" } near-contract-standards = { path = "../../../near-contract-standards" } +wasmcov = { version = "0.2", optional = true } [dev-dependencies] -near-sdk = { path = "../../../near-sdk", features = ["unit-testing"] } \ No newline at end of file +near-sdk = { path = "../../../near-sdk", features = ["unit-testing"] } + +[features] +wasmcov = ["dep:wasmcov"] \ No newline at end of file diff --git a/examples/non-fungible-token/nft/src/lib.rs b/examples/non-fungible-token/nft/src/lib.rs index 33c0ce5fe..88d6dbab8 100644 --- a/examples/non-fungible-token/nft/src/lib.rs +++ b/examples/non-fungible-token/nft/src/lib.rs @@ -32,6 +32,9 @@ use near_sdk::{ }; use std::collections::HashMap; +#[cfg(all(feature = "wasmcov", target_family = "wasm"))] +wasmcov::near::add_coverage!(); + #[derive(PanicOnDefault)] #[near(contract_state)] pub struct Contract { diff --git a/examples/non-fungible-token/test-approval-receiver/Cargo.toml b/examples/non-fungible-token/test-approval-receiver/Cargo.toml index 7f9036e07..288be0ffe 100644 --- a/examples/non-fungible-token/test-approval-receiver/Cargo.toml +++ b/examples/non-fungible-token/test-approval-receiver/Cargo.toml @@ -10,6 +10,10 @@ crate-type = ["cdylib"] [dependencies] near-sdk = { path = "../../../near-sdk" } near-contract-standards = { path = "../../../near-contract-standards" } +wasmcov = { version = "0.2", optional = true } [dev-dependencies] near-sdk = { path = "../../../near-sdk", features = ["unit-testing"] } + +[features] +wasmcov = ["dep:wasmcov"] \ No newline at end of file diff --git a/examples/non-fungible-token/test-approval-receiver/src/lib.rs b/examples/non-fungible-token/test-approval-receiver/src/lib.rs index 975e798e8..a5ef46bfd 100644 --- a/examples/non-fungible-token/test-approval-receiver/src/lib.rs +++ b/examples/non-fungible-token/test-approval-receiver/src/lib.rs @@ -5,6 +5,9 @@ use near_contract_standards::non_fungible_token::approval::NonFungibleTokenAppro use near_contract_standards::non_fungible_token::TokenId; use near_sdk::{env, log, near, require, AccountId, Gas, PanicOnDefault, PromiseOrValue}; +#[cfg(all(feature = "wasmcov", target_family = "wasm"))] +wasmcov::near::add_coverage!(); + /// It is estimated that we need to attach 5 TGas for the code execution and 5 TGas for cross-contract call const GAS_FOR_NFT_ON_APPROVE: Gas = Gas::from_tgas(10); diff --git a/examples/non-fungible-token/test-token-receiver/Cargo.toml b/examples/non-fungible-token/test-token-receiver/Cargo.toml index 2a4eeaadf..a85fd48e3 100644 --- a/examples/non-fungible-token/test-token-receiver/Cargo.toml +++ b/examples/non-fungible-token/test-token-receiver/Cargo.toml @@ -10,6 +10,10 @@ crate-type = ["cdylib"] [dependencies] near-sdk = { path = "../../../near-sdk" } near-contract-standards = { path = "../../../near-contract-standards" } +wasmcov = { version = "0.2", optional = true } [dev-dependencies] -near-sdk = { path = "../../../near-sdk", features = ["unit-testing"] } \ No newline at end of file +near-sdk = { path = "../../../near-sdk", features = ["unit-testing"] } + +[features] +wasmcov = ["dep:wasmcov"] \ No newline at end of file diff --git a/examples/non-fungible-token/test-token-receiver/src/lib.rs b/examples/non-fungible-token/test-token-receiver/src/lib.rs index ccedf9884..96cb07b35 100644 --- a/examples/non-fungible-token/test-token-receiver/src/lib.rs +++ b/examples/non-fungible-token/test-token-receiver/src/lib.rs @@ -5,6 +5,9 @@ use near_contract_standards::non_fungible_token::core::NonFungibleTokenReceiver; use near_contract_standards::non_fungible_token::TokenId; use near_sdk::{env, log, near, require, AccountId, Gas, PanicOnDefault, PromiseOrValue}; +#[cfg(all(feature = "wasmcov", target_family = "wasm"))] +wasmcov::near::add_coverage!(); + /// It is estimated that we need to attach 5 TGas for the code execution and 5 TGas for cross-contract call const GAS_FOR_NFT_ON_TRANSFER: Gas = Gas::from_tgas(10); diff --git a/examples/status-message/Cargo.toml b/examples/status-message/Cargo.toml index e72a5ba0e..b95f3e525 100644 --- a/examples/status-message/Cargo.toml +++ b/examples/status-message/Cargo.toml @@ -9,10 +9,14 @@ crate-type = ["cdylib"] [dependencies] near-sdk = { path = "../../near-sdk" } +wasmcov = { version = "0.2", optional = true } [dev-dependencies] near-sdk = { path = "../../near-sdk", features = ["unit-testing"] } +[features] +wasmcov = ["dep:wasmcov"] + [profile.release] codegen-units = 1 # Tell `rustc` to optimize for small code size. diff --git a/examples/status-message/build.sh b/examples/status-message/build.sh index ee45d0690..f5679b9f7 100755 --- a/examples/status-message/build.sh +++ b/examples/status-message/build.sh @@ -1,7 +1,15 @@ #!/bin/bash -TARGET="${CARGO_TARGET_DIR:-../../target}" + +if [ -n "$WASMCOV_DIR" ]; then + TARGET="${WASMCOV_DIR}/target" + BUILD_COMMAND="cargo wasmcov build -- --features wasmcov" +else + TARGET="${CARGO_TARGET_DIR:-../../target}" + BUILD_COMMAND="cargo build" +fi + set -e cd "$(dirname $0)" -cargo build --target wasm32-unknown-unknown --release +$BUILD_COMMAND --target wasm32-unknown-unknown --release cp $TARGET/wasm32-unknown-unknown/release/status_message.wasm ./res/ -#wasm-opt -Oz --output ./res/status_message.wasm ./res/status_message.wasm +#wasm-opt -Oz --output ./res/status_message.wasm ./res/status_message.wasm \ No newline at end of file diff --git a/examples/status-message/src/lib.rs b/examples/status-message/src/lib.rs index 142c68598..ac8e7cd7e 100644 --- a/examples/status-message/src/lib.rs +++ b/examples/status-message/src/lib.rs @@ -1,6 +1,9 @@ use near_sdk::store::LookupMap; use near_sdk::{env, log, near, AccountId, BorshStorageKey}; +#[cfg(all(feature = "wasmcov", target_family = "wasm"))] +wasmcov::near::add_coverage!(); + #[derive(BorshStorageKey)] #[near] struct RecordsKey; diff --git a/examples/test-contract/Cargo.toml b/examples/test-contract/Cargo.toml index 423588334..d9cd38c78 100644 --- a/examples/test-contract/Cargo.toml +++ b/examples/test-contract/Cargo.toml @@ -9,6 +9,7 @@ crate-type = ["cdylib"] [dependencies] near-sdk = { path = "../../near-sdk" } +wasmcov = { version = "0.2", optional = true } [profile.release] codegen-units = 1 @@ -19,4 +20,7 @@ debug = false panic = "abort" [dev-dependencies] -near-sdk = { path = "../../near-sdk", features = ["unit-testing"] } +near-sdk = { path = "../../near-sdk", features = ["unit-testing"] } + +[features] +wasmcov = ["dep:wasmcov"] \ No newline at end of file diff --git a/examples/test-contract/build.sh b/examples/test-contract/build.sh index c9a158b54..c535116c5 100755 --- a/examples/test-contract/build.sh +++ b/examples/test-contract/build.sh @@ -1,7 +1,15 @@ #!/bin/bash -TARGET="${CARGO_TARGET_DIR:-../../target}" + +if [ -n "$WASMCOV_DIR" ]; then + TARGET="${WASMCOV_DIR}/target" + BUILD_COMMAND="cargo wasmcov build -- --features wasmcov" +else + TARGET="${CARGO_TARGET_DIR:-../../target}" + BUILD_COMMAND="cargo build" +fi + set -e cd "$(dirname $0)" -cargo build --target wasm32-unknown-unknown --release +$BUILD_COMMAND --target wasm32-unknown-unknown --release cp $TARGET/wasm32-unknown-unknown/release/test_contract.wasm ./res/ -#wasm-opt -Oz --output ./res/test_contract.wasm ./res/test_contract.wasm +#wasm-opt -Oz --output ./res/test_contract.wasm ./res/test_contract.wasm \ No newline at end of file diff --git a/examples/test-contract/src/lib.rs b/examples/test-contract/src/lib.rs index 46444f9fc..036c6f322 100644 --- a/examples/test-contract/src/lib.rs +++ b/examples/test-contract/src/lib.rs @@ -1,5 +1,8 @@ use near_sdk::{env, near}; +#[cfg(all(feature = "wasmcov", target_family = "wasm"))] +wasmcov::near::add_coverage!(); + #[near(contract_state)] pub struct TestContract {} diff --git a/examples/versioned/Cargo.toml b/examples/versioned/Cargo.toml index 3f07f872f..6e8dfbcff 100644 --- a/examples/versioned/Cargo.toml +++ b/examples/versioned/Cargo.toml @@ -9,6 +9,7 @@ crate-type = ["cdylib"] [dependencies] near-sdk = { path = "../../near-sdk", features = ["unstable", "unit-testing"] } +wasmcov = { version = "0.2", optional = true } [profile.release] codegen-units = 1 @@ -19,4 +20,7 @@ debug = false panic = "abort" [dev-dependencies] -near-sdk = { path = "../../near-sdk", features = ["unit-testing"] } \ No newline at end of file +near-sdk = { path = "../../near-sdk", features = ["unit-testing"] } + +[features] +wasmcov = ["dep:wasmcov"] \ No newline at end of file diff --git a/examples/versioned/build.sh b/examples/versioned/build.sh index 2d9f6377e..f9d3626d1 100755 --- a/examples/versioned/build.sh +++ b/examples/versioned/build.sh @@ -1,6 +1,14 @@ #!/bin/bash -TARGET="${CARGO_TARGET_DIR:-../../target}" + +if [ -n "$WASMCOV_DIR" ]; then + TARGET="${WASMCOV_DIR}/target" + BUILD_COMMAND="cargo wasmcov build -- --features wasmcov" +else + TARGET="${CARGO_TARGET_DIR:-../../target}" + BUILD_COMMAND="cargo build" +fi + set -e cd "$(dirname $0)" -cargo build --target wasm32-unknown-unknown --release -cp $TARGET/wasm32-unknown-unknown/release/versioned.wasm ./res/ +$BUILD_COMMAND --target wasm32-unknown-unknown --release +cp $TARGET/wasm32-unknown-unknown/release/versioned.wasm ./res/ \ No newline at end of file diff --git a/examples/versioned/src/lib.rs b/examples/versioned/src/lib.rs index 6739de4d2..85e45e3a0 100644 --- a/examples/versioned/src/lib.rs +++ b/examples/versioned/src/lib.rs @@ -1,6 +1,9 @@ use near_sdk::store::IterableMap; use near_sdk::{env, log, near, AccountId, NearToken}; +#[cfg(all(feature = "wasmcov", target_family = "wasm"))] +wasmcov::near::add_coverage!(); + /// An example of a versioned contract. This is a simple contract that tracks how much /// each account deposits into the contract. In v1, a nonce is added to state which increments /// after each successful deposit.