Saturday, February 24, 2018

Cardano - New Smart Contract Platform

Another promising Smart Contract Platform - Cardano.

This is similar to Ethereum, Cardano is a smart contract platform however, Cardano offers scalability and security through layered architecture.

Cardano was conceptualized by Charles Hoskinson one of the co-founders of Ethereum.
While Ethereum is good as a smart contract platform, according to Hoskinson it is a second generation blockchain (I.e. It still needs evolution in terms of Scalability, Interoperability, & Sustainability).

Cardano is unique in asense that it is built on scientific philosophy and peer-reviewed academic research. All the engineering that goes into it has the ultimate goal of being “High Assurance Code”. 

You can read Cardano principles directly from the Cardano website.
https://www.cardanohub.org/en/philosophy/
 

More details to be added .. soon...

Keep Blogging..

Arun Manglick

Sunday, February 18, 2018

Ethereum Blockchain

Overview:

Ethereum.. in easy words - It is an open-source, decentralized network that allows smart contracts to be deployed on a blockchain with the use of ether. It’s a blockchain app (Dapp) platform that uses gas to run – in short. The network was created by Vitalik Buterin in 2015 although the white paper came into existence two years earlier.

Ethereum Blockchan.. in easy words -It is a distributed database that is constantly growing and therefore adding a new block to the chain every 17 seconds on average. Each new block contains records such as transactions and contracts.

More Detailed....

Ethereum is an open source Blockchain Platform that lets anyone to build/use decentralized applications that run on blockchain technology. It is easy to create new applications on the Ethereum platform, and with the Homestead release, it is now safe for anyone to use those applications.

Like Bitcoin, no one controls or owns Ethereum – it is an open-source project built by many people around the world. But unlike the Bitcoin protocol, Ethereum was designed to be adaptable and flexible. 

Ethereum is a Programmable Blockchain. Rather than give users a set of pre-defined operations (e.g. bitcoin transactions), Ethereum allows users to create their own operations of any complexity they wish. In this way, it serves as a platform for many different types of decentralized blockchain applications, including but not just limited to crypto-currencies.

Ethereum in the narrow sense refers to a suite of protocols that define a platform for decentralized applications. Like any blockchain, Ethereum also includes a peer-to-peer network protocol.

At the heart of it is the Ethereum Virtual Machine (“EVM”), which can execute code of arbitrary algorithmic complexity. The Ethereum blockchain database is maintained and updated by many nodes connected to the network. Each and every node of the network runs the EVM (Ethereum Virtual Machine) and executes the same instructions.   

Developers can create applications that run on the EVM using friendly programming languages modelled on existing languages like JavaScript and Python.

Ethereum Blockchain
  • Ethereum is distributed Cloud-based platform
  • Ethereum Blockchain can be used for:
    • Personal Finance
    • Self Executing Contracts
    • Gambling Sites
  • Ether is the underlying crypto-currency which powers Ethereum Network
  • For every code run, you need to pay Ether

How Etherum Works

Operational Details:
Whereas the Bitcoin blockchain was purely a list of transactions, Ethereum’s basic unit is the account. The Ethereum blockchain tracks the state of every account, and all state transitions on the Ethereum blockchain are transfers of value and information between accounts.
  • Accounts play central role in 'Ethereum'
  • Accounts can be created using Mist or Command Line
  • Each account is defined by pair of keys - Private/Public Key-Pair
  • Type of accounts:
    • EOA - Externally Owned Account
      • Controlled By Private Key
      • Has Ether Balance
      • Can Send Transaction
      • Has No Code but Can Trigger Contract Code
    • Contract Account
      • Has Ether Balance
      • Has Code - Code Exectuion is triggered by Transaction or Messages recieved from other Contracts
      • Controlled by their Contract code and can only be “activated” by an EOA
      • Note: The popular term “smart contracts” refers to code in a Contract Account – programs that execute when a transaction is sent to that account. Users can create new contracts by deploying code to the blockchain.
So human users control EOAs - because they can control the private keys which give control over an EOA, which in-turn control Contract accounts (governed by their internal code).

Contract accounts only perform an operation when instructed to do so by an EOA. So it is not possible for a Contract account to be performing native operations like random number generation or API calls – it can do these things only if prompted by an EOA. This is because Ethereum requires nodes to be able to agree on the outcome of computation, which requires a guarantee of strictly deterministic execution.

Payment:
Like in Bitcoin, users must pay small transaction fees to the network. This protects the Ethereum blockchain from malicious computational tasks. The sender of a transaction must pay for each step of the “program” they activated, including computation and memory storage. These fees are paid in amounts of Ethereum’s native value-token, Ether.

Mining Blocks: Proof of Work
These transaction fees are collected by the nodes that validate the network. These “miners” are nodes in the Ethereum network that receive, propagate, verify, and execute transactions. The miners then group the transactions – which include many updates to the “state” of accounts in the Ethereum blockchain – into what are called “blocks”, and miners then compete with one another for their block to be the next one to be added to the blockchain. Miners are rewarded with Ether for each successful block they mine. This provides the economic incentive for people to dedicate hardware and electricity to the Ethereum network.

Just as in the Bitcoin network, miners are tasked with solving a complex mathematical problem in order to successfully “mine” a block. This is known as a “Proof of Work”.

Any computational problem that requires orders of magnitude more resources to solve algorithmically than it takes to verify the solution is a good candidate for proof of work. In order to discourage centralization due to the use of specialized hardware (e.g. ASICs) used for mining (used in the Bitcoin network), Ethereum chose a Memory-Hard Computational Problem.

If the problem requires memory as well as CPU, the ideal hardware is in fact the general computer. This makes Ethereum’s Proof of Work ASIC-resistant, allowing a more decentralized distribution of security than blockchains whose mining is dominated by specialized hardware, like Bitcoin.

Ethereum Ecosystem:
  • Meta-Mask
  • Infura
  • Mist
  • Blockchain Nodes
  • Solidity
  • Remix
  • Web3.js
  • Truffle
  • Geth (Go-Ethereum)
  • Webpack, Browserify, NPM
  • Angular, Vue.js

Smart Contracts:
  • Helps in transforming something of values in secured way. E.g. Property, Shares, Insurance etc.
  • Provide Speed and avoid Middle man.
  • Ethereum Smart Contracts
    • Are Code that can send data, send value/money, interact with other smart contracts etc.
    • Having Smart Contracts (Programs) to Blockchain enables - Logic & Currency running in the same system. 
    • Thus enable lot of opportunities to  utilize - Escrow, Insurance, Crowd-Funding, Real-Estate etc.
  • E.g. Selling-Purchasing a Home/Property Transaction
    • Handling thru Blockchain and paying thru Bitcoin
    • All receipts will be stored in Virtual Smart Contracts
    • These transactions are witnessed by many nodes.
Advantages
  • Autonomy: I.e. Cannot be manipulated by 3rd Party/Middleware (e.g. Lawyer)
  • Trust: Smart Contracts are encrypted and stored in Shared Ledger (Blockchain)
  • Backup: Smart Contracts are backed-up and thus safe.
  • Safety: Smart Contracts are secure thru Crypto-graphy
  • Speed: Due no 3rd party involved, it speeds ups
  • Saves Money: No payments for 3rd party (Lawyer)
  • Accuracy: Code driven thus accurate
Code: Simple Smart Contract
pragma solidity ^0.4.0;
contract SimpleStorage {
uint storedData;

function set(uint x) public {
storedData = x;
}

function get() public constant returns (uint) {
return storedData;
}
}

Inheritance & Abstraction:
  • Allows Multiple Inheritance
  • Even if contracts inherit from multipe other contracts, only a single contract is created on Blockchain
  • Forward Kill - Two classes/contract having similar function
  • Forward Kill with Super - Resolve Forward Kill Issue with Super
Using Libraries 
  • This is similar to contracts
  • Deloyed once at an 'Address' and they are reused using 'DELEGATECALL'
  • As the compiler, cannot know where the library will be deployed these adddresses have to be filled into final 'Byte-Code' by a 'Linker'
Special Variables
  • block.coinbase(address)
  • block.number(unit)
  • msg.sender(address)
Code Debugger
  • Mix
  • Browser-Solidity
  • Solidity Studio

Meta-Mask: (Ethereum Browser Extension)
  • Install - 
    • Install plug-in and Set Password, you'll be given 'Seed'  to manage/restore your account vault in other browsers using Meta-Mask.
    • Your account vault is encrypted and stored locally in your browser. I.e No information touches Ethereum Servers
    • Meta-Mask suggest to use Ethereum enabled website to interact with Ethereum Blockchain 
  • Usage:
    • Choose Blockchain Nodes (either below)
      • Main Ethereum N/w
      • Ropsten Test Framework
      • Kovan Test Framework
      • RinkBy Test Framework (Preferred)
    • Create New Multiple Account
      • Send Ether b/w accounts
      • Switch between Accounts
      • Exort Private Key
      • Import JSON
  • Extension injects the Ethereum Web3 API into every website's javascript context, so that dapps can read from the blockchain.
  • Lets  user create and manage their own identities, so when a Dapp wants to perform a transaction and write to the blockchain, the user gets a secure interface to review the transaction, before approving or rejecting it.
  • Because it adds functionality to the normal browser context, MetaMask requires the permission to read and write to any webpage.
  • Implementation:
    • Go to (https://remix.ethereum.org)
    • Create New  Simple Contract and Submit to an account in Meta-Mask
    • Using Meta-Mask (which internally uses Infura) interact with Blockchain Nodes (e.g. RinkBy) to send Ether from one a/c to another
    • In case you need your own Blockchain Nodes, then download Geth/Parity/Mist
    • With Geth (Go-Ethereum): You can have your own Blockchain Nodes in Local Computer
    • With Mist - You can have your own Blockchain Nodes in Browser

RinkBy Test Framework (Preferred)
  • To work with this, you need to get test Ether
    • Go to https://www.rinkeby.io/#stats
    • In left pane, select 'Crypto-Faucet' for instructions
    • Copy Test Ether
    • Publish on Facebook/Twitter
    • Copy Publish link and paster here to request Ether

Mist: (Ethereum Wallet) (https://github.com/ethereum/mist)
  • Full Browser - Mist browser is the tool of choice to browse and use Ðapps.
  • With Mist - You can have your own Blockchain Nodes in Browser
  • One of the 'Ethereum Wallet' (Others are: MyEther Wallet, Hardware Wallet, Brain Wallet, EtherWall etc.)
  • Ethereum Wallet allows:
    • Manage your accounts
    • Send Funds
    • Check Last Transactions
  • Check this for the full guide - https://github.com/ethereum/mist

Blockchain Nodes
  • Are like My-SQL Clusters
  • Are like Geth/Parity/CPP-Ethereum
  • Runs at client side
  • All nodes implement Ethereum Protocol
  • Replicate all blocks from Blockchain locally

Solidity:
  • High Level Programming Language (like Javascript)
  • Needs Compilation 
  • Code Compiles to EVM assembly I.e. Byte-Code
  • Transaction send 'Byte-Code' (I.e. Data) to Blockchain
  • Mining Node - Adds 'Byte-Code' to the Block and assigns a new Address

Remix: (https://remix.ethereum.org)
  • Web-based Solidity IDE in the cloud
  • Compiles Solidity Code in Browser
  • Has Integrated debugger
  • Integrated Blockchain Simulator, where you can create
    • Distributed Applications
    • Solidity Contracts
    • Smart Contracts
  • File extension (.sol)
  • IDE has Left/Main/Right Tab
  • Right Tab comes with - Compile, Run, Settings, Debugger, Analysis, Support Menu Options

Web3.js & Eth.js
  • These are framework libraries
  • Interact with Blockchain from Browser
  • Used to communicate between Website and Contracts
  • Javascript Library to interact with local Blockchain Nodes (Javascript & HTTP-RPC)
  • Web3 works with:
    • RPC Calls:
      • Provide access to Ethereum Blockchain
      • Have Geth or CPP-Ethereum running
      • Have Ethereum-Test RPC (Local Blockchain)
      • In HTML/JS
    • HTML/Javascript
      • Use Web3.js
      • Connect to HTTP RPC - web3.setProvider(httpURL)
      • E.g. Commands
        • var coinbase - web3.eth.coinbase  (Get coinbase address)
        • var balance - web3.eth.getbalance(coinbase)
    • Interface
      • ABI: Application Binary Interface
        • var mycontract = web3.eth.contract(objarray)
        • var contractInstance = mycontract.at([address])
      • ABI Sample: Tells Web3.js how to interact with binary code on Blockchain

[
{
name:send,
const:false,
input: [{name:to, type:address}],
output:[]
}
]

Truffle & Embark 
  • Both are framework for Solidity & Blockchain Development
  • Truffle - 
    • Makes Ethereum Developer Life Easy
    • Has Development Environment
    • Has Contract Management
    • Has Integrated Testing Framework
    • Allows
      • Built-in Smart Contracts, Compilation, Linking, Deployment and Binary Management
      • Automated Contracts Testing with Mocha/Chai
      • Interactive Console for Direct Contract Negotiation
  • Truffle Boxes - Comes with Pre-Configured Web-Dev Environment (e.g. Truffle-React, Truffle-Webpack etc.)
  • Is a Asset Pipeline for Ethereum
  • It is as good as 'Ethereum Studio', except that code run on local Blockchain
  • Can be installed with Ethereum Studio
  • While development with Truffle, you also need 'Ethereum Client' which connects to Blockchain
    • Recomended Ethereum Client is 'Ethereum JS Test RPC' - Complete In-Memory Blockchain
  • Installation:
Angular, Vue.js, React or Redux

  • No Relation to do with Truffle or Solidity or Blockchain Development
  • Instead use Web3 JS Library to connect with Blockchain

Webpack, Browserify, NPM
  • Webpack - Bundler for JS. Packs many modules in few bundles
  • Browserify - Let us require modules in browser, by bundling up all dependency
  • NPM - Allows Package Download

Basic Ethereum:
  • Ethereum Blockchain is another Blockchain almost like Bitcoin Blockchain
  • Has Ether (Bitcoin BC has Bitcoin)
  • Has Miner (So as Bitcoin)
  • Currently 'Proof of Work' (Bitcoin currently has 'Proof of State')
  • Can Host Code/Logic and Run Applications directly on Blockchain (Bitcoin cannot)
  • I.e. Programs are executed by every participating Blockchain Nodes
  • Thus powerful and shared Infrastructure
  • Blockchain is accessed
    • Via Ethereum Nodes over Ethereum Protocol 
    • Also with Javascript JSON RPC (Over HTTP) to interact with Ethereum Nodes

  • Ether is the currency used in Ethereum
  • Ether is used to pay computation within EVM
  • Ethereum has a metric system of 'Denominations' used as units of Ether
  • There are three ways to get Ether
    • Ethereum Mining
    • Trade other currencies for Ether (like Livecoin, Gatecoin, BlueTrader etc)
    • Use 'Mist Ethereum GUI Wallet' having the ability to purchase ether using 'ShapeShift API'
    • Free Way: Read above bullet - RinkBy Test Framework (Preferred)
      • Go to https://www.rinkeby.io/#stats
      • In left pane, select 'Crypto-Faucet'
      • Copy Test Ether
      • Publish on Facebook/Twitter
      • Copy Publish link and paste here to request Ether
  • Ether Units

Geth - Go Ethereum Client
  • Geth is the the command line interface for running a full Ethereum Node implemented in 'Go'. 
  • It is the main deliverable of the Frontier Release
  • To install locally:
  • By installing and running geth, you can take part in the ethereum frontier live network and
    • Mine Real Ether - Using Javascript Console
    • Transfer Funds Between Addresses
    • Create Contracts And Send Transactions
    • Explore Block History
    • And Much Much More
  • To run Geth
    • Open Command Prompt
      • geth : This will start server
      • geth console
      • geth attach
    • Account creation
      • personal.newaccount("Myaccount")
      • personal.listaccounts
  • E.g. Send Ether between accounts
    • List Account - personal.listaccounts
    • Check Balance - eth.getbalance(eth.accounts[0])
    • Unlock account - personal.unlokcaccount(eth.accounts[0])
    • Transfer - eth.sendTransaction({from:eth.accounts[0], to:eth.accounts[1]}, value:web3.toWei(1,"ether")});
    • Above command retruns 'Transaction ID'
    • To check it's status 
    • To check transaction process, connect to MIST on Ethereum Wallet
Practical - Hands-On
  • Create, Compile, Test, Deploy and Interact with Deloyed Contracts
  • Create/Compile/Test:
    • Write Contract in IDE (e.g. Ethereum Studio, Notepad)
    • Unit Test - Manual (In Browser-Solidity), Mocha, Truffle
    • Verify - Test-Net
  • Deploy
    • Contract Deployment on Live-Chain
    • Link HTML/JS to Contract Address
    • Deploy HTML on Server
    • Easy 4 steps:
      • Browser - Executes/Render HTML/JS
      • HTML/JS - Using Meta-Mask, connects to Geth/CPP-Ethereum Node 
      • Geth/CPP-Ethereum Node - Using HTTP-RPC, connect/access to 'Ethereum Blockchain'
      • Ethereum Blockchain - Then connect/access deployed Contracts
  • Interact
    • Create contract using Solidity
    • Deloy (Copy-Paste) using 'Ethereum Wallet'
    • Interact with deployed contract using 'Ethereum Wallet'

Setup Blockchain in Private Network:
  • Goto Go-Ethereum - https://github.com/ethereum/go-ethereum/wiki/geth
  • Download Go-Ethereum client
  • Download Genesis Block JSON File
  • Create New Dir and start 'Geth'
  • Use 'init' command to create your own 'Block' (Custom Genesis Block) in a chain
    • geth -datadir <somelocation> init genesis.json 
    • Here somelocation refers to local path to downloaded Blockchain) 
    • Output - Create multiple folder (e.g. Chaindata, dapp, nodes etc) and add Block to local location
  • To check this private n/w transaction - Run local 'EthereumWallet.exe')
Keep Blogging...

Arun Manglick