Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Coverage #2

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/coverage.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ on:
push:
branches:
- master
- coverage
pull_request:
branches:
- master
Expand All @@ -26,6 +25,7 @@ jobs:
- name: Install llvm-cov
run: cargo install cargo-llvm-cov

# TODO: exclude functions (e.g. down() in migrations)
- name: Generate coverage report
run: cargo llvm-cov --lcov --output-path coverage.lcov

Expand Down
173 changes: 0 additions & 173 deletions src/wallet/test/send.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3807,179 +3807,6 @@ fn witness_multiple_assets_success() {
);
}

#[test]
#[parallel]
fn min_confirmations() {
initialize();

let amount: u64 = 66;

// 2 minimum confirmations
println!("2 confirmations");
let min_confirmations = 2;

// wallets
let (wallet, online) = get_funded_wallet!();
let (rcv_wallet, rcv_online) = get_funded_wallet!();

// issue
let asset = test_issue_asset_nia(&wallet, &online, None);

// send
let receive_data = rcv_wallet
.blind_receive(
None,
None,
None,
TRANSPORT_ENDPOINTS.clone(),
min_confirmations,
)
.unwrap();
let recipient_map = HashMap::from([(
asset.asset_id.clone(),
vec![Recipient {
amount,
recipient_data: RecipientData::BlindedUTXO(
SecretSeal::from_str(&receive_data.recipient_id).unwrap(),
),
transport_endpoints: TRANSPORT_ENDPOINTS.clone(),
}],
)]);
let txid = wallet
.send(
online.clone(),
recipient_map,
false,
FEE_RATE,
min_confirmations,
)
.unwrap();
assert!(!txid.is_empty());

let rcv_transfer = get_test_transfer_recipient(&rcv_wallet, &receive_data.recipient_id);
let (rcv_transfer_data, _) = get_test_transfer_data(&rcv_wallet, &rcv_transfer);
let (_, rcv_batch_transfer) = get_test_transfer_related(&rcv_wallet, &rcv_transfer);
let (transfer, _, _) = get_test_transfer_sender(&wallet, &txid);
let (transfer_data, _) = get_test_transfer_data(&wallet, &transfer);
let (_, batch_transfer) = get_test_transfer_related(&wallet, &transfer);
assert_eq!(rcv_batch_transfer.min_confirmations, min_confirmations);
assert_eq!(batch_transfer.min_confirmations, min_confirmations);
assert_eq!(
rcv_transfer_data.status,
TransferStatus::WaitingCounterparty
);
assert_eq!(transfer_data.status, TransferStatus::WaitingCounterparty);

stop_mining();

// transfers progress to status WaitingConfirmations after a refresh
test_refresh_all(&rcv_wallet, &rcv_online);
test_refresh_asset(&wallet, &online, &asset.asset_id);

let (rcv_transfer_data, _) = get_test_transfer_data(&rcv_wallet, &rcv_transfer);
let (transfer_data, _) = get_test_transfer_data(&wallet, &transfer);
assert_eq!(
rcv_transfer_data.status,
TransferStatus::WaitingConfirmations
);
assert_eq!(transfer_data.status, TransferStatus::WaitingConfirmations);

// transfers remain in status WaitingConfirmations after a block is mined
mine(true);
test_refresh_all(&rcv_wallet, &rcv_online);
test_refresh_asset(&wallet, &online, &asset.asset_id);

let (rcv_transfer_data, _) = get_test_transfer_data(&rcv_wallet, &rcv_transfer);
let (transfer_data, _) = get_test_transfer_data(&wallet, &transfer);
assert_eq!(
rcv_transfer_data.status,
TransferStatus::WaitingConfirmations
);
assert_eq!(transfer_data.status, TransferStatus::WaitingConfirmations);

// transfers progress to status Settled after a second block is mined
mine(false);
test_refresh_all(&rcv_wallet, &rcv_online);
test_refresh_asset(&wallet, &online, &asset.asset_id);

let (rcv_transfer_data, _) = get_test_transfer_data(&rcv_wallet, &rcv_transfer);
let (transfer_data, _) = get_test_transfer_data(&wallet, &transfer);
assert_eq!(rcv_transfer_data.status, TransferStatus::Settled);
assert_eq!(transfer_data.status, TransferStatus::Settled);

// 0 minimum confirmations
println!("0 confirmations");
let min_confirmations = 0;

// send
let receive_data = rcv_wallet
.blind_receive(
None,
None,
None,
TRANSPORT_ENDPOINTS.clone(),
min_confirmations,
)
.unwrap();
let recipient_map = HashMap::from([(
asset.asset_id.clone(),
vec![Recipient {
amount,
recipient_data: RecipientData::BlindedUTXO(
SecretSeal::from_str(&receive_data.recipient_id).unwrap(),
),
transport_endpoints: TRANSPORT_ENDPOINTS.clone(),
}],
)]);
let txid = wallet
.send(
online.clone(),
recipient_map,
false,
FEE_RATE,
min_confirmations,
)
.unwrap();
assert!(!txid.is_empty());

let rcv_transfer = get_test_transfer_recipient(&rcv_wallet, &receive_data.recipient_id);
let (rcv_transfer_data, _) = get_test_transfer_data(&rcv_wallet, &rcv_transfer);
let (_, rcv_batch_transfer) = get_test_transfer_related(&rcv_wallet, &rcv_transfer);
let (transfer, _, _) = get_test_transfer_sender(&wallet, &txid);
let (transfer_data, _) = get_test_transfer_data(&wallet, &transfer);
let (_, batch_transfer) = get_test_transfer_related(&wallet, &transfer);
assert_eq!(rcv_batch_transfer.min_confirmations, min_confirmations);
assert_eq!(batch_transfer.min_confirmations, min_confirmations);
assert_eq!(
rcv_transfer_data.status,
TransferStatus::WaitingCounterparty
);
assert_eq!(transfer_data.status, TransferStatus::WaitingCounterparty);

stop_mining();

// transfers progress to status WaitingConfirmations after a refresh
test_refresh_all(&rcv_wallet, &rcv_online);
test_refresh_asset(&wallet, &online, &asset.asset_id);

let (rcv_transfer_data, _) = get_test_transfer_data(&rcv_wallet, &rcv_transfer);
let (transfer_data, _) = get_test_transfer_data(&wallet, &transfer);
assert_eq!(
rcv_transfer_data.status,
TransferStatus::WaitingConfirmations
);
assert_eq!(transfer_data.status, TransferStatus::WaitingConfirmations);

// transfers progress to status Settled before a block is mined
test_refresh_all(&rcv_wallet, &rcv_online);
test_refresh_asset(&wallet, &online, &asset.asset_id);

let (rcv_transfer_data, _) = get_test_transfer_data(&rcv_wallet, &rcv_transfer);
let (transfer_data, _) = get_test_transfer_data(&wallet, &transfer);
assert_eq!(rcv_transfer_data.status, TransferStatus::Settled);
assert_eq!(transfer_data.status, TransferStatus::Settled);
}

#[test]
#[serial]
fn spend_double_receive() {
Expand Down
67 changes: 44 additions & 23 deletions tests/coverage.sh
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
#!/bin/bash -e
#
# script to run projects tests and report code coverage
#
# uses grcov (https://github.com/mozilla/grcov)
# script to run project tests and report code coverage
# uses llvm-cov (https://github.com/taiki-e/cargo-llvm-cov)

LLVM_COV_OPTS=""
CARGO_TEST_OPTS=""

COVERAGE_DIR="target/coverage"
_die() {
echo "err $*"
exit 1
}

_tit() {
echo
Expand All @@ -13,28 +18,44 @@ _tit() {
echo "========================================"
}

help() {
echo "$NAME [-h|--help] [-t|--test] [--no-clean]"
echo ""
echo "options:"
echo " -h --help show this help message"
echo " -t --test only run these test(s)"
echo " --no-clean don't cleanup before the run"
}

# cmdline arguments
while [ -n "$1" ]; do
case $1 in
-h|--help)
help
exit 0
;;
-t|--test)
CARGO_TEST_OPTS="-- $2"
shift
;;
--no-clean)
LLVM_COV_OPTS="$1"
;;
*)
help
_die "unsupported argument \"$1\""
;;
esac
shift
done

_tit "installing requirements"
rustup component add llvm-tools-preview
cargo install grcov

_tit "gathering coverage info"
# enable code coverage instrumentation and set per-test profile file name
export RUSTFLAGS="-Cinstrument-coverage"
export RUSTDOCFLAGS="-Cinstrument-coverage"
export LLVM_PROFILE_FILE="$COVERAGE_DIR/%p-%m.profraw"
# run tests
rm -rf $COVERAGE_DIR && mkdir -p $COVERAGE_DIR
cargo test --no-fail-fast || true
cargo install cargo-llvm-cov

_tit "generating coverage report"
grcov $COVERAGE_DIR \
-s . \
--binary-path target/debug/ \
--output-types html \
--branch \
--ignore 'target/*' \
--ignore-not-existing \
-o $COVERAGE_DIR/
# shellcheck disable=2086
cargo llvm-cov --html $LLVM_COV_OPTS $CARGO_TEST_OPTS

## show html report location
echo "generated html report: $COVERAGE_DIR/html/index.html"
echo "generated html report: target/llvm-cov/html/index.html"