How To: Create Your Own Private Ethereum Blockchain
Dev highlights of this week
- Releasing a Dust Server update today to include client local time zone based login rewards
- Android and iOS client updates are currently in QA and will release next week including promoted blasts now marked as “promoted”, legacy Dusters will now get rewards, and other performance enhancements and bug fixes.
- Designing analytics of rewards in preparation for switching to mainnet
- Developing method of paying Ethereum gas costs with an ERC-20 token
- Researching different mechanisms for storing content on the blockchain
How to: blockchain
This post marks the first in a new “How To” series we’re starting in an effort to provide some easy to read instructions covering topics our developers found particularly ill-documented online. As we move beyond the token sale, our focus now is helping other developer teams interested in integrating GMT into their apps. To that end, we’re ramping up our technical content production to make it easier to onboard new developers looking to try their hand with blockchain technology.
If you have a particular topic you’d like to see detailed in a How To post, we’d love to hear from you at email@example.com.
This is a guide for starting your own custom Ethereum blockchain on Mac, not to be confused with starting a node on the main Ethereum blockchain. Here we are starting an entirely new and separate blockchain that cannot interact with Ethereum mainnet.
Starting your own Ethereum blockchain is useful, educational, and safer than the public testnet. Learning to set up a private testnet provides tangibility to otherwise abstract concepts such as mining, network peers, and even the geth datadir.
You need to have Geth installed. The easiest way to do this is through homebrew.
- Open Terminal and install homebrew
ruby -e “$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
2. Now install geth
brew tap ethereum/ethereum
brew install ethereum
Create Genesis File
The Genesis block is the first block in the chain, the Genesis file is a JSON file that defines the characteristics of that initial block and subsequently the rest of the blockchain.
- Create a directory to hold your network files
2. Create your genesis file
3. Open your genesis file and paste the following
- chainId — this is your chain’s identifier, and is used in replay protection.
- homesteadBlock, eip155Block, eip158Block, byzantiumBlock — these relate to chain forking and versioning, so in our case lets leave them 0 since we’re starting a new blockchain.
This dictates how difficult it is to mine a block. Setting this value low (~10–10000) is helpful in a private blockchain as it lets you mine blocks quickly, which equals fast transactions, and plenty of ETH to test with. For comparison, the Ethereum mainnet Genesis file defines a difficulty of 17179869184.
This is the the total amount of gas that can be used in each block. With such a low mining difficulty, blocks will be moving pretty quick, but you should still set this value pretty high to avoid hitting the limit and slowing down your network.
Here you can allocate ETH to specific addresses. This won’t create the account for you, so make sure its an account you already have control of. You will need to add the account to your private chain in order to use it, and to do that you need access to the keystore/utc file. For example, Geth and MyEtherWallet give you access to this file when you create an account, but Metamask and Coinbase do not. The addresses provided are not real addresses, they are just examples. Here we allocate 100,000 and 120,000 ETH respectively.
Start your Node!
Now the real fun begins, we’re all set to fire up our new Ethereum blockchain.
1. Instantiate your data directory
geth --datadir ./myDataDir init ./myGenesis.json
2. Start your Ethereum peer node.
Networkid helps ensure the privacy of your network. You can use any number here (where we used “1114”), but other peers joining your network must use the same one.
geth --datadir ./myDataDir --networkid 1114 console 2>> myEth.log
Output should look like this:
at block: 5 (Thu, 07 Dec 2017 17:08:48 PST)
modules: admin:1.0 clique:1.0 debug:1.0 eth:1.0 miner:1.0 net:1.0 personal:1.0 rpc:1.0 txpool:1.0 web3:1.0
> should be typed here.
3. Display your Ethereum logs
- Open another terminal window
- cd to
tail -f myEth.log
4. Import/Create an Account
- If you allocated ETH in the Genesis file, import the corresponding account by dragging the UTC file into the
myDataDir/keystoredirectory and skip to step 5.
- Do not forget this passphrase! You will be typing this a lot, so for this test network you can keep it simple.
5. Set Default Account
- Check your default account, type
- If this address is the same as the one from step 4, skip the rest of step 5.
- To set your default account, type
6. Start mining
- Check your balance with
- Look at your other terminal window, you should see some mining action in the logs. Check your balance again and it should be higher.
- To end mining, type
Optional: Add Other Peers
Add more nodes to your private Ethereum network.
1. Start another peer
- On your same machine instantiate a new datadir:
geth --datadir ./peer2DataDir init ./myGenesis.json
- Launch the 2nd peer on a different port:
geth --datadir ./peer2DataDir --networkid 1114 --port 30304 console 2>> myEth2.log
2. Display your Ethereum logs
- Open another terminal window
- cd to
tail -f myEth2.log
3. Join the 1st peer
- Output will look like this:
> admin.addPeer( “enode://firstname.lastname@example.org:30303” )
- Make sure you replace
“enode://…@”above with the output from
admin.nodeInfo.enodewhich is specific to you. As shown above, the “
[::]” is replaced with “
127.0.0.1:30303” which is the IP:Port of the 1st peer.
4. Verify your nodes are now communicating
- Output should show that peer 2 is connected to
Helpful: geth console commands
debug.verbosity(6) // highest logging level, 3 is default
Share your thoughts with us in any of the community channels linked below!