Get the scoop on finance - sign up for mobile alerts
HodlX
| On
May 26, 2019

Best Practices for Developers and Programmers to Debug Ethereum Smart Contracts

By Ved Raj
HodlX Guest Post  Submit Your Post
 

Ensuring the security of any smart contracts transaction is important because a single bug in your programmer’s code can cost you thousands or millions of dollars if it is exploited by a hacker. Though a blockchain developer or an expert will tell you that blockchains are secure, it’s a fact that the exploiters of technology grow as rapidly as technology itself. Let’s discuss in detail how Solidity developers can debug smart contracts transactions.

Although bug attacks in programming codes are becoming common, Ethereum’s DAO is a famous victim of a recent bug attack. The DAO or Decentralized Autonomous Organization codify the rules of an organization and create a structure with decentralized control by eliminating the need for keeping documents and personnel in governing.

ADVERTISEMENT

Although blockchain smart contracts seem to be a solid and transparent system to control fraud in digital contracts, in reality, hackers have stolen nearly $2 billion worth of cryptocurrency since 2017 using a technique known as a “51% attack.”

On January 5, 2019, Ethereum Classic was hacked through the same 51% attack technique which led to an ETC trading freeze.

PoW 51% Attack Cost

ADVERTISEMENT

People in the business industry are worried. So, I have gathered here details of errors in the Ethereum smart contracts and how Solidity developers can debug them.

Types of Errors in Ethereum Smart Contracts

Syntax Error

The syntax error occurs when there is a problem in the syntax of Ethereum smart contracts coding. When there is a syntax error in your smart contracts, it can’t be compiled or deployed onto the blockchain.

The best way for diagnosing a syntax error is by using Remix, Truffle or IDEs. When the semicolon(;) is removed from the code in remix IDE, it displays the error message with the solution for the syntax error.

ADVERTISEMENT

Runtime Error

Runtime error in Ethereum smart contracts occurs when Solidity developers deploy a smart contract to the blockchain and when you’ve compiled your Solidity code to bytecode which has been understood by the Ethereum Virtual Machine.

EVM is a component of Ethereum that is used to run the Solidity code, and when the runtime error occurs, EVM will tell you that you are doing something wrong with the logic of your smart contracts coding.

Runtime errors in Ethereum smart contracts are more difficult to identify for blockchain developers than syntax errors because these errors cannot be identified before deploying on the blockchain. These errors only arise when any kind of state change takes place in your smart contracts coding.

Some of the Common Types of Runtime Errors Faced by Solidity Developers

Revert – Revert error in a transaction will arise when a Solidity developer tries to execute a transaction that cannot be executed as per the logic of the smart contracts, and EVM will revert an error.

Invalid Opcode – Invalid opcode error arises when you try to call a code that doesn’t exist in the system.

Invalid Jump – Invalid jump error arises when you try to call a function that doesn’t exist or when you point to wrong memory.

Stack Overflow – Stack overflow error arises when you call a function recursively and you put no condition to stop it. A function can call itself only 1024x times, but if you exceed this limit the stack overflow error will occur.

Stack Underflow – Stack underflow error arises in assembly language when you try to put a variable that doesn’t exist in the system.

Logic Errors

The best example of logic errors is reentrancy from the famous DAO attack where the developer made a mistake and transferred Ether before updating the balance of the user. Logic errors cannot be captured by EVM, and code can be run easily. These logic errors occur when a developer makes a mistake and opens all the coding loopholes in smart contracts for hackers.

To diagnose the logical errors you can use Audit for Ethereum smart contracts because Auditor can run a smart contract with the intent to find all the errors present in the coding and security flaws in the smart contracts.

Example of Ethereum Smart Contracts Coding for Debugging All Types of 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);

}
}

Steps to Debug a Transaction using Remix IDE

Step 1

Once the transaction is failed, press the “Debugger” button given in the remix IDE to find out the possible reason for the transaction failure.

Step 2

To start with the debugging process, enter block number, transaction hash, and finally press the “Debugging” button to start the process of debugging.

Step 3

Once the debugging process starts the assembly language operations along with the gas are displayed. Also, the code is highlighted in the remix IDE.

Address public amountOwner ;
constructor(address _owner) public payable {

require(_owner != address(0));
require(msg.value > 0.1 ether);
amountInContract = msg.value;
amountOwner =_owner;

}

As shown in the coding above, a transaction is reverted back in the ether scan and the value sent is 0.1 ether. The value entered is less than the required value; that’s why the transaction is failed in this scenario.

This entire process can be used by Solidity developers to debug Ethereum smart contracts. Debugging can be done using Truffle and by using the transaction hash.


Ved Raj is a senior IT consultant at ValueCoders, an offshore IT outsourcing company based in India. He has worked in the IT outsourcing industry for 10+ years. His current job usually involves working with clients on their blockchain development needs and helping them to build decentralized products such as Ethereum wallets, smart contracts and ICOs.

 
Check Latest Headlines on HodlX


Follow Us on Twitter Facebook Telegram

Check out the Latest Industry Announcements
 

Disclaimer: Opinions expressed at The Daily Hodl are not investment advice. Investors should do their due diligence before making any high-risk investments in Bitcoin, cryptocurrency or digital assets. Please be advised that your transfers and trades are at your own risk, and any loses you may incur are your responsibility. The Daily Hodl does not recommend the buying or selling of any cryptocurrencies or digital assets, nor is The Daily Hodl an investment advisor. Please note that The Daily Hodl participates in affiliate marketing.