3

我已经使用以下客户端启动了一个 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() 这开始挖掘大量区块。

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

4

2 回答 2

1

将以下代码保存为 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专用网络

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

当您有任何待处理的交易时,这将自动运行 miner.start()。

于 2016-12-02T05:31:21.100 回答
0

如果您使用的是 POA,以及更新版本的geth(至少1.9.25),将 设置period0将停止挖掘并使节点在挖掘新块之前等待交易。这对于专用网络或实验环境非常有用。

创世纪.json:

....
"clique": {
  "period": 0,
  "epoch": 30000
},
....

先前M Gopal回答

于 2021-04-09T17:02:08.907 回答