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

Af78adc6caf494f74c1e4fb10a15c74a?s=128

Preslav Mihaylov

November 27, 2018
Tweet

Transcript

  1. None
  2. Where’s my money scenario \w friend

  3. Where’s my money scenario \w friend

  4. Where’s my money scenario \w friend

  5. Where’s my money scenario \w friend

  6. Where’s my money scenario \w friend

  7. Where’s my money scenario \w friend

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

  10. None
  11. None
  12. None
  13. None
  14. A Ledger

  15. TODO: Neighborhood picture

  16. A Centralized Approach

  17. None
  18. TODO: Centralized Problems

  19. None
  20. TODO: Centralized Problems

  21. None
  22. None
  23. None
  24. None
  25. None
  26. None
  27. None
  28. None
  29. None
  30. None
  31. None
  32. None
  33. public class Transaction { private String sender; private String recipient;

    private int amount; // constructor, getters, setters, toString... }
  34. public class Block { private final List<Transaction> transactions; // constructor,

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

    setters, toString... }
  36. None
  37. None
  38. None
  39. None
  40. None
  41. None
  42. None
  43. None
  44. public class Blockchain { private List<Block> blocks; private List<Transaction> pendingTransactions;

    // constructor, getters, setters, toString... }
  45. // … // TODO: Implement Peer Synchronization // …

  46. Fake Transactions

  47. TODO: Rebecca buying a bike from Peter

  48. None
  49. None
  50. // … // TODO: Implement Signatures // …

  51. Making the Ledger Immutable

  52. None
  53. None
  54. None
  55. None
  56. public class Block { private final List<Transaction> transactions; private final

    String prevBlockHash; private final String blockHash; // constructor, getters, setters, toString... }
  57. Achieving Consensus

  58. None
  59. None
  60. None
  61. None
  62. None
  63. None
  64. None
  65. None
  66. public class Block { private final List<Transaction> transactions; private final

    long nonce; private final String prevBlockHash; private final String blockHash; // constructor, getters, setters, toString... }
  67. public BlockCandidate getMiningJob() { String lastBlockHash = blocks.get(blocks.size() - 1)

    .getBlockHash(); return new BlockCandidate( pendingTransactions, lastBlockHash); }
  68. public void submitBlock(Block newBlock) { if (isValidBlock(newBlock)) { blocks.add(newBlock); }

    else { throw new IllegalArgumentException("Invalid block"); } }
  69. private boolean isValidBlock(Block block) { // … // intermediary calculations…

    // … return prevBlockHash.equals(lastBlock.getBlockHash()) && calculatedBlockHash.equals(blockHash) && Utils.startsWithZeroes(blockHash, difficulty); }
  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… }
  71. Longest Chain Consensus

  72. None
  73. None
  74. 51% Attack

  75. None
  76. None
  77. None
  78. None
  79. None
  80. None
  81. None
  82. public class Blockchain { private List<Block> blocks; private List<Transaction> pendingTransactions;

    private int difficulty = 4; // constructors, getters, setters, toString… }
  83. TODO: Mining Rewards

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

  85. • Live Demo

  86. • Link to app in github

  87. Act 3: Blockchain’s Evolution

  88. Data -> Code

  89. Data -> Code

  90. Data -> Code

  91. Data -> Code

  92. Data -> Code

  93. Smart Contracts

  94. In Sum…

  95. None
  96. None
  97. None
  98. None
  99. None
  100. None
  101. None
  102. Data -> Code

  103. It all sounds great but…

  104. Blockchain is still a baby

  105. Blockchain the next big thing & people laughing

  106. The Earth is round & people laughing

  107. Now is the time for pioneers

  108. Now is the time for pioneers