This is the second part of our Ethereum Zero to Hero guide. If you have not read part, I highly recommend it before jumping ahead.
In this post, we are going to set up a basic private test-net environment for ethereum development and experimentation.
While we are learning the in’s and out’s of Ethereum development, we probably don’t want to test against the real production network with real ether.
There are several Ethereum tests-nets out in the wild for this purpose Rinkeby and Morden for example; however interacting with this networks still requires for us to acquire ethereum either by mining, or getting ether from other users.
While we will eventually work with the public test networks, for beginners like us that are just starting out, that is far from ideal, and it can be difficult to get significant amounts of ether.
A better option is setting up our private testnet, that we can bend and twist; as well it will allow us to gain a deeper understanding of the inner workings of the Ethereum network.
We will need to install the geth, a go CLI client that will allow us to run a full ethereum node locally.
brew tap ethereum/ethereum brew install ethereum
We can verify that everything installed correctly by typing:
Which should give you an output similar to the following:
To get our on private testnet started, we are going to need a Genesis Block. Every Blockchain requires a genesis block, which is essentially the configuration file for our blockchain.
The genesis block is the start of the blockchain - the first block, block 0, and the only block that does not point to a predecessor block. The protocol ensures that no other node will agree with your version of the blockchain unless they have the same genesis block, so you can make as many private testnet blockchains as you would like!
As you can see the file contents are in json and for the most part self-explanatory, but let’s go over the each one of the parameters quickly to clarify their purpose.
Finally, the config parameters are there to ensure that certain protocol upgrades are available from the get go.
For a full explanation check this answer in stackexchange
Next, we will need to initialize our chain by running the following command:
geth --datadir ./TestNetData init HeroGenesis.json
Setting the data directory is important since otherwise we will override the default data directory for the real Ethereum network.
The output should return something similar to the following:
Now that we create our initial chain we can run it by executing the following command:
geth --datadir ./TestNetData --identity "HeroNode1" --rpc --rpcport "8080" --rpccorsdomain "*" --port "30303" --nodiscover --rpcapi "dbundefinedethundefinednetundefinedweb3" --maxpeers 0 --networkid 24 console
This command does a few things:
The output should be something similar to the following:
At this point, you should be up and running, and in the geth console ready to do something, but what?
Well let’s switch attention to the following Warning:
WARN No etherbase set and no accounts found as default
We have node up and running, but it will not be much use without any accounts.
Since we are already in the Geth console is easier to ahead and create an account directly from there, in the terminal type the following:
The console will ask for a passphrase DO NOT LOSE THIS!, and return a has like the following:
And let’s validate that the account exist by running:
> eth.getBalance("0xe857331e4e3354bb72b3751cce419c8444e89e17") 0
Now, let’s put some ether into that account.
Now, while we could easily issue ether to our account using the Genesis block, let’s have some fun and mine Ether on our private network.
Open a new terminal tab and running the following command:
get attach ./<DirectoryName>/geth.ipc
If we go back to our first terminal we should see the following happening:
Let’s go back to the second terminal and stop the miner and check our balance:
miner.stop() eth.getBalance('0xe857331e4e3354bb72b3751cce419c8444e89e17') 75000000000000000000
Congratulations! We have now created our private network and mined our few amount of ether, and we have a valid network that we can use to developer our Smart Contracts and DApps.
In the next post of the series, we will start developing with Solidity and Smart Contracts.