Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.Sign up
accounts/abi/bind, internal/ethapi: binary search gas estimation #3587
Gas estimation currently mostly works, but can underestimate for more funky refunds. This is because various ops (e.g. CALL) need more gas to run than they actually consume (e.g. 2300 stipend that is refunded if not used). With more intricate contract interplays, it becomes almost impossible to return a proper value to the user.
This PR swaps out the simplistic gas estimation to a binary search approach, honing in on the correct gas use. This does mean that gas estimation needs to rerun the transaction log(max-price) times to measure whether it fails or not, but it's a price paid by the transaction issuer, and it should be worth it to support proper estimates.
Verified in both the simulator tests as well as on Ropsten:
Deployed at 0xf347ee4bF34a1a4e5c7a11694669602b5324Fc56
Calling SetField is estimated as
Running with this estimated gas cost:
Results in successful transaction 0x31912e1743c5db3a9af4739c937fa1b595277e7d445b12c977dcecf2230430da
Note, actual gas usage is 42918, way below the estimated 122418!
Running with 1 gas below the estimated cost:
Results in a failed transaction 0xffb8ffbf722de204781ba9f94c7c4065edc03de7bbfb5583b859b782ee815718
Note, even though the end gas usage would have been 42918, the contract goes OOG with only 122417 allowance!