Subscribe to get access to this video

and the whole library of videos, sample code, and tutorials.

Tip: Mining only when there are transactions

I want to give you a tip on how to only mine blocks when there are transactions. More generally, this will show you how to load utility scripts into your geth console.

Here’s the problem: when we’re developing smart contracts, every time we want to make a change we have to issue and transaction and that transaction has to be picked up by a miner.

Eventually, we’ll deploy contracts to live test networks or the production network, but for testing, we just run a local node.

Ethereum miners, by default, mine blocks very quickly, and the mining process can be CPU intensive. The problem is I’m generally developing on my personal laptop, and I don’t have a lot of extra CPU to spare.

Fortunately we this script which can be found on the Ethereum wiki which will start and stop our miners automatically depending on if there are transactions to be mined.

Let’s copy and paste this code into a file.

mkdir ~/courses/newline-web3/scripts
pbpaste > ~/courses/newline-web3/scripts/mine-only-when-transactions.js

Now you could just copy and paste this directly into your geth console, that would work. But geth also has a loadScript helper function we can use to load this, and any other helper functions we might want:

loadScript("./scripts/mine-only-when-transactions.js")

If you get the path wrong, this will return false. If you have the correct path, you should see “No transactions! Mining stopped.” and it will return true.

 

Subscribe to get the full text of this lesson

and the entire library of videos, sample code, and tutorials.