Upgrade to Pro — share decks privately, control downloads, hide ads and more …

スマートコントラクトプログラミング (3) イーサリアム実習 I / Ethereum Practice I

スマートコントラクトプログラミング (3) イーサリアム実習 I / Ethereum Practice I

ブロックチェーンハブ主催で開催している (第2期) スマートコントラクトプログラミング講義 (3) 「イーサリアム実習 I」のスライドです。2017年5月24日(水) に使用しました。

Kenji Saito

May 24, 2017
Tweet

More Decks by Kenji Saito

Other Decks in Technology

Transcript

  1. Solidity JavaScript ( , ) (constructor) ( ) ( )

    Ether I – (3) – 2017-05-24 – p.6/37
  2. pragma solidity ˆ0.4.8; contract MyToken { ( ) : (EVM

    ) : function MyToken(...) { /* */ : } : } function C (/* */ // ) I – (3) – 2017-05-24 – p.7/37
  3. MyToken string public name; string public symbol; uint8 public decimals;

    mapping (address => uint256) public balanceOf; name, symbol decimals : 2 100 1.00 mapping balanceOf I – (3) – 2017-05-24 – p.8/37
  4. MyToken event Transfer(address indexed from, address indexed to, uint256 value);

    indexed (3 ) MyToken function Transfer() Ethereum-Wallet I – (3) – 2017-05-24 – p.9/37
  5. MyToken function MyToken(uint256 _supply, string _name, string _symbol, uint8 _decimals)

    { if (_supply == 0) { _supply = 1000000; /* _supply 1,000,000 */ } balanceOf[msg.sender] = _supply; name = _name; symbol = _symbol; decimals = _decimals; } msg.sender _supply C++ _ I – (3) – 2017-05-24 – p.10/37
  6. MyToken transfer() function transfer(address _to, uint256 _value) { if (balanceOf[msg.sender]

    < _value) { /* */ throw; } if (balanceOf[_to] + _value < balanceOf[_to]) { /* */ throw; } balanceOf[msg.sender] -= _value; balanceOf[_to] += _value; Transfer(msg.sender, _to, _value); } throw (function ) catch I – (3) – 2017-05-24 – p.12/37
  7. (1) import pytest @pytest.fixture() def token_contract(chain): TokenFactory = chain.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 – (3) – 2017-05-24 – p.13/37
  8. (2) def test_my_token(token_contract, chain): account0 = chain.web3.eth.accounts[0] account1 = chain.web3.eth.accounts[1]

    assert token_contract.call().getBalanceOf(account0) == 1000000 assert token_contract.call().getBalanceOf(account1) == 0 txid = token_contract.transact().transfer(account1, 10) chain.wait.for_receipt(txid) assert token_contract.call().getBalanceOf(account0) == 999990 assert token_contract.call().getBalanceOf(account1) == 10 account0 coinbase account1 account0 account1 10BcH I – (3) – 2017-05-24 – p.14/37
  9. A, B A-B m A B A B A B

    . . . I – (3) – 2017-05-24 – p.17/37
  10. 1. X 2. X C 3. C 1. 2. 3.

    reliable multicast I – (3) – 2017-05-24 – p.18/37
  11. (safety) (liveness) ( ) ( = ) I – (3)

    – 2017-05-24 – p.20/37
  12. CAP Consistency ( ) Availability ( ) Partition tolerance (

    ) ⇒ 3 C Eventual consistency ( ) . . . I – (3) – 2017-05-24 – p.23/37
  13. Consistency ( ) Strong consistency ( ) (safety) Eventual consistency

    ( ) (liveness) ↑ Weak consistency ( ) ↑ ( ) I – (3) – 2017-05-24 – p.24/37
  14. n = f ( ) ⇒ I – (3) –

    2017-05-24 – p.25/37
  15. (1) 1 1, 2 n ≤ 3f I – (3)

    – 2017-05-24 – p.26/37
  16. CS1 : CS2 : CS3 : CL1 : CL2 :

    : : (e.g. ) I – (3) – 2017-05-24 – p.28/37
  17. (B)FT-CUP (Consensus with Unknown Participants) ( / ) : FT

    (Fault-Tolerant) : BFT (Byzantine Fault-Tolerant) P2P n FT/BFT I – (3) – 2017-05-24 – p.29/37
  18. State Machine Replication ( ) (by ) (since 1984) (

    ) ( ) . . . ( ) I – (3) – 2017-05-24 – p.30/37
  19. ( ) ( ) ( ) ( ) (by )

    ⇒ ( ) I – (3) – 2017-05-24 – p.31/37
  20. P2P : P2P 3 3 ( ) strategyproof group strategyproof

    I – (3) – 2017-05-24 – p.32/37
  21. again n > 3f R F R > 2F ⇒

    I – (3) – 2017-05-24 – p.34/37
  22. ( ) f = 1 ⇒ R I – (3)

    – 2017-05-24 – p.35/37