Xiaobai's personal notes on learning blockchain from scratch (2) First acquaintance with the distributed system of Bitcoin

Chapter 2 Getting to know the distributed system of Bitcoin

2.1 The original purpose of Bitcoin

a. The stage of currency development

Barter-> physical currency (mainly metal currency)-> paper currency-> centralized bookkeeping currency.
Centralization has the problem of trust in electronic payment, see below

b. Two basic issues in the electronic payment industry

1. How can I transfer money online between two parties in need without using an authoritative third party?
2. How to record payment information while conducting electronic transfer to avoid double payment and ensure the safety of funds?

c. The birth of Bitcoin

In order to solve the above two problems, in 2008, a person who changed his name to Satoshi Nakamoto published a white
paper titled ** "A Peer-to-Peer Electronic Cash Payment System" , which proposed two questions There are two solutions: 1. In personal-to-individual transfers, there needs to be an electronic currency that can judge the value of it without being attached to a third-party pricing agency **.
2. We need to have a center of digital books to go , be able to trade information and transaction records stored distributed to computers around the world
and therefore, along with the decentralization to expectations, Bitcoin came into being.
Bitcoin is the source of the rise of blockchain technology and the earliest and most successful application of blockchain technology. Blockchain has also entered the eyes of the broad masses of people

2.2 Three basic concepts of Bitcoin (wallet, address, private key)

a. Relationship between user, private key, public key, and address

Public key and private key: The public key and private key are a set of concepts used together, and there is a close relationship between them. Simply put, the private key is randomly generated by the system, and the public key is calculated by the private key; the public key is responsible for encryption, the private key is responsible for decryption; the private key is responsible for signature, and the public key is responsible for verification.
Bitcoin actually exists on the user's address .
This process cannot be reversed.
Personal understanding

b. The transaction process

Transaction: The act of transferring the bitcoin from one address to another using the private key held by the miner
1. First assume that A wants to transfer bitcoin to B, where CD is a passerby
Insert picture description here
2.
If A sends B some bitcoin, then This transaction has three items of information:
1. Enter. It records from which address the coins owned by A were originally transferred to A.
2. Number. This is how many bitcoins A transferred to B.
3. Output. That is B's Bitcoin address.
In addition to the first transaction being miners' mining proceeds, each transaction has one or more inputs and one or more outputs.
Insert picture description here
Among them, there are three points to add
1. The encryption of the private key records the output of the transaction, here A. This ensures that every transaction can be traced back
2. The verification of the public key becomes a public key script, ensuring that the receiving address of the transaction must be the address of B

2.3 Asymmetric encryption and how to avoid false accounting

I.e. asymmetric encryption to encrypt and decrypt the same algorithm which irreversible
operation principle of asymmetric encryption information transfer among:

1. A wants to send information to B. Both A and B must generate a pair of public and private keys for encryption and decryption.
2. A's private key is kept secret, and A's public key is told to B; B's private key is kept secret, and B's public key is told to A.
3. When A wants to send information to B, A encrypts the information with B's public key, because A knows B's public key.
4. A sends the message to B (the message has been encrypted with B's public key).
5. After B receives this message, B decrypts A's message with his private key. No one else who received this message can decrypt it, because only B has B's private key.

Insert picture description here

2.4 Hard-to-tamper hash algorithm

1. Features of Hash Algorithm

Features of Hash Algorithm
➢ Unidirectional
➢ Calculate a fixed-length hash based on a message of any length
➢ Different inputs have different outputs
➢ The algorithm is highly efficient, and the time
hash function for calculating the hash value must have the following properties:
⚫ H can be used for "arbitrary" length messages. "Any" refers to what actually exists.
⚫ The hash value generated by H is of fixed length. This is the basic nature of the Hash function.
⚫ For any given message M, it is easy to calculate the H (M) value. This is the availability of hash functions.
⚫ Unidirectionality (antigenicity): For a given hash value h,
it is computationally infeasible to find M such that H (M) = h .

2. The role of hash pointers in the blockchain

That is, data encryption
Hash pointer: not only to save the position of the structure in memory but also to save the hash value of the structure
The main difference between the blockchain and the ordinary linked list is that the hash pointer is used instead of the ordinary pointer
Insert picture description here

Actual use A-encrypted transaction address

One-way hash is used. The top of the hash tree is the top hash, also known as root hash or master hash. It is to climb up the tree by connecting two sub-hashes in parallel until the root hash is found. Function:
1. Quickly locate each transaction
2. Verify whether the transaction data has been tampered with

Practical use B-the essence of mining

Mining: There is a parameter in the block header called a random number Nonce. The process of finding this random number is called "mining. The
bitcoin mining process uses SHA256 hash function to continuously calculate. Mining is to repeatedly calculate the hash value of the block header. Continue to modify the Nonce value until it meets the target hash value process. The result of the hash function is unpredictable, and there is no specific mode to quickly calculate the hash value.

2.5 Double spend problem and UTXO mechanism

Double flower problem

What is double flower?

A sum of money is spent twice or more, also called "double payment".
In the digital currency system, digital assets are essentially virtual numbers based on the Internet. In short, the so-called digital assets are actually a string of characters, so it is easy to be copied or sent multiple times. In this case, if there is no central organization, people cannot determine whether an asset has been spent, which leads to the situation that the same digital asset may be reused due to improper operation.

How to avoid double spending in the Bitcoin system? ——UTXO mechanism

1. What is UTXO?

• UTXO is the abbreviation of Unspent Transaction Outputs, the full name is "unused transaction output".
• U stands for Unspent, which means unpaid or unused. "Unpaid" means that the transaction output has not yet appeared on the input of other transactions.
• TX is an abbreviation of transaction. A transaction is a transfer from one bitcoin wallet to another. It is the only way to change the ownership of bitcoin, including the basic contents of input, number, and output.
• O is Output, which means output, and TXO means transaction output.

2. UTXO in Bitcoin transactions

Bitcoin's distributed blockchain ledger is formed by one transaction at a time. Each transaction requires an input to generate an output, and the output it generates is "unspent" "Transaction output", which is UTXO.

There is no concept of user accounts in Bitcoin.

We say how many bitcoins we actually refer to the number of bitcoins specified in UTXO that we own.
D has 14 bitcoins. In essence, in the current blockchain ledger, the UTXO item payees of several transactions wrote the address of D, and the total amount of these UTXO items is 14.
See the figure below for detailsInsert picture description here

2.6 Consensus algorithm and workload proof mechanism

What is consensus

xx (object) agree on xx (thing)

Consensus in Bitcoin

All nodes on which node can generate the next block to reach a unified view of
the bitcoin block chain consensus algorithm provides the next new block which is generated by the miners, at the same time, on this block chain to reach a deal, Some nodes selected by the consensus algorithm need to reach consensus.

Several consensus algorithms

• PoW (Proof of Work, Proof of Work)
• PoS (Proof of Stake, Proof of Stake)
• DPoW (Delegate Proof of Work, Proof of Work)
• DPoS (Delegate Proof of Stake, Proof of Stake)
• PBFT (Practical Byzantine Fault Tolerance, practical Byzantine fault tolerance algorithm)

Here we mainly explain the pow mechanism used by the Bitcoin network

Consensus mechanism in the Bitcoin network-proof of work mechanism

Features:
Users who perform actual work with asymmetry need to pay a lot of work to get a result that meets the specified conditions, but as a verifier, it can easily recalculate the result based on the materials provided by the user, and verify whether the result is satisfied Define good conditions in advance.

In the Bitcoin network, first of all, generate a new transaction information to be added to the blockchain, that is, the requirements that must be met when a new block is met. Then in the blockchain network based on the proof-of-work mechanism, all nodes begin to continuously try and calculate until the first node finds the numerical solution of the random hash hash, so it has the right to generate new blocks .
Advantages: Completely decentralized
Disadvantages: 1. Mining behavior causes a lot of waste of resources
2. Longer period required to reach consensus
Personal understanding: By giving a hash value, the encryption operation is continuously performed through exhaustive method until the original Expressions, and then it can be easily verified whether the result is correct

2.7 Block height, 51% attack, mining pool and computing power

Block and block height

A block is the basic unit that constitutes a blockchain, and is composed of a block header and a block body . The block header contains the hash information of the previous block, which can help the new block to connect with the previous block; and the block body contains all the transaction information during this period.
Insert picture description here

What is the block header

Insert picture description here

1) Version number (Version) is
used to identify the transaction version and the referenced rules. For example, Android 10, or iOS13, the block also has its own version number.
2) The hash value of the previous block is
also called the “hash value of the parent block”. This hash value is obtained by hashing the block header data of the previous block (SHA256 algorithm). Its significance lies in: Each newly mined block is connected to the back of the previous block in order; in this way, after the new block is dug, you can have a sip of peace of mind, because you know that this block is followed by a quilt Behind the reliable block verified by each node.
3) Merkle Root (Merkle Root)
As shown above, in the body of the block, all transaction information is first hashed in groups of two. This structure is called the Merkle Tree (Merkle Tree), and it is an inverted tree. tree.
We simplify the problem, assuming that there are 4 transaction information in the body of the block, namely transaction 1, transaction 2, transaction 3, and transaction 4. The Merkle tree first performs hash Hash calculation on each transaction information to obtain Hash 1, Hash 2, Hash 3, Hash 4 respectively.
At the second floor, like the class teacher arranges seats, two groups, that is to say, the strings of Hash 1 and Hash 2 sit in a row and become a double-length string, and then calculate the hash value of this string, Let's remember it as Hash (1 & 2); Hash 3 and Hash 4 are processed in the same way as above to get Hash (3 & 4), and then count up, the two strings Hash (1 & 2) and Hash (3 & 4) are merged again, and then hash operation The final hash value is the Merkle root of the block header.
As far as Bitcoin is concerned, the real situation is that a block body contains more than 4,000 strokes. In more cases, everyone will make inferences. In short, the two groups are continuously merged and hashed. Hopefully.
The Merkle tree can quickly check the integrity of transaction data, that is, whether the data has been tampered with. According to the characteristics of the hash function in cryptography, if someone has manipulated the data, the calculated hash value will become completely different. Even in these 4000 transactions, I only moved a decimal point, you can find the problem from the hash value of the Merkle root.
Insert picture description here
4) Timestamp (Time)
records the time this block was generated, accurate to the second. Each time a new block is born, it will be stamped with the corresponding timestamp, so as to ensure that the blocks on the entire chain are arranged in chronological order.
5) The difficulty value (Target_bits)
digs out the difficulty target of the block. Every 2016 blocks are generated, the data block calculation difficulty will be adjusted once. For example, the Bitcoin blockchain network can automatically adjust the difficulty of mining, allowing miners to dig a block every 10 minutes. Originally it took 14 days to dig up the 2016 block, but I do n’t know which sky-killing suddenly got a few mining machines that burst the computing power. It was done in 7 days, which means that when the scheduled adjustment period is reached, mining The difficulty will double.
6) Nonce
mining is equivalent to a miner doing a math problem. They use the hashrate of a computer or a mining machine to perform a large number of calculations (professionally called "hash collision") to try out a correct area. Block hashes; many times we also refer to mining as contention for billing rights.Insert picture description here

What is the main body of the block

The place used to record transaction information. In Bitcoin, each block stores about ten minutes of transaction information, and then the next block is generated, and the transaction information is then stored in the next block, and so on.
The 10-minute transaction information is determined by the average block generation speed, and will change according to the mining speed. For example, the Bitcoin setting allows miners to dig out a block every 10 minutes. Originally it took 14 days to dig up the 2016 block, but I do n’t know which god killed a few mining machines that suddenly burst the computing power. It took 7 days to complete, which means that when the scheduled adjustment period is reached, the system Automatically increase the difficulty of mining, so that the overall block generation speed tends to be stable for ten minutes

What is the block height

Block height is a concept used to identify the location of a block in the blockchain.
Insert picture description here

Mining pool

After combining a lot of computing power, the website formed by the joint operation

Computing power

Hash rate, also known as hash rate, is an index used to measure the ability to perform hash operations, or
the time required to perform a hash calculation. If the network reaches a hash rate of 10T hash / s (10T hash per second), it means that it can perform 10 trillion calculations per second.
Hash collision: The process of trying to solve random hash values.
The number of times a miner can make such collisions per second represents its computing power.
The more advanced the machines used by miners to mine, the higher the computing power.

51% attack and paradox

The computing power of someone in the entire network exceeds 50% of the entire network. Then he can try to modify the state of the blockchain, perform reverse transactions, and achieve double spending.
See https://www.jianshu.com/p/d6be6637edc1

Published 3 original articles · Likes0 · Visits 62

Guess you like

Origin blog.csdn.net/weixin_45067603/article/details/105444151