@@ -53,6 +53,53 @@ getAddressBalance() {
5353 echo ${total_balance}
5454}
5555
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+
56103NETWORK_MAGIC=888
57104UTXO_DIR=network-files/utxo-keys
58105TRANSACTIONS_DIR=network-files/transactions
@@ -119,21 +166,25 @@ for NODE_ID in ${SP_NODES_ID}; do
119166 --signing-key-file " ${UTXO_DIR} /utxo${NODE_ID} .skey" \
120167 --out-file " ${TRANSACTIONS_DIR} /tx${NODE_ID} .signed"
121168
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
126177done
127178
128179# Wait for funds to reach destAddr
129180for NODE_ID in ${SP_NODES_ID} ; do
130181 destAddr=" $( cat ${DELEGATORS_DIR} /payment${NODE_ID} .addr) "
131182
132- currentBalance=0
183+ currentBalance=$( getAddressBalance " $destAddr " )
133184 while [ " $currentBalance " -lt " $AMOUNT_PER_DELEGATOR " ]; do
134185 echo " ==== Waiting for funding ${currentBalance} < $AMOUNT_PER_DELEGATOR ==="
135186 currentBalance=$( getAddressBalance " $destAddr " )
136- sleep 10
187+ sleep 1
137188 done
138189done
139190
@@ -165,7 +216,7 @@ for NODE_ID in ${SP_NODES_ID}; do
165216 echo " UTxO not found for ${stakeAddr} , retrying..."
166217 txInJson=" $( cardano-cli latest query utxo --address " $stakeAddr " --testnet-magic $NETWORK_MAGIC --out-file /dev/stdout) " ;
167218 txIn=$( jq -r ' keys[0]' <<< " $txInJson" ) ;
168- sleep 10
219+ sleep 0.1
169220 done
170221
171222 cardano-cli latest transaction build \
@@ -182,9 +233,7 @@ for NODE_ID in ${SP_NODES_ID}; do
182233 --signing-key-file " ${DELEGATORS_DIR} /staking${NODE_ID} .skey" \
183234 --out-file " ${TRANSACTIONS_DIR} /reg-stake-tx${NODE_ID} .signed"
184235
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"
188237done
189238
190239updatedBalance=$( getAddressBalance " $stakeAddr " )
0 commit comments