How to Mine Ethereum Locally with Geth

Now that we have some accounts, we can earn ETH by running a miner.

Project Source Code

Get the project source code below, and follow along with the lesson material.

Download Project Source Code

To set up the project on your local machine, please follow the directions provided in the README.md file. If you run into any issues with running the project source code, then feel free to reach out to the author in the course's Discord channel.

Now that we have an account, let's play with it.

So far we've been running geth commands from our shell, but geth actually comes with a console of it's own. We can run this with the console command:

./bin/geth --datadir=./datadir console

This console is a JavaScript console! That means we can write functions, perform calculations, etc. There are also a number of libraries, pre-installed, that we can use to interact with Ethereum.

The first one is simply called eth. We can view our list of accounts like this:

eth.accounts

Ether is the currency of Ethereum. If we want to find out how much ether we have, we call eth.getBalance:

eth.getBalance("abcxyz")

We don't have any ether yet, so it just says zero. And this brings up an interesting question: Where does ether come from in the first place?

Ether is created as a reward for mining new blocks. Again, we'll talk later about how mining works, technically, but loosely, when we want to send ether to someone else

  • we create a transaction
  • these transactions are grouped into blocks
  • miners verify that these blocks are valid and then
  • they are rewarded ether for doing this

The key idea is this: the ether is created out of thin air! This is another example of consensus -- we all just agree that miners get to create ether to give themselves a reward for doing their job.

So we can get ether by being a miner ourselves. To do this, we'll call miner.start:

miner.start(1)

When you call this, you might need to wait a few seconds before you see "Successfully sealed a new block". Let it run for a bit and then stop the miner:

miner.stop()

You can find out the number of blocks we mined by calling eth.blockNumber:

eth.blockNumber

Every block we mined will have earned us a certain number of ether as a reward. This reward will go to the account address we setup earlier. (Your mining destination address is sometimes called your "etherbase" address).

Let's find out how much ether we earned:

# first let's lookup our accounts
eth.accounts

# this is javascript, so we can also do this:
eth.accounts[0]

# now instead of copying and pasting our address we can use that in getBalance
eth.getBalance(eth.accounts[0])

This is a large number! What we're actually seeing here is the number of wei we earned. Ether has a bunch of different units, with wei being the smallest. wei is like cents to dollars, except one ether is 10^18th wei. You can view the whole list of the units here, but we're only going to use wei and ether.

It would be nice if we could see this number in ether instead of wei. Thankfully we can use the fromWei method to do this:

web3.fromWei(eth.getBalance(eth.accounts[0]), "ether")

Much better. Now we can see that we earned N ether for mining the last M blocks.

Now that we have some ether, let's spend it!

  • |

Lesson Transcript

  • [00:00 - 00:10] Now that we have an account, let's play with it. So far we've been running "get commands from our shell" but "get actually comes with a console of its own ". We can run this with the console command. This console is a JavaScript console.

  • [00:11 - 00:18] That means we can write functions, perform calculations, etc. There are also a number of libraries pre-installed that we can use to interact with Ethereum.

  • [00:19 - 00:27] The first one is simply called "eth". We can view our list of accounts like this. "eth.accounts". "eth" is the currency of Ethereum.

  • [00:28 - 00:36] If we want to find out how much Ether we have, we call "eth.getbalance". So here we call "eth.getbalance" and then we paste in the address of our account.

  • [00:37 - 00:48] We don't have any Ether yet, so it just says zero. And this brings up an interesting question. Where does Ether come from in the first place? Ether is created as a reward from mining blocks.

  • [00:49 - 01:06] Again, we'll talk later about how mining works technically, but loosely, when we want to send Ether to someone else, we create a transaction and these transactions are grouped into blocks. Miners verify that these blocks are valid and then they are rewarded Ether for doing this.

  • [01:07 - 01:17] The key idea is this. The Ether is created out of thin air. This is another example of consensus. We all just agree that miners get to create Ether to give themselves a reward for doing their job.

  • [01:18 - 01:29] So here we can get Ether by being a miner ourselves. To do this, we'll call " miner.start". We pass the number of threads, in this case it'll just be one.

  • [01:30 - 01:41] Now when you call this, you might need to wait a few seconds, maybe even a minute, before you see successfully sealed a new block. But let it run for a bit, just wait. And then after a few blocks, stop the miner.

  • [01:42 - 01:53] You can find out the number of blocks we mined by calling eth.block number. Every block we mined will have earned us a certain number of Ether as a reward.

  • [01:54 - 02:02] This reward will go to the account address we set up earlier. And note that your mining destination address is sometimes called your Ether base address.

  • [02:03 - 02:10] Let's find out how much Ether we earned. First, let's look up our accounts. And because this is JavaScript, we can also do this.

  • [02:11 - 02:28] Now, instead of copying and pasting our address, we can use that in getbalance. Well, you can see this is a large number. What we're actually seeing here is the number of whey we earned. Ether has a bunch of different units, with whey being the smallest.

  • [02:29 - 02:40] Whey is sort of like cents arta dollars, except that one Ether is 10 to the 18 th whey. You can view the whole list of the units here, but we're only going to be using whey and Ether.

  • [02:41 - 02:51] It would be nice if we could see this number in Ether instead of whey. And thankfully, we can use the from whey method to do this.

  • [02:52 - 03:06] The from whey method is in the web3 library. So we type web3.from whey, we put in our account address, and then the unit we want, which is Ether. Much better.

  • [03:07 - 03:12] Now we can see that we earned 20 Ether from mining the last few blocks. Now that we have some ether, let's spend it.

Now that we have an account, let's play with it. So far we've been running "get commands from our shell" but "get actually comes with a console of its own ". We can run this with the console command. This console is a JavaScript console. That means we can write functions, perform calculations, etc. There are also a number of libraries pre-installed that we can use to interact with Ethereum. The first one is simply called "eth". We can view our list of accounts like this. "eth.accounts". "eth" is the currency of Ethereum. If we want to find out how much Ether we have, we call "eth.getbalance". So here we call "eth.getbalance" and then we paste in the address of our account. We don't have any Ether yet, so it just says zero. And this brings up an interesting question. Where does Ether come from in the first place? Ether is created as a reward from mining blocks. Again, we'll talk later about how mining works technically, but loosely, when we want to send Ether to someone else, we create a transaction and these transactions are grouped into blocks. Miners verify that these blocks are valid and then they are rewarded Ether for doing this. The key idea is this. The Ether is created out of thin air. This is another example of consensus. We all just agree that miners get to create Ether to give themselves a reward for doing their job. So here we can get Ether by being a miner ourselves. To do this, we'll call " miner.start". We pass the number of threads, in this case it'll just be one. Now when you call this, you might need to wait a few seconds, maybe even a minute, before you see successfully sealed a new block. But let it run for a bit, just wait. And then after a few blocks, stop the miner. You can find out the number of blocks we mined by calling eth.block number. Every block we mined will have earned us a certain number of Ether as a reward. This reward will go to the account address we set up earlier. And note that your mining destination address is sometimes called your Ether base address. Let's find out how much Ether we earned. First, let's look up our accounts. And because this is JavaScript, we can also do this. Now, instead of copying and pasting our address, we can use that in getbalance. Well, you can see this is a large number. What we're actually seeing here is the number of whey we earned. Ether has a bunch of different units, with whey being the smallest. Whey is sort of like cents arta dollars, except that one Ether is 10 to the 18 th whey. You can view the whole list of the units here, but we're only going to be using whey and Ether. It would be nice if we could see this number in Ether instead of whey. And thankfully, we can use the from whey method to do this. The from whey method is in the web3 library. So we type web3.from whey, we put in our account address, and then the unit we want, which is Ether. Much better. Now we can see that we earned 20 Ether from mining the last few blocks. Now that we have some ether, let's spend it.