0

I've got a function with a decent number of modifiers and I hit the CompilerError: Stack too deep, try removing local variables error. If I remove one of the modifiers though, everything's all right.

It seems that modifiers get bundled into the same stack region as the function, thus increasing the size and throwing the error above. Is that a true statement? If yes, what are the best practices for handling large modifiers? That is, I don't want to create a function as a replacement, because I want to use require.

The full contract can be found here, but here's a snippet with the tricky function:

function update(
  uint256 _streamId,
  uint256 _stopBlock,
  uint256 _payment,
  uint256 _interval
)
  public
  payable
  streamExists(_streamId)
  onlyInvolvedParties(_streamId)
  onlyNewTerms(_streamId, _stopBlock, _payment, _interval)
  validTerms(block.number, _stopBlock, _payment, _interval)
{
  shallowStackUpdate(_streamId, _stopBlock, _payment, _interval);
}

function shallowStackUpdate(
  uint256 _streamId,
  uint256 _stopBlock,
  uint256 _payment,
  uint256 _interval
)
  private
{
  Stream memory stream = streams[_streamId];
  stop(_streamId);
  create(stream.sender, stream.recipient, block.number, _stopBlock, _payment, _interval);
  emit LogUpdate(streamNonce, stream.sender, stream.recipient, block.number, _stopBlock, _payment, _interval);
}

I had to write the shallowStackUpdate function to successfully compile the contract. Ideally, the two would merge into only one function.

Note: I searched SE and my situation is NOT the same with the one in the How to Fix “Stack Too Deep” Error? thread.

4
  • Can you please share your code? Nov 11 '18 at 19:03
  • @goodvibration Sure, I updated the question body. Nov 11 '18 at 19:27
  • Does any of these modifiers contain a function call? If yes, then this stack overflow is easily explainable. When you call a function from a function on the same contract, it adds to the stack depth (as many as the number of local variables and input arguments in that function). Nov 11 '18 at 19:54
  • No, they're not calling any function, but they are relatively bulky modifiers. Nov 12 '18 at 12:28