bitcoin core – Find out how to seek for an tackle in final block?

0
20


When you’ve got 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

# Examine if all temp recordsdata 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 tackle file identify
BLOCK_HEIGHT=$(jq ".peak" ${BLOCK_TEMPFILE})

# Examine if the tackle file exists. If that's the 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"
accomplished < ${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 . Take into account 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 latest block comprise 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 now we have txindex enabled. This info contains, most significantly for us, the tackle that’s being spent (and “null” if the tackle being spent just isn’t a normal tackle kind). So for every vin within the block, we discover the related transaction, and get the tackle on the specified vout index.



Supply hyperlink

Leave a reply