All field notes

Blockchain · Retrospective

Changing a live chain's proof-of-work algorithm

A chain of glass blocks that changes from cubes to hexagonal prisms at a block topped by a small clock.
Concept illustration · AI-generated

In 2019 Trivechain changed how its blocks are mined. Before the change, a miner proved work by hashing a block header with X11. After it, the network accepted only headers hashed with X16R. I worked on that hard fork as one of the chain's developers, and the code that keys the switch to a timestamp is still public in the trivechain-core repository.

The switch itself is small. In simplified form, the function that hashes a block header reads:

if header.time < 1555872222:  hash the header with X11
otherwise:                    hash the header with X16R

The number is a Unix timestamp: 18:43:42 UTC on 21 April 2019, or 2:43 in the morning of 22 April in Malaysia. In September 2019 I added X16Rv2 to the client, with the mainnet switch set for 16:00 UTC on 1 October 2019. That is the same instant Ravencoin, where X16R began, moved to X16Rv2.

A few lines of code, then. Everything else about the change happened outside the repository.

What the switch asks of each party

Nodes. A node still running the old client hashes every new header with X11. After the switch those hashes do not meet the difficulty target, so the old node rejects each new block. If no miner stayed behind, it simply stops advancing. If some did, it follows their chain instead, a separate and far weaker one. Either way, its view of balances is no longer the network's. That applies to every node: wallets, masternodes and the block explorer alike.

Pools. I had built Trivechain's mining pool, so this is the side I can describe most concretely. A pool checks the shares its miners submit with the hash function the chain will apply to the block being built. With a switch keyed to time, that function depends on the timestamp inside each block template, not on the pool's own clock. Near the boundary, a pool that picks the wrong function either rewards work the chain will refuse or refuses work the chain would accept.

Miners. Hardware built for X11 cannot compute X16R. Anyone who had pointed X11 machines at the chain lost it as somewhere to point them, and everyone else needed new mining software before the deadline. That loss is the intent: X16R chains sixteen hash functions in an order set by the previous block's hash, to make specialised hardware harder to build. It is still a cost, and it falls on people who were securing the chain the day before.

Exchanges. An exchange credits deposits from its own node. If that node is not upgraded in time, deposits stop confirming, or are confirmed on a chain the rest of the network has left. This is why exchanges usually pause deposits and withdrawals around a fork, and why the notice they need is a date and a time in their own time zone, not a constant in a source file.

Time or height

Keying a switch to a timestamp has one clear advantage: it is a moment that can be announced, and everyone can put it in a calendar.

The cost is that a block's timestamp is written by whoever mined it, within limits the consensus rules allow. The algorithm a block must use depends on the time its miner put in the header, not on when the block actually reached the network. Close to the boundary, a block mined just after the announced moment can still be valid under the old algorithm if its timestamp says it came first.

A block height removes that ambiguity, because the switch happens at a fixed position in the chain whatever any clock says. In exchange, nobody can say in advance exactly when that height will arrive, since it moves with hashrate. Trivechain's later fork, in 2021, was set by height.

The old algorithm never leaves

The part of a change like this that is easiest to miss is that nothing gets removed. A new node syncing from the first block still has to verify every block mined before the switch, so the client keeps X11 beside X16R permanently. After October 2019 it carried three paths. Every change to proof of work adds a rule for new blocks and keeps the old rule for old ones.

The 2.0 code also targeted one-minute blocks, where the earlier code had targeted two and a half minutes. In June 2019 I committed a fix for "the problem where older blocks can't sync". It made the difficulty calculation use the old two-and-a-half-minute spacing again for blocks below a fixed height. A parameter changed for the future had to be fenced off from the past, or a node checking history from the beginning would reject blocks that were valid when they were mined.

That is the lesson I took from working on these forks. A proof-of-work change is announced as a new algorithm. In the code it is another branch that every later version must carry. In practice it is a deadline that node operators, pool operators, miners and exchanges each have to meet, and most of the work is making sure they can.