Creating a new blockchain using Ethereum code

Toptal academy Blockchain lectures #7

2018-03-14

ivan.voras@toptal.com

Today's agenda

  • Types of blockchain supported by Ethereum: PoW and PoA
  • The genesis block and the genesis.json file
  • Creating a new PoW blockchain, using Puppeth and Geth
  • Extended Q&A

PoW and PoA

Proof-of-Work (PoW)
s useful when there is no implicit trust between nodes and where "everyone" needs to mine blocks.

 

Miners "prove their worth" by dedicating a lot of computing power to mining blocks: the PoW process also guards the integrity (immutability) of the blockchain: the more miners participate, the greater the difficulty, and the more work an attacker has to do to alter the blockchain.

Proof-of-Authority (PoA)
is useful when there is trust between miner nodes, e.g. they all belong to the same org.

 

"Authority" means two things: literally, who has the authority / is allowed to mine blocks, but also proving who is the "author" of new blocks.

 

Miners are usually identified by their private keys (similar to how addresses are used), and all nodes can verify the blocks' signature.

Other Ethereum chains (networks)

 

Interesting note: the Ropsten network has a much lower PoW difficulty to enable quick testing, and was spammed / attacked because of that, proving the importance of keeping PoW difficulty as high as possible.

General steps

  1. Create initial accounts / addresses (optional, like premining)
  2. Create the genesis.json file, describing your genesis block
  3. Use "geth init" to initialise a blockchain directory
  4. Instruct other nodes to connect
  5. Configure other software, such as wallets, etc.
  6. ???
  7. Profit!

genesis.json (example)

{
 "alloc": {
    "0xca843569e3427144cead5e4d5999a3d0ccf92b8e": {
      "balance": "1000000000000000000000000000"
    },
    "0x0fbdc686b912d7722dc86510934589e0aaf3b55a": {
      "balance": "1000000000000000000000000000"
    }
  },
 "config": {
   "chainID": 42,
   "homesteadBlock": 0,
   "eip155Block": 0,
   "eip158Block": 0
 },
 "nonce": "0x0000000000000000",
 "difficulty": "0x0400",
 "mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000",
 "coinbase": "0x0000000000000000000000000000000000000000",
 "timestamp": "0x00",
 "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
 "extraData": "0x43a3dfdb4j343b428c638c19837004b5ed33adb3db69cbdb7a38e1e50b1b82fa",
 "gasLimit": "0xffffffff"
}

when certain features become available

should be unique

pre-loaded addresses

not relevant for the genesis block, but useful to differentiate blockchains!

Before we start

  1. Create a new directory for your new blockchain's first node, e.g. c:\EthPriv\node1
  2. Create an account (or several) to hold an initial sum of coins, e.g.  geth --datadir c:\EthPriv\node1 account new
  3. The private keys will be recorded in the new node's folder, in the "keystore" subdirectory (the address is a part of the filename, in case you forget).
E:\EthPriv>geth --datadir node1 account new
INFO [03-13|23:29:02] Maximum peer count                       ETH=25 LES=0 total=25
Your new account is locked with a password. Please give a password. Do not forget this password.
Passphrase:
Repeat passphrase:
Address: {fcb7bcc7492da0e8921692103f9cf284fe47575f}

Puppeth

  • A tool for creating the genesis.json file
  • Has a simple question-and-answer command-line interface
  • Can configure PoW and PoA blockchains / networks

 

Remember to edit the genesis.json file, it may contain some defaults / examples not required in the deployed network!

 

(like 256 pre-funded accounts with no keys, or the huge amount of coins assigned by default to the pre-funded addresses...)

E:\EthPriv>puppeth
+-----------------------------------------------------------+
| Welcome to puppeth, your Ethereum private network manager |
|                                                           |
| This tool lets you create a new Ethereum network down to  |
| the genesis block, bootnodes, miners and ethstats servers |
| without the hassle that it would normally entail.         |
|                                                           |
| Puppeth uses SSH to dial in to remote servers, and builds |
| its network components out of Docker containers using the |
| docker-compose toolset.                                   |
+-----------------------------------------------------------+

Please specify a network name to administer (no spaces or hyphens, please)
> toptalnet

Sweet, you can set this via --network=toptalnet next time!

INFO [03-13|23:39:40] Administering Ethereum network           name=toptalnet
WARN [03-13|23:39:40] No previous configurations found         path=.puppeth\\toptalnet

What would you like to do? (default = stats)
 1. Show network stats
 2. Configure new genesis
 3. Track new remote server
 4. Deploy network components
> 2

Which consensus engine to use? (default = clique)
 1. Ethash - proof-of-work
 2. Clique - proof-of-authority
> 1

Which accounts should be pre-funded? (advisable at least one)
> 0xfcb7bcc7492da0e8921692103f9cf284fe47575f
> 0x

Specify your chain/network ID if you want an explicit one (default = random)
> 42
INFO [03-13|23:40:29] Configured new genesis block

What would you like to do? (default = stats)
 1. Show network stats
 2. Manage existing genesis
 3. Track new remote server
 4. Deploy network components
> 2

 1. Modify existing fork rules
 2. Export genesis configuration
 3. Remove genesis configuration
> 2

Which file to save the genesis into? (default = toptalnet.json)
>
INFO [03-13|23:41:06] Exported existing genesis block

The pre-funded address

The genesis block JSON description filename

Network name, for convenience

Initialise the blockchain

geth --datadir c:\EthPriv\node1 init toptalnet.json

E:\EthPriv>geth --datadir node1 init toptalnet.json
INFO [03-13|23:57:05] Maximum peer count                       ETH=25 LES=0 total=25
INFO [03-13|23:57:05] Allocated cache and file handles         database=E:\\EthPriv\\node1\\geth\\chaindata cache=16 handles=16
INFO [03-13|23:57:05] Writing custom genesis block
INFO [03-13|23:57:05] Persisted trie from memory database      nodes=1 size=224.00B time=1.0029ms gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00B
INFO [03-13|23:57:05] Successfully wrote genesis state         database=chaindata                           hash=b5cadb…184f7b
INFO [03-13|23:57:05] Allocated cache and file handles         database=E:\\EthPriv\\node1\\geth\\lightchaindata cache=16 handles=16
INFO [03-13|23:57:05] Writing custom genesis block
INFO [03-13|23:57:05] Persisted trie from memory database      nodes=1 size=224.00B time=0s       gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00B
INFO [03-13|23:57:05] Successfully wrote genesis state         database=lightchaindata                           hash=b5cadb…184f7b

This creates the blockchain storage, the state trie, and all the other things needed for operation.

Start it up

geth --datadir node1 --rpc --rpcapi admin,eth,miner,personal,web3,net console

E:\EthPriv>geth --datadir node1 --rpc --rpcapi admin,eth,miner,personal,web3,net console
INFO [03-14|00:06:14] Maximum peer count                       ETH=25 LES=0 total=25
INFO [03-14|00:06:14] Starting peer-to-peer node               instance=Geth/v1.8.2-stable-b8b9f7f4/windows-amd64/go1.9.2
INFO [03-14|00:06:14] Allocated cache and file handles         database=E:\\EthPriv\\node1\\geth\\chaindata cache=768 handles=1024
WARN [03-14|00:06:14] Upgrading database to use lookup entries
INFO [03-14|00:06:14] Database deduplication successful        deduped=0
INFO [03-14|00:06:14] Initialised chain configuration          config="{ChainID: 42 Homestead: 1 DAO: <nil> DAOSupport: false EIP150: 2 EIP155: 3 EIP158: 3 Byzantium: 4 Constantinople: <nil> Engine: ethash}"
INFO [03-14|00:06:14] Disk storage enabled for ethash caches   dir=E:\\EthPriv\\node1\\geth\\ethash count=3
INFO [03-14|00:06:14] Disk storage enabled for ethash DAGs     dir=C:\\Users\\ivoras\\AppData\\Ethash count=2
INFO [03-14|00:06:14] Initialising Ethereum protocol           versions="[63 62]" network=1
INFO [03-14|00:06:14] Loaded most recent local header          number=0 hash=b5cadb…184f7b td=524288
INFO [03-14|00:06:14] Loaded most recent local full block      number=0 hash=b5cadb…184f7b td=524288
INFO [03-14|00:06:14] Loaded most recent local fast block      number=0 hash=b5cadb…184f7b td=524288
INFO [03-14|00:06:14] Regenerated local transaction journal    transactions=0 accounts=0
INFO [03-14|00:06:14] Starting P2P networking
INFO [03-14|00:06:16] UDP listener up                          self=enode://6fc14916cefae9082d017a9266a4eed4360719838ba656f59c7bbfdeffc7d933bfeceeae0d7cbdc2f82da2129a6524828ac00afa6778413f0cb2633427745d83@[::]:30303
INFO [03-14|00:06:16] RLPx listener up                         self=enode://6fc14916cefae9082d017a9266a4eed4360719838ba656f59c7bbfdeffc7d933bfeceeae0d7cbdc2f82da2129a6524828ac00afa6778413f0cb2633427745d83@[::]:30303
INFO [03-14|00:06:16] IPC endpoint opened                      url=\\\\.\\pipe\\geth.ipc
INFO [03-14|00:06:16] HTTP endpoint opened                     url=http://127.0.0.1:8545 cors= vhosts=localhost
Welcome to the Geth JavaScript console!

instance: Geth/v1.8.2-stable-b8b9f7f4/windows-amd64/go1.9.2
INFO [03-14|00:06:17] Etherbase automatically configured       address=0xfCB7bCC7492da0e8921692103F9cF284Fe47575F
coinbase: 0xfcb7bcc7492da0e8921692103f9cf284fe47575f
at block: 0 (Tue, 13 Mar 2018 23:39:42 CET)
 datadir: E:\EthPriv\node1
 modules: admin: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

> __

Mine some starting coins

In the geth console, issue:

miner.start()

This will generate the new DAG structure used in ETHash mining, and then start mining blocks.

INFO [03-14|00:14:34] Generating DAG in progress               epoch=0 percentage=96 elapsed=1m54.247s
INFO [03-14|00:14:35] Generating DAG in progress               epoch=0 percentage=97 elapsed=1m55.416s
INFO [03-14|00:14:36] Generating DAG in progress               epoch=0 percentage=98 elapsed=1m56.578s
INFO [03-14|00:14:38] Generating DAG in progress               epoch=0 percentage=99 elapsed=1m58.159s
INFO [03-14|00:14:38] Generated ethash verification cache      epoch=0 elapsed=1m58.167s
INFO [03-14|00:14:49] Generating DAG in progress               epoch=1 percentage=0  elapsed=2.459s
INFO [03-14|00:14:52] Generating DAG in progress               epoch=1 percentage=1  elapsed=4.930s
INFO [03-14|00:14:54] Generating DAG in progress               epoch=1 percentage=2  elapsed=7.320s
INFO [03-14|00:14:57] Generating DAG in progress               epoch=1 percentage=3  elapsed=9.819s
INFO [03-14|00:14:59] Generating DAG in progress               epoch=1 percentage=4  elapsed=12.225s
INFO [03-14|00:15:02] Generating DAG in progress               epoch=1 percentage=5  elapsed=14.647s
INFO [03-14|00:15:03] Successfully sealed new block            number=1 hash=bb70a1…805665
INFO [03-14|00:15:03] 🔨 mined potential block                  number=1 hash=bb70a1…805665
INFO [03-14|00:15:03] Commit new mining work                   number=2 txs=0 uncles=0 elapsed=0s
INFO [03-14|00:15:04] Generating DAG in progress               epoch=1 percentage=6  elapsed=17.049s
INFO [03-14|00:15:07] Generating DAG in progress               epoch=1 percentage=7  elapsed=19.637s
INFO [03-14|00:15:09] Generating DAG in progress               epoch=1 percentage=8  elapsed=22.203s
INFO [03-14|00:15:10] Successfully sealed new block            number=2 hash=89b11c…15a266
INFO [03-14|00:15:10] 🔨 mined potential block                  number=2 hash=89b11c…15a266
INFO [03-14|00:15:10] Commit new mining work                   number=3 txs=0 uncles=0 elapsed=0s
INFO [03-14|00:15:12] Generating DAG in progress               epoch=1 percentage=9  elapsed=24.853s
INFO [03-14|00:15:14] Generating DAG in progress               epoch=1 percentage=10 elapsed=27.524s
INFO [03-14|00:15:17] Generating DAG in progress               epoch=1 percentage=11 elapsed=30.196s
INFO [03-14|00:15:18] Successfully sealed new block            number=3 hash=f3cce3…491273
INFO [03-14|00:15:18] 🔨 mined potential block                  number=3 hash=f3cce3…491273
INFO [03-14|00:15:18] Commit new mining work                   number=4 txs=0 uncles=0 elapsed=0s
INFO [03-14|00:15:20] Generating DAG in progress               epoch=1 percentage=12 elapsed=32.993s
INFO [03-14|00:15:23] Successfully sealed new block            number=4 hash=a5a610…09a4a3
INFO [03-14|00:15:23] 🔨 mined potential block                  number=4 hash=a5a610…09a4a3
INFO [03-14|00:15:23] Commit new mining work                   number=5 txs=0 uncles=0 elapsed=0s
INFO [03-14|00:15:23] Generating DAG in progress               epoch=1 percentage=13 elapsed=35.699s

Attaching Mist

Exactly as in the development blockchain, you can start Mist so it attaches to the new blockchain and use it as usual.

The pre-funded address

Attaching new nodes

  1. Initialise the data directory on a new system with the same genesis.json file (because the default is to use the mainnet)
  2. On the first system, look up its "enode" discovery address in the console by inspecting this variable:
    admin.nodeInfo.enode
  3. Start geth on the second system by running:
    geth --datadir node2 console
  4. In the console, peer it with the first node by using the "enode" variable where the host part is replaced with the IP address:
    admin.addPeer("enode://6fc14916cefae9082d017a9266a4eed4360719838ba656f59c7bbfdeffc7d933bfeceeae0d7cbdc2f82da2129a6524828ac00afa6778413f0cb2633427745d83@10.0.0.216:30303")

Note: check firewalls!

Bootnode

  • The "bootnode" tool starts a light-weight discovery-only node, which can mediate between different geth full nodes
  • It's used with the --bootnode argument to geth
  • No automatic discovery of nodes beyond that.

Live demo and Q&A

....

THE END

ivan.voras@toptal.com

Blockchain lecture #7: Creating a new blockchain using Ethereum code

March 2018

Q&A?

Blockchain lectures #7: Creating a custom Ethereum blockchain

By Ivan Voras

Blockchain lectures #7: Creating a custom Ethereum blockchain

Creating a new blockchain with the Ethereum code by using the built-in facilities which enable the creation of arbitrary new blockchains for production use.

  • 594