I'm trying to figure out a solution to this problem, to do with transaction confirmation order and setting values to 0
pragma solidity ^0.5.17;
contract Test {
uint256 amount;
constructor() public {}
function join() public {
amount += 100;
}
function leave() public {
amount -= 100;
}
}
Given these transactions (tested on ropsten):
tx 1) Call Join Confirmed
amount == 100
tx 2) Call Join (gas price 1) Pending
amount == 100 should tx3 get mined first
tx 3) Call Leave (gas price 100) Pending
amount == 0
However tx 2
will always fail with an out of gas
error for as long as the amount
is set back to 0
. This doesn't happen if the value is any higher than 0
. My understanding is that it costs more gas to set a value to its 0
state instead of a positive integer, and the gas estimation isn't taking this into account. I've tried delete
hoping this would give a gas refund to compensate for the too-low gas limit, but it still failed.
Is there an elegant way to handle this scenario? The only ways I can think of are over-estimating the gas for all join
transactions, which has its obvious drawbacks, or never setting amount
back to 0
.