[Ethereum] solidity string limit of 32 bytes

go-ethereumsolidity

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

}

Best Answer

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?

Related Topic