0

I am using the web3 api and want to try out mining using rpc. The usual miner.start() (which is used on terminal) does not work even after including the miner api when starting geth and I get the following error.

Error: Cannot read property start of undefined.

What am I missing?

2
  • Please share exact code.
    – niksmac
    Commented Feb 13, 2017 at 2:47
  • I have a web3 instance and have written var mine = web3.miner.start();
    – anonymous
    Commented Feb 13, 2017 at 12:09

3 Answers 3

1

Web3 is not exposing a miner object as its own. Refer here

You can do this from ipc socket with help of javascript.

  • Save the function as a file mine.js
  • Load the script into ipc console using loadScript('YOURPATH/mine.js');
  • Make sure you have necessary modules loaded. geth --rpc --rpcapi web3,eth,net,personal,miner,admin,debug--rpcaddr="localhost" --rpcport="8545" --rpccorsdomain="localhost:8545" console

An example would be

/*
 * Mine when there are transactions!
 */

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 refer more ethereum utility snippets

1
  • Sorry for the late reply. I cannot understand how to use such a script. Please explain how this will work.
    – anonymous
    Commented Feb 14, 2017 at 19:19
1

try to load geth with the miner module:

e.g : --ipcapi "admin,db,eth,debug,miner,personal,web3"

4
  • You might need need --rpcapi instead, if it's over RPC. Commented Feb 12, 2017 at 21:29
  • tried that still not working. This is what I wrote geth --rpc --rpcapi web3,eth,net,personal,miner,admin,debug--rpcaddr="localhost" --rpcport="8545" --rpccorsdomain="localhost:8545" console
    – anonymous
    Commented Feb 13, 2017 at 12:04
  • try geth --rpc --rpcaddr="0.0.0.0" --rpccorsdomain="*" --rpcapi="db,eth,net,web3,personal,web3" --ipcapi "admin,eth,debug,personal,web3,miner"
    – Badr Bellaj
    Commented Feb 13, 2017 at 15:22
  • Tried this but still no result.
    – anonymous
    Commented Feb 14, 2017 at 19:21
0

I finally used an extended web3 module. Used npm install web3admin and then in my server code wrote web3Admin.extend(web3). This allows me to use the miner api.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.