@@ -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 | 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
@@ -102,33 +149,43 @@ done
102149
103150# FUND OUR NEWLY CREATED ADDRESSES
104151
105- stakeAddr=" $( cat " ${UTXO_DIR} /utxo1.addr" ) "
106- currentBalance=$( getAddressBalance " $stakeAddr " )
107- echo " Funding stake addresses with ${AMOUNT_PER_DELEGATOR} "
108-
109152for NODE_ID in ${SP_NODES_ID} ; do
153+ sourceAddr=" $( cat " ${UTXO_DIR} /utxo${NODE_ID} .addr" ) "
154+ destAddr=" $( cat ${DELEGATORS_DIR} /payment${NODE_ID} .addr) "
155+ echo " Funding ${destAddr} with ${AMOUNT_PER_DELEGATOR} "
156+
110157 cardano-cli latest transaction build \
111158 --testnet-magic $NETWORK_MAGIC \
112- --tx-in " $( cardano-cli query utxo --address " $( cat " ${UTXO_DIR} /utxo ${NODE_ID} .addr " ) " --testnet-magic $NETWORK_MAGIC --out-file /dev/stdout | jq -r ' keys[0]' ) " \
113- --tx-out " $( cat ${DELEGATORS_DIR} /payment ${NODE_ID} .addr ) +${AMOUNT_PER_DELEGATOR} " \
114- --change-address " $( cat ${UTXO_DIR} /utxo ${NODE_ID} .addr ) " \
159+ --tx-in " $( cardano-cli query utxo --address " $sourceAddr " --testnet-magic $NETWORK_MAGIC --out-file /dev/stdout | jq -r ' keys[0]' ) " \
160+ --tx-out " ${destAddr} +${AMOUNT_PER_DELEGATOR} " \
161+ --change-address " $sourceAddr " \
115162 --out-file " ${TRANSACTIONS_DIR} /tx${NODE_ID} .raw"
116163
117- cardano-cli latest transaction sign --testnet-magic $NETWORK_MAGIC \
118- --tx-body-file " ${TRANSACTIONS_DIR} /tx${NODE_ID} .raw" \
119- --signing-key-file " ${UTXO_DIR} /utxo${NODE_ID} .skey" \
120- --out-file " ${TRANSACTIONS_DIR} /tx${NODE_ID} .signed"
121-
122- cardano-cli latest transaction submit \
123- --testnet-magic $NETWORK_MAGIC \
124- --tx-file " ${TRANSACTIONS_DIR} /tx${NODE_ID} .signed"
164+ cardano-cli latest transaction sign --testnet-magic $NETWORK_MAGIC \
165+ --tx-body-file " ${TRANSACTIONS_DIR} /tx${NODE_ID} .raw" \
166+ --signing-key-file " ${UTXO_DIR} /utxo${NODE_ID} .skey" \
167+ --out-file " ${TRANSACTIONS_DIR} /tx${NODE_ID} .signed"
168+
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
125177done
126178
127- updatedBalance=$( getAddressBalance " $stakeAddr " )
179+ # Wait for funds to reach destAddr
180+ for NODE_ID in ${SP_NODES_ID} ; do
181+ destAddr=" $( cat ${DELEGATORS_DIR} /payment${NODE_ID} .addr) "
128182
129- while [ " $currentBalance " -eq " $updatedBalance " ]; do
130- updatedBalance=$( getAddressBalance " $stakeAddr " )
131- sleep 1
183+ currentBalance=$( getAddressBalance " $destAddr " )
184+ while [ " $currentBalance " -lt " $AMOUNT_PER_DELEGATOR " ]; do
185+ echo " Waiting for funds to reach ${destAddr} ${currentBalance} < ${AMOUNT_PER_DELEGATOR} "
186+ currentBalance=$( getAddressBalance " $destAddr " )
187+ sleep 1
188+ done
132189done
133190
134191sleep 10
@@ -139,24 +196,26 @@ cardano-cli query utxo --whole-utxo --testnet-magic $NETWORK_MAGIC
139196
140197# REGISTER STAKE ADDRESSES
141198
142- echo " Registering $( cat " ${DELEGATORS_DIR} /payment${NODE_ID} .addr" ) "
143-
144199keyDeposit=2000000
145200stakeAddr=" $( cat " ${DELEGATORS_DIR} /payment1.addr" ) "
146201currentBalance=$( getAddressBalance " $stakeAddr " )
147202
148203for NODE_ID in ${SP_NODES_ID} ; do
204+ stakeAddr=$( cat " ${DELEGATORS_DIR} /payment${NODE_ID} .addr" )
205+ echo " Registering $stakeAddr "
206+
149207 cardano-cli latest stake-address registration-certificate \
150208 --stake-verification-key-file " ${DELEGATORS_DIR} /staking${NODE_ID} .vkey" \
151209 --key-reg-deposit-amt ${keyDeposit} \
152210 --out-file " ${TRANSACTIONS_DIR} /staking${NODE_ID} reg.cert"
153211
154212 # Wait for utxo to become available
155- txIn=" $( cardano-cli latest query utxo --address " $( cat " ${DELEGATORS_DIR} /payment ${NODE_ID} .addr " ) " --testnet-magic $NETWORK_MAGIC --out-file /dev/stdout | jq -r ' keys[0] ' ) " ;
213+ txIn=" null " ;
156214 while [ " $txIn " == " null" ]; do
157- echo " TxIN is null, retrying..."
158- txIn=" $( cardano-cli latest query utxo --address " $( cat " ${DELEGATORS_DIR} /payment${NODE_ID} .addr" ) " --testnet-magic $NETWORK_MAGIC --out-file /dev/stdout | jq -r ' keys[0]' ) " ;
159- sleep 1
215+ echo " Waiting for ${stakeAddr} UTxO..."
216+ txInJson=" $( cardano-cli latest query utxo --address " $stakeAddr " --testnet-magic $NETWORK_MAGIC --out-file /dev/stdout) " ;
217+ txIn=$( jq -r ' keys[0]' <<< " $txInJson" ) ;
218+ sleep 0.1
160219 done
161220
162221 cardano-cli latest transaction build \
@@ -173,9 +232,7 @@ for NODE_ID in ${SP_NODES_ID}; do
173232 --signing-key-file " ${DELEGATORS_DIR} /staking${NODE_ID} .skey" \
174233 --out-file " ${TRANSACTIONS_DIR} /reg-stake-tx${NODE_ID} .signed"
175234
176- cardano-cli latest transaction submit \
177- --testnet-magic $NETWORK_MAGIC \
178- --tx-file " ${TRANSACTIONS_DIR} /reg-stake-tx${NODE_ID} .signed"
235+ cardano-cli latest transaction submit --testnet-magic $NETWORK_MAGIC --tx-file " ${TRANSACTIONS_DIR} /reg-stake-tx${NODE_ID} .signed"
179236done
180237
181238updatedBalance=$( getAddressBalance " $stakeAddr " )
0 commit comments