Solidity and the Ethereum Virtual Machine

Welcome to the first decentralized global computational machine.

Over the past couple weeks I have been reading the Homestead release notes, the Solidity Documentation, watching youtube videos about Ethereum; really just an all out effort on trying to understand what this protocol is and why it is a technological successor to Bitcoin. In trying to explain it to others(or even write about it on here) I come to the notion that it is:

A global cryptographic, decentralized, immutable, permissionless world computer.

And in relation to the protocols of that which it is built on.

Internet = Communications

Bitcoin = Money

Ethereum = Computation

Without the Internet Bitcoin could have never existed and without Bitcoin, Ethereum could have never existed.

Now that’s a bold claim but I’m willing to make in that I truly believe that the predecessor technology has enabled this new paradigm in decentralized computing.

Bitcoin has and will continue to enable an unimaginable number of new and disruptive applications which will impact a number of verticals. I use my Shift card every day, wake up and check the price with an eye half open, and I anticipate my full attention will be back as we approach The Halvening. But right now, I want to learn how to write Smart Contracts in the Solidity programming language. I want to know how these contracts are compiled onto the Ethereum Virtual Machine (EVM). And how this is powering the next generation of peer-to-peer technology.

Installation and Setup

Download the latest Ethereum Wallet

Install Mix IDE, the IDE for authoring Smart Contracts using the High Level Language Solidity.

Program Solidity contracts on Visual Studio

You can also use the Online Compiler

The Ethereum Computer

The EVM is stack-based execution environment that uses Smart Contracts (similar to object-oriented classes) and HTML, CSS, and JavaScript to create dApps. When you are running a decentralized application (dApp), every instruction is executed on every node of the network. This means the operands for the instructions are taken from the stack, and it is also where the results are added. This is the low level assembly code of the EVM and the resulting flow-control functions that can be found in the Ethereum Yellow Paper. Items are pushed to the Stack and can be manipulated using POP (remove top item from the stack), SWAP (Swap item order on the stack / Limit of 16 elements), and DUP (copy and order new item on top of the stack).

Memory and Calldata

The stack also extends Memory and Calldata to be used in during program execution. Memory is an expandable byte-array used to store data during program execution. It can be accessed using the MSTORE and MLOAD instructions. Calldata is a byte-array, just like memory, but it is read-only. It contains the data from the transaction that triggered the code execution in the Contract.

Storage

Storage is a map used for fields in contracts. A contract can neither write nor read any storage other that its own. Essentially it is permanently storing the state variables within the contract.

Elements of the Ethereum Smart Contract

Contracts in Solidity are similar to classes in object-oriented languages. Each contract can contain declarations of:

  • State Variables
  • Functions
  • Function Modifiers
  • Events
  • Structs Types
  • Enum Types

with Parameters of:

  • The gas-price I want to pay (gasPrice).
  • The maximum amount of gas that may be spent (gas).
  • The amount of ether I want to transfer (value).
  • My account address (from).
  • The target account address (to).
  • The nonce (nonce).
  • The transaction data (data).

A smart contract’s code resides in a Contract Account. It is unalterable once deployed.

Accounts in Ethereum

There are two kinds of accounts on the Ethereum network: Externally Owned Accounts (Public-Private Key) and Contract Accounts.

Externally Owned Account (EOAs): an account controlled by a private key, and if you own the private key associated with the EOA you have the ability to send ether and messages from it.

  • Can have an Ether balance.
  • Can send transactions.
  • Are controlled by private keys.
  • Has no code.

Contract Accounts (CA): an account that has its own code, and is controlled by code.

  • Can have an Ether balance.
  • Can send transactions.
  • Can send messages.
  • Contracts are controlled by their contract code.
  • Only send transactions in response to other transactions that they have received. Therefore, all action on the Ethereum blockchain is set in motion by transactions fired from Externally Owned Accounts.
  • Every time a contract account receives a transaction its code activates, allowing it to read and write to internal storage and send other transactions/messages or create contracts.

EOA0 —-Transaction–> CA1 = Activate Code in CA1

CA1 —– Messages —> CA2, CA3, CA4= Perform functions in CA2, CA3, CA4

EOA1 —– Transaction —-> EOA2 = Send Ether EOA2

Ether

Ether, the currency used in Ethereum, is exchanged for computation on the platform. Gas is the name for the execution fee for every operation made on an Ethereum blockchain. Its price is expressed in ether and it’s decided by the miners, which can refuse to process transaction with less than a certain gas price. To get gas you simply need to add ether to your account. The Ethereum client automatically converts Ether to gas and gas to Ether when transactions are processed.

solid

When a transaction is sent from an EOA to a CA, it is sent with input code and Ether. The input code and ether is then processed by the Contract Account, thereby activating itself and thus executes its code. A CA can then send a message out to other (CA) contracts.The contracts are essentially talking and passing messages between themselves, in this way, sending a message is exactly like calling a function. This could produce another smart contract, send Ether to another address, register a vote of confidence, open a door to a house; virtually any sort of call to or update to the state of the decentralized network.

The total cost of a transaction is based on 2 factors:

  • gasUsed is the total gas that is consumed by the transaction
  • gasPrice price (in ether) of one unit of gas specified in the transaction

Total cost = gasUsed * gasPrice

An EOA can also send a transaction to another EOA and nothing happens accept transfer some Ether (P2P payment or ether).

Solidity

Visit the Solidity website to see example Smart Contracts and write and edit them within your wallet. You will be able to use Electron by Shapeshift to deposit bitcoin in exchange for Ether so you pay the mining fee and begin deploying your first smart contracts.

Start with the following three example smart contracts.

Create a cryptocurrency

Crowdfund your idea

Create a DAO

A new block is created every 15 seconds in Ethereum. Essentially you are sending binary data to invoke state transition rules through EVM bytecode at the assembly level thus updating the distributed state of the blockchain; AKA deploying your Smart Contract.

Mining Ether = Securing the Network = Verifying Computation on the Global Computer

Next: 3 RaspPi3’s, 32GB SD cards each a running the Ethereum computer (Web3.0, next-generation peer-to-peer technology platform) running on each…

Final Thoughts

Watch Ethereum for Dummies – Dr. Gavin Wood

A global cryptographic, decentralized, immutable, permissionless world computer.

Internet = Communications

Bitcoin = Finance

Etheruem = Computation

https://gateway.ipfs.io/ipfs/QmSbS6tk4GG4k2yEPWaYXBZquUxV1uxjmMsQyZTbM5Duyk

UPDATE:

This thing is AWESOME! I have been deploying contracts, sending transactions, and it’s all (near) real time. I can create a couple wallets and a couple cryptocurrencies and exchange them back and forth all confirming within 12 confirmations < a minute or 2. It’s pretty wild to keep the network stats up on on one screen the Wallet on the other and see them contracts confirm. Working on the front end now, possibly Salesforce integration + React.

Sources:

Ethereum White Paper

Ethereum Yellow Paper

Ethereum.Org

https://media.readthedocs.org/pdf/ethereum-homestead/latest/ethereum-homestead.pdf                 

https://media.readthedocs.org/pdf/solidity/latest/solidity.pdf

https://www.ethereum.org/assets

https://github.com/androlo/solidity-workshop

https://ethstats.net/

One thought on “Solidity and the Ethereum Virtual Machine

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s