0

Ethereum smart contract functions are limited to around 16 local variables. When they exceed that number, stack is too deep error is generated.

The solution is to refactor the large functions into smaller ones. For example:

Contract A{
   function B(p1,p2,p3...pn){
   }
}

Can be developed as:

Contract A{
   function B1(p1,p2){
   }
   function B2(p3,p4){
   }
   ...

}

Now assume that some functions are dependent on the results of others. In other words, the change in state caused by a function affects how another one acts.

This issue I am thinking of is unpredictable state discussed in this paper which states:

In general, when a user sends a transaction to the network in order to invoke some contract, he cannot be sure that the transaction will be run in the same state the contract was at the time of sending that transaction.

How can we ensure that a user that wants to transact with function B1 then wants to transact with function B2 without someone else changing the state caused by transacting with B1 again?

1 Answer 1

0

If some of the variables are temporary and can be contained inside their functions, you may be able to call B1() from inside B2(), which gets around the stack depth limitation while still guaranteeing execution order.

If you have to split it into two calls, and it matters in which order they are called you may need to manage the dependency yourself. For example, write a variable in B1() and check it in B2(), calling revert if it hasn't been written.

2
  • Can you clarify what A1() is? Jul 4, 2018 at 0:55
  • Sorry, I meant B1 and B2, ie the 2 functions in the example. Edited. Jul 4, 2018 at 0:59

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.