unfork.sh
Tags: Community, Fork, Masternode, Wallet, Systemnode

unfork.sh

A tool for checking if you’re forked and optionally unforking you

In a recent article we described how to check if your masternode/systemnode or wallet is on a forked blockchain. The article also described a method for getting back on the correct chain without having to resync the entire chain. It was prompted by a recent period of instability caused by syncing difficulties with v0.13.2. The difficulties were resolved with the release of v0.13.4. There have been other forkfests in the past, and it’s in the nature of blockchains that there could be more in the future.

In another recent article we promised a useful tool building on the API functionality of the newly released Crown PoS Explorer.

Well here it is, unfork.sh!

It’s a bash shell script, designed to be used on your VPS or with a linux wallet. It does two things:

Checks whether your node/wallet is on the same chain as the explorer.

Optionally executes the appropriate commands to get you back on the same chain as the explorer.

The script assumes that the explorer is on the correct chain. If you suspect you are forked, and the script says you are, then you should check if the explorer is believed to be on the correct chain before letting it (attempt to) fix the problem. Ask in Discord or Telegram. If the consensus is that the explorer is on the correct chain then you can re-run the script with the fix option and let it work its magic.

Download and install

Sign on to your VPS with the userid you use to run your masternode or systemnode.

Enter

sudo curl -o /usr/local/bin/unfork.sh https://nextcloud.crownplatform.com/index.php/s/4GX7KJ3PYDj2nbp/download sudo chmod +x /usr/local/bin/unfork.sh

If you login as root you don’t need to use the sudo prefix.

You can also use the script on your linux wallet machine. If you normally run the QT wallet you’ll need to shut it down and run the daemon wallet instead.

Customise

If you installed your node using the crown_server_install.sh script then it is likely you don’t need to do anything here. However, if your datadir is not called .crown and located in the logged in user’s home directory then you need to customise the script.

Use vi or nano or your favourite editor to customise the values for CONFIG and DATADIR.

Additionally, if you choose not to install the script in /usr/local/bin, customise the PREFIX variable with the appropriate value.

Run manually

At any time simply enter:

unfork.sh

to get a current sit rep.

Unforked example

mark@x230:~$ unfork.sh

unfork.sh checking crown blockchain for forks at Wed 29 May 14:03:04 +07 2019

Our latest block is 2392389

Latest block at the explorer is 2392389 We are level with the explorer

and on the same chain. Nothing to do here!

Forked example

crown@Crown-Testnet:~$ unfork.sh

unfork.sh checking crown blockchain for forks at Wed May 29 14:59:55 UTC 2019

Our latest block is 2392775

Latest block at the explorer is 2392846 We are behind the explorer Searching for the fork point...

Forked at 2392775

Our hash is 4759b0b107552c7c70d28910c1a4bae3c4fe89d6532e85d0b5241c1968ba19e0

Explorer has 58efb6cb2972ee48fe1a036e1de68989ca82ec4d0953b6d6fab91710ba3981ff Run with unfork.sh fix to actually fix the fork

Here, the script has detected the node is behind the explorer and not on the same chain. It used a binary chop method to identify the fork point and then reported the situation to the user.

Forked example with fix option

If you’re running the script on your wallet machine rather than a node VPS, it is good practice to make a backup of your wallet.dat before proceeding further.

Then re-run the script with the fix option for it to attempt to resolve the problem without having to resync from scratch.

crown@Crown-Testnet:~$ unfork.sh fix

unfork.sh checking crown blockchain for forks at Wed May 29 15:02:57 UTC 2019

Our latest block is 2392775

Latest block at the explorer is 2392850 We are behind the explorer Searching for the fork point...

Forked at 2392775

Our hash is 4759b0b107552c7c70d28910c1a4bae3c4fe89d6532e85d0b5241c1968ba19e0

Explorer has 58efb6cb2972ee48fe1a036e1de68989ca82ec4d0953b6d6fab91710ba3981ff Invalidating the fork point

Shutting down the daemon

Crown server stopping

...waiting...

Re-starting the daemon

Crown server starting

Use the command

crown-cli getblockcount

to monitor the chain and make sure the daemon is resyncing.

You have at least 76 blocks to catch up. crown@Crown-Testnet:~$ crown-cli getblockcount

error: {"code":-28,"message":"Loading block index..."} crown@Crown-Testnet:~$ crown-cli getblockcount

2392852

You can see the first attempt after restarting at a getblockcount failed with an error message because the daemon was busy reloading the block index. A few seconds later the retried command worked and the daemon had caught up with the explorer.

Automated execution

You could create a crontab entry to run the script automatically but doing so isn’t really recommended. The reason it’s not recommended is that the explorer is not an Oracle; it’s just as likely to be on a forked chain as anyone else.

Comments or questions?

Leave your feedback in the #general channel in Discord. In particular, if you’re a Windows user and care to rewrite it as a batch file, or try running it in win-bash or the Windows Subsystem for Linux, we’d be interested to know how you get on.