# 1. Architecture

To understand the blockchain, let us start with Bitcoin from the very beginning.

Bitcoin is essentially a globally unified distributed ledger that records all transaction records. Just based on all your transaction records, you can know how much money you have now.

It's easy to get a ledger, but it's difficult to get the world to trust this ledger. Let's take a look at how Bitcoin does it.

The technology supporting Bitcoin can be roughly divided into three levels from bottom to top:

• The first layer of p2p network layer. The main problem that this layer solves is how distributed nodes find themselves and how to communicate between nodes to form a network.
• The second layer of consensus storage layer. How do so many nodes around the world reach a consensus and jointly maintain the only one ledger, and others can't make trouble.
• The third-level ledger model. How to keep the account reasonable?

# 2. the ledger model

## 2.1 Transaction

There is only one thing recorded in Bitcoin's ledger, which is "transactions."

The above figure records three transactions. Each transaction has two main parts: Transaction input: It records where the transaction money comes from. Transaction output: where will the input money go.

Let s look at these three transactions: Transaction No. 1001: The general transaction input is filled in with the output of the transaction that has been recorded , while mining is a special transaction. The input does not need to refer to other transaction outputs , so the Bitcoin ledger The money in China is derived from mining transactions, and other transactions will only be transferred and will not generate money out of thin air. In this transaction, Zhang San received 12.5 coins.

Transaction No. 2001: Transaction input refers to the first output of Transaction No. 1001, and there are two outputs: 2.5 for Li Si and 10 for Zhang San. That is, Zhang San gave Li Si 2.5 of the 12.5 points obtained from mining, and left 10 for himself. Generally, the sum of transaction input is equal to the sum of transaction output , so Bitcoin often has this kind of self appearing in transaction output called change .

Transaction No. 3001: There are two transaction inputs and two transaction outputs. Li Si and Zhang San transferred money to Wang Wu together.

## 2.2 How do I know how much money I have

Now suppose you are Zhang San, do you know how much balance you have now, and what should you do if you want to transfer 3 coins to Li Si?

We already know that the input of each transaction is the output of the previous transaction. The so-called spending is to construct a transaction to refer to the previous transaction output, so the balance in Bitcoin is called UTXO (unspent transaction output). To know how much balance you have, you only need to find that the transaction output is your account number, and the sum of all transaction outputs that are not referenced by other transaction inputs is your entire balance.

If you want to transfer 3 coins to Li Si, you will find your UTXO to fill in the transaction input, and Li Si s account number to fill in the transaction output, construct a transaction and publish it to the Bitcoin network and wait for the miner to pack it. When the transaction is packaged and released to the network and recognized, your transfer will be successful, and Li Si will have 3 coins of UTXO.

## 2.3 How to prove that the money is yours

Bitcoin transaction output is public, so how can it be guaranteed that only I can use my transaction output? , and others will not use my money? First of all, let's take the knowledge of asymmetric encryption under popular science.

In asymmetric encryption, the public key and the private key exist in pairs. Only the matching private key can be unlocked for public key encryption, and only the matching public key can be unlocked for private key encryption. **Encryption:** Generally speaking, we release the public key, and the person who wants to send information to you will use your public key to encrypt the information and send it to you. You can decrypt it with the private key to see the real information. And people who don't have the private key can't solve the ciphertext all the time. **Signature:** You use your private key to encrypt what you have said, and others can use your public key to verify that it is not what you said, because only your private key can encrypt things with your public key. Untie it, no one else can forge it, and you won t get your private key for others.

In Bitcoin, the public key can be regarded as your account. You can keep the private key privately. When someone transfers money to you, put your public key in the transaction output . If you want to use this UTXO has to use your private key to provide a signature proof that you have the private key corresponding to the public key in order to be recognized by the miners and recognized by the world. In the end, your money can be counted as spent.

## 2.4 Smart contract

The above picture is what a transaction actually looks like. Each transaction output contains a locking script , and each transaction input contains an unlocking script . The script here refers to the kind of programming script, which can run a program code. The ordinary transaction locking script means that the unlocking script can provide a signature that matches its own public key, then you can use the UTXO. After you construct the unlocking script and the transaction, the miners will run the locking script and the unlocking script to verify whether your transaction is legal or not, and decide whether to package it.

Do you see where Bitcoin is the best way to implement smart contracts? For example, if your dad wants to give you a sum of money, but wants your mom to agree when you use it, then your dad can construct a transaction and write it in the lock script to use your mom and your signature at the same time. Money, so you have to get your mom s signature to construct the unlocking script , and then you can use the money. Here again, we can see that Bitcoin's concept of accounts is relatively weak: as long as you can give the unlocking script and match the locking script, you can use this UTXO, there is no concept of an account. With the lock script and unlock script, we can do a lot of extended things.

Reference: How to lock and unlock scripts to run

# 3. Consensus storage

Above we have already known how to construct a legal transaction, how to form a legal ledger with so many transactions, and where should the ledger be safe.

## 3.1 Ledger-Blockchain

The ledger is just like this. We wrap a bunch of transaction records into each of the small squares in the picture above. Such squares are called blocks, and then connect them end to end to become a blockchain. Isn t it easy? Every Each block records the hash value of the previous block, which makes it have such a feature: if you want to change one of the transaction records, the hash of this block will change, which will cause the hash of the next block to change. It produces a series of reactions, so it is harder to tamper with the longer history.

## 3.2 Where to put the ledger

Where should such a ledger be kept?

Bitcoin s strategy is to let everyone save and maintain it. When someone digs out a new block, they will roar. Everyone will verify if it is true. If the verification is successful, the ledger will be updated, so that the historical ledger is almost It cannot be tampered with, unless you change everyone's.

## 3.3 Mining

The history cannot be changed, so how do we write new blocks?

Assuming that each of us can write, wouldn't it be very confusing. You want to add a block B to A, but he wants to add a block C to A, so you can't maintain a single ledger.

Bitcoin thought of a way so that only one person can write each new block. Who will write it? Bitcoin said that every time I post a question, whoever solves it first will have the right to write this new block. Then everyone came to solve the problem. This problem is difficult to solve (it may take more than ten minutes), but it is easy to verify (you will know if you are doing it right at first glance). Which ones do not solve the problem but think People who make up for it are easy to be seen by everyone and cannot be recognized by everyone.

The process of solving problems and gaining the right to bookkeeping is called mining. Bitcoin's model of consuming computing resources to calculate the problem and gaining the right to bookkeeping is also called proof of work, which means that I have to let me see if I want to keep accounts. Your hard work, the harder you work, the more chance you will get the right to bookkeeping.

## 3.4 Consensus

With so many introductions, everyone should be able to understand the meaning of the word "consensus". The system established by Bitcoin is actually just a set of rules. Everyone believes in and abides by this set of rules, and resists not. People with rules, then Bitcoin can exist consistently.

# 4. summary

Speaking of this, everyone should understand the basic principles of Bitcoin. As for the p2p network it uses, it is a new world.

From the perspective of a distributed system, Bitcoin mining is actually a leader election process. Only the leader has write permissions, and the slave nodes will synchronize after writing.

Blockchain has brought a lot of new things to the world, please share your opinion in the comment section.