I'm a Floridian web engineer who planted his roots in Paris. Some of my favorite past-times are analyzing and optimizing development processes, building solid APIs, mentoring junior devs, and long walks on the beach.

Ethereum Part 1: Test Blockchain Setup

Ethereum

Ethereum is a cryptocurrency like Bitcoin which allows for transactions to take place that are not limited to leger changes but also allow for the running of code known as smart contracts. Their unit of currency is called ether and it is used whenever you want to push code onto the blockchain or you run code on the blockchain that modifies the underlying data in some way. This article from EtherScripter goes more into detail.

Requirements

The ethereum site says you just need 1-2gb free to start developing. For me, I am using an ubuntu virtualbox with

  • 4 gigs of RAM
  • 2 CPU
  • 8 gigs of HDD // With 2 chains(dev and staging) I’m at 42% capacity

Setup

In order to start playing around with ethereum I used the following things:

  • ubuntu - apt-get all the things
  • go-ethereum - geth command line utility to run your own blockchain
  • truffle - A lightweight framework for developing, compiling, and deploying code
  • vim - A command-line editor
  • tmux - A terminal multiplexer because there is a lot going on

Get go-ethereum and build your blockchain


sudo apt-get install software-properties-common
sudo add-apt-repository -y ppa:ethereum/ethereum
sudo add-apt-repository -y ppa:ethereum/ethereum-dev
sudo apt-get update
sudo apt-get install -y ethereum
mkdir -p ~/dapps/test/data
mkdir -p ~/dapps/test/config/
touch ~/dapps/test/dev_up   # Startup script for development
chmod +x ~/dapps/test/dev_up
touch ~/dapps/test/up           # Startup script
chmod +x ~/dapps/test/up

Create your startup scripts

~/dapps/test/dev_up:


geth --datadir "data" --trace "data/chaintrace.log" \
     --port 30303 --rpc --rpcport 8101 \
     --rpcaddr 0.0.0.0 --networkid 0 \
     --rpccorsdomain "*" --nodiscover \
     --genesis="config/genesis.json" \
     --rpcapi "eth,web3" \
     --maxpeers 0 --unlock 0 \
     --password ./password \
     console

This file will call geth and specify logging and data directories. It doesn’t look for other network ids to connect with. It is pretty standard for a dev environment. This also attempts to unlock the first account in it’s system and use the password in a local password file. This will be used for development.

~/dapps/test/up:


geth --datadir "testnet/data" --trace "data/chaintrace.log" \
     --port 30303 --rpc --rpcport 8101 \
     --rpcaddr 0.0.0.0 --networkid 0 \
     --rpccorsdomain "*" --nodiscover \
     --genesis="config/genesis.json" \
     --rpcapi "eth,web3" \
     console

It’s the same as the previous file but you can use this one without having any accounts. If you want more information on this setting up a test chain, you can use this as a reference: Ethereum Testing.

Genesis block

The next thing is to create a genesis file to seed the blockchain with. This file is needed for creating the base of the blockchain. A little more information about the production chain can be found on the Bitcoin wiki page and the Ethereum blog.

config/genesis.json


{
    "nonce": "0xdeadbeefdeadbeef",
    "timestamp": "0x0",
    "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
    "extraData": "0x0",
    "gasLimit": "0x8000000",
    "difficulty": "0x400",
    "mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000",
    "coinbase": "0x3333333333333333333333333333333333333333",
    "alloc": {
    }
}

Create an Account

You can spool up your new test blockchain and access the console with ~/dapps/test/up


> personal.newAccount()
Passphrase:
Repeat passphrase:
"0xYourNewAccount"

Ok. A word to the wise, don’t lose your password or your account hash. When they are gone, they are gone.

In the console, you can find a list of your accounts with the command personal.listAccounts. Now take the password you just entered and put it in ~/dapps/test/password. Now when you run ~/dapps/test/dev_up it will bring up your test chain and unlock the first account in personal.listAccounts with the password in the password file.

Exit the console and start up the dev_up version. Now we need to grab some ether by starting a miner in the console.


miner.start(10) // Starts 10 miner threads.
I0216 20:13:20.229458 miner/worker.go:349] 🔨  Mined block (#1 / 67af97f2). Wait 5 blocks for confirmation
I0216 20:13:20.230321 miner/worker.go:570] commit new work on block 2 with 0 txs & 0 uncles. Took 358.013µs
I0216 20:13:20.235390 miner/worker.go:349] 🔨  Mined stale block (#1 / 7feeeaa9).
….

This will go on forever unless you stop it. So just run miner.stop(10) to stop the miner threads. You can see your ether balance by running web3.fromWei(eth.getBalance("0xYourAccountHash"), "ether"). With that you will be ready to start making a dapp.