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. Where’s my money scenario
    \w friend

    View full-size slide

  2. Where’s my money scenario
    \w friend

    View full-size slide

  3. Where’s my money scenario
    \w friend

    View full-size slide

  4. Where’s my money scenario
    \w friend

    View full-size slide

  5. Where’s my money scenario
    \w friend

    View full-size slide

  6. Where’s my money scenario
    \w friend

    View full-size slide

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

    View full-size slide

  8. TODO: Neighborhood picture

    View full-size slide

  9. A Centralized
    Approach

    View full-size slide

  10. TODO:
    Centralized
    Problems

    View full-size slide

  11. TODO:
    Centralized
    Problems

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  17. Fake Transactions

    View full-size slide

  18. TODO: Rebecca buying a bike
    from Peter

    View full-size slide

  19. // …
    // TODO: Implement Signatures
    // …

    View full-size slide

  20. Making the Ledger
    Immutable

    View full-size slide

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

    View full-size slide

  22. Achieving
    Consensus

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  27. 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 full-size slide

  28. Longest Chain
    Consensus

    View full-size slide

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

    View full-size slide

  30. TODO: Mining Rewards

    View full-size slide

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

    View full-size slide

  32. • Live Demo

    View full-size slide

  33. • Link to app in github

    View full-size slide

  34. Act 3:
    Blockchain’s Evolution

    View full-size slide

  35. Data -> Code

    View full-size slide

  36. Data -> Code

    View full-size slide

  37. Data -> Code

    View full-size slide

  38. Data -> Code

    View full-size slide

  39. Data -> Code

    View full-size slide

  40. Smart Contracts

    View full-size slide

  41. Data -> Code

    View full-size slide

  42. It all sounds great but…

    View full-size slide

  43. Blockchain is still a baby

    View full-size slide

  44. Blockchain the next big thing
    & people laughing

    View full-size slide

  45. The Earth is round & people
    laughing

    View full-size slide

  46. Now is the time for pioneers

    View full-size slide

  47. Now is the time for pioneers

    View full-size slide