Monday, May 20, 2024
HomeBitcoinblockchain - How you can test if the block chain is updated...

blockchain – How you can test 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 usually… so, I am going to present some choices on methods to keep away from getting banned.
First do not use the script below this replace; the variable enlargement Bash preforms causes a number of calls to https://blockchain.information in to in need of a time-frame. You possibly can strive including sleep 120 between the project and calls however that might be very gradual.
Second the important thing issues are; Checking native depend $(bitcoin-cli getblockcount) and Checking a well-known node $(wget -q -O - http://blockchain.information/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.information/q/getblockcount)-$(bitcoin-cli getblockcount))
# To print this with echo and date stamp
echo "    $(date) : $($(wget -q -O - http://blockchain.information/q/getblockcount)-$(bitcoin-cli getblockcount))"
# To redirect and exit with 0 : ie place in crontab
echo "$($(wget -q -O - http://blockchain.information/q/getblockcount)-$(bitcoin-cli getblockcount))" > /var/log/btc_blocks_left_to_sync.log 2>&1

The final one above is probably going the most effective for scripting because the file it saves to will 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.

attainable 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 point out the right perform was @LoHoris, nonetheless, the updates to Bitcoin node software program now makes use of bitcoin-cli getblocktemplate and never bitcoind for these lookups.

Because of each of them it’s attainable to give you all a mixture of each solutions wrapped right into a checker script; prepared on your customization and up to date for the yr 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.information/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 out there checks"
                ## name perform that runs all of it on a timer
                networked_sync
                echo "Node has $localCheck of $crossCheck blocks out there."
                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
        ## Operate to print distinction in node sync
        if [ $diffBlocks -eq 0 ]
        then
            echo "Sync is sweet 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’s going to run the opposite elements provided that bitcoin-cli getblocktemplate exhibits an error. In fact it’s attainable to test for particular error codes as a substitute and do issues for particular error codes… however this works positive as a template for me when establishing BTC nodes in a headless setting.
The opposite checks then evaluate native block depend verses https://blockchain.information and prints out what number of blocks your system is off. These solely run when native test of sync standing error out thus should you’re on a slower like @Steven Roose talked about then this script is right.

By default the wait time between checks is 600 seconds (10 minuets) to maintain community checks and native checks to a minimal, nonetheless, 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 out there checks"
                ## name perform that runs all of it on a timer
                networked_sync
                echo "Node has $localCheck of $crossCheck blocks out there."
                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 internet searchers can have a better time of this.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments