Although the term smart contract is now very popular, if you ask me what is a smart contract? I really can't give an accurate answer right away, so I looked at Wikipedia's entry on Smart contract:
A smart contract is a computer protocol intended to digitally facilitate, verify, or enforce the negotiation or performance of a contract. Smart contracts allow the performance of credible transactions withoutthird parties. These transactions is trackable and irreversible.
Proponents of smart contracts claim that many kinds of the contractual clauses may be made partially or fully self-executi Ng, self-enforcing, or both. The aim of smart contracts is to provide security this is superior to traditional contract law and to reduce other TRA Nsaction costs associated with contracting.
Can see, the so-called intelligent Contract intelligence, in fact, and artificial intelligence (Intelligence) still has a very big difference, the reason is called the smart contract, perhaps because it is more advanced than the traditional contract, mainly embodied in the following aspects
- You believe in the contract, you don't need a third party.
- It's traceable, irreversible.
- It can be executed automatically.
Do not underestimate these characteristics, the popularity of smart contracts, you can make people in the transaction process more transparent, you and a stranger trading, no longer a game relationship, but many games, because all the transaction records are traceable, imagine the following, if you are trading with others, you know in the future will be trading with this person , will you choose to cooperate or betray? Cooperation is of course. Therefore, compared to traditional contracts, smart contracts can reduce transaction costs (Transaction cost), thereby improving the overall social efficiency.
What is an EOS smart contract
In the Eosio developer documentation, there is a description
A real-world contract, simply stated, is an agreement governing outcomes for actions, given A set of Inpu Ts. A contract can range from formal legal contracts (e.g, a financial transaction) to something as simple as the "rules" of A game. Typical actions can be things such as fund transfers (in the case of a financial contract) or game moves (in the case of a Game contract).
The
Real-world contract, in a nutshell, is a set of protocols that give them the input of a certain behavior, and it produces the corresponding result. From formal legal contracts (such as financial transactions) to simple game "rules" are all part of the scope of the contract. A typical behavior can be a transfer (financial contract) or a player's mobile (game contract).
An Eosio Smart contract are software registered on the blockchain and executed on Eosio nodes, that implements the Semantics of a "contract" whose Ledger of action requests is being stored on the blockchain.
The
Eosio Smart Contract software is registered on the blockchain and executed on the Eosio node, which implements the semantics of the contract and stores the request behavior of the contract on the blockchain, that is, the request ledger.
The smart contract defined by EOS consists of two elements: The program representing the contract and the behavior when the contract is executed. At the same time, when you read the above text, you are likely to match it with the bitcoin system, in fact, Bitcoin is a special case of the smart contract: it only defines the transfer contract, that is, receive UTXO (unspent Transaction Output), verify the signature and create a new UTXO , and the contract has only one act--transfer (send Utxo).
And bitcoin to record UTXO on the blockchain, Eos also recorded all the behavioral data on the blockchain, the only difference is that EOS is a smart contract platform, it not only supports the transfer contract, now you can see the CPU, Bandwidth Mortgage, RAM man-machine trading market, Voting, among other things, can see the Intelligent contract platform is very imaginative, it provides diversity, even so, it is still composed of the most basic factors: the process of representing the contract and the conduct of the execution of the contract. From this point of view, EOS is still only a bitcoin extension, so in learning, remember these two elements, help in the process of learning not lost.
How to develop an EOS smart contract
To develop the EOS Smart contract, you need to complete the program of the contract and upload it to the EOS platform, the computer language, the current smart contract can only be C++ developed and compiled into a WebAssembly compatible executable program, because the Go language will support WebAssembly , so in the future you can also use Golangto write a smart contract, there are also developers who are connecting EOS to JavaScript virtual machines and Python virtual machines, so in the near future, the development of smart contracts will no longer be limited by the C++ Mountain.
Even if you can only use C++ language development now, it is not complicated to implement an EOS smart contract, in fact you only need to develop the behavior provided by the contract, and from this point of view, the relationship between contract and behavior is like the relationship between WebService and the Interface it provides. eosio.tokenWhat would you do if the current contract doesn't exist and requires you to implement it? eosio.tokenThis contract primarily accomplishes the creation, distribution, and transfer of tokens in the Eosio platform, so you should first define the 3 behaviors it has:
create: Used to create a new token
issue: Used to issue tokens, a bit like the function of a bank banknote printing
transfer: Allows the user to transfer funds between users
At the same time, as with the interface (interface) provided by the application, you need to expose all the behavior so that the user can make the call. Eosio provides a uniform behavior entry for each contract- apply function, you can interpret it as a route in WebService (routing), and for the sake of development, Eosio provides a macro, you only need to add a line of code, you can implement apply the function, As follows:
EOSIO_ABI( eosio::token, (create)(issue)(transfer) )
The above code create issue exposes, and transfer three behaviors, to the outside. That is, the user will execute the contract, first enter the apply function, and then according to the specific behavior, then into a different create , issue or transfer run.
In order to complete the eosio.token smart contract, we have implemented its behavior, and exposed these behaviors to the apply outside through the function, according to Eosio on the definition of the smart contract, there seems to be something less, indeed, in the process of implementation, We missed the most critical step: storing the behavior of the user requesting the smart contract on the blockchain.
We all know that blockchain is a chain of time series database, that is, it is a transaction as a unit, in accordance with the order of time, in the form of data structure stored in the chain list, the characteristics of the list is difficult to retrieve, so usually the blockchain system will be equipped with a KV database to accelerate the retrieval, Eosio is no exception, it provides a boost::multi_index(It Boost is a C++ powerful library of extensions) data structure, also known as a multi_index database, this database compared to the traditional KV database, the advantage is that it can build a multi-level index of the same data, retrieval efficiency is higher.
In Eosio, the multi_index database in addition to help speed up the retrieval, it is also a smart contract to write Blockchain bridge, so to write a good smart contract, the multi_index learning is essential, I believe you guessed, the multi_index use of resources is the most important--ram in the Eosio system, The importance of it is more explained.
Here is a good illustration of the process of EOS processing smart contracts, from top to bottom, the system executes each contract sequentially, from left to right, for each contract, the system assigns a context to it apply , and the context between the different contracts is isolated and does not affect each other.
Img
Above, you write an EOS smart contract points, if you read these content, feel that writing a smart contract is not difficult, then the purpose of this article is achieved, the next step, you should be able to read contracts/eosio.token the next code.
To summarize, this article focuses on what a smart contract is, what the EOS Smart contract specifically refers to, and the key to writing an EOS smart contract:
- Implement the behavior contained in a given contract, exposing the behavior to an external
- Learn to use the
multi_index database interface
Related articles:
- Teach you how to use EOS multi-signature-Protect your assets
- Eos. Plug-in layouts in IO | SOURCE Interpretation
- 5 lines of code to help you comb Eos.io's veins
Welcome to my knowledge planet and learn together: