区块链--Ubuntu上搭建以太坊私有链

2023-01-04,,,,

1搭建私链所需环境

操作系统:ubuntu16.04,开虚拟机的话要至少4G,否则会影响测试挖矿时的速度

软件:

geth客户端

Mist和Ethereum Wallet:https://github.com/ethereum/mist/releases/

2、安装geth客户端

sudo apt-get update
sudo apt-get install software-properties-common
sudo add-apt-repository -y ppa:ethereum/ethereum
sudo add-apt-repository -y ppa:ethereum/ethereum-dev
sudo apt-get update
sudo apt-get install ethereum

安装完成后,输入geth -h,如果有列出信息则说明安装成功,可查看列出信息,方便后续的操作

3、创建创始块

(1)创建以太坊初始区块文件 genesis.json

作为区块链,链子总要有个头,所以需要创建一个创世块作为头部,才好往下添加,创世文件名称命名为genesis.json,数据目录存放在/home/cll/privateDemo/data1,genesis.json存放在/home/cll/ privateDemo/:

cat > genesis.json

输入:

{
"config": {
"chainId": 411,
"homesteadBlock": 0,
"eip155Block": 0,
"eip158Block": 0
},
"nonce": "0x0000000000000033",
"timestamp": "0x0",
"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"gasLimit": "0x8000000",
"difficulty": "0x100",
"mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"coinbase":"0x0000000000000000000000000000000000000000",
"alloc": {
"0x1C83C95473e1e93A2C8560c73976dAFA9C3f0a79":
{"balance":""}
}
}

查看:

cat genesis.json

部分字段解释:

chainId : 以太坊区块链网络Id,ethereum主链是1,私有链只用不要与主链冲突即可。

alloc : 预留账户,如下

coinbase: 旷工账户

difficulty: 挖矿难度

extraData:相当于备注

gasLimit:最小消耗gas

nonce : 64位随机数,用于挖矿,注意他和mixhash的设置需要满足以太坊黄皮书中的要求

parentHash : 上一个区块的Hash值,因为是创世块,没有在它前面的,所以是0

timestamp : 时间戳

(2)初始化区块节点

geth --datadir data1 init genesis.json

(3)启动geth客户端节点

geth --identity "data1" --rpc --rpcport 8000 --rpccorsdomain "*" --datadir "data1" --port 30303 --rpcapi "db,eth,net,web3" --networkid 999 console 2>>data1/geth.log

geth常用属性:

--Identity : 节点身份标识,起个名字

--datadir : 指定节点存在位置,“data1”

--rpc : 启用http-rpc服务器

--rpcapi : 基于http-rpc提供的api接口。eth,net,web3,db...

--rpcaddr : http-rpc服务器接口地址:默认“127.0.0.1”

--rpcport : http-rpc 端口(多节点时,不要重复)

--port : 节点端口号(多节点时,不要重复)

--networkid : 网络标识符 随便指定一个id(确保多节点是统一网络,保持一致)

(4)geth常用命令

#创建账户
personal.newAccount("") #获取账户数组
eth.accounts #解锁账户,转账时可使用
personal.unlockAccount(eth.accounts[0], "") #节点主账户
eth.coinbase #查看账户余额
eth.getBalance(eth.accounts[0]) #启动,结束挖矿,写区块
miner.start(), miner.stop() #查看区块数量
eth.blockNumber

4、配置多节点服务

(1)再初始化一个节点(与上一个节点使用同一个genesis.json)

(privateDemo目录)

geth --datadir data2 init genesis.json

(2)启动新节点

(privateDemo目录)

geth --identity "data2" --rpc --rpcport 8001 --rpccorsdomain "*" --datadir "data2" --port 30306 --rpcapi "db,eth,net,web3" --networkid 999 console 2>>data2/geth.log

注:如果在同一台电脑上这两个端口号与之前的节点不能相同

(3)查看:新节点enode信息

admin.nodeInfo.enode

(4)主节点添加新节点,使两节点连在同一个私有链上

在data1终端下输入新节点data2的enode信息

例如:

admin.addpeer("enode://d4f64272de882d2e2ccefc6466c6580ddecd253f5c9d87f977ac3881cbea7b141c07681ea605c53af5815cbfc25b5138b9ddb07be61b757850a55b7197939ba4@127.0.0.1:30306")

(5)检查两节点是否已连接

首先使用命令admin.nodeInfo,查看结果是否为空[],或者net.peerCount,查看是否为0(这里有点问题,有时候由于某些原因,可能网络不稳定,添加完后是[]和0,有时候又显示有同伴);另外在其中一个节点启动挖矿命令 miner.start(),看另一个节点是否也同步,可以使用命令行跟踪(data1目录):tail -f geth.log。

4、Mist连接私链

(1)以rpc方式打开访问ipc接口方式打开mist钱包

“(mist应用所在路径)” –rpc “(节点geth.ipc所在目录)”

例如:

“/home/cll/ethereum/mist/mist” –rpc “/home/cll/privateDemo/data1/geth.ipc”

(2)只读的http方式打开钱包。不能转账,不能部署合约

“/home/cll/ethereum/mist/mist” –rpc http://127.0.0.1:8000

5、在Mist上部署智能合约

点击CONTRACTS->点击DEPLOY NEW CONTRACT->选择账户,编写合约内容->最底下点击DEPLOY。成功提交后,等待挖矿确认,确认的合约会显示在CONTRACTS页面,点击合约即可进行相关操作。

区块链--Ubuntu上搭建以太坊私有链的相关教程结束。

《区块链--Ubuntu上搭建以太坊私有链.doc》

下载本文的Word格式文档,以方便收藏与打印。