Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

simplify functionSignatureRegex #578

Merged
merged 1 commit into from
Mar 17, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion precompile/contract/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const (
ReadGasCostPerSlot = 5_000
)

var functionSignatureRegex = regexp.MustCompile(`[\w]+\(((([\w]+)?)|((([\w]+),)+([\w]+)))\)`)
var functionSignatureRegex = regexp.MustCompile(`\w+\((\w*|(\w+,)+\w+)\)`)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fun with ChatGPT:

In Solidity, a function signature is the first 4 bytes of the Keccak-256 hash of the function's canonical signature. The canonical signature includes the function name and parameter types enclosed in parentheses, separated by commas.

Here's a regular expression that matches a Solidity function signature:

ruby
Copy code
/^[a-zA-Z_]\w*\((\w+(,\s*\w+)*)?\)$/
This regular expression matches a string that starts with a letter or underscore, followed by zero or more letters, underscores, or digits, representing the function name. The function name is followed by an opening parenthesis, zero or more parameter types separated by commas and optional spaces, and a closing parenthesis.

For example, this regular expression matches function signatures like:

scss
Copy code
myFunction()
myFunction(uint256,string)
_transfer(address,address,uint256)
Note that this regular expression does not include the return types, as they are not part of the function signature in Solidity.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

resim

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We still have time before ChatGPT-5 😅


// CalculateFunctionSelector returns the 4 byte function selector that results from [functionSignature]
// Ex. the function setBalance(addr address, balance uint256) should be passed in as the string:
Expand Down