$30 off During Our Annual Pro Sale. View Details »

イーサリアム実習 I / Ethereum Practice 1

イーサリアム実習 I / Ethereum Practice 1

2018年7月18日(水)、ブロックチェーンハブ主催で開催されたブロックチェーンアカデミー「イーサリアム実習 I」(【ハンズオン】スマートコントラクトプログラミング (2)) にて使用したスライドです。

Kenji Saito
PRO

July 18, 2018
Tweet

More Decks by Kenji Saito

Other Decks in Technology

Transcript

  1. I
    (2)
    CSO / SFC
    ( https://speakerdeck.com/ks91 )
    [email protected]
    I — (2) — 2018-07-18 – p.1/38

    View Slide

  2. 1.
    2.
    3.
    4.
    5.
    I — (2) — 2018-07-18 – p.2/38

    View Slide

  3. 1. : ERC20
    2. ∼ ∼
    I — (2) — 2018-07-18 – p.3/38

    View Slide

  4. 1. : ERC20
    ERC20
    I — (2) — 2018-07-18 – p.4/38

    View Slide

  5. (2018 7 18 )
    BcH-smart-contract-programming.zip
    Solidity
    py.test solidity 0.4.22 + populus
    ongoing
    solidity 0.4.21
    contracts tests
    I — (2) — 2018-07-18 – p.5/38

    View Slide

  6. Solidity
    JavaScript
    ( , )
    (constructor)
    ( )
    ( )
    Ether
    I — (2) — 2018-07-18 – p.6/38

    View Slide

  7. pragma solidity ˆ0.4.24;
    contract MyToken {
    ( )
    :
    (EVM )
    :
    constructor (...) public { /* */
    :
    }
    function MyToken(...) { /* ( ) */
    :
    }
    :
    }
    constructor
    C (/* */ // )
    I — (2) — 2018-07-18 – p.7/38

    View Slide

  8. ERC20
    ERC (Ethereum Request for Comment) 20
    https://github.com/ethereum/EIPs/blob/master/EIPS/eip-20.md
    contract ERC20 {
    function totalSupply() constant returns (uint totalSupply);
    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);
    }
    name/ , symbol/ , decimals/
    approve allowance
    ERC223 ( ), ERC721 (Non-Fungible)
    I — (2) — 2018-07-18 – p.8/38

    View Slide

  9. MyToken
    string public _name;
    string public _symbol;
    uint8 public _decimals;
    mapping (address => uint256) public _balanceOf;
    uint256 _totalSupply;
    _name, _symbol
    _decimals
    : 2 100 1.00
    mapping _balanceOf
    I — (2) — 2018-07-18 – p.9/38

    View Slide

  10. MyToken
    event Transfer(address indexed from, address indexed to, uint256 value);
    indexed (3 )
    MyToken function Transfer()
    I — (2) — 2018-07-18 – p.10/38

    View Slide

  11. MyToken
    constructor (uint256 supply, string name, string symbol, uint8 decimals) public {
    if (supply == 0) {
    supply = 1000000; /* supply 1,000,000 */
    }
    _totalSupply = supply;
    _balanceOf[msg.sender] = supply;
    _name = name;
    _symbol = symbol;
    _decimals = decimals;
    }
    msg.sender
    supply
    I — (2) — 2018-07-18 – p.11/38

    View Slide

  12. MyToken balanceOf()
    function balanceOf(address tokenOwner) external view returns (uint256) {
    return _balanceOf[tokenOwner];
    }
    I — (2) — 2018-07-18 – p.12/38

    View Slide

  13. MyToken transfer()
    function transfer(address to, uint256 value) external returns (bool) {
    require(value <= _balanceOf[msg.sender]); /* */
    require(_balanceOf[to] + value >= _balanceOf[to]); /* */
    _balanceOf[msg.sender] -= value;
    _balanceOf[to] += value;
    Transfer(msg.sender, to, value);
    return true;
    }
    require (function )
    OpenZeppelin
    https://openzeppelin.org
    I — (2) — 2018-07-18 – p.13/38

    View Slide

  14. (1)
    import pytest
    @pytest.fixture()
    def token_contract(chain):
    TokenFactory = chain.provider.get_contract_factory(’MyToken’)
    deploy_txid = TokenFactory.deploy(args=[
    0,
    "BcH Coin",
    "BcH",
    0,
    ])
    contract_address = chain.wait.for_contract_address(deploy_txid)
    return TokenFactory(address=contract_address)
    populus
    I — (2) — 2018-07-18 – p.14/38

    View Slide

  15. (2)
    def test_my_token(token_contract, chain):
    account0 = chain.web3.eth.accounts[0]
    account1 = chain.web3.eth.accounts[1]
    assert token_contract.call().balanceOf(account0) == 1000000
    assert token_contract.call().balanceOf(account1) == 0
    txid = token_contract.transact().transfer(account1, 10)
    chain.wait.for_receipt(txid)
    assert token_contract.call().balanceOf(account0) == 999990
    assert token_contract.call().balanceOf(account1) == 10
    account0 coinbase
    account1
    account0 account1 10BcH
    I — (2) — 2018-07-18 – p.15/38

    View Slide

  16. $ py.test -k test_my_token.py
    I — (2) — 2018-07-18 – p.16/38

    View Slide

  17. 2.
    ∼ ∼
    I — (2) — 2018-07-18 – p.17/38

    View Slide

  18. A, B
    A-B m
    A B A
    B A B
    . . .
    I — (2) — 2018-07-18 – p.18/38

    View Slide

  19. 1. X
    2. X C
    3. C
    1.
    2.
    3.
    reliable multicast
    I — (2) — 2018-07-18 – p.19/38

    View Slide

  20. :
    I — (2) — 2018-07-18 – p.20/38

    View Slide

  21. (safety)
    (liveness)
    ( )
    ( = )
    I — (2) — 2018-07-18 – p.21/38

    View Slide

  22. → / (benign)
    → (Byzantine)
    (malicious)
    I — (2) — 2018-07-18 – p.22/38

    View Slide

  23. FLP
    Fischer, Lynch, Paterson
    I — (2) — 2018-07-18 – p.23/38

    View Slide

  24. CAP
    Consistency ( )
    Availability ( )
    Partition tolerance ( )
    ⇒ 3
    C
    Eventual consistency ( )
    . . .
    I — (2) — 2018-07-18 – p.24/38

    View Slide

  25. Consistency ( )
    Strong consistency ( )
    (safety)
    Eventual consistency ( )
    (liveness)

    Weak consistency ( )
    ↑ ( )
    I — (2) — 2018-07-18 – p.25/38

    View Slide

  26. n
    =
    f
    ( )

    I — (2) — 2018-07-18 – p.26/38

    View Slide

  27. (1)
    1 1, 2
    n ≤ 3f
    I — (2) — 2018-07-18 – p.27/38

    View Slide

  28. (2)
    I — (2) — 2018-07-18 – p.28/38

    View Slide

  29. CS1 :
    CS2 :
    CS3 :
    CL1 :
    CL2 :
    :
    :
    (e.g. )
    I — (2) — 2018-07-18 – p.29/38

    View Slide

  30. (B)FT-CUP (Consensus with Unknown Participants)
    ( / )
    : FT (Fault-Tolerant)
    : BFT (Byzantine Fault-Tolerant)
    P2P
    n FT/BFT
    I — (2) — 2018-07-18 – p.30/38

    View Slide

  31. State Machine Replication ( )
    (by ) (since 1984) ( )
    ( )
    . . .
    ( )
    I — (2) — 2018-07-18 – p.31/38

    View Slide

  32. ( )
    ( ) ( ) ( )
    (by )
    ⇒ ( )
    I — (2) — 2018-07-18 – p.32/38

    View Slide

  33. P2P
    : P2P
    3
    3
    ( )
    strategyproof
    group strategyproof
    I — (2) — 2018-07-18 – p.33/38

    View Slide

  34. (Sybil)
    16
    I — (2) — 2018-07-18 – p.34/38

    View Slide

  35. again
    n > 3f
    R F
    R > 2F

    I — (2) — 2018-07-18 – p.35/38

    View Slide

  36. ( )
    f = 1 ⇒ R
    I — (2) — 2018-07-18 – p.36/38

    View Slide

  37. (centralized) (decentralized) (distributed)
    Paul Baran, “On Distributed Communications Networks”, 1964
    (C) (A)
    I — (2) — 2018-07-18 – p.37/38

    View Slide

  38. II
    :
    :
    /
    I — (2) — 2018-07-18 – p.38/38

    View Slide