在以太坊私有网络中建立节点集群,并发生交易

原文地址:http://www.cocoachina.com/cms/wap.php?action=article&id=22439

为了在本地网络运行多个以太坊节点的实例,必须确保一下几点:

  1. 每个实例都有独立的数据目录(–datadir) 

  2. 每个实例运行都有独立的端口.(eth和rpc两者都是)(–port 和 –rpcprot) 

  3. 在集群的情况下, 实例之间都必须要知道彼此. 

  4. 唯一的ipc通信端点,或者禁用ipc. 

启动第一个节点(指定端口,并禁用ipc),运行命令和结果如下:

root@i-ct5y7t0o:/home/ubuntu/private-geth#  geth --datadir ./data0 --networkid 31459 --ipcdisable --port 619 --rpcport 8200 console
INFO [06-28|14:52:52] Starting peer-to-peer node               instance=Geth/v1.6.6-stable-10a45cb5/linux-amd64/go1.8.1
INFO [06-28|14:52:52] Allocated cache and file handles         database=/home/ubuntu/private-geth/data0/geth/chaindata cache=128 handles=1024
INFO [06-28|14:52:52] Initialised chain configuration          config="{ChainID: 666 Homestead: 0 DAO:  DAOSupport: false EIP150:  EIP155: 0 EIP158: 0 Metropolis:  Engine: unknown}"
INFO [06-28|14:52:52] Disk storage enabled for ethash caches   dir=/home/ubuntu/private-geth/data0/geth/ethash count=3
INFO [06-28|14:52:52] Disk storage enabled for ethash DAGs     dir=/root/.ethash                               count=2
INFO [06-28|14:52:52] Initialising Ethereum protocol           versions="[63 62]" network=31459
INFO [06-28|14:52:52] Loaded most recent local header          number=546 hash=a511c3鈥40ac8 td=79201418
INFO [06-28|14:52:52] Loaded most recent local full block      number=546 hash=a511c3鈥40ac8 td=79201418
INFO [06-28|14:52:52] Loaded most recent local fast block      number=546 hash=a511c3鈥40ac8 td=79201418
WARN [06-28|14:52:52] Blockchain not empty, fast sync disabled 
INFO [06-28|14:52:52] Starting P2P networking 
INFO [06-28|14:52:54] UDP listener up                          self=enode://fc96d5fb078c82f33092c4944b31b3a8b9201ddec88b880ae245a131e1107b33f3d7833f5f6992d7c9ffdb557fbdd9ad2ab5b2ead2492f0bdb5690a0392c50f7@[::]:619
INFO [06-28|14:52:54] RLPx listener up                         self=enode://fc96d5fb078c82f33092c4944b31b3a8b9201ddec88b880ae245a131e1107b33f3d7833f5f6992d7c9ffdb557fbdd9ad2ab5b2ead2492f0bdb5690a0392c50f7@[::]:619
Welcome to the Geth JavaScript console!
6
instance: Geth/v1.6.6-stable-10a45cb5/linux-amd64/go1.8.1
coinbase: 0x66c62f1afa08eae5343bd2b3129e0ae9aa141fc3
at block: 546 (Wed, 28 Jun 2017 14:08:50 CST)
 datadir: /home/ubuntu/private-geth/data0
 modules: admin:1.0 debug:1.0 eth:1.0 miner:1.0 net:1.0 personal:1.0 rpc:1.0 txpool:1.0 web3:1.0

上面的命令以命令行的(console)的方式启动了节点, 所以我们可以通过继续输入下面的命令获取节点实例的enode url:

> admin.nodeInfo.enode
"enode://fc96d5fb078c82f33092c4944b31b3a8b9201ddec88b880ae245a131e1107b33f3d7833f5f6992d7c9ffdb557fbdd9ad2ab5b2ead2492f0bdb5690a0392c50f7@[::]:619"

获取本机IP地址

ubuntu@i-ct5y7t0o:~$ ip addr
1: lo:  mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0:  mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 52:54:0f:63:73:66 brd ff:ff:ff:ff:ff:ff
    inet 192.168.0.3/24 brd 192.168.0.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::5054:fff:fe63:7366/64 scope link 
       valid_lft forever preferred_lft forever

再打开一个终端,初始化第二个节点:

root@i-ct5y7t0o:/home/ubuntu/private-geth# geth --datadir ./data1 init ./genesis.json
WARN [06-28|14:55:45] No etherbase set and no accounts found as default 
INFO [06-28|14:55:45] Allocated cache and file handles         database=/home/ubuntu/private-geth/data1/geth/chaindata cache=16 handles=16
INFO [06-28|14:55:45] Writing custom genesis block 
INFO [06-28|14:55:45] Successfully wrote genesis state         database=chaindata                                      hash=e492a5鈥0420d
INFO [06-28|14:55:45] Allocated cache and file handles         database=/home/ubuntu/private-geth/data1/geth/lightchaindata cache=16 handles=16
INFO [06-28|14:55:45] Writing custom genesis block 
INFO [06-28|14:55:45] Successfully wrote genesis state         database=lightchaindata                                      hash=e492a5鈥0420d

启动节点, 加上console 表示启动后,启用命令行:

root@i-ct5y7t0o:/home/ubuntu/private-geth# geth --datadir ./data1 --networkid 31459 --ipcdisable --port 6191 --rpcport 8101 --bootnodes "enode://fc96d5fb078c82f33092c4944b31b3a8b9201ddec88b880ae245a131e1107b33f3d7833f5f6992d7c9ffdb557fbdd9ad2ab5b2ead2492f0bdb5690a0392c50f7@192.168.0.3:619" console
WARN [06-28|15:01:27] No etherbase set and no accounts found as default 
INFO [06-28|15:01:27] Starting peer-to-peer node               instance=Geth/v1.6.6-stable-10a45cb5/linux-amd64/go1.8.1
INFO [06-28|15:01:27] Allocated cache and file handles         database=/home/ubuntu/private-geth/data1/geth/chaindata cache=128 handles=1024
WARN [06-28|15:01:27] Upgrading chain database to use sequential keys 
INFO [06-28|15:01:27] Initialised chain configuration          config="{ChainID: 666 Homestead: 0 DAO:  DAOSupport: false EIP150:  EIP155: 0 EIP158: 0 Metropolis:  Engine: unknown}"
INFO [06-28|15:01:27] Disk storage enabled for ethash caches   dir=/home/ubuntu/private-geth/data1/geth/ethash count=3
INFO [06-28|15:01:27] Disk storage enabled for ethash DAGs     dir=/root/.ethash                               count=2
WARN [06-28|15:01:27] Upgrading db log bloom bins 
INFO [06-28|15:01:27] Bloom-bin upgrade completed              elapsed=198.75碌s
INFO [06-28|15:01:27] Initialising Ethereum protocol           versions="[63 62]" network=31459
INFO [06-28|15:01:27] Database conversion successful 
INFO [06-28|15:01:27] Loaded most recent local header          number=0 hash=e492a5鈥0420d td=1024
INFO [06-28|15:01:27] Loaded most recent local full block      number=0 hash=e492a5鈥0420d td=1024
INFO [06-28|15:01:27] Loaded most recent local fast block      number=0 hash=e492a5鈥0420d td=1024
INFO [06-28|15:01:27] Starting P2P networking 
INFO [06-28|15:01:29] UDP listener up                          self=enode://769c6de9b913f40edac8a037bbc697242097c4fa8f1574240bd2aeb930c049fd0d85310b9ba04bf6027c9d1a36a13789210140c9f2e38525deff87e28c8f5a09@[::]:6191
INFO [06-28|15:01:29] RLPx listener up                         self=enode://769c6de9b913f40edac8a037bbc697242097c4fa8f1574240bd2aeb930c049fd0d85310b9ba04bf6027c9d1a36a13789210140c9f2e38525deff87e28c8f5a09@[::]:6191
Welcome to the Geth JavaScript console!
6
instance: Geth/v1.6.6-stable-10a45cb5/linux-amd64/go1.8.1
 modules: admin:1.0 debug:1.0 eth:1.0 miner:1.0 net:1.0 personal:1.0 rpc:1.0 txpool:1.0 web3:1.0

 然后他会自动同步区块链的数据

上面的命令中,–bootndoes 是设置当前节点启动后,直接通过设置–bootndoes 的值来链接第一个节点, –bootnoedes 的值可以通过在第一个节的命令行中,输入:admin.nodeInfo.enode命令打印出来. 

也可以不设置 –bootnodes, 直接启动,启动后进入命令行, 通过命令admin.addPeer(enodeUrlOfFirst Instance)把它作为一个peer添加进来. 

为了确认链接成功,第二个节点输入:

> admin.nodeInfo
{
  enode: "enode://769c6de9b913f40edac8a037bbc697242097c4fa8f1574240bd2aeb930c049fd0d85310b9ba04bf6027c9d1a36a13789210140c9f2e38525deff87e28c8f5a09@[::]:6191",
  id: "769c6de9b913f40edac8a037bbc697242097c4fa8f1574240bd2aeb930c049fd0d85310b9ba04bf6027c9d1a36a13789210140c9f2e38525deff87e28c8f5a09",
  ip: "::",
  listenAddr: "[::]:6191",
  name: "Geth/v1.6.6-stable-10a45cb5/linux-amd64/go1.8.1",
  ports: {
    discovery: 6191,
    listener: 6191
  },
  protocols: {
    eth: {
      difficulty: 79750339,
      genesis: "0xe492a51ac029fd78d9c27dd1ff018b233231d15aa795d90aab1bd7d1ebc0420d",
      head: "0xdeeb2bc33146dccc9b5a3f02c56735d4fa4cb562fb92b3966053ede8880db8bf",
      network: 31459
    }
  }
}

第一个节点输入:

> admin.nodeInfo.enode
"enode://fc96d5fb078c82f33092c4944b31b3a8b9201ddec88b880ae245a131e1107b33f3d7833f5f6992d7c9ffdb557fbdd9ad2ab5b2ead2492f0bdb5690a0392c50f7@[::]:619"
> admin.nodeInfo
{
  enode: "enode://fc96d5fb078c82f33092c4944b31b3a8b9201ddec88b880ae245a131e1107b33f3d7833f5f6992d7c9ffdb557fbdd9ad2ab5b2ead2492f0bdb5690a0392c50f7@[::]:619",
  id: "fc96d5fb078c82f33092c4944b31b3a8b9201ddec88b880ae245a131e1107b33f3d7833f5f6992d7c9ffdb557fbdd9ad2ab5b2ead2492f0bdb5690a0392c50f7",
  ip: "::",
  listenAddr: "[::]:619",
  name: "Geth/v1.6.6-stable-10a45cb5/linux-amd64/go1.8.1",
  ports: {
    discovery: 619,
    listener: 619
  },
  protocols: {
    eth: {
      difficulty: 79750339,
      genesis: "0xe492a51ac029fd78d9c27dd1ff018b233231d15aa795d90aab1bd7d1ebc0420d",
      head: "0xdeeb2bc33146dccc9b5a3f02c56735d4fa4cb562fb92b3966053ede8880db8bf",
      network: 31459
    }
  }
}
> net.peerCount
1
> admin.peers
[{
    caps: ["eth/63"],
    id: "769c6de9b913f40edac8a037bbc697242097c4fa8f1574240bd2aeb930c049fd0d85310b9ba04bf6027c9d1a36a13789210140c9f2e38525deff87e28c8f5a09",
    name: "Geth/v1.6.6-stable-10a45cb5/linux-amd64/go1.8.1",
    network: {
      localAddress: "192.168.0.3:619",
      remoteAddress: "192.168.0.3:38620"
    },
    protocols: {
      eth: {
        difficulty: 1024,
        head: "0xe492a51ac029fd78d9c27dd1ff018b233231d15aa795d90aab1bd7d1ebc0420d",
        version: 63
      }
    }
}]

从得到的结果可以看出,第一个节点有1个peer链接, 链接的node id为: 

“769c6de9b913f40edac8a037bbc697242097c4fa8f1574240bd2aeb930c049fd0d85310b9ba04bf6027c9d1a36a13789210140c9f2e38525deff87e28c8f5a09” 

这个id,正好就是第二个节点的id. 

按照这样的方式继续扩展,可以非常容易就可以建立本地节点集群.这些工作都可以写成脚本代码来完成, 里面还可以包含创建账户,挖矿等.. 

请参考:https://github.com/ethersphere/eth-utils下的gethcluster.sh脚本,以及README中的使用方法和示例.

链接成功后,使用我们在上一篇文章中挖矿的账户,向第二个节点发送 “ether”(以太币的货币单位,还有一种叫”Wei”). 

首先查看第二个节点的Wei数量和整个网络的区块号,还有接收货币的账号id:

 personal.newAccount()
Passphrase: 
Repeat passphrase: 
"0xd600f5622024bb8f53ca3f0506cf1a13c811e17b"
> INFO [06-28|15:07:38] New wallet appeared                      url=keystore:///home/ubuntu/private鈥[32mstatus=Locked
5
> 
> eth.getBalance(eth.accounts[0])

在第一个节点命令行中,执行下面的操作:

 personal.unlockAccount(eth.accounts[0])
Unlock account 0x66c62f1afa08eae5343bd2b3129e0ae9aa141fc3
Passphrase: 
true
> ^C
> eth.sendTransaction({from: "0x66c62f1afa08eae5343bd2b3129e0ae9aa141fc3", to: "0xd600f5622024bb8f53ca3f0506cf1a13c811e17b", value: web3.toWei(1, "ether")})
> miner.start()

再在第二个节点的命令行输入:

eth.getBalance(eth.accounts[0])就可以查看转账成功

再打开一个终端,初始化第三个节点:

root@i-ct5y7t0o:/home/ubuntu/private-geth# geth --datadir ./data2 init ./genesis.json
WARN [06-28|14:55:45] No etherbase set and no accounts found as default 
INFO [06-28|14:55:45] Allocated cache and file handles         database=/home/ubuntu/private-geth/data1/geth/chaindata cache=16 handles=16
INFO [06-28|14:55:45] Writing custom genesis block 
INFO [06-28|14:55:45] Successfully wrote genesis state         database=chaindata                                      hash=e492a5鈥0420d
INFO [06-28|14:55:45] Allocated cache and file handles         database=/home/ubuntu/private-geth/data1/geth/lightchaindata cache=16 handles=16
INFO [06-28|14:55:45] Writing custom genesis block 
INFO [06-28|14:55:45] Successfully wrote genesis state         database=lightchaindata                                      hash=e492a5鈥0420d

启动节点, 加上console 表示启动后,启用命令行:

root@i-ct5y7t0o:/home/ubuntu/private-geth# geth --datadir ./data2 --networkid 31459 --ipcdisable --port 6192 --rpcport 8100 --bootnodes "enode://fc96d5fb078c82f33092c4944b31b3a8b9201ddec88b880ae245a131e1107b33f3d7833f5f6992d7c9ffdb557fbdd9ad2ab5b2ead2492f0bdb5690a0392c50f7@192.168.0.3:619" console
WARN [06-28|15:01:27] No etherbase set and no accounts found as default 
INFO [06-28|15:01:27] Starting peer-to-peer node               instance=Geth/v1.6.6-stable-10a45cb5/linux-amd64/go1.8.1
INFO [06-28|15:01:27] Allocated cache and file handles         database=/home/ubuntu/private-geth/data1/geth/chaindata cache=128 handles=1024
WARN [06-28|15:01:27] Upgrading chain database to use sequential keys 
INFO [06-28|15:01:27] Initialised chain configuration          config="{ChainID: 666 Homestead: 0 DAO:  DAOSupport: false EIP150:  EIP155: 0 EIP158: 0 Metropolis:  Engine: unknown}"
INFO [06-28|15:01:27] Disk storage enabled for ethash caches   dir=/home/ubuntu/private-geth/data1/geth/ethash count=3
INFO [06-28|15:01:27] Disk storage enabled for ethash DAGs     dir=/root/.ethash                               count=2
WARN [06-28|15:01:27] Upgrading db log bloom bins 
INFO [06-28|15:01:27] Bloom-bin upgrade completed              elapsed=198.75碌s
INFO [06-28|15:01:27] Initialising Ethereum protocol           versions="[63 62]" network=31459
INFO [06-28|15:01:27] Database conversion successful 
INFO [06-28|15:01:27] Loaded most recent local header          number=0 hash=e492a5鈥0420d td=1024
INFO [06-28|15:01:27] Loaded most recent local full block      number=0 hash=e492a5鈥0420d td=1024
INFO [06-28|15:01:27] Loaded most recent local fast block      number=0 hash=e492a5鈥0420d td=1024
INFO [06-28|15:01:27] Starting P2P networking 
INFO [06-28|15:01:29] UDP listener up                          self=enode://769c6de9b913f40edac8a037bbc697242097c4fa8f1574240bd2aeb930c049fd0d85310b9ba04bf6027c9d1a36a13789210140c9f2e38525deff87e28c8f5a09@[::]:6191
INFO [06-28|15:01:29] RLPx listener up                         self=enode://769c6de9b913f40edac8a037bbc697242097c4fa8f1574240bd2aeb930c049fd0d85310b9ba04bf6027c9d1a36a13789210140c9f2e38525deff87e28c8f5a09@[::]:6191
Welcome to the Geth JavaScript console!

instance: Geth/v1.6.6-stable-10a45cb5/linux-amd64/go1.8.1
 modules: admin:1.0 debug:1.0 eth:1.0 miner:1.0 net:1.0 personal:1.0 rpc:1.0 txpool:1.0 web3:1.0

 然后他会自动同步区块链的数据

这样这条链就有三个节点了


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值