1

I have a library and a contract like this:

pragma solidity ^0.4.8;

library LibUserData{

    struct data{
        string firstName;
        string lastName;
    }

    function setName(data storage _data, string _firstName, string _lastName){
        _data.firstName = _firstName;
        _data.lastName = _lastName;
    }
}

contract User{

    mapping (address => LibUserData.data) public userData;

    function setName(address _address, string _firstName, string _lastName){
        LibUserData.setName(userData[_address], _firstName, _lastName);
    }

    function getName(address _address) constant returns (string, string) {
        return (userData[_address].firstName, userData[_address].firstName);
    }
}

I deploy the library LibUserData first (and only once) and insert its address in the bytecode of the User contract. Then I deploy the User contract with this bytecode. If I call the function setName() of the User contract the data will obviously be stored in the library LibUserData. Then I deploy the User contract again with the same bytecode (same address of library LibUserData). And I call the function setName() again from the second User contract. What happens now? Will the data be stored in the same LibUserData like in the first User contract or is a new instance of the LibUserData library created?

1

the answer to this can be found in solidity's docs:

Libraries are similar to contracts, but their purpose is that they are deployed only once at a specific address and their code is reused using the DELEGATECALL (CALLCODE until Homestead) feature of the EVM. This means that if library functions are called, their code is executed in the context of the calling contract, i.e. this points to the calling contract, and especially the storage from the calling contract can be accessed. As a library is an isolated piece of source code, it can only access state variables of the calling contract if they are explicitly supplied (it would have no way to name them, otherwise).

http://solidity.readthedocs.io/en/develop/contracts.html#libraries

So if you link the same old address, you'll keep using the same old library.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.