|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -e |
| 3 | + |
| 4 | +IMAGE_NAME=icp-cli-network-launcher-bitcoin |
| 5 | +# Find the running container built from our custom image |
| 6 | +BITCOIN_CONTAINER=$(docker ps --filter "ancestor=${IMAGE_NAME}" --format "{{.ID}}" | head -1) |
| 7 | + |
| 8 | +echo "=== Test 1: get_p2pkh_address returns a valid Bitcoin address ===" |
| 9 | +result=$(icp canister call backend get_p2pkh_address '()') && \ |
| 10 | + echo "$result" && \ |
| 11 | + echo "$result" | grep -q '"' && \ |
| 12 | + echo "PASS" || (echo "FAIL" && exit 1) |
| 13 | + |
| 14 | +echo "=== Test 2: get_p2tr_key_only_address returns a valid Bitcoin address ===" |
| 15 | +result=$(icp canister call backend get_p2tr_key_only_address '()') && \ |
| 16 | + echo "$result" && \ |
| 17 | + echo "$result" | grep -q '"' && \ |
| 18 | + echo "PASS" || (echo "FAIL" && exit 1) |
| 19 | + |
| 20 | +echo "=== Test 3: get_p2tr_address returns a valid Bitcoin address ===" |
| 21 | +result=$(icp canister call backend get_p2tr_address '()') && \ |
| 22 | + echo "$result" && \ |
| 23 | + echo "$result" | grep -q '"' && \ |
| 24 | + echo "PASS" || (echo "FAIL" && exit 1) |
| 25 | + |
| 26 | +echo "=== Test 4: get_current_fee_percentiles returns a vec ===" |
| 27 | +result=$(icp canister call backend get_current_fee_percentiles '()') && \ |
| 28 | + echo "$result" && \ |
| 29 | + echo "PASS" || (echo "FAIL" && exit 1) |
| 30 | + |
| 31 | +echo "=== Mining 101 blocks to fund test address ===" |
| 32 | +[ -n "$BITCOIN_CONTAINER" ] || (echo "ERROR: network launcher container not running — run 'icp network start -d' first" && exit 1) |
| 33 | +addr=$(icp canister call backend get_p2pkh_address '()' | grep -o '"[^"]*"' | tr -d '"') && \ |
| 34 | + docker exec "$BITCOIN_CONTAINER" bitcoin-cli -regtest \ |
| 35 | + -rpcuser=ic-btc-integration -rpcpassword=ic-btc-integration \ |
| 36 | + generatetoaddress 101 "$addr" > /dev/null && \ |
| 37 | + echo "mined 101 blocks to $addr" |
| 38 | + |
| 39 | +echo "=== Waiting for IC to sync Bitcoin blocks ===" |
| 40 | +_sync_addr=$(icp canister call backend get_p2pkh_address '()' | grep -o '"[^"]*"' | tr -d '"') |
| 41 | +# Poll get_utxos until it succeeds AND reports a non-zero tip_height. |
| 42 | +# Using get_utxos (not get_balance) because it reflects the definitive sync state: |
| 43 | +# get_balance can return stale non-zero values from previous runs while the bitcoin |
| 44 | +# integration canister is still syncing new blocks and rejecting all fresh calls. |
| 45 | +# The || { sleep; continue; } pattern retries on both rejection errors and zero height. |
| 46 | +_synced=false |
| 47 | +for _i in $(seq 1 60); do |
| 48 | + _result=$(icp canister call backend get_utxos "(\"$_sync_addr\")" 2>/dev/null) \ |
| 49 | + || { sleep 1; continue; } |
| 50 | + echo "$_result" | grep -qE 'tip_height = [1-9][0-9]*' \ |
| 51 | + || { sleep 1; continue; } |
| 52 | + echo "IC synced after ${_i}s" |
| 53 | + _synced=true |
| 54 | + break |
| 55 | +done |
| 56 | +[ "$_synced" = true ] || { echo "FAIL: IC did not sync within 60s"; exit 1; } |
| 57 | + |
| 58 | +echo "=== Test 5: get_balance returns non-zero after mining ===" |
| 59 | +addr=$(icp canister call backend get_p2pkh_address '()' | grep -o '"[^"]*"' | tr -d '"') && \ |
| 60 | + result=$(icp canister call backend get_balance "(\"$addr\")") && \ |
| 61 | + echo "$result" && \ |
| 62 | + echo "$result" | grep -qE '^\([1-9]' && \ |
| 63 | + echo "PASS" || (echo "FAIL" && exit 1) |
| 64 | + |
| 65 | +echo "=== Test 6: get_utxos returns synced chain state after mining ===" |
| 66 | +addr=$(icp canister call backend get_p2pkh_address '()' | grep -o '"[^"]*"' | tr -d '"') && \ |
| 67 | + result=$(icp canister call backend get_utxos "(\"$addr\")") && \ |
| 68 | + echo "$result" && \ |
| 69 | + echo "$result" | grep -qE 'tip_height = [1-9][0-9]*' && \ |
| 70 | + echo "PASS" || (echo "FAIL" && exit 1) |
| 71 | + |
| 72 | +echo "=== All tests passed ===" |
0 commit comments