1

In my test environment, which is ssh,bash and screen, I'm launching a monerod followed by several other programs wallet-rpc/cli and more, which are all depending on monerod.

To prevent any confusion, I would like to wait for monerod to be fully synced, before launching the dependent programs.

Can I RPC the daemon, and get the local blockheight / network blockheight, so I can wait for the sync to complete?

My solution (Updated to wait if daemon is not ready)

get_info() {
    local JSON=$( curl -s -X POST http://127.0.0.1:28081/json_rpc -d '{"jsonrpc":"2.0","id":"0","method":"get_info"}' -H 'Content-Type: application/json' | grep "$1" | grep -o '[0-9]*' )
    [ "${JSON}" = "" ] \
    || {
        echo ${JSON}
        return 0
    } \
    && {
        return 1
    }
}

wait_for_sync() {
    local TARGET=0
    local HEIGHT=0
    local CNT=0
    until [[ ${TARGET} -ne 0 ]]
    do
        ((CNT++))
        echo -en "${WHITE}Waiting for daemon: ${RED}${CNT}${RESTORE}\r"
        sleep 1
        TARGET=$(get_info '"target_height"' | grep -o '[0-9]*')
    done
    echo -ne "\033[2K"
    echo -n "${WHITE}Waiting for daemon to sync: ${HEIGHT}/${TARGET}${RESTORE}"
    while [ ${HEIGHT} -lt ${TARGET} ]
    do
        HEIGHT=$(get_info '"height"' | grep -o '[0-9]*')
        echo -ne "\r${WHITE}Waiting for daemon to sync: ${HEIGHT}/${TARGET}${RESTORE}"
        sleep 1
    done
    echo " ${GREEN}OK Synced${RESTORE}"
}

1 Answer 1

2

I have found the best way to deal with this is just to start them all up at once. Monero RPC methods that are dependent on a fully synchronized blockchain will return with a relevant status code if the chain is not fully synced, and thus code can deal with this scenario as and when this happens.

If you find a situation it's imperative the blockchain is fully synchronized before starting some other tool/service, you could call the daemon RPC method get_info which shows you the daemon's current height in the height field and the blockchain target height in the target_height field. Therefore, you are syncing whilst height is less than target_height.

4
  • I was thinking about the get_info but. Right now it says "height": 1306035, and "target_height": 1306014, so that not really useful. Yes, I'm on testnet. Commented Sep 23, 2019 at 17:32
  • I'm pretty sure height greater than target height just means you have mined blocks that aren't confirmed on the blockchain yet. Therefore height greater than target height means you have fully synced.
    – jtgrassie
    Commented Sep 23, 2019 at 17:41
  • For the record, I also popped blocks to test and verify my answer above w.r.t. get_info.
    – jtgrassie
    Commented Sep 23, 2019 at 17:45
  • So, I did the --pop-blocks thing, followed by the curl get_info and fed the result into | grep '"height"' | grep -o '[0-9]*' and | grep '"target_height"' | grep -o '[0-9]*'. You are right, that while syncing height < target_height. When the sync catches up height = target_height all is good. If I continue to poll target_height it doesn't increase with height increasing. And I'm not mining. Commented Sep 24, 2019 at 3:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.