|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +cardano_cli_log() { |
| 4 | + local out retval _ |
| 5 | + echo cardano-cli "$@" >> "$START_CLUSTER_LOG" |
| 6 | + |
| 7 | + for _ in {1..3}; do |
| 8 | + retval=0 |
| 9 | + { out="$(cardano-cli "$@" 2>&1)"; } || retval="$?" |
| 10 | + |
| 11 | + case "$out" in |
| 12 | + *"resource vanished"*) |
| 13 | + printf "Retrying \`cardano-cli %s\`. Failure:\n%s\n" "$*" "$out" >&2 |
| 14 | + sleep 1 |
| 15 | + ;; |
| 16 | + *) |
| 17 | + if [ -n "$out" ]; then echo "$out"; fi |
| 18 | + break |
| 19 | + ;; |
| 20 | + esac |
| 21 | + done |
| 22 | + |
| 23 | + return "$retval" |
| 24 | +} |
| 25 | + |
| 26 | +check_spend_success() { |
| 27 | + for _ in {1..10}; do |
| 28 | + if ! cardano_cli_log latest query utxo "$@" \ |
| 29 | + --testnet-magic "$NETWORK_MAGIC" --output-text | grep -q lovelace; then |
| 30 | + return 0 |
| 31 | + fi |
| 32 | + sleep 3 |
| 33 | + done |
| 34 | + return 1 |
| 35 | +} |
| 36 | + |
| 37 | +get_txins() { |
| 38 | + local txin_addr stop_txin_amount txhash txix amount _ |
| 39 | + |
| 40 | + txin_addr="${1:?"Missing TxIn address"}" |
| 41 | + stop_txin_amount="${2:?"Missing stop TxIn amount"}" |
| 42 | + |
| 43 | + stop_txin_amount="$((stop_txin_amount + 2000000))" |
| 44 | + |
| 45 | + # Repeat in case `query utxo` fails |
| 46 | + for _ in {1..3}; do |
| 47 | + TXINS=() |
| 48 | + TXIN_AMOUNT=0 |
| 49 | + while read -r txhash txix amount _; do |
| 50 | + if [ -z "$txhash" ] || [ -z "$txix" ] || [ "$amount" -lt 1000000 ]; then |
| 51 | + continue |
| 52 | + fi |
| 53 | + TXIN_AMOUNT="$((TXIN_AMOUNT + amount))" |
| 54 | + TXINS+=("--tx-in" "${txhash}#${txix}") |
| 55 | + if [ "$TXIN_AMOUNT" -ge "$stop_txin_amount" ]; then |
| 56 | + break |
| 57 | + fi |
| 58 | + done <<< "$(cardano_cli_log latest query utxo \ |
| 59 | + --testnet-magic "$NETWORK_MAGIC" \ |
| 60 | + --output-text \ |
| 61 | + --address "$txin_addr" | |
| 62 | + grep -E "lovelace$|[0-9]$|lovelace \+ TxOutDatumNone$")" |
| 63 | + |
| 64 | + if [ "$TXIN_AMOUNT" -ge "$stop_txin_amount" ]; then |
| 65 | + break |
| 66 | + fi |
| 67 | + done |
| 68 | +} |
| 69 | + |
| 70 | +get_address_balance() { |
| 71 | + local txhash txix amount total_amount _ |
| 72 | + |
| 73 | + # Repeat in case `query utxo` fails |
| 74 | + for _ in {1..3}; do |
| 75 | + total_amount=0 |
| 76 | + while read -r txhash txix amount _; do |
| 77 | + if [ -z "$txhash" ] || [ -z "$txix" ]; then |
| 78 | + continue |
| 79 | + fi |
| 80 | + total_amount="$((total_amount + amount))" |
| 81 | + done <<< "$(cardano-cli latest query utxo "$@" --output-text | grep " lovelace")" |
| 82 | + |
| 83 | + if [ "$total_amount" -gt 0 ]; then |
| 84 | + break |
| 85 | + fi |
| 86 | + done |
| 87 | + |
| 88 | + echo "$total_amount" |
| 89 | +} |
| 90 | + |
| 91 | +get_epoch() { |
| 92 | + cardano_cli_log latest query tip --testnet-magic "$NETWORK_MAGIC" | jq -r '.epoch' |
| 93 | +} |
| 94 | + |
| 95 | +get_slot() { |
| 96 | + local future_offset="${1:-0}" |
| 97 | + cardano_cli_log latest query tip --testnet-magic "$NETWORK_MAGIC" | jq -r ".slot + $future_offset" |
| 98 | +} |
| 99 | + |
| 100 | +get_era() { |
| 101 | + cardano_cli_log latest query tip --testnet-magic "$NETWORK_MAGIC" | jq -r ".era" |
| 102 | +} |
| 103 | + |
| 104 | +get_sec_to_epoch_end() { |
| 105 | + cardano_cli_log latest query tip --testnet-magic "$NETWORK_MAGIC" | |
| 106 | + jq -r "$SLOT_LENGTH * .slotsToEpochEnd | ceil" |
| 107 | +} |
| 108 | + |
| 109 | +wait_for_era() { |
| 110 | + local era |
| 111 | + |
| 112 | + for _ in {1..10}; do |
| 113 | + era="$(get_era)" |
| 114 | + if [ "$era" = "$1" ]; then |
| 115 | + return |
| 116 | + fi |
| 117 | + sleep 3 |
| 118 | + done |
| 119 | + |
| 120 | + echo "Unexpected era '$era' instead of '$1'" >&2 |
| 121 | + exit 1 |
| 122 | +} |
| 123 | + |
| 124 | +wait_for_epoch() { |
| 125 | + local start_epoch |
| 126 | + local target_epoch="$1" |
| 127 | + local epochs_to_go=1 |
| 128 | + local sec_to_epoch_end |
| 129 | + local sec_to_sleep |
| 130 | + local curr_epoch |
| 131 | + local _ |
| 132 | + |
| 133 | + start_epoch="$(get_epoch)" |
| 134 | + |
| 135 | + if [ "$start_epoch" -ge "$target_epoch" ]; then |
| 136 | + return |
| 137 | + else |
| 138 | + epochs_to_go="$((target_epoch - start_epoch))" |
| 139 | + fi |
| 140 | + |
| 141 | + sec_to_epoch_end="$(get_sec_to_epoch_end)" |
| 142 | + sec_to_sleep="$(( sec_to_epoch_end + ((epochs_to_go - 1) * EPOCH_SEC) ))" |
| 143 | + sleep "$sec_to_sleep" |
| 144 | + |
| 145 | + for _ in {1..10}; do |
| 146 | + curr_epoch="$(get_epoch)" |
| 147 | + if [ "$curr_epoch" -ge "$target_epoch" ]; then |
| 148 | + return |
| 149 | + fi |
| 150 | + sleep 3 |
| 151 | + done |
| 152 | + |
| 153 | + echo "Unexpected epoch '$curr_epoch' instead of '$target_epoch'" >&2 |
| 154 | + exit 1 |
| 155 | +} |
0 commit comments