0

The method to execute a function on a contract in Ethereum is to use the JSON RPC protocol to make a request which has the details of which function you want to call.

https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_sendtransaction

When it's eg. a function called get() what will happen is:

  1. Take the sha3 hash: sha3("get()")
  2. Get the first 4 bytes: sha3("get()")[0:4] == 0x6d4ce63c
  3. Add this function "selector" in the data field of your eth_sendTransaction call.

I am wondering what happens if you try to compile and deploy a contract with conflicting function selectors.

When 1 contract has 2 functions with the same selector:
a. There will be an error during the contract compilation
b. There will be an error during the contract deploy (but it will compile)
c. No errors but when a function is called you cannot know which function will be triggered.

Does anyone know?

0

For a contract to have two or more functions with the same selector is necessary that the functions have the same name and the same number and type of parameters. This is prevented by the compiler.

For instance you can have to functions get() and get(uint256 value) these will not collide because they use different number of parameters. So the scenario that you present will not be possible.

Hope this helps

EDIT:

Furthermore, the probability of colliding with four bytes is still ridiculously low. Unless you have more than 65535 functions (this is not even half of the possible values with four bytes) in your contract you won't be seeing collision happening and when they happen the compiler will still tell you.

  • They can have a different name and params and still collide since it's only the first 4 bytes of the hash which makes the "selector". Just so you know... – mesqueeb Jun 13 '18 at 12:09
  • Thanks for letting me know. Yet it won't happen, the compiler will detect the collision. Furthermore, the probability of colliding with four bytes is still ridiculously low unless you have more than 65535 functions in your contract you won't be seeing collision happening and when they happen the compiler will still tell you. – Jaime Jun 13 '18 at 12:21

Not the answer you're looking for? Browse other questions tagged or ask your own question.