Skip to content

Commit

Permalink
Submit deposit transactions at a random interval
Browse files Browse the repository at this point in the history
  • Loading branch information
abailly committed Nov 30, 2024
1 parent a21c4d8 commit e169895
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 10 deletions.
19 changes: 9 additions & 10 deletions scripts/tx-gen/gen-deposits.sh
Original file line number Diff line number Diff line change
Expand Up @@ -132,24 +132,22 @@ try_building_tx () {

args+=" --total-utxo-value ${total_utxo_value}"

num_utxo=${#current_addresses[@]}

# shellcheck disable=SC2086
if ! cardano-cli conway transaction build-estimate $args; then
echo "failed to build txs with ${#current_addresses[@]} outputs"
return 1 # retry
return ${num_utxo}
elif [[ ${accumulated_deposits} -gt ${max_amount} ]] ; then
echo "${accumulated_deposits} greater than requested ${max_amount}"
return 1
return ${num_utxo}
else
# shellcheck disable=SC2012
tx_size=$(ls -l ${tmp_raw} | tr -s ' ' | cut -d ' ' -f 5)

# check the size of the raw transaction
if [[ tx_size -gt $((MAX_TX_SIZE - 300)) ]]; then
echo "tx too large"
return 1 #retry
return ${num_utxo}
else
cp ${tmp_raw} ${raw}
echo "built transaction with ${#current_addresses[@]} outputs (${tx_size}B)"
cardano-cli conway transaction sign --tx-file ${raw} --signing-key-file "${signing_key_file}" --testnet-magic 1 --out-file ${signed}
fi

Expand All @@ -172,12 +170,13 @@ for r in $random_u32; do

# try to construct a tx paying to all current addresses
try_building_tx
if [[ $? -eq 1 ]] ; then
num_utxo=$?
if [[ $num_utxo -gt 0 ]] ; then
signed=tx.${tx_num}.signed
echo "build transaction ${tx_num}: $(cardano-cli conway transaction txid --tx-file ${signed})"
tx_id=$(cardano-cli conway transaction txid --tx-file ${signed})
tx_num=$(( tx_num + 1 ))
total_utxo_value=$(cardano-cli debug transaction view --tx-file ${signed} | jq ".outputs[] | select ( .address == \"${change_address}\") | .amount.lovelace")
echo "remaining ₳$(( total_utxo_value / 1000000 )) (${total_utxo_value} lovelaces)"
echo "${tx_id}#$(( num_utxo -1 )) ${total_utxo_value}"
# need to retry
break
fi
Expand Down
48 changes: 48 additions & 0 deletions scripts/tx-gen/gen-multiple-txs.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/usr/bin/env bash
# Generate multiple deposit transactions
#
# This script will call gen-deposit.sh in a loop, reusing its output to generate
# more txs until the initial amount is exhausted.

set -e

[[ $# -eq 5 ]] || { echo "Usage: $0 <tx-in> <amount> <address> <key file> <increment> <interval>"; exit 1; }

tx_in=$1
amount=$2
address=$3
keyfile=$4
increment=$5

# Mean interval in seconds
mean_interval=$6

generate_exponential() {
rate=$(echo "1 / $mean_interval" | bc -l)

# Generate a random number between 0 and 1 using /dev/urandom
n=$(od -An -N4 -tu4 < /dev/urandom)

u=$(echo "$n / 4294967295" | bc -l)

# Compute the exponential interval using the inverse CDF formula
interval=$(echo "-l(1 - $u) / $rate" | bc -l)
echo "$interval"
}


while [[ $amount -gt 1000000000 ]]; do
latest_tx=( $(./gen-deposits.sh "$tx_in" "$amount" "$address" "$keyfile" "$increment") )
amount=${latest_tx[1]}
tx_in=${latest_tx[0]}
last_signed_tx=$(ls -1 tx.*.signed | sort -r -n -t '.' -k2 | head -1)

if ! cardano-cli conway transaction submit --tx-file ${last_signed_tx} --testnet-magic 1 --socket-path node.socket ; then
echo "Failed to submit ${last_signed_tx}"
exit 1
else
echo "Submitted ${last_signed_tx} : ${latest_tx[@]}"
fi

sleep $(generate_exponential)
done

0 comments on commit e169895

Please sign in to comment.