Monday, March 2, 2015

Develop Solidity Contracts on PoC-8



This is the fastest setup I could come up with that allows you to learn by doing.
This is for learning how to develop Ethereum contracts (not for developing Ethereum itself).

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



  • Add this to your ~/.bashrc file:
    export GOPATH=$HOME/go
    export PATH=$PATH:$GOPATH/bin
    export PATH=$PATH:/usr/local/go/bin
  • Close & re-open bash window.
  • Download the go installation file.
  • cd path-of-downloaded-file
    sudo tar -C /usr/local -xzf downloaded-file
  • sudo add-apt-repository -y ppa:mercurial-ppa/releases
    sudo apt-get install -y mercurial g++
    libgmp-dev libreadline-dev
  • go get github.com/tools/godep
    go get github.com/ethereum/go-ethereum/cmd/ethereum

    cd ~/go/src/github.com/ethereum/go-ethereum
    git checkout develop
    godep go install -v ./cmd/ethereum
  • ethereum account new
    (and enter a passphrase)
  • Take a snapshot of the VM. Name it: "go-ethereum develop branch".

    • Take a snapshot of the VM. Name it: "chrome browser". 

    • Install ethereum.js, this is necessary to make your JavaScript communicate with your contracts.
    • Take a snapshot of the VM. Name it: "ethereum.js".


    • 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
      chmod 755 xampp-filename
      sudo ./xampp-
      filename
      Accept all defaults when installing XAMPP.
    • 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 .
       
    • Take a snapshot of the VM. Name it: "xampp web server".

    • Get the files that will be served up:
      mkdir -p ~/src && cd ~/src

      mkdir -p ~/src/gists && cd ~/src/gists


      git clone https://gist.github.com/2b57ad41419908cf6b8c.git

      git clone https://gist.github.com/a9b10d3c58f7f434c0f1.git


      sudo mkdir -p /opt/lampp/htdocs/dev


      npm install bignumber.js
      sudo cp -R
      ~/node_modules/bignumber.js/*.js
      /opt/lampp/htdocs/dev
      npm install ethereum.js
      sudo cp ~/node_modules/ethereum.js/dist/*.js /opt/lampp/htdocs/dev

      sudo cp ~/src/gists/2b57ad41419908cf6b8c/balance.html /opt/lampp/htdocs/dev

      sudo cp ~/src
      /gists/a9b10d3c58f7f434c0f1/contract.html /opt/lampp/htdocs/dev 
    • Take a snapshot of the VM. Name it: "files to be served". 

    • sudo add-apt-repository -y ppa:ethereum/ethereum-qt
    • Build the develop branch of cpp-ethereum -- while following the build instructions, if any issues crop up (such as packages held back, unable to upgrade, etc), then address them first before proceeding.
    • sudo ln -s ~/src/cpp-ethereum/build/eth/eth /usr/bin/eth
    • sudo ln -s ~/src/cpp-ethereum/build/alethzero/alethzero /usr/bin/alethzero
    • Take a snapshot of the VM. Name it: "build cpp-ethereum develop branch".


    • Install cpp-ethereum from the Ubuntu PPA (so you can use solc, the Solidity compiler):
      sudo add-apt-repository -y ppa:ethereum/ethereum-qt
      sudo add-apt-repository -y ppa:ethereum/ethereum

      sudo apt-get dist-upgrade -y &&
      sudo apt-get update -y
      sudo apt-get install -y cpp-ethereum
    • Take a snapshot of the VM. Name it: "cpp-ethereum ppa".


    • From the Terminal, run the GUI client:
      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: "alethzero settings". 

    • wget https://bootstrap.pypa.io/ez_setup.py -O - | sudo python
    • git clone https://github.com/ethereum/pyethereum.git
    • cd pyethereum && python setup.py install


    Launch the tools that allow us to start developing smart contracts

    • Close all programs and re-boot the VM.
      (The web server automatically launches at boot-up.)
    • From a Terminal window, run:
      alethzero
      Minimize AlethZero to put it in the background.
    • From a Terminal window, run:
      ethereum account(and copy the address that appears in curly braces -- edit the address by removing every "0x" that occurs prior to each two-digit hex value. So, for example, the address "0x7f0xe00x01..." needs to be changed to "7fe001...")
    • ethereum --mine --rpc --unlock=address:password
      Wait for it to generate DAG -- this takes a long time.
      DAG generation is done when it displays messages: "Hash rate: x Khash", where x is non-zero -- this also takes a long time.
    • Launch the Chrome browser and go to:
      http://localhost/dev/balance.html
    • Click the button "watch balance"
      If it displays a non-zero balance, then you have successfully communicated from a JavaScript program to a contract on Ethereum!
    • Continue with steps 5 through 8 in this forum posting here -- but remember that you must wait at least 12 seconds after loading each contract before you can interact with it.

      To compile solidity code, you can either use the web-based compiler you bookmarked earlier or you can call solc as follows:
      solc --input-file mySource.sol --json-abi file --binary file
      (two files will be created that contain the abi and binary info respectively.)

      Every time you create a contract from JavaScript you must: (1) Confirm a popup window from AlethZero that asks you to "Reject" or "Allow" the contract -- click on "Allow". (3) Open AlethZero and note the first few characters of the pending contract. (4) In AlethZero, click "Mine" then click "Go" and wait for the pending contract to become a contract. (5) Go back to the Chrome browser and continue to interact with the contract.

    Sunday, March 1, 2015

    How to build cpp-ethereum PoC-8

    The instructions at this page:
    https://github.com/ethereum/cpp-ethereum/wiki/Building-on-Ubuntu
    are excellent, but are missing a few steps.

    Before you begin, create a VirtualBox VM with Ubuntu 14.04.1 installed in it.
    Before you begin take a snapshot of the VM. Name it: "Before poc-8".

    I've shortened the instructions by only describing how to install on 64-bit Ubuntu (the whole process took me more than an hour):

    sudo apt-get -y update && sudo apt-get -y dist-upgrade
    sudo apt-get -y install language-pack-en-base

    sudo dpkg-reconfigure locales
    sudo apt-get -y install software-properties-common

    wget -O - http://llvm.org/apt/llvm-snapshot.gpg.key | sudo apt-key add -

    sudo add-apt-repository -y "deb http://llvm.org/apt/trusty/ llvm-toolchain-trusty-3.5-binaries main"

    sudo add-apt-repository -y ppa:ethereum/ethereum
    sudo add-apt-repository -y ppa:ethereum/ethereum-dev

    sudo apt-get -y update

    got a signature verification error, so "some index files failed to download"

    sudo apt-get -y dist-upgrade

    sudo apt-get -y install build-essential g++-4.8 git cmake libboost-all-dev automake unzip libgmp-dev libtool libleveldb-dev yasm libminiupnpc-dev libreadline-dev scons libncurses5-dev libcurl4-openssl-dev wget qtbase5-dev qt5-default qtdeclarative5-dev libqt5webkit5-dev libcryptopp-dev libjson-rpc-cpp-dev libmicrohttpd-dev libjsoncpp-dev libargtable2-dev clang-3.5 lldb-3.5

    sudo apt-get -y install build-essential g++-4.8 git cmake libboost-all-dev automake unzip libgmp-dev libtool libleveldb-dev yasm libminiupnpc-dev libreadline-dev scons libncurses5-dev libcurl4-openssl-dev wget libjsoncpp-dev libargtable2-dev libcryptopp-dev libjson-rpc-cpp-dev libmicrohttpd-dev clang-3.5 lldb-3.5


    Download the latest Qt:

    mkdir -p ~/src && cd ~/src

    wget http://download.qt-project.org/official_releases/online_installers/qt-opensource-linux-x64-online.run

    chmod +x qt-opensource-linux-x64-online.run


    For the following command, accept the installation defaults.
    (This took more than ten minutes to complete.)

    sudo ./qt-opensource-linux-x64-online.run

    Add the following to ~/.bashrc (but replace the yellow highlight with the correct folder name):

    export LD_LIBRARY_PATH=$QT_BASE_DIR/lib/x86_64-linux-gnu:$QT_BASE_DIR/lib:$LD_LIBRARY_PATH
    export PKG_CONFIG_PATH=$QT_BASE_DIR/lib/pkgconfig:$PKG_CONFIG_PATH

    export CMAKE_PREFIX_PATH=~/Qt/5.4/gcc_64

    Then close and re-open your Terminal window.


    Build JSON-RPC library:
    mkdir -p ~/src
    cd ~/src
    git clone https://github.com/cinemast/libjson-rpc-cpp
    cd libjson-rpc-cpp && git checkout tags/v0.3.2
    mkdir -p build && cd build && cmake .. && make
    sudo make install
    sudo ldconfig


    Get the ethereum cpp source code and build it:
    cd ~/src && git clone https://github.com/ethereum/cpp-ethereum
    cd cpp-ethereum && mkdir -p build

    Execute the following line any time you want switch to another PoC build, just replace the yellow highlight with the appropriate tag ID (call  git tag  to list all IDs):
    cd ~/src/cpp-ethereum/build && git checkout poc-8-tag && cmake .. && make -j2

    While building neth, I got these errors:
    npm ERR! fetch failed https://registry.npmjs.org/win-release/-/win-release-1.0.0.tgz
    npm ERR! fetch failed https://registry.npmjs.org/minimatch/-/minimatch-1.0.0.tgz



    Take a snapshot of the VM. Name it "cpp-ethereum poc8".


    Now you can call "alethzero" to run the GUI, or call "eth" for the command-line.
    Read here for the CLI (command-line interface) instructions.