Ethereum Blockchain

Amit Kumar Jaiswal

Google Summer of Code 2017 Intern at CNCF [Kubernetes]

amitkumarj441@gmail.com

 

   Twitter/Medium : @AMIT_GKP

  Github : @amitkumarj441

1. The philosophy behind blockchain

2. What is Ethereum?

3. Pros & Cons of ethereum

4. Supporting tools for ethereum

 

5. Developing framework

6. BlockCypher

7. Truffle & TestRPC – A quick testing environment for block chain

Outline

The philosophy behind blockchain

Blockchain ?

Blockchain technology is the technological basis of Bitcoin, first described by its mysterious author Satoshi Nakamoto in his white paper

“Bitcoin: A Peer-to-Peer Electronic Cash System"

An online global ledger

A question 

How to let many different users agree on the current state of the blockchain even though they don't trust each other or any central authority?  

 

 

This is a challenging problem, and until the Bitcoin network was launched, it had remained unsolved.

Consensus

How Bitcoin achieve consensus?

Proof of Work, also called POW

PoW algorithms are able to use the number and difficulty of solutions being found to measure how much of the network agrees on the current state of the blockchain.

The concept of PoW

This won't work !

Still not good enough 

Application-specific integrated circuit

It takes lots of works to mine some blocks

But they get bitcoins for reward :D

Charactieristics of PoW

 

  1. Hard to compute
  2. Easy to verify

 

That way, users can simply pick the longest valid chain with the highest amount of work as the correct chain.

Good Side

That requirement for resources is a good thing, because it means that interfering with the group's consensus takes a lot of resources (a.k.a. money).

Bad Side :(

But this implies that Proof of Work is extremely inefficient in term of energy, and therefore also very expensive; which incentivize miners to centralize the hashing power

But this implies that Proof of Work is extremely inefficient in term of energy, and therefore also very expensive; which incentivize miners to centralize the hashing power

→ obviously not desirable for a network whose goal is to minimize the need to trust third parties.

Catchy part

Bitcoin maybe viewed as centralized in China by a small group of people holding the mining power which may be greater than 51%.

Proof of Stake

  • Agreement within the block chain would be measured not on the basis of how much computing power agrees with the current state, but instead on the basis of how much digital currency agrees with the current state.

 

But it is another story :p

So far we are talking about block chain used in Bitcoin

Different form Ethereum !

The Differences

  • Bitcoin is more focused on being a currency
  • Ethereum is more focused on being a smart contract platform, which is a general-purpose, global blockchain that can govern both financial and non-financial types of application states

 

They both play their roles well, even though they are separate roles with some overlap

Things in common

 

1. Cryptocurrency

2. An inherent blockchain

3. A decentralized consensus-based proof mechanism

4. Miners that support the network

Things not in common

  • Ethereum was conceived from Day 1 as a software development platform for decentralized applications, and its blockchain was specifically designed to support executing apps on it

 

So what is the difference between ether & bitcoin

  • Instead of focusing on fulfilling financial transactions, ether is more like an incentive plumbing in the form of required “crypto-fuel” to pay for the transaction fees necessary to run the various smart business logic programs that users submit to its blockchain​

ether-based block chain

  • When you run an application in the cloud, you are charged based on a combination of time, storage, data transfer and computing speed requirements.

 

It’s a form of cloud-based micro-value pricing that un-bundles a layer from the traditional cloud computing stack.

Ethereum excels at

Programmability

Scalability

Upgradability

Transactions Manageability

Visibility

Affordability

Security

Speed/Performance

High Availability

Extensibility

btw 

Mining in the Ethereum context can be done by regular computers !

Some basic concepts in Ethereum

Smart Contract

  • Smart Contract : Smart contracts are like cryptographic “boxes” that contain a value and only unlock it if certain conditions are met. They typically encapsulate some logic, rules, a process, or an agreement between parties. When they are launched on Ethereum, the network enforces their ramifications.

Gas

When a contract is executed as a result of being triggered by a message or transaction, every instruction is executed on every node of the network.

This has a cost: for every executed operation there is a specified cost, expressed in a number of gas units.

→ Prevent deliberate attacks and abuse on the Ethereum network.

→ Gas limit keeps the network decentralized

 

Gas Cost

Some concepts

  • Solidity
    • Solidity is the most popular language for smart contract
  • solc
    • After writing a contract in Solidity, use solc to compile it
  • web3.js API
    • Once a Solidity contract is compiled with solc and sent to the network, you can call it using the Ethereum web3.js JavaScript API and build web apps that interact with contracts

Some concepts

  • DApp 
    • Decentralized App, what applications using smart contracts are called in the Ethereum community.
  • Node
    • Using this to mean you can run a node and through it read and write to the Ethereum blockchain, i.e., use the Ethereum Virtual Machine.

How to connect to Ethereum?

Block Cypher

What BlockCypher excels at

  • Simple and easy web APIs (REST) enable developers to build blockchain applications quickly and confidently.

 

  • They maintain a cluster of distributed databases which contain all transactional data for the entire blockchain.

Almost all resources exist under a given blockchain

curl -s https://api.blockcypher.com/v1/eth/main
{
  "name": "ETH.main",
  "height": 1663353,
  "hash": "863dda1124f2b438c607f5b8d00e8511f6f8d206b21aad3b9c460b8c5221e31b",
  "time": "2016-06-08T00:46:34.795856213Z",
  "latest_url": "https://api.blockcypher.com/v1/eth/main/blocks/863dda1124f2b438c607f5b8d00e8511f6f8d206b21aad3b9c460b8c5221e31b",
  "previous_hash": "783aa3ef1b45121ee5bc33acb6c5986d6132d04cf20c85ba256b155b2c196006",
  "previous_url": "https://api.blockcypher.com/v1/eth/main/blocks/783aa3ef1b45121ee5bc33acb6c5986d6132d04cf20c85ba256b155b2c196006",
  "peer_count": 52,
  "unconfirmed_count": 11924,
  "high_gas_price": 40000000000,
  "medium_gas_price": 20000000000,
  "low_gas_price": 5000000000,
  "last_fork_height": 1661588,
  "last_fork_hash": "79075d95aacc6ac50dbdf58da044af396ca97e09cbb31527809579cc96f1c8a7"
}

The API always returns values in wei, the lowest non-divisible unit in Ethereum.

There are 10^18 wei in a single ether (1,000,000,000,000,000,000w = 1ETH).

We can inspect each block by specifying height, address

curl -s https://api.blockcypher.com/v1/eth/main/blocks/7
{
  "hash": "e0c7c0b46e116b874354dce6f64b8581bd239186b03f30a978e3dc38656f723a",
  "height": 7,
  "chain": "ETH.main",
  "total": 0,
  "fees": 0,
  "size": 1078,
  "ver": 0,
  "time": "2015-07-30T15:28:30Z",
  "received_time": "2015-07-30T15:28:30Z",
  "coinbase_addr": "dd2f1e6e498202e86d8f5442af596580a4f03c2c",
  "relayed_by": "",
  "nonce": 13599487003767981,
  "n_tx": 0,
  "prev_block": "1f1aed8e3694a067496c248e61879cda99b0709a1dfbacd0b693750df06b326e",
  "mrkl_root": "ff8d97d9ca01ee59c793c4da2ba4ce8c31d358b42f216ab2f11af4bb097b6a2b",
  "uncles": [
    "4b8729311c5b59f418c5154fd54d85e6a8b42eabf83a1d3c05c754a8f10354cc"
  ],
  "txids": [],
  "depth": 1656646,
  "prev_block_url": "https://api.blockcypher.com/v1/eth/main/blocks/1f1aed8e3694a067496c248e61879cda99b0709a1dfbacd0b693750df06b326e",
  "tx_url": "https://api.blockcypher.com/v1/eth/main/txs/"
}

The API always returns values in wei, the lowest non-divisible unit in Ethereum.

There are 10^18 wei in a single ether (1,000,000,000,000,000,000w = 1ETH).

Generate an address for transaction

curl -sX POST https://api.blockcypher.com/v1/eth/main/addrs?token=YOURTOKEN

{
  "private": "44c984a089184a9a659e10210af360049f366a34f7f3cb253ac5cbfcb33f3d7c",
  "public": "04b7de9b47fc1fb278648266316dffe47cc7f5280b69b05b5770ad1624ee3da7647c98ade5e4c35b99e606535eaca54db0f913c8aa1fa7226f15b9996325d0989d",
  "address": "e3f7e628fff7589218d88ae1d6bcdac52fef2168"
}

Creating Transactions

curl -sd '{
"inputs":[
    {"addresses": ["add42af7dd58b27e1e6ca5c4fdc01214b52d382f"]}
],
"outputs":[
    {"addresses": ["884bae20ee442a1d53a1d44b1067af42f896e541"], 
    "value": 4200000000000000}
]}' https://api.blockcypher.com/v1/eth/main/txs/new?token=YOURTOKEN

{
  "tx": {
    "block_height": -1,
    "block_index": 0,
    "hash": "9403244c6bcfe4063e7fb33138b02881d7a72ccdbd9c04d9abdff2b432d67ada",
    "addresses": [
      "add42af7dd58b27e1e6ca5c4fdc01214b52d382f",
      "884bae20ee442a1d53a1d44b1067af42f896e541"
    ],
    "total": 4200000000000000,
    "fees": 861000000000000,
    "size": 44,
    "gas_used": 0,
    "gas_price": 41000000000,
    "relayed_by": "",
    "received": "2016-06-08T01:32:35.573921633Z",
    "ver": 0,
    "double_spend": false,
    "vin_sz": 1,
    "vout_sz": 1,
    "inputs": [
      {
        "sequence": 0,
        "addresses": [
          "add42af7dd58b27e1e6ca5c4fdc01214b52d382f"
        ]
      }
    ],
    "outputs": [
      {
        "value": 4200000000000000,
        "addresses": [
          "884bae20ee442a1d53a1d44b1067af42f896e541"
        ]
      }
    ]
  },
  "tosign": [
    "a83f5bea598e0d217a03a2646d6c49edb2e99daf4537b2c09b008df76b77acec"
  ]
}

Look Up the balance in your account

curl -s https://api.blockcypher.com/v1/eth/main/addrs/$YOURADDRESS/balance

// it should return this

{
  "address": "738d145faabb1e00cf5a017588a9c0f998318012",
  "total_received": 9762206505909057760,
  "total_sent": 6916970312523512365,
  "balance": 2845236193385545395,
  "unconfirmed_balance": 0,
  "final_balance": 2845236193385545395,
  "n_tx": 702,
  "unconfirmed_n_tx": 0,
  "final_n_tx": 702
}

//but actually it will return this

{"error": "Address not found or unsupported: 8134d81b5bfc1f9143424bde04d9b969d77b6213"}

Why

The address doesn't exist on the Ethereum block chain until it has a transaction associated with it

 

Without requiring funding an address, if anyone could embed addresses into the block chain, it would be a large DDoS vector.

Create a contract (1/4)

cat publishGreeter.json
{
    "solidity": 
        "contract greeter{    
            /* define variable greeting of the type string */    
            string greeting;    
            /* this runs when the contract is executed */    
            function greeter(string _greeting) public {        
                greeting = _greeting;    
            }    

            /* main function */    
            function greet() constant returns (string) {        
                return greeting;    
            }
        }",
  "params": ["Hello BlockCypher Test"]
}

Check solidity compilation via non-published test

Create a contract (2/4)

curl -sd @testGreeter.json https://api.blockcypher.com/v1/eth/main/contracts?token=YOURTOKEN
[
  {
    "solidity": 
        "contract greeter{    
            /* define variable greeting of the type string */    
            string greeting;    
            /* this runs when the contract is executed */    
            function greeter(string _greeting) public {        
                greeting = _greeting;    
            }    

            /* main function */    
            function greet() constant returns (string) {        
                return greeting;    
            }
        }",
    "bin": "606060405260405161023e38038061023e8339810160405280510160008054600160a060020a031916331790558060016000509080519060200190828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f10609f57805160ff19168380011785555b50608e9291505b8082111560cc57600081558301607d565b50505061016e806100d06000396000f35b828001600101855582156076579182015b82811115607657825182600050559160200191906001019060b0565b509056606060405260e060020a600035046341c0e1b58114610026578063cfae321714610068575b005b6100246000543373ffffffffffffffffffffffffffffffffffffffff908116911614156101375760005473ffffffffffffffffffffffffffffffffffffffff16ff5b6100c9600060609081526001805460a06020601f6002600019610100868816150201909416939093049283018190040281016040526080828152929190828280156101645780601f1061013957610100808354040283529160200191610164565b60405180806020018281038252838181518152602001915080519060200190808383829060006004602084601f0104600f02600301f150905090810190601f1680156101295780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b565b820191906000526020600020905b81548152906001019060200180831161014757829003601f168201915b505050505090509056",
    "abi": [
      {
        "constant": false,
        "inputs": [],
        "name": "kill",
        "outputs": [],
        "type": "function"
      },
      {
        "constant": true,
        "inputs": [],
        "name": "greet",
        "outputs": [
          {
            "name": "",
            "type": "string"
          }
        ],
        "type": "function"
      },
      {
        "inputs": [
          {
            "name": "_greeting",
            "type": "string"
          }
        ],
        "type": "constructor"
      }
    ]
  }

]

Looking good! Now let's publish it to block chain

Create a contract (3/4)

cat publishGreeter.json
{
  "solidity": 
        "contract greeter{    
            /* define variable greeting of the type string */    
            string greeting;    
            /* this runs when the contract is executed */    
            function greeter(string _greeting) public {        
                greeting = _greeting;    
            }    

            /* main function */    
            function greet() constant returns (string) {        
                return greeting;    
            }
        }",
  "params": ["Hello BlockCypher Test"],
  "publish": ["greeter"],
  "private": "3ca40...",
  "gas_limit": 500000
}

Now publish the greeter contract

Create a contract (4/4)

curl -sd @testGreeter.json https://api.blockcypher.com/v1/eth/main/contracts?token=YOURTOKEN
[
  {
    "name": "greeter",
    "solidity": 
        "contract greeter{    
            /* define variable greeting of the type string */    
            string greeting;    
            /* this runs when the contract is executed */    
            function greeter(string _greeting) public {        
                greeting = _greeting;    
            }    

            /* main function */    
            function greet() constant returns (string) {        
                return greeting;    
            }
        }",
    "bin": "606060405260405161023e38038061023e8339810160405280510160008054600160a060020a031916331790558060016000509080519060200190828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f10609f57805160ff19168380011785555b50608e9291505b8082111560cc57600081558301607d565b50505061016e806100d06000396000f35b828001600101855582156076579182015b82811115607657825182600050559160200191906001019060b0565b509056606060405260e060020a600035046341c0e1b58114610026578063cfae321714610068575b005b6100246000543373ffffffffffffffffffffffffffffffffffffffff908116911614156101375760005473ffffffffffffffffffffffffffffffffffffffff16ff5b6100c9600060609081526001805460a06020601f6002600019610100868816150201909416939093049283018190040281016040526080828152929190828280156101645780601f1061013957610100808354040283529160200191610164565b60405180806020018281038252838181518152602001915080519060200190808383829060006004602084601f0104600f02600301f150905090810190601f1680156101295780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b565b820191906000526020600020905b81548152906001019060200180831161014757829003601f168201915b505050505090509056",
    "abi": [
      {
        "constant": false,
        "inputs": [],
        "name": "kill",
        "outputs": [],
        "type": "function"
      },
      {
        "constant": true,
        "inputs": [],
        "name": "greet",
        "outputs": [
          {
            "name": "",
            "type": "string"
          }
        ],
        "type": "function"
      },
      {
        "inputs": [
          {
            "name": "_greeting",
            "type": "string"
          }
        ],
        "type": "constructor"
      }
    ],
    "gas_limit": 500000,
    "creation_tx_hash": "61474003e56d67aba6bf148c5ec361e3a3c1ceea37fe3ace7d87759b399292f9",
    "address": "0eb688e79698d645df015cf2e9db5a6fe16357f1",
    "params": [
      "Hello BlockCypher Test"
    ]
  }

]

Call Contract Method Endpoint

cat call.json
{
    "private": "3ca40...",
    "gas_limit": 20000 
}

curl -d @call.json -s https://api.blockcypher.com/v1/eth/main/contracts/0eb688e79698d645df015cf2e9db5a6fe16357f1/greet?token=YOURTOKEN
{
  "gas_limit": 20000,
  "address": "0eb688e79698d645df015cf2e9db5a6fe16357f1",
  "results": [
    "Hello BlockCypher Test"
  ]
}

call the greet method, using your own private key and gas amount to fund

Truffle + Testrpc

a convenient & cheap way to get started :D

Once your contract is working ok

 

it’s nice to use a framework for adding a UI and packaging it all up as a DApp

 

which is what Truffle does

What about Testrpc?

Testrpc will create a private block chain for you

And it will also create a bunch of pre-funded accounts for you

It’s is super fast, so easier to develop and test with

It takes about 23 hours to download the whole block chain ...

  • Instead there are ways to run clients on a local testnet for for free.

 

They won’t download the full blockchain and will create a private instance of the Ethereum network with its own blockchain, so are faster to use for development.

The work flow

1.Start an Ethereum node (e.g. geth, testrpc or ethersim)

 

2.Compile your Solidity smart contract using solc

    → get back the binary

 

3.Deploy your compiled contract to the network.

   → get back the contract’s blockchain address and ABI (a JSON-ified representation of your compiled contract’s variables, events and methods that you can call)

 

4.Call stuff in the contract using web3.js’s JavaScript API to interact with it 

Let's get started

You might be curious how to do all these stuffs without API

Steps are as follows

  • Install geth
    • geth is the the command line interface for running a full ethereum node implemented in Go
  • Sync with the Ethereum block chain
  • Create smart contract
  • Compile it
var greeterSource = 'contract mortal { address owner; function mortal() { owner = msg.sender; } function kill() { if (msg.sender == owner) selfdestruct(owner); } } contract greeter is mortal { string greeting; function greeter(string _greeting) public { greeting = _greeting; } function greet() constant returns (string) { return greeting; } }'

var greeterCompiled = web3.eth.compile.solidity(greeterSource)

Deploy the contract

var _greeting = "Hello World!"
var greeterContract = web3.eth.contract(greeterCompiled.greeter.info.abiDefinition);

var greeter = greeterContract.new(_greeting,{from:web3.eth.accounts[0], data: greeterCompiled.greeter.code, gas: 300000}, function(e, contract){
    if(!e) {

      if(!contract.address) {
        console.log("Contract transaction send: TransactionHash: " + contract.transactionHash + " waiting to be mined...");

      } else {
        console.log("Contract mined! Address: " + contract.address);
        console.log(contract);
      }

    }
})

After deployed the contract

Within less than a minute, you should have a log with the contract address, this means you've successfully deployed your contract.

 

And you will get two data:

  • Application Binary Interface
  • Contract Address

Calling the contract

var greeter = eth.contract(ABI).at(Address);
greeter.greet();

Conclusion

What Ethereum hope

  • what developers see is not enough in order to reveal the entire spectrum of the potential of Ethereum.

 

  • Developers need business people as partners who understand Ethereum’s capabilities, and are willing to apply their own experience towards that understanding.

What Ethereum hope

  • From a technology point of view, decentralization is particularly valuable for “base layer” services, i.e. what everything else relies on, such as
    • identity
    • reputation
    • communications
    • social networks
    • markets
  • and making them work without relying on specific trusted intermediaries in the middle.

What Ethereum hope

Business people must learn about decentralization, and ask if old processes can be replaced by decentralized ones, enabled by Ethereum

References

  • https://blog.ethereum.org/2015/05/24/the-business-imperative-behind-the-ethereum-vision/
  • http://bitcoin.stackexchange.com/questions/1600/where-are-the-users-bitcoins-actually-stored
  • https://github.com/ethereum/wiki/wiki
  • https://bitcointalk.org/index.php?topic=27787.0
  • https://upload.wikimedia.org/wikipedia/commons/thumb/5/55/Proof_of_Work_challenge_response.svg/459px-Proof_of_Work_challenge_response.svg.png
  • http://ethdocs.org/en/latest/introduction/what-is-ethereum.html

Ethereum Blockchain - PyDelhi

By AMIT KUMAR JAISWAL

Ethereum Blockchain - PyDelhi

Some basic concept and implementation of block chain :D

  • 1,491