Yin Cheng 2021-11-24 08:08:19 阅读数:995
Intelligent contract
What is a smart contract
The program on Ethereum is called smart contract , It's code and data ( state ) Set .
Smart contracts can be understood as those that can be executed automatically on the blockchain ( Message driven )、 A contract written in code ( Special transactions ).
Smart contract is Smart Contract, And artificial intelligence ( AI:Artificial Intelligence ) Your intelligence doesn't matter , The first nick Saab was 95 The concept of smart contract was put forward in , Its concept is very simple , Is to write the legal provisions into executable code . There was no blockchain , However, smart contracts are best matched with blockchains , We know that all contracts are made in duplicate 、 Three or four , Not in the hands of one party , This is called decentralization .
stay Bitcoin script in , We mentioned that bitcoin transactions are programmable , But bitcoin scripts have many limitations , The programs that can be written are also limited .
Ethereum is more complete ( In computer science terminology , Call it yes “ Turing complete ”), Let's write programs that can do almost anything just like any high-level language ( Intelligent contract ).
Smart contracts are great for trust 、 Application scenarios with high security and persistence requirements , such as : Digital currency 、 Digital assets 、 vote 、 insurance 、 Financial applications 、 Forecast market 、 Property ownership management 、 The Internet of things 、 Point to point trading and so on .
At present, in addition to digital currency , There are not many real applications ( Just like the mobile platform is just beginning to come out ), Believe in 1 To 3 During the year , All kinds of killers will slowly appear .
programing language :Solidity
The official recommended programming language for smart contracts is Solidity, The file extension starts with .sol ending .
Solidity Language and JavaScript Very similar , Use it to develop contracts and compile them into Ethereum virtual machine bytecode .
also Viper,Serpent,LLL And Bamboo, I suggest you still use Solidity.
to update :Serpent The authorities no longer recommend , Suggest Serpent The user of the switch to Viper, They are all kind of Python Language .
Browser-Solidity It's a browser Solidity IDE, You can go in and have a look , In the future, we will introduce more articles Solidity This language .
Running environment :EVM
EVM(Ethereum Virtual Machine) Ethereum virtual machine is the running environment of smart contract in Ethereum .
Solidity To EVM, It's like talking to JVM Same relationship , So it's easy for everyone to understand .
Ethereum virtual machine is an isolated environment , The outside is inaccessible in EVM Internally running code .
and EVM Running on Ethereum nodes , When we deploy the contract to the Ethereum network , The contract can run in the Ethereum network .
Contract compilation
What runs on Ethereum virtual machine is the bytecode form of contract , We need to compile the contract before deployment , You can choose Browser-SolidityWeb IDE or solc compiler .
The deployment of the contract
When developing applications on Ethereum , Always use the Ethereum client ( wallet ). Usually we are developing , Generally do not touch the concept of client or wallet , What is it ?
Ethereum client ( wallet )
Ethereum client , In fact, we can understand it as a developer tool , It provides account management 、 dig 、 Transfer accounts 、 Smart contract deployment and execution and other functions .
EVM It is provided by Ethereum client
Geth It's a typical client for developing Ethereum , be based on Go Language development . Geth Provides an interactive command console , The command console contains various functions of Ethereum (API).Geth We will have an article on the use of , Let's start with a concept .
Geth Console and Chrome The console in the browser developer tool is similar , however Geth The console is running in the terminal .
be relative to Geth,Mist It is the Ethereum client with graphical operation interface .
How to deploy
The deployment of smart contract refers to publishing the contract bytecode to the blockchain , And use a specific address to mark the contract , This address is called the contract account .
There are two types of accounts in Ethereum :
And bitcoin UTXO The design is different , Ethereum uses a simpler account concept .
Two types of accounts for EVM It's the same for me .
This is the difference and relationship between external accounts and contract accounts : An external account can sign transactions by creating and using its own private key , To send a message to another external account or contract account .
Transferring messages between two external accounts is the process of value transfer . However, the message from the external account to the contract account will activate the code of the contract account , Allow it to perform various actions ( Like transferring tokens , Write to internal storage , Dig out a new token , Perform some operations , Create a new contract, etc ).
Only when an external account gives instructions , The contract account will perform the corresponding operation .
Contract deployment is to deploy the compiled contract bytecode to the Ethereum blockchain in the form of sending transactions through an external account ( After the actual miners get out of the block , It's a real deployment ).
function
After contract deployment , When you need to call the method of the smart contract, you only need to send a message to the contract account ( transaction ) that will do , After triggering by message, the code of smart contract will be in EVM To perform the .
Gas
Similar to cloud computing , Occupy the resources of the blockchain ( Whether it's a simple transfer transaction , Or the deployment and execution of the contract ) Also need to pay the corresponding cost ( There is no free lunch, right !).
Used in Ethereum Gas Mechanism to charge ,Gas It can also be considered as a unit of work , The more complex the smart contract is ( Calculate the number and type of steps , Occupied memory, etc ), The more it takes to complete the run Gas.
The number of operating contracts required for any particular contract Gas The quantity is fixed , Determined by the complexity of the contract .
and Gas The price is set by the operator of the operation contract when submitting the operation contract request , To determine what he is willing to pay for the deal :Gas Price ( In ether ) * Gas Number .
Gas The purpose is to limit the amount of work required to execute the transaction , At the same time, pay for execution . When EVM When executing a transaction ,Gas Will be consumed gradually according to specific rules , No matter where it is executed , once Gas Exhausted , Will trigger an exception . All status changes made by the current call frame will be rolled back. , If the execution is over, there are Gas The remaining , these Gas Will be returned to the sending account .
If there is no such limitation , Someone will write that you can't stop ( Such as : Dead cycle ) To block the network .
So in fact ( String the previous content ), We need an external account with an ether balance , To start a deal ( Ordinary transaction or deployment 、 Run a contract ), Runtime , Miners charge corresponding workload fees .
Ethereum network
Some anxious students have to ask , No ether , How to develop smart contracts ? You can choose from the following ways :
Choose Ethereum official website to test the network Testnet
Test the network , We can easily get free ether money , The disadvantage is that it takes a long time to initialize the node .
Use private chains
Create your own Ethernet private test network , It's also called private chain , We can use it as a test environment to develop 、 Debugging and testing smart contracts .
Through the above Geth It's easy to create your own test network , Dig as much as ether wants , It also eliminates the need to synchronize the entire blockchain data of the formal network .
Use the developer network ( Pattern )
Compared to the private chain , Developer Network ( Pattern ) Next , Will automatically allocate a developer account with a large balance for us to use .
Use simulation environment
Another way to create a test network is to use testrpc,testrpc It is an Ethereum environment simulated locally using memory , For development debugging , More convenient and quick . and testrpc Can help us create... At startup 10 A test account with money .
In contract development , Can be in testrpc After passing the middle test , Then deploy to Geth Go to the node . to update :testrpc Now it has been incorporated into Truffle In the development framework , Now the name is Ganache CLI.
Dapp: Decentralized applications
Ethereum community calls smart contract based applications decentralized applications (Decentralized App). If we understand blockchain as a database that cannot be tampered with , Smart contracts are programs that deal with databases , It's easy to understand Dapp 了 , One Dapp Not just smart contracts , For example, you need a friendly user interface and other things .
Truffle
Truffle yes Dapp Development framework , He can help us deal with a lot of unimportant little things , Let's start writing code quickly - compile - Deploy - test - pack DApp This process .
summary
Now let's summarize , Ethereum is the platform , It allows us to easily use blockchain technology to develop decentralized applications , In this application , Use Solidity To write smart contracts that interact with blockchain , After the contract is written , We need an Ethereum client with a balance account to deploy and run the contract ( Use Truffle Framework can better help us do these things ). For development convenience , We can use Geth or testrpc To build a test network .
website :http://www.qukuailianxueyuan.io/
Want to get the coinage technology and a full set of virtual machine information
Blockchain technology exchange QQ Group :756146052 remarks :CSDN
Wechat of Yin Cheng College : remarks :CSDN
版权声明:本文为[Yin Cheng]所创,转载请带上原文链接,感谢。 https://netfreeman.com/2021/11/20211108224341565s.html