GoVertical presents

Blockchain Startup Ideation Workshop

Hosted by TiE Seattle & Madrona Venture Labs

Getting Started with Ethereum

Goals

  1. Create a private Ethereum blockchain network with 2 nodes
  2. Create a simple SmartContract with the Solidity IDE
  3. Start and observe that the mining process is working on both nodes

Getting started

Install Geth. Geth is the Go language Ethereum CLI client and is used to interact with Ethereum networks. 

INSTALL GETH

Create a private Ethereum blockchain network

For development purposes, you can set up a "private" Ethereum blockchain that is separate from the main Ethereum blockchain. A private network is useful for testing out distributed applications without needing to use real ether. This tutorial assumes you are on an unix computer.

  1. Create an Ethereum directory where you will store your local blockchain

    mkdir ethereum

  2. Create an environment variable called ‘ethereum_home’ in your .bash_profile file in your home directory.  ‘Ethereum_home’ should point to the directory where you are storing your ethereum dapp folders:

    echo 'export ethereum_home=/Path_to_folder/ethereum' >> ~/.bash_profile

  3. In your ethereum directory create a new file genesis.json, paste the below code and save. A genesis block is required for a private network.  This defines the properties of your blockchain. The difficulty property determines how long it will take for miners to mine blocks. I recommend setting it to something low

    {

    "config": {

    "chainId": 1234,

    "homesteadBlock": 0,

    "eip155Block": 0,

    "eip158Block": 0

    },

    "difficulty": "2",

    "gasLimit": "0x8000000",

    "alloc": {}

    }

  4. Initialize a new blockchain network with the genesis block and create the first node. Create a directory in your ‘ethereum_home’ home path for your first node (e.g. ‘chain1’).  In a console window type the following:

    geth --datadir "$ethereum_home/chain1" init "$ethereum_home/genesis.json"

  5. Start your node. Note that each node runs on its own port so if you run multiple nodes on your development machine they each need a unique port. Also you need to include the networkid flag if you want to pair nodes.

    geth --datadir="$ethereum_home/chain1" --ipcdisable --port 30301 --networkid 15 console 2>> console.log

  6. If everything went well you should now be in the geth shell with a chevron (>) prompt

  7. Accounts are a fundamental concept in Ethereum.  Create a new account, so you can mine blocks, send/receive ether, etc:

    personal.newAccount().

    Hint: to know which accounts already exist, type personal.listAccounts

  8. Now Create a second node on the same network. Create a second directory in your ‘ethereum_home’ path.  Then, open a new console window and enter the following to initialize the node:

    geth --datadir "$ethereum_home/chain2" init "$ethereum_home/genesis.json"

  9. Start your second node. The second node must have the same ‘networkid’ as the first node for the nodes to be able to communicate.  Also note that the port for the second node needs to be different than the first node (here 30301 for node1 and and 30302 for node2):

    geth --datadir="$ethereum_home/chain2" --ipcdisable --port 30302 --networkid 15 console 2>> console.log

  10. Now connect the nodes. In the second node instance type admin.nodeInfo and copy the ‘enode’ url from the console output. Then in the first node run the following:

    admin.addPeer(‘enode’ url from above)

  11. Start mining blocks (blocks will be empty at first).

    miner.start()

  12. Stop mining in both consoles

    miner.stop()

  13. Check that the number of blocks is the same in both consoles.

    eth.blockNumber

    If the nodes are correctly paired they should show the same number of blocks.

Create a smart contract

First you will need to install the Solidity IDE (which runs in a browser) to write and compile Ethereum contracts.

  1. Go to https://github.com/ethereum/browser-solidity/tree/gh-pages and download the zip file. Open index.html to run a local copy of Solidity.

  2. Create your first SmartContract:  Click the ‘+’ icon in the top left corner of the Solidity IDE to create an empty file.  Double click the new file tab title (‘Untitled.sol’) and rename it ‘greeter.sol’. Paste the following simple SmartContract code into the text editor:

    pragma solidity ^0.4.0;

    contract greeter {

    string greeting;

    function greeter(string _greeting) public {

    greeting = _greeting;

    }

    function greet() constant returns (string){

    return greeting;

    }

    function setGreeting(string _greeting) public {

    greeting = _greeting;

    }

    }

  3. Now deploy an instance of the contract to a node in your local blockchain. Stop your first node by typing ‘exit’ at the chevron (‘>’) and restart it with the correct RPC (remote procedure call) flags.

    geth --datadir="$ethereum_home/chain1" --ipcdisable --port 30301 --rpc --rpcport 8545 --rpccorsdomain "*" --networkid 15 console 2>> console.log

  4. At the geth chevron prompt, unlock your account with the following:

    personal.unlockAccount(eth.accounts[0])

  5. Now deploy the contract: Go back to the Solidity IDE in your browser and under the ‘Contract’ tab change the ‘Environment’ selector to ‘Web3 Provider’. A prompt will ask you for the Web3 Provider endpoint.

    Type http://localhost:8545

    Note that this matches the geth parameter (--rpcport 8545) from above.

  6. Now deploy the simple SmartContract. This will deploy the contract to the blockchain in the form of a transaction.  In Solidity IDE, under the ‘contract’ tab, find the ‘Create’ button that’s attached to a text field.  In that text field type a greeting message like “hi, joe” and click the Create button.  This will call the constructor in the SmartContract ‘greeter’ code with the parameter “hi, joe”.  You should see a message in the Solidity IDE that the transaction is waiting to be mined.

  7. Go back to your consoles and and start the mining process in both of your nodes.

    miner.start()

  8. Return to Solidity and wait until you see the message change from “transaction is waiting to be mined” to a record of the transaction. You have now deployed the Smart Contract. The deployed contract has an address (location on blockchain) and an interface, which allows you to interact with it.

  9. You can now stop mining. Go to the Geth CLI and type:

    miner.stop()

    Questions? Send us a note!

Ⓒ 2017 TiE Seattle and Madrona Venture Labs. Contact us