0

I'm going through the Numerai contracts and noticing a common pattern - https://github.com/numerai/contract/blob/master/contracts/NumeraireBackend.sol

and https://gist.github.com/Arachnid/4ca9da48d51e23e5cfe0f0e14dd6318f

function stakeOnBehalf(address _staker, uint256 _value, bytes32 _tag, uint256 _tournamentID, uint256 _roundID, uint256 _confidence) stopInEmergency onlyPayloadSize(6) returns (bool ok) {
    return delegateContract.delegatecall(bytes4(sha3("stakeOnBehalf(address,uint256,bytes32,uint256,uint256,uint256)")), _staker, _value, _tag, _tournamentID, _roundID, _confidence);
}

I see the delegatecall with the bytes4 and get the sha3 hash of a string before the variables of the function. I understand what sha3 does in this context, but my question is why?

Is it for security? What is the purpose of the string before the variables as well?

1 Answer 1

2

That string is called the function selector. It is calculated as the first four bytes of the sha3 hash of the signature of the function. The signature is defined as the canonical expression of the basic prototype, i.e. the function name with the parenthesised list of parameter types. Parameter types are split by a single comma - no spaces are used (as you can also observe in the code snippet you provided).

Function selectors are used to tell Solidity and EVM which functions you actually want to (delegate) call.

See more info here. You might also find this helpful.

3
  • Thank you. So to clarify, the EVM will not be able to find the delegatecall function unless I add the function selector? Commented Oct 5, 2017 at 14:03
  • 1
    Exactly! That's the reason why you have to provide it ;) Commented Oct 5, 2017 at 15:04
  • Thanks again. Having an issue after writing a sample contract to test this. Maybe you can spot the issue I'm having - ethereum.stackexchange.com/questions/27860/… Commented Oct 5, 2017 at 17:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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