0

I have this code which does work.

pragma solidity ^0.4.18;
// this stores keys in state
// a key is a hash of data, eg as used in IPFS
contract DBKeys {

    struct keyStruct {
        bool keyStatus; 
    }
    mapping(bytes32=>keyStruct) public zkeys;
    function addZkey( bytes32 newkey) public returns (bool) {
    if (zkeys[newkey].keyStatus) {
        return false; // already added  
        }
        zkeys[newkey].keyStatus=true;
        return true;    
    }
    function delZkey(bytes32 newkey) public returns (bool) {
        if (zkeys[newkey].keyStatus) {
            delete zkeys[newkey];
            return true;
        }
        return false;
    }
}

I tried to convert into a library which would basically provide the function of building an array of hashes. But I ended up with numerous compile errors.

I understand libraries do not hold any storage but they can define a struct. I saw an example in the docs but that did not delete array members. I want to delete array members to free up storage: I assume that would free up storage.

How is the struct/mapping coded in a library which then also allows the array members to be deleted?

1

From the documentation:

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.

In your case the library is not stateless and you're trying to modify the state. Anyway there is a good example in the Solidity documentation which will do what are you looking for. Try to have a look http://solidity.readthedocs.io/en/develop/contracts.html#libraries

p.s. Library will use the storage from the calling contract.

pragma solidity ^0.4.17;

library DBKeys {

    struct keyStruct {
      mapping(bytes32=>bool) zkeys;
    }

    function addZkey(keyStruct storage data, bytes32 newkey) public returns (bool) {
      if (data.zkeys[newkey]) {
        return false; // already added  
      }

      data.zkeys[newkey] = true;

      return true;    
    }
    function delZkey(keyStruct storage data, bytes32 newkey) public returns (bool) {
        if (data.zkeys[newkey]) {
            delete data.zkeys[newkey];

            return true;
        }
        return false;
    }
}

contract Example {
    DBKeys.keyStruct knownValues;

    function add(bytes32 value) public {
        require(DBKeys.addZkey(knownValues, value));
    }
    function remove(bytes32 value) public {
        require(DBKeys.delZkey(knownValues, value));
    }
}

this compile without warnings

  • I added this function to test and it compiles but the getter returns only false; function contains(keyStruct storage self, bytes32 value) public view returns (bool) { return self.zkeys[value]; } – Trevor Lee Oakley Mar 5 '18 at 6:53

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.