【产品也编程】技术小白用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 端口被第一个启动的节点占用,后面的启动就会报失败;

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

你可能感兴趣的:(【产品也编程】技术小白用geth把自己的电脑变成ETH网络)