<> Smart contract

<> definition


Smart contract is a set of agreements defined in digital form , Including the agreement on which the contracting parties can execute these agreements . The basic idea of smart contract is , Various contract terms can be embedded in the hardware and software we use , So the attacker needs a lot of cost to attack .

characteristic :

* The development of computer program if-then sentence
* The treaty will be executed automatically when it is concluded
* Digital contract
* Automatic execution clause of computing system
features :

* Data transparency : The data on the blockchain is open and transparent to the participants , Data processing is also open and transparent .
* No tampering : All data of the blockchain itself cannot be tampered with , The smart contract code on the blockchain and the data output generated by the operation are also tamperable .
* Permanent operation : The nodes supporting the blockchain network often reach hundreds or even thousands , The failure of some nodes will not lead to the termination of the smart contract , Its reliability is close to permanent operation in theory .
<> constitute

* Participants : People or organizations involved in digital asset trading .
* assets : Something that has a certain value and is the subject matter of a transaction , It can be concrete, it can be abstract .
* transaction : The participants query the assets , Transfer and other operations .
* Account book : A database that records the ownership of assets and their transaction facts .
<> Design process

* Multi user participation , Making smart contracts .
* The contract passed P2P Network diffusion and storage in blockchain .
* Automatic execution of smart contract based on blockchain construction .
<> programing language

<> Basic construction model

<> operating mechanism

<> application area

<> Problems and challenges

<> Consensus mechanism

<> brief introduction

<> Byzantine Generals Problem

​ A story about fault tolerance of distributed system .

<> background :


During the war , All generals and adjutants in the Byzantine army must reach a consensus , Decide if you have a chance to win before you attack the enemy camp . however , There may be traitors and enemy spies in the army , The decision of the left and right generals disrupted the order of the whole army . When it comes to consensus , The results do not represent the majority opinion . At this time , In the case of known conspiracy , How can the remaining loyal generals reach an agreement without being influenced by the traitors , The Byzantine problem came into being .

<> Solution :

​ Each node sends messages to all other nodes

​ Each node decides the final policy according to all the received messages

<> shortcoming

​ Each node sends a large number of messages first .

<> concept


Due to the high network delay in point-to-point network , The transaction order observed by each node can not be completely consistent . Therefore, the blockchain system needs to set up a mechanism to agree on the sequence of transactions that occur in the same time . This kind of algorithm is called consensus mechanism .

* Consensus algorithm : Algorithm for nodes to reach consensus according to consensus rules
* Consensus rule : There are well-designed regular protocols in each blockchain , These protocols use consensus algorithm to ensure the correct implementation of consensus rules .
<> effect

​ It's the law of life

<> overview

* proof of work PoW: Bitcoin BTC, Ethereum ETH, Ethereum classic ETC
* Proof of interest PoS:ADA Aida coin ,Peercoin A little bit of money
* Certificate of authorized work DPoS:EOS,Asch,Steem
* Byzantine arithmetic BFT: Practical Byzantine fault tolerance PBFT, derive BFT
* RAFT algorithm :ETCD
<>PoW

<> characteristic :

​ The calculation cost of solving the problem of calculation difficulty is used as the voucher of the new block and the reward benefit .

<> process :

<> defect :

​ because PoW Lack of decentralization and consumption of resources , So there's another consensus mechanism PoS

<>PoS

<> characteristic :

​ Replace proof of workload with proof of equity , The node with the highest equity can join the new block and obtain the incentive income .

<> process :


Before starting to compete for block bookkeeping , The node with rights and interests puts its own rights and interests into the PoS In the mechanism , At the same time, the identity becomes the verifier ,PoS The mechanism depends on how much the verifier bet , A bookkeeper is selected randomly for block bookkeeping .

<> defect :

​ It's not completely random , But the larger the weight is, the more probability it gets , It still hasn't solved the problem of waste of resources and computing power , And the problem of incomplete equivalence .

<>DPoS

<> characteristic :

​ First of all PoS Election of Representatives , Then the block generator is selected from the representatives and the revenue is obtained .

<> process :


DPoS The basic idea of consensus mechanism is similar to “ Board decisions ”, That is to say, each shareholder node in the system can award its share equity to a representative as a vote , The first member who has the largest number of votes and is willing to be a representative 101 Nodes will enter “ Board of directors ”, Take turns to pack and sign the transaction according to the established schedule ( That is production ) A new block .

<> defect :



<>BFT( Byzantine fault tolerant algorithm )

<> characteristic :


DBFT In the algorithm, super nodes participate in accounting , Common nodes can see the consensus process , And synchronize account information , But do not participate in bookkeeping . in total n A super node is divided into a speaker and a super node n-1 And members , The speaker will be elected in turn . At each time of bookkeeping , Existing speaker initiates blockchain proposal ( Block contents to be recorded ), Once there is at least (2n+1)/3 Accounting nodes ( speaker + an mp ) I agree to the proposal , Then this proposal will be the final block , And the block is irreversible .

<> process :

​ PBFT process :

<>RAFT

<> characteristic :

​ That's right PAXOS A simple implementation of the algorithm , The core idea is if the initial states of several databases are consistent , As long as the following and retrograde operations are consistent , It can ensure the consistency of the data .

<> process :

<> evaluation criterion

<> Comparison of consensus mechanism

PoWPoSDPoSRaftPBFT
Scene public chain public chain , Alliance chain and public chain , Alliance chain alliance chain alliance chain
Decentralisation, complete semi neutralization, semi neutralization
The accounting node selects several representatives of the whole network and one representative of the whole network Leader Dynamic decision
response time 10 minute 1 minute 3 Second or so second level second level
Storage efficiency full book full book full book full book full book + Some books
Throughput about 7TPS about 15TPS about 300TPS Or even thousands or even tens of thousands 10000TPS Or higher
fault-tolerant 50%50%50%50%33%

Technology