停止在gehere ethereum的私人网络上开采

问题描述 投票:0回答:1

我使用下面的客户端创建了一个geth客户端(我已经创建了两个帐户:

geth --datadir datadir --networkid 123 --rpc --rpcaddr="localhost" --rpccorsdomain="*" --unlock <my account> --minerthreads="1" --maxpeers=0 --mine console

我打开了以太坊钱包并从那里部署了智能合约。在我的geth控制台上收到transactionId和合同地址。

然后我启动了我的Dapp并创建了合同的实例,我正在调用通过web3 API调用契约函数的契约。调用契约函数但是除非我开始挖掘,否则事务不会在块中提交。因此,我开始miner.start()这开始挖掘无数块。

我的问题是,如果我拥有自己的私人网络并且只提交了一笔交易,那么这些街区会从哪里来。这会添加太多块,我的块大小不必要地增加。如何只挖掘我提交的交易?

blockchain ethereum mining
1个回答
0
投票

将以下代码保存为startmine.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();

并使用以下选项启动geth Private网络

geth .......... . . . . . . .   --rpcapi="web3,eth,miner" --preload "startmine.js" console

当您有任何挂起的事务时,这将自动运行miner.start()。

© www.soinside.com 2019 - 2024. All rights reserved.