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 {
    }

}
Trevor Oakley
  • 777
  • 9
  • 20

3 Answers3

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?

Jesbus
  • 10,478
  • 6
  • 35
  • 62
  • I can send the connection details, if you have a PM here. – Trevor Oakley Jun 02 '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"

    – Trevor Oakley Jun 02 '18 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. – Alex Papageorgiou Jun 02 '18 at 13:58
  • 2
    Technically it's (2^256 -1)*256 because of length encoding – Tjaden Hess Jun 02 '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 03 '18 at 09: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?

2

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

Trevor Oakley
  • 777
  • 9
  • 20
  • 2
    If this was intended to answer the question, please edit it to clarify how it answers the question that was asked. – eth Jun 03 '18 at 17:16