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?

  • Please share exact code. – niksmac Feb 13 '17 at 2:47
  • I have a web3 instance and have written var mine = web3.miner.start(); – anonymous Feb 13 '17 at 12:09
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

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

try to load geth with the miner module:

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

  • You might need need --rpcapi instead, if it's over RPC. – Matthew Schmidt Feb 12 '17 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 Feb 13 '17 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 Feb 13 '17 at 15:22
  • Tried this but still no result. – anonymous Feb 14 '17 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, privacy policy and cookie policy

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