5

I know there was a string limit but I thought that was long ago. I had a smart contract working but there is a limit of 32 bytes on the string input. I am unsure if a version changed or something like that. I had tested before and it was handling strings longer than 32 bytes. Now it does not.

When that is exceeded it does not insert any data. I ma using PoA now, I was testing with PoW before. But that should not make any difference.

I am running addInventory in the following contract.

Can someone clarify the allowed string length?

pragma solidity ^0.4.18;

// this contract stores the inventory strings, the hash of the string, and the keys to storage areas
// to add - events for updates, sha to check passed sha (if an error reject),

contract RegisterContract {

    uint public inventoryWeight;

    mapping(bytes32=>string) public inventories; // stores of json strings
    bytes32[] public inventoryHashes; // this is used mainly in testing eg inventoryHashes(uint) for the sequence of inventories added
    bytes32[] public inventoryHashesStorage; // keys to storage

    event eventNewInventory(bytes32 hashInventory);
    event eventNewStorageInventory(bytes32 hashInventory);

    function addInventory ( string inventory, bytes32 hashInventory) public {
        inventories[hashInventory] = inventory;
        inventoryHashes.push(hashInventory);
        eventNewInventory(hashInventory);
     }

     function addInventoryHash(bytes32 hashInventory) public {
         // storage key - kept separate in case add addInventory fails due to gas limits
         // during testing addInventory did sometimes fail due to long strings
         // this hashInventory would be a key to IPFS or a database
         inventoryHashesStorage.push(hashInventory);
         eventNewStorageInventory(hashInventory);
     }

    function getAllInventories() public view returns (bytes32[]) {
        return inventoryHashesStorage;
    }

    function addWeight (uint weight) public {
        inventoryWeight = weight;
    }

    function () payable public {
    }

}
1
  • This works on Ropsten. So it is a geth issue. Commented Jun 2, 2018 at 12:48

3 Answers 3

6

A variable of type bytes32 can't contain strings longer than 32 characters.

A variable of type string can contain a string of any size.

Do you have a reproduceable example where a string can't contain more than 32 characters? Maybe an etherscan link to a transaction on your contract?

5
  • I can send the connection details, if you have a PM here. Commented Jun 2, 2018 at 11:59
  • > f1="jkhdfkjsdhfjdshkhskjghdfjkghkjdfhgjkdfhgkjfhgkjdfhgkjfhdgkjfhgkjfhgkjf" "jkhdfkjsdhfjdshkhskjghdfjkghkjdfhgjkdfhgkjfhgkjdfhgkjfhdgkjfhgkjfhgkjf" > eth.defaultAccount=eth.coinbase "0x0c12901e1ec7f80763cd433e8eca0c837d11a0c1" > con.addInventory(f1, web3.sha3(f1)) "0x8d506679b06d4d6cb4a81e0ab53f5a0abe7abfb12f7bb1cd04764c8bdc444eb9" > con.inventories(web3.sha3(f1)) "" > f1="hjkfhgfjghfjkg" "hjkfhgfjghfjkg" > con.addInventory(f1, web3.sha3(f1)) "0x737419aff198c110d3290e33bd5f0897c885983ebc57d41cd8b547f0df8c6858" > con.inventories(web3.sha3(f1)) "hjkfhgfjghfjkg" > Commented Jun 2, 2018 at 12:16
  • 3
    I would like to clarify that, while in practice it is a string of any size, there is actually a limit of 2^256 * 256 bits or about 3.7 * 10^54 yottabytes. Commented Jun 2, 2018 at 13:58
  • 2
    Technically it's (2^256 -1)*256 because of length encoding Commented Jun 2, 2018 at 14:30
  • 2
    If you're going to consider that a limit, you should consider lots of other limitations (hard drive size, network speed, transaction fee, etc) which all cause even lower limits :)
    – Jesbus
    Commented Jun 3, 2018 at 9:12
2

Maybe the hashes pushed into the array are longer than 32bytes. Btw, wouldn't it better to save the hashes in a mapping instead?

3
  • 1
    Hashes are all 32 bytes. There is a mapping already. Commented Jun 2, 2018 at 12:34
  • web3.sha3(f1) is 66bytes "0x" included or 64bytes without "0x" prefix
    – user38075
    Commented Jun 2, 2018 at 12:56
  • @siid That is the hexadecimal notation used in javascript, when encoded in a raw transaction it is just 32 bytes.
    – Ismael
    Commented Jun 2, 2018 at 20:53
2

Gas cost - commodityContract.addInventory(mystrS, shastr, {gas: 4712000}); commodityContract.addInventoryHash(shastr,{gas: 4712000});

1
  • 2
    If this was intended to answer the question, please edit it to clarify how it answers the question that was asked.
    – eth
    Commented Jun 3, 2018 at 17:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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