Initializing a private node - cogeorg/teaching GitHub Wiki
Tools like Ganache or Truffle come with Ethereum emulators that provide the same interface as an Ethereum node but work completely different internally. If you want to ensure that your contract will run properly on the main network, it is highly recommended to test it on a real ethereum node.
First of all, create a folder that will contain the database and the accounts for your private node
$ mkdir -p Ethereum/private
$ cd Ethereum/private
Before you can start the private instance, the genesis block needs to be defined. Doing so from scratch or copy/paste is not recommended because the structure depends on your installation of Geth. Therfore, we are using a tool called Puppeth that is a command-line tool that is installed together with Geth.
$ puppeth
The tool will first ask you for the network name. Call it genesis
. Next, it will give you several options to procede, choose "2. Configure new genesis" by typing 2.
Now the tool will guide you through the setup. Please create a proof-of-work network with no prefunded accounts. When you get to the question about the network ID, make sure not to use any of the predefined IDs like 1 (Main net), 2 (Morden test net), 3 (Ropsten test net), 4 (Rinkeby test net), or 42 (Kovan test net).
Once the tool asks you "What would you like to do?", the genesis block has been created but is still in memory. We need to export it into a file, thus choose "2. Manage existing genesis". Export the file and then exit puppeth by typing ^C
(Ctrl + C). An explanation of the items in the genesis block can be found in Genesis Block. Note that puppeth creates multiple genesis files
genesis.json
-- native genesis spec used by puppeth and Gethgenesis-parity.json
--genesis spec used by Paritygenesis-aleth.json
-- genesis spec used by Aleth (formerly C++ Ethereum)genesis-harmony.json
-- genesis spec used by Harmony (formerly EthereumJ)
but we only use genesis.json
.
This is all we need to initialize the private node, which will be created with the following parameters
- datadir: target directory where the private node data is saved (without this parameter, the node uses the folder for the main network, which is not desired)
- GENESIS.json: the path to the genesis block file
Since you should already be in this directory, simply type
$ geth --datadir . init genesis.json
This command will create two directories
- geth: stores the chain data
- keystore: stores the accounts