【產品也編程】技術小白用geth把自己的電腦變成ETH網絡

在寫這篇教程之前,我是連 cmd 都不知道使用的技術小白,在官方文檔、技術博客、公司技術同事的幫助下,硬磕了1、2天才從0到1構建了一個ETH網絡。以下是我嘔心瀝血整理的教程,應該是網上最小白的教程了。

1. 下載并安裝geth,支持Windows、Mac、Linux、源碼

安裝包中包含Geth和開發者工具包

? Geth = 以太坊節點進程 + 與區塊鏈交互的命令行客戶端 + 密鑰管理與簽名工具;

? 開發者工具包,包含以下:

安裝成功后,在本地指定目錄新增以下程序:

安裝成功后,geth會自動加入到系統環境變量中,因此可以直接打開Windows CMD輸入 geth version,查看版本號,以此證明安裝成功。

C:\Users\user>geth version

Geth

Version: 1.9.12-stable

Git Commit: b6f1c8dcc058a936955eb8e5766e2962218924bc

Git Commit Date: 20200316

Architecture: amd64

Protocol Versions: [65 64 63]

Go Version: go1.13.8

Operating System: windows

GOPATH=

GOROOT=C:\go

2. 在啟動geth程序之前,先創建以太坊公私鑰對,打開Windows cmd, 輸入 geth account new , 輸入密碼,為了安全,輸入的密碼是不會在cmd上顯示出來的, 成功后,錢包地址會顯示出來,私鑰文件自動存在本地默認目錄下,并且再次文字強調要保管好私鑰。

C:\Users\user>geth account new

INFO [04-10|17:58:44.410] Maximum peer count? ? ? ? ? ? ? ? ? ? ? ETH=50 LES=0 total=50

Your new account is locked with a password. Please give a password. Do not forget this password.

Password:

Repeat password:


Your new key was generated

Public address of the key:? 0xAb4a26Ca4cD52a84d6fc9fb2D7472D2C228742F7

Path of the secret key file: C:\Users\user\AppData\Local\Ethereum\keystore\UTC--2020-04-10T09-58-59.166758700Z--ab4a26ca4cd52a84d6fc9fb2d7472d2c228742f7

- You can share your public address with anyone. Others need it to interact with you.

- You must NEVER share the secret key with anyone! The key controls access to your funds!

- You must BACKUP your key file! Without the key, it's impossible to access account funds!

- You must REMEMBER your password! Without the password, it's impossible to decrypt the key!

可以創建多個公私鑰對,私鑰都是保存在同一個文件夾中。輸入 geth account list,可以將這個文件夾中所有的公私鑰對顯示出來。

C:\Users\user>geth account list

INFO [04-10|18:27:17.007] Maximum peer count? ? ? ? ? ? ? ? ? ? ? ETH=50 LES=0 total=50

Account #0: {ab4a26ca4cd52a84d6fc9fb2d7472d2c228742f7} keystore://C:\Users\user\AppData\Local\Ethereum\keystore\UTC--2020-04-10T09-58-59.166758700Z--ab4a26ca4cd52a84d6fc9fb2d7472d2c228742f7

Account #1: {820118cd4a04278c773ad30606798d84ce3578e9} keystore://C:\Users\user\AppData\Local\Ethereum\keystore\UTC--2020-04-10T10-25-07.390348400Z--820118cd4a04278c773ad30606798d84ce3578e9

3. 如果需要部署的是私鏈,可以手動創建創世的json文件,也可以用開發者工具中的puppeth,命令行交互式的完成配置,然后在同級目錄下生成一份js文件。支持配置的選項有:鏈的名字、鏈ID、POA算法還是POW算法、預充值賬戶、允許挖礦的賬戶等,如下:

+-----------------------------------------------------------+

| Welcome to puppeth, your Ethereum private network manager |

|? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? |

| This tool lets you create a new Ethereum network down to? |

| the genesis block, bootnodes, miners and ethstats servers |

| without the hassle that it would normally entail.? ? ? ? |

|? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? |

| Puppeth uses SSH to dial in to remote servers, and builds |

| its network components out of Docker containers using the |

| docker-compose toolset.? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? |

+-----------------------------------------------------------+

Please specify a network name to administer (no spaces, hyphens or capital letters please)

> myethtestnet

Sweet, you can set this via --network=myethtestnet next time!

?[32mINFO ?[0m[04-11|17:01:54.956] Administering Ethereum network? ? ? ? ? ?[32mname?[0m=myethtestnet

?[33mWARN ?[0m[04-11|17:01:55.141] No previous configurations found? ? ? ? ?[33mpath?[0m=.puppeth\\myethtestnet

What would you like to do? (default = stats)

1. Show network stats

2. Configure new genesis

3. Track new remote server

4. Deploy network components

> 2

What would you like to do? (default = create)

1. Create new genesis from scratch

2. Import already existing genesis

> 1

Which consensus engine to use? (default = clique)

1. Ethash - proof-of-work

2. Clique - proof-of-authority

> 2

//最好選擇POA算法,出塊時間固定,ETH最大測試網rinkeby用的就是POA算法

How many seconds should blocks take? (default = 15)

> 5

//設置5秒出個塊,對應json文件clique:{"period"}

Which accounts are allowed to seal? (mandatory at least one)

> 0xAb4a26Ca4cD52a84d6fc9fb2D7472D2C228742F7

> 0x820118Cd4a04278c773aD30606798D84cE3578e9

> 0xaFdd64dc5A421cB35ceC78eC991e153045Ca2aB8

> 0x

//一個機器也可以部署多個節點,3個節點基本上可以覆蓋全部測試場景

Which accounts should be pre-funded? (advisable at least one)

> 0xAb4a26Ca4cD52a84d6fc9fb2D7472D2C228742F7

> 0x820118Cd4a04278c773aD30606798D84cE3578e9

> 0xaFdd64dc5A421cB35ceC78eC991e153045Ca2aB8

> 0x

Should the precompile-addresses (0x1 .. 0xff) be pre-funded with 1 wei? (advisable yes)

> no

// 如果選 yes,那么從 0x0000000000000000000000000000000000000000 到

// 0x00000000000000000000000000000000000000ff 這些地址都會有 1wei 的以太幣。

// 這個看情況選擇,用得著就選 yes

Specify your chain/network ID if you want an explicit one (default = random)

>

?[32mINFO ?[0m[04-11|17:12:53.755] Configured new genesis block

What would you like to do? (default = stats)

1. Show network stats

2. Manage existing genesis

3. Track new remote server

4. Deploy network components

>

但是 geth 在讀取這個配置文件時,只會取到「“genesis”」這個字段,后面的字段不會取到,所以要將 "genesis" 字段的內容作為這個 json 文件的內容(相當于刪除{ "genesis":這段字符和文件末尾最后一個})。

3. 創建節點數據存儲的文件夾,并用創世文件初始化節點。創建文件夾可以通過CMD自帶的 mkdir 實現,也可以直接在GUI的文件夾下創建。指定節點數據存儲位置用 datadir 選項,讀取創世配置文件初始化節點用 init 命令。

C:\Users\user>e:

E:\>cd myethtestnet

//我希望節點數據存儲在E盤,所以先指定根目錄

E:\myethtestnet>geth --datadir ./node1? init ./mygenesis

INFO [04-11|17:39:37.240] Maximum peer count? ? ? ? ? ? ? ? ? ? ? ETH=50 LES=0 total=50

INFO [04-11|17:39:37.687] Allocated cache and file handles? ? ? ? database=E:\\myethtestnet\\node1\\geth\\chaindata cache=16.00MiB handles=16

INFO [04-11|17:39:37.807] Writing custom genesis block

INFO [04-11|17:39:37.820] Persisted trie from memory database? ? ? nodes=4 size=657.00B time=998.4μs gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00B

INFO [04-11|17:39:37.841] Successfully wrote genesis state? ? ? ? database=chaindata? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? hash=838c17…ced2bd

INFO [04-11|17:39:37.854] Allocated cache and file handles? ? ? ? database=E:\\myethtestnet\\node1\\geth\\lightchaindata cache=16.00MiB handles=16

INFO [04-11|17:39:38.080] Writing custom genesis block

INFO [04-11|17:39:38.088] Persisted trie from memory database? ? ? nodes=4 size=657.00B time=1.0142ms gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00B

INFO [04-11|17:39:38.110] Successfully wrote genesis state? ? ? ? database=lightchaindata? ? ? ? ? ? ? ? ? ? ? ? ? hash=838c17…ced2bd

//初始化第一個節點node1

E:\myethtestnet>geth --datadir ./node2? init ./mygenesis

INFO [04-11|17:40:50.190] Maximum peer count? ? ? ? ? ? ? ? ? ? ? ETH=50 LES=0 total=50

INFO [04-11|17:40:50.627] Allocated cache and file handles? ? ? ? database=E:\\myethtestnet\\node2\\geth\\chaindata cache=16.00MiB handles=16

INFO [04-11|17:40:50.746] Writing custom genesis block

INFO [04-11|17:40:50.753] Persisted trie from memory database? ? ? nodes=4 size=657.00B time=999.8μs gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00B

INFO [04-11|17:40:50.765] Successfully wrote genesis state? ? ? ? database=chaindata? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? hash=838c17…ced2bd

INFO [04-11|17:40:50.770] Allocated cache and file handles? ? ? ? database=E:\\myethtestnet\\node2\\geth\\lightchaindata cache=16.00MiB handles=16

INFO [04-11|17:40:50.879] Writing custom genesis block

INFO [04-11|17:40:50.887] Persisted trie from memory database? ? ? nodes=4 size=657.00B time=997.8μs gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00B

INFO [04-11|17:40:50.910] Successfully wrote genesis state? ? ? ? database=lightchaindata? ? ? ? ? ? ? ? ? ? ? ? ? hash=838c17…ced2bd

//初始化第二個節點node2

E:\myethtestnet>geth --datadir ./node3? init ./mygenesis

INFO [04-11|17:41:08.357] Maximum peer count? ? ? ? ? ? ? ? ? ? ? ETH=50 LES=0 total=50

INFO [04-11|17:41:08.781] Allocated cache and file handles? ? ? ? database=E:\\myethtestnet\\node3\\geth\\chaindata cache=16.00MiB handles=16

INFO [04-11|17:41:08.937] Writing custom genesis block

INFO [04-11|17:41:08.946] Persisted trie from memory database? ? ? nodes=4 size=657.00B time=1.0007ms gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00B

INFO [04-11|17:41:08.974] Successfully wrote genesis state? ? ? ? database=chaindata? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? hash=838c17…ced2bd

INFO [04-11|17:41:08.989] Allocated cache and file handles? ? ? ? database=E:\\myethtestnet\\node3\\geth\\lightchaindata cache=16.00MiB handles=16

INFO [04-11|17:41:09.112] Writing custom genesis block

INFO [04-11|17:41:09.118] Persisted trie from memory database? ? ? nodes=4 size=657.00B time=0s? ? ? gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00B

INFO [04-11|17:41:09.146] Successfully wrote genesis state? ? ? ? database=lightchaindata? ? ? ? ? ? ? ? ? ? ? ? ? hash=838c17…ced2bd

//初始化第三個節點node3

初始化節點成功后,會在指定目錄下增加兩個文件夾:

其中 geth/chaindata 中存放的是區塊數據,keystore 中存放的是賬戶數據

4. 配置節點P2P發現網絡。新節點要接入P2P網絡,至少要發現網絡中的一個節點。加入公共網絡,直接啟動節點即可,因為節點代碼中已經寫死了boot節點;創建私網,需要用 --networkid="12345" 連接上已知節點;或者將靜態節點的地址寫到 static-nodes.json 文件中,始終通過靜態節點發現網絡。我們采用靜態節點發現網絡的方式,將3個節點的地址全部加到文件中。

先需要知道節點的P2P地址,查詢方法,運行geth(記得用 --datadir 指定數據存儲位置),運行后按 Ctrl+C 停止運行:

E:\myethtestnet>geth --datadir ./node2

……

……

INFO [04-11|18:20:59.174] Started P2P networking? ? ? ? ? ? ? ? ? self=enode://c1f667baf27075879900ef48b90c55842c7dfcf2fc63710fdd36955438131087fe259156c0b4595a4fb7ad293b982609ee81edab321f08f61617b02d554dec71@127.0.0.1:30303

在運行過程中輸出的log中,有這么一行:enode:……@127.0.0.1:30303。就是該節點的P2P地址,注意P2P地址不是IP地址。

接著創建static-nodes.json 文件,文件名一定要一致,按照以下格式輸入三個節點的地址:

[

? "enode://f4642fa65af50cfdea8fa7414a5def7bb7991478b768e296f5e4a54e8b995de102e0ceae2e826f293c481b5325f89be6d207b003382e18a8ecba66fbaf6416c0@33.4.2.1:30303",

? "enode://pubkey@ip:port"

]

注意,3個節點的端口(port)號不能一樣。

注意,一定要將json文件放在節點目錄下,如:E:\myethtestnet\node3

5. 啟動節點,使用以下命令:

geth --datadir ./node1 --unlock 0 --mine --rpc

geth --datadir ./node2 --unlock 0 --mine --port 30304

geth --datadir ./node3 --unlock 0 --mine --port 30305

--mine,有該選項表示該節點支持挖礦;

--rpc,有該選項表示該節點啟動rpc功能,除了第一個節點,其它節點都沒有啟動 rpc 功能。一是因為 rpc 功能有一個節點啟動就夠用了;二是因為多個節點啟動 rpc 功能,rpc 端口被第一個啟動的節點占用,后面的啟動就會報失?。?/p>

--port,指定了P2P連接的端口,需要與 static-nodes.json 文件中的端口一致,且不同節點不能重復;

節點啟動,默認是按照fast節點模式運行。補充說明一下ETH共有三種節點模式:

? Full: Downloads all blocks (including headers, transactions and receipts) and generates the state of the blockchain incrementally by executing every block.

? Fast (Default): Downloads all blocks (including headers, transactions and receipts), verifies all headers, and downloads the state and verifies it against the headers.

? Light: Downloads all block headers, block data, and verifies some randomly.

?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 230,622評論 6 544
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 99,716評論 3 429
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 178,746評論 0 383
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,991評論 1 318
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 72,706評論 6 413
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 56,036評論 1 329
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 44,029評論 3 450
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 43,203評論 0 290
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 49,725評論 1 336
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 41,451評論 3 361
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 43,677評論 1 374
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 39,161評論 5 365
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,857評論 3 351
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 35,266評論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,606評論 1 295
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 52,407評論 3 400
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 48,643評論 2 380

推薦閱讀更多精彩內容

  • 背景: 閱讀新聞 12C CDB模式下RMAN備份與恢復 [日期:2016-11-29] 來源:Linux社區 作...
    陽屯okyepd閱讀 3,558評論 0 7
  • 目標:逐步指導,幫助你使用權威證明共識引擎(也稱為clique)設置本地私有以太網網絡。 簡而言之:我們將在同一臺...
    編程狂魔閱讀 1,517評論 0 1
  • “你相信死后重生嗎?”老板問他的一名雇員。 “相信,先生。”這個員工回答。 “哦,那么每一件事就正正好了這么說來,...
    終遇傘上花閱讀 135評論 0 0
  • 今天看到排地鐵等待的人里幾個在玩兒消消樂。 我沒忍住,在那個間歇里,我也點擊了下載。 還沒下載完,然后地鐵來了。就...
    ArtisticYouth閱讀 429評論 0 2
  • 一:任務分解 A:總任務制定1500件 周一到周四:每天40件 周五到周日:每天70件 B:個人業績分解與規劃 1...
    鄔曉樂閱讀 417評論 0 0