stacks-network / stacks-core

The Stacks blockchain implementation
https://docs.stacks.co
GNU General Public License v3.0
3.01k stars 671 forks source link

Exit on startup if attempting to mine but no UTXOs found #5124

Closed obycode closed 2 months ago

obycode commented 2 months ago

Currently, we have a case where if your node config has miner = true but then the node is unable to find any UTXOs during startup, it will print a warning:

UTXOs not found for mnNXb9yT19qA718ox4hW8mLuGyYriygLPA. If this is unexpected, please ensure that your bitcoind instance is indexing transactions for the address mnNXb9yT19qA718ox4hW8mLuGyYriygLPA (importaddress)

but then keep running as a follower node. This problem would probably be easier to diagnose if instead of printing a warning and then continuing to run, it would just print that warning and exit.

CharlieC3 commented 2 months ago

Alternatively (and preferably if time permits): check for funded UTXOs during startup in a loop every N seconds. Exiting constantly can cause an exponential backoff delay between container restarts, making the operator wait longer than needed. A check loop instead of exiting would avoid this.

hstove commented 2 months ago

I do think some improvements here in stacks-core could be helpful, including:

But also, here's a simple bash script that polls and waits until more than 0 UTXOs are found:

#!/usr/bin/env bash

# check if UTXOs exist, otherwise exit
while :; do
    length=$(bitcoin-cli -rpcconnect=127.0.0.1 -rpcport=18443 -rpcuser=blah -rpcpassword=blah listunspent 1 999999 "[\"MY_MINER_ADDR\"]" | jq length)
    # echo "$length UTXOs"
    if [ "$length" -gt 0 ]; then
        echo "UTXOs found. Exiting."
        exit 0
    fi
    # echo "Not found. Sleeping.."
    sleep 10
done
CharlieC3 commented 2 months ago

@hstove thanks for the tip, that's similar to what I ended up using:


...
address_ready="$(bitcoin-cli \
  -rpcconnect=${BITCOIN_HOSTNAME} \
  -rpcport=${BITCOIN_PORT} \
  -rpcuser=${BITCOIN_CREDS} \
  -rpcpassword=${BITCOIN_CREDS} \
  -rpcwait listaddressgroupings | \
  jq -r --arg miner_address "${miner_address}" '.[][] | select(.[0]==$miner_address) | .[1] | select(. > 0)')"
...
obycode commented 2 months ago

We'll add a simple loop to recheck every X seconds, or exit if no UTXOs found after Y minutes.

jcnelson commented 2 months ago

Can we close this?

obycode commented 2 months ago

Yup.

blockstack-devops commented 3 weeks ago

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.