Blockchain in the eyes of programmers

Jiedao jdon 2021-04-13 11:47:31 阅读数:44

本文一共[544]字,预计阅读时长:1分钟~
blockchain eyes programmers
This is an article on blockchain from the perspective of programmers , Make blockchain a concept that programmers can understand , Like Turing state machine , And a state machine is a state mode , It's the core of event driven modeling . This article is suitable for beginners of blockchain :

Asymmetric cryptography has been around for a long time , A key can be divided into a public key and a private key . We use the public key to encrypt and the private key to decrypt . Or vice versa . You can't get another key from one key . therefore , If you use asymmetric encryption , I can send a message to someone , such as “ I transfer money 100 dollar ”, And everyone will know that it was sent by me , And there's no change .

Suppose there are ten people who agree that each has a million units of new currency . Each of them can make sure that ( And everyone can be sure ) Someone sent 300000 units . however X It's not known if they really own these 300000 units of currency . To find out , We can know everyone's balance . So , We just add up all the revenue transactions and deduct all the expenses to calculate . thus , We need information about revenues and expenditures , This is the public ledger . It allows us to identify whether someone can really send out 30 Ten thousand units of currency .

The currency : Start

There's another problem - What if I send all my money to two people at the same time ? This is called double spending . It would be inconvenient for these two people to communicate with each other to find out who will eventually get paid ; Look at the timestamp to see who I sent the money to first ? This method is not fail-safe Of . Here's a solution - We need to streamline transactions , In this streamlined trading model , It's obvious that the first payment has been sent to John , There is no way to send this payment to a second person , Because I don't have any more money .

Streamline trading is mainly to sort the transactions , But it's not clear how to determine the order of transactions . In banks and other centralized systems , The bank itself represents the trustee , It's a single point of risk , It's also a single point of trust . The bank can come forward and clarify that , Who did the money of so and so go to first . But in the blockchain , There is no single trustee . It's probably most of the participants in the network ( Of course , It means the whole network , Because you can't trust every participant , Because there must be some cheaters ). The assumption here is that the network is not controlled by anyone , And most of the participants act in strict accordance with their own economic interests ( be beneficial to oneself ).

But the problem of consistency in streamlined trading has not been completely solved - This requires us to implement a network of participants called “ Consensus ” The mechanism of . This problem is handled by a consensus algorithm , This is a key algorithm in every blockchain .

Before we discuss a specific consensus algorithm , Let's talk about the blocks of blockchain first . Block is a technical measure , It's batch processing ( It's called batch processing ) An example of , So many deals can be done in one place “ Batch ” In order to reach a consensus , And the overhead of the network can span multiple transactions “ Share the ”. The order of intra block transactions is determined by the network participants ( The person who generated the block ( Also known as “ The miners ”)) Defined . But the order between blocks is determined by the consensus algorithm . Consensus algorithms have existed for quite a long time . For example, it includes multiple hosts NoSQL In the system Paxos and Raft And so on . But these algorithms are not suitable for networks with a million pairs of participants .

Bitcoin represents something quite revolutionary when it came into being - A proof of work (PoW) Consensus algorithm . Many network participants ( The miners ) Many new blocks will be generated at the same time . So it's necessary to decide which of these blocks can really be used as the next block in the blockchain .

In order to make the block generated by itself the real next block in the blockchain , Miners have to perform specially selected and time-consuming hash calculations . When a hash value is calculated to a large number , And must meet certain conditions , For example, it must be preceded by 10 A zero . That would take billions of hashes , This amount of calculation means that miners can “ prove ” Evidence of the existence of the block . meanwhile , It's very easy to check that the work is done correctly - You only need one hash operation . It has to be understood that : On average, , The whole network ( Including thousands of miners !) stay 10 A block can be generated in minutes ( The average interval between bitcoin blocks )! . This greatly reduces the possibility of collisions between absentees , That is, it is impossible for two miners to provide two or more blocks as the next block at the same time .

however , The possibility of a collision is still possible . therefore , There are rules - Network participants accept the blocks that form the longest chain ( When two blocks are generated at the same time , Select one of the longer chain blocks ).

in fact , It's not a chain , It's a directed acyclic graph with many branches (DAG) Chain , And chains are often called “ Common history ”( It's the longest chain ). Since consensus cannot be reached immediately , You have to wait until a few blocks appear . To prevent bitcoin from double spending , People usually wait for six blocks ( It takes an hour in bitcoin ).

Besides business , The block also includes the hash of the previous block . therefore , History can't be changed , Because this will invalidate all subsequent hashes . meanwhile , Connect to the same block , You'll get a mining reward , This will bring you new benefits ! otherwise , You can't bring new money into the system - The money will have to go out to someone , It makes this person special , This is in conflict with the idea of decentralization . Paying for mining seems to be a logical and acceptable solution .

By the way , About mining . You need to understand , The miner's main job is to hash out new blocks violently ; There's no asymmetric encryption , Because you just need to check the transaction before it's added to the block . Bitcoin uses SHA-256 Hash ; In Ethereum , It's taking Keccak-256. If you're mining alone , So before you generate a block , It takes a long time to prepare ( One block is generated every ten minutes , You have hundreds of thousands of competitors !). If the miners get together . In a pool , Rewards between participants are distributed in various ways ( There are different ways ), That means you can get a bonus more often ( Although the number is relatively small ). under these circumstances , The risk is proportional to the return .

up to now , We mainly deal with data exchange between network participants , Without going into the details . It's important to remember that data is exchanged peer to peer over the network .

This is bitcoin blockchain Technology .

Smart contracts and ICO

Perform two operations on each bitcoin account ( Add and subtract ), And it can be checked by anyone , Of course, mainly by the miners . But if we add an internal variable to the account ( Storage ), It's for operational comparison , Suppose I accumulate 1000 Ten thousand dollars per hour , I want my account to stop receiving funds - This means that the implementation of the code is not only to increase the funds to my account , We should also add certain conditions for this increase . Looking at the past from the perspective of development , What we see is a Turing machine , A type of certainty ( Everyone can still check my account ). therefore , We have a smart contract . In the following , Smart contract refers to the smart contract in Ethereum network , Because it's the most popular contract right now .

Certainty means , In the case of the same transaction parameters and the same initial state stored , We'll end up with the same result , And the same changes in storage (banq notes : Operational events ). Only under these conditions , Only the Internet can agree on every deal .

It has to be understood that , Because of the need for certainty , Verifiability and dispersion , Smart contracts are quite isolated from the outside world . for example , It doesn't trigger decentralized Web API( There is a solution , That is to say “oracles”, But it's all complicated , It needs to be dealt with separately ).

in addition , The contract also has very limited control over the outside world . for example , To understand the results of smart contract execution ( The change action of a new balance or storage ), The network node must perform the operation specified in the contract . Computing on multiple nodes is not without cost . therefore , Ethereum introduced gas The concept of , In order to avoid the cost of cryptocurrency hard coding , And allows users to calculate the number of calculations and set their prices . This structure provides an additional layer of indirection , It allows users to choose between low-cost transactions and fast transactions .

When funds are sent to the contract account ( By simply using the address ) when , It triggers the callback function , This is the default setting . Different from the previous account type of bitcoin , There are multiple input points in the smart contract - These are ways to accept parameters . To amend the contract ( Whatever the input point is ), You have to send the transaction to the network to make changes . however , If you just want to see something in read-only mode , You can perform this locally by using a real-time updated blockchain . Another difference from an account is , Smart contracts don't have private keys . You cannot withdraw funds directly from the balance - This can be done by using well-defined methods , Of course, these methods should have security restrictions , For example, check the address of the user trying to access the funds .

Last , We got smart contract technology on bitcoin style blockchain .

Many cryptocurrencies or so-called “ Tokens, tokens” All based on Ethereum smart contract . Yes , Usually this cryptocurrency is just a contract in Ethereum ( One !). The balance is usually in the smart contract store map A data entry of a data type , And transfer money from one participant to another as follows :

map[Alice] –= payment;
map[Bob] += payment;
<p>

They don't need their own absenteeism and networks - They can use Ethereum's infrastructure . In Ethereum ,ICO / ITO It's actually cryptocurrency (tokens) Ethernet switching for ( Ethereum's cryptocurrency ).

The future development

In addition to smart contracts , There are many other ideas based on the basic concept of blockchain , such as “ Proof of zero knowledge ” and “ Self regulatory network ”.

Zero knowledge proof is a family of the same name based on cryptographic protocols , In essence , It's designed to prove the ownership of the object, but not completely expose it . for example , If I could answer ten questions about bytes in various random positions of a file , I can prove that I have a specific 1 GB file . obviously , under these circumstances , I just need to be public 1 GB Medium 10 Only bytes .

This means that anyone can check that my account balance is correct , And there's no need to disclose how much money I have in my account ! By building this idea , We can create an cryptocurrency with balance and payment privacy information ( actually , It already exists ; See Zcash), And it completely hides the calculation parameters in the smart contract .

Blockchain has network general settings for writing network client program code , For example, the number of transactions contained in the block . Usually , Changing them requires all participants on the network to accept the changes ( In the form of code updates ), It's a threat to network branching ( When some participants decide not to accept the change and continue with “ used ” code ).Self-regulation Trying to solve these problems ( for example , See Tezos or EOS Blockchain ). The essence of this idea is , For every change , You can vote with your own money ( Of course, we won't lose the money ).

Conclusion

All in all , The technical path of blockchain technology is as follows :

Transaction signature ( Asymmetric cryptography )+

The openness of trading +

Linearization of transactions +

The expansion of Turing machine (banq notes : Turing machine means switching between events and states )+

( Proof of zero knowledge | Self regulation | ...)

original text :Blockchain Through the Eyes of a Developer – Alex

Reference resources :http://www.jdon.com/49396

[ The quilt admin On 2018-05-22 22:03 A modified ]

              

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