之前用的是CENTOS7,后边发现有好多搞不定的小坑,果断换用ubuntu继续学习。
环境:ubuntu16.04
安装git
之后的安装都需要依赖Git
sudo add-apt-repository ppa:git-core/ppa
sudo apt-get update
sudo apt-get install git
查看版本号
git--version
git version 2.18.0
1.安装go语言环境
(1)下载linux下go的安装包
$ wget https://studygolang.com/dl/golang/go1.10.3.linux-amd64.tar.gz
(2)下载完后,进入到下载目录,将其解压到 /usr/local 文件夹下并在$HOME下新建一个文件夹go:
$ sudo tar -C /usr/local -xzf go1.10.3.linux-amd64.tar.gz
$ mkdir go
(3)配置环境变量
使用如下命令打开环境变量配置文件
$ sudo vim ~/.bashrc
将下列代码复制粘贴到文件最后,wq命令保存退出
export GOROOT=/usr/local/go
export GOBIN=$GOROOT/bin
export GOPATH=$HOME/go
export PATH=$PATH:$GOBIN
输入如下命令使环境变量生效(注意:不用加sudo)
$ source ~/.bashrc
至此,go语言环境已经安装完成,输入go version查看安装是否成功
$ go version
go version go1.10.3 linux/amd64
使用如下命令进行安装
$ sudo apt-get install software-properties-common
$ sudo add-apt-repository -y ppa:ethereum/ethereum
$ sudo apt-get update
$ sudo apt-get install ethereum
安装完后,输入geth version查看安装是否成功
$ geth version
Geth Version: 1.8.11-stable
(1)新建一个文件夹,例如mychain,用来保存私有链的数据。创建一个初始化的配置文件,genesis.json
$ mkdir mychain
$ vim genesis.json
然后将如下的配置信息写入到genesis.json文件中(注意:chainId不能为0)
{
"config": {
"chainId": 3,
"homesteadBlock": 0,
"eip155Block": 0,
"eip158Block": 0
},
"alloc" : {},
"coinbase" : "0x0000000000000000000000000000000000000000",
"difficulty" : "0x200",
"extraData" : "",
"gasLimit" : "0x2fefd8",
"nonce" : "0x0000000000000042",
"mixhash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
"parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
"timestamp" : "0x00"
}
(2)初始化创世区块,并指定数据存放路径
$ geth --datadir Mychain init genesis.json
(3)启动私有链节点,并将日志输入到mychain.log
$ geth --networkid 3 --datadir mychain --identity "mychain" --rpc --rpcaddr 0.0.0.0 --rpcport 8545 --rpcapi "admin,debug,eth,miner,net,personal,shh,txpool,web3" --port 30303 --rpccorsdomain "*" --nodiscover console 2>>mychain.log
具体参数含义请参考这个博客:geth命令
启动成功后进入控制台界面,如下所示:
Welcome to the Geth JavaScript console!
instance: Geth/mychain/v1.8.11-stable-dea1ce05/linux-amd64/go1.10
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
>
至此,ubuntu 16.04 下以太坊私有链环境已经搭好,接下在进行简单测试。
(1)创建新账户
personal.newAccount("密码")
创建成功将返回账户的地址
> personal.newAccount("123")
"0xe6319357003ff9889b7a849f5ec66b2903d08289"
> user1=eth.accounts[0]
"0x3e46c05151dcce7c994aaeb6f7366fc39ddfc694"
>user2=eth.accounts[1]
"0x611b1c6a1a859da53ed6154aeeec797ea03b9289"
(2)查询账户余额
>eth.getBalance("账户地址")
0
>eth.getBalance(user1) //两种查询方式都可以
0
由于新建账户,没有余额,所以查询的结果为 0
> eth.getBalance("0xe6319357003ff9889b7a849f5ec66b2903d08289")
0
(3)开启/停止挖矿,第一次开起需要等待一段时间。
miner.start()
第一次要等一阵时间,大约5分钟左右
开始都如第一行所示,出现加粗则是挖矿成功了
INFO [07-26|18:15:21.550] Generating DAG in progress epoch=1 percentage=16 elapsed=3m25.200s
INFO [07-26|18:15:33.338] Generating DAG in progress epoch=1 percentage=17 elapsed=3m36.988s
INFO [07-26|18:15:45.307] Generating DAG in progress epoch=1 percentage=18 elapsed=3m48.956s
INFO [07-26|18:15:57.158] Generating DAG in progress epoch=1 percentage=19 elapsed=4m0.808s
INFO [07-26|18:15:59.828] Successfully sealed new block number=30 hash=2b5e96…217cae
INFO [07-26|18:15:59.828] block reached canonical chain number=25 hash=37bbe5…bcba21
INFO [07-26|18:15:59.829] mined potential block number=30 hash=2b5e96…217cae
INFO [07-26|18:16:20.992] Generating DAG in progress epoch=1 percentage=21 elapsed=4m24.642s
INFO [07-26|18:16:28.468] Successfully sealed new block number=34 hash=bac86c…464986
INFO [07-26|18:16:28.468] block reached canonical chain number=29 hash=d874de…8cfc4c
INFO [07-26|18:16:28.469] mined potential block number=34 hash=bac86c…464986
INFO [07-26|18:16:28.489] Commit new mining work number=35 txs=0 uncles=0 elapsed=176.283µs
INFO [07-26|18:16:28.541] Successfully sealed new block number=35 hash=421888…c52d31
INFO [07-26|18:16:28.541] block reached canonical chain number=30 hash=2b5e96…217cae
INFO [07-26|18:16:28.542] mined potential block number=35 hash=421888…c52d31
INFO [07-26|18:16:28.543] Commit new mining work number=36 txs=0 uncles=0 elapsed=500.117µs
INFO [07-26|18:16:33.099] Generating DAG in progress epoch=1 percentage=22 elapsed=4m36.749s
INFO [07-26|18:16:34.837] Successfully sealed new block number=36 hash=f9ec16…7feafa
INFO [07-26|18:16:34.838] block reached canonical chain number=31 hash=0ef145…b841bb
INFO [07-26|18:16:34.838] mined potential block number=36 hash=f9ec16…7feafa
INFO [07-26|18:16:34.840] Commit new mining work number=37 txs=0 uncles=0 elapsed=498.032µs
INFO [07-26|18:16:40.194] Successfully sealed new block number=37 hash=549c29…93f046
miner.stop()
查看挖矿日志,可以新开启一个终端,输入下面命令进行查看
$ tail -f mychain.log
(4)转账
挖矿成功后,默认会将以太币给第一个账户,查看余额
>eth.getBalance(eth.accounts[0])
135000000000000000000
eth.accounts:以数组形式返回为账户列表,当前只有一个账户,可用eth.accounts[0]表示
新建一个账户,查看余额
>personal.newAccount("111")
"0x866e46b71923d54e6fee26f42ececc216ca76c1d"
>eth.getBalance(eth.accounts[1])
0
从accounts[0]向accounts[1]发送1个以太币,首先需要解锁accounts[0],使用如下命令
>personal.unlockAccount(eth.accounts[0],"123",0)
true
三个参数分别为accounts[0]的地址,创建时的密码及解锁时间(毫秒为单位)。0 代表长时间解锁
转账操作
>eth.sendTransaction({from:eth.accounts[0],to:eth.accounts[1],value:web3.toWei(1,'ether')})
开启挖矿等待区块验证,转账成功后,查询accounts[1]余额,默认单位是Wei
>eth.getBalance(eth.accounts[1])
100000000000000000
可以使用web3.fromWei()命令将wei转为ether。
转自:https://blog.csdn.net/qq_27317475/article/details/80882034
有疑问加站长微信联系(非本文作者)