C 343: Mining Sepolia Testnet Ether (20 pts.)

What You Need

Purpose

To set up a miner and get sepolia test ether. This Ether has no monetary value. The only point is to learn how mining works.

Background

Ethereum is undergoing The Merge, a big change from Proof of Work (PoW) to Proof of Stake (PoS). This will end many of the testnets, and end the practice of mining. The Ropsten PoW testnet will end. The newer Sepolia testnet will be the only remaining PoW network, as shown below.

Click the image to see an article with more details of The Merge.

Cooling your M1

Back in the days of CPU mining on Intel-based Macs, mining could cause CPUs to overheat. I assume the same is true of GPU mining, although I don't know for sure. On Intel machines, I used a program named "smcFanControl" to control the CPU temperature, but I haven't found a similar program for the M1.

I just put a block under my M1's back to lift it off the table, and ran a fan blowing air across it during mining. That worked.

Adjusting Screen Timeout Settings

Mining will fully tax your graphics card, and if the screen saver comes on it will freeze the machine, so you need to turn that off. I also prefer to stop screen blanking so I can see the operation of the miner.

On your M1, in "System Preferences", click Battery, on the Battery tab, Slide the "Turn display off after" slider to Never

In "System Preferences", "Desktop & Screen Saver", on the "Screen Saver" tab, clear the "Show screen saver after" box

Installing Homebrew

Homebrew is an extremely useful package manager for Macs, with functionality similar to "apt" on Debian Linux.

On your M1 Mac, in a Terminal, execute this command:

brew help
If Homebrew is installed, you will see a help message. If it is not installed, execute this command to install it:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
You will need to enter your password and press ENTER when you are prompted to.

After the installation is done, you will see two commands you need to run, outlined in the image below.

Execute those commands in your Terminal.

Then execute this command:

brew help
You should see a help message.

Installing geth

geth is the engine that runs Ethereum servers. These servers are called "nodes".

On your M1 Mac, in a Terminal, execute these commands, one at a time:

brew tap ethereum/ethereum
brew install ethereum
The "tap" operation shows a Warning about Aleth. Just ignore it.

Creating an Account

The mining rewards need to go into an account, and this account is also needed to pay gas fees.

On your M1 Mac, in a Terminal, execute this command:

geth --sepolia account new
You need to enter a passphrase twice. For this project, I recommend using P@ssw0rd

Obviously, if you get real Ether worth real money, you need to use a more secure passphrase.

Notice the Path value, outlined in the image below.

You'll need this for the next step.

Execute this command, replacing the first path with the correct path from your geth output:

cp /Users/sambowne/Library/Ethereum/sepolia/keystore/* ~/Desktop

Starting your Node

On your M1 Mac, in a Terminal, execute this command:
geth --sepolia --http --http.addr 0.0.0.0 -http.port 8545 \
 --http.api eth,net,web3,personal,miner,admin,txpool,debug
This starts a node on the Sepolia testnet. It also starts an RPC service listening on port 8545 with many permissions allowed. This makes it easy to connect to the RPC port, but could also allow attackers easy access, so it's not recommended for a network handling real Ether.

Finding Peers

Your node will start hunting on the Internet for peers to download chain data from. It takes a while to find them. Usually it takes about ten minutes, but I've seen it take more than an hour.

Importing Chain Data

After your node finds some peers, it starts importing block headers. This process will also take some time, perhaps 30 minutes.

You can proceed with the steps below while the node is working.

Importing your Account into MetaMask

On your M1, open Chrome. If you don't already have MetaMask, get it from https://metamask.io/download/

Open MetaMask. At the top right, click the network name. Click the "Add Network" button, as shown below.

Enter these values, as shown below.

Click the Save button.

Click the circular icon at the top right and click "Import Account", as shown below.

On the Import Account page, select a Type of "JSON File".

Click the "Choose File" button and navigate to the private key file on your desktop.

Enter your password, which is probably

P@ssw0rd

as shown below.

Click Import.

You see a frozen page, as shown below.

The import is working, but it is incredibly slow, due to a known limitation of MetaMask.

It may take up to 20 minutes to finish. You can leave this running while doing other steps below.

When the import finishes, you see your account, with a zero balance, as shown below.

Connecting via RPC

It's common to control your node through RPC calls (now called "http") rather than using geth directly.

Leave the other Terminal window open with geth running.

Open a second Terminal window on your M1. In the new window, execute these commands:

geth attach http://127.0.0.1:8545
net
eth.syncing
The "net" command shows the number of peers you are connected to, and some other information.

The "eth.syncing" command shows the progress of building your local copy of the blockchain.

If you see a list of numbers starting with "currentBlock", your node is still syncing and not ready to start mining yet.

When the synchronization process is complete, The "eth.syncing" command will respond with "false", as shown below.

Don't proceed past this point until your node is done syncing, so "eth.syncing" responds with "false."

Starting CPU Mining

In the Terminal window connected to your node via RPC, execute these commands:
eth.accounts
miner.setEtherbase(eth.accounts[0])
miner.start(1)
eth.getBalance(eth.accounts[0])/1E18
The first command should show your account address, and the other commands should succeed with return values as shown below.

Your current account balance is zero.

This starts your M1 mining with one processor core, which is far too slow to earn any Ether. However, it causes your node to finish prepare for mining.

C 343.1: chainID (10 pts)

In the Terminal window connected to your node via RPC, execute this command:

eth.chainId()
The flag is covered by a green rectangle in the image below.

Downloading ethminer-m1

Ethminer is the program that controls GPU mining.

On your M1, in a Web browser, go to

https://github.com/gyf304/ethminer-m1/releases

In the "M1 Precompiled Binary" section, click the arrow next to Assets to expand that section, and click ethminer-m1, as shown below.

The program downloads into your Downloads folder.

Starting Ethminer

Open a third Terminal window on your M1 and execute these commands:
cd
cd Downloads
chmod +x ethminer-m1
./ethminer-m1
The first time you try to run it, it will be blocked because it's unsigned software.

Click OK to close the error message, then open "System Preferences". Click "Security and Privacy" and then click "Allow anyway" next to the "ethminer-m1 was blocked from use" message.

Now restart ethminer with this command:

./ethminer-m1 -P getwork://127.0.0.1:8545
This connects to your local node, detects the GPU, as shown below, and starts mining with it.

After a few minutes, it settles down to a steady mining rate. On my system, I got 3.4 Mh, as shown below.

You'll gradually earn Ether. When I did it, in June, 2022, three days of mining earned about 16 seppETH, as shown below.

C 343.2: Mined Blocks (10 pts)

Once you have earned some seppETH, go to this website:

https://sepolia.etherscan.io/

Set the search type to Addresses and search for your miner's etherbase address.

There are no Transactions, but you will see some "Mined Blocks".

The flag is covered by a green rectangle in the image below.

Sources

How to mine ropsten testnet Ether
How to import account from geth console to metamask
The new AWS instance that makes ETH mining profitable
AWS Ethereum miner
What is Ethereum and How Do You Mine It?
Go Ethereum Documentation
Ethereum Testnets after The Merge
Testnets after merge
Sepolia Resources
Ethereum WikiL JSON RPC API
geth JSON-RPC Server


Posted 5-21-2022 by Sam Bowne