Wait for a Bitcoin tip change without tight polling

A worker can wait on its last observed block hash and wake on a tip change or deadline. The returned hash determines which case occurred.

Pass the previous tip explicitly

In Bitcoin Core 30 the second argument accepts the known tip. The first argument is a timeout in milliseconds. Supplying the hash also covers a change that occurs between reading the tip and starting the wait.

tip=$(bitcoin-cli getbestblockhash)
bitcoin-cli waitfornewblock 15000 "$tip"

Handle timeouts as ordinary responses

Compare the returned hash with the saved value. On timeout the RPC returns the current block, which can be the same one. A different hash can occur at the same height after a reorganisation. For a consumer that must process every intervening block, walk the chain from its stored checkpoint after waking.

bitcoincore.org waitfornewblock