Store data in Ethereum by logging to reduce gas cost

0
9

I propose a realistic solution to using Ethereum as a public ledger

Problems are gas cost and capacity

We will face two problems with using Ethereum as a public ledger. The first one is the gas cost. Storing data to storage is the most expensive operation. (see SSTORE gas cost) The other one is capacity. Event solidity seems not have any data size limitation, we restricted by gas limit.

So, the current Ethereum is not suitable for large data or frequent storing.

Ethereum is a reference and IPFS is storage

Store data on distributed storage, like IPFS. IPFS doesn’t have any data size limitations. Then, store the hash to Ethereum for data integrity. IPFS hash is really small so that this contributes to saving gas cost. This is the most likely taken solution. Etherum famous projects, like Origin Protocol or Openlaw are doing.

For reducing the gas costs further, we propose to store data by event emission. Logging is enough unless you delete data or operational data from a smart contract.

How you implement?

We define following event.

event Recorded (bytes4 indexed eventSig, uint256 indexed createdAt, bytes32 ipfsHash);

‘eventSig’ is like event name. It doesn’t matter to specify any arbitrary length string. Though it’s better specifying fixed length short letters to save gas cost. We define ‘evetSig’ as the first 4 bytes of the Keccak256 hash of event name. This rule is similar to ‘function selector

‘createdAt’ is self explaining. The data time when this event emitted.

‘ipfsHash’ is place to specify IPFS hash. Please take care that the argument type is not ‘string’. See the following example, the length of IPFS hash is fixed as 46.

QmXbTtSAPJ545YRnLt7n7ngMa4ZTmizmznshZZjXDRhYih

According to solidity specific, string data is encoded as utf8, so IPFS hash is 46 byte size.

According to solidity specific, this string data consume 64 bytes storage because 18 bytes are padded with trailing zero-bytes to a length of 32 bytes. Then we decode IPFS hash by base58. (Originally, IPFS hash string is base58 encoded) The base58 decoded IPFS hash is 32 byte size so we achieve to reduce 32 byte. This contribute saving gas cost.

Sample implementation

This is the implementation example.

Unnecessarily logs should not be stored, so that only allowed people should be able to emit ‘Recorded’ event. So we set ‘onlyWhitelistAdmin’ modifier.

import 'openzeppelin-solidity/contracts/access/roles/WhitelistAdminRole.sol';

event Recorded (bytes4 indexed eventSig, uint256 indexed createdAt, bytes32 ipfsHash);

/**
* @dev Write ipfsHash as log
*/
function writeHash(bytes4 _eventSig, bytes32 _ipfsHash) public onlyWhitelistAdmin {
emit Recorded(_eventSig, uint256(now), _ipfsHash);
}

Discussion

Please tell me your opinion here, ERC 2307.


Store data in Ethereum by logging to reduce gas cost was originally published in Coinmonks on Medium, where people are continuing the conversation by highlighting and responding to this story.