fblife avatar

使用Go-Ethereum搭建以太坊私有链

fblife

Published: 01 Mar 2018 › Updated: 01 Mar 2018使用Go-Ethereum搭建以太坊私有链

使用Go-Ethereum搭建以太坊私有链

发一篇技术文章,之前从网上找了好多教程(没有一个是完整的),安装过程中遇到不少问题,记录一下。

一、安装

git clone https://github.com/ethereum/go-ethereum.git

cd  go-ethereum

make geth

make all

二、运行

mkdir chain

geth --datadir "./chain" -dev -dev.period 1 --nodiscover console 2>>eth_output.log

三、使用

查看账户

> eth.accounts

创建账户

> personal.newAccount("123456")

查看余额

> web3.eth.getBalance("0xbe323cc4fde114269a9513a27d3e985f82b9e25d")

或者

> acc0 = eth.accounts[0]

> eth.getBalance(acc0)

查看格式化的以太币

> web3.fromWei(web3.eth.getBalance(acc0))

挖矿

> miner.start()

> miner.stop()

查看全部账户余额

> function checkAllBalances() {

     var totalBal = 0;

     for (var acctNum in eth.accounts) {

         var acct = eth.accounts[acctNum];

         var acctBal = web3.fromWei(eth.getBalance(acct), "ether");

         totalBal += parseFloat(acctBal);

         console.log("  eth.accounts[" + acctNum + "]: \t" + acct + " \tbalance: " + acctBal + " ether");

     }

     console.log("  Total balance: " + totalBal + " ether");

 };

> checkAllBalances()

或者保存到一个脚本里,然后加载运行

> loadScript('/path/script/here.js')

> checkAllBalances()

转账

> web3.eth.sendTransaction({from:acc0,to:acc1,value:web3.toWei(3,"ether")})

四、问题

1. 转账失败

a. 账号被锁

当直接执行此方法时会抛出异常,显示帐号被锁

> web3.eth.sendTransaction({from:acc0,to:acc1,value:web3.toWei(3,"ether")})

Error: authentication needed: password or unlock

    at web3.js:3104:20

    at web3.js:6191:15

    at web3.js:5004:36

    at <anonymous>:1:1

解锁转帐帐户

> web3.personal.unlockAccount(acc0,"123456")

b. 未挖矿

可能未运行挖矿

> miner.start()

2. miner.start()返回null

a. 是否设置miner地址

查看当前节点下面是否有账号存在

> personal.listAccounts

当确认账户已经存在时,可以设置Etherbase。先查看以下coinbase账户

> eth.coinbase

执行设置miner地址

> miner.setEtherbase(eth.coinbase)

或者

> miner.setEtherbase(eth.accounts[0])

b. 节点误报

另外一种情况就是其实miner.start()命令已经执行成功,只不过节点返回null。如果是dev模式,可以使用eth.blockNumber查看一下区块高度是否增加。

c. 节点版本问题

出现此问题的原因在于geth版本更新之后,–dev模式下新增了一个参数项:

--dev               Ephemeral proof-of-authority network with a pre-funded developer account, mining enabled

--dev.period value  Block period to use in developer mode (0 = mine only if transaction pending) (default: 0)

–dev是我们常用的参数,之前版本中我们只用使用–dev然后执行miner.start()就可以挖矿,但是在后面的版本中,当我们会发现只有发送交易了才会挖一个块。

引起此问题的原因就是新增了–dev.period value配置项。此配置默认值为0,也就是说只有有pending中的交易才会挖矿。

明白了这个参数的含义之后,解决问题就很简答了,之前的–dev参数依旧使用,然后再在后面添加–dev.period 1,注意,参数值为1,不是默认的0。

再重新启动节点,然后执行挖矿,先不管返回是否是null,执行之后,无论查看日志或执行eth.blockNumber都会发现块在不停的增高。

Leave 使用Go-Ethereum搭建以太坊私有链 to:

Written by

steemitcn.com - Steemit中国,致力于打造国内steemiter交流的社区。

Read more #cn posts


Best Posts From fblife

We have not curated any of fblife's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.

More Posts From fblife