1) 我使用以下命令设置了一个私有以太坊网络
$geth --genesis <genesis json file path> --datadir <some path to an empty
folder> --networkid 123 --nodiscover --maxpeers 0 console
2) 已创建帐户
3)然后,使用miner.start()命令启动矿工。
一段时间后,以太币会自动添加到我的帐户中,但我的私人网络中没有任何待处理的交易。那么我的矿工可以从哪里获得以太币呢?
即使我没有在我的网络中实例化任何交易,一旦我启动矿工,我可以看到日志中记录了一些交易。
日志如下:
I0118 11:59:11.696523 9427 backend.go:584] Automatic pregeneration of ethash
DAG ON (ethash dir: /Users/minisha/.ethash)
I0118 11:59:11.696590 9427 backend.go:591] checking DAG (ethash dir:
/Users/minisha/.ethash)
I0118 11:59:11.696728 9427 miner.go:119] Starting mining operation (CPU=4
TOT=5)
true
> I0118 11:59:11.703907 9427 worker.go:570] commit new work on block 1 with 0
txs & 0 uncles. Took 7.109111ms
I0118 11:59:11.704083 9427 ethash.go:220] Generating DAG for epoch 0 (size
1073739904) (0000000000000000000000000000000000000000000000000000000000000000)
I0118 11:59:12.698679 9427 ethash.go:237] Done generating DAG for epoch 0, it
took 994.61107ms
I0118 11:59:15.163864 9427 worker.go:349]
我的创世区块代码如下:
{
“nonce”: “0xdeadbeefdeadbeef”,
“timestamp”: “0x0”,
“parentHash”:
“0x0000000000000000000000000000000000000000000000000000000000000000”,
“extraData”: “0x0”,
“gasLimit”: “0x8000000”,
“difficulty”: “0x400”,
“mixhash”:
“0x0000000000000000000000000000000000000000000000000000000000000000”,
“coinbase”: “0x3333333333333333333333333333333333333333”,
“alloc”: {
}
}
由于我的网络是孤立的并且只有一个节点(没有对等节点),我对这种行为感到非常困惑。任何见解将不胜感激。
您的客户正在挖掘空区块(不包含交易)并获得开采区块的奖励,即每个区块 5 ETH。
如果你想在你的私有区块链中防止空块,你应该考虑使用eth客户端(C++实现)。
对于 geth 客户端,您可以使用 JavaScript 脚本来修改客户端的行为。任何脚本都可以使用 js
命令加载:geth js script.js
。
var mining_threads = 1
function checkWork() {
if (eth.getBlock("pending").transactions.length > 0) {
if (eth.mining) return;
console.log("== Pending transactions! Mining...");
miner.start(mining_threads);
} else {
miner.stop(0); // This param means nothing
console.log("== No transactions! Mining stopped.");
}
}
eth.filter("latest", function(err, block) { checkWork(); });
eth.filter("pending", function(err, block) { checkWork(); });
checkWork();
您可以尝试 EmbarkJS,它可以在专用网络上运行geth
带有mineWhenNeeded
选项的客户端。它只会在新交易进入时挖矿。