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

Getting to know the blockchain in plain java!

Getting to know the blockchain in plain java!

The slides of our presentation about the Blockchain as shown during the DevMotion Meetup and JFall 2017.

Avatar for CvBeijsterveldt

CvBeijsterveldt

October 24, 2017

Other Decks in Programming

Transcript

  1. WHAT ARE WE GOING TO TALK ABOUT? The components of

    the blockchain explained with plain Java code No blockchain libraries / frameworks used!
  2. WHAT DOES BITCOIN INTEND TO DO? Solution for a payment

    system with two requirements: No central authority (not a single "boss") No double spending (you only live spend once)
  3. WHAT ARE TRANSACTIONS? Basically, anything! Definition in the broadest sense

    of the word Not necessarily monetary All transactions are saved (in Bitcoin: a ledger)
  4. OUR TRANSACTION Our "transaction" will be a simple message It

    will contain an index and and a String The index must be unique class Message { int index; String text; }
  5. VALIDATING AND HANDLING MESSAGES private final Set<Message> unhandledMessages; private final

    Set<Message> handledMessages; public void addMessage(final Message m) { if (!handledMessages.contains(m) && !unhandledMessages.contains(m)) { unhandledMessages.add(m); if (isReadyForNewBlock()) { createNewBlock(); } } }
  6. WHAT IS A NODE? Something that receives and processes transactions

    No central authority, we need distribution of power!
  7. DISTRIBUTED AUTHORITY A network of nodes, each running the Blockchain

    so ware All nodes are equal, no single one is 'in charge' Each node can operate individually A node is not (necessarily) connected to all others
  8. A WORLD WITHOUT BLOCKS Sending single transactions across the network

    is slow and expensive Solution: send them in batches
  9. CREATING BLOCKS public Block createBlock(Block parentBlock, Set<Message> messages) { String

    hash; String parentHash = parentBlock.getHash(); long nonce = random.nextLong(); do { hash = jChainHasher.hash(parentHash, messages, Long.toString(++nonce)); } while (!jChainHasher.isValidHash(hash) && state == RUNNING); return new Block(hash, parentHash, messages, Long.toString(nonce)); }
  10. ADDING CREATED BLOCKS private void addCreatedBlock(final Block block) { if

    (chain.getEndBlockHash().equals(block.getParentHash())) { chain.addBlock(block); informNodesOfNewBlock(block); } }
  11. THE PROBLEM Nodes aggregating transactions and building blocks individually Nodes

    will grow apart, all having different chains We need to make them agree on a single state Different solutions, we will discuss the way Bitcoin solved it
  12. ONE THING WE CAN ALL AGREE ON Bigger is better!

    (sorry, gentlemen) The longest chain wins
  13. PROOF OF WORK Requires a lot of computational power 'Investment

    of resources into the blockchain' Difficult to compute, but easy to verify Finding a proof of work is rewarded: mining
  14. THE ALGORITHM 1. A condition for a hash is set,

    e.g. any hash that starts with 17 '0's 2. Nodes start to calculate a hash that matches the condition, based on transactions and a nonce 3. A node finds such a hash, creates a block with it and informs the network 4. Other nodes verify the validity and accept the new longest chain
  15. AWESOME HASHING public boolean isValidHash(final String hash) { if (null

    == hash) { return false; } final int jIndex = hash.indexOf('J'); final int cIndex = hash.indexOf('C'); final int oIndex = hash.indexOf('o'); final int rIndex = hash.indexOf('r'); final int eIndex = hash.indexOf('e'); final boolean didFindJcoreCharacters = jIndex != -1 && cIndex != -1 && oInd final boolean jcoreCharactersInCorrectOrder = jIndex < cIndex && cIndex < o return didFindJcoreCharacters && jcoreCharactersInCorrectOrder; }
  16. CREATING BLOCKS public Block createBlock(Block parentBlock, Set messages) { String

    hash; String parentHash = parentBlock.getHash(); long nonce = random.nextLong(); do { hash = jChainHasher.hash(parentHash, messages, Long.toString(++nonce)); try { // Don't try this at home! Thread.sleep(parameterService.getDifficulty()); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } } while (!jChainHasher.isValidHash(hash) && state == RUNNING); return new Block(hash, parentHash, messages, Long.toString(nonce)); }
  17. ACCEPTING BLOCKS public void addBlock(final Block block) { final String

    hash = jChainHasher.hash(block.content()); if (jChainHasher.isValidHash(hash) && block.getHash().equals(hash)) { final String lastBlockHash = chain.getEndBlockHash(); chain.addBlock(block); if (!chain.getEndBlockHash().equals(lastBlockHash)) { if (blockCreatorService.getState() == RUNNING) { blockCreatorService.cancelRun(); } resetMessagesAccordingToChain(); if (isReadyForNewBlock()) { createNewBlock(); } } } }
  18. INTERESTING THOUGHTS Blocks are not forever (necessarily) You make the

    longest chain, you are the king of the hill 51% attack Cracking the hash Distributed computing resources + proper hashing algorithm = happy Bitcoin
  19. SOFTWARE CHANGES Changes can be breaking / non backwards compatible

    Nodes with non-compatible so ware cannot communicate Nodes are never forced to update (no central authority!) What now?
  20. THEY CAN COEXIST Two networks, two different directions You can

    have money in both chains One "loses" if people are leaving it