Open franck44 opened 5 days ago
LGTM, thanks @franck44.
What's the process for "approving" issues? I could implement this as a PR but want to avoid duplicating other people's work and PR race conditions so to speak, as well.
I created and tested #852 to address this, tagged you for review @franck44
Problem
Using
require
to validate inputsIn our (bridge) Solidity contracts, we are using the pattern
if (!cond) revert Error
to validate inputs. This is not the best practice and there were several instances of bugs in the past where the contract hadif !C someCode; revert()
, ommitting the{}
around theif-body
, which can have unintended consequences. You can read more about the risks (and a bug in APPLE-SSL) here.Using
require(cond, error)
has several advantages:require
clearly identify the conditioncond
that should be checked rather than the negation of the condition that appears in the equivalentif-revert
pattern;require(cond, error)
is automatically translated (by the Solidity compiler) inif (!cond) revert error
thereby enforcing that the patternif-revert
is used correctly.CustomError
to minimise gas costThere is a caveat though: until recently,
require(cond, error)
only supportederror
of type int or strings, and these two types are more expensive gas-wise than custom errors. This is why developers would deliberately useif (!C) revert CustomError
(instead of therequire
) in the code to optimise gas costs, while compromising security.As of version 0.8.27 (release notes) , the Solidity compiler supports
require(conditional, customError)
.Proposed changes
There are two Solidity contracts that are currently using the unsafe and hard-to-read
if-revert
pattern to validate inputs:The proposal is to use
require
for input validation.For example, the validation of inputs
amount > 0
in the functioninitiateBridgeTransfer
:would be re-written into (note that the
revert
related to the transfer of Move tokens is not an input validation, but a more complex error and best practice is to use anif ... revert
in that case):Implementation
To implement the changes we have to:
require
Validation
Here are the steps to validate the changes: