TangibleTNFT / ustb

MIT License
0 stars 1 forks source link

[NLA-01C] Inefficient `mapping` Lookups #12

Open dkuppitz opened 9 months ago

dkuppitz commented 9 months ago

NLA-01C: Inefficient mapping Lookups

Type Severity Location
Gas Optimization NonblockingLzAppUpgradeable.sol:L172, L176

Description:

The linked statements perform key-based lookup operations on mapping declarations from storage multiple times for the same key redundantly.

Example:

bytes32 payloadHash = $.failedMessages[srcChainId][srcAddress][nonce];
require(payloadHash != bytes32(0), "NonblockingLzApp: no stored message");
require(keccak256(payload) == payloadHash, "NonblockingLzApp: invalid payload");
// clear the stored message
$.failedMessages[srcChainId][srcAddress][nonce] = bytes32(0);

Recommendation:

As the lookups internally perform an expensive keccak256 operation, we advise the lookups to be cached wherever possible to a single local declaration that either holds the value of the mapping in case of primitive types or holds a storage pointer to the struct contained.

dkuppitz commented 9 months ago

Resolved in 9d292c1c70e73f3f8dd104698adf1742c67cf3ef.

dkuppitz commented 9 months ago

Post-audit recommendation implemented in 66bcf240a7dd86fe947bc0aa222d6f5b34439864.