in Blockchain, Blockchain stack, Ethereum, Smart Contracts, Solidity, Web 3.0

Blockchain stack #1: Installing Ethereum Geth and your first smart contract

I’m writing a series of posts on the Blockchain Stack, giving simple but fully coded, step by step examples of how each of the element work both independently and collaboratively. This is the second post in the series (1st: Blockchain stack #0: Distributed stack vs LAMP) and will show you how to install Ethereum, mine some Ether and create your first contract.

There’s three key things I’d say to newbies about Ethereum out of the blocks:

  1. It’s really really fast moving. This means a lot of the tutorials you will try and follow will not quite work. A self-help-first attitude will really help you knock down those barriers, but given that you are here, you already have that! 🙂
  2. There are some amazing people out there giving their time up to help. I’ve been particularly inspired by people on the gitter.com channels like Chris Hafey. Quite a few companies have slack channels and there’s usually a lot of help provided on them e.g. Griff Green (Griff Green) on Giveth.
  3. The open and asynchronous nature of Blockchain means that security is a key challenge. This makes things fun and exciting as it requires an additional layer onto the more typical hacking mindset. Manuel Aráoz has some good examples of this.

Use Linux

I think Ethereum and the rest of the stack is best being run from Linux (I use Ubuntu) because a lot of the examples I see out there are for this set up. This should make it easier to hunt for solutions and get advice and help. If you haven’t used Linux before, don’t worry, you’ll use the command line and only need to know a few commands.

It’s free and pretty easy to setup an Oracle VM using Ubuntu on Windows. When you do it, make sure you allocate at least 150GB of virtual hard drive because the Ethereum node will take a lot of this. Also install the guest additions. If you already have VM installed, you are probably like me and didn’t give it enough space so you’ll you need to resize the virtual drive.

All of the examples in this series of posts build on top of each other and started with the base Ubuntu 64bit install with Guest Additions.

Installing a Geth node

There are various client nodes that can be used, I went for Geth as this seems to be one of the most supported and, along with Parity, is recommended by the Ethereum Foundation.

Follow instructions here or just open your linux Terminal and run these commands:

sudo apt-get install software-properties-common
sudo add-apt-repository -y ppa:ethereum/ethereum
sudo apt-get update
sudo apt-get install ethereum

Check your install works:

cd ~
geth
I0210 11:31:14.966082 cmd/utils/flags.go:608] WARNING: No etherbase set and no accounts found as default
…
…
I0210 11:31:17.831461 p2p/server.go:608] Listening on [::]:30303
I0210 11:31:17.832291 node/node.go:341] IPC endpoint opened: /home/monkia/.ethereum/geth.ipc

Use Ctrl+C to exit.

The Networks

You can run the Geth client on various networks. A quick overview:

Let’s set up Testnet to sync and in the meantime we can set up Dev network to work on our first contract.

Setting up Testnet to sync

Open a new terminal:

geth --testnet --fast --cache=512 console

This will start Geth up in fast sync mode and it will start downloading the Blockchain. It has completed the sync when it has downloaded the latest header. You can check the latest header on the testnet here and then run this command in the geth console:

> web3.eth.blockNumber

Once it has finished downloading to the latest header (about 4 hours for me), we can start mining, but we will come back to that later.

There’s a good article here about the fast sync mode for Geth.

Setting up and mining in Dev

So that you can crack on, let’s setup a Dev network.

cd ~
geth --datadir ./.ethereum/devnet --dev console

You’ll see there are no blocks syncing:

>web3.eth.blockNumber
0

That’s it, Dev network is set up :-).

We now need to create an account and mine. We are going to use Geth’s Javascript Console using the functions defined here. Geth provides a wrapper for Ethereum’s web3 JavaScript Dapp API and the admin API.

> personal.newAccount()
Passphrase:
Repeat passphrase:
“0xfd8547ebc55618de2daa43fa78d683c81968c3a9”
> personal.listAccounts
[“0xfd8547ebc55618de2daa43fa78d683c81968c3a9”]
> exit

Now let’s start mining!

geth --datadir ./.ethereum/devnet --dev --mine --minerthreads 1 --etherbase 0

minethreads : how many CPU cores to use
etherbase : which account to send the Ether too (0 being first in the list from personal.listAccounts)

You can leave that running and you should start seeing Ether being mined.

Show me the money!

Leave that terminal running so that you keep mining Ether to use. Open a new terminal window. We are now going to connect to the client that is running through the miner.

geth --datadir ./.ethereum/devnet --dev attach ipc:./.ethereum/devnet/geth.ipc

Check how much Ether you have.

> eth.getBalance( eth.accounts[0] )
95000000000000000000

Unlock the account so that you can use it to make your first contract.

> personal.unlockAccount(eth.accounts[0])
Unlock account 0xfd8547ebc55618de2daa43fa78d683c81968c3a9
Passphrase:
true

At anytime you can stop the miner and then reconnect to Geth using the console flag. This starts both the node and the client.

geth --datadir ./.ethereum/devnet --dev console

Your first contract

Ok, now you’re rich, let’s make a contract.

We will use the Greeter from Ethereum. There has been a lot of changes and improvements since their tutorial was created so a number of the steps do not work, hopefully this will get you going fairly painlessly. No doubt that some point in the future someone will say the same about this one!

We will not use the command line compiler as it did not work at the time of writing this for the Greeter contract. I think more effort is going into maintaining the Ethereum browser compiler and it’s also got more features — so using that is a win win.

Greeter contract

pragma solidity ^0.4.9;
contract mortal {
 /* Define variable owner of the type address*/
 address owner;

 /* this function is executed at initialization and sets the owner of the contract */

function mortal() { owner = msg.sender; }
   /* Function to recover the funds on the contract */
   function kill() { if (msg.sender == owner) selfdestruct(owner); }
}

contract greeter is mortal {
 /* define variable greeting of the type string */
 string greeting;
 /* this runs when the contract is executed */
 function greeter(string _greeting) public {
    greeting = _greeting;
 }

 /* main function */
 function greet() constant returns (string) {
    return greeting;
 }
}

Copy this into the Ethereum browser compiler as shown here on the left.

Copy the text from the right titled ‘Web3 deploy’ to a text file (making sure you have word wrap turned off). You need to specify the text to use in the _greeting variable and Geth didn’t like the “untitled:” additions the editor added. Therefore in the text file, change the following lines:

var _greeting = /* var of type string here */ ;
var untitled:greeterContract = web3.eth.contract(…..);
var untitled:greeter = untitled:greeterContract.new(….

to

var _greeting = “hello world” ;
var greeterContract = web3.eth.contract(…..);
var greeter = greeterContract.new(

Copy and paste the text file to Geth and run it. Do the same for both the mortal and greeter code. You should see something like this (it can take up to a minute for the ‘mined’ response).

undefined
> null [object Object]
Contract mined! address: 0xecd2db305e2e6730101c76c2ee8d0b3113037c3b transactionHash: 0xf7c4272d8a8e15ba04110beda1c063946f981c8280eec6be656f589e165d44b6

You can now test if your first contract works:

> greeter.address
“0xecd2db305e2e6730101c76c2ee8d0b3113037c3b”
> greeter.greet();
“Hello world”

Anyone else on your Dev network can interact with your contract using this API:

var <name> = eth.contract(ABI).at(Address);

Where ABI is the ‘Interface’ field from the browser compiler and Address is the address of the contract you have just created (greeter.address).

Try opening a new terminal and doing the following, changing the address in the at(…) to the address of your contract:

geth — datadir ./.ethereum/devnet — dev attach ipc:./.ethereum/devnet/geth.ipc
> var greeterTest = eth.contract([{“constant”:false,”inputs”:[],”name”:”kill”,”outputs”:[],”payable”:false,”type”:”function”},{“constant”:true,”inputs”:[],”name”:”greet”,”outputs”:[{“name”:””,”type”:”string”}],”payable”:false,”type”:”function”},{“inputs”:[{“name”:”_greeting”,”type”:”string”}],”payable”:false,”type”:”constructor”}]).at(“0xecd2db305e2e6730101c76c2ee8d0b3113037c3b”);
> greeterTest.greet()
“Hello world”

That’s it. You’re done with your first contract. Congratulations!

Ether on testnet

Before you go, if testnet has finished its sync, why don’t you now go ahead and repeat all of this on that network? You can mine for Ether like we have done above or you can get it from a faucet provided by the lovely folks at ropsten.be. Don’t forget to set yourself up with a new account on your testnet (personal.newAccount()).

You can check the transaction, its block details and when it has been mined on the etherscan website. Enter the account number in the search box and you should see something like this:

Where to go for help related to part 1

General: https://gitter.im/ethereum/home
Geth: https://gitter.im/ethereum/go-ethereum
Solidity: https://gitter.im/ethereum/solidity
Various: http://ethereum.stackexchange.com
Various: https://www.reddit.com/r/ethereum/
Documentation: https://github.com/ethereum/

Thanks to André Hammen and Griff Green for reviewing.

Next in the Blockchain stack series

Next in the series is Creating your first ‘domain’ name on Ethereum using ENS (Blockchain stack #2)

Ziggify Blockchain

There’s a team of us that have come together at Ziggify to work on a Blockchain play. We are in the exploration phase, so if you want to get involved or have an idea please get in touch. I’m based in London and enjoy chewing the Blockchain fat over a cuppa or a beer.

ianmonk

https://twitter.com/IanMonk_uk

https://www.linkedin.com/in/ian-monk-uk/

Write a Comment

Comment

Webmentions

  • Creating your first ‘domain’ name on Ethereum using ENS (Blockchain stack #2) - Ziggify

    […] Check you have two accounts and they have ETH.  This post tells you how to get Ether on testnet. […]

  • Blockchain stack #0: Introduction of distributed stack vs LAMP - Ziggify

    […] #1: Getting started — Mining your first Ethereum and making your first contract […]

  • Your first Ethereum web page : Blockchain stack #3 - Ziggify

    […] web page is going to be an exciting experience for you!  So far you should have built your first smart contract, installed Parity for the Ethereum blockchain node, done a fast syn with Ropsten testnet and […]

  • Ethereum Parity fast sync for Ropsten testnet - Ziggify

    […] You wont see any ETH in your account until the DAG has been generated.  It took approx a day for me. This post tells you how to get Ether on testnet. […]