Getting started with ethereum

Ethereum is a blockchain platform similar to Bitcoin that allows you to  create smart contracts, distributed applications, virtual currencies and more. I've read a lot about it but to me a line of code says more than a thousand words. Well, maybe not that much but it really helps me to get a grasp on things.

There's a sort of a "Hello World" tutorial on ethereum.org but when I tried to follow the steps I found that it was missing some steps and made some confusing side steps. If you run into errors like "TypeError: 'greet' is not a function", some of my hints below might help.

prerequisites

I'm working on ubuntu so the instructions below mostly apply there and similar debian-like systems. If you're on Windows or OSX, it shouldn't be too hard to install the required dependencies, I assume they're wel documented for those platforms.

You need geth (this is the Go ethereum implementation) and solc (this is a compiler you need to compile smart contracts written in solidity). On Ubuntu (14.04), you can get these as follows.

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

Network setup

You will run your smart contracts on a network where miners will execute your smart contracts. In a production scenario this would be the actual Ethereum network but while testing/experimenting it's better to use a testnet:

  • it won't cost you actual ether
  • you have more control over the execution 
  • execution may be faster (not sure about this)

There's a global testnet that you can join (geth --testnet) but you can also run your completely standalone private testnet. I prefer the latter: it's easier to control, you control the mining of your contracts and you don't need to start with downloading the multi-gigabyte testnet blockchain.

To create your test network, start by creating a genesis block which is just a json file. The "geth init" command might be of use here but I can't figure out how it works (and many with me) so let's do it by hand. Create a separate directory for it and create a genesis.json block in it:

$ mkdir mytestnet
$ cat > genesis.json
{
"nonce": "0xc0ffeebabe000042",
"timestamp": "0x0",
"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"extraData": "0x0",
"gasLimit": "0x8000000",
"difficulty": "0x400",
"mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"coinbase": "0x3333333333333333333333333333333333333333",
"alloc": {}
}
$ geth --datadir mytestnet init mytestnet/genesis.json
I0912 12:49:10.919528 cmd/geth/main.go:299] successfully wrote genesis block and/or chain rule set: 5b9e70cd24c4e39b49ba6301066d1f7c5a81bc6bf72f32cc18f4ea13d02afec7

the 'nonce' defines the uniqueness of your network, make it something nice and random. We'll also use the 'alloc' section later to give us some ether later.

Starting the network

Now it's time to start the network.

$ geth --datadir mytestnet --networkid 123123123 --ipcpath ~/.ethereum/geth.ipc console

The networkid is also a random number that makes your network unique. Make sure it's the same with each invocation though.

Also make sure you point to the datadir you created (mytestnet) and point to the standard ipc socket. The latter makes it easier for commands (like the Ethereum Wallet and "geth console") to connect to your node.

This network will not be mining by default which is important to keep in mind.

Create an account and allocate coins

On the geth console, create an account as follows

> personal.newAccount('some pass phrase')
"0xe02887918e205144967a1e3e70d5a50000000000" 

 The hexcode will be your accountid and 'some pass phrase' will be your passphrase (duh!).

Now exit/terminate geth (control-D on linux) and add your account to mytestnet/genesis.json so that the alloc part looks as follows

"alloc": { "0xe02887918e205144967a1e3e70d5a50000000000": { "balance": "20000000000000000000" } } 

 Of course make sure you replace the 0x... id with the one just genreted. Now start geth again as described earlier and start mining. This, by itself, will probably already generate enough coins to do things.

> miner.start()

The first time you do this geth will start building a DAG. This is a one-time thing and once it's complete you can stop mining using

> miner.stop()

if you get annoyed (like me) by the noisy output and CPU fan blowing. I always thought "DAG" refered to a Directed Acyclic Graph which it indirectly does but it's primariliy named after the mining algorithm, Dagger Hashimoto.

Run the tutorial

At this point you should be able to actually run the steps in the tutorial. Just don't for get to start the miner again to execute the smart contract. I'm not going to repeat the entire tutorial here, just the individual steps.

 

First of all, and this is a step I was missing from the tutorial, make sure your account is unlocked:

> personal.unlockAccount("0xe02887918e205144967a1e3e70d5a50000000000", "some pass phrase")

Then execute the contract (details are in the original tutorial)

var greeterSource = 'contract mortal { address owner; function mortal() { owner = msg.sender; } function kill() { if (msg.sender == owner) suicide(owner); } } contract greeter is mortal { string greeting; function greeter(string _greeting) public { greeting = _greeting; } function greet() constant returns (string) { return greeting; } }'

var greeterCompiled = web3.eth.compile.solidity(greeterSource)

var _greeting = "Hello World!"
var greeterContract = web3.eth.contract(greeterCompiled.greeter.info.abiDefinition);
var greeter = greeterContract.new(_greeting,{from:web3.eth.accounts[0], data: greeterCompiled.greeter.code, gas: 300000}, function(e, contract){
     if(!e) {
       if(!contract.address) {
console.log("Contract transaction send: TransactionHash: " + contract.transactionHash + " waiting to be mined...");
} else { console.log("Contract mined! Address: " + contract.address); console.log(contract); } }
})

You should see the contract being created.

I0514 10:18:58.186877 eth/api.go:1178] Tx(0x4f44e81ea8d4774d075f5ebc324f9a9b2deb2e33adabf05433243349c040bdde) created: 0xf0247048158cb0ef92cb6c173acacf0e9fd00e87
Contract transaction send: TransactionHash: 0x4f44e81ea8d4774d075f5ebc324f9a9b2deb2e33adabf05433243349c040bdde waiting to be mined...

If not you may have forgotten to unlock your account. Start mining to get it executed. 

> miner.start()

Eventually you should see something like

Contract mined! Address: 0xf0247048158cb0ef92cb6c173acacf0e9fd00e87

at which point you can stop mining again.

Now you can get the result from your contract / dapp:

> greeter.greet()
'Hello World'

That's it!

I plan to write a second blog soon about the different options for integrating ethereum with python and plan to have a look at solidity, serpent, pyethereum and ethjsonrpc

 

Last updated Sept. 12, 2016, 1:03 p.m. | blockchain solidity smart-contract ethereum geth
comments powered by Disqus