Open In App

How to Debug Ethereum Smart Contracts?

Last Updated : 08 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Ethereum smart contracts are the collection of code and data which is present in the Ethereum blockchain at a well-defined address. It is a block of code that runs on blockchain. It is a program that executes on the Ethereum blockchain. Smart contracts execute automatically. These contracts form the basic blocks of the Ethereum blockchain.

Debugging smart contracts is the process of analyzing the internal work of the transaction sequentially. Debugging allows us to find and fix the errors in the smart contract. Debugging checks the smart contract functions whether generates the desired output. 

Debugger provides the functionality to debug a transaction sequentially to analyze the space, cost, and cost after each step. Debugger finds and fixes the error in the smart contracts. It also checks the functions that perform the specified task.

Types of Error

The following are the types of errors in the Ethereum smart contracts:

1. Syntax Errors

Syntax errors are errors that occur because of incorrect syntax in the smart contract code. The syntax errors are identified by the compiler. Smart contracts cannot be compiled or deployed if any syntax error is present in it. Syntax errors can be easily identified using tools such as Remix, truffle, etc.

2. Logical Errors

Logical errors are errors that occur because of incorrect logic in the contracts at the time of development. These errors execute the contracts and produce undesired results. These errors arise when there is a mistake or open loopholes made by the developer. Logical errors cannot be identified using IDE or EVM. It can be identified using an audit of smart contracts. The auditor runs the smart contracts with the purpose of finding logical mistakes, loopholes, etc.
Some of the ways to debug Logical errors in Remix IDE are:

  • Function Stack: The function stack in the debugging process gives the list of functions involved in the transaction. It gives information on the functions through which one can identify errors.
  • Call Stack: The call stack in the debugging process gives the order in which the functions are called during the execution of the transaction.
  • Solidity State: Solidity state in the debugging process gives information about the state variables and state changes during the transaction execution.

3. Runtime Errors

Runtime errors are the errors that occur after the deployment of the smart contracts. Runtime errors are identified by the EVM (Ethereum Virtual Machine). These errors are difficult to be identified as they cannot be identified before the deployment of the smart contracts. These errors arise after the state change of the smart contract.
Some of the runtime errors in Ethereum smart contract are:

  1. Out of gas: Out of gas is a runtime error that occurs when we have an insufficient amount of gas to run a transaction.
  2. Reverted Execution: Reverted execution is a runtime error that occurs when the transaction’s further execution is interrupted, which leads to a change in the logic of the contract. 
  3. Invalid opcode: Invalid opcode is a runtime error that occurs rarely when the EVM lands to ambiguity like incompatibility with the smart contract etc.  
  4. Invalid Jump: Invalid jump is a runtime error that occurs when the target address of the jump does not exist, or it is not present in the contract.
  5. Stack Overflow: Stack overflow is a runtime error that occurs when a function is called recursively and does not contain a base condition. 
  6. Stack Underflow: Stack underflow is a runtime error that occurs when we try to execute a POP operation in an empty stack.

Some of the ways to debug runtime errors in Remix IDE are:

  • Reverted execution errors: The reverted errors are the runtime error that can be debugged using the function stack and call stack characteristics in the Remix IDE debugging process. The function stack and call stack give information about the execution of the transaction so that reverted execution can be traced. 
  • Out-of-gas errors: The out-of-gas error can be traced with the help of step details in the Remix IDE debug process which gives the information about the gas used and required for the transaction.
  • Stack overflow and underflow errors: The stack overflow and stack underflow errors can be traced using the stack characteristic of the Remix IDE debugging process which gives the information about the stack.

Debug Ethereum Smart Contracts

When a transaction is deployed in Remix IDE, the below steps are followed to debug a transaction:

  1. When a transaction is deployed, it gets submitted to the Ethereum blockchain using remix. 
  2. There is a debugger button present in the remix IDE. 
  3. Press the debugger button which identifies the reasons for the transaction failures. 
  4. We have to provide the block number and transaction hash and press the debugger button to start debugging.

Below is the Solidity code to implement the above approach to debug a Solidity smart contract in Remix IDE:

Solidity




// Solidity code to implement the above 
// approach to debug a Solidity 
// smart contract in Remix IDE
pragma solidity ^0.5.0;
  
contract Comparison {
  uint a;
  uint b;
  function co (uint a, 
               uint b) public pure returns (uint c) 
  {
    uint c = a + b ;
    return (c);
  }
}


Step 1: Write the code in the remix IDE and compile it using the compile button.

Compile the Smart Contract

 

Step 2: After the compilation, deploy the transaction using the deploy button.

Deploy the transaction

 

Step 3: After the successful deployment the debug button will appear on the right-hand side corner.

Debug the transaction

 

Step 4: Expand the line on which debug button is present, you can view the whole transaction details.

Debug the transaction

 

Step 5: Press the debug button to start the debugging process.

Debug the transaction

 

Debugging Tools

1. Hardhat

Hardhat is an open-source tool and built-in developer for developing, deploying, and testing smart contracts. It is a tool based on JavaScript to develop Ethereum-based applications. It provides developers with a huge range of functions to make smart contract development easy.

2. Truffle

Truffle is a framework for Ethereum smart contracts. Truffle tools help with developing, deploying, and testing these contracts. It contains a Truffle debugger for debugging smart contracts.

3. Remix

Remix is an editor for Solidity which contains an in-built debugger for debugging smart contracts. Remix is IDE used for developing and deploying smart contracts easily using Solidity programming language. It can test, deploy and debug smart contracts.

4. EtherScan

EtherScan is a blockchain explorer for Ethereum used to inspect and debug the smart contracts deployed on Ethereum. It is used to visualize data in the blockchain. It tracks the transactions and smart contracts in the blockchain.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads