Blockchain study notes - simple introduction

0x01 Intro

The blockchain is an immutable, distributed, decentralized ledger. No one is the owner of the ledger, because everyone is the owner of the ledger, and everyone’s ledger writes exactly the same thing, remember everyone. asset information. (This is probably our basic understanding of it)

In the blockchain, each block contains a bunch of transaction records (that is, accounts), and the information in the previous block (transaction serial number, accounting (transaction) time, transaction records) will be used as the original information, the value after the hash is combined with the current account page information to become a block (this data structure is similar to a linked list - in short, it can not only store the data from the previous node, but also connect the next a node).

And so on, the next block records the hash information of the previous block, so when the chain becomes quite long, it becomes very difficult to tamper with it.

It can be considered that the immutability is guaranteed by the hash value, chain length, and computing power of the entire network.

Let's look at an example of a block:
insert image description herethe boxes from top to bottom are:

  • the hash value of this block
  • Birth time of this block
  • Height (number of blocks since creation)
  • random number
  • Reward for wrapping this block (6.25btc)

0x02 Bitcoin Operation Overview

First: Currency has:

  • Value scale (money is the material that expresses value)
  • The means of circulation (how much money is appropriate? The generation of paper money as a currency symbol)
  • Hoarding Means (Commodities are sold for the purpose of substituting the monetary form for the commodity form rather than buying the commodity, the currency hardening into a hoard currency)
  • means of payment
  • world currency

and so on properties.

In the process of currency evolution from shells and precious metals to paper money, the reason why it still has the functions of a value scale and a means of circulation is that it is issued by the government and endorsed by the government's credit.

Banks exist to provide a reliable bookkeeping (maintenance) mechanism.

However, the reason why money becomes money is that some people recognize its function as a measure of value. The government's endorsement and compulsory issuance are only for general promotion, and then to strengthen governance and management.

Here, governments and banks are equivalent to centralized certification and maintenance organizations.

Blockchain-Bitcoin can be issued and circulated through the global P2P network without the need for central trust institutions like governments and banks. Counterfeit currency system.

It allows you to send money to anyone in the world without the need for a central authority to issue accounts or process payments.

For example, traditional financial remittance requires both parties to use accounts on the same platform, and all transactions are controlled by the platform's system. eg Alipay and WeChat Pay.

Bitcoin achieves decentralization through technical means, and solves the problem of clearing and settlement of transactions after electronic cash.

0x03 P2P

P2P is generally referred to as a peer-to-peer network or a peer-to-peer network. In the P2P network environment, multiple computers connected to each other are in a peer-to-peer position. Each computer has the same function, and there is no master-slave distinction. A computer can be used as a server or a workstation. This is very different from the traditional Client/Server model.

Features

  • Direct interaction
    P2P is to connect people directly, allowing people to interact directly through the Internet. P2P makes communication on the web easier, more direct sharing and interaction, truly eliminating middlemen.
  • Another important feature of decentralized
    P2P is to change the current Ethernet station-centric state of the Internet, return to "decentralization", and return power to users.
  • The performance-expanding
    peer-to-peer network is a successful extension of the distributed concept. It allocates the server burden in the traditional way to each node in the network. Each node will undertake limited storage and computing tasks. The more nodes, the more resources the nodes contribute, and the higher the quality of service.
  • Robust
    P2P architecture is inherently resistant to attacks and high fault tolerance. Since the service is distributed among the various nodes, the disruption of some nodes or the network has little impact on other parts. Generally, P2P networks can automatically adjust the overall topology (the arrangement of nodes) when some nodes fail to maintain the connectivity of other nodes. P2P networks are usually established in an ad hoc manner and allow nodes to join and leave freely.
  • Privacy Protection
    In P2P, all participants can provide the function of relaying and forwarding, thus improving the flexibility and reliability of anonymous communication and providing users with better privacy protection.

0x04 digital signature

The technical principles of asymmetric encryption and digital signatures can be found in any computer network textbook.

Here we only briefly describe the principle:

How do I prove that a file is written by me: I first calculate a Hash value for the file, then encrypt the hash with my private key to generate a digital signature, and then others use my public key to unlock it, and then make a Hash, compare the two hash values ​​to know if this file is written by me.

The reason for not hashing the entire file directly is that asymmetric encryption is too expensive.

In the actual record of the blockchain, the private key is used to digitally sign the transaction data and put it behind the record.

The signature and transaction must match or they will be rejected.

0x05 block

Every transaction is broadcast to miners through the P2P network.

Miners then package these transactions into candidate blocks, which are then added to the blockchain.

In addition to the transaction data, this candidate block will also have some metadata, such as the block header, including the version, the information of the previous block, the number of transactions, the timestamp, and the target value.

Metadata: If we regard the block as a file in Windows, the metadata is the attribute of the file.

Metadata includes:

  • version: protocol type
  • last block: the identifier (hash) of the previous block
  • merkle root: the hash of this block, ensuring it cannot be changed
  • time: timestamp
  • target: target value, related to mining, adjusted according to the computing power of the entire network
  • nonce: an integer, will be said later

0x06 How to put candidate blocks into the blockchain?

Remember our first picture? The nonce in that picture is also a metadata, and the miner's goal is to change the value of this nonce to ensure that the first m bits of the hash value of the entire block are 0. This m will have different selection methods at different times. With the development of time, the computing power will continue to increase, the miners will continue to enter the market, and m will definitely become larger and larger. The purpose of adjusting m is to make the average duration of block generation about 10min. The reason will be given in the "Longest Chain Principle" below.

The hash function is a highly discrete function, so discrete that we can think that every binary bit of its result obeys a 0-1 distribution, that is, it can be regarded as generating random output from random input. Therefore, the probability that each of the first m bits is 0 can be considered to be approximately independent.

Once a nonce that satisfies the conditions is found, it can be approximately considered that the block is packaged and added to the blockchain.

0x07 Nodes

A node is a computer running a Bitcoin program that connects to other computers running the same program to form a network. So when running the program, it will connect to other nodes and start downloading a full copy of the block (a file containing all verified transactions). After that, the program will start receiving transactions from other nodes and relaying to other nodes on the network.

The node here is not talking about users, but miners, because Bitcoin users seem to be only responsible for writing transaction records, while miners collect and broadcast transactions...

Nodes have three roles:

  • Follow the rules
    A transaction will only be stored and relayed to other blocks if all the information about the transaction is correct. (For example, the balance of the account of the person who transfers the money must be greater than the amount spent to be authenticated)
  • There are two types of transmission information
    : First, the unconfirmed transaction that has just entered. 2. Confirmed transactions, information can be checked publicly
  • Confirm and save a copy of the transaction
    Each node has a copy of the transaction information that is stored, and if it is up-to-date, it is shared. Each node will share information, so that each node has the latest data.

Therefore, the Bitcoin network is called a P2P network.

0x08 Mining

Mining: The process of adding transactions to the blockchain

Each node will share new transaction information, which will be temporarily stored in the transaction pool.

The process of mining is the process of confirming the completion of the transaction. See 0x06 for details.

By the way, that trial and error process is called Proof of Work.

So why is the mining process necessary?

Because it's a means of dusting off previous transactions, and it's how digital currency is used to prevent fraud.

insert image description here
Example: Zhang San took a chance and bought a beer with 10 yuan in area A, and then immediately used the 10 yuan to buy a pizza in area B (double-spending). Since the recording and broadcasting have not been completed, the money seems to be reused (x

In order to prevent such fraud, Zhang San must buy beer first, and then buy pizza after confirmation . Otherwise, both will be rejected due to the rules of node confirmation.

That is, the blockchain does not allow a user to have two records that are not recorded in the block at the same time.

0x09 Transactions

Transactions are one of the core concepts of blockchain. A transaction records the amount of money, the sender's public key, and the recipient's public key.

The public key here can be understood as the address address, which is globally unique.

insert image description hereThe information of the transaction becomes a string like the picture above, which is then broadcast to the miners' P2P network.

The process of 0x0a transaction

We usually spend money differently than this. After each transfer, the balance will decrease immediately.

Unlike Bitcoin, each transaction is stored separately and then integrated when needed.

insert image description here
But what if you can't come up with such a fortunate combination?

insert image description here
Bitcoin's solution is to flip the rest of the left hand to the right .

In addition, in order to truly ensure that this transaction is initiated by address1 and received by address2, and the amount of money is still that much, address1 needs to do the following processing on the information in total:

  • Generate a transaction record
  • Hash the transaction record, and then sign the hash value with your own private key
  • Then use the public key of address2 to sign the hash value

The principle is as follows:

  • address1 encrypts the transaction signature with its own private key to obtain i, to ensure that the information is written by address1
  • Use the public key of address2 to encrypt the transaction signature to get j, to ensure that the money is for address2

This way, anyone in the whole network can decrypt i to determine its authenticity, and only address2 can spend the money by decrypting j.

The overall flow chart is roughly as follows:
insert image description here

0x0b Solving difficulty

The average block time of the network is evaluated after n number of blocks, if it is greater than the expected block time, the difficulty of the proof-of-work algorithm decreases, and if it is less than the expected block time, the difficulty increases. Bitcoin has a 10min standard and re-evaluates the difficulty level every 2016 blocks (i.e. roughly every 2 weeks).

We record the difficulty of the starting block as 1. In order to maintain relative stability, the range of each adjustment is 1/4 to 4 times of the original.

(By July 20 the difficulty was about 1734.5 billion…

0x0c longest chain principle

In order for the blockchain to expand (mining), it is necessary to broadcast the newly generated block to other nodes in the network, so as to avoid waste of resources and confusion of standards caused by repeated useless mining.

Therefore, if the difficulty of generating a block is too small, and the speed of block appearance is too high, two people may mine at the same time, that is, a competing chain will appear. At this point, we need chain reorganization. Only one chain can remain, and the rest are all void. After the reorganization, miners can concentrate their computing power to do useful work.

The criterion for the longest chain is the amount of work. Generally speaking, the chain with more blocks has more work, but the work of each chain is the sum of the computational difficulty of each block of the chain.

Since the blockchain will eventually keep the data consistent, a transaction may be packaged into a block that is subsequently orphaned. So, to confirm that a transaction is permanently recorded in the blockchain, the transaction needs to be confirmed. If subsequent blocks are appended to the blockchain, the original transaction is actually confirmed, because the longer the chain, the more difficult it is to modify. Generally speaking, a transaction confirmed after 6 blocks is almost impossible to modify.

How to keep the longest chain and cut the short ones?

Assuming that O is followed by two blocks A and B, the next generated block C on the timeline will inevitably be connected to A or B (the result may be that there are two groups of miners fighting for computing power). Suppose a group of miners calculate that C is connected to A, then AC will automatically become the longest chain

Whichever block the formed block is built on first, it will be confirmed as part of the longest chain. For example, the green block in the image below. The blue block in the figure below will be removed, and the transaction record will be placed in the transaction pool again to be re-recorded on the chain.

insert image description here

Since the blockchain will eventually keep the data consistent, a transaction may be packaged into a block that is subsequently orphaned. So, to confirm that a transaction is permanently recorded in the blockchain, the transaction needs to be confirmed. If subsequent blocks are appended to the blockchain, the original transaction is actually confirmed, because the longer the chain, the more difficult it is to modify. Generally speaking, a transaction confirmed after 6 blocks is almost impossible to modify.

block confirmation
There is one and only one possibility to modify a block that has been confirmed: create a new block from the genesis node. Such an approach requires the modifier to have more than half of the computing power of the entire network in order to create blocks from scratch, catch up with the previous blockchain and replace it.

Using the longest chain will make each block irreplaceable, thereby protecting a consistent and true distributed ledger. If someone wants to cover a certain transaction, then he needs to re-establish the longest chain, but not only the height of the block is already very large, but also there are many miners extending the longest chain, it is almost impossible for a single computing power to complete of. Even an attack with huge computing power requires a high price.

0x0d reward mechanism

In the actual transaction, each transaction will be deducted some fees as part of the miner's reward.

0x0e Reference Portal

https://www.liaoxuefeng.com/wiki/1207298049439968

https://learnblockchain.cn/

(I am a novice, the content of the arrangement is inevitably wrong, please also criticize and correct the dalao who caught the bug in the comment area

Guess you like

Origin blog.csdn.net/weixin_43466027/article/details/116052759