3

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. Jun 2 '18 at 12:48
4

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. Jun 2 '18 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" > Jun 2 '18 at 12:16
  • 1
    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. Jun 2 '18 at 13:58
  • 2
    Technically it's (2^256 -1)*256 because of length encoding Jun 2 '18 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
    Jun 3 '18 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. Jun 2 '18 at 12:34
  • web3.sha3(f1) is 66bytes "0x" included or 64bytes without "0x" prefix
    – user38075
    Jun 2 '18 at 12:56
  • @siid That is the hexadecimal notation used in javascript, when encoded in a raw transaction it is just 32 bytes.
    – Ismael
    Jun 2 '18 at 20:53
2

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

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

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.