Wednesday, February 25, 2015

How to do Ethereum Contract Development using the PoC-7 VMWare Image

Please use the instructions at this page instead -- I was never able to get a return value from a contract using the setup described below.


These are my instructions for a fast setup of an environment that allows you to learn by doing without explaining what's under the hood -- in essence I'm trying to do the Ethereum equivalent of letting people drive cars without having to learn how an internal combustion engine works.

So, this setup is intended for people who want to develop Ethereum contracts (not for developing Ethereum itself).
Please let me know if there are any mistakes, missing information or dead links.

First, download the VMWare image from here


magnet:?xt=urn:btih:524badd3fc667452dde3e7a72f67b86d261aaae8&dn=Ethereum%20VM&tr=udp%3A%2F%2Fopen.demonii.com%3A1337%2Fannounce

Also, you need to download the appropriate VMWare software:

While those are downloading here's some info you may be interested in

  • Once I set up the VMWare image, I tried to upgrade Ubuntu which resulted in the VM being unable to boot afterwards -- I recommend never upgrading Ubuntu in this VMWare image.
  • I tried to load the VMWare image into VirtualBox without success (it resulted in an Ubuntu VM with no Ethereum software installed) -- I'm avoiding using this in VirtualBox until Ethereum releases a more stable system.

Set up the VM -- you only need to perform these steps one time

  • Take a snapshot of the VM. Name it: "Before setting up contract dev".
  • Launch the VM. (the VM password is "password".)

  • Modify  keyboard so that "Text entry settings" are for US instead of UK -- otherwise the double-quote and pipe characters will be mapped to the wrong keys.


  • WARNING: Do not install the Mist browser because it breaks AlethZero.
    Install the Mist browser -- you will use Mist for uploading contracts. Included with Mist is the "ethereum" command which supports JSON-RPC for the JavaScript/HTML GUI that runs in Chrome.


  • Install a web server (so we can access the go-ethereum clients):
    (Substitute yellow highlights with appropriate info.)

    Download XAMPP web server from here.

    cd path-where-xampp-was-downloaded
    (In my case I downloaded: xampp-linux-x64-5.6.3-0-installer.run)
    chmod 755 xampp-linux-x64-5.6.3-0-installer.run
    sudo ./xampp-linux-x64-5.6.3-0-installer.run

    Accept all defaults when installing XAMPP,
    but at the very end do not launch XAMPP.

    Get the files that will be served up:
    mkdir -p ~/src && cd ~/src
    git clone https://github.com/MikeMcl/bignumber.js.git
    git clone https://github.com/ethereum/ethereum.js.git
    git clone https://gist.github.com/2b57ad41419908cf6b8c.git
    git clone https://gist.github.com/a9b10d3c58f7f434c0f1.git
    sudo mkdir -p /opt/lampp/htdocs/dev
    sudo cp -R ~/src/bignumber.js/* /opt/lampp/htdocs/dev
    sudo cp ~/src/ethereum.js/dist/* /opt/lampp/htdocs/dev
    sudo cp ~/src/2b57ad41419908cf6b8c/balance.html /opt/lampp/htdocs/dev
    sudo cp ~/src/a9b10d3c58f7f434c0f1/contract.html /opt/lampp/htdocs/dev


    Make the web server launch at boot-up every time:
    sudo ln -s /opt/lampp/lampp /etc/init.d/lampp
    sudo update-rc.d lampp start 80 2 3 4 5 . stop 30 0 1 6 .


    Open Chrome browser and bookmark these two locations:
    http://localhost/dev/balance.html
    http://localhost/dev/contract.html


  • On the desktop, double-click on "Link to alethzero".
  • Make sure this menu item is check-marked:
    Debug > Force Mining
  • Also, make sure this menu item is check-marked:
    Debug > Use Private Chain...
    (Enter the name for your private chain as "PrivateChain".)
  • Exit Ubuntu and take a snapshot of the VM. Name it: "Local Testnet".
  • From now on, every time you launch AlethZero these settings should already be set this way.

  • Take a snapshot of the VM. Name it: "Contract dev environment". 
Note: A lot of these setup details came from a forum posting by drcode.

Launch the tools that allow us to start developing smart contracts

  • Optional: To delete a previous blockchain, type this into a terminal window:
    rm -rf /home/ethereum/.ethereum
  • From a Terminal window, run:
    ethereum
    (Note: You can run Mist (misted) or the "ethereum" app (ethereum), but you can not run both at the same time.)
  • Launch the Chrome browser and go to:
    http://127.0.1.1/balance.html
  • Click the button "watch balance" (NOT WORKING)


  • Double-click the desktop icon called "Link to alethzero".
  • To compile solidity code:
    cd
    ~/src/cpp-ethereum/build/solc && ./solc

Mine some fake Ether

  • After launching AlethZero, click the "Mine" button so it's enabled (depressed).
  • Then click the "Go" button.
    (If at any time the "Mine" button does not do anything, then also click the "Go" button.)
  • Once you've got about 15,000 Finney, click the "Mine" button again so it's disabled.
  • Basically we only mine when we need to get some currency units to run tests or to get a transaction (including contract submission and execution) to go through. Otherwise we keep mining disabled so we don't waste CPU cycles.

Send Ether from one account to another

  • Choose menu item: Tools > New Address
    (You should see a new address appear on the left side in "Owned Accounts".)
  • To copy the new address into your copy buffer double-click on it -- yeah, I know that's not standard behavior.
  • Then paste the address into the "To" field.
  • Enter an Amount that's less than what you have -- remember to set the units: the smallest units are at the top of the list.
  • Delete all info from the "Data" field.
  • Click "Execute".
  • Click on the "Mine" button so it enables mining.
    Wait for the transaction to disappear from "Pending".
  • Wait some more for an amount to appear at the new address -- where it used to say "0 wei" it should now show the amount you sent.
  • Click on the "Mine" button so it disables mining.


Manually load a contract

  • For now I only load Serpent contracts because some fundamental aspects Solidity didn't work at all when I tried it last time (on PoC-7 & PoC-8).
  • Paste the Serpent code into the "Data" field in the upper-left and click the "Execute" button.
  • Make absolutely certain that the indentations are perfect when using Serpent. Do not mix tabs and spaces (I only use spaces). And each level of indentation must be the same throughout the code. When I pasted code it usually messed up the spacing so I always had to re-edit the code.
  • Here's the sample code I tested:
def code():
    return(msg.data[0]*2)
  • You will always see the message "LLL Parse error" -- ignore it.
    As long as you see compiled code below the LLL error then it compiled correctly.
  • Remember that you cannot submit a contract unless the "To" field is blank.
  • Make sure the "Mine" button is still disabled, then press "Execute".
  • On the right side you will see your contract appear in the "Pending" window. Make note of the first few characters of the contract address.
    For example, mine shows: 5e70af27... +> 7790f06c...
    So, in my example the contract address begins with "7790".
    When you get lots of contracts loaded it will be important to write this info so you can keep things straight.
  • Click on the "Mine" button so it enables mining.
    Wait for the contract to disappear from "Pending" and appear in "Contracts".
  • Click on the "Mine" button so it disables mining.

Manually call the contract

  • Copy the address of the contract by double-clicking on the contract address on the right side.
  • Paste the address into the "To" field.
  • Delete the code from the "Data" field and replace it with the parameters you wish to pass to the contract, one parameter per line.
    In our example contract it only takes one parameter, so enter an integer (Ethereum does not currently handle floating point numbers by the way).
    Also, if you have a parameter that is an Ethereum address, you must put "0x" before the address for it to understand it properly.
  • To call the contract, press the "Execute" button -- if the Execute button is disabled, simply set "Amount" to zero and the button should re-enable (I wasted many hours figuring that one out).
  • You will again see the calling message appear in the "Pending" section.
  • Click on the "Mine" button so it enables mining.
    Wait for the message to disappear from "Pending".
  • Click on the "Mine" button so it disables mining.
  • Note that we don't have any way of retrieving the return value when calling manually. For that we need to call from JavaScript.

Call the contract from JavaScript

  • This is a mystery to me. I've been trying to do this for months without success. I'll update this blog post once I get something to work.

Learn more


This blog post is intended to get you up and running without much explanation.
For a detailed explanation of what's really going on, be sure to check out KenK's three excellent tutorials:

Tutorial 1:
https://forum.ethereum.org/discussion/1634/tutorial-1-your-first-contract/p1

Tutorial 2:
https://forum.ethereum.org/discussion/1635/tutorial-2-rainbow-coin

Tutorial 3 (JavaScript API for Ethereum):
https://forum.ethereum.org/discussion/1636/tutorial-3-introduction-to-the-javascript-api

No comments:

Post a Comment