在路上 ……

Linux系统运维与架构

以太坊多节点私链

一,安装以太坊客户端

系统版本:Ubuntu 16.04

添加Geth repository

apt install software-properties-common
add-apt-repository -y ppa:ethereum/ethereum

升级apt,安装Geth和Supervisor(将Geth作为服务运行)

apt update
apt -y install ethereum supervisor python-pip curl

升级pip & Supervisor

pip install pip --upgrade
pip install supervisor --upgrade
sed -i "s#usr/bin#usr/local/bin#g" /lib/systemd/system/supervisor.service

配置 Geth Supervisor Service – Copy and paste this into /etc/supervisor/conf.d/geth.conf

vi /etc/supervisor/conf.d/geth.conf
[program:geth]
command=bash -c '/usr/bin/geth'
autostart=true
autorestart=true
stderr_logfile=/var/log/supervisor/geth.err.log
stdout_logfile=/var/log/supervisor/geth.out.log

Start supervisor, which will auto-start Geth

systemctl enable supervisor
systemctl start supervisor

至此,以太坊公链就安装好了,运行geth就会自动开始同步区块

二,搭建私链

创建私链目录

mkdir /data/testchain

创建创始块json文件

vi genesis.json
{
    "config": {
        "chainId": 2018,
        "homesteadBlock": 0
    },
    "coinbase" : "0x0000000000000000000000000000000000000000",
    "difficulty" : "0x400",
    "gasLimit" : "0x2fefd8",
    "nonce" : "0x0000000000000142",
    "mixhash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
    "parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
    "timestamp" : "0x00",
    "alloc": {
    }
}

创建创始块

geth init  genesis.json --datadir /data/testchain/
WARN [02-06|17:46:00] No etherbase set and no accounts found as default 
INFO [02-06|17:46:00] Allocated cache and file handles         database=/root/testchain/geth/chaindata cache=16 handles=16
INFO [02-06|17:46:00] Writing custom genesis block 
INFO [02-06|17:46:00] Successfully wrote genesis state         database=chaindata                      hash=ac4e66…7f2921
INFO [02-06|17:46:00] Allocated cache and file handles         database=/root/testchain/geth/lightchaindata cache=16 handles=16
INFO [02-06|17:46:00] Writing custom genesis block 
INFO [02-06|17:46:00] Successfully wrote genesis state         database=lightchaindata

启动私链

geth  --datadir /data/testchain/ --networkid 2018 --rpc --rpcport "8845" --rpccorsdomain "*" --port "30333" --nodiscover

这样,第一个私链节点就已正常启动了,rpc端口和p2p端口都是可以自己随意定义的

第二个节点,前几步和之前一样,只是最后一步启动节点时的命令稍有变化

先查看第一个节点的nodeinfo,用于第二个节点启动
在第一个节点上连接到ipc console

geth attach /data/testchain/geth.ipc
Welcome to the Geth JavaScript console!

instance: Geth/Roadchain/v1.7.3-stable-4bb3c89d/linux-amd64/go1.9
coinbase: 0x81e71d34e8a9e4382c36fd90c3f234549106addd
at block: 6 (Tue, 06 Feb 2018 17:54:11 CST)
 datadir: /root/testchain
 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

> admin.nodeInfo
{
  enode: "enode://1f9cf6ef261966099b2d3498a2517a900318c141bea00edac71f1617dc6987852ce0239eea2d6490bd2af07409b2d623072ce3c1d3f3074dd914f31ba06a7c2f@[::]:30333?discport=0",
  id: "1f9cf6ef261966099b2d3498a2517a900318c141bea00edac71f1617dc6987852ce0239eea2d6490bd2af07409b2d623072ce3c1d3f3074dd914f31ba06a7c2f",
  ip: "::",
  listenAddr: "[::]:30333",
  name: "Geth/Roadchain/v1.7.3-stable-4bb3c89d/linux-amd64/go1.9",
  ports: {
    discovery: 0,
    listener: 30333
  },
  protocols: {
    eth: {
      difficulty: 788096,
      genesis: "0x3782eafbc5ab71618f9a6aaa3506a385c50c20d3682ade9ea817e9025cadf804",
      head: "0x74ae4f44d9326a000cd4920e2f9cf4d85ff1b7289c5b04af91a6cc1b8ba032df",
      network: 2018
    }
  }
}

enode的信息就是我们需要记录的enode://1f9cf6ef261966099b2d3498a2517a900318c141bea00edac71f1617dc6987852ce0239eea2d6490bd2af07409b2d623072ce3c1d3f3074dd914f31ba06a7c2f@[::]:30333

这里需要把@后面的'[::]'替换为服务器的IP,例如192.168.1.11

下面开始在第二节点上启动geth

geth  --datadir /data/testchain/ --networkid "2018" --rpc --rpcport "8845" --rpccorsdomain "*"  --port "30333"  --bootnodes "enode://1f9cf6ef261966099b2d3498a2517a900318c141bea00edac71f1617dc6987852ce0239eea2d6490bd2af07409b2d623072ce3c1d3f3074dd914f31ba06a7c2f@192.168.1.11:30333"

现在就可以在两个节点上geth console中分别执行admin.peers查看两个节点是否都看到了对方

三,附加操作

创建账户
在geth console中执行,12345678就是账户密码,请自行修改

> personal.newAccount("12345678")
"0x81e71d34e8a9e4382c36fd90c3f234549106addd"

解锁账户

personal.unlockAccount("0x81e71d34e8a9e4382c36fd90c3f234549106addd","12345678")

单机挖矿和停止挖矿

> miner.start()
null
> miner.stop()
true

查看账户余额

eth.getBalance("0x81e71d34e8a9e4382c36fd90c3f234549106addd")

通过创始块预分配账户余额
按照上面的步骤将第一节点启动后,创建一个账户,复制地址
编辑genesis.json,在alloc段,增加如下内容:

    "alloc": {
        "0x81e71d34e8a9e4382c36fd90c3f234549106addd": { "balance": "20000000000000000000" }
    }

这里的地址就是刚才创建的地址,后面的balance就是你想预分配的余额

然后将数据目录下的geth目录删掉,重新创建创始块(keystore目录不要动)

rm -rf /data/testchain/geth

然后再重新执行geth init创建创始块的命令,然后再启动geth,就可以在console中查看到账户被预分配的余额了


转载请注明出处:https://dp2u.com/2018/ethereum-multinode-private-chain.html

添加新评论 »

在这里输入你的评论...

Typecho 强力驱动