Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Analysis of Solidity background data

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "Solidity background data Analysis". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "Solidity background data Analysis".

In essence, Etay Fong is a decentralized virtual machine, in the process of running, contracts can be executed in a certain order. A contract consists of a set of functions that users can call. For example, a standard ERC 20 contract should implement the following six functions.

When a user wants to transfer his ERC 20 token to another address, he can initiate a transaction to call the transfer function and let Ethernet Square execute the code corresponding to the transfer function in the contract to complete the transfer.

In Ethernet Square, users can implement three functions by initiating transactions:

Create a new smart contract

Execute the function in the contract

Transfer ether

The deal is packed into blocks by miners, all of which form a chain. In this way, all transactions have an order of execution, based on which the contract status and transaction balance at each moment can also be determined.

Contract writing

In Ethernet Square, the contract is published on the block chain as bytecode and executed in EVM (Ethereum Virutal Machine). When developing by users, there is no need to write bytecode directly. The Etay Fong community provides a language called Solidity. A code developed by Solidity is shown in the following figure.

In the first two lines of the contract, a variable and a dictionary mapping are defined. These two variables may be modified each time the contract is executed. When the next contract is executed, if you read these two variables, you will get the modified content.

The first function of the contract, AWallet, is the constructor of the contract, which is the same as the contract name. When this contract is created on the block chain, the constructor is executed and is typically used to initialize some variables in the contract.

In the Solidity code, you can get it through two variables, msg.sender and msg.value: 1. Who sent the transaction that triggered the execution of this contract, 2. How much ether has been transferred to the contract by the transaction that triggered the execution of this contract? Based on these two variables, the authority management of contract operation can be realized, and some digital assets can be changed from user private key control to contract execution control.

Solidity allows the existence of exceptions, such as the first line of the function pay. If the address of calling this function is not the same as that of creating the contract, it means that the person calling the contract does not have permission to use the assets in the contract, so an exception is triggered through throw. When the exception is triggered, there is no way to catch, directly stop the execution of the contract, roll back the status, and deduct all transaction costs.

For the variable r of type address, r.send (amount) means that the contract sends wei with the amount of amount to address v. (10 ^ 18 wei = 1 ether)

The last function function () without a function name is called the fallback function, which is a special function in the smart contract. Because there is no support for functions in EVM bytecode, after Solidity compiles the code into bytecode, the first part of the bytecode is function matching. The user tells EVM which function to execute by specifying the name of the function to call and the first four bytes of the hash value of the parameter type. For example, byte4 (sha3 ("pay (uint,address)")), these four bytes are called function signatures. If the function signature used by the user when calling matches a function, the function is executed. If no function is matched, the fallback function is executed as a default option.

Fuel cost

In Ethernet Square, each call to the contract function needs to be performed by all miners. The transaction fees in the transaction can motivate miners to do this while protecting the execution of etheric Fong contracts from DoS attacks (attackers submit a large number of computer-sized contracts).

The execution transaction fee is determined by fuel consumption (gas used) and fuel cost (gas price). Fuel consumption is calculated by operations performed during the execution of the contract, and each bytecode instruction of the EVM virtual machine is marked with fuel consumption. The fuel fee is the user's bid, and the higher fuel fee means that the transaction can be added to the block earlier. The amount of fuel used multiplied by the cost of fuel is the transaction fee for the execution of the contract.

At the same time, the fuel consumption ceiling (gas limit) should be specified for each transaction. The maximum amount of fuel multiplied by the cost of fuel is paid in advance. When the transaction is initiated, make sure that there is enough money in the account balance to pay the advance payment. When the upper limit of fuel consumption is reached, if the execution has not been completed, the execution will be judged to have failed, all operations will be rolled back, and the advance payment will be confiscated. If the contract is executed successfully, the unused portion of the advance payment will be returned.

Thank you for your reading, the above is the content of "Solidity background data Analysis". After the study of this article, I believe you have a deeper understanding of Solidity background data analysis, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report