1

Sometimes we need to use string instead of byte32 only because of one byte (or one character) and this increases the gas and the cost of transaction.

For example, I want to store the assets state of a building in blockchain. We have several specific actions for these assets such that we have to mention exactly the title of an action ex. Check operation of low water level float that is 41 bytes. So we cannot use byte32 to store this action. And we need string.

According to this article

Logs were designed to be a form of storage that costs significantly less gas than contract storage. Logs basically cost 8 gas per byte, whereas contract storage costs 20,000 gas per 32 bytes.

However,I am not sure in case of using string in event, how much the cost will be decreased?

What is the reason that string is more expensive than other types ? Solidity or Ethereum ? It depends on consensus mechanism used in the blockchain ? And can it will resolved in future ?

Here is some part of my Solidity code (I tried to summarize the code to be more clear. However, if you need more, please let me know.)

struct StateStruct {
                    mapping(bytes32 => bytes32) subState;
                }

                struct AssetStruct {
                    StateStruct state;
                    address owner; 
                    bool isAsset;
                    bytes32 review;
                    bytes32 action;
                    bytes32 player;
                    bytes32 actionState;
                    bytes32 assetSite;
                    bool isOwner;
                }

            mapping(uint256 => AssetStruct) assetStructs;
                uint256[] public assetList;

                uint256 public _public_id = 0;


       event LogNewAssetOneAction(uint256 indexed _id, bytes32 _action, bytes32 _player, bytes32 _actionState,  address _owner);

            function newAssetOneActionManualID(uint256 _id, bytes32 _action, bytes32 _player, bytes32 _actionState,  address _owner, bytes32 _assetSite) public returns(bool success) {
            require(!isAsset(_id));

            assetStructs[_id].owner = _owner;
            assetStructs[_id].isAsset = true;
            assetStructs[_id].assetSite = _assetSite;

            assetStructs[_id].action = _action;
            assetStructs[_id].player = _player;
            assetStructs[_id].actionState = _actionState;

            assetList.push(_id);

            emit LogNewAssetOneAction(uint256(_id), bytes32(_action), bytes32(_player), bytes32(_actionState), address(_owner));

            return true;
        }
  • Where is your code, and what is the gas difference you're seeing? bytes32 can fit into a single 32-byte word, but anything longer will require storing a length and then multiple words. Perhaps this is the difference you're seeing. – user19510 Jun 4 '18 at 19:59
  • @smarx Thank you. The source code is not currently available, however I add it to my question tomorrow. Thanks – Questioner Jun 4 '18 at 21:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.