How to fork Bitcoin and create your own Blockchain?6 min readReading Time: 5 minutes
Many companies want to create their own blockchain for their business case. Do you think that it’s very difficult to create your own blockchain network? You need to have deep understanding in peer to peer communication, cryptography and consensus algorithms to create your own network. Even if you know all that stuff it will take huge amount of time to create it, test it, and release it.
There is an easier option – to fork a network. You could use already working product and add on top of it your business logic. The challenges here are the lack of documentation regarding where and what you need to change in order to separate from existing network.
In this tutorial I will show you how to separate a node from the Bitcoin network and create your own blockchain network on top of Bitcoin.
So, what code changes we need to do?
We must change communication configuration (ports, magic values), address prefixes, genesis block, node discovery configuration. The configuration is separated these files:
Changes in src/chainparams.cpp
Inside of it you have configuration for the 3 networks Mainnet, Testnet, Regtest. Each configuration is an entirely new class. We will change Mainnet configuration only. It’s the same principle for all other networks.
What we need to change here?
Information: This is the default port that nodes try to connect to each other.
New value: Random port number.
Message signature also called Message start, Magic value
Information: Magic value indicating message origin network and used to seek to next message when stream state is unknown.
New Value: Find characters from ASCII table that are unlikely to occur in normal data and are different from those used in bitcoin and put their code.
Information: You have different address prefix for different type of address.
New value: Choose new value from here https://en.bitcoin.it/wiki/List_of_address_prefixes
Information: You have block height/hash configuration that tells when a feature is enabled. This is done because there are a lot of features that were implemented after bitcoin network was released.
New value: You should put 0 (meaning genesis block) when block height is required and genesis hash when block hash is required so you could have all of bitcoin features enabled.
Minimum chain work
Information: This is the minimum chain work required to consider the blockchain is updated
New value: Set this to the minimum value otherwise your coin will not mine. When you have mined some blocks, this can be updated to a larger value.
Information: DNS seeds are used for peer discovery and we are creating a new network so we should remove them for now. In another post I will explain how to setup a DNS seeder for your network.
New value: Remove them for now. There will be a new post about creating your own DNS seeder.(Here)
Information: This is used for adding checkpoints, so a node knows that is connected to the correct network.
New Value: Remove checkpoint data and chain transaction data. When out network is up and ready, we could update that data.
We must generate an entirely new genesis block.
Steps to mine new genesis block.
- Clone https://github.com/liveblockchain/genesisgen
- Compile genesis gen: gcc genesis.c -o genesis -lcrypto
- Generate a private kay and get its public key. Example: “048E794284AD7E4D776919BDA05CDD38447D89B436BDAF5F65EBE9D7AD3A0B084908B88162BB60B1AA5ED6542063A30FC9584A335F656A54CD9F66D6C742B67F55”
- Choose a genesis message. Example: “NY Times 2019/04/06 A Mysterious Infection, Spanning the Globe in a Climate of Secrecy”
- Run genesis gen: ./genesis 048E794284AD7E4D776919BDA05CDD38447D89B436BDAF5F65EBE9D7AD3A0B084908B88162BB60B1AA5ED6542063A30FC9584A335F656A54CD9F66D6C742B67F55 “NY Times 2019/04/06 A Mysterious Infection, Spanning the Globe in a Climate of Secrecy” 486604799
After genesis block is generated, we need to update our file with the new data.
Genesis message / Coinbase
Information: This is the input of the generation transaction. While regular transactions use the ‘inputs’ section to refer to their parent transaction outputs, a generation transaction has no parent, and creates new coins from nothing.
New value: It could contain any arbitrary data. It should be the same as the input send to genesis gen “NY Times 2019/04/06 A Mysterious Infection, Spanning the Globe in a Climate of Secrecy”
Genesis transaction Public key
Information: This is the receiver of the first transaction.
New Value: Public key that was used in genesis gen. “048E794284AD7E4D776919BDA05CDD38447D89B436BDAF5F65EBE9D7AD3A0B084908B88162BB60B1AA5ED6542063A30FC9584A335F656A54CD9F66D6C742B67F55”
Information: When genesis block was created.
New Value: The output value from genesis gen. In this case Unix time: 1567080967
Information: The nonce is a field whose value is adjusted by miners so that the hash of the block will be less than or equal to the current target of the network
New Value: The output value from genesis gen. In this case Nonce: 455915214
Information: C++ asserts
New Value: The new Merkle hash and Genesis Hash. In this case:
Merkle Hash: 41e4734699e4a996a460fcde407acd9c58452c7dbaded973265195b31723ce47
Changes in src/chainparamsbase.cpp
The only thing that we need to change here are RPC ports.
Information: Used for administering your node, from bitcoin client.
New Value: Random port number not equal to Network port.
Changes in src/chainparamsseeds.h
Here we have hardcoded IPs, used for a backup when DNS seeds fail to send network node IPs.
Change hardcoded IPs
Because we want to detach from bitcoin, we must remove them and add out own node IPs. If you don’t add any hadrcoded IPs and dns seeds you need to add nodes manually using addnode command.
These changes will detach you from bitcoin network, but they will not change block time, mining reward etc. If you want to read more about bitcoin, I suggest you check: