L3 Blockchain

Blockchain Ledgers

There’s nothing too complicated about the computer science of blockchains[1]. There are many ways to do it[2], but let’s make a simple example blockchain to explain.

Blocks

A block is a page of transactions to be put into a ledger:

The block’s data has to be serialized into plain text[3] so that it can be fed into a hash function:

Then the previous block hash is added:

 

This plain text is fed into a hash function to get the new block’s hash:

Genesis Blocks

A sequence of blocks must start somewhere – a genesis block. There’s no previous block hash to add, so its hash is hardcoded into the system:

 

Blockchains

When blocks are put in sequence, starting with their genesis block, the result is a blockchain – a ledger made from hash-chained blocks of transactions:

Cryptocurrencies

A cryptocurrency is a blockchain on a peer-to-peer network. Usually, each user will independently store their own copy of the blockchain and freely share blocks with other peers. New transactions go in new blocks that propagate virally through the network and get added to each user’s chain as they go.

 

Cryptocurrencies use coinbase transactions (which have no “from” field) to mint new money. Cryptocurrencies may be fiat money issued at will by privileged administrators, or they may be commodities that can only be mined through a timed algorithmic process outside of the control of anyone – more on that in a later course.

Forks

If someone changes a transaction in a block, they will have to make a new block with a different hash, and also rebuild the subsequent block hashes all down the line. This results in a forked blockchain, which makes it easy to tell where the split originates:

You don’t want forks to happen, though. Cryptocurrencies have various ways to try to prevent them. They usually consider the longest chain available to be the accepted one. They either restrict who is allowed to make new blocks (private blockchains) or make it take a certain amount of computing time to mine a new block (proof-of-work) – we’ll go into this later in a later course.

Merkle Trees

A Merkle tree is a data structure (invented by Ralph Merkle in 1979) used by cryptocurrencies to improve blockchain computing performance.

 

When you have a lot of transactions in each block, you’d have to churn through a huge amount of data just to verify and share blocks. For most blockchains, when making a new block, a Merkle root hash is calculated from the list of transactions:

The Merkle tree of transactions A, B, C, and D.[4]

 

You can’t change or reorder the list of transactions without changing the Merkle root hash.

 

Instead of having big blocks with every transaction in them, each block can now be split into a block header with just the Merkle root hash and a block body with the list of transactions.

 

Block headers are very small and can be kept in-memory, making it very fast to rescan and verify them. The big block bodies can be stored on disk and accessed as-needed.

 

For casual payments, simplified payment verification nodes don’t even need to store all the block bodies – they request them as-needed from other nodes. This isn’t quite as secure as keeping a full blockchain yourself, but it’s much much faster. To verify a single transaction, you only need the block headers and nearby transactions:

The entire dataset doesn’t need to be downloaded to verify the integrity of Transaction 5.

Conclusion

This course covered the historical trends in finance and ledger technology leading up to the blockchain data structure that powers cryptocurrencies. Future courses will cover different methods of organizing and implementing blockchain mining and cryptographic transactions.

[1] For a blockchain implementation in ~200 lines of code, see Naivechain: https://github.com/lhartikk/naivechain – you’ll revisit something like this for your capstone project.

[2] For the classic introduction, see the Bitcoin whitepaper: S. Nakamoto, Bitcoin: A Peer-to-Peer Electronic Cash System, 2009. https://bitcoin.org/bitcoin.pdf

[3] A byte string in an unambiguous format. JSON serialization format shown.

[4] Image source: S. Ray, Merkle Trees, 2017. https://hackernoon.com/merkle-trees-181cb4bc30b4

Introduction to Blockchains: Assignments

L1-L3: Introduction, History, Blockchain

For Review

Blockchains, blocks, payment gateways

This section deals with the uses of blockchain technology and understanding it’s structure.

 

Written assignments

  • Blockchain has a wide variety of applications, design a use case for Blockchain technology in the supply chain and explain the costs / benefits to the organization.

 

  • Find implementations of a consortium blockchain, a private blockchain, a semi-private blockchain, and a public blockchain. Then find ways you could do those same tasks without a blockchain, is it better or worse.

 

  • In this digital era, there exists a growing amount of increasingly diverse ways for people to process their transactions, pick 2 different payment types that could be implemented by a local merchant in a small online store. measure the speed – cost – ease – adoption – liquidity of each, compare them and come to a conclusion as to which one you would recommend.

 

Assigned Readings

  1. Investopedia: “What is a Blockchain?”
    https://www.investopedia.com/terms/b/blockchain.asp

 

  1. Square: History of Money and Payments
    https://squareup.com/townsquare/history-of-money-and-payments

 

  1. Finder: Blockchain Guide
    https://www.finder.com/blockchain-guide

 

Additional Supporting Material - References