转载请注明链接
ubuntu系统安装geth,并搭建私有链进行挖矿测试。
环境:ubuntu 14.04 64bit
由于网络代理或是防火墙的原因,无法使用网上大多数人提供的apt-get方式安装,只能手动下载安装包进行安装。
geth的安装包需要go语言环境进行编译,因此先安装go语言环境
https://www.golangtc.com/download
下载go1.9.2.linux-amd64.tar.gz,由于geth默认的go命令位置在/usr/local/go/bin,所以将go语言包解压至/usr/local/下
https://github.com/ethereum/go-ethereum/releases
下载tar.gz包,同样放在/usr/local/go-ethereum/下,执行安装:
$ make geth
export PATH=$PATH:/usr/local/go-ethereum/build/bin/:/usr/local/go/bin。
执行环境生效:
$ source ~/.bashrc
安装geth主要用以开发,共有链挖矿此处不做研讨。
需要配置genesis.json用以产生创世区块,具体内容如下
{
"config": { "chainId": 22, "homesteadBlock": 0, "eip155Block": 0, "eip158Block": 0 },
"alloc" : {},
"coinbase" : "0x0000000000000000000000000000000000000000",
"difficulty" : "0x400",
"extraData" : "",
"gasLimit" : "0x2fefd8",
"nonce" : "0x0000000000000038",
"mixhash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
"parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
"timestamp" : "0x00" }
配置文件各项说明如下:
mixhash
与nonce配合用于挖矿,由上一个区块的一部分生成的hash。注意他和nonce的设置需要满足以太坊的Yellow paper, 4.3.4. Block Header Validity, (44)章节所描述的条件。.
nonce
nonce就是一个64位随机数,用于挖矿,注意他和mixhash的设置需要满足以太坊的Yellow paper, 4.3.4. Block Header Validity, (44)章节所描述的条件。
difficulty
设置当前区块的难度,如果难度过大,cpu挖矿就很难,这里设置较小难度。
alloc
用来预置账号以及账号的以太币数量,因为私有链挖矿比较容易,所以我们不需要预置有币的账号,需要的时候自己创建即可以。预置方式如下:
“alloc”: {
“7df9a875a174b3bc565e6424a0050ebc1b2d1d82”: { “balance”: “300000” },
“f41c74c9ae680c1aa78f42e5647a62f353b7bdde”: { “balance”: “400000” }
}
coinbase
矿工的账号,随便填,下面会执行eth指令创建账户。
timestamp
设置创世块的时间戳。
parentHash
上一个区块的hash值,因为是创世块,所以这个值是0。
extraData
附加信息,随便填,可以填你的个性信息,网上有实例给此项赋值0x0,这会init时报错:invalid genesis file: hex string has odd length,必须为0x00。
gasLimit
该值设置对GAS的消耗总量限制,用来限制区块能包含的交易信息总和,因为我们是私有链,所以填最大。
该文件对于下面的init执行时发生的常见错误如下:
一个比较简单的指令如下,仅仅指定了data目录及init json。
$ geth --datadir "/home/username/data0" init genesis.json
或是下面这条比较复杂的指令:
$ geth --identity "MyNodeName" --rpc --rpcport "8080" --rpccorsdomain "*" --datadir "/home/username/data0" --port "30303" --nodiscover --rpcapi "db,eth,net,web3" --networkid 1999 init genesis.json
上述私有化命令的参数详细说明如下:
创世区块初始化成功后,会在数据目录data0中生成geth和keystore两个文件夹,此时目录结构如下:
privatechain
├── data0
│ ├── geth
│ │ └── chaindata
│ │ ├── 000001.log
│ │ ├── CURRENT
│ │ ├── LOCK
│ │ ├── LOG
│ │ └── MANIFEST-000000
│ └── keystore
└── genesis.json
其中geth/chaindata中存放的是区块数据,keystore中存放的是账户数据。
$ geth --datadir data0 --networkid 1999 console
–datadir选项指定使用data0作为数据目录,
–networkid选项为私有链的网络id,如前所述为1999。网络id在连接到其他节点的时候会用到,以太坊公网的网络id是1,不能与其冲突。
启用私有链后会进入以太坊Javascript Console后,就可以使用里面的内置对象做一些操作,这些内置对象提供的功能很丰富,比如查看区块和交易、创建账户、挖矿、发送交易、部署智能合约等。
Welcome to the Geth JavaScript console!
instance: Geth/v1.8.12-stable/linux-amd64/go1.9.2
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
>
console下的命令tab键可以自动补全。
创建方式1:
> personal.newAccount("123456")
"0x5ee8bdf8543e00f3cccea9651d2f1e372591cba3"
创建方式2:
> personal.newAccount()
Passphrase:
Repeat passphrase:
"0xeabdde05da845490d24d7492275fd256b28c05b9"
查看当前的账户列表:
> eth.accounts
["0x5ee8bdf8543e00f3cccea9651d2f1e372591cba3", "0xeabdde05da845490d24d7492275fd256b28c05b9"]
账户默认会保存在数据目录的keystore文件夹中。查看目录结构,发现data0/keystore中多了两个文件,这两个文件就对应刚才创建的两个账户,这是json格式的文本文件,可以打开查看,里面存的是私钥经过密码加密后的信息。此时tree如下:
├── data0
│ ├── geth
│ │ ├── chaindata
│ │ │ ├── 000002.ldb
│ │ │ ├── 000003.log
│ │ │ ├── CURRENT
│ │ │ ├── LOCK │ │ │ ├── LOG │ │ │ └── MANIFEST-000004 │ │ ├── lightchaindata │ │ │ ├── 000001.log │ │ │ ├── CURRENT │ │ │ ├── LOCK │ │ │ ├── LOG │ │ │ └── MANIFEST-000000 │ │ ├── LOCK │ │ ├── nodekey │ │ ├── nodes │ │ │ ├── 000001.log │ │ │ ├── CURRENT │ │ │ ├── LOCK │ │ │ ├── LOG │ │ │ └── MANIFEST-000000 │ │ └── transactions.rlp │ ├── history │ └── keystore │ ├── UTC--2018-07-24T07-44-30.884353877Z--5ee8bdf8543e00f3cccea9651d2f1e372591cba3 │ └── UTC--2018-07-24T07-44-43.711728811Z--eabdde05da845490d24d7492275fd256b28c05b9
启动挖矿:
> miner.start(2)
其中start的参数表示挖矿使用的线程数。第一次启动挖矿会先生成挖矿所需的DAG文件,等进度达到100%后,就会开始挖矿。多线程可能并行执行。
停止挖矿:
> miner.stop()
挖到一个区块会奖励5个以太币,挖矿所得的奖励会进入矿工的账户,这个账户叫做coinbase,默认情况下coinbase是本地账户中的第一个账户:
查看当前挖矿奖励入账账户,默认为创建的第一个账户:
> eth.coinbase INFO [07-24|14:59:12.890] Etherbase automatically configured address=0xB9d7fA2DaB728AbA0738BEdfD48d352d0EDff32a
"0xb9d7fa2dab728aba0738bedfd48d352d0edff32a"
更改入账账户:
> miner.setEtherbase(eth.accounts[1])
true
> eth.coinbase
"0xeabdde05da845490d24d7492275fd256b28c05b9"
查看挖矿收入:
> eth.getBalance(eth.coinbase)
50000000000000000000
单位wei,以太币的最小单位,1个以太币=10的18次方个wei。
要查看有多少个以太币,可以用web3.fromWei()将返回值换算成以太币:
> web3.fromWei(eth.getBalance(eth.coinbase))
50
开始一笔交易,从账户0转移10个以太币到账户1:
> amount = web3.toWei(10,'ether')
"10000000000000000000"
> eth.sendTransaction({from:eth.accounts[0],to:eth.accounts[1],value:amount})
Error: authentication needed: password or unlock
at web3.js:3143:20
at web3.js:6347:15
at web3.js:5081:36
at <anonymous>:1:1
提示失败,没有授权,这是因为一段时间不操作账户,自动lock,解除lock:
> personal.unlockAccount(eth.accounts[0])
Unlock account 0x5ee8bdf8543e00f3cccea9651d2f1e372591cba3
Passphrase:
true
再次转帐:
> amount = web3.toWei(10,'ether')
"10000000000000000000"
> eth.sendTransaction({from:eth.accounts[0],to:eth.accounts[1],value:amount})
INFO [07-24|15:55:44.646] Submitted transaction fullhash=0x8ec15449376791daedfb34e0a8f0dc2f249de4e7587c267a857db2d3443e6e17 recipient=0xEABDDe05da845490d24d7492275fD256b28C05b9
"0x8ec15449376791daedfb34e0a8f0dc2f249de4e7587c267a857db2d3443e6e17"
查看账户1是否收到:
> eth.getBalance(eth.accounts[1])
0
仍旧是0,没有收到。这说明这笔交易没有人确认,需要矿工挖矿记录。
先看一下交易池的状况:
> txpool.status
{ pending: 1, queued: 0 }
很明显有一笔交易pending状态,未确认。
自己挖矿确认下,这里仅仅挖出一个区块就可以了:
> miner.start(1);admin.sleepBlocks(1);miner.stop();
INFO [07-24|15:59:00.308] Updated mining threads threads=1
INFO [07-24|15:59:00.308] Transaction pool price threshold updated price=18000000000
INFO [07-24|15:59:00.309] Starting mining operation
INFO [07-24|15:59:00.309] Commit new mining work number=11 txs=1 uncles=0 elapsed=183.92µs
INFO [07-24|15:59:03.009] Successfully sealed new block number=11 hash=2dad01…7e68d3
INFO [07-24|15:59:03.010] ? mined potential block number=11 hash=2dad01…7e68d3
INFO [07-24|15:59:03.010] Commit new mining work number=12 txs=0 uncles=0 elapsed=144.847µs
true
再查看下账户1到的余额:
> web3.fromWei(eth.getBalance(eth.coinbase))
15.000378
为什么是15呢?还有小数,这是怎么回事?
这是因为之前执行了更改入账账户:
> miner.setEtherbase(eth.accounts[1])
true
> eth.coinbase
"0xeabdde05da845490d24d7492275fd256b28c05b9"
这次挖矿奖励就会打入账户1,这样余额就是转入的10个币加上5个挖矿奖励,小数是交易费收入。
ok,初步操作到此为止。