Setting Up a Private Ethereum Network with Geth
Geth Geth is a concrete implementation of the Ethereum protocol. Through Geth, you can implement various Ethereum features, such as creating, editing, and deleting accounts, starting mining, transferring Ether, and deploying and executing smart contracts. Geth, also known as go-ethereum, is one of three implementations of the Ethereum protocol. Geth can be installed on many operating systems, including Windows, Linux, Mac OSX, Android, and iOS. Installation The process takes a while — be patient. After installation, run to verify: Defining the Private Genesis State First, you need to define the starting state of the private network. All subsequent nodes need to agree on this. The starting state is defined by a JSON file, for example . Navigate to a directory in the command line and create the file . - // The blockchain ID; the parameter in the geth command must match the chainId value - // Homestead hard fork block height; no need to worry about this - // EIP 155 hard fork height; no need to worry about this - // EIP 158 hard fork height; no need to worry about this - // The miner account; after the first block is mined, the Ether reward will be sent to this miner account - // Difficulty value; the higher, the harder - // Additional information; fill in anything - // The total gas consumption limit, used to limit the total amount of transaction information a block can contain; set to the maximum since this is a private chain - // A 64-bit random number - // Used with nonce for mining; a hash generated from part of the previous block - // The hash value of the previous block - // Preset accounts and their Ether balances; can be left unconfigured since mining on a private chain is easy Typically the definitions above work fine, but it's best to change the nonce to some random value to prevent being connected to by unknown remote nodes. The field can be used to configure accounts, for example to provide funds for testing. After defining the genesis state in the JSON file, you also need to initialize each Geth node before starting, to ensure all blockchain parameters are set correctly. Run the following in the project directory: specifies the directory where the node data is saved. Running the Interactive Console We can run Geth's built-in JavaScript console with the following command: Output: Type to quit. Starting the Node - must match the chainId in genesis.json - sets the node ID - geth node port; default: 30303 - enables the RPC service; default: 8545 - RPC service port - setting here allows access from any domain; you can also specify a specific domain such as disables automatic node discovery - log level: 0=silent, 1=error, 2=warn, 3=info, 4=debug, 5=detail (default: 3) You can use to view node information. Creating a User Create an account in the console: Here, is the account password. View the account list: Creating and Starting a Second Node In a new terminal, run: Use to change the port and avoid conflicts. Connecting Nodes Check currently connected nodes in the network. As you can see, there are no nodes connected yet. In the first node's terminal, run: is the information from the second node's . Checking the node list again, you can now see: Perform a similar operation in the second terminal. Mining Check the block count: This is because mining hasn't started yet. Run the following command in the first terminal to start mining: Open a new terminal window and run: When the percentage reaches 100, mining will begin. Check the block count again after a while: Stop mining: In the other node's terminal, running will also show the block count, because when the first block is mined, the block information is broadcast. The second node, upon receiving and verifying the block information, adds it to the chain. Querying Block Information You can see that the files under and are similar; in fact, the data in those files is identical. In the first node's terminal, view the block at index 12: The same block information can be viewed from the second node's terminal, both with a hash of . Querying block 78's hash also returns on both nodes. Note: The parameter must be consistent, otherwise will not find the peer after adding. Use the command to view the miner of the most recently mined block. As you can see, the output is the first node's account address: Check the account balance of the miner address; as mining continues, the account balance keeps increasing: Add an account on the second node (an account must be added before mining), and start mining: After initialization is complete, check the miner of the latest block: The two miners are now racing to mine 🙂. Check the second miner's account: Here is a desktop screenshot for reference: Supplement: Using IPC calls (referring to other processes on the same computer using the IPC file to create two-way communication with geth, for example calling the same node from another terminal): Calling from another terminal: