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

Noding a Blockchain

Noding a Blockchain

Blockchains and cryptocurrency are all the hot rage these days. But if you're like me, it all seems like a bunch of voodoo magic math and super complicated algorithms.

So I decided the best way to de-mystify and learn it was to build my own blockchain and cryptocurrency. And of course, I started with Node! I'm going to walk you through some of my code and show a few bits of intermediate-to-advanced Node that I had to tackle to build my blockchain.

Topics covered will include: efficient asynchronous logic, SHA256 hashing and RSA cryptography, mesh communications via UDP and TCP messages, streams, offloading work to child processes, and persisting data in Git.

Kyle Simpson
PRO

April 19, 2018
Tweet

More Decks by Kyle Simpson

Other Decks in Programming

Transcript

  1. NODING A BLOCKCHAIN
    KYLE SIMPSON [email protected]

    View Slide

  2. Private Databases

    View Slide

  3. View Slide

  4. • Read/write access is private
    • Data is (usually) mutated
    • Sharing and backups:

    replication
    • All stakeholders are trusted

    View Slide

  5. • Private/sensitive data
    • "Single" point of failure (SPOF)
    • Target for hacking
    • Load balancing, eventual

    consistency

    View Slide

  6. Public Databases

    View Slide

  7. View Slide

  8. • Read/write access is public
    • Data is never mutated
    • No stakeholders are trusted

    View Slide

  9. • Public data
    • Trust based on math
    • Essentially zero failure
    • "50% + 1" weakness
    • Never consistent, consensus

    complications

    View Slide

  10. What is a blockchain?

    View Slide

  11. 0
    1
    2
    3
    A chain of blocks.

    View Slide

  12. 0
    1
    B22D1D8...89E2B87
    BCC29FC...E03F7E0
    B22D1D8...89E2B87

    View Slide

  13. 0
    1
    B22D1D8...89E2B87
    {
    index: 1,
    hash: "BCC29FC...E03F7E0,
    prevHash: "B22D1D8...89E2B87",
    data: ... ,
    timestamp: 1524029051480,
    }

    View Slide

  14. {
    index: 1,
    prevHash: "B22D1D8...89E2B87",
    data: ... ,
    timestamp: 1524029051480,
    }
    SHA256(
    );
    // "BCC29FC...E03F7E0"
    MATH!

    View Slide

  15. 0
    1
    B22D1D8...89E2B87
    BCC29FC...E03F7E0
    B22D1D8...89E2B87

    View Slide

  16. {
    index: 1,
    prevHash: "B22D1D8...89E2B87",
    data: ... ,
    timestamp: 1524029051480,
    }

    View Slide

  17. {
    index: 1,
    prevHash: "B22D1D8...89E2B87",
    data: ... ,
    timestamp: 1524029051480,
    }
    data: [
    { hash: "C37AF10...0040A18", data: ... , timestamp: ... , ... },
    { hash: "B9DC4FC...91F59BD", data: ... , timestamp: ... , ... },
    ... ,
    ],
    {
    index: 1,
    prevHash: "B22D1D8...89E2B87",
    timestamp: 1524029051480,
    }

    View Slide

  18. Timestamps
    In blockchains, purely
    informational

    View Slide

  19. Code!

    View Slide

  20. View Slide

  21. View Slide

  22. View Slide

  23. View Slide

  24. View Slide

  25. Hashes

    View Slide

  26. Rewrite history?
    Recompute All Hashes!

    View Slide

  27. Chaos

    View Slide

  28. Make recomputation of all
    hashes in the whole chain
    "hard"!
    (aka "not worth it")

    View Slide

  29. Consensus
    50% + 1

    View Slide

  30. Proof of Work
    Proof of Stake
    ...

    View Slide

  31. "Hard" math problem
    Solution: brute force
    "Mining"

    View Slide

  32. View Slide

  33. Transactions

    View Slide

  34. Authorization

    View Slide

  35. Private
    Key
    Public

    Key
    Asymmetric Cryptography

    View Slide

  36. Private
    Key
    Public

    Key
    Asymmetric Cryptography

    View Slide

  37. Private
    Key
    Public

    Key
    "some secret text"
    Encrypt
    Decrypt
    "some secret text"
    "jn4ra...bBH3/sKn"
    What I always thought...

    View Slide

  38. Private
    Key
    Public

    Key
    "some secret text"
    Encrypt
    Decrypt
    "some secret text"
    "jn4ra...bBH3/sKn"
    How it actually works...

    View Slide

  39. Public

    Key
    Private
    Key
    "some public text"
    Sign
    Verify
    "c7/De0...fhJjK3"
    Digital Signatures

    View Slide

  40. View Slide

  41. View Slide

  42. Node Code!

    View Slide

  43. View Slide

  44. View Slide

  45. View Slide

  46. github.com/getify/CAF

    View Slide

  47. View Slide

  48. View Slide

  49. View Slide

  50. View Slide

  51. NODING A BLOCKCHAIN
    KYLE SIMPSON [email protected]
    THANKS!!!!

    View Slide