#37139 [SC-Insight] insight inefficient use of storage reentrancy locks
Was this helpful?
Was this helpful?
Submitted on Nov 26th 2024 at 18:18:50 UTC by @Blockian for
Report ID: #37139
Report Type: Smart Contract
Report severity: Insight
Target: https://github.com/Hydrogen-Labs/fluid-protocol/tree/main/contracts/sorted-troves-contract/src/main.sw
Impacts:
Contract fails to deliver promised returns, but doesn't lose value
Gas Optimization Insight
Many contracts within the Fluid protocol are using storage-based reentrancy locks. This practice appears to be inherited from the Liquity codebase, which is written for the Ethereum codebase. However, in the Fuel ecosystem, there is a more efficient alternative for managing reentrancy using Sway Libs, as detailed in the .
Additionally, in the Fuel ecosystem, token transfers to an address do not invoke a function on the recipient, even if the recipient is a contract. This behavior contrasts with Ethereum's token transfer mechanics, where a transfer can trigger a contract call. As a result, reentrancy locks are often unnecessary in Fuel-based contracts.
Example: The stake
function in the fpt-staking-contract
employs a lock_stake
mechanism. However, since this function never transfers execution to the user, reentrancy is not a concern.
The unnecessary use of reentrancy locks increases gas consumption for users without providing any meaningful benefit, making the protocol less efficient.
To address this inefficiency:
Remove reentrancy locks entirely where they are unnecessary.
Adopt the Sway Libs reentrancy_guard
, a more gas efficient alternative for preventing reentrancy.
Below is an example demonstrating how to implement the reentrancy_guard
from Sway Libs effectively:
Switching to the Sway Libs reentrancy_guard
or eliminating unnecessary locks entirely will:
Reduce gas costs for users.
Improve the protocol's overall efficiency.
Align with best practices in the Fuel ecosystem.
This change is a straightforward yet impactful optimization that enhances both developer and user experience.