How to not embed a library?

Library Embedding issue.

:computer: Environment
Truffle, Solidity version: “0.5.16”

:memo:Details
I am using the library contract in our base contract. I thought I understood the difference between embedding a library and launching and linking it differently. One public function should cause the library not to embed (Ive read that a lot, to my experience one isn’t always enough).

Anyways we have one library that is just for storage which is only structs. I notice that we don’t link it, which I have to assume means it is embedded. We are close to block size limit and launching it separately would save a lot of space.

I tried adding a public function and even calling it from the base contract. Still isn’t throwing the normal errors when I switch a library away from embedding (which is something along the lines of hey link this library)

:1234: Code to reproduce
Hard to give full code reproduction but I made a gist

1 Like

Hi @JesseAbram,

Welcome to the community forum :wave:

Looking at the Solidity documentation on Libraries:
https://solidity.readthedocs.io/en/v0.5.15/contracts.html#libraries

There is an example library Set:

The calls to Set.contains, Set.insert and Set.remove are all compiled as calls (DELEGATECALL) to an external contract/library. If you use libraries, be aware that an actual external function call is performed.

There is an example library BigInt:

shows how to use types stored in memory and internal functions in libraries in order to implement custom types without the overhead of external function calls

My understanding is that you would need to interact with your storage library using public functions for it to be external, as per the library Set example in the Solidity documentation (though there is the added gas cost of external calls).


I deployed your example and the library wasn’t required to be deployed separately. (I verified the contract)
https://rinkeby.etherscan.io/tx/0xfd5c58afa248bbb64a970e487dba364cfc870a623536af5ee64b7fbeb95f5c89

I added a call to the library public function and the library was deployed separately. (I verified the library and the contract)
https://rinkeby.etherscan.io/tx/0xea5edfc821c0b0902f36ab117b2a3288b7c609ce8f82c7a6e469c50bcc7eb171
https://rinkeby.etherscan.io/tx/0x3de97c30bc9ced8f890d06e59d625562a8703b5b494625e9cf2a03a55a9c0bc6

Storage.sol

pragma solidity ^0.5.0;


library Storage {
    struct Structing {
        uint256 this;
        mapping(uint256 => bool) that;
    } // this does not require linking and is therefore embedding in contract demo?

    function bePublic() public {} // with this function library should not embed, yet it does not seem to work
}

demo.sol

Includes call to library public function

pragma solidity ^0.5.0;

import "./Storage.sol";

contract demo {
    Storage.Structing public structing;

    function doStuff() public {
        Storage.bePublic();
    }
}

We recently had a discussion about Using modifiers for permission access control when faced with the code size limit.

in general and ideally, we shouldn’t have to change how we use the language, the compiler should automagically handle this for us and optimize for contract byte size or gas cost depending how we configure it. Rather than we having to understand the internal workings of the compiler and manually optimize to handle the implementation.

This is definitely frustrating and I have similar feelings for having to use libraries to work around contract byte size.

Please ask all the questions that you need.

1 Like

Thanks this worked! I really really thought I tried this but I guess I didn’t again thank you so much! The modifier tip would probably shave off some size too if needed

1 Like