How to Debug Ethereum Smart Contract

How to Debug Ethereum Smart Contract

How to Debug Ethereum Smart Contract

How to Debug Ethereum Smart Contract

How to Debug Ethereum Smart Contract

Read Time: 5 minutes

Debugging a smart contract transaction in the blockchain is different from debugging a traditional application build on c++ or JavaScript as you are not running code in real time, instead of in blockchain, historic transaction execution is mapped with the associated code to debug a transaction.

A Smart contract is a piece of code run on the top of the blockchain, smart contracts need to make sense in a precise manner, debugging a Smart contract is analyzing a transaction step by step how functions internally work and proceed or where the actual transaction has failed.

Let us discuss first types of error in solidity in detail to debug transaction.

Types of Error in solidity  –

1. Syntax Error

A syntax error arises when there is a problem in a syntax of smart contract code when your code strike with syntax error your smart contract will not be able to compiled or deployed on the blockchain.

Syntax error can be easily diagnosed using remix, as you can see in the picture below, when semicolon(;) is removed from the above code in remix IDE, it displays an error message with solution of syntax error.

Syntax error can be easily diagnosed using truffle, remix or any IDE.

2. Run time Error

Run time errors arises only when you have deployed a smart contract to blockchain and your solidity code has been compiled to bytecode that has been understood by EVM (Ethereum virtual machine). EVM is defined as a component of Ethereum that runs solidity code and run time error arises when EVM thinks that you are doing something wrong with your smart contract code or you are making transaction against the logic of the code.

Run time errors are more difficult to diagnose than syntax error because these errors are not identified before deploying on blockchain, they can only arise when state change takes place in smart contract, we will discuss more run time errors and debugging of run time errors in details.

3. Logic Errors

Logic errors cannot be debugged using any IDE or tool, because logic errors are not captured by EVM, according to EVM everything is perfect and code can be run easily. the logic error arises when a developer makes a mistake and open loopholes in a smart contract for attackers.

Example of Logic Error: Famous DAO attack(Reentrancy) is an example of a logic error where the developer made a mistake and transferred either before updating the balance of the user.

Logical errors can be identified by Audit of smart contract because sometimes according to developer’s mindset everything is going well and there may not be an error in smart contract but an Auditor can run a smart contract with the intent to find loopholes, check business logic and to find security flaws in smart contract.

Formal Verification may come into use for logic verification of smart contract.

Types of Run time error

Out of Gas

Out of gas error occurs when you don’t provide enough gas to execute a transaction, or gas is not enough to complete a transaction.

Revert

Transaction revert will occur when you are trying to execute a transaction that cannot be executed according to the logic of smart contract hence EVM will return an error and transaction is reverted.

Invalid opcode

Invalid opcode occurs when you are trying to call a code that doesn’t exist.

Invalid Jump

The invalid jump occurs when you are trying to call a function that doesn’t exist, for example, if you call a function of contract via another contract that doesn’t exist, an invalid jump occurs. This type of error also occurs when you use assembly language and you point to wrong memory.

Stack Overflow

Stack Overflow occurs when you are trying to call the function recursively and there is no condition to stop it, in solidity stack can be at most 1024 frame, so a function can call itself only 1024x times if you exceed that Stack Overflow will occur.

Stack Underflow

Stack underflow occurs in assembly language when you try to pop a variable that doesn’t exist.

Sample Smart Contract code to be used in debugging all types of errors and transactions-

pragma solidity 0.4.24;
contract Sample {
   uint256 public amountInContract;
   address public amountOwner;
  constructor(address _owner) public payable {
     require(_owner != address(0));
     require(msg.value > 0.1 ether);
     amountInContract = msg.value;
     amountOwner = _owner;
   }
 function () external{
  revert();
 }
function withdraw () public {
require(msg.sender==amountOwner);
msg.sender.transfer(address(this).balance);
}
}

The debugger provides the ability to debug any transaction (creation or call) step by step and to visualize the memory/storage space. It helps to trace the gas cost by the opcode and the remaining gas after each step.

Debugging a transaction-

In above code as specified constructor is payable while deploying a contract constructor required sending ether value above 0.1 ethers. If less than 0.1 ether is sent or address is zero, it will throw an error and transaction will be reverted.

As shown below, the transaction is reverted so we will debug a transaction step by step using remix IDE.

Step 1. After deployment transaction is submitted to Ethereum blockchain using remix, the transaction is failed as shown above. Now press the debugger button in remix IDE to debug the transaction and to find out the possible reason for transaction failure.

Step 2. To start debugging, provide block number and transaction hash and press the start debugging button this will start the debugging process.

Assembly language operations along with with the gas used and remaining gas are displayed while debugging a transaction, as shown above, also the code is highlighted in remix IDE where actual transaction failed parallel to opcode as shown below.

The transaction is reverted in assembly opcode. The code is highlighted where actual transaction reverted.

As shown in the above picture where transaction is reverted in etherscan the value sent is 0.1 ether, that is less than the required value; that is why the transaction failed in this scenario.

This process can be used to debug any transaction to know more about the gas used, remaining gas at each step, state changes and to visualize memory/storage space.

Smart contract debugging can also be done using truffle framework, you need a transaction hash to debug a transaction.

Launch your blockchain project with Quillhash: https://quillhash.typeform.com/to/KQ5Hhm


At QuillHash, we understand the Potential of Blockchain and have a good team of developers who can develop any blockchain applications like Smart Contracts, dApps,Smart Coins, DeFi, DEX on the any Blockchain Platform like Ethereum, EOS and Hyperledger.

To be up to date with our work, Join Our Community :-

Telegram | Twitter | Facebook | LinkedIn

3,199 Views

Blockchain for dog nose wrinkles' Ponzi makes off ~$127M🐶

Project promised up to 150% returns on investment in 100 days, raising about 166.4 billion South Korean won — or about $127 million — from 22,000 people.

Latest blogs for this week

Understanding Fuzzing and Fuzz Testing: A Vital Tool in Web3 Security

Read Time: 5 minutes When it comes to smart contracts, ensuring the robustness and security of code is paramount. Many techniques are employed to safeguard these contracts against vulnerabilities
Read More

How EigenLayer’s Restaking Enhances Security and Rewards in DeFi

Read Time: 7 minutes Decentralized finance (DeFi) relies on Ethereum staking to secure the blockchain and maintain consensus. Restaking allows liquid staking tokens to be staked with validators in
Read More

ERC 404 Standard: Everything You Need to Know

Read Time: 7 minutes Introduction Ethereum has significantly shaped the crypto world with its introduction of smart contracts and decentralized applications (DApps). This has led to innovative developments in
Read More

DNS Attacks:  Cascading Effects and Mitigation Strategies

Read Time: 8 minutes Introduction DNS security is vital for a safe online space. DNS translates domain names to IP addresses, crucial for internet functionality. DNS ensures unique name-value
Read More

EIP-4844 Explained: The Key to Ethereum’s Scalability with Protodanksharding

Read Time: 7 minutes Introduction  Ethereum, the driving force behind dApps, has struggled with scalability. High fees and slow processing have limited its potential. They have kept it from
Read More

QuillAudits Powers Supermoon at ETH Denver!

Read Time: 4 minutes Calling all the brightest minds and leaders in the crypto world! Are you ready to build, connect, and innovate at the hottest event during ETH
Read More

Decoding the Role of Artificial Intelligence in Metaverse and Web3

Read Time: 7 minutes Introduction  Experts predict a transformative shift in global software, driven by AI and ML, marking the dawn of a new era. PwC predicts AI will
Read More

Transforming Assets: Unlocking Real-World Asset Tokenization

Read Time: 7 minutes In the blockchain, real-world assets (RWAs) are digital tokens that stand for tangible and conventional financial assets, including money, raw materials, stocks, and bonds. As
Read More
Scroll to Top

Become a Quiffiliate!
Join our mission to safeguard web3

Sounds Interesting, Right? All you have to do is:

1

Refer QuillAudits to Web3 projects for audits.

2

Earn rewards as we conclude the audits.

3

Thereby help us Secure web3 ecosystem.

Total Rewards Shared Out: $200K+