Jeidy

Jeidy

hihihi

A step-by-step guide to setting up your own Geth private chain on macOS

@[TOC](Table of Contents)


1. Install Ethereum Client#

brew tap ethereum/ethereum
Add the Ethereum Homebrew repository to prepare for the subsequent installation.
Insert image description here
brew install ethereum
Install the Ethereum client (mainly Geth) from the Ethereum repository.
Geth has been installed here


2. Create Required Folders and Configure genesis.json File#

  1. Create a blockchain folder to store the local geth, and create data0 and geth folders inside it, as shown in the figure below.
    Insert image description here
    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#

  1. Initialize the blockchain data directory and generate the genesis block.
    geth --datadir data0 init genesis.json

Insert image description here
Insert image description here
The block data is stored in geth/chaindata, and account data is stored in keystore, but it is still empty now.

  1. Start the local private chain.
    geth --datadir data0 --networkid 10 --http --http.port 8545 --http.api personal,db,eth,net,web3,miner,admin console

Insert image description here
3. Query accounts.
eth.accounts
Insert image description here
4. Create an account.
Right-click to open another terminal.
geth --datadir data0 account new
Insert image description here
![Insert image description here](https://i-blog.csdnimg.cn/direct/61bdad54161747d58ecd48187cdeef84.png#pic_center =300x)5. Query accounts again.
eth.accounts
Insert image description here
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.
Insert image description here

  1. 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
}

Note this here
8. Reinitialize.
==Delete the geth folder to clear the data directory, as there is already a genesis block (genesis) in the data0 data directory.==
Insert image description here
Reinitialize.
geth --datadir data0 init genesis.json

  1. 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])
Insert image description here

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!

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.