简体   繁体   English

以太坊专用网挖矿

[英]Ethereum private network mining

1) I setup a private ethereum network using the following command 1)我使用以下命令建立一个专用的以太坊网络

$geth --genesis <genesis json file path> --datadir <some path to an empty  
folder> --networkid 123 --nodiscover --maxpeers 0 console

2) Created an account 2)创建一个帐户

3) Then, started the miner using miner.start() command. 3)然后,使用miner.start()命令启动矿工。

After a while ethers were getting added automatically to my account, but I don't have any pending transaction in my private network. 不久之后,以太币被自动添加到我的帐户中,但是我的专用网络中没有任何待处理的交易。 So from where could my miners are getting the ethers? 那么我的矿工从哪里可以得到以太币呢?

Even though, I didn't instantiate any transactions in my network, I could see the some transaction being recorded in the logs, once I start the miner. 即使我没有实例化网络中的任何事务,但一旦启动矿机,我就能看到一些事务记录在日志中。

The log is as follows: 日志如下:

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]

And my genesis block code is as follows: 我的创始块代码如下:

{
“nonce”: “0xdeadbeefdeadbeef”,
“timestamp”: “0x0”,
“parentHash”: 
“0x0000000000000000000000000000000000000000000000000000000000000000”,
“extraData”: “0x0”,
“gasLimit”: “0x8000000”,
“difficulty”: “0x400”,
“mixhash”: 
“0x0000000000000000000000000000000000000000000000000000000000000000”,
“coinbase”: “0x3333333333333333333333333333333333333333”,
“alloc”: {
}
}

Since my network is isolated and have only one node (no peers), I am quite confused with this behaviour. 由于我的网络是孤立的,并且只有一个节点(没有对等节点),因此我对此行为感到非常困惑。 Any insights would be greatly appreciated. 任何见解将不胜感激。

Your client is mining empty blocks (containing no transactions) and getting rewards for mined blocks what is 5 ETH per block. 您的客户正在挖空区块(不包含任何交易),并获得挖出区块的奖励,即每区块5 ETH。

If you want to prevent empty block in your private blockchain you should consider using eth client (C++ implementation). 如果要防止私有区块链中出现空块,则应考虑使用eth客户端(C ++实现)。

In case of geth client you can use a JavaScript script that will modify client's behavior. 如果是geth客户,则可以使用JavaScript脚本来修改客户的行为。 Any script can be loaded with js command: geth js script.js . 任何脚本都可以使用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();

You can try EmbarkJS which can run geth client with mineWhenNeeded option on private network. 您可以尝试EmbarkJS ,它可以在专用网络上使用mineWhenNeeded选项运行geth客户端。 It will only mine when new transactions come in. 它只会在有新交易进入时才开采。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM