blockchain
blockchain is a center-less distributed database.
- every server can join the blockchain as a node.
- every node should save the full database by synchronization. (bitcoin database is around 100G now)
- once the data are written into the database, they cannot be modified (unless one has control over more than 51% nodes).
Algorithm
blockchain is a single-direction linked list (chain) of blocks.
each block contains header and body.
- header:
- Timestamp: when is this block generated.
- Prev hash: block hash of the previous block.
- Merkle hash: hash of the data.
- ...
- body: the data.
blockchain usually uses SHA-256 as the hash algorithm.
There are three hashes used here:
- Merkle hash: the hash of the body, by treating all records as leaf nodes of a binary tree, and finally generating the hash of the root node as the Merkle hash (Merkle tree). This hash is used to assure the body is not modified once generated.
- Block hash: the hash of the header, not written in the block, but computed dynamically.
- Prev hash: the block hash of the previous block, work as a pointer.
We first assume we can synchronize the new block for all nodes, then once a block is generated, the content of this block cannot be changed, since:
- if the body's data are changed, the Merkle hash of this specific block also changes.
- then the header is changed, and the Block hash of this specific block also changes.
- one have to change all the blocks after this specific block to keep the blockchain connected, while this is impossible for a center-less database (unless he controls 51% computation resources among all the nodes).
But how to make the synchronization for all nodes?
We have to slowdown the speed of block generation.
Blockchain is designed to be updated every 10 minutes, by making the computation of new block's hash extremely difficult.
And this computation progress is called mining.
Mining is the procedure that writes data into a blockchain.
In fact, it is very expensive to maintain a blockchain, and maybe the only practical application of blockchain is bitcoin.
Bitcoin
Bitcoin protocol requires each block to be at most 1MB. (around 2000 transactions)
All the transaction records are sent to miners, and the miners choose which records to be written to blockchain, so the Merkle hash is determined.
Then it is time to mine, with some extra information in the header:
-
difficulty: a parameter to control the speed of block generation (to make block is generated every 10 minutes in average).
Only if the block hash is smaller than the target (
TARGET_MAX / difficulty
), the block can be accepted as a new block by the blockchain.This parameter is dynamically generated for the next block by the protocol.
- Nonce: A random value to guess/mine, to make the block hash smaller than target.
Miners start to guess the nonce, and once the block hash is smaller than the target, this block is accepted to the blockchain, and starts to be synchronized to all nodes.
If two block are generated nearly the same time so both are accepted, the blockchain will check which fork first reaches 6 length, and only the longer one is kept. (so each transaction is confirmed in at least 10 minutes, at most 1 hour)
So why do they mine?
Mining is very expensive, and is in fact meaningless.
But blockchain needs miners to write data.
There are 2 ways for miners to get profits:
-
Bitcoin protocol rewards miners who successfully write a block, by giving them bitcoins.
this amount is 50 in 2008, and halves every 4 years. (and it becomes 0 in 2140, since bitcoins' precision is .8f)
All of the bitcoins are generated by this mining reward, so the total amount of bitcoins will be fixed at 2140.
-
Service charge of transaction.
Each transaction can set an amount of bitcoins to reward the miners that write it.
Of course, miners will choose transactions of high service charge in priority.
How to make a valid transaction?
Digital Signature technology is used here.
Problems of cryptocurrency
- Not real time. (10~60 minutes)
- Slow. only 2000 transactions in 10 minutes.