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

为了在本地网络运行多个以太坊节点的实例,必须确保一下几点:
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: <nil> DAOSupport: false EIP150: <nil> EIP155: 0 EIP158: 0 Metropolis: <nil> 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!

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: <LOOPBACK,UP,LOWER_UP> 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: <BROADCAST,MULTICAST,UP,LOWER_UP> 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: <nil> DAOSupport: false EIP150: <nil> EIP155: 0 EIP158: 0 Metropolis: <nil> 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
 然后他会自动同步区块链的数据

上面的命令中,–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

> 
> eth.getBalance(eth.accounts[0])
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: <nil> DAOSupport: false EIP150: <nil> EIP155: 0 EIP158: 0 Metropolis: <nil> 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
 然后他会自动同步区块链的数据

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

以下是使用geth创建基于联盟链的私有区块链,拥有自己的节点和genesis区块,以及创建10个合约账户的步骤: **1. 安装geth** 首先需要安装geth,可以从官网下载二进制文件或者通过包管理器进行安装。 **2. 创建genesis区块** 在创建私有区块链之前,需要先创建genesis区块,可以使用puppeth工具来完成。puppeth是一个命令行工具,可以帮助我们创建和管理私有区块链。 使用以下命令来启动puppeth: ``` puppeth ``` 根据提示进行以下设置: - 输入一个名字来创建一个新的网络 - 选择一个网络类型,选择2(Configure new genesis) - 选择网络协议,选择1(Ethash - proof-of-work) - 选择预分配的币数,输入0 - 输入一个新的地址作为挖矿奖励的收件人地址 - 输入一个新的账户地址,用于签署genesis区块 - 选择一个密码,用于加密私钥 - 选择一个目标平台,选择1(Ethereum) - 选择一个链ID,输入一个数字作为你的私有链ID - 选择是否要使用Clique共识算法,选择2(No) 完成以上设置后,puppeth会自动创建genesis.json文件,该文件包含了我们需要在geth使用的genesis区块数据。 **3. 初始化节点** 接下来需要初始化节点,使用以下命令: ``` geth --datadir /path/to/chain init /path/to/genesis.json ``` 其,/path/to/chain是你想要存储区块链数据的目录,/path/to/genesis.json是上一步生成的genesis.json文件的路径。 **4. 创建账户** 使用以下命令来创建账户: ``` geth --datadir /path/to/chain account new ``` 这将会提示你输入一个密码,该密码将会用于加密账户的私钥。创建完毕后,你将会得到一个账户地址。 重复以上步骤,创建10个账户。 **5. 启动节点** 使用以下命令来启动节点: ``` geth --datadir /path/to/chain --networkid [network_id] --nodiscover console ``` 其,/path/to/chain是你的区块链数据目录,[network_id]是你在puppeth设置的私有链ID。 启动后,会进入geth控制台,可以在控制台进行各种操作,例如挖矿、转账等。 **6. 解锁账户** 在控制台,使用以下命令来解锁账户: ``` personal.unlockAccount(address, password, duration) ``` 其,address是你的账户地址,password是你在创建账户时设置的密码,duration是解锁时间,单位为秒。 解锁账户后,可以使用以下命令来查询账户余额: ``` eth.getBalance(address) ``` 以上就是使用geth创建基于联盟链的私有区块链,拥有自己的节点和genesis区块,以及创建10个合约账户的步骤。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值