In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how to troubleshoot Solidity". 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 "how to troubleshoot Solidity".
1 Editor description
1. Recommended editor
By far the best way to try Solidity programming is to use Remix (https://remix.ethereum.org/) (it takes time to load, please be patient). Remix is a Web-based IDE that allows you to write a Solidity smart contract, then deploy and run it.
2 、 Visual Studio Extension
Solidity plug-in for Microsoft Visual Studio, including the Solidity compiler.
3 、 Visual Studio Code extension
Microsoft Visual Studio Code plug-in, including syntax highlighting and Solidity compiler.
Function reference of 2REMIN
Function mint (address receiver, uint amount)
1. When entering REMIX, the address must be represented by "", otherwise amount will not be able to get the value.
For example, mint ("0xca35b7d915458ef540ade6068dfe2f44e8fa733c", 101)
2. In the program, if the comment contains Chinese, the location of single-step debugging is not accurate.
3address related global functions
.balance (uint256): how much is the balance in Tai Fong at this address (in wei)
.transfer (uint256 amount): sends a specific number of Ethernet cells (in wei units) to the corresponding address, throws an exception when an error occurs, but does not stop because of the exception. A fixed cost of 2300 gas.
.send (uint256 amount) returns (bool): sends a specific number of Ethernet cells (in wei units) to the corresponding address, and returns flase when an error occurs. A fixed cost of 2300 gas.
[warning] send () execution has some risks: if the depth of the call stack exceeds 1024 or the gas runs out, the transaction will fail. Therefore, in order to ensure security, the return value of send must be checked and the etheric currency will be returned if the transaction fails. It would be better to use transfer.
.call (...) Returns (bool): a low-level calling function of CALL that returns false on failure. Execution requires a variable amount of gas.
[description] the use of the call function is discouraged and will be removed later. Calling this function may cause a security attack. For more information, please see later security related articles.
.callcode (...) Returns (bool): a low-level calling function of CALLCODE that returns false on failure. Execution requires a variable amount of gas.
It is not recommended and will be deleted in subsequent versions.
.delegateCall (...) Returns (bool): a low-level calling function of DELEGATECALL that returns false on failure. Execution requires a variable amount of gas.
[note] in order to interact with non-ABI contracts, you can use the call () function, which is used to send raw data to another contract and supports any number of parameters of any type. Each parameter is packaged into 32 bytes according to the rule (ABI protocol) and spliced together one by one. One exception is that if the first parameter is exactly 4 bytes, in this case, it is considered to be used directly according to the function signature specified by the function defined by the ABI protocol. If you only want to send the message body, you need to avoid that the first parameter is 4 bytes. Such as the following example:
Function callfunc (address addr) returns (bool) {bytes4 methodId = bytes4 (keccak256 ("setScore (uint256)"); return addr.call (methodId, 100);}
Test address and address call code example
Pragma solidity ^ 0.4.18 position contract AddrTest {/ * event function knowledge prints parameter information in the LOG location area of compilers such as REMIX, without the need for function definition. * / event logdata (bytes data); event LogContractAddress (address exAccount, address contractAddress); uint score = 0; / * callback function, no function name. If any function is called that does not exist, the fallback function of the contract being called executes. Payable: if a function needs currency operation, it must take the payable keyword * / function () payable {logdata (msg.data);} / * Intelligent contract Construction function * / function AddrTest () {LogContractAddress (msg.sender,this);} function getBalance () returns (uint) {return this.balance;} function setScore (uint s) public {score = s } function getScore () returns (uint) {return score;}} contract CallTest {/ * this function declares that there is no actual function content. Set the number of Ethernet Square in the value region of remix. Calling this function will transfer the Ethernet Square in the external account (ACCOUNT) to the intelligent contract account * / function deposit () payable {} event logSendEvent (address to, uint value). Event LogContractAddress (address exAccount, address contractAddress); / * transfer to the destination address * / function transferEther (address towho) payable {towho.transfer (10); / * in wei*/ logSendEvent (towho, 10) } / * if the calling function is not specified, the callback function without function name will be called * / function callNoFunc (address addr) returns (bool) {return addr.call ("tinyxiong", 1234);} / * formulate the method for calling the function * / function callfunc (address addr) returns (bool) {bytes4 methodId = bytes4 ("setScore (uint256)"); return addr.call (methodId, 1234) } / * return the balance of the current contract * / function getBalance () returns (uint) {return this.balance;//0} / * destroy the smart contract and return the balance of the Ethernet place to the current external account * / function ContractSuide () {LogContractAddress (this,msg.sender); suicide (msg.sender);}} 4Contract Related
This (current contract's type): represents the current contract and can be explicitly converted to Address
Selfdestruct (address recipient): destroy the current contract, sending its funds to the given Address
Destroy the current contract and send the current ether place balance to the given address suicide (address recipient):
Alias function of selfdestruct
5 Block and transaction attributes
* * block.blockhash (uint blockNumber) returns (bytes32): * *
Hash of a given block-valid only for the nearest 256 blocks but not the current block
* * block.coinbase (address): * *
Dig up the address of the miners in the current block
* * block.difficulty (uint): * *
Current block difficulty
Block.gaslimit (uint):
Current block gas quota
* * block.number (uint): * *
Current block number
Block.timestamp (uint):
Timestamp in seconds of the current block since unix epoch
* * msg.data (bytes): * *
Complete calldata
* * msg.gas (uint): * *
Remaining gas
Msg.sender (address):
Message sender (current call)
* * msg.sig (bytes4): * *
The first 4 bytes of the calldata (that is, the function identifier)
Msg.value (uint):
The number of wei sent with the message
* * now (uint): * *
Current block timestamp (block.timestamp)
* * tx.gasprice (uint): * *
Gas price of the transaction
* * tx.origin (address): * *
Transaction initiator (full invocation chain)
Annotations change the values of all msg members, including msg.sender and msg.value, for each external function call. This includes calls to library functions.
Note do not rely on block.timestamp, now, and block.blockhash to generate random numbers unless you know what you are doing.
Both timestamp and block hash may be affected by miners to some extent. For example, malicious miners in the mining community can run the payout function of the casino contract with a given hash, and if they don't receive the money, they can try again with a different hash. The timestamp of the current block must be strictly larger than that of the last block, but the only guarantee here is that it will be the value between the timestamps of two consecutive blocks on the chain of authority.
Annotations are based on extensible factors, and block hashes are not valid for all blocks. You can only access the hashes of the last 256 blocks, and the rest of the hashes are zero.
6 error handling
Assert (bool condition): thrown if the condition is not met-for internal errors.
Require (bool condition): discard if the condition is not met-for errors caused by input or external components.
Revert (): terminates the operation and resumes the state change.
7 Mathematical and cryptographic functions
Addmod (uint x, uint y, uint k) returns (uint): compute (x + y)% k, the addition will be performed at any precision, and the result of the addition will not be intercepted even if it exceeds 2 percent 256. Starting with version 0. 5. 0, the compiler adds a check for k! = 0 (assert).
Mulmod (uint x, uint y, uint k) returns (uint): compute (x * y)% k, the multiplication will be performed at any precision, and the result of the multiplication will not be intercepted even if it exceeds 2 times 256. Starting with version 0. 5. 0, the compiler adds a check for k! = 0 (assert).
Keccak256 (...) Returns (bytes32): calculates (tightly packed) the Ethereum-SHA-3 (Keccak-256) hash of arguments.
Sha256 (...) Returns (bytes32): calculates (tightly packed) the SHA-256 hash of arguments.
Sha3 (...) Returns (bytes32): equivalent to keccak256.
Ripemd160 (...) Returns (bytes20): calculates (tightly packed) the RIPEMD-160 hash of the arguments.
Ecrecover (bytes32 hash, uint8 v, bytes32 r, bytes32 s) returns (address): uses the elliptic curve signature to recover the address related to the public key, and returns zero in error. (example usage: https://ethereum.stackexchange.com/q/1777/222)
The word "tightly packed" above means that the parameter values will not be padding processed, which means that the following calls are equivalent:
Keccak256 ("ab", "c") keccak256 ("abc") keccak256 (0x616263) keccak256 (6382179) keccak256 (97, 98, 99)
If you need padding, you can use explicit type casting: keccak256 ("\ X00\ x12") is the same as keccak256 (uint16 (0x12)).
Note that constant values are packaged with the minimum number of bytes required to store them. For example: keccak256 (0) = = keccak256 (uint8 (0)), keccak256 (0x12345678) = = keccak256 (uint32 (0x12345678)).
On a private chain, you are likely to encounter Out-of-Gas caused by sha256, ripemd160, or ecrecover. The reason for this is that they are executed as so-called precompiled contracts, and these contracts do not really exist until the first message is received (although the contract code is hard code). Messages sent to contracts that do not exist are expensive, so actual execution can lead to Out-of-Gas errors. Before you actually use them in your contract, send a little etheric currency to each contract, such as 1 Wei. This is not a problem on the official or test networks.
Common error prompts and cause Analysis of 8solidity
1. Failed to execute the smart contract
Alarm description: "Warning! Error encountered during contract execution [Out of gas]"
Scenario: execute the crowdfunding intelligent contract code on the official website. Type ETH to the smart contract address account before giving the token to the smart contract, and the transaction fails. The prompt is as follows:
Click on the view information link: https://ropsten.etherscan.io/tx/0x8b4da573b36dbf7361c95a0156dfe060553874fbb4d401f989c7a4a6d539ebfa
Code and cause analysis: the following is the callback function executed, where "tokenReward.transfer (msg.sender, amount / price);" means to call the token of the smart contract to the sender's account. This account does not have a token yet, so it is bound to fail.
Function () payable {require (! crowdsaleClosed); uint amount = msg.value; balanceOf [msg.sender] + = amount; amountRaised + = amount; tokenReward.transfer (msg.sender, amount / price); FundTransfer (msg.sender, amount, true);}
Solution: first type tokens to the smart contract account, and then type ETH, and the execution will not fail.
2. In the case of REMIX+MetaMASK, call the intelligent contract function to prompt the unknown address.
Alarm description:
The REMIX output box outputs the following alarm content: transact to Crowdsale.checkGoalReached errored: Unknown address-unable to sign transaction for this address: "0x3d7dfb80e71096f2c4ee63c42c4d849f2cbbe363"
Occurrence scenario: after changing the Meta account, call the function of the intelligent contract created by the previous account.
Cause analysis: Remix output box prompts unknown address:
Alarm description
Check the current account of MetaMask and find that it is the account of Account 1, so the contract cannot be executed.
Current account number of MetaMask
Solution:
Change the account whose MetaMask is Account8 (address is 0x3D7DfB80E71096F2c4Ee63C42C4D849F2CBBE363) to run normally.
3. Exception occurred during GETH installation
Alarm description: the following alarm occurs during GETH installation:
E: Failed to fetch http://101.110.118.22/ppa.launchpad.net/ethereum/ethereum/ubuntu/pool/main/e/ethereum/ethereum_1.8.10+build13740+artful_i386.deb Could not connect to 101.110.118.22 connection timed out 80 (101.110.118.22)
E: Unable to fetch some archives, maybe run apt-get update or try with-- fix-missing?
Occurrence scenario:
GETH installation, execute the following command appears.
Sudo apt-get install ethereum
Cause analysis:
Solution:
Refer to the online solution sudo vim / etc/resolv.conf and add:
Nameserver 8.8.8.8
Then execute:
Sudo / etc/init.d/networking restart
I still can't.
If you move from the Greenland Financial Center to your home and do the same thing, you can successfully install it. It should be that the router in Greenland Financial Center made some configuration, which caused the download to be unsuccessful.
4. Problem description template
Alarm description: occurrence scenario: cause analysis: solution:
9 frequently asked questions and answers
1) what is the modifer function for?
2)。 How to coin back to the payment account?
3)。 What are the timers and system functions of smart contracts?
4)。 What is the difference between msg.sender and this when creating a smart contract?
Answer: msg.sender refers to the address of the external account, and this refers to the address of the smart contract currently created.
Contract AddrTest {event LogContractAddress (address exAccount, address contractAddress); function AddrTest () {LogContractAddress (msg.sender,this);}}
Running on remix can prove this conjecture.
LOG description
Thank you for reading, the above is the content of "how to troubleshoot Solidity faults". After the study of this article, I believe you have a deeper understanding of how to troubleshoot Solidity faults, 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.