blockchain – Easy methods to verify if the block chain is updated utilizing bitcoind or json-rpc?

0
155
blockchain – Easy methods to verify if the block chain is updated utilizing bitcoind or json-rpc?


Replace August 7 2015 ~ begin

So after having an IP or two banned due to checking to typically… so, I am going to present some choices on tips on how to keep away from getting banned.
First do not use the script beneath this replace; the variable enlargement Bash preforms causes a number of calls to https://blockchain.data in to in need of a time-frame. You’ll be able to attempt including sleep 120 between the project and calls however that will be very sluggish.
Second the important thing issues are; Checking native rely $(bitcoin-cli getblockcount) and Checking a well-known node $(wget -q -O - http://blockchain.data/q/getblockcount) and Checking native sync state $(bitcoin-cli getblocktemplate)

To mix the Blockchain out of your node to a different in a single line

$($(wget -q -O - http://blockchain.data/q/getblockcount)-$(bitcoin-cli getblockcount))
# To print this with echo and date stamp
echo "    $(date) : $($(wget -q -O - http://blockchain.data/q/getblockcount)-$(bitcoin-cli getblockcount))"
# To redirect and exit with 0 : ie place in crontab
echo "$($(wget -q -O - http://blockchain.data/q/getblockcount)-$(bitcoin-cli getblockcount))" > /var/log/btc_blocks_left_to_sync.log 2>&1

The final one above is probably going the perfect for scripting because the file it saves to may be learn for simply the quantity inside or the final time it was modifide.

Replace August 7 2015 ~ finish

Certainly the right perform name is proven there, @BinaryMage, thanks.

potential duplicate of When downloading the blockchain for the primary time, how do I do know when it is accomplished? – BinaryMage Apr 4 ’13 at 0:50

The individual to indicate the right perform was @LoHoris, nevertheless, the updates to Bitcoin node software program now makes use of bitcoin-cli getblocktemplate and never bitcoind for these lookups.

Due to each of them it’s potential to give you all a mix of each solutions wrapped right into a checker script; prepared in your customization and up to date for the 12 months 2015


    #!/bin/bash
    timed_wait=600
    vars_to_load(){
        ## Set vars to perform such that they're set upon each name
        localCheck=$(bitcoin-cli getblockcount)
        crossCheck=$(wget -q -O - http://blockchain.data/q/getblockcount)
        diffBlocks=$(($crossCheck-$localCheck))
        localSize=$(ls -hal ~/.bitcoin/blocks | awk '/complete/{print $2}')
    }
    check_sync(){
        whereas true
        do
            current_time=$(date)
            echo $current_time
            bitcoin-cli getblocktemplate
            if [ $? = 0 ]
            then
                echo "All synced up"
            else
                echo "Uh oh, operating obtainable checks"
                ## name perform that runs all of it on a timer
                networked_sync
                echo "Node has $localCheck of $crossCheck blocks obtainable."
                echo "Dimension of native blockchain is $localSize"
            fi
            echo "Sleeping for $timed_wait seconds or $(($timed_wait/60)) minuets."
            sleep $timed_wait
        accomplished
    }
    networked_sync(){
        vars_to_load
        ## Perform to print distinction in node sync
        if [ $diffBlocks -eq 0 ]
        then
            echo "Sync is nice with $diffBlocks blocks off"
        else
            echo "Sync has $diffBlocks blocks left to obtain"
        fi
    }
    check_sync

This script first checks the exit standing of bitcoin-cli getblocktemplate as a result of something aside from 0 is normally an error it will run the opposite components provided that bitcoin-cli getblocktemplate reveals an error. In fact it’s potential to verify for particular error codes as an alternative and do issues for particular error codes… however this works tremendous as a template for me when establishing BTC nodes in a headless surroundings.
The opposite checks then examine native block rely verses https://blockchain.data and prints out what number of blocks your system is off. These solely run when native verify of sync standing error out thus should you’re on a slower like @Steven Roose talked about then this script is good.

By default the wait time between checks is 600 seconds (10 minuets) to maintain community checks and native checks to a minimal, nevertheless, for logging and higher useful resource administration it’s straightforward sufficient to remark out the whereas, do, and accomplished and sleep $timed_wait strains. And add one line to crontab to perform operating on a schedule. Final modify the very finish of the script to put in writing output to a file.


The perform modified;

    check_sync(){
    #   whereas true
    #   do
            current_time=$(date)
            echo $current_time
            bitcoin-cli getblocktemplate
            if [ $? = 0 ]
            then
                echo "All synced up"
            else
                echo "Uh oh, operating obtainable checks"
                ## name perform that runs all of it on a timer
                networked_sync
                echo "Node has $localCheck of $crossCheck blocks obtainable."
                echo "Dimension of native blockchain is $localSize"
            fi
            echo "Sleeping for $timed_wait seconds or $(($timed_wait/60)) minuets."
    #       sleep $timed_wait
    #   accomplished
    }

And to run each 10 minuets

    sudo nano /and so forth/crontab
        */10 *  * * *   root    /pathTo/script.sh 2>&1

Then to allow logging modify the decision that begins each factor inside the script;

    check_sync | tee -a /tmp/BTCsync_log.txt

Hopefully that is acceptable sufficient of a solution to mark this query as [Solved] such that future net searchers can have a neater time of this.

LEAVE A REPLY

Please enter your comment!
Please enter your name here