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

Learn Blockchain By Building It

Learn Blockchain By Building It

This is the talk I gave at the Code Monsters 2018 conference (https://2018.codemonsters.pro/).

In it, I go through the process of implementing a Blockchain system from scratch by covering the fundamental concepts around it and discussing the problems it solves. I have also supplied a Java implementation of a simplified Blockchain system in order for the audience to better understand the concept and play with the system.

It can be found at https://github.com/preslavmihaylov/noobchain

Preslav Mihaylov

November 27, 2018
Tweet

More Decks by Preslav Mihaylov

Other Decks in Programming

Transcript

  1. View Slide

  2. Where’s my money scenario
    \w friend

    View Slide

  3. Where’s my money scenario
    \w friend

    View Slide

  4. Where’s my money scenario
    \w friend

    View Slide

  5. Where’s my money scenario
    \w friend

    View Slide

  6. Where’s my money scenario
    \w friend

    View Slide

  7. Where’s my money scenario
    \w friend

    View Slide

  8. View Slide

  9. KTB & people shouting
    “Where’s my money”

    View Slide

  10. View Slide

  11. View Slide

  12. View Slide

  13. View Slide

  14. A Ledger

    View Slide

  15. TODO: Neighborhood picture

    View Slide

  16. A Centralized
    Approach

    View Slide

  17. View Slide

  18. TODO:
    Centralized
    Problems

    View Slide

  19. View Slide

  20. TODO:
    Centralized
    Problems

    View Slide

  21. View Slide

  22. View Slide

  23. View Slide

  24. View Slide

  25. View Slide

  26. View Slide

  27. View Slide

  28. View Slide

  29. View Slide

  30. View Slide

  31. View Slide

  32. View Slide

  33. public class Transaction {
    private String sender;
    private String recipient;
    private int amount;
    // constructor, getters, setters, toString...
    }

    View Slide

  34. public class Block {
    private final List transactions;
    // constructor, getters, setters, toString...
    }

    View Slide

  35. public class Blockchain {
    private List blocks;
    // constructor, getters, setters, toString...
    }

    View Slide

  36. View Slide

  37. View Slide

  38. View Slide

  39. View Slide

  40. View Slide

  41. View Slide

  42. View Slide

  43. View Slide

  44. public class Blockchain {
    private List blocks;
    private List pendingTransactions;
    // constructor, getters, setters, toString...
    }

    View Slide

  45. // …
    // TODO: Implement Peer Synchronization
    // …

    View Slide

  46. Fake Transactions

    View Slide

  47. TODO: Rebecca buying a bike
    from Peter

    View Slide

  48. View Slide

  49. View Slide

  50. // …
    // TODO: Implement Signatures
    // …

    View Slide

  51. Making the Ledger
    Immutable

    View Slide

  52. View Slide

  53. View Slide

  54. View Slide

  55. View Slide

  56. public class Block {
    private final List transactions;
    private final String prevBlockHash;
    private final String blockHash;
    // constructor, getters, setters, toString...
    }

    View Slide

  57. Achieving
    Consensus

    View Slide

  58. View Slide

  59. View Slide

  60. View Slide

  61. View Slide

  62. View Slide

  63. View Slide

  64. View Slide

  65. View Slide

  66. public class Block {
    private final List transactions;
    private final long nonce;
    private final String prevBlockHash;
    private final String blockHash;
    // constructor, getters, setters, toString...
    }

    View Slide

  67. public BlockCandidate getMiningJob() {
    String lastBlockHash = blocks.get(blocks.size() - 1)
    .getBlockHash();
    return new BlockCandidate(
    pendingTransactions, lastBlockHash);
    }

    View Slide

  68. public void submitBlock(Block newBlock) {
    if (isValidBlock(newBlock)) {
    blocks.add(newBlock);
    } else {
    throw new IllegalArgumentException("Invalid block");
    }
    }

    View Slide

  69. private boolean isValidBlock(Block block) {
    // …
    // intermediary calculations…
    // …
    return prevBlockHash.equals(lastBlock.getBlockHash()) &&
    calculatedBlockHash.equals(blockHash) &&
    Utils.startsWithZeroes(blockHash, difficulty);
    }

    View Slide

  70. public void mine(Blockchain blockchain) {
    // intermediary calculations…
    while (!startsWithZeroes(blockHash, difficulty)) {
    nonce++;
    String blockData =
    prevBlockHash + transactions + nonce;
    blockHash = Utils.calculateSHA256(blockData);
    }
    // submit mined block…
    }

    View Slide

  71. Longest Chain
    Consensus

    View Slide

  72. View Slide

  73. View Slide

  74. 51% Attack

    View Slide

  75. View Slide

  76. View Slide

  77. View Slide

  78. View Slide

  79. View Slide

  80. View Slide

  81. View Slide

  82. public class Blockchain {
    private List blocks;
    private List pendingTransactions;
    private int difficulty = 4;
    // constructors, getters, setters, toString…
    }

    View Slide

  83. TODO: Mining Rewards

    View Slide

  84. // …
    // TODO: Implement Mining Rewards
    // …

    View Slide

  85. • Live Demo

    View Slide

  86. • Link to app in github

    View Slide

  87. Act 3:
    Blockchain’s Evolution

    View Slide

  88. Data -> Code

    View Slide

  89. Data -> Code

    View Slide

  90. Data -> Code

    View Slide

  91. Data -> Code

    View Slide

  92. Data -> Code

    View Slide

  93. Smart Contracts

    View Slide

  94. In Sum…

    View Slide

  95. View Slide

  96. View Slide

  97. View Slide

  98. View Slide

  99. View Slide

  100. View Slide

  101. View Slide

  102. Data -> Code

    View Slide

  103. It all sounds great but…

    View Slide

  104. Blockchain is still a baby

    View Slide

  105. Blockchain the next big thing
    & people laughing

    View Slide

  106. The Earth is round & people
    laughing

    View Slide

  107. Now is the time for pioneers

    View Slide

  108. Now is the time for pioneers

    View Slide