前言

为了进一步了解以太坊区块链网络的工作方式和运行原理,笔者通过官方软件Geth搭建了私有以太坊网络fantasynetwork,最终实现了单机和多机节点间的相互连通:首先通过VMware Workstation创建基础Ubuntu实验平台,再安装Golang[1]、Geth[2]等依赖环境;其次使用puppeth工具生成私网的配置文件fantasynetwork.json并复制到三个节点目录下,三个节点均使用该配置文件初始化网络;最后使用static-nodes.json的方式将三个节点设为默认接入节点,实现节点间的连通,连通后各节点中的账户可以互相转账挖矿。

单机多节点私网[3]

本试验的项目结构为:

privateNet
├── accounts.txt
├── fantasynetwork.json
├── node1
│ ├── geth
│ ├── keystore
│ ├── node.sh
│ ├── password.txt
│ └── static-nodes.json
├── node2
│ ├── geth
│ ├── keystore
│ ├── node.sh
│ ├── password.txt
│ └── static-nodes.json
└── node3
├── geth
├── keystore
├── node.sh
├── password.txt
└── static-nodes.json

创建工作目录

  1. 创建私网工作目录mkdir privateNet && cd privateNet
  2. 创建三个节点数据目录mkdir node1 node2 node3
     privateNet/
    ├── node1
    ├── node2
    └── node3

创建默认用户

  1. Node1
     test@ubuntu:~/privateNet$ geth --datadir node1/ account new
    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: 0x600d77B8ce36B829BFC8a1Cc5696Faf2218bDf75
    Path of the secret key file: node1/keystore/UTC--2021-08-11T04-51-26.533482715Z--600d77b8ce36b829bfc8a1cc5696faf2218bdf75
  2. Node 2
     test@ubuntu:~/privateNet$ geth --datadir node2/ account new
    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: 0x2F7fD5BD0026f7C2f0dB94b79D58AFE517BC56d2
    Path of the secret key file: node2/keystore/UTC--2021-08-11T04-53-30.820914994Z--2f7fd5bd0026f7c2f0db94b79d58afe517bc56d2
  3. Node3
     test@ubuntu:~/privateNet$ geth --datadir node3/ account new
    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: 0x6c1440E9c6Ca93C18B1e2A069D1D5a70e29C2363
    Path of the secret key file: node3/keystore/UTC--2021-08-11T04-54-24.244487186Z--6c1440e9c6ca93c18b1e2a069d1d5a70e29c2363

在此创建账户的密码设置为fantasy,操作完成后会在每个节点目录下的keystore目录中找到账户密钥文件/钱包地址。

保存账户凭证

  1. 将以上账户的公钥地址保存至文本文件
     echo '0x600d77B8ce36B829BFC8a1Cc5696Faf2218bDf75' >> accounts.txt
    echo '0x2F7fD5BD0026f7C2f0dB94b79D58AFE517BC56d2' >> accounts.txt
    echo '0x6c1440E9c6Ca93C18B1e2A069D1D5a70e29C2363' >> accounts.txt
  2. 为了实验方便,将账户对应的密码文件保存至对应节点目录下
     echo 'fantasy' > node1/password.txt
    echo 'fantasy' > node2/password.txt
    echo 'fantasy' > node3/password.txt

创建配置文件

  1. 设置网络名称
     test@ubuntu:~/privateNet$ puppeth
    Please specify a network name to administer (no spaces, hyphens or capital letters please)
    > fantasynetwork
    Sweet, you can set this via --network=fantasynetwork next time!
    INFO [08-10|22:08:31.110] Administering Ethereum network name=fantasynetwork
    WARN [08-10|22:08:31.110] No previous configurations found path=/home/test/.puppeth/fantasynetwork
  2. 选择程序功能
     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
  3. 选择创建网络
     What would you like to do? (default = create)
    1. Create new genesis from scratch
    2. Import already existing genesis
    > 1
  4. 选择共识算法
     Which consensus engine to use? (default = clique)
    1. Ethash - proof-of-work
    2. Clique - proof-of-authority
    > 1
  5. 选择默认账号
     Which accounts should be pre-funded? (advisable at least one)
    > 0x600d77B8ce36B829BFC8a1Cc5696Faf2218bDf75
    > 0x2F7fD5BD0026f7C2f0dB94b79D58AFE517BC56d2
    > 0x6c1440E9c6Ca93C18B1e2A069D1D5a70e29C2363
    > 0x
  6. 设置默认单位
     Should the precompile-addresses (0x1 .. 0xff) be pre-funded with 1 wei? (advisable yes)
    > yes
  7. 设置网络ID
     Specify your chain/network ID if you want an explicit one (default = random)
    > 7777
    INFO [08-10|22:16:40.485] Configured new genesis block
  8. 导出配置文件
     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
    > 2
    1. Modify existing configurations
    2. Export genesis configurations
    3. Remove genesis configuration
    > 2
    Which folder to save the genesis specs into? (default = current)
    Will create fantasynetwork.json, fantasynetwork-aleth.json, fantasynetwork-harmony.json, fantasynetwork-parity.json
    >
    INFO [08-10|22:18:48.283] Saved native genesis chain spec path=fantasynetwork.json
    INFO [08-10|22:18:48.285] Saved genesis chain spec client=aleth path=fantasynetwork-aleth.json
    INFO [08-10|22:18:48.286] Saved genesis chain spec client=parity path=fantasynetwork-parity.json
    INFO [08-10|22:18:48.287] Saved genesis chain spec client=harmony path=fantasynetwork-harmony.json
  9. 退出工具
    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
    > ^C

此时可在当前目录下看见生成的四个配置文件,在此只用到fantasynetwork.json文件,其他文件可删去。

  1. 修改配置文件

    为了更容易挖到矿,将配置文件中difficulty难度值调小(其它参数含义可参考《创世区块配置文件genesis.json的格式解读》[4]):

初始化三个节点

每个节点必须使用相同的配置文件进行初始化:

geth --datadir node1/ init fantasynetwork.json
geth --datadir node2/ init fantasynetwork.json
geth --datadir node3/ init fantasynetwork.json

运行三个节点

进入对应的节点目录下执行一下命令(其它参数含义可参考《以太坊客户端Geth命令用法-参数详解》[5]):

  1. Node1
    geth --nousb --datadir=$pwd --syncmode 'full' --port 27271 --miner.gasprice 0 --miner.gastarget 470000000000 --http --http.addr 'localhost' --http.port 7271 --http.api admin,eth,miner,net,txpool,personal,web3 --mine --allow-insecure-unlock --unlock "0x600d77B8ce36B829BFC8a1Cc5696Faf2218bDf75" --password password.txt



    得到节点一的enode:

    enode://0f870fa3f8085f5abf74ea7c2a12a0809a9daaece20e3b1c4c80fb6929ff652681068c6ffd47852a4544dc282a4a15f531b452e05c4f1cf6861d4fb3b728edeb@127.0.0.1:27271
  2. Node2
    geth --nousb --datadir=$pwd --syncmode 'full' --port 27272 --miner.gasprice 0 --miner.gastarget 470000000000 --http --http.addr 'localhost' --http.port 7272 --http.api admin,eth,miner,net,txpool,personal,web3 --mine --allow-insecure-unlock --unlock "0x2F7fD5BD0026f7C2f0dB94b79D58AFE517BC56d2" --password password.txt

    得到节点二的enode:

    enode://45c2fc2bfdf0f48afe2083d82cc1cc642a96fcc2815755024a17b95b9fd1b3124f89e186c88a5013ced1c00bd10060a90e6b53e94fdbbfa6098b3088b3f78274@127.0.0.1:27272
  3. Node3
    geth --nousb --datadir=$pwd --syncmode 'full' --port 27273 --miner.gasprice 0 --miner.gastarget 470000000000 --http --http.addr 'localhost' --http.port 7273 --http.api admin,eth,miner,net,txpool,personal,web3 --mine --allow-insecure-unlock --unlock "0x6c1440E9c6Ca93C18B1e2A069D1D5a70e29C2363" --password password.txt

    得到节点三的enode:

    enode://ae4b4e18afa6238753e14ca3e99c0858509fc76efee715dd1c8278bbb7eaa5614fdc8b77a82bf7baf128c14ef574cc6701514fbb97780d30c731f7bc82dfd932@127.0.0.1:27273

连通三个节点

Geth主要有三种方法连通其它节点:启动前配置static-nodes.json文件添加节点、启动时通过--bootnodes添加节点、启动后在控制台通过admin.addPeer命令添加节点。在此我们使用第一种方法。

  1. 在工作目录下创建static-nodes.json
    [
    "enode://0f870fa3f8085f5abf74ea7c2a12a0809a9daaece20e3b1c4c80fb6929ff652681068c6ffd47852a4544dc282a4a15f531b452e05c4f1cf6861d4fb3b728edeb@127.0.0.1:27271",
    "enode://45c2fc2bfdf0f48afe2083d82cc1cc642a96fcc2815755024a17b95b9fd1b3124f89e186c88a5013ced1c00bd10060a90e6b53e94fdbbfa6098b3088b3f78274@127.0.0.1:27272",
    "enode://ae4b4e18afa6238753e14ca3e99c0858509fc76efee715dd1c8278bbb7eaa5614fdc8b77a82bf7baf128c14ef574cc6701514fbb97780d30c731f7bc82dfd932@127.0.0.1:27273"
    ]
  2. 将该文件复制到每个节点根目录下:
    cp static-nodes.json node1/
    cp static-nodes.json node2/
    cp static-nodes.json node3/
  3. 为了简化启动,可以创建启动节点批处理文件
    • node1/node.sh
      nohup geth --nousb --datadir=$pwd --syncmode 'full' --port 27271 --miner.gasprice 0 --miner.gastarget 470000000000 --http --http.addr 'localhost' --http.port 7271 --http.api admin,eth,miner,net,txpool,personal,web3 --mine --allow-insecure-unlock --unlock "0x600d77B8ce36B829BFC8a1Cc5696Faf2218bDf75" --password password.txt &
      echo "Geth started on node 1"
    • node2/node.sh
      nohup geth --nousb --datadir=$pwd --syncmode 'full' --port 27272 --miner.gasprice 0 --miner.gastarget 470000000000 --http --http.addr 'localhost' --http.port 7272 --http.api admin,eth,miner,net,txpool,personal,web3 --mine --allow-insecure-unlock --unlock "0x2F7fD5BD0026f7C2f0dB94b79D58AFE517BC56d2" --password password.txt &
      echo "Geth started on node 2"
    • node2/node.sh
      nohup geth --nousb --datadir=$pwd --syncmode 'full' --port 27273 --miner.gasprice 0 --miner.gastarget 470000000000 --http --http.addr 'localhost' --http.port 7273 --http.api admin,eth,miner,net,txpool,personal,web3 --mine --allow-insecure-unlock --unlock "0x6c1440E9c6Ca93C18B1e2A069D1D5a70e29C2363" --password password.txt &
      echo "Geth started on node 3"
  4. 启动各节点
    test@ubuntu:~/privateNet/node1$ sh node.sh
    test@ubuntu:~/privateNet/node2$ sh node.sh
    test@ubuntu:~/privateNet/node3$ sh node.sh
  5. 打开三个终端,使用geth attach命令接入三个节点命令行
    test@ubuntu:~/privateNet/node1$ geth attach geth.ipc
    test@ubuntu:~/privateNet/node2$ geth attach geth.ipc
    test@ubuntu:~/privateNet/node3$ geth attach geth.ipc

  6. 查看已连接节点

此时各节点已连接完成,每个节点账户默认为10个以太币,各节点账户间可自由转账和挖矿,需要注意的是转账后必须经过挖矿操作才能被写入区块链。上方法启动后的程序将会运行在后台,关闭需通过ps ax | grep geth命令和kill <process id>命令。

多机多节点私网

准备

  1. 设置上节Ubuntu虚拟机网络模式为桥接,且IP设为静态IP172.25.1.99
  2. Windows的IP设为静态172.25.1.55

加入私网

  1. 在Windows下创建文件夹node4,并将fantasynetwork.json复制到其中
  2. 初始化节点4
    C:\Users\Fantasy\Desktop\node4> geth --datadir . init fantasynetwork.json
  3. 新建账户
    C:\Users\Fantasy\Desktop\node4>geth -datadir . console
    > personal.newAccount("fantasy")
    WARN [08-11|16:13:32.987] Please remember your password!
    "0xbef61b5754ffaa843cc9199fb9a11aac468134f4"
    > exit
  4. 启动节点4
    geth --nousb --datadir=. --syncmode "full" --port 27271 --miner.gasprice 0 --miner.gastarget 470000000000 --http --http.addr 0.0.0.0 --http.port 7271 --http.api admin,eth,miner,net,txpool,personal,web3 --mine --allow-insecure-unlock console
  5. 添加节点1[6]
    > net.peerCount
    0
    > admin.addPeer("enode://0f870fa3f8085f5abf74ea7c2a12a0809a9daaece20e3b1c4c80fb6929ff652681068c6ffd47852a4544dc282a4a15f531b452e05c4f1cf6861d4fb3b728edeb@172.25.1.99:27271")
    true
    > net.peerCount
    1

此时各节点已连接完成,各节点账户间可自由转账和挖矿,需要注意的是转账后必须经过挖矿操作才能被写入区块链

注意事项:

  • 很多教程中说不同节点启动时不能使用相同的端口,那是因为其运行在同一个主机上,这里节点1和节点4运行在不同的端口上,故可以使用相同的端口。
  • 添加节点无效常见原因/解决办法:
    • admin.addPeer后等一段时间才会生效
    • admin.addPeer时使用的是NAT后公网地址,而公网防火墙通常拒绝异常接入
    • admin.addPeer后开始挖矿增加同步速度
    • 简单的方法是使用上节中的static-nodes.json方法

参考


  1. yuanlulu. golang学习1:ubuntu下安装golang并简单测试. CSDN. [2021-02-21]

  2. shciily. Linux系统下安装Geth客户端. CSDN. [2020-08-29]

  3. Divyang Desai. Setup Your Private Ethereum Network With Geth. c-sharpcorner.com. [2020-08-04]

  4. soowin. 创世区块配置文件genesis.json的格式解读. CSDN. [2021-01-26]

  5. mb5fe559b5073e8. 以太坊客户端Geth命令用法-参数详解. CSDN. [2021-06-13]

  6. Someone. "admin.addPeer" is not working. Github. [2020-09-27]

通过Geth搭建私有以太坊网络的更多相关文章

  1. 转:区块链开发(一)搭建基于以太坊go-ethereum的私有链环境

    区块链开发(一)搭建基于以太坊go-ethereum的私有链环境 wo541075754 · 2016-11-07 13:00:03 · 3730 次点击 · 预计阅读时间 3 分钟 · 约1小时前  ...

  2. 科普贴 | 以太坊网络中的Gas Limit 和 Gas Price 是什么概念?

    接触以太坊的同学都听过 Gas/ Gas Price/ Gas Limit,那么这些词汇究竟是什么意思? 还有,为什么有时候你的ETH转账会很慢?如何提高ETH转账速度? Ethereum平台 Vit ...

  3. geth搭建私有网络

    geth --rpc --unlock "3ae88fe370c39384fc16da2c9e768cf5d2495b48,81063419f13cab5ac090cd8329d8fff9f ...

  4. 使用Mist部署Contract到Rinkeby以太坊网络

    本文使用MyEthWallet新建一个账号,并导入到Mist中,然后部署Contract到Rinkeby网络使用MyEthWallet新建账号的好处是除了JSON文件之外,还能得到一张它生成的pdf( ...

  5. 区块链入门(2):搭建以太坊私有链(private network of ethereum),以及挖矿的操作..

    在做一些测试工作的时候, 为了方便控制以及更快的进入真正的测试工作,可能需要搭建一个私有的以太坊网络. 而以太坊节点之间能够互相链接需要满足1)相同的协议版本2)相同的networkid,所以搭建私有 ...

  6. 以太坊 链私有链环境搭建(windows)

    摸索以太坊区块链技术几个月了.最近打算逐步的把自己学到的东西和大家分享一下.在阅读本文之前,希望大家能对区块链的概念能有所了解.这样操作过程中的环节理解更深入.下面开始进入准备.因为本次是window ...

  7. Mac环境搭建以太坊私有链

    原文地址: 石匠的blog 为了测试以太坊智能合约,最方便的是在本地搭建一个以太坊私有链.在mac上搭建环境主要需要以下步骤. geth安装 geth是go-ethereum的简写,是一个用go语言编 ...

  8. 区块链学习(四)truffle部署编译智能合约以太坊私有链

    前面我们介绍了以太坊私有链的搭建以及多节点私有链网络,这次我们介绍如何使用truffle框架来部署编译智能合约到我们之前搭建的私有链网络中. 搭建环境及需使用的工具:ubuntu18.04  Truf ...

  9. 基于docker的以太坊集群的私有链开发环境

    转载博文:https://www.jianshu.com/p/8af386ec5f9e https://www.jianshu.com/p/7994db7a2b89?from=singlemessag ...

随机推荐

  1. 关于setInterval方法中function的定义方法

    使用window对象的setInterval方法,作为第一个参数传递的function必须在全局作用域中定义,否则会出现报错而无法执行. 具体如下: 在下面的代码中,试用jQuery方式在回调函数中使 ...

  2. 无脑安装——Python 及 安装python集成开发环境pycharm

    无脑安装--Python 及安装python集成开发环境pycharm 1.真机安装python 2.安装python集成开发环境pycharm Python 是一种解释型语言 Python 是面向对 ...

  3. SpringBoot源码解读系列三——引导注解

    我们再来看下SpringBoot应用的启动类: 查看代码 import org.springframework.boot.SpringApplication; import org.springfra ...

  4. HTTP流量神器Goreplay核心源码详解

    摘要:Goreplay 前称是 Gor,一个简单的 TCP/HTTP 流量录制及重放的工具,主要用 Go 语言编写. 本文分享自华为云社区<流量回放工具之 goreplay 核心源码分析> ...

  5. Lesson11——Pandas去重函数:drop_duplicates()

    pandas目录 "去重"通过字面意思不难理解,就是删除重复的数据.在一个数据集中,找出重复的数据删并将其删除,最终只保存一个唯一存在的数据项,这就是数据去重的整个过程.删除重复数 ...

  6. 实战 | 关于随手一点就发现XSS漏洞这件事

    起因 今天正在做一个项目时,需要找一个云接码平台接码去登录系统.当我随手从百度找了一个接码平台时,一个偶然的发现,有了今天这篇文章. 正文 当我进入这个接码平台随便找了个手机号,然后等了好几分钟都没啥 ...

  7. CPU、进程、线程原理

    巨人的肩膀 看完这篇还不懂高并发中的线程与线程池你来打我 (qq.com)

  8. DNS中的FQDN

    FQDN:(Fully Qualified Domain Name)全限定域名:同时带有主机名和域名的名称.(通过符号".") 例如:主机名是bigserver,域名是mycomp ...

  9. 使用burpsuite对APP数据包进行安全测试

    如之前的文章将手机抓包监听环境设置好后(之前学习burpsuite的时候写的,保存到草稿箱,忘记发了...),主要用到的功能如下: 1-1.数据包篡改 截获包后,可以对数据包中的内容在Raw标签框中直 ...

  10. 快速安装与配置kubernetes集群搭

    Kubernetes是什么? 首先,它是一个全新的基于容器技术的分布式架构领先方案.这个方案尽然很新,但它是谷歌十几年以来大规模应用容器技术的经验积累和升华的一个重要成果.确切地说,kubernete ...