Check if you are farming the latest block

Hey, i wrote a really simple script just because the latest video from SlothTechTV had me worried.
He said he was farming old blocks, and the logs were showing everything OK. Good read times, no errors. Node was fully synced as well.

This will take the value of the last block hash from chia show -s and then use the full_node RPC to get the challenge hex (which is what the harvester log shows “farming xxxxx… found 0 proofs”
If the challenge you are harvesting is the same as the latest block, then you are fine, if not, show an error.

I have this working with a pushover notification to get notified on my phone if this happens.

anyway, here’s the script. As always, use at your own risk.

#!/bin/bash
farming_block_challenge=$(cat ~/.chia/mainnet/log/debug.log | grep eligible | tail -n 1 | awk {'print $11'} | cut -d. -f1)
block_hash=$(chia show -s | cut -d\| -f2 | grep -A1 Hash\:$ | tail -n 1 | sed 's# ##g')
block_info=$(curl --silent --insecure --cert ~/.chia/mainnet/config/ssl/full_node/private_full_node.crt --key ~/.chia/mainnet/config/ssl/full_node/private_full_node.key -d '{"header_hash": "'"${block_hash}"'" }' -H "Content-Type: application/json" -X POST https://localhost:8555/get_block)
block_challenge=$(echo $block_info | jq -r .block.reward_chain_block.challenge_chain_ip_vdf.challenge)
block_height=$(echo $block_info | jq .block.reward_chain_block.height)
farming_latest=$(echo $block_challenge | grep $farming_block_challenge)
if [[ -z $farming_latest ]];then
	echo "Not farming latest block... Something's wrong"
else
	echo "Farming block Height $block_height - Hash: $block_hash"
fi

You’ll need jq to run this. If you are running linux, install it with your package manager.
Example: apt install jq -y

3 Likes

Hey there,

I see that you put a lot effort into the script, appreciate that. Is it possible for you to explain the logic behind it and what can be done if not farming the latest block?

Thank you.

1 Like

Also I noticed that when log level set to DEBUG, the script produces false positives due to debug logs from farmer and harvester. You can consider replacing;

grep farming

at line 2 with

grep eligible

2 Likes

Hey, thanks for the input.
Glad you find a way to fix the grep issue with DEBUG level logs. I use my logs in INFO to avoid the extra noise in the file.
The script logic is explained in the first post, but let me know if you want me to dig deeper into a specific point. Happy to do it.
Regarding a measure to fix the issue if you find you’re farming an old block, i’m not sure really. I would try restarting the harvester, and maybe restarting the full node or computer as well, because an issue like this means that your harvester is in a weird state. I did encounter this issue a few times using the script, but usually adding a 5 second delay and retrying fixes the ‘alert’, because i start harvesting the correct block

@0x0 Thanks for the script!! I threw it in my cron and have it checking every hour just to be safe. I also added a line to send an email out if it’s broke.

To test I had it email either way but then just left the failure:

if [[ -z $farming_latest ]];then
echo “Not farming latest block… Something’s wrong”
echo “Not farming latest block… Something’s wrong” | mail -s “BROKE - CHIA Block Height Check” xxxxx@gmail.com
else
echo “Farming block Height $block_height - Hash: $block_hash”
echo “Farming block Height $block_height - Hash: $block_hash” | mail -s “CHIA Block Height Check” xxxxx@gmail.com

1 Like

Hey @SB1Racing , glad you find it useful!. Let me know how it goes. I have it running in loop all the time, with just a 5 second sleep if it fails and then retry, and if it fails again then notify.
I found that i get notified several times a day, maybe 4 or 5 (for some reason im skipping blocks at random) Still not sure why this happens

I think this is an issue worth making more global (also after watching some of Sloth’s video). Has anyone noticed anything similar or have a check we can do in Windows simply?

have you tried with Task Scheduler ?

I switched mine to run more often and I’m seeing the same random behavior you are. I setup some monitoring for the network, farmer, etc to see if I could see why but haven’t been able to pinpoint anything yet. Very strange and occurs very randomly for a check or two and then goes back to being fine. I guess that I’ll keep digging to see if I can find wtf is going on with this :man_shrugging:

Hey! Thanks for sharing this, I watched SlothTech video and was wondering exactly how to check this.

I have installed jq and created the shell script, but I have no idea how to run it, could please share the command required to make it work?

Many thanks and apologies for my “noobiness” :sweat_smile:

@SB1Racing Looks like the errors shown by this script are actually “false positives”.
The harvester starts to work on newer blocks before the “full node” shows the new block height with chia show -s… So the in reality, i’m still mining the newest block, but the command is behind for a few seconds (which is still weird).

[2021-06-02 15:16:27] Current block Height 374376 - Hash: e3e329b4dd47ff03335dcf6c75580bd357e943f8f76977f154a6ef9862b57313 - Challenge: 0xb167aff54681e6962eae525a23261ac11d062058563fa10d6dbff6f66ab4b7ad - Farming: b167aff546
[2021-06-02 15:16:35] Current block Height 374377 - Hash: 7185cb00e3fcd10232b814d1f1d19f768c6b88d0e58b8593b8f2f03863f6b7ea - Challenge: 0xb167aff54681e6962eae525a23261ac11d062058563fa10d6dbff6f66ab4b7ad - Farming: b167aff546
[2021-06-02 15:16:44] Not farming latest block... Checking again in 2 seconds
[2021-06-02 15:16:44] Current block Height 374378 - Hash: b2868a78e230a508bd51591c519bc4afa0dbb126fdaa9052650cc6a6bb4502b8 - Challenge: 0xb167aff54681e6962eae525a23261ac11d062058563fa10d6dbff6f66ab4b7ad - Farming: 0a48e5659d
[2021-06-02 15:16:50] Not farming latest block... Checking again in 2 seconds
[2021-06-02 15:16:50] Current block Height 374378 - Hash: b2868a78e230a508bd51591c519bc4afa0dbb126fdaa9052650cc6a6bb4502b8 - Challenge: 0xb167aff54681e6962eae525a23261ac11d062058563fa10d6dbff6f66ab4b7ad - Farming: 0a48e5659d
[2021-06-02 15:16:56] Current block Height 374379 - Hash: 65506299561d34b1b422fc4aa8e7e1c5f12ef5d6ddab0e20d71fe322ae08dc5a - Challenge: 0x0a48e5659d6a46f0b467f0a104b052e09538e7e1eee3b3b772ec1d54bab0393e - Farming: 0a48e5659d
[2021-06-02 15:16:59] Current block Height 374380 - Hash: a858d7bb2aa7a6ebf34bef6f9d78ee1c819cf89146040341dbbbc102efca3322 - Challenge: 0x0a48e5659d6a46f0b467f0a104b052e09538e7e1eee3b3b772ec1d54bab0393e - Farming: 0a48e5659d
[2021-06-02 15:17:05] Current block Height 374381 - Hash: 1e0d36c9b73bc3d343bf9e092d3e849d5f5e6d47c379d59fedfe73f53cb81e78 - Challenge: 0x0a48e5659d6a46f0b467f0a104b052e09538e7e1eee3b3b772ec1d54bab0393e - Farming: 0a48e5659d

I’m running this version of the script, which is more verbose, but allowed me to discover this.

#!/bin/bash
check_farming_block(){
farming_block_challenge=$(cat ~/.chia/mainnet/log/debug.log | grep eligible | tail -n 1 | awk {'print $11'} | cut -d. -f1)
block_hash=$(chia show -s | cut -d\| -f2 | grep -A1 Hash\:$ | tail -n 1 | sed 's# ##g')
block_info=$(curl --silent --insecure --cert ~/.chia/mainnet/config/ssl/full_node/private_full_node.crt --key ~/.chia/mainnet/config/ssl/full_node/private_full_node.key -d '{"header_hash": "'"${block_hash}"'" }' -H "Content-Type: application/json" -X POST https://localhost:8555/get_block)
block_challenge=$(echo $block_info | jq -r .block.reward_chain_block.challenge_chain_ip_vdf.challenge)
block_height=$(echo $block_info | jq .block.reward_chain_block.height)
farming_latest=$(echo $block_challenge | grep $farming_block_challenge)
}
check_farming_block
if [[ -z $farming_latest ]];then
        if [[ "$farmed_block" != "$block_height" ]];then
        echo "$now_date Not farming latest block... Checking again in 2 seconds"
        echo "$now_date Current block Height $block_height - Hash: $block_hash - Challenge: $block_challenge - Farming: $farming_block_challenge"
        check_farming_block
        sleep 2
        if [[ -z $farming_latest ]];then
       #Still not in correct block, send notification
       #Here you should add your prefered way of notifying you
       echo "$now_date Not farming latest block... Checking again in 2 seconds"
        echo "$now_date Current block Height $block_height - Hash: $block_hash - Challenge: $block_challenge - Farming: $farming_block_challenge"
        else
        echo "$now_date Current block Height $block_height - Hash: $block_hash - Challenge: $block_challenge - Farming: $farming_block_challenge"
        farmed_block=$block_height
        fi
        fi
else

        if [[ "$farmed_block" != "$block_height" ]];then
        echo "$now_date Current block Height $block_height - Hash: $block_hash - Challenge: $block_challenge - Farming: $farming_block_challenge"
        farmed_block=$block_height
        fi
fi

@jbatarda To run the script, copy and paste the contents in a file and save it block-check-notify.sh

After that, give it permissions to execute using chmod +x block-check-notify.sh and then execute it by running ./block-check-notify.sh

If you want to run this continously you can use the following line:

while true;do ./block-check-notify.sh;sleep 2;done

Let me know if that helps.

If someone finds that my logic is wrong, please step up :smiley:

2 Likes