1

In Solidity I can call an internal function (inside the same contract that is calling it) the "usual" way:

myFunc();

or I can call it via .call, as discussed here or here:

callData = bytes4(sha3("myFunc()"));
this.call(callData);

(similarly also with appending parameters to callData).

The drawback of the second option is that it costs significantly much more gas (CALL is much more expensive than JUMP) and requires myFunc to be an externally available function - which could be fixed with an onlySelf modifier as suggested before.

The advantage of the second option is, that it allows for abstraction: A function (and it's parameters) could be provided dynamically and only be known at run-time. This would allow features similar to function pointers. Is there a way (potentially via inline assembly?) to call an internal function with the callData bytes instead of the explicit function name and parameters that have to be known at compile time without using the expensive .call?

  • I'm not sure I see your use case. The code of a contract is immutable, so it doesn't seem particularly useful to have jumps to arbitrary locations – Tjaden Hess May 1 '17 at 17:09
  • I'm thinking about a voting library. One could vote on some function to be executed or not. This would allow me to write a base contract/library which then calls a function of the derived contract which is not known by the base contract. Thus the base voting contract can be used for totally different polls and corresponding functions to be triggered. – Validity Labs - Sebastian May 1 '17 at 19:28
  • Did you ever find a way to do this? – mudge Jun 21 '18 at 22:35
  • No unfortunately not... – Validity Labs - Sebastian Jun 27 '18 at 12:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.