This tutorial is part of a series of blog posts on getting a web page on your web server to communicate via web3 with your Ethereum test network on localhost.
In our previous post, we created the first block in our testnet using a CustomGenesis.json. In the CustomGenesis.json, we set the difficulty attribute to a very low value, which will enable us to mine some Ether on our testnet quickly.
In this tutorial, we’ll describe how to mine for Ether on your testnet.
In your Terminal, run the following command to create a new account on your testnet. All of the arguments should look familiar. Pay special attention to the account new, which will create a new user.
Joels-MBP-4:Clients joelgarcia$ geth --networkid 200 --identity node1 --verbosity 3 --nodiscover --nat none --datadir=~/myBlockChain/node1 account new
After you enter the password for this new user, you’ll be provided with the unique id for your new user. Copy and paste the address to a safe place.
You should also be able to see this user in your file system by opening up Finder to your myBlockChain directory.
Next, we’re going to start up geth. We’re going to start up geth to enable RPC so that Mist and our web pages in the future can access geth.
geth --networkid 200 --identity node1 --datadir=~/myBlockChain/node1 --rpc --rpcaddr "127.0.0.1" --rpcport "8545" --rpccorsdomain "*"
Most of the parameters should look familiar. We’ve added arguments to enable RPC to enable remote procedure calls to 127.0.0.1, localhost, over port 8545, for all domains. You would not want to run this for a production environment as you’re enabling any domain to access via RPC.
In the subsequent results, you should see that the testnet has started. Make note of your IPC endpoint in the second to last line as we’ll use it next.
INFO [07-20|22:07:32] IPC endpoint opened: /Users/joelgarcia/myBlockChain/node1/geth.ipc INFO [07-20|22:07:32] HTTP endpoint opened: http://127.0.0.1:8545
Open another Terminal shell. In this Terminal shell, we’re going to connect to the existing geth server via ipc. Type the following replacing my ipc endpoint address with your ipc endpoint address into the Terminal shell.
geth attach /Users/joelgarcia/myBlockChain/node1/geth.ipc
The output from the geth attach should be a subsequent command prompt
Type the following command to verify that your user is in the system. The results should be the same address as the results from the new account creation. In this case,
> personal.listAccounts "0x446e063b51d75ef76ab66c2307e04ced44f39ea6"
Next, we’re going to associate the miner with this account.
> miner.setEtherbase(personal.listAccounts) true
Last, but not least, we’ll start to mine Ether.
> miner.start() null
All of a sudden in your geth instance Terminal, you should start to see a ton of activity as the DAG starts to get built
INFO [07-20|22:20:01] Starting mining operation INFO [07-20|22:20:01] Commit new mining work number=1 txs=0 uncles=0 elapsed=665.777µs INFO [07-20|22:20:03] Generating DAG in progress epoch=0 percentage=0 elapsed=493.162ms INFO [07-20|22:20:03] Generating DAG in progress epoch=0 percentage=1 elapsed=974.826ms INFO [07-20|22:20:04] Generating DAG in progress epoch=0 percentage=2 elapsed=1.473s INFO [07-20|22:20:04] Generating DAG in progress epoch=0 percentage=3 elapsed=1.991s INFO [07-20|22:20:05] Generating DAG in progress epoch=0 percentage=4 elapsed=2.479s
The CPU will get very busy as you build the DAG and start to mine.
After you’ve created the DAG, you’ll start to see the new mining work in the output of your first Terminal shell.
INFO [07-20|22:22:44] Commit new mining work number=84 txs=0 uncles=0 elapsed=138.825µs
> eth.getBalance(personal.listAccounts) 455000000000000000000
Wow, we mined a lot of Ether on our testnet. In our next tutorial, we’ll demonstrate how to connect Mist to our local testnet so we can see how much ether is in the account through the Wallet. We’ll then start to fire up a Node.js http server to connect to our Geth instance via RPC.