创建Dapp的第一步,搭建私链。

一、环境准备

go、cmake以及geth资源下载,提取码:qfe2

1.1 安装go

  1. 下载源码,上传至opt/module,解压。以下所有的文件解压后的文件夹,全部修改文件夹名为:包名+版本号

    1
    tar -xvf go1.14.6.linux-amd64.tar.gz
  2. 设置环境变量,vim /etc/profile

    1
    2
    3
    4
    export GOROOT=/opt/module/go1.14.6
    #GOPATH需要自己新建一个文件夹
    export GOPATH=/root/Code/go
    export PATH=$PATH:/opt/module/go1.14.6/bin
  3. 执行source /etc/profile更新环境变量配置。

  4. 验证安装:

    1
    go version

1.2 安装cmake

  1. 下载源码,上传至opt/module,解压

    1
    tar -xvf cmake-3.15.2.tar.gz
  2. 设置环境变量,vim /etc/profile

    1
    export PATH=$PATH:/opt/module/cmake3.15.2/bin
  3. 执行source /etc/profile更新环境变量配置。

  4. 安装gcc程序包

    1
    yum install -y gcc gcc-c++ make automake
  5. 进入cmke目录执行以下下命令,完成编译。这几步时间有些长,耐心等待。

    1
    2
    3
    4
    cd cmake3.15.2
    ./bootstrap
    gmake
    gmake install
  6. 验证安装

    1
    cmake --version

二、安装geth

  1. 下载源码,上传至opt/module,解压。

    1
    tar zxvf go-ethereum-1.9.19.tar.gz
  2. 设置环境变量,vim /etc/profile

    1
    export PATH=$PATH:/opt/module/go-ethereum1.9.19/build/bin
  3. 执行source /etc/profile更新环境变量配置。

  4. 执行以下命令来编译源码

    1
    2
    3
    go env -w GOPROXY=https://goproxy.cn
    cd go-ethereum1.9.19
    make all
  5. 验证安装

    1
    geth version

三、geth操作

1、启动节点同步,(搭建私链不需要)

1
2
3
4
geth --datadir . --syncmode fast
//一快速模式同步区块,只会下载每个区块同和区块体,不会执行验证所有的交易。

geth --testnet --datadit . --syncmode fast

2、开发者模式

开发者模式:是为方便以太坊开发者在以太坊平台上开发测试所建立的一种模式。开发者模式相比自己搭建私链有很多好处,比如不需要链码,会预留一个拥有巨额ETH的账户,账户之间转账不需要先解锁,采用POA证明方式,自动进行挖矿且有交易才挖矿(不会一直挖矿消耗资源)。总体是类似与ganache的模拟链。

开发者模式的开启方法:

1
2
3
mkdir devChain
cd devChain
geth --datadir . --dev --nodiscover --networkid 1234 --rpc --rpccorsdomain "*" --rpcaddr 192.168.2.151 --rpcport 8545 console --allow-insecure-unlock 2>output.log

参数说明:

  • –datadir:以太坊私链的文件目录。
  • –dev:开启开发者模式。
  • –nodiscover:不发现同一个networkid其他节点。
  • –networkid:以太坊的网路id,可以自己任意取值。
  • –rpc:开启rpc接口,Dapp就是通过rpc方式来与以太坊进行交互。
  • –rpcaddr:rpc的ip,默认是localhost,这里填写的是CentOS本机的IP。如在本地运行虚拟机,网络IP最好不要用DHCP模式获取,固定IP的设置方法。如是远程服务器,填服务器的ip即可。
  • –rpcport:rpc端口,需要将防火墙中的8545端口开放,开放方法:防火墙设置
  • console:开启console,能够和geth交互。
  • –allow-insecure-unlock:没有这个参数,无法解锁账户。
  • 2>output.log:将输出重定向到文件中。

output.log中会包括私链开启以及挖矿的详细信息,执行以下命令可以查看私链运行状态:

1
tail -f output.log

3、搭建私链

新建mychain文件夹,创建genesis.json

chainId:以太坊的链的id,可以自己随便取值,但是需要注意搭建私链时,这个值不能与以太坊主网或者是测试网的chainId相同以太坊chainId查询

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
{
"config": {
"chainId": 666,
"homesteadBlock": 0,
"eip150Block": 0,
"eip150Hash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"eip155Block": 0,
"eip158Block": 0,
"byzantiumBlock": 0,
"constantinopleBlock": 0,
"petersburgBlock": 0,
"istanbulBlock": 0,
"ethash": {}
},
"nonce": "0x0",
"timestamp": "0x5ddf8f3e",
"extraData": "0x0000000000000000000000000000000000000000000000000000000000000000",
"gasLimit": "0x47b760",
"difficulty": "0x00002",
"mixHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"coinbase": "0x0000000000000000000000000000000000000000",
"alloc": { }
},
"number": "0x0",
"gasUsed": "0x0",
"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000"
}

在mychain文件夹下,初始化链json

1
geth --datadir . init genesis.json

启动私链,networkidrpcaddr的设置方法同开发者模式。查看输出日志output.log同上。

1
geth --datadir . --nodiscover --networkid 1234 --rpc --rpccorsdomain "*" --rpcaddr 192.168.2.151 --rpcport 8545 console --allow-insecure-unlock 2>output.log

注意:搭建私链后一定要进行以下测试,看是否能够正常发起交易。

4、私链console

验证交易的步骤是:

  1. 创建两个账户
  2. 执行挖矿
  3. 执行转账
  4. 查看recipient余额

查看块数量

1
eth.blockNumber

创建账户:

1
personal.newAccount("password")

执行挖矿:(这一步会生成DAG,花费时间会很长,需要耐心等待到100%)

如果出块速度很慢,可以设置链码的difficulty参数,或者提高虚拟机的内存和核心数量,然后执行miner.start(2)来执行多线程挖矿。

1
miner.start(1)

获取账户:

1
2
my=eth.accounts[0]
other=eth.accounts[1]

解锁账户(发起交易前需要对sender解锁):

1
2
personal.unlockAccount(my)
//随后输入密码:1234

查看余额:

1
eth.getBalance(other)

发起转账:

1
eth.sendTransaction({from:my, to:other, value:10000})

如果没有挖矿

1
2
txpool.status//查看本地交易池中有没有待确认的交易
eth.getBlock("pending",true).transactions//查看当前待确认的交易

查看交易信息:

1
eth.getTransaction("交易哈希")

通过区块号查看区块信息:

1
eth.getBlock(0)