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

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

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

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

Kenji Saito

July 18, 2018
Tweet

More Decks by Kenji Saito

Other Decks in Technology

Transcript

  1. 1. : ERC20 2. ∼ ∼ I — (2) —

    2018-07-18 – p.3/38
  2. (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
  3. Solidity JavaScript ( , ) (constructor) ( ) ( )

    Ether I — (2) — 2018-07-18 – p.6/38
  4. pragma solidity ˆ0.4.24; contract MyToken { ( ) : (EVM

    ) : constructor (...) public { /* */ : } function MyToken(...) { /* ( ) */ : } : } constructor C (/* */ // ) I — (2) — 2018-07-18 – p.7/38
  5. 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
  6. 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
  7. MyToken event Transfer(address indexed from, address indexed to, uint256 value);

    indexed (3 ) MyToken function Transfer() I — (2) — 2018-07-18 – p.10/38
  8. 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
  9. MyToken balanceOf() function balanceOf(address tokenOwner) external view returns (uint256) {

    return _balanceOf[tokenOwner]; } I — (2) — 2018-07-18 – p.12/38
  10. 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
  11. (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
  12. (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
  13. A, B A-B m A B A B A B

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

    reliable multicast I — (2) — 2018-07-18 – p.19/38
  15. (safety) (liveness) ( ) ( = ) I — (2)

    — 2018-07-18 – p.21/38
  16. CAP Consistency ( ) Availability ( ) Partition tolerance (

    ) ⇒ 3 C Eventual consistency ( ) . . . I — (2) — 2018-07-18 – p.24/38
  17. Consistency ( ) Strong consistency ( ) (safety) Eventual consistency

    ( ) (liveness) ↑ Weak consistency ( ) ↑ ( ) I — (2) — 2018-07-18 – p.25/38
  18. n = f ( ) ⇒ I — (2) —

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

    — 2018-07-18 – p.27/38
  20. CS1 : CS2 : CS3 : CL1 : CL2 :

    : : (e.g. ) I — (2) — 2018-07-18 – p.29/38
  21. (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
  22. State Machine Replication ( ) (by ) (since 1984) (

    ) ( ) . . . ( ) I — (2) — 2018-07-18 – p.31/38
  23. ( ) ( ) ( ) ( ) (by )

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

    I — (2) — 2018-07-18 – p.33/38
  25. again n > 3f R F R > 2F ⇒

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

    — 2018-07-18 – p.36/38