You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm trying to understand how estimateGas works in current geth. This question is not related to ethers, but I'd appreciate your experienced eye to take a quick look at this. Thank you.
If I run the callMain transaction directly(without estimation or whatsover), it says that gasUsed is 55834. Though, estimateGas from ethers returns 58955.
I wonder why estimateGas didn't return smaller value closer to 55834. The reason I am expecting this is because of binary search. Geth uses binary search and tries different values and finds the smallest gas limit for which tx succeeds, but it's interesting why for example it didn't return 57000 instead of 58955. Then I looked into the implementation
In my case, lo = 55834 - 1 = 55833, optimisticGas = (55833 + 0 + 2300) * 64/63 = 59055 and since optimisticGas < hi, then hi = optimisticGas = 59055 according to the code. This all suggests that binary search here happens between 55833 and 50102 and no chance it would end up with 58955 (see the binary search). For simplicity, I wrote the following in js:
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I'm trying to understand how estimateGas works in current geth. This question is not related to ethers, but I'd appreciate your experienced eye to take a quick look at this. Thank you.
I have the following contract:
If I run the
callMain
transaction directly(without estimation or whatsover), it says thatgasUsed
is 55834. Though,estimateGas
from ethers returns 58955.I wonder why
estimateGas
didn't return smaller value closer to 55834. The reason I am expecting this is because of binary search. Geth uses binary search and tries different values and finds the smallest gas limit for which tx succeeds, but it's interesting why for example it didn't return 57000 instead of 58955. Then I looked into the implementationIn my case,
lo = 55834 - 1 = 55833
,optimisticGas = (55833 + 0 + 2300) * 64/63 = 59055
and sinceoptimisticGas < hi
, thenhi = optimisticGas = 59055
according to the code. This all suggests that binary search here happens between 55833 and 50102 and no chance it would end up with 58955 (see the binary search). For simplicity, I wrote the following in js:It logs 56639 instead of 58955.
Any idea ? Thanks in advance for your advices.
Beta Was this translation helpful? Give feedback.
All reactions