question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Throws during pure calls to library functions

See original GitHub issue

Hello sc team,

Recently we have attempted to upgrade to newer version of solidity as well as solidity-coverage and found some issues that took a very long time to track down.

I have created a very minimal example to show the issue which can be found here.

Essentially, a .call() to a pure library function is throwing Error: VM Exception while processing transaction: revert in the coverage environment.

If you take a look at the repo, the instructions to reproduce are there.

According to the docs of solidity 0.4.23 this should be possible and has worked in the past

Library functions can only be called directly (i.e. without the use of DELEGATECALL) if they do not modify the state (i.e. if they are view or pure functions), because libraries are assumed to be stateless

please let me know what other information I can help provide to get this resolved. I think the repo should help bring out the issue pretty clearly.

Thanks for all you guys do!

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:3
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
cgeweckecommented, May 22, 2018

Hi @pelsasser. Thanks so much for opening. Quite sorry because this issue is known and should have been documented. It was discovered by Zeppelin about a month ago (more detail in this thread)

The underlying issue is that solidity-coverage rewrites the contract ABIs of pure/view functions in order to sneak event-based instrumentation into the contracts during compilation. It then swaps the original ABIs back into the contract artifacts to get truffle to call the functions correctly. (e.g. as calls, not transactions).

Since 0.4.20, in response to Parity’s wallet being deleted, solc has added protections (at the bytecode level) that prevent non-view library methods from being called directly. TLDR, the pure library functions are being compiled as non-view functions and this triggers the error.

A work-around is to call these library methods through a mock contract that uses the library.

  • an example at Zeppelin here
  • mocks must be located in the contracts directory (not test). You can put them in a mocks folder.
  • mock contracts themselves can be excluded from coverage using the skipFiles option in .solcover.js, e.g
    skipFiles: [
          'lifecycle/Migrations.sol', // <-- single file
          'mocks' // <-- whole folder
      ]
    
  • if you are publishing your contracts to npm you’ll probably want to .npmignore your mocks folder
0reactions
cgeweckecommented, Dec 31, 2019

Fixed with 0.7.0

Read more comments on GitHub >

github_iconTop Results From Across the Web

C++ pure virtual function call doesn't throw run-time exception?
The issue with calling a virtual method in a constructor has to do with a base class constructor calling a derived class method,...
Read more >
A Guide to @Throws in Kotlin - Baeldung
Learn how the @Throws annotation facilitates calling Kotlin methods and functions from Java.
Read more >
Solidity - View and Pure Functions - GeeksforGeeks
The view functions are read-only function, which ensures that state variables cannot be modified after calling them.
Read more >
Can a pure function throw exceptions? : r/scala - Reddit
Now, I'm glossing over a few things here and Jakub's totally right that throwing an exception isn't pure in Scala generally speaking.
Read more >
Contracts — Solidity 0.8.17 documentation
Functions outside of a contract, also called “free functions”, always have implicit internal visibility. Their code is included in all contracts that call...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found