You’re forked!

Getting resynced using the invalidateblock command and finding some new peers.

Sometimes it happens that your node or wallet gets enticed by the cookies and wanders off to the dark side. Otherwise known as forks off the main chain. Depending on how many nodes/wallets are running on the same chain you might not notice for some time. Or you might notice quickly when your node/wallet stops syncing and no amount of addnodes, connects or restarts allows the sync to progress.

If you have a node which just refuses to progress then it’s worth checking if it has forked off the main chain. Find the node’s block count and compare it with known good nodes. If you don’t control any other nodes then check with the block explorer or ask in Discord/Telegram. Just because there is a difference in the block heights doesn’t necessarily mean you’ve forked off the main chain.

You need to compare the blockhash for the highest block you have with what the explorer, or your other nodes have for the same block height. You can get the block hash from the debug console window of the QT wallet, or direct from the command line using crown-cli.

Suppose your node is stuck at block height 2344133 and consensus has it the latest block height is actually 2346502. You can find out what blockhash your highest block has by

crown-cli getblockhash 2344133

and compare this with the value shown by the explorer. If the 2 values aren’t identical then you have forked.

You can try to resync from scratch, perhaps using the bootstrap. Alternatively, you can do a little detective work to find out at what height you forked and tell your node to discard its chain above that height and resync from that point. Using a binary chop or binary search you can quickly identify the fork height. Then you invalidate the block at that height and get some new peers to resume syncing from.

Binary chop

Imagine the block chain laid out along a long horizontal line with the genesis block at the left end, and the current highest block at the right end.

Find the blockhash for the block at the middle of the line.

Compare it with what the explorer has for that block. If they’re identical discard the left half of the chain and repeat the process in the right half. Or if they differ then discard the right half of the chain and repeat the process in the left half.

Here’s a worked example:

If you had previously synced using a bootstrap then you know you were good up to the height of the bootstrap (possibly 2340250). If you’re stuck at2344133 then you actually only need to consider the portion of the chain between those heights, drastically reducing the number of iterations you need to find the fork.

  1. Find the middle block (2340250+2344133)/2 = 2342191.
  2. Compare your value of the blockhash for block 2342191 with what the explorer has.
  3. If they match move right (to block (2342191+2344133)/2 = 2343162), if they differ move left (to block (2340250+2342191)/2 = 2341220).
  4. Repeat steps 1–3 until you find the smallest block number with a mismatched hash. That’s the point at which you forked.
  5. Suppose the fork point is 2343087 and your hash for this block is 4b3bc3ae3b5e4870c11f23b116409dc29de4ac31a0a509c55a9d1b6841feb03

Invalidateblock

Use the debug console of the QT wallet, or the crown-cli command to invalidate the block. For example

crown-cli invalidateblock  4b3bc3ae3b5e4870c11f23b116409dc29de4ac31a0a509c55a9d1b6841feb031

(Note: that’s all one line; there is no newline/enter between the invalidate block and the hash)

This will take a few seconds to execute.

Get some new peers

If you’re on a fork with a bunch of other nodes, none of them will be able to share the correct version of the block with you. You need some new peers.

  1. Shutdown your wallet.
  2. Remove the peers.dat file from the datadir.
  3. Restart your wallet.

Your wallet will talk to the seednodes, get some new peers (who should be on the correct chain) and start syncing from the height you forked at.