@[TOC](Table of Contents)
1. Install Ethereum Client#
brew tap ethereum/ethereum
Add the Ethereum Homebrew repository to prepare for the subsequent installation.
brew install ethereum
Install the Ethereum client (mainly Geth) from the Ethereum repository.
2. Create Required Folders and Configure genesis.json File#
- Create a blockchain folder to store the local geth, and create data0 and geth folders inside it, as shown in the figure below.
data0 folder:
Stores blockchain data (such as blocks, transactions, account states) and related configurations.
geth folder:
Is a subdirectory generated by Geth, storing specific block data, logs, keystore, and other information.
genesis.json file:
The genesis block configuration file used to define the initial state of the private chain (such as difficulty, gasLimit, pre-allocated accounts, etc.).
Below is the code for the genesis.json file.
{
"config": {
"chainId": 1337,
"homesteadBlock": 0,
"eip150Block": 0,
"eip155Block": 0,
"eip158Block": 0,
"byzantiumBlock": 0,
"constantinopleBlock": 0,
"petersburgBlock": 0,
"istanbulBlock": 0,
"muirGlacierBlock": 0,
"berlinBlock": 0,
"londonBlock": 0,
"arrowGlacierBlock": 0,
"grayGlacierBlock": 0,
"shanghaiTime": 0,
"terminalTotalDifficulty": 0,
"terminalTotalDifficultyPassed": true
},
"nonce": "0x0",
"timestamp": "0x0",
"extraData": "0x",
"gasLimit": "0xaf79e0",
"difficulty": "0x1",
"mixHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"coinbase": "0x0000000000000000000000000000000000000000",
"alloc": {
"0000000000000000000000000000000000000003": {
"balance": "0x1"
},
"0000000000000000000000000000000000000003": {
"balance": "0x1"
},
"0000000000000000000000000000000000000003": {
"balance": "0x1"
},
"0000000000000000000000000000000000000004": {
"balance": "0x1"
},
"0000000000000000000000000000000000000005": {
"balance": "0x1"
},
"0000000000000000000000000000000000000006": {
"balance": "0x1"
},
"0000000000000000000000000000000000000007": {
"balance": "0x1"
},
"0000000000000000000000000000000000000008": {
"balance": "0x1"
},
"0000000000000000000000000000000000000009": {
"balance": "0x1"
}
},
"number": "0x0",
"gasUsed": "0x0",
"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"baseFeePerGas": "0x3b9aca00",
"excessBlobGas": null,
"blobGasUsed": null
}
3. Generate the Genesis Block of the Blockchain#
- Initialize the blockchain data directory and generate the genesis block.
geth --datadir data0 init genesis.json
The block data is stored in geth/chaindata, and account data is stored in keystore, but it is still empty now.
- Start the local private chain.
geth --datadir data0 --networkid 10 --http --http.port 8545 --http.api personal,db,eth,net,web3,miner,admin console
3. Query accounts.
eth.accounts
4. Create an account.
Right-click to open another terminal.
geth --datadir data0 account new
5. Query accounts again.
eth.accounts
6. Query account balance.
eth.getBalance(eth.accounts[0])
Find that the balance is 0, and after mining, the balance does not change either.
==Here, we need to initialize the account's amount because starting from version 13 of geth, the account amount must be set in the genesis.json file, so go back to the genesis.json file, modify the code again, replacing it with the private key of the account just created and setting the amount==
As for why it was not set in the genesis.json file beforehand, it is because we need to create the account first.
- Modify the genesis.json file again.
{
"config": {
"chainId": 1337,
"homesteadBlock": 0,
"eip150Block": 0,
"eip155Block": 0,
"eip158Block": 0,
"byzantiumBlock": 0,
"constantinopleBlock": 0,
"petersburgBlock": 0,
"istanbulBlock": 0,
"muirGlacierBlock": 0,
"berlinBlock": 0,
"londonBlock": 0,
"arrowGlacierBlock": 0,
"grayGlacierBlock": 0,
"shanghaiTime": 0,
"terminalTotalDifficulty": 0,
"terminalTotalDifficultyPassed": true
},
"nonce": "0x0",
"timestamp": "0x0",
"extraData": "0x",
"gasLimit": "0xaf79e0",
"difficulty": "0x1",
"mixHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"coinbase": "0x0000000000000000000000000000000000000000",
"alloc": {
"8f3D6d2d65948cFE4470cCD478F14194f630e813": {
"balance": "0x100000000000000000000000000"
},
"0000000000000000000000000000000000000003": {
"balance": "0x1"
},
"0000000000000000000000000000000000000003": {
"balance": "0x1"
},
"0000000000000000000000000000000000000004": {
"balance": "0x1"
},
"0000000000000000000000000000000000000005": {
"balance": "0x1"
},
"0000000000000000000000000000000000000006": {
"balance": "0x1"
},
"0000000000000000000000000000000000000007": {
"balance": "0x1"
},
"0000000000000000000000000000000000000008": {
"balance": "0x1"
},
"0000000000000000000000000000000000000009": {
"balance": "0x1"
}
},
"number": "0x0",
"gasUsed": "0x0",
"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"baseFeePerGas": "0x3b9aca00",
"excessBlobGas": null,
"blobGasUsed": null
}
8. Reinitialize.
==Delete the geth folder to clear the data directory, as there is already a genesis block (genesis) in the data0 data directory.==
Reinitialize.
geth --datadir data0 init genesis.json
- Restart the local private chain and query the account balance.
geth --datadir data0 --networkid 10 --http --http.port 8545 --http.api personal,db,eth,net,web3,miner,admin console
Query the balance.
eth.getBalance(eth.accounts[0])
At this point, we have successfully set up our own Geth private chain on macOS. From environment installation to private chain initialization, to node startup and account querying.
I hope this tutorial helps you take an important step into blockchain development! If you encounter problems, feel free to try more and learn more; I believe you will be able to master Geth!