citadel

My dotfiles, scripts and nix configs
git clone git://jb55.com/citadel
Log | Files | Refs | README | LICENSE

btc-blocktimes (1262B)


      1 #!/usr/bin/env bash
      2 
      3 set -e
      4 
      5 BITCOIN_RPCUSER=rpcuser
      6 BITCOIN_RPCPASS=rpcpass
      7 BITCOIN_RPCPORT=${BITCOIN_RPCPORT:-8332}
      8 BITCOIN_HOST=${BITCOIN_HOST:-127.0.0.1}
      9 
     10 net=${BTCNET:-mainnet}
     11 
     12 CLI="bcli -$BTCNET"
     13 
     14 blocks=$($CLI getblockcount)
     15 
     16 n=${1:-26}
     17 n1=$((blocks - n + 1))
     18 
     19 mkreq () {
     20     (
     21         printf '['
     22         echo ${1#","}
     23         printf ']'
     24     ) > /tmp/blocktimes-req.txt
     25 }
     26 
     27 doreq() {
     28     local host="http://${BITCOIN_HOST}:${BITCOIN_RPCPORT}"
     29     mkreq "$1"
     30     curl -s -u $BITCOIN_RPCUSER:$BITCOIN_RPCPASS \
     31         --data-binary @/tmp/blocktimes-req.txt \
     32         -H 'content-type: text/plain;' "$host"
     33 }
     34 
     35 heights=$(seq $n1 $blocks)
     36 
     37 blockhash_reqs=$(
     38     <<<"$heights" xargs printf ',{"jsonrpc": "1.0", "id":"blocktimes", "method": "getblockhash", "params": [%d] }\n'
     39 )
     40 
     41 txid_reqs=$(
     42     doreq "$blockhash_reqs" \
     43         | jq -rc '.[].result' \
     44         | xargs printf ',{"jsonrpc": "1.0", "id":"blocktimes", "method": "getblock", "params": ["%s", 1] }\n'
     45 )
     46 
     47 doreq "$txid_reqs" \
     48   | jq -rc '.[].result.time' \
     49   | sed '$!N;s/\n/,/' \
     50   | tee /tmp/blocktimes \
     51   | sed -e 's/\(.*\),\(.*\)/datediff -f %S @\1 @\2/g' \
     52   | sh \
     53   | paste -d, <(<<<"$heights" sed 'N;s/\n/,/') /dev/stdin \
     54   | cut -d, -f3 | awk '{ total += $1 } END { print (total/NR) }'
     55