dourada

Explorations in Ethereum Part II

Blog Post created by dourada on Mar 22, 2017

As described in a previous article I spent a day taking a deep dive into ethereum.  This is the follow up post that will provide the details and outcome of the day.

 

Josh Mather wrote an excellent article last year explaining how to get ethereum up and running:   https://developer.vantiv.com/community/news-and-communications/blog/2016/08/05/explorations-in-ethereum.    My main deviation from Josh's info is that I installed Mist and leveraged it as a learning tool.  In addition I installed Mist on Windows 10 and ran into a few snags so I leveraged Josh's article to help me clear a few hurdles in the process.

 

Here are some links that I used to gather information and/or help me with the journey.  Thanks to all of you that wrote the below!

 

 

First a quick overview:

 

  • Ethereum currently uses Proof of work as its consensus algorithm.  There will be an attempt to switch to proof of stake at some point in the future.
  • contract development is in solidity that looks like javascript.
  • Many clients written in different languages:  c++, go, python, etc.
  • Proof of work for bitcoin is resource/power intensive and quickly morphed from CPU processing to GPU processing to ASIC miners and basically what was supposed to be a distributed experiment became consolidated.  Ethereum tried to solve the 'consolidated' issue using a proof of work algorithm that is supposed to be memory hard which means it is not as easy for ASIC miners to build custom chips to mine.
  • Proof of Stake -- a consensus mechanism where the next block is chosen in a mostly random way with the chance of being selected as proportaional to the accounts wealth (it's stake).  There are also issues with Proof of Stake mining but the goal is to completely remove the resource/power intensive mechanism of Proof of Work while still maintaining the security of the blockchain.

 

 

What did I actually do:

 

  • Download MIST.  I downloaded the windows installer version from the last link in the link(s) section above.  Remember I am installing on Windows 10, if you install on macOS or linux the file locations will be different.
  • After installation you will find that it installs to:  c:\program files\Mist
  • Run Mist (there should be an icon on your desktop).  Executing Mist will download/create an ethereum node on your computer and files will be located at the following locations (note the 'danie' is my user account on windows 10):
    • C:\Users\danie\AppData\Roaming

    • C:\Users\danie\AppData\Roaming\Mist

    • C:\Users\danie\AppData\Roaming\Ethereum

  • We want to create a private network so create a new directory:  dantest at:  C:\Users\danie\AppData\Roaming\Ethereum\dantest

  • Create a new file named:  DanCustomGenesis.json and place it at:  C:\Users\danie\AppData\Roaming\Ethereum\dantest

  • Add the following to DanCustomGenesis.json.  This is the genesis file that allows us to bootstrap the private network.

{
    "nonce": "0x0000000000000042",
    "timestamp": "0x0",
    "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
    "extraData": "0x0",
    "gasLimit": "0x8000000",
    "difficulty": "0x400",
    "mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000",
    "coinbase": "0x3333333333333333333333333333333333333333",
    "alloc": {
    }
}

 

  • I created an initialize.bat file and placed the following contents:

 

C:\Users\danie\AppData\Roaming\Mist\binaries\Geth\unpacked\geth --datadir "C:\\Users\\danie\\AppData\\Roaming\\Ethereum\\dannet" init "C:\\Users\\danie\\AppData\\Roaming\\Ethereum\\dannet\\DanCustomGenesis.json"
PAUSE

 

  • Execute the initialize.bat and you should see some output and geth and keystore directories should be created under the dannet folder.

 

 

  • Then I created a start.bat and placed the following:

 

C:\Users\danie\AppData\Roaming\Mist\binaries\Geth\unpacked\geth --networkid 1234 --identity dantestnode --verbosity 3 --nodiscover --nat none --datadir="C:\\Users\\danie\\AppData\\Roaming\\Ethereum\\dannet"
PAUSE

 

  • Execute start.bat and the last line should be IPC endpoint opened:  \\.\pipe\geth.ipc

 

 

  • Now that geth is running connect another instance by running (note to either put geth in your path and/or make sure you use the full path to geth).
geth attach 
  • you should see welcome to the geth javascript console

 

  • Now we need to create a new account and set this account as the miner etherbase account so that we can start the miner and generate some eth.  The goal here is to generate enough eth to play around with accounts and to start writing contracts.
  • Type the following at the javascript command prompt.  If you were doing this in production take care to use a strong password and manage that password however you would manage any other production password.  Because this is a private test net I simply use the phrase:  password.

 

personal.newAccount("password")
miner.setEtherbase(personal.listAccounts[0])
miner.start()

 

  • You should see the miner start logging in your other console windows.  It might take a while for everything to startup so be patient.  As soon as you see a bunch of logging that means things are ready.

  • Type the following commands to see the balance of the primary account
eth.accounts
primary = eth.accounts[0]
balance = web3.fromWei(eth.getBalance(primary), "ether");

 

  • Now start Mist by clicking on the icon on your desktop.  When Mist launches it will attach to the currently running geth which means Mist will be our interface into the test network that we just created.  It will allow us to add accounts, transfer eth, and also build smart contracts.  When you start it there should be a 'Private-net' graphic on the startup screen as well as on the main Mist windows in the bottom left.

 

  • Now we're ready to write a contract.  Let's just copy paste the token example from the ethereum website:  https://www.ethereum.org/token.  Scroll down until you see the minimum viable token (it's about 19 lines of solidity code).  Copy that code, click over to Mist and click on the "Contracts" icon.  Click 'deploy new contract'.  Make sure the main account is selected and that it has an ether balance.  Scroll down a bit and paste the code into the "Solidity Contract Source Code" window.  Make sure to keep the 'pragma solidity 0.4.8;' line and paste the new code over the current contract that is already in the window.  This is the code I copied as the minimal viable token.

 

contract MyToken {
    /* This creates an array with all balances */
    mapping (address => uint256) public balanceOf;
    /* Initializes contract with initial supply tokens to the creator of the contract */
    function MyToken(
        uint256 initialSupply
        ) {
        balanceOf[msg.sender] = initialSupply;              // Give the creator all initial tokens
    }
    /* Send coins */
    function transfer(address _to, uint256 _value) {
        if (balanceOf[msg.sender] < _value) throw;           // Check if the sender has enough
        if (balanceOf[_to] + _value < balanceOf[_to]) throw; // Check for overflows
        balanceOf[msg.sender] -= _value;                     // Subtract from the sender
        balanceOf[_to] += _value;                            // Add the same to the recipient
    }
}

 

  • That should change the right side of the window to 'select contract to deploy'.  Select 'My Token' and enter 10000 into the initial supply text box.

 

 

  • Scroll down a bit further and you will see the 'deploy' button.  Go ahead and press it.  There should be another window popup, enter your password for the account you are creating the contract from and press the 'send transaction' button/link.  That sends the contract to the geth node and now the miner will validate the contract and make it available for use.

 

 

  • That should be the end of deployment but for some reason this does not work and gives me the following error:  " No data is deployed on the contract address!"  I googled around and it appears there are some errors with contracts and/or not enough gas was supplied.

 

  • I do not think either applies to my scenario so I "stole" some of the work @joshmather did to compile the contract via the remix UI provided in Mist.
  • In Mist click on Develop and 'Open Remix IDE', then paste in the contract code (above) in the left pane and your Remix IDE should look like the following.

  • I started at step 8 in Josh's article and performed 8, 9, 10.  But as indicated above I used the remix UI instead of online solidity compiler.
  • So using Josh's steps type this into the geth console:
personal.unlockAccount(web3.eth.accounts[0]);

 

  • This is the code you should see in the Web3 deploy section of Remix in the right pane.  We need to edit the initialSupply value.  Here you can see I set it to 10000 which means there will be 10000 tokens created.
  • The untitled_mytokenContract is the json interface.  We will use it later.
  • untitled_mytoken is the actual contract.  We will use it later.

 

var initialSupply = 10000 ;
var untitled_mytokenContract = web3.eth.contract([{"constant":true,"inputs":[{"name":"","type":"address"}],"name":"balanceOf","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transfer","outputs":[],"payable":false,"type":"function"},{"inputs":[{"name":"initialSupply","type":"uint256"}],"payable":false,"type":"constructor"}]);
var untitled_mytoken = untitled_mytokenContract.new(
   initialSupply,
   {
     from: web3.eth.accounts[0],
     data: '0x6060604052341561000c57fe5b60405160208061030a833981016040528080519060200190919050505b80600060003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020819055505b505b61028b8061007f6000396000f30060606040526000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806370a0823114610046578063a9059cbb14610090575bfe5b341561004e57fe5b61007a600480803573ffffffffffffffffffffffffffffffffffffffff169060200190919050506100cf565b6040518082815260200191505060405180910390f35b341561009857fe5b6100cd600480803573ffffffffffffffffffffffffffffffffffffffff169060200190919080359060200190919050506100e7565b005b60006020528060005260406000206000915090505481565b80600060003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054101561013357610000565b600060008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000205481600060008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020540110156101c057610000565b80600060003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206000828254039250508190555080600060008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600082825401925050819055505b50505600a165627a7a7230582097cdec5f88d4e9e72bdc2bdfaf26e619c61d250d294c98688f9f439b0450da480029',
     gas: '4700000'
   }, function (e, contract){
    console.log(e, contract);
    if (typeof contract.address !== 'undefined') {
         console.log('Contract mined! address: ' + contract.address + ' transactionHash: ' + contract.transactionHash);
    }
})

 

 

  • Now we can deploy the contract by copy pasting everything above into the geth console.  I am again on windows 10 and when I copy it in the console gets a bit wiggy and shows undefined, undefined, then some dots and basically looks like it's broken but if you have the miner mining you will see that the contract indeed is executed. And finally in the geth console you will see "Contract mined!" and the address.

 

 

  • Now you can execute the contract by typing untitled_mytoken.transfer(address, amountToTransfer);
  • Or, because I like seeing things I grabbed the address of the contract and the json interface, opened mist, selected contracts, watch contract, gave the contract a name, copied in the address received from above, and copied in the json interface.

 

 

 

  • Now I can interact with the contract from Mist which is what should have happened a few hundred steps above but hey, this is still an experiment.

 

 

  • What can you do with the contract?  Basically the address that deployed the contract now has 10,000 tokens and now that address can transfer tokens to other addresses and then when those addresses have tokens they can transfer them around.  Pretty crazy.

 

This ends our ethereum experiment.  There is so much more to learn and understand but hopefully this helps someone out there get started.  If you are experimenting with ethereum let us know what you are building in the comments and if you have any tips/feedback on the information above.  I would especially like to understand what I did to get the "No data is deployed on the contract address!" error.  But the great thing about errors like that is that it sends one down a different path and I was able to play with the Remix IDE.

 

Hopefully you enjoyed, if you have questions/comments please send them along below.  Happy blockchain'ing!

Outcomes