Introduction to basic knowledge of blockchain

learnerL 2021-04-17 15:13:39 阅读数:695

本文一共[544]字,预计阅读时长:1分钟~
introduction basic knowledge blockchain

Introduction to blockchain

Blockchain 1.0( A description based on bitcoin )—— Basic cognition

It refers to an unchangeable , Distributed , Decentralized ledger , No one is a central register , Because everyone is a central register , Everyone's account books as like as two peas , Keep track of everyone's assets .(** This is probably our basic understanding of it )**

Blockchain accounting **, I'll take the last deal ** Account information **( Transaction number , Bookkeeping ( transaction ) Time , Transaction records ) As raw information ,hash After that ** The value is combined with the current account page information ** together , It becomes a ** block **;( This data structure is similar to ** Linked list **—— In short , It can store data from the previous node , Can also connect to the next node )**

Continue derivation , The next block will also contain the account page information of this transaction hash value , And the account page information of the next transaction , thus , Each block contains the last account page information , according to hash The avalanche benefit and irreversible recursive property of the algorithm , So as to achieve what the blockchain itself was designed to achieve at the beginning :**** Non tamperability ****

When a block is updated , This block will put all the information of this transaction , Synchronize to every block in the blockchain through the Internet , Each block synchronizes the ledger in a short time , Make the account books of all blocks in the blockchain consistent , It has reached the beginning of blockchain design :**** Non repudiation ****

Let's take a look at the complete information example of a block below :

img

  • In this block hash value ;(** The first one to circle )**
  • Billing time stamp
  • Block height ( The first 640298 Block )
  • random number

Bitcoin awarded this time ( This is the third 210000 Block , So the reward is 50/2/2/2=6.25bitcoin)

Overview of bitcoin operation

Bitcoin is 2009 E-payment system founded in . You can remit money to anyone in the world through it , Without the need for a central agency to issue accounts or process payments .( For example, traditional financial remittance requires both parties to use the same platform account , All transactions are controlled by the system of the platform .** Think about Alipay and WeChat pay )**

All of a sudden , Some people don't want all financial services to be controlled by a few big banks , Because it's not fair , Banks can use other people's money to make bubbles , I didn't have a lot of principal , But they use other people's hundreds of billions to make profits for themselves , And once the crisis comes , Rapid depreciation , It can't even be exchanged .

This creates absolute control over money , And forcing users to trust banks .image-20201205134047432

And in 2007 year , This kind of abuse 、 Forced trust caused the financial crisis , Stimulated the emergence of bitcoin without centralized management . The following is the first post that Nakamoto put forward bitcoin in the community :

1

2

It's like he built a point-to-point e-finance system , Here's a link to the client , Download it and have a look .

(**** Transaction costs **) And then we talk about the drawbacks of the current financial system , Because it's managed by people , Users have to believe that their money won't be stolen , The center will protect our privacy . and , Traditional payment and PayPal When dealing with small payments, the cost is high ( Think about how many employees are in the banking system ..PayPal Based on the traditional banking system , It needs to get information from the bank , Then return . But Alipay and WeChat (Alipay,WeChat Adopt your own system , Make use of trade platform advantage and user group advantage respectively , Make the cost of micropayment very low ).**

(**** Absolute protection of personal assets **) And then go further to the situation at that time , Personal information ( Including assets, etc ) It's all done by setting a password for the account . however , Out of the concerns of managers or the demands of higher authorities , Personal data is not protected . Now cryptography is developed enough , We need to change that , Make it impossible for other people to have access to our account , Whatever the reason 、 What's a better reason 、 What else . He proposed a low-cost transaction method based on password proof without trusting middleman , To solve this problem .**

(**** Principle analysis **) Next, the process of identity authentication ( Let me explain in a little detail )..................... But it raises a question : How to prevent spending again . Finally, I came up with a solution , Using information is easy to spread but hard to kill (stifle) Characteristics of .**

Finally, point to point (P2P) Direct trading .

Here is the paper he published that year ( I packed them together, too ):https://bitcoin.org/bitcoin.pdf

P2P

It's generally called **** Point to point network ** perhaps ** Peer-to-peer networks **. Because in P2P In the network environment , Multiple computers connected to each other are in a peer-to-peer position , Each computer has the same function , There is no subject or subordinate , A computer can serve as a server , It can also be used as a workstation . It's like traditional Client/Server The pattern is very different .**

img

characteristic

  • Direct interaction P2P It's about connecting people directly , Let people interact directly through the Internet .P2P Make it easy to communicate on the Internet 、 More direct sharing and interaction , Truly eliminate middlemen .
  • De centralization P2P Another important feature is to change the status of the Internet Centered on the Ethernet website 、 Return “ Decentralization ”, And give the power back to the user .
  • Performance expansion

Peer to peer network is a successful extension of the concept of distribution , It allocates the traditional server burden to every node in the network , Each node will undertake limited storage and computing tasks ,**** The more nodes join the network , The more resources a node contributes , The higher the quality of service .** Especially the operation of personal computers in recent years 、 Storage capacity has been greatly improved , For this “ Human sea tactics ” Create an advantage ): In the traditional way FTP In the file download mode of , When the number of download users increases , The download speed will get slower and slower , However P2P The Internet is the opposite , The more users you join ,P2P The more resources available on the Internet , The faster you download .**

  • Robustness **(Robust)**

Robustness ** It's also called ** Robustness, **, It refers to the ability to keep the performance unchanged when the parameters change .P2P Architecture is inherently attack resistant 、 Advantages of high fault tolerance . Because services are distributed among nodes , The destruction of some nodes or networks has little impact on other parts .P2P Generally, the network can automatically adjust the overall topology when some nodes fail ( Arrangement of nodes ), Keep other nodes connected .P2P Networks are usually self-organized , And allow nodes to join and leave freely . **

  • Privacy protection

stay P2P In the network , Because the transmission of information is distributed among the nodes without going through a centralized link , The possibility of users' privacy information being eavesdropped and leaked is greatly reduced . stay P2P in , All participants can provide relay forwarding function , Therefore, the flexibility and reliability of anonymous communication are greatly improved , Can provide users with better privacy protection .

Many properties of blockchain and P2P Very similar **, Because blockchain is used when it is proposed P2P The computer network .**

Digital Signatures

The number generated by the private key that proves that it has its public key , Just prove that you know the private key corresponding to the public key , You don't have to show your private key .

image-20201205162947332

Because the correct digital signature can only be generated by the correct private key , So you can prove your identity by using a digital signature .

Why a digital signature is needed ?

When making a deal , Need to unlock address (address, It can be regarded as an account ) Corresponding Outputs.

image-20201205165800299

If you use the private key directly, everyone will know , Others can unlock others Outputs Come and spend our money .

image-20201205165525751

That's why digital signatures are used .

The digital signature of each transaction is unique , Only once .

Think about it , Now that other people know my digital signature , They are used directly , Can you ?

Can not be . In fact, it's not just the private key that generates the digital signature , It's the private key and the transaction data (transaction data) Generating digital signature together . The digital signature and the transaction must match .image-20201205181955284

If it doesn't match, it will be rejected by other nodes .

How signature works ?

image-20201205190505451

Hash

  • Hash is a function or algorithm , It has a fixed length , Its function is encryption .
  • Hash value ( Hash hash ) It's almost irreversible 、 Indecipherable , Even the length of the input value cannot be determined at first sight .
  • The hash value is identified by the block (the block header) Decisive .

Hash algorithm is an irreversible encryption algorithm , Full name ****“ Hash hash function ”**. Its biggest characteristic is that the input of any length is transformed into the output of fixed length by hash algorithm ( This simplifies and identifies information ), The output value is called **“ Hash value ”** ; This transformation is a kind of information compression mapping , The space for hash values is much smaller than the space for input values , From the perspective of information theory, it will consume the amount of information ; And small changes in the input value will cause huge fluctuations in the final hash value ** Avalanche benefits **;img This is the original expression of the hash algorithm . among a[n] Is an array of strings ,s Is a random number , But usually the strings we want to encrypt are ASCII Encoded data , Its maximum number is 127, Because the final result of hash algorithm is a remainder calculation , So we usually encrypt with s Take greater than 127 The smallest prime number , namely 131**

Block

  • image-20201130132515382
  • And then the miners package these deals , Put in candidate block (candidate block) in , Then add the preselected block to the blockchain .
  • Give the candidate a metadata (** It can be understood as the identification of data , The data after that is all about it ), It's called size (block header), Including the version 、 Information about the previous block 、 Number of transactions 、 Time stamp 、 The target .**image-20201130133033439

Version:** The data structure of this block , Describe its protocol .**

Last Block:** Identification of the previous block **

Merkle Root:** All transactions are hashed together to generate encrypted values , Very important .**

Time:** Time stamp **

Target:** The target , It's about mining .( I'll explain later )**

Like this value

010000009500c43a25c624520b5100adf82cb9f9da72fd2447a496bc600b0000000000006cd862370395dedf1da2841ccda0fc489e3039de5f1ccddef0e834991a65600ea6c8cb4db3936a1ae3143991

The whole string is block header, Every paragraph has a unique meaning :

01000000——****version****

9500c43a25c624520b5100adf82cb9f9da72fd2447a496bc600b000000000000——****previous block****

6cd862370395dedf1da2841ccda0fc489e3039de5f1ccddef0e834991a65600e——****Merkle root****

a6c8cb4d——****time****

b3936a1a——****bits****

e3143991——****nonce****

Here is a simulation process diagram :

How to add blocks to the blockchain ?

The miners create a random number (nonce)( The number of encrypted transmissions used only once ), Then use random numbers (nonce) Hash the block data . This random number starts from 0 Start , If it doesn't work, increase it .

image-20201130141608412

Until a satisfactory value appears .

image-20201130135810551

congratulations!!** succeed , We are going to compete in the next new district .**

Nodes

  • Nodes are computers running bitcoin programs , It connects to other programs that run the same program ( client )(a client) The computer , Form a network (network) So when you run the client , It will connect to other nodes and start downloading the full copy of the blockchain ( File containing all verified transactions ). after , The client will start to receive transactions from other nodes and relay them to other nodes on the network .**** however , This doesn't mean that you can only trade after you become a node , It's enough to be able to send messages to the network **image-20201130142132285 effect :**

    1. Follow the rules .
    2. Transmit information .
    3. Confirm and keep a copy of the transaction .

      Follow rules

      image-20201130142823489

      Only when all information of the transaction is correct , Transactions are stored and relayed to other blocks .(** For example, the balance of the account of the person who transfers money must be greater than the amount spent in order to be authenticated )**

      Share information

      There are two kinds of : One , Just entered the unconfirmed transaction . Two 、 Confirmed transactions , Information disclosure course inquiry .

      image-20201130143520539

      Keep a copy of confirmed transactions

      Each node has a copy for storing transaction information , If it's not up to date , Then it will be shared . Any change in the distribution is obvious compared with the replica .(** Design deals here , Later on ) Each node will share information , Let each node have the latest data .**

      image-20201130182627345

      therefore , Bitcoin networks are called “ Point to point (Peer-to-peer, P2P in short) The Internet ”, because :

      1. Everyone is connected , So it's a network .
      2. Everyone on the Internet is equal , So we are a little bit (Peer).

Mining

The process of adding transactions to the blockchain is called mining .

Every bitcoin node will share new transaction information , They are temporarily stored in the memory pool (memory pool, It's also called a trading pool )

image-20201201132553617

Then each node can turn the transaction into a file through mining (file), This file is called the ledger (****ledger**).**

This process requires a lot of computing power , First, turn the transaction into a string (string).

image-20201201145237396

Then hash the hexadecimal number (** Like an introduction block That's the same )**

image-20201201145352990

then , If you're lucky , You're the first to win in this super random blindfold , You will be paid (rewards). This process of trial and error is called **** Proof of workload **(proof of word,PoW in short)**

Why is the process of mining necessary ?

Because it's a way to confirm that a transaction has been completed , It's the way digital money is used to prevent fraud .** At this time , Zhang San's opportunism , stay A Regional use 10 I bought a beer for three yuan , And right away B The district uses this ten yuan to buy pizza (double-spending), Because we haven't finished recording and broadcasting yet ......)**image-20201201151123824

** To prevent such deception (fraud), Zhang San must buy beer first , Confirm before buying pizza . Otherwise, due to the rules of node confirmation , Both will be rejected .)( If the memory pool including Zhang San beer is turned into a block by mining , And add into the blockchain , After being authenticated by other blocks , The sale of his waiting pizza will be automatically cancelled .)————** This is the contradiction bitcoin network faces (conflicting) To reach a consensus (consensus) Methods ****

therefore , Bitcoin network is mining to confirm which transaction needs to be carried out .

How do I start mining?—— Do you want to mine ?

Now mining is basically some mining machines ( Fully customized highly integrated circuits for infinite hash processing ):

Don't worry about starting , Because the effect is very obvious , because CPU It's confusing 、 The fan seems to be spinning away ..

Transactions

  • Transaction is one of the core concepts of blockchain ,**** It's a bunch of data **.**
  • address It can be understood as an account with bitcoin , It's unique .

image-20201130184803694

In other words, the transaction information becomes a string of characters , And send it to the bitcoin network (** Blockchain 1.0 Take bitcoin as an example ), After mining, it was added to the confirmed transaction file , Block chain .**

The process of trading ?

It's not what we think ,image-20201130185904198 It's like a can full of silver , Pour out as much as you want . It is , Each transaction is stored separately , Integrate them when you need to (Outputs are in batches),image-20201130190112817 such as 5 By 2 and 3 Integrated .

This is how every account trades :

image-20201130190330046

But the problem is , If I can't just come up with the amount I need ?

Then it will return a difference value .

** Although it looks a little awkward , But you need to do that when you program )**

How to keep people from spending my money ?

Since the transaction is a string of data input into the network , So why can't others build exactly the same data including the address to pay ?

Because every one of them output They all have a lock , Only when all of them are opened can they be recognized by nodes , If you don't follow the rules, you will be rejected .

image-20201130232104153

You need a private key (a private key) To unlock , Until you need it outputs It's all unlocked , To continue trading .

image-20201130232603486

Here's a little bit of detail :

If you have ten bitcoins , But it needs flowers 8 individual , Change for two . that , Will be in 8 The one who's the one who's the one who's the one who's the one who's the one Output Add the receiver's public key to the , The receiver can unlock it with its private key , The next two are the same Outputs Only you can unlock it .

image-20201205201417011

** Explain the diagram below )**

image-20201205202116664

The public key is based on the private key , But the private key cannot be inferred from the public key .

img

General process chart :

img

Difficulty

The average block time of the network is n Evaluation after the number of blocks , If it is greater than the expected block time , Then the difficulty of the algorithm will be reduced , If it's less than the expected block time , The difficulty will increase . Bitcoin uses 10min As the standard , And every 2016 Block ( That is, about every 2 Zhou ) Reevaluate the difficulty level .

The calculation formula is as follows :****new_difficulty = old_difficulty X(2016 block X 10 minute )/( Dig the last 2016 Block time ( minute ))****

Generally, the difficulty of setting Chuangshi block is 1. In order to maintain relative stability , The range of each adjustment is in the original 1/4 To 4 times , Here's the curve :

image-20201204102803083

17 year 1 month 12 What's the difficulty 678 Billion ,2018 year 1 month 24 Number is 2208 Billion , here we are 20 year 7 The difficulty of the month is about 17345 Billion , The development of computer is very important .(** Don't underestimate the index , They're not even here yet 4^24**^

img

Target

Just mentioned the process of mining , It's not easy , The value of the block header encrypted by hash must be less than or equal to a certain value , It's a success . That is, the higher the target value, the easier it is (** Think of it as a hole , The smaller the hole, the harder it is to pass through )**

image-20201203112053670

target It is used to confirm whether the block meets the requirements , And adjust the difficulty by a value .

image-20201129230509405

This is a 16 Binary number ,(** As a contrast , Let's look at the target value of the genesis block , Obviously much bigger .)**

image-20201129230819030

For example, discovery 0-2016 The speed of building blocks is very fast , Then we'll increase the target value , Enlarge the back 2017-4032 difficulty :

image-20201203112748230

contrary , If you find out later 2017-4032 It takes a long time to build a block , Then increase the target value , The first 4033-6048 A block can be generated faster .

image-20201203113243891

Here is a simulation of the rules of computation :(** Of course, it can't be on the Internet PDF It's entered in )**

image-20201203113801625

Its corresponding code is as follows :

# 403,200 - NEW TARGET# 403,199              | last block#                      |#                      |#                      |# 401,184 - NEW TARGET | first block (target = 0x000000000000000006f0a8000000000000000000000000000000000000000000)# 1. Get the timestamps for the first and last block in the target adjustment periodfirst = 1457133956 # block 401,184last  = 1458291885 # block 403,199# 2. Work out the ratio of the actual time against the expected timeactual = last - first     # 1157929 (number of seconds between first and last block)expected = 2016 * 10 * 60 # 1209600 (number of seconds expected between 2016 blocks)ratio = actual.to_f / expected.to_f# 3. Limit the adjustment by a factor of 4 (to prevent massive changes from one target to the next)ratio = 0.25 if ratio < 0.25ratio = 4 if ratio > 4# 4. Multiply the current target by this ratio to get the new targetcurrent_target = 0x000000000000000006f0a8000000000000000000000000000000000000000000new_target = (current_target * ratio)# 5. Don't let the target go above the maximum targetmax_target = 0x00000000ffff0000000000000000000000000000000000000000000000000000new_target = max_target if new_target > max_target# 5. Truncate the target, because the official target is the truncated "bits" format stored in the block header# This code is a bit rough, because it's working with strings when I should really be working with actual bytes.new_target = new_target.to_i.to_s(16) # convert from decimal to hexadecimalnew_target = new_target.size % 2 != 0 ? '0' + new_target : new_target # make sure it's an even number of characters (i.e. bytes)truncated = new_target.scan(/../).each_with_index.map { |byte, i| byte = i >= 3 ? "00" : byte }.join # set all bytes apart from first 3 to zeros# e.g. 6a4c316c01f354000000000000000000000000000000000 <- full precision# e.g. 6a4c3000000000000000000000000000000000000000000 <- official target# 6. Display the full target (with leading zeros)target = truncated.rjust(64, '0')puts target# 000000000000000006a4c3000000000000000000000000000000000000000000

All nodes share the same target

Because all nodes adopt the block composed of the longest chain , When you first run the client , The computer will automatically accept the data of the initial block , And be consistent with other nodes , So eventually they will share the same target.

image-20201203130009990

image-20201203130602010 So says the founder, Tsung Nakamoto .

Why set up target To adjust the time interval ?

In order to expand the blockchain ( dig ), We need to make the newly generated block broadcast (propagate) To other nodes in the network , Avoid waste of resources and confusion of standards caused by repeated and useless mining **.**

image-20201203131317172

Because blockchain has to follow **** The principle of the longest chain **(longest chain) If there are no blocks, there are no gaps between them , So when a miner completes a block , Other miners who didn't get the message are still digging the original layer , When he's done, there's a chain of competition (competing chains), happen ** Chain recombination **(chain reorganization, Also called reorg), Only one can be kept , All the rest To void .**

image-20201203132844628

After restructuring , The miner can concentrate on his work .

image-20201203133949300

Nakamoto has an elaboration on this setting :

image-20201203134102344

Issue money at a constant rate

Bitcoin is a currency , He has to stay scarce and need to issue , Setting a target value will allow bitcoin to be issued in a predictable way .

image-20201203231006429

image-20201203231051959

Longest Chain

The longest chain is the chain adopted by the node as the blockchain

image-20201204101207288

The remaining branches or bifurcations are considered invalid .

Judging standard :

We know , Different difficulties require different computing power , The smaller the target value is , The more difficult it is , More work . therefore , Its criterion is not the number of blocks , And the workload (work). But generally speaking, chains with more blocks consume more work **.**

image-20201204133537177

The chain on the left is shorter than that on the right , But its workload is greater than the one on the right , So the chain on the left is the longest chain .

image-20201204110843502

image-20201204110011796

That short chain is usually abandoned , actually , There's never been a single block with three consecutive ones (****orphan blocks**).**

image-20201204205859306

computing method :

Add up the coefficients of each layer .

image-20201204133407311

It can be seen from the picture that , The number of hashes depends on the size of the target value , It is positively related to difficulty , It is positively related to workload .

Why do nodes use the longest chain

1. To resolve the differences caused by two blocks at the same time

image-20201204134345199

Although the starting data of the two blocks is the same , But there has to be a credible and effective longest chain , Ensure consistency (consensus).

image-20201204194703261

that , Which block is the first to build the next block , It will be identified as part of the longest chain .

2. Protect blocks that have been added to the blockchain

Using the longest chain , It makes every block irreplaceable , And then protect the consistent 、 What a distributed ledger . If someone wants to cover a transaction , Then he needs to rebuild the longest chain , But not only is the height of the block very large , And there are a lot of miners extending the longest chain , It's almost impossible to do a single calculation . Even if it's a massive attack , And there's a high price to pay .

image-20201204201616650

It's going to take more than the sum of all the other miners (****51% attack **).**

Why do miners work on the longest chain

Bitcoin blockchain has a perverse setting , Every miner has to dig in the longest chain to get a profit (block reward), If the chain reorganization is abandoned , It's a big loss . That is to say, to build a new longest chain is the act of benevolence if you fail .

image-20201204202346890

Besides , The profits from mining also have to wait until it comes back 100 It can only be used after a block is formed .... If the longest chain is tried to tamper with , A lot of mining profits can't be used , According to the market value , Tens of millions of dollars are missing from the wallet .. We have to work hard .

What happens when you're not in the longest chain ?

The trade in the competition block will be invalid , Rejected by all nodes , Unable to archive . Transactions that are not in the competitive chain are sent back to the memory pool , Waiting for an opportunity to re-enter a new block by mining .

image-20201204204423016

Confirmations

  • In the figure block confirmation It can be simply understood as the number of blocks behind .
  • Generally refers to the number of transactions confirmed , Every time a block is completed, add one , And it needs to reach a certain amount to be considered credible , exceed 6 One can prevent one percent 40 The attack of computing power ( The attack success rate is less than 1%). It's in the trade , Bitcoin is 6, Ethereum is 12. But sometimes it's too cheap to care or irreplaceable , You can skip this review .
  • This is the time interval for each block confirmation
  •  Substitute words
  • bitcoin confirmations
  • This is when the deal is confirmed , In about 10min There are 66.3% The probability of finding a block , Yes 99.7% Within an hour .
  • image-20201129102724232

The above is based on Bitcoin network Explanation , Different technologies will be different , But the basic concepts are common

Korda (R3-Corda)

R3 Founded in 2014 year , Officially established in 2015 year 9 month , It's a blockchain startup , It is mainly committed to providing banks with the exploration of blockchain technology and the establishment of blockchain conceptual products . So far, R3 Alliance already has 70 Multiple financial institutions ,100 Multi member , Its members are almost all over the world , Through this alliance, we will study the application of blockchain in the financial industry , Use its distributed ledger technology to help financial institutions transform their infrastructure , Cost savings , Increase of efficiency .Corda By R3 Launched a distributed ledger platform , It draws on some of the characteristics of blockchain , for example UTXO Model and smart contract , But it is different from blockchain in essence , Not everyone can use this platform , It is oriented to the interoperability scenarios between banks or between banks and their commercial users .Corda It's publicly available... Operated by network participants Corda Node Internet . Each node is identified by a certificate issued by the network identity service , And it can also be identified on the network diagram .

The etheric fang (Ethereum)

2014 It's the year of the Russians Vitalik Buterin Initiated blockchain projects , Blockchain 2.0 The representative of the , It's the most popular public chain right now , It's a platform for blockchain , You can develop all kinds of smart contracts on it , Once deployed, the contract will always be in force , You need to pay some tokens (ETH). Ethereum's vision is to create an unstoppable , Anti shielding ( review ) And self-sustaining decentralized world computers , At present, the mechanism is consensus : Proof of workload (POW)+ Proof of interest (POS) Mixed mode .

Know Ethereum

Ethereum basic interaction and development tools

be based on Solidity Smart contract programming based on XML

版权声明:本文为[learnerL]所创,转载请带上原文链接,感谢。 https://netfreeman.com/2021/04/20210417150446144M.html