Background

Gas Optimization Audit: Everything You Need To Know

Cyberscope Team
December 28, 2023
Gas Optimization Audit: Everything You Need To Know

Introduction

In the world of smart contracts and blockchain, gas optimization is the key to unlocking efficiency and cost-effectiveness. Imagine it as the secret sauce that makes your transactions run smoother, faster, and without breaking the bank. Let's take a journey into the realm of gas optimization without getting lost in the complexities.

Decoding Gas

On EVM-compatible networks, “gas” refers to the unit that measures the computational effort required to execute specific operations. It’s the fuel that powers your smart contracts on the blockchain. It's the energy required for every operation, and you pay for it in the native blockchain’s token (like Ether, BTC, BNB, etc). Each transaction requires computational resources to execute, so fees are charged to keep the wheels turning but also ensure the security of the network. These fees are commonly referred to as ‘gas fees’.

The following diagram illustrates the layout of the Ethereum Virtual Machine. Going over it, we can see that the gas consumption is divided into three parts: operation execution, external message calls, and reading and writing from memory and storage.

Ethereum Virtual Machine Diagram
Ethereum Virtual Machine Diagram

When you compile a smart contract in Solidity, it transforms into a sequence of "operation codes," commonly known as opcodes. Each segment of opcode, such as creating contracts, initiating message calls, accessing account storage, and performing operations on the virtual machine, carries an agreed-upon cost in gas. These costs are standardized and documented in the Ethereum yellow paper.

Ethereum yellow paper
Ethereum yellow paper

(After several EIPs, some of these gas costs have been changed and might have deviated from the yellow paper. For the latest information regarding costs associated with each opcode, check here.)

Optimizing gas consumption isn't a stroll in the park. It's like trying to predict the stock market – a challenging feat. Developers need an in-depth understanding of the Ethereum Virtual Machine (EVM) and must navigate through the ever-changing landscape of gas prices. It's a delicate balancing act, to say the least.

Identifying the Culprits Behind High Transaction Gas

Imagine this scenario: you receive your monthly utility bill, and it's through the roof. You scratch your head, wondering what could have caused such a spike. In the world of smart contracts, high gas bills can similarly leave developers puzzled. Let's dive into the factors that might be behind those unexpectedly high transaction costs.

Complex Logic Overload: Complex logic, involving multiple conditional statements and intricate operations, can significantly increase the computational effort needed, translating to higher gas consumption. Simplifying and streamlining your code can be the equivalent of adopting a more efficient cooking process.

Inefficient Algorithms: Algorithms are the recipes for your smart contracts. Using inefficient algorithms is like choosing a complex recipe with unnecessary steps. It might get the job done – but at a higher cost. Opting for streamlined algorithms reduces the computational load, saving gas in the process. It's like opting for a simpler, yet equally delicious, dish.

Data Overload: Imagine trying to carry too many groceries at once – it's cumbersome and may cost you more energy. Similarly, if your smart contract deals with an excessive amount of data in a single transaction, it can lead to higher gas consumption. Minimizing the amount of data processed in one go, perhaps by storing some offline and only accessing essential information, can significantly reduce gas costs.

Excessive Storage Operations: Storage operations are like renting space in the blockchain's storage unit. However, not all storage operations are created equal. Reading and writing state variables stored in contract storage is particularly expensive in terms of gas usage. If your smart contract frequently engages in these costly operations, it could majorly contribute to high gas fees. Optimal storage usage strategies, such as minimizing storage modifications and utilizing memory for non-permanent data, can be the key to reducing these costs.

External Function Calls: Sometimes your smart contract needs to make a call to an external function, much like reaching out to a specialist for a specific task. However, these external function calls come at a cost, often higher than internal operations. If your contract heavily relies on external function calls, it could be a significant factor behind elevated gas expenses. Evaluating the necessity and frequency of these calls and optimizing where possible can be a strategic move.

By identifying these culprits, developers can take a targeted approach to gas optimization. It's like detective work for your smart contracts, ensuring they operate efficiently without unnecessary financial burdens. Just as you'd investigate a sudden spike in your utility bill, scrutinizing these factors can unveil the mysteries behind high gas costs in your smart contracts.

Gas Optimization Tips
Gas Optimization Tips

Proven Tips and Tricks for Gas Optimization

Alright, let's talk about turning your smart contract into a lean, mean, gas-saving machine. By following the below practices, developers can reduce the gas consumption of their smart contracts, lower transaction costs, and create more efficient and user-friendly applications.

Minimize Storage Usage

In Solidity, ‘Storage’, a finite and expensive resource, warrants careful consideration. ‘Memory’ proves more economical for non-permanent data, reducing significant gas costs incurred during storage operations.

As detailed in the Ethereum yellow paper, storage operations surpass memory operations in cost by over 100 times. For instance, opcodes like mload and mstore incur a mere 3 gas units, whereas storage operations like sload and sstore demand at least 100 units, even under the most optimistic scenarios.

SLOAD Operation Gas Cost
SLOAD Operation Gas Cost

Some strategies to limit storage use include:

  • Storing non-permanent data in Memory.
  • Reducing storage modifications by saving intermediate results in Memory and assigning them to Storage variables only after completing all calculations.

Variable Packing

The utilization of storage slots and the representation of data within a smart contract significantly impact gas usage.

The Solidity compiler, during the compilation process, packs contiguous storage variables, using a 32-byte slot as the base unit. Variable packing involves arranging variables to fit multiple ones into a single slot.

A simple adjustment in implementation can result in substantial gas savings — developers can conserve 20,000 units of gas by requiring only two slots for storage (right column), compared to the inefficient three slots previously consumed (left column):

Variable Packing Example
Variable Packing Example

Save on Data Types

An inappropriate choice of data types can substantially impact gas usage. Different data types have varying operational costs, emphasizing the importance of opting for the most appropriate type.

Solidity allows breaking down integers into different sizes, such as uint8, uint16, and uint32. Opting for the suitable type aids in minimizing gas consumption.

Integer Size Breakdown Example
Integer Size Breakdown Example

A comparative analysis using code showcases the gas cost difference between uint8 and uint256. The UseUint() function costs 120,382 gas units, while UseUInt8() costs 166,111 gas units. While using uint256 appears cheaper in isolation, when considering variable packing, uint8 can be more efficient. Packing four uint8 variables in one slot can outperform the cost of four uint256 variables, ultimately optimizing gas usage.

Use Fixed-Size Variables Instead of Dynamic

The choice between fixed-size and variable-size variables has cost implications. Using the bytes32 datatype over bytes or strings is recommended if the data can fit within 32 bytes. In general, fixed-size variables prove less expensive than their variable-size counterparts. Opting for the lowest possible bytes size, such as bytes1 to bytes32, is advised.

Fixed Variables Example
Fixed Variables Example

Mapping vs Array

Solidity offers two data types, arrays, and mappings, for representing lists of data. Although syntax and structure differ, mappings tend to be more efficient and less expensive. Arrays, being iterable and packable, have their merits, especially when iteration is necessary or when data types can be packed.

Use Calldata Instead of Memory

Function parameters, declared as variables, are stored either in calldata or memory. A crucial distinction lies in the mutability of memory, which can be modified by the function, whereas calldata remains immutable.

The principle here is to leverage calldata for read-only function arguments, thus bypassing unnecessary copies from function calldata to memory. This adjustment significantly improves gas efficiency.

Memory Keyword Example
Memory Keyword Example

In this example, which makes use of the memory keyword, the array values are kept in encoded calldata and are copied to memory during ABI decoding. The execution cost is 3,694 gas units for this code block.

Calldata Usage Example
Calldata Usage Example

In the second example, however, the value is directly read from calldata and there are no intermediate memory operations. This adjustment results in an execution cost of only 2,413 gas units for this code block, marking a 35% improvement in gas efficiency.

Use Constant/Immutable Keywords Whenever Possible

Constant/Immutable variables, evaluated at compile-time and stored in the bytecode of the contract, offer a cost-effective alternative to storage. Their usage is recommended wherever possible to reduce gas consumption.

Use Unchecked When Under/Overflow is Impossible

The ‘unchecked’ keyword, introduced in Solidity v0.8.0, proves beneficial when developers are certain that an arithmetic operation will not result in overflow or underflow.

Unchecked Keyword Usage
Unchecked Keyword Usage

The above example, the variable ‘i’ can never overflow due to the conditional constraint ‘i < length’. Here, ‘length’ is defined as uint256, meaning the maximum value ‘i’ can reach is ‘max(uint) - 1’. This example illustrates the use of unchecked in a scenario where arithmetic underflow checks are redundant, offering both safety and gas efficiency.

Notably, with Solidity v0.8.0 and above, the SafeMath library is rendered obsolete, as overflow and underflow protection are now integral to the compiler itself.

Modifier Optimization

Modifiers, essential for enhancing code readability and reusability, can contribute to increased bytecode size and gas usage when not optimized. Let’s see an example of how you can optimize modifier gas cost:

Before:

Modifier Example Before
Modifier Example Before

After:

Modifier Example After
Modifier Example After

This example demonstrates an optimization technique, involving the refactoring of the internal function ‘_checkOwner()’, allowing for its reuse in modifiers. This optimization minimizes bytecode size and gas costs.

Short-Circuit

The evaluation of logical expressions involving ‘||’ and ‘&&’ operators is short-circuited, implying that the second condition is not evaluated if the first one already determines the result.

To optimize for lower gas usage, arranging conditions so that the less expensive computation comes first can potentially bypass the execution of more expensive computations.

Remove Unused Code

Removing unused code emerges as a straightforward strategy to reduce contract deployment costs and maintain a compact contract size.

Some practical recommendations include:

  • Consider employing the most efficient algorithms for computations, eliminating redundant calculations where possible.
  • In Ethereum, developers are incentivized with a gas refund for freeing up storage space. Removing unnecessary variables using the 'delete' keyword or setting them to their default values can be a prudent approach.
  • Loop optimization strategies, such as avoiding expensive loop operations, combining loops when feasible, and moving repeated computations out of loops, contribute to enhanced gas efficiency.
Why Gas Optimization Matters
Why Gas Optimization Matters

Why Gas Optimization Matters

Alright, so gas optimization isn't just for the tech wizards. It's like making your favorite recipe with fewer ingredients but keeping all the flavor. Let's break it down:

Cost-Effective Contracts: Optimizing gas is like finding a shortcut in a game – you achieve the same result with fewer resources. For smart contracts, it means cutting costs. Just imagine if you could do your grocery shopping and spend less money – that's the goal here.

Speedy Transactions: Think of gas optimization as the express lane at the supermarket. Your transactions move faster, and you don't have to wait in line. It's like getting through traffic without any red lights – smooth and quick.

Reliable Performance: Efficient gas use is like having a reliable car that never breaks down. Your smart contracts become trustworthy, doing what they're supposed to without hiccups. It's like driving a car that always starts on the first try.

Test Before You Go Live

Okay, so you've fine-tuned your smart contract for optimal gas use. Now, before you release it into the wild blockchain, let's take it for a spin.

Gas Analyzers as Co-Pilots: Gas analyzers are tools designed to scrutinize and assess the gas consumption of smart contracts on blockchain networks, particularly on platforms like Ethereum. These analyzers delve into the intricacies of a smart contract's code to identify specific areas that consume a significant amount of gas during execution. These gas analyzers act like co-pilots, navigating your code and highlighting areas that need attention. It's like having a second set of eyes to ensure your contract is ready for the blockchain journey.

Smooth Ride to Deployment: Testing your smart contract is like taking your car for a test drive before a long road trip. It's about ensuring everything runs smoothly and there are no surprises. Gas analyzers help you catch any glitches or potential issues before deployment.

Optimize and Secure: Testing isn't just about performance; it's also about security. Getting a gas optimization audit can help you optimize for efficiency while ensuring your contract is robust and secure. It's like checking the brakes and engine before hitting the highway. Choosing an experienced company that specializes in this, is crucial since it can help you fulfill your project’s potential.

Conclusion

Gas optimization is the unsung hero of impeccable smart contracts. It's not just about cutting costs; it's about making your contracts faster, smarter, and more reliable. So, whether you're a blockchain enthusiast or a seasoned developer, ensure your smart contracts are finely tuned and ready for the blockchain race. As an experienced cybersecurity firm, Cyberscope’s expertise is the ally you need for your smart contract’s gas optimization audit.

In this journey, a gas optimization audit is your ally, ensuring contracts operate efficiently and economically. So, as you navigate Solidity, consider the audit as a guide, leading to contracts that function seamlessly and efficiently.

Tags :
2023,
AUDIT
Share :
Background

Subscribe To Our Newsletter

Stay updated with the latest hacks, threats, security best practices, and educational content in the crypto world right in your inbox!