Skip to content

Tx Gas Estimation Improvement #4938

Open

Description

Summary

If you provide "gas": "auto" when submitting a tx, the application will automatically estimate gas for you via runTxModeSimulate before submitting it with that gas estimate.

However, this estimate is not always entirely accurate for the following reasons:

  1. Estimation does not take into consideration state writes
// Safety check: don't write the cache state unless we're in DeliverTx.
if mode != runTxModeDeliver {
	return result
}

// only update state if all messages pass
if result.IsOK() {
	msCache.Write() // <= this is where I believe most of the difference comes from
}
  1. State mutations may have occurred between gas estimation and tx execution which could cause a slight deviation from the estimate. This is exacerbated if the time is increased between estimation and broadcasting/execution.

As a result, the gas-adjustment option exists to multiply this estimate by some factor to account for the delta.

However, the gas-adjustment can become cumbersome for clients and may have to constantly change because of (1) requiring them to constantly adjust with new values (or use very large gas and pay the fees to avoid headache).

Proposal

There is not much we can do about (2) hence I propose we keep the gas-adjustment option. However, I propose we can remove (1) and make estimation nearly exact. I believe this can easily be done by introducing a third volatile yet ephemeral state in BaseApp -- simulateState.

During runTxModeSimulate, we create this simulateState based off of the checkState and cache-wrap it. This will allow us to call Write() and get a nearly true estimation. In addition, this is pretty trivial to implement.

This is mainly a brain dump and I'm not 100% sure this works but I don't immediately see a reason why it should not work.

/cc @faboweb


For Admin Use

  • Not duplicate issue
  • Appropriate labels applied
  • Appropriate contributors tagged
  • Contributor assigned/self-assigned
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    • Status

      📋 Backlog

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions