Coping with mega forks

Something really strange happened on the Testnet just a few days before our update which also inflicted our gamers. This is how we dealt with it.

A fork is nothing unusual, but the four forks that occurred on March 24th and the following day were extraordinary. It was basically a unicorn among forks. While normal forks are just one or two blocks long, these were on average unbelievable 40 blocks long. A phenomenon which is practically impossible on the mainnet blockchain.

How we used to handle forks

Our client was not able to cope with such a mega fork at the time because we had designed our game for the mainnet. On the mainnet, forks with more than 6 blocks are extremely unlikely. Even providers of financial services consider any transaction older than 6 blocks as being safe.

Before our update, the client used to cope with forks of up to 6 blocks by executing a rollback. This means all the game operations that had taken place in the blocks were dismissed, from the point when the chain split. Then it collected all transactions from the blocks of the main chain which had been created after the split.

After such a rollback the game operations/transactions in the main chain would then be restored to how they were before the fork, often just rearranged. This means that the reconstructed game state will in most cases be exactly the same as before. As a player, you will hardly notice that a fork occurred at all.

This will obviously not work regarding forks which are longer than 6 blocks. If a client executes a rollback it will still include and show the game state changes stemming from the fork. Returning to the original main chain game state will not be possible.

How we handle forks now

Even if it is practically impossible for such a mega fork to occur on the mainnet, we wanted a system which will work no matter what happens. We therefore improved our client and it can now recognize large forks.

The way we handle forks with up to 6 blocks has not been changed, because it is still the most sensible approach. We could wait until several blocks are validated before a transaction is processed but this would slow down any game operations immensely. Instead of 2.5 minutes, a single operation would take about 15 minutes.

For that reason we went for a different solution. Bigger forks are solved by a resynchronization. The client will start all over again and will compute a new game state. Our services will simultaneously create the precomputed game state. If you are not playing while the chain is switched or login shortly after, you will not know what happened. Your client will download the precomputed game state as usual, merge it and you can simply start playing again.

We can now also handle forks of any size, our game will still work even when absolute chaos occurs on the blockchain.

What is the worst case?

In the worst case you obtain different items in a mission, but only for those blocks affected by the fork. Let’s say, you are on a mission which takes 20 blocks. If a fork of 2 blocks occurs, you will get 2 replaced items while the other 18 items will not be affected. You will only notice that something has changed when you are watching the loot section at that very moment.

Most importantly though, you will not lose any money because of a fork! Let’s say, you want to buy an item in a block, which is going to be the first block of a fork. The transaction is finished in the second block and you obtain the item you wanted.

If someone wanted to buy the same item, your and his transaction were stored into the mainpool. The miner, as usual, will pick transactions out of the mainpool to include it in a block. If he takes yours, the other transaction would be dismissed by the network.

Now the main chain is switched.

As a result of this switch it is possible that on the new main chain the other transaction is processed instead of yours. So, the other player will get the item and your transaction would not be accepted by the network. If this happens, you will simply get your money back. In other words, even in this worst case your funds will never get lost.