|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Copyright (c) 2018 The Bitcoin Core developers |
| 3 | +# Distributed under the MIT software license, see the accompanying |
| 4 | +# file COPYING or http://www.opensource.org/licenses/mit-license.php. |
| 5 | + |
| 6 | +export LC_ALL=C |
| 7 | + |
| 8 | +# |
| 9 | +# Issue blocks using a local node at a given interval. |
| 10 | +# |
| 11 | + |
| 12 | +if [ $# -lt 3 ]; then |
| 13 | + echo "syntax: $0 <min_time> <max_time> <bitcoin-cli path> [<bitcoin-cli args>]" ; exit 1 |
| 14 | +fi |
| 15 | + |
| 16 | +function log() |
| 17 | +{ |
| 18 | + echo "- $(date +%H:%M:%S): $*" |
| 19 | +} |
| 20 | + |
| 21 | +min_time=$1 |
| 22 | +shift |
| 23 | +max_time=$1 |
| 24 | +shift |
| 25 | +bcli=$1 |
| 26 | +shift |
| 27 | + |
| 28 | +# https://stackoverflow.com/questions/806906/how-do-i-test-if-a-variable-is-a-number-in-bash |
| 29 | +re='^[0-9]+$' |
| 30 | +if ! [[ $min_time =~ $re ]] ; then |
| 31 | + echo "error: min_time $min_time is not a number" ; exit 1 |
| 32 | +fi |
| 33 | +if ! [[ $max_time =~ $re ]] ; then |
| 34 | + echo "error: max_time $max_time is not a number" ; exit 1 |
| 35 | +fi |
| 36 | + |
| 37 | +let randinterval=max_time-min_time |
| 38 | +if [ $randinterval -lt 1 ]; then |
| 39 | + echo "error: interval min..max must be positive and greater than 0" ; exit 1 |
| 40 | +fi |
| 41 | + |
| 42 | +if ! [ -e "$bcli" ]; then |
| 43 | + which "$bcli" &> /dev/null |
| 44 | + if [ $? -ne 0 ]; then |
| 45 | + echo "error: unable to find bitcoin binary: $bcli" ; exit 1 |
| 46 | + fi |
| 47 | +fi |
| 48 | + |
| 49 | +echo "- checking node status" |
| 50 | +conns=$($bcli "$@" getconnectioncount) |
| 51 | + |
| 52 | +if [ $? -ne 0 ]; then |
| 53 | + echo "node error" ; exit 1 |
| 54 | +fi |
| 55 | + |
| 56 | +if [ $conns -lt 1 ]; then |
| 57 | + echo "warning: node is not connected to any other node" |
| 58 | +fi |
| 59 | + |
| 60 | +log "node OK with $conns connection(s)" |
| 61 | +log "mining in random intervals between $min_time .. $max_time seconds" |
| 62 | +log "hit ^C to stop" |
| 63 | + |
| 64 | +while true; do |
| 65 | + let rv=$RANDOM%$randinterval |
| 66 | + echo -n -e "- $(date +%H:%M:%S): next block in $rv seconds..." |
| 67 | + sleep $rv |
| 68 | + echo -n -e " [submit]" |
| 69 | + blockhash=$($bcli "$@" getnewblockhex true) |
| 70 | + if [ $? -ne 0 ]; then |
| 71 | + echo "node error; aborting" ; exit 1 |
| 72 | + fi |
| 73 | + echo "" |
| 74 | + log "broadcasting block $($bcli "$@" getblockcount) $blockhash to $($bcli "$@" getconnectioncount) peer(s)" |
| 75 | +done |
0 commit comments