Open srdtrk opened 2 months ago
I asked chatGPT and it seems like Etherum does refund gas from the transaction if you remove state. This was the answer:
Yes, Ethereum does give gas refunds when values are deleted from a mapping in a Solidity contract. This refund occurs because of how storage is managed on the Ethereum blockchain.
Storage Costs: Writing data to the blockchain's storage (such as adding or updating a value in a mapping) is expensive in terms of gas. Storage Clearing: When you delete a value from a mapping, the corresponding storage slot is cleared. This action reduces the amount of storage used by the contract, which Ethereum incentivizes by offering a partial gas refund.
When you delete an entry from a mapping (or set a storage value to 0), Ethereum refunds some of the gas costs to the transaction initiator. This refund is not the full cost of the original storage operation but is designed to encourage the efficient use of storage.
Here's a simple example:
mapping(uint => uint) public data;
function storeData(uint key, uint value) public {
data[key] = value; // This consumes gas to store the value
}
function deleteData(uint key) public {
delete data[key]; // This deletes the value, triggering a gas refund
}
When delete data[key];
is executed, the storage slot associated with key is cleared, and a gas refund is issued.
Refund Cap: Ethereum imposes a cap on the maximum gas refund, which is 50% of the total gas used in the transaction. This means you can't completely offset the cost of your transaction with refunds. Gas Refund Mechanism: The gas refund is applied at the end of the transaction, meaning you must still have enough gas upfront to execute the transaction, even if you will receive a refund afterward.
This behavior is important to keep in mind for developers looking to optimize the gas efficiency of their contracts.
Currently, consensus state hashes will continue to be stored in the solidity contract indefinitely. However, once consensus state hashes are outside the trusting period, they could be pruned. Therefore it makes sense to allow tx senders to prune expired consensus states. Therefore, suppose the sender shows that the consensus state at height 500 is expired, then we can allow the sender to prune all heights less than or equal to 500.
Note that I'm not sure if it makes sense to do this from a gas perspective since I'm not sure if Eth is doing gas refunds for removing state. It would be good to get clarity on whether or not there are any gas benefits to implementing this.