Implementing a blockchain
What is blockchain?
Blocks of transactions, each with a link to the previous block and a hash of the previous block.
What is blockchain?
It is a system for achieving consensus, aligning incentives between adverserial parties.
What is it used for?
It is a distributed ledger which registers some transaction of values between a sender and receiver.
It is different from a traditional database because several nodes will communicate to validate the transactions of the chain.
How do blocks get created (and what is proof of work)?
A batch of transactions are “mined”, by nodes investing computational power.
This can be done by solving a cryptographic puzzle which is hard to solve, but easy to verify.
This cryptographic puzzle is called Proof of Work (PoW).
Why would anyone even want to spend computational power to do so?
Well, we can incentivise them! Creation of a new block is rewarded by a predefined amount of coins
How can we prevent people from altering it?
Can’t people simply swap blocks out?
First, we need to define a single source of truth.
To do so, we assert that the source of truth is the longest chain of blocks.
Next, since each block is linked to the previous one with a hash, this means that if we wanted to replace transactions, we need to create a chain which is longer than the current one.
Suppose we wanted to change a block with a depth of n=3.
This means we need to mine 3 more blocks to include a new transaction, much faster than all the other miners, so that our chain is the longest. This is very difficult, as while we are mining these 3 blocks, miners are advancing the longest chain, meaning that we are playing catch-up.
Wait! What are transactions??
Well a transaction is the movement of currency between two parties (a sender and recipient).
Why do we even want to group these up in blocks?
This is to prevent double spending.
Blocks have a timestamp (in the form of a block number). It is easy to verify within a block if there are duplicate transactions, and also if a transaction was included already in a previous block (making it illegitimate to spend again).
What is the implementation of PoW?
Recall earlier that PoW is used to mine blocks, and there was a mention of a “cryptographic puzzle” to do so.
Well, why can’t we just vote on which block should be the one, and based on majority votes, we decide. We need to decide what makes a vote valid, this is difficult to solve universally. Maybe we could issue keys? But how do we make this process equitable? Can we use existing identities: Email, IP addresses etc…? It is too easy to create many of these, especially if you’re a provider.
Hence we come up with a better solution in the form of cryptographic puzzle.
We first decide what properties are required:
We want it to be difficult to solve, easy to verify, so we should use NP-Complete problems.
Next we want to be able to vary difficulty as well, since hardware may become faster and incentives may change for running validation nodes.
Recall that we also want to establish a chain of trust, to prove that the newest block is indeed the head of a blockchain.
We propose a hashing problem, where we attempt to discover a value, which when hashed (e.g. with SHA256), gives us hash with a number of leading zeros.
The zeros can be adjusted, allowing us to adjust difficulty.
Discovering the value is exponential in the number of zeros, but easy to verify.
Establishing a chain of trust can be done by hashing this value (which is a proof that we solved the puzzle), and previous block hash.
How do we participate in the blockchain?
We need to be able to:
- Mine new blocks (if you’re a miner)
- Get list of pending transactions (if you’re a miner)
- Send a new transaction (anyone)