@@ -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 | 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
@@ -102,33 +149,43 @@ done
102
149
103
150
# FUND OUR NEWLY CREATED ADDRESSES
104
151
105
- stakeAddr=" $( cat " ${UTXO_DIR} /utxo1.addr" ) "
106
- currentBalance=$( getAddressBalance " $stakeAddr " )
107
- echo " Funding stake addresses with ${AMOUNT_PER_DELEGATOR} "
108
-
109
152
for 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
+
110
157
cardano-cli latest transaction build \
111
158
--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 " \
115
162
--out-file " ${TRANSACTIONS_DIR} /tx${NODE_ID} .raw"
116
163
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
125
177
done
126
178
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) "
128
182
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
132
189
done
133
190
134
191
sleep 10
@@ -139,24 +196,26 @@ cardano-cli query utxo --whole-utxo --testnet-magic $NETWORK_MAGIC
139
196
140
197
# REGISTER STAKE ADDRESSES
141
198
142
- echo " Registering $( cat " ${DELEGATORS_DIR} /payment${NODE_ID} .addr" ) "
143
-
144
199
keyDeposit=2000000
145
200
stakeAddr=" $( cat " ${DELEGATORS_DIR} /payment1.addr" ) "
146
201
currentBalance=$( getAddressBalance " $stakeAddr " )
147
202
148
203
for NODE_ID in ${SP_NODES_ID} ; do
204
+ stakeAddr=$( cat " ${DELEGATORS_DIR} /payment${NODE_ID} .addr" )
205
+ echo " Registering $stakeAddr "
206
+
149
207
cardano-cli latest stake-address registration-certificate \
150
208
--stake-verification-key-file " ${DELEGATORS_DIR} /staking${NODE_ID} .vkey" \
151
209
--key-reg-deposit-amt ${keyDeposit} \
152
210
--out-file " ${TRANSACTIONS_DIR} /staking${NODE_ID} reg.cert"
153
211
154
212
# 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 " ;
156
214
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
160
219
done
161
220
162
221
cardano-cli latest transaction build \
@@ -173,9 +232,7 @@ for NODE_ID in ${SP_NODES_ID}; do
173
232
--signing-key-file " ${DELEGATORS_DIR} /staking${NODE_ID} .skey" \
174
233
--out-file " ${TRANSACTIONS_DIR} /reg-stake-tx${NODE_ID} .signed"
175
234
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"
179
236
done
180
237
181
238
updatedBalance=$( getAddressBalance " $stakeAddr " )
0 commit comments