bitcoin core – How you can seek for an handle in final block?

0
33


If in case you have txindex enabled and are utilizing a Linux, debian-based machine, set up jq: sudo apt set up jq

and save this script someplace as “any-name.sh”:

#!/bin/bash

BLOCK_HASH=$(bitcoin-cli getbestblockhash)
BLOCK_TEMPFILE=bestblock.json
VIN_TXNS_TEMPFILE=vin_txns.temp
VIN_N_TEMPFILE=vin_n.temp

# Test if all temp information and addresses file exists, and delete them in the event that they do earlier than recreating them.
if [ -f "${VIN_TXNS_TEMPFILE}" ]; then
  rm ${VIN_TXNS_TEMPFILE}
fi

if [ -f "${VIN_N_TEMPFILE}" ]; then
  rm ${VIN_N_TEMPFILE}
fi

if [ -f "${BLOCK_TEMPFILE}" ]; then
  rm ${BLOCK_TEMPFILE}
fi

# Get the block info in JSON format
bitcoin-cli getblock ${BLOCK_HASH} 2 > ${BLOCK_TEMPFILE}

# Get the block peak to be used in handle file identify
BLOCK_HEIGHT=$(jq ".peak" ${BLOCK_TEMPFILE})

# Test if the handle file exists. In that case, we don't must run the script once more.
ADDRESSES_FILE="Block_${BLOCK_HEIGHT}_Addresses.txt"
if [ -f "${ADDRESSES_FILE}" ]; then
  rm ${BLOCK_TEMPFILE}
  exit 0
fi
contact ${ADDRESSES_FILE}
contact ${VIN_TXNS_TEMPFILE}
contact ${VIN_N_TEMPFILE}
contact ${BLOCK_TEMPFILE}

# Get the block transaction ID's, excluding the primary transaction (coinbase)
jq ".tx[].vin[].txid" ${BLOCK_TEMPFILE} > ${VIN_TXNS_TEMPFILE}
tail -n +2 "${VIN_TXNS_TEMPFILE}" > "${VIN_TXNS_TEMPFILE}.temp" && mv "${VIN_TXNS_TEMPFILE}.temp" "${VIN_TXNS_TEMPFILE}"
sed -i 's/"//g' ${VIN_TXNS_TEMPFILE}

# Get the block transaction n's, excluding the primary transaction (coinbase)
jq ".tx[].vin[].vout" ${BLOCK_TEMPFILE} > ${VIN_N_TEMPFILE}
tail -n +2 "${VIN_N_TEMPFILE}" > "${VIN_N_TEMPFILE}.temp" && mv "${VIN_N_TEMPFILE}.temp" "${VIN_N_TEMPFILE}"
readarray -t n_vals < ${VIN_N_TEMPFILE}

# Delete the vin n tempfile
rm ${VIN_N_TEMPFILE}

# Get the block txout addresses
jq ".tx[].vout[].scriptPubKey.addresses | .[]?" ${BLOCK_TEMPFILE} > ${ADDRESSES_FILE}

# Delete the block tempfile
rm ${BLOCK_TEMPFILE}

# Get every transaction's vout addresses
i=0
whereas learn line; do
  val=${n_vals[${i}]}
  echo $(bitcoin-cli getrawtransaction "$line" true) | jq ".vout[${val}].scriptPubKey.addresses | .[]?" >> ${ADDRESSES_FILE}
  let "i=i+1"
carried out < ${VIN_TXNS_TEMPFILE}

# Delete transaction IDs tempfile
rm ${VIN_TXNS_TEMPFILE}

sed -i 's/"//g' ${ADDRESSES_FILE}
awk '!a[$0]++' ${ADDRESSES_FILE} > "${ADDRESSES_FILE}.temp" && mv "${ADDRESSES_FILE}.temp" "${ADDRESSES_FILE}"

Make the script executable: chmod +x any-name.sh

then you’ll be able to run the script with ./any-name.sh . Remember that this script takes a number of minutes to finish. I run a node on a raspberry pi 3b, and this script takes ~4 minutes to finish.

Script clarification:
The vins from the newest block include a transaction ID in addition to the vout index from that transaction that is being spent. We will use the command bitcoin-cli getrawtransaction <transactionhash> true to print JSON info from the transaction so long as we’ve txindex enabled. This info contains, most significantly for us, the handle that’s being spent (and “null” if the handle being spent will not be a regular handle sort). So for every vin within the block, we discover the related transaction, and get the handle on the specified vout index.



Supply hyperlink

Leave a reply