美文网首页
Ethereum-2.Multiple nodes on a c

Ethereum-2.Multiple nodes on a c

作者: Kylin824 | 来源:发表于2019-07-12 12:22 被阅读0次

目标

以太坊本地私有网络搭建创建多个节点并进行挖矿与交易

操作步骤

  • 创建节点1

    参考Ethereum-1.Create a private chain

    一些不同地方:

    1. 初始化创世区块时为节点1建立文件夹data1:

      geth --datadir "data1" init genesis.json
      
    2. 启动私有链时,增加参数设置:

      geth --identity "Node1"  --datadir "data1" --allow-insecure-unlock --rpc --rpcapi "db,eth,net,web3,personal" --ipcpath "data1\geth\geth.ipc" --networkid "1108"  console 2>> node1.log
      
    3. 创建新账户

      personal.newAccount()
      Passphrase:
      Repeat passphrase:
      "0xa8679e9a0b7d4ba098ff9e2cf713f5dcdb383b75"
      
    4. 开始挖矿

      miner.start(5)
      
  • 创建节点2

  1. 新打开一个终端,同样初始化创世区块于文件夹data2中

    geth --datadir "data2" init genesis.json
    
  1. 启动私有链时,增加如下参数设置:

    geth --identity "Node2"  --datadir "data2" --allow-insecure-unlock --rpc --rpcapi "db,eth,net,web3,personal"  --rpcport "8487" --port "30305" --ipcpath "data2\geth\geth.ipc" --networkid "1108"  console 2>> node2.log
    

使用命令 geth -h 可以查看geth 相关的帮助文档,这里讲解几个常用的属性。

--identity : 节点身份标识,起个名字   
--datadir : 指定databases and keystore存储位置
--rpc : 启用http-rpc服务器
--rpcapi : 基于http-rpc提供的api接口  
--rpcaddr : http-rpc服务器接口地址:默认“localhost”  
--rpcport : http-rpc 端口(多节点时,不要重复,默认“8545”)  
--port : 网络监听端口号(多节点时,不要重复,默认"30303")  
--networkid : 网络标识符 (1=Frontier, 2=Morden (disused), 3=Ropsten, 4=Rinkeby) (默认: 1) 多节点需指定同个id
  1. 创建新账户

    personal.newAccount()
    Passphrase:
    Repeat passphrase:
    "0x8a8a02a1247579f91b7a8bc36a03e8fa2d55ef0c"
    
  2. 查看区块数

    > eth.blockNumber
    0
    
  • 节点1连接节点2

  1. 首先要知道节点2的 enode 信息:
     > admin.nodeInfo.enode

注意:此处不能使用显示的IP,需要用localhost(127.0.0.1)才行

  1. 节点1通过 admin.addPeer() 方法连接到节点2

    >admin.addPeer("enode://7a06d987939e66cb65f90d5eb58b4ee3640ff87dab7c62dc2f38a678a6f6d25ecef947a4c75364306a504389305caabcb5a3fd4f2facc36bd7dfeef85f067ed5@127.0.0.1:30305")
    true
    
  1. 返回true则连接成功,查看节点2日志:

可以看到节点2开始同步节点1的区块,此时查看节点2区块数:

    > eth.blockNumber
    847

此时两个节点同步成功,可以发起交易、创建智能合约等操作。

tips:

  • 两节点要加入同一条私链,这两个节点初始化的创世区块必须要一模一样。
  • 两个节点连接不通时,尝试关闭网络防火墙
  • 进行transaction时,私链网络中至少要有一个节点在挖矿。

相关文章

网友评论

      本文标题:Ethereum-2.Multiple nodes on a c

      本文链接:https://www.haomeiwen.com/subject/tyjekctx.html