Open grarco opened 1 year ago
@grarco can I take this up in case you aren't actively working on it?
@ameya-deshmukh yes sure, just make sure to set me as a reviewer for the PR, thank you!
How would this work for the case where you want to use all available funds in an account? If the gas limit is calculated as part of submitting the transaction, there would be no way to know in advance how much is available to transfer after the fees are deducted. I'm guessing you could still manually set the gas limit, or something like that?
Hi @emccorson, yes it's always possible to manually set a specific gas limit. The CLI argument is optional and this issue refers to the case where the user does not pass this argument and the client need to estimate the limit to produce a valid transaction
Currently we have set a predefined value for the
gas-limit
argument in the client. A user can otherwise run a a tx withdry-run-wrapper
to get the gas requirement of it and override the default value.A better solution would be to make the client estimate the gas cost automatically before submitting a transaction. A first, naive, implementation could silently call the same transaction with the
dry-run-wrapper
flag to extract the gas cost that can then be set for the transaction. This might cause an excessive overhead on every transaction.A possible optimization could be to keep a cache in the client mapping every transaction's hash to its gas cost. If the gas cost is missing then the client proceeds as explained before and then caches the gas value that it got. If instead the gas cost is in cache, we can avoid the simulation step and submit the transaction immediately.
After we submit a tx we should track its execution: if the transaction fails because of insufficient gas, the client should invalidate the cached value by deleting it: the next time the same transaction will be submitted the client will first simulate the gas cost to get the updated value.
Note that, given that the gas cost of a transaction is not deterministic, we should also add some more gas on top of the estimated one (say 15% more) as a safety margin.
Gas simulation with the
dry-run-wrapper
might be a bit tricky though because of the way it works: at the moment, it expect a valid transaction. To do gas estimation, though, we'd like to remove the the gas limit and let the tx run till completion or failure (but ignorin fee failures because of insufficient balance since fees are paid based on gas).