@@ -53,6 +53,53 @@ getAddressBalance() {
53
53
echo ${total_balance}
54
54
}
55
55
56
+ getBlockHeight () {
57
+ cardano-cli query tip --testnet-magic $NETWORK_MAGIC | jq -r ' .block'
58
+ }
59
+
60
+ submitTransactionWithRetry () {
61
+ local txFile=$1
62
+ local retryCount=${2:- 0}
63
+ local numberOfConfirmations=5
64
+
65
+ if [ " $retryCount " -ge 5 ]; then
66
+ echo " Transaction failed after $retryCount retries"
67
+ return 1
68
+ fi
69
+
70
+ cardano-cli latest transaction submit --testnet-magic $NETWORK_MAGIC --tx-file " $txFile "
71
+
72
+ local txId=$( cardano-cli latest transaction txid --tx-file " $txFile " )
73
+
74
+ local mempoolTx=" true"
75
+ while [ " $mempoolTx " == " true" ]; do
76
+ echo " Transaction is still in the mempool, waiting ${txId} "
77
+ sleep 1
78
+ mempoolTx=$( cardano-cli latest query tx-mempool --testnet-magic $NETWORK_MAGIC tx-exists ${txId} --out-file /dev/stdout | jq -r ' .exists' )
79
+ done
80
+
81
+ local initialTip=$( getBlockHeight)
82
+ local currentTip=$initialTip
83
+ local utxo=" null"
84
+ while [ $(( $currentTip - $initialTip )) -lt $numberOfConfirmations ]; do
85
+ sleep 1
86
+ utxo=$( cardano-cli query utxo --tx-in " ${txId} #0" --testnet-magic $NETWORK_MAGIC --out-file /dev/stdout --volatile-tip | jq -r ' keys[0]' )
87
+ if [ " $utxo " == " null" ]; then
88
+ # Transaction was rolled back
89
+ break ;
90
+ fi
91
+ currentTip=$( getBlockHeight)
92
+ done
93
+
94
+ if [ " $utxo " == " null" ]; then
95
+ echo " Transaction rolled back, retrying ${retryCount} ..."
96
+ submitTransactionWithRetry " $txFile " $(( retryCount + 1 ))
97
+ return $?
98
+ fi
99
+
100
+ echo " Transaction successful ${txId} "
101
+ }
102
+
56
103
NETWORK_MAGIC=888
57
104
UTXO_DIR=network-files/utxo-keys
58
105
TRANSACTIONS_DIR=network-files/transactions
@@ -119,21 +166,25 @@ for NODE_ID in ${SP_NODES_ID}; do
119
166
--signing-key-file " ${UTXO_DIR} /utxo${NODE_ID} .skey" \
120
167
--out-file " ${TRANSACTIONS_DIR} /tx${NODE_ID} .signed"
121
168
122
- cardano-cli latest transaction submit \
123
- --testnet-magic $NETWORK_MAGIC \
124
- --tx-file " ${TRANSACTIONS_DIR} /tx${NODE_ID} .signed"
125
-
169
+ if [ " $NODE_ID " -eq 1 ]; then
170
+ # This is the first transaction after startin the network.
171
+ # It usually takes a long time to be included in a block and often rolled back, so use submit with retry.
172
+ # Do not use submit with retry for regular transactions because it waits for 5 block confirmations before it returns
173
+ submitTransactionWithRetry " ${TRANSACTIONS_DIR} /tx${NODE_ID} .signed"
174
+ else
175
+ cardano-cli latest transaction submit --testnet-magic $NETWORK_MAGIC --tx-file " ${TRANSACTIONS_DIR} /tx${NODE_ID} .signed"
176
+ fi
126
177
done
127
178
128
179
# Wait for funds to reach destAddr
129
180
for NODE_ID in ${SP_NODES_ID} ; do
130
181
destAddr=" $( cat ${DELEGATORS_DIR} /payment${NODE_ID} .addr) "
131
182
132
- currentBalance=0
183
+ currentBalance=$( getAddressBalance " $destAddr " )
133
184
while [ " $currentBalance " -lt " $AMOUNT_PER_DELEGATOR " ]; do
134
185
echo " ==== Waiting for funding ${currentBalance} < $AMOUNT_PER_DELEGATOR ==="
135
186
currentBalance=$( getAddressBalance " $destAddr " )
136
- sleep 10
187
+ sleep 1
137
188
done
138
189
done
139
190
@@ -165,7 +216,7 @@ for NODE_ID in ${SP_NODES_ID}; do
165
216
echo " UTxO not found for ${stakeAddr} , retrying..."
166
217
txInJson=" $( cardano-cli latest query utxo --address " $stakeAddr " --testnet-magic $NETWORK_MAGIC --out-file /dev/stdout) " ;
167
218
txIn=$( jq -r ' keys[0]' <<< " $txInJson" ) ;
168
- sleep 10
219
+ sleep 0.1
169
220
done
170
221
171
222
cardano-cli latest transaction build \
@@ -182,9 +233,7 @@ for NODE_ID in ${SP_NODES_ID}; do
182
233
--signing-key-file " ${DELEGATORS_DIR} /staking${NODE_ID} .skey" \
183
234
--out-file " ${TRANSACTIONS_DIR} /reg-stake-tx${NODE_ID} .signed"
184
235
185
- cardano-cli latest transaction submit \
186
- --testnet-magic $NETWORK_MAGIC \
187
- --tx-file " ${TRANSACTIONS_DIR} /reg-stake-tx${NODE_ID} .signed"
236
+ cardano-cli latest transaction submit --testnet-magic $NETWORK_MAGIC --tx-file " ${TRANSACTIONS_DIR} /reg-stake-tx${NODE_ID} .signed"
188
237
done
189
238
190
239
updatedBalance=$( getAddressBalance " $stakeAddr " )
0 commit comments