a short intro to Bitcoin, Ethereum and blockchain

Dapeng Li

May 2021

agenda

- Bitcoin (transaction)

- Ethereum (basic programming)

- blockchain

Cryptocurrency

A peer-to-peer electronic cash system.

https://www.youtube.com/watch?v=g6iDZspbRMg

https://en.wikipedia.org/wiki/Bitcoin

BTC motivation

- balance (who owns how much)

- payment (who paid whom)

BTC use case

BTC wallet

public/private keys

bearer payment, no identity

keep private key as secret (!)

https://en.bitcoinwiki.org/wiki/Private_key

BTC transaction

from, to, amount

https://www.blockchain.com/btc/tx/671a30fd9553399b21d4c6ceaee9c4dbaa980f567cd6abd4c206e57c6f28dd3a

signed by the sender

BTC chain of blocks

block contains transactions

block references parent

BTC chain of blocks

https://www.blockchain.com/btc/block/0000000000000000000999b09f3d68de48b87e0412cbc6e4dd8fd00fe7ecabf1?page=3

BTC use case (rev)

Double spending

What if Bob replays the transaction?

No state of "balance" on the blockchain

 

Unspent Transaction Output (UTXO)

https://github.com/bitcoinbook/bitcoinbook/blob/develop/ch06.asciidoc#transaction-outputs-and-inputs

UTXO

UTXO

Calculate balance

Add up all UTXOs

block rewards

Where does Bitcoin come from?

new block mints new Bitcoin

 

new block roughly every 10 minutes

 

"miner" receives rewards

Consensus

Who can create block?

Puzzle (Proof of Work)

difficult to solve, easy to verify

decentralized system needs consensus

Hash function

https://github.com/ethereumbook/ethereumbook/blob/develop/04keys-addresses.asciidoc#cryptographic-hash-functions

deterministic

one-way

collision resistant

Proof of Work (PoW)

https://github.com/bitcoinbook/bitcoinbook/blob/develop/ch10.asciidoc#proof-of-work-algorithm

hash(blockheader) <= target

Consensus rules

Other miners validate block

 

Start the next round after validation

BTC use case (rev2)

BTC more details

  • wallet manages different addresses, seed phrases
  • transaction lock/unlock script
  • transaction fee
  • difficulty adjustment
  • halving and limit of supply
  • forks
  • consensus attack
  • ...

https://github.com/bitcoinbook/bitcoinbook

BTC summary

inefficient

  • transaction throughput (3/s-7/s)
  • PoW
  • open (everyone can join)
  • transparent

scale?

  • increase block size
  • reduce transaction data in block
  • off-chain

Ethereum

similar to Bitcoin

  • currency (no double spending)
  • address (public/private keys)
  • chain of blocks
  • PoW consensus rule

difference

  • program (smart contract)
  • address (no private key)
  • transaction (interact with program)

EVM and lang

high level languages compiles to bytecode

bytecode runs on Ethereum Virtual Machine

Object-Oriented

  • define class (state + operation)
  • run as singleton
  • "called" with transaction
  • can call other smart contracts

https://github.com/ethereumbook/ethereumbook/blob/develop/07smart-contracts-solidity.asciidoc#introduction-to-ethereum-high-level-languages

Solidity ex.

pragma solidity >=0.4.22 <0.6.0;
contract Ballot {
  // structs
  struct Voter {
    bool voted;
    address delegate;
    uint vote;
  }

  struct Proposal {
    bytes32 name;
    uint voteCount;
  }

  // variables
  address public chairperson;
  mapping(address => Voter) public voters;
  Proposal[] public proposals;

  constructor(bytes32[] memory proposalNames) public {}

  // methods
  function giveRightToVote(address voter) public {}

  function vote(uint proposal) public {}

  function winningProposal() public view
          returns (uint winningProposal_) {}
}

https://docs.soliditylang.org/en/v0.5.3/solidity-by-example.html#voting

EVM lang.

Turing complete

halting problem - can it stop?

Pay to use

  • each operation has a gas price
  • in transaction, offer gas price
  • execution stops if runs "out of gas"

https://github.com/ethereumbook/ethereumbook/blob/develop/06transactions.asciidoc#transaction-gas

Oracle

Smart contract

  • deterministic
  • no randomness
  • no accessing resources off-chain

Oracle: "bridge" between smart contract and off-chain

  • weather
  • price

https://github.com/ethereumbook/ethereumbook/blob/develop/11oracles.asciidoc

Oracle

Oracle ex.

pragma solidity ^0.4.1;
import "github.com/oraclize/ethereum-api/oraclizeAPI.sol";

contract EthUsdPriceTicker is usingOraclize {
  uint public ethUsd;

  function EthUsdPriceTicker() payable {
    queryTicker();
  }

  function __callback(bytes32 _queryId, string _result, bytes _proof) public {
    if (msg.sender != oraclize_cbAddress()) throw;
    
    ethUsd = parseInt(_result, 2);

    queryTicker();
  }

  function queryTicker() external payable {
    oraclize_query(60 * 10, "URL",
      "json(https://min-api.cryptocompare.com/data/price?\
      fsym=ETH&tsyms=USD,EUR,GBP).USD");
  }
}

https://github.com/ethereumbook/ethereumbook/blob/develop/11oracles.asciidoc#oracle-client-interfaces-in-solidity

BTC, ETH programming

Dapp (Decentralized app)

smart contracts with web interfaces

https://www.manning.com/books/blockchain-in-action

https://github.com/ethereumbook/ethereumbook/blob/develop/12dapps.asciidoc

ETH token standards

ERC-20: fungible (interchangeable) tokens

contract ERC20 {
   function totalSupply() constant returns (uint theTotalSupply);
   function balanceOf(address _owner) constant returns (uint balance);
   function transfer(address _to, uint _value) returns (bool success);
   function transferFrom(address _from, address _to, uint _value) returns
      (bool success);
   function approve(address _spender, uint _value) returns (bool success);
   function allowance(address _owner, address _spender) constant returns
      (uint remaining);
   event Transfer(address indexed _from, address indexed _to, uint _value);
   event Approval(address indexed _owner, address indexed _spender, uint _value);
}

https://github.com/ethereumbook/ethereumbook/blob/develop/10tokens.asciidoc#the-erc20-token-standard

ETH token standards

ERC-721: non-fungible tokens (NFT)

interface ERC721 /* is ERC165 */ {
    event Transfer(address indexed _from, address indexed _to, uint256 _deedId);
    event Approval(address indexed _owner, address indexed _approved,
                   uint256 _deedId);
    event ApprovalForAll(address indexed _owner, address indexed _operator,
                         bool _approved);

    function balanceOf(address _owner) external view returns (uint256 _balance);
    function ownerOf(uint256 _deedId) external view returns (address _owner);
    function transfer(address _to, uint256 _deedId) external payable;
    function transferFrom(address _from, address _to, uint256 _deedId)
        external payable;
    function approve(address _approved, uint256 _deedId) external payable;
    function setApprovalForAll(address _operator, boolean _approved) payable;
    function supportsInterface(bytes4 interfaceID) external view returns (bool);
}
mapping (uint256 => address) private deedOwner;

https://github.com/ethereumbook/ethereumbook/blob/develop/10tokens.asciidoc#erc721-non-fungible-token-deed-standard

ETH consensus

PoW (proof of work)

  • compete by computing power
  • "extrinsic" punishment (loss of funds on electricity)

PoS (proof of stake)

  • stake to gain voting right
  • selected to validate transactions
  • "intrinsic" punishment (loss of stake)

https://github.com/ethereumbook/ethereumbook/blob/develop/14consensus.asciidoc#consensus

open, public blockchain

  • peer-to-peer network
  • messages (transactions)
  • state machine
  • chain of cryptographically secured blocks
  • consensus rules
  • incentivization scheme
  • open source implementations

https://github.com/ethereumbook/ethereumbook/blob/develop/01what-is.asciidoc#components-of-a-blockchain

learning curve

multiple disciplines

  • programming
  • information security
  • cryptography
  • economics
  • distributed systems
  • peer-to-peer networks

https://github.com/ethereumbook/ethereumbook/blob/develop/01what-is.asciidoc#why-learn-ethereum

blockchain use case?

https://www.manning.com/books/blockchain-in-action

blockchain us?

https://deepmind.com/blog/article/trust-confidence-verifiable-data-audit

https://www.manning.com/books/blockchain-in-action

public blockchain

Divided opinions

Technology is not endorsement for everything.

DYOR, good luck.

Resources

Thank you!

Not used

blockchain attrs.

https://www.manning.com/books/blockchain-in-action

intro to blockchain

By Dapeng Li

intro to blockchain

  • 231