Autodelegation, Auto unjail for Stride validator (and other networks on Cosmos)

CrypTech
4 min readAug 11, 2022

--

In order for our validator to always have an up-to-date place in the ranking of the active set, we need to configure auto-delegation. There are many different ways to do this, I will tell you about one of them that we use. This method is based on the Expect application. So we need to install it on our server first.

sudo apt install expect -y

To make your own script, you need to start a text file editor and thereby create a new file in the main directory:

cd /root
nano autopass.sh

In the window that opens, you need to enter the following text, where 12345678 is your local wallet password, if you have a different one, you need to replace it.

#!/usr/bin/expect -f
set timeout -1
spawn /root/autodelegation.sh
expect {
{Enter keyring passphrase:}
{send "12345678\r";exp_continue}
}

Next, you need to exit the editor and save this file, in the Nano editor this is done with the following key combinations: ctrl+s ctrl+x .
You have saved the auto-enter password script for your Stride node. Now you need the actual script of the redelegation itself.
To begin with, you need information such as the address of the operator of your validator. To find it out, you need to enter the following command, where you substitute the Name of your node, and after pressing Enter, enter the local password of the wallet:

strided keys show <you wallet moniker> — bech=val

In the line “address: stradevaloper…. “ your operator’s address is exactly written. Write it down somewhere, you will need it to set up redelegation.

We call nano again with the following file:

nano autodelegation.sh

And we insert and also edit the following code, where “stride…” is the address of your wallet, and “stridevaloper…” is the address of your operator, which must be replaced with yours. You also need to replace <you wallet moniker> with the name of your node:

#!/bin/bash
source /root/.profile
source /root/.bash_profile
source /root/.bashrc
strided tx distribution withdraw-all-rewards --chain-id=STRIDE-TESTNET-2 --from=stride… --gas=auto -y
sleep 30
strided tx distribution withdraw-rewards stridevaloper… --commission --chain-id=STRIDE-TESTNET-2 --from=<you wallet moniker> --gas=auto -y
sleep 30
BALANCE=$(strided q bank balances stride… | grep amount | awk '{print $3}' | tr -d '"')
DIFFERENCE=$(echo $(($BALANCE-10000))utorii)
echo $DIFFERENCE
sleep 1
strided tx staking delegate stridevaloper… $DIFFERENCE --chain-id=STRIDE-TESTNET-2 --gas="auto" --from=<you wallet moniker> -y

Press ctrl + s, ctrl + x (save and exit). Next, we need to allow root rights to our scripts, so we enter the following commands:

chmod a+x autopass.sh
chmod a+x autodelegation.sh

Next, you need to register these scripts in the crontab, so that our scripts run automatically. The crontab service itself can be called with a command:

crontab -e

and then we need to add the line at the bottom of the text configuration file:

*/5 * * * * /root/autopass.sh >> /root/cron.log

press ctrl + s , ctrl + x to save and exit the configuration file.
Now, if you did everything right, you will have token auto-delegation every 5 minutes working.

Also, among the scripts for automatic maintenance mode, we need synchronization monitoring and automatic jailbreak if you suddenly get there.
First, I will describe how to make a script for tracking the operation of your node. With the scheme familiar to us, we tear off the Nano:nano checkheight.sh

and insert the following code:

#!/bin/bash
source .bash_profile
SAVESTATE=$(curl http://127.0.0.1:26657/status | grep “latest_block_height” | awk ‘{print $2}’ | tr -d ‘“,’)
sleep 30
SAVESTATE2=$(curl http://127.0.0.1:26657/status | grep “latest_block_height” | awk ‘{print $2}’ | tr -d ‘“,’)
RESULT=$(( $SAVESTATE2 — $SAVESTATE ))
if (( $RESULT > 0 ));
then
echo -e “Blocks up\n”
else
sudo systemctl restart strided
echo -e “Blocks non up\n”
fi

Next, save using ctrl+s, ctrl+c. Also, this script needs to be given root rights by the command:

chmod a+x checkheight.sh

This script will, within 30 seconds, compare the height of the blocks, and if something is wrong with the node, restart the Stride service. But again, for automatic operation, you need to insert it into the crontab.

crontab -e

you also need to insert a period from below

*/5 * * * * /root/checkheight.sh >> /root/cron.log

And now it’s the turn of the automatic jailbreak script. First, we need to copy the script for automatically entering the local node password:

cp /root/autopass.sh /root/autounjail.sh

Then edit it:

nano autounjail.sh

where to change autodelegation.sh to unjail.sh. Save ctrl+s , ctrl+x .

Next, we need to prepare the address of the operator. As always, we start with Nano

nano unjail.sh

then insert the code where we replace the address of the operator and the name of the node:

#!/bin/bash
source /root/.bashrc
source /root/.profile
source /root/.bash_profile
STATE=$(strided q staking validator stridevaloper…. | grep jailed | awk '{print $2}')
if [[ $STATE == false ]];
then
echo -e "Not in jail\n"
else
strided tx slashing unjail --from <you wallet moniker> --gas auto -y
echo -e "In jail\n"
fi

Press ctrl+s, ctrl+x. Save the file, then give the rights to the script:

chmod a+x unjail.sh

We also open and register it in crontab:

crontab -e
*/5 * * * * /root/autounjail.sh >> /root/cron.log

That’s all I think. We have the basic things needed for the stable operation of the validator, and they work. These scripts can also be used for other networks in Cosmos ecosystem.

The CrypTech team is happy to share its experience and knowledge, we recommend that you subscribe to our social platforms to get more useful information.

Site | YouTube | Twich | Twitter

--

--