以太坊搭建私链集群

#安装Geth

参考:https://github.com/ethereum/go-ethereum/wiki/Building-Ethereum

#初始化脚本

geth.exe --identity "doteth" init ./genesis.json --datadir "./chain"

#启动脚本

geth.exe --identity "doteth" --networkid 2018  --nodiscover --datadir ./chain  --port 61911 --rpccorsdomain "*" --rpcapi net,eth,web3,personal --rpc  --rpcaddr 0.0.0.0  --rpcport 8101 console

#也可以不加console参数,启动后用attach命令附加

geth.exe attach ipc:\\\\.\\pipe\\geth.ipc

第二台:

#初始化脚本

geth.exe --identity "doteth" init ./genesis.json --datadir "./chain"

#启动脚本

geth.exe --identity "doteth" --networkid 2018  --nodiscover --datadir ./chain  --port 61911 --rpccorsdomain "*" --rpcapi net,eth,web3,personal --rpc  --rpcaddr 0.0.0.0  --rpcport 8101 console

#执行加入子网命令

admin.addPeer("enode://e42502d5e083a6f79fb461857c64bbaa9d3fc27ab1450ed1ec477e96b916046182b3799b1e53b0b50a0e89816a1e64022e93324aaf906eed4408e77b29ccedf5@192.168.6.36:61911?discport=0")

#然后在两个节点上测试:

net.peerCount  -会返回已连接的其他节点的个数

admin.peers    -返回其他节点的信息

#查看账户信息

eth.accounts

#创建两个账号,括号中是密码

personal.newAccount("123456") 

返回账号字符串:类似"0x945d861772fad6f16dbe2eed77a3afd3d750a69b"

personal.newAccount("123456")

#执行挖矿

miner.start()

注意点:

1. 挖矿挖到的ether币会默认保在第一个账户中,即eth.acccounts[0]中。

2. 挖矿是执行智能合约的基础。如果停止挖矿的话,不仅以太币会停止生成,所有智能合约的调用也会不起作用。

3. 如果真的要停止挖矿,可以执行命令miner.stop()来停止挖矿

4. 按上面的命令,应该是可以实现以太坊挖矿的。如果不行的话,有可能就是之前有存在的链,此时应该删除之前的数据。在Windows下即删除ethash文件夹和里面的文件即可。

#查询账号情况

acc0 = eth.accounts[0]

eth.getBalance(acc0)

#两个账户之间转移以太币

acc0 = eth.accounts[0] 

acc1 = eth.accounts[1] 

amount = web3.toWei(0.01) 

eth.sendTransaction({from: acc0, to: acc1, value: amount}) 

#如果提示账户被锁定

Error: authentication needed: password or unlock

    at web3.js:3143:20

    at web3.js:6347:15

    at web3.js:5081:36

    at :1:1

personal.unlockAccount(acc0) 并输入密码来解锁acc0才可。

需要注意的是,如果一个节点重启,则需要重新添加peer。另外,在实际生产中可去掉console命令,通过之前介绍的attach命令进入控制台。

你可能感兴趣的:(以太坊搭建私链集群)