Jeidy

Jeidy

hihihi

手把手教你在 macOS 上搭建屬於自己的 Geth 私鏈

@TOC


一、安裝 ethereum 客戶端#

brew tap ethereum/ethereum
添加以太坊的 Homebrew 軟體倉庫,為後續安裝做準備
在這裡插入圖片描述
brew install ethereum
從以太坊倉庫中安裝 Ethereum 客戶端(主要是 Geth)
這裡已經安裝過了 geth


二、創建所需資料夾,配置 genesis.json 文件#

1. 新建一個 blockchain 資料夾來存儲本地 geth,裡面分別創建 data0、geth 資料夾,以及 genesis.json 文件,如下圖所示
在這裡插入圖片描述
data0 資料夾:
存放區塊鏈數據(如區塊、交易、帳戶狀態)以及相關配置

geth 資料夾:
是 Geth 生成的子目錄,存儲具體的區塊數據、日誌、密鑰庫等信息

genesis.json 文件:
創世區塊配置文件,用於定義私鏈的初始狀態(如難度、gasLimit、預置帳戶等)
下面是 genesis.json 文件的代碼

{
    "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
}


三、生成區塊鏈的創世區塊#

1. 初始化區塊鏈數據目錄並生成創世區塊
geth --datadir data0 init genesis.json

在這裡插入圖片描述
在這裡插入圖片描述
其中 geth/chaindata 中存放的是區塊數據,keystore 中存放的是帳戶數據,但現在還是空的

2. 啟動本地私鏈
geth --datadir data0 --networkid 10 --http --http.port 8545 --http.api personal,db,eth,net,web3,miner,admin console

在這裡插入圖片描述
3. 查詢帳戶
eth.accounts
在這裡插入圖片描述
4. 創建帳號
右鍵再打開一個終端
geth --datadir data0 account new
在這裡插入圖片描述
![在這裡插入圖片描述](https://i-blog.csdnimg.cn/direct/61bdad54161747d58ecd48187cdeef84.png#pic_center =300x) 5. 再次查詢帳戶
eth.accounts
在這裡插入圖片描述
6. 查詢帳戶餘額
eth.getBalance(eth.accounts[0])

發現餘額為 0,且挖礦後,餘額也沒有變化

== 這裡要初始化帳號的金額,因為從 geth 的 13 版本後,就要在 genesis.json 裡面設置帳號金額了,所以回到 genesis.json 文件,重新修改代碼,我們要替換成剛剛創建的帳戶私鑰,並設置金額 ==

至於為什麼一開始不提前在 genesis.json 文件裡面設置好,因為要先創建帳戶呀
在這裡插入圖片描述

7. 重新修改 genesis.json 文件

{
    "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. 重新初始化
== 刪除 geth 資料夾,目的是清空數據目錄,因為 data0 數據目錄中已經存在一個創世區塊(genesis)==
在這裡插入圖片描述
重新初始化
geth --datadir data0 init genesis.json

9. 再次啟動本地私鏈,查詢帳戶餘額
geth --datadir data0 --networkid 10 --http --http.port 8545 --http.api personal,db,eth,net,web3,miner,admin console

查詢餘額
eth.getBalance(eth.accounts[0])
在這裡插入圖片描述

至此,我們已經成功在 macOS 上搭建了一個屬於自己的 Geth 私鏈。從環境安裝到私鏈初始化,再到節點啟動和帳戶查詢

希望這篇教程對你有所幫助,帶你邁出區塊鏈開發的重要一步!如果遇到問題,不妨多嘗試,多學習,相信你一定能玩轉 Geth!

載入中......
此文章數據所有權由區塊鏈加密技術和智能合約保障僅歸創作者所有。