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

April 19, 2018
Tweet

More Decks by Kyle Simpson

Other Decks in Programming

Transcript

  1. NODING A BLOCKCHAIN
    KYLE SIMPSON [email protected]

    View full-size slide

  2. Private Databases

    View full-size slide

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

    replication
    • All stakeholders are trusted

    View full-size slide

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

    consistency

    View full-size slide

  5. Public Databases

    View full-size slide

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

    View full-size slide

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

    complications

    View full-size slide

  8. What is a blockchain?

    View full-size slide

  9. 0
    1
    2
    3
    A chain of blocks.

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

  16. Timestamps
    In blockchains, purely
    informational

    View full-size slide

  17. Rewrite history?
    Recompute All Hashes!

    View full-size slide

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

    View full-size slide

  19. Consensus
    50% + 1

    View full-size slide

  20. Proof of Work
    Proof of Stake
    ...

    View full-size slide

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

    View full-size slide

  22. Transactions

    View full-size slide

  23. Authorization

    View full-size slide

  24. Private
    Key
    Public

    Key
    Asymmetric Cryptography

    View full-size slide

  25. Private
    Key
    Public

    Key
    Asymmetric Cryptography

    View full-size slide

  26. Private
    Key
    Public

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

    View full-size slide

  27. Private
    Key
    Public

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

    View full-size slide

  28. Public

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

    View full-size slide

  29. github.com/getify/CAF

    View full-size slide

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

    View full-size slide