What are Some best practices for Ethereum Smart Contracts?
1. Smart Contracts
A bridge smart contract can be simply understood as an executable program segment consisting of many methods and variables .
After the local compilation is successful, it can be published (deployed) to the blockchain (equivalent to sending a transaction).
2. Ethereum Smart Contract
The smart contract of Ethereum can be understood as a special transaction (including executable code), which will be packaged and recorded in a certain block by miners after being sent . The address of the smart contract can send a transaction .
3. Trigger the execution of the contract
There are two ways to execute smart contracts:
One is to change the state of the contract (that is, to store data), then a transaction will be triggered (or triggered by a transaction)
One is a local call that doesn’t change the state of the contract (like just reading data, etc.). Generally speaking, it will be automatically processed when the method is called through ABI. If the constant return is declared, it will be a call operation, and the others will trigger a send Transaction operation.
smart contract is to tell the EVM that I want to execute a certain method of a smart contract. This way of telling is achieved by sending a transaction (call to change the state), and the method of executing the smart contract needs to consume For computing resources and storage resources, the consumption of these resources needs to pay a price, and the cost is gas in the economic model of Ethereum
4. Execution environment of Ethereum smart contracts
Each node computer needs to install the Ethereum client, and this Ethereum client comes with EVM , an execution environment for Ethereum smart contracts, similar to JVM.
You need to know the address of the smart contract to access the smart contract, and you also need to know the ABI interface of the smart contract to interact with the smart contract (including accessing the methods and data of the smart contract). After the smart contract is triggered by a transaction , the code of the smart contract will be in the EVM. implemented.
This method is equivalent to deploying the program to a very large number of computers (as long as the computer is installed with the Ethereum client and connected to the Ethereum network), the execution of these smart contracts can be triggered through transactions at any time, thereby completing The deployment and invocation of distributed programs.
5. Limitations of Smart Contracts
At present, the functions of smart contracts are still very limited, and they cannot complete all the things that can be solved by current programming ( such as accessing the network to download files , which is currently not allowed or supported by EVM).
But this does not prevent the development of Ethereum and smart contract technology, and the possibility of becoming the next generation of Internet infrastructure and protocols!
6. Consume Ether
As long as the change of state is involved on Ethereum, ether will be consumed . For example, the generation of a transaction will change the state of the account, the storage of a smart contract or the change of the state of the smart contract, etc., which consumes both computing resources and storage resources. Pay the fee.
A smart contract is a program fragment consisting of many methods and variables. Cross chain bridge development Deploying a smart contract to the chain (equivalent to sending a transaction) requires ETH, and changing the internal state of a smart contract through a smart contract also consumes ETH, but there are also Reading locally without changing state does not require ether.
7. Where is the code of the smart contract stored?
A smart contract is published by submitting a transaction, including the code and interface of the contract, which is then included in a block, and finally linked to the blockchain.
The code and interface information of the smart contract is placed in a block data structure called the state tree (Merkel tree ). The call is made by sending a transaction to the Ethereum network, which includes the address of the smart contract (transaction id) and other call parameters.
When miners package this transaction, they will find the contract through the contract address and run the contract by calling the parameters.
After the smart contract is deployed, you can obtain the address of the contract , and you can use this contract address to search.
8. Where to save the data generated by the smart contract
There is a , which is used to store the data generated by the contract. I understand that the bottom layer should be a database. When querying a general account, it corresponds to the account balance, and when querying a contract account, it corresponds to the of the contract. , and then run through the query command operation.
I never understood before that creating so many accounts on the Mao blockchain will not collapse. Now that I think about it, each new account creates a relationship, and only smart contracts consume storage.
9. Ethereum’s smart contracts are “destroyed”, but the data on the blockchain cannot be tampered with and can only be continuously increased in length. So how is this “destroyed” accomplished?