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. public class Transaction { private String sender; private String recipient;

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

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

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

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

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

    // … return prevBlockHash.equals(lastBlock.getBlockHash()) && calculatedBlockHash.equals(blockHash) && Utils.startsWithZeroes(blockHash, difficulty); }
  7. public void mine(Blockchain blockchain) { // intermediary calculations… while (!startsWithZeroes(blockHash,

    difficulty)) { nonce++; String blockData = prevBlockHash + transactions + nonce; blockHash = Utils.calculateSHA256(blockData); } // submit mined block… }
  8. public class Blockchain { private List<Block> blocks; private List<Transaction> pendingTransactions;

    private int difficulty = 4; // constructors, getters, setters, toString… }