第02课:以太坊私联搭建 & 公链同步
上一篇内容我们介绍了以太坊 geth 节点的基本操作,本篇内容就带大家搭建不同环境下 geth 的服务、相关配置及操作流程。

dev 模式启动

dev
模式,也叫回归测试模式,主要用来给开发人员提供一个方便的开发测试环境。这样,我们可以轻松的获得以太币,方便发起交易,交易也会被快速的打包,节省时间方便验证;而且我们还可以利用
dev 模式进行区块孤立场景的测试。dev 模式的启动与上一篇所讲的启动命令有所区别,下面就带大家详细了解一下。

dev 模式默认启动
--dev 使用POA共识网络,默认预分配一个开发者账户并且会自动开启挖矿。
启动 dev 模式其实很简单,在我们上一篇内容中命令后面添加 -dev 即可。
>./geth --datadir /Users/zzs/develop/eth/geth/data-test --rpc --rpcapi
"db,eth,net,web3,miner,personal" -dev
这样就启动了 dev 模式,我们来看一下启动日志信息:
WARN [04-02|09:04:34] No etherbase set and no accounts found as default INFO
[04-02|09:04:35] Using developer account
address=0xADCE6e0E1eE491e7c1945e76d3dc5975418C4e45 INFO [04-02|09:04:35]
Starting peer-to-peer node
instance=Geth/v1.7.3-stable-4bb3c89d/darwin-amd64/go1.9.2 INFO [04-02|09:04:35]
Allocated cache and file handles
database=/Users/zzs/develop/eth/geth/data-test/geth/chaindata cache=128
handles=1024 INFO [04-02|09:04:35] Writing custom genesis block INFO
[04-02|09:04:35] Initialised chain configuration config="{ChainID: 1337
Homestead: 0 DAO: <nil> DAOSupport: false EIP150: 0 EIP155: 0 EIP158: 0
Byzantium: 0 Engine: clique}" INFO [04-02|09:04:35] Initialising Ethereum
protocol versions="[63 62]" network=1 INFO [04-02|09:04:35] Loaded most recent
local header number=0 hash=419c07…e277dd td=1 INFO [04-02|09:04:35] Loaded most
recent local full block number=0 hash=419c07…e277dd td=1 INFO [04-02|09:04:35]
Loaded most recent local fast block number=0 hash=419c07…e277dd td=1 INFO
[04-02|09:04:35] Regenerated local transaction journal transactions=0
accounts=0 INFO [04-02|09:04:35] Starting P2P networking INFO [04-02|09:04:35]
started whisper v.5.0 INFO [04-02|09:04:35] RLPx listener up
self="enode://e630c712e4c2f52f4d535a9ded1c7489a579abea4420753e4d6a899736ed512a4858684e24457484d61d3093a9b1882cc3d11e92241e1f91a1300f9aa117f671@[::]:52275?discport=0"
INFO [04-02|09:04:35] IPC endpoint opened:
/Users/zzs/develop/eth/geth/data-test/geth.ipc INFO [04-02|09:04:35] HTTP
endpoint opened: http://127.0.0.1:8545 INFO [04-02|09:04:35] Transaction pool
price threshold updated price=18000000000 INFO [04-02|09:04:35] Starting mining
operation INFO [04-02|09:04:35] Commit new mining work number=1 txs=0 uncles=0
elapsed=69.581µs WARN [04-02|09:04:35] Block sealing failed err="waiting for
transactions" INFO [04-02|09:04:38] Mapped network port proto=tcp extport=52275
intport=52275 interface="UPNP IGDv1-IP1"
通过日志可以看到,在 dev
模式下,启动节点之后,会默认提供一个开发者账号:0xADCE6e0E1eE491e7c1945e76d3dc5975418C4e45,这个账号会作为当前的
coinbase 账号,在 keystore 目录下也有对应的加密私钥文件。

实战经验

默认启动模式在当前版本中是有一些迷惑人的地方,但请相信 geth 开发者的初衷是友好的。当启动节点之后,指定 miner.start() 命令并不会返回
true,进行挖矿。
> miner.start() null
这样大家经常咨询的一个问题,为什么呢?其实,在 dev 默认模式启动的情况下,已经开启了挖矿,因此当再执行挖矿命令时会返回
null。以下是挖矿命令对应的源代码,也就是说无论是否成功挖矿都会返回 null。
func (s *Ethereum) StartMining(local bool) error { eb, err := s.Etherbase() if
err != nil { log.Error("Cannot start mining without etherbase", "err", err)
return fmt.Errorf("etherbase missing: %v", err) } if clique, ok :=
s.engine.(*clique.Clique); ok { wallet, err :=
s.accountManager.Find(accounts.Account{Address: eb}) if wallet == nil || err !=
nil { log.Error("Etherbase account unavailable locally", "err", err) return
fmt.Errorf("signer missing: %v", err) } clique.Authorize(eb, wallet.SignHash) }
if local { // If local (CPU) mining is started, we can disable the transaction
rejection // mechanism introduced to speed sync times. CPU mining on mainnet is
ludicrous // so noone will ever hit this path, whereas marking sync done on CPU
mining // will ensure that private networks work in single miner mode too.
atomic.StoreUint32(&s.protocolManager.acceptTxs, 1) } go s.miner.Start(eb)
return nil }
交易模式

再看一下启动日志中的此行信息:
WARN [04-02|09:04:35] Block sealing failed err="waiting for transactions"
这是什么?这就是默认启动 dev 模式的一个特性之一。geth
节点的开发者为了给测试环境提供一个更友好的操作:只有你发过来交易我才会挖矿打包,如果未发送交易过来,就不会去挖矿打包。这样是不是很方便,不用被一些并没有交易的区块刷屏了。

但是此模式有一个弊端,那就是发送一笔交易想让它被确认多次是不是很费劲,还需要再在后面发送 N 次交易?这个不用担心,后面会讲到另外一种模式。

实战经验


有同学又问了,这种模式我想发起交易,可是没有以太币怎么办?因为只有发起了交易才能挖到以太币,而发起交易又需要以太币,这不是形成了一个死循环了吗?针对这个问题,不用担心,geth
节点已经为你想到了,我们来查询一下开发者账号里面的余额:
> eth.getBalance("0xadce6e0e1ee491e7c1945e76d3dc5975418c4e45")
1.15792089237316195423570985008687907853269984665640564039457584007913129639927e+77
看看里面的以太币是不是已经足够使用了?!

dev 之自动挖矿

在官方文档中我们可以看到如下参数,此参数默认为 0,也就是上面讲的被动挖矿的模式,当有 pending 交易到来才进行挖矿,同时它还有一个参数值
1,主动挖矿。
--dev.period value 开发者模式下挖矿周期 (0 = 有pending状态交易时进行挖矿) (默认: 0)
此时启动命令如下:
>./geth --datadir /Users/zzs/develop/eth/geth/data-test --rpc --rpcapi
"db,eth,net,web3,miner,personal" -dev --dev.period 1
这样就回到常见的疯狂挖矿的模式了,当然此时还是可以执行 stop 命令来停止挖矿的。以下是挖矿时的日志输出:
INFO [04-02|09:33:59] Starting mining operation INFO [04-02|09:33:59] Commit
new mining work number=1 txs=0 uncles=0 elapsed=103.436µs INFO [04-02|09:33:59]
Successfully sealed new block number=1 hash=3fe3df…3a0a53 INFO [04-02|09:33:59]

友情链接
KaDraw流程图
API参考文档
OK工具箱
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:[email protected]
QQ群:637538335
关注微信