Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why we charge the gasWant fee but not the gasUsed fee? #6016

Closed
4 tasks
louisliu2048 opened this issue Apr 17, 2020 · 3 comments
Closed
4 tasks

Why we charge the gasWant fee but not the gasUsed fee? #6016

louisliu2048 opened this issue Apr 17, 2020 · 3 comments

Comments

@louisliu2048
Copy link

louisliu2048 commented Apr 17, 2020

Summary

One question that confused me is that:why we charge the gasWant fee but not the gasUsed fee?

Problem Definition

Since in the
func (app *BaseApp) runTx(mode runTxMode, txBytes []byte, tx sdk.Tx) (gInfo sdk.GasInfo, result *sdk.Result, err error){}
function, we can get the gasWanted after run the app.anteHandler and we also get the gasUsed after we run the app.runMsgs, why not do something more to make the right gas charge.

Proposal

Here is my mind:
1、Add a WithdrawHandler type in cosmos-sdk/types/handler.go:
type WithdrawHandler func(ctx Context, tx Tx, fee uint64)(newCtx Context, err error)

2、Add a withdrawHandler field for BaseApp in cosmos-sdk/baseapp/baseapp.go:

type BaseApp struct {
 ...
 // set upon LoadVersion or LoadLatestVersion.
 baseKey *sdk.KVStoreKey // Main KVStore in cms

 withdrawHandler     sdk.WithdrawHandler
 anteHandler    sdk.AnteHandler  // ante handler for fee and auth
 initChainer    sdk.InitChainer  // initialize state with validators and state blob
 ...
}

And add a SetWithdrawHandler func for the BaseApp struct:

func (app *BaseApp) SetWithdrawHandler(wh sdk.WithdrawHandler) {
 if app.sealed {
  panic("SetWithdrawHandler() on sealed BaseApp")
 }
 app.withdrawHandler = wh
}

3、Modify the runTx function in cosmos-sdk/baseapp/baseapp.go:

func (app *BaseApp) runTx(mode runTxMode, txBytes []byte, tx sdk.Tx) (gInfo sdk.GasInfo, result *sdk.Result, err error) {
 ...
 // Attempt to execute all messages and only update state if all messages pass
 // and we're in DeliverTx. Note, runMsgs will never return a reference to a
 // Result if any single message fails or does not have a registered Handler.
 result, err = app.runMsgs(runMsgCtx, msgs, mode)
 if err == nil && mode == runTxModeDeliver {
  msCache.Write()
 }

 // If the origin apps do not want to add a feeHandler, just ignore it, everything will still be ok
 if app.withdrawHandler != nil {
  gasUsed := runMsgCtx.GasMeter().GasConsumed()
  
  if gasWanted > gasUsed {
    app.withdrawHandler(runMsgCtx, tx, gasWanted - gasUsed)
    // The withdraw Handler will do something like this:
    // supplyKeeper.SendCoinsFromModuleToAccount(ctx, acc.GetAddress(), types.FeeCollectorName, fees) 
  }
  // if gasUsed > gasWanted, it will fail and return when runMsgs
 }

 return gInfo, result, err
}

4、 Add a default withdrawHandler in x/auth module, the process has been describe in step 3.


For Admin Use

  • Not duplicate issue
  • Appropriate labels applied
  • Appropriate contributors tagged
  • Contributor assigned/self-assigned
@alexanderbez
Copy link
Contributor

I'm sorry, but I don't understand the problem definition you've laid out.

why we charge the gasWant fee but not the gasUsed fee?

Why is this confusing?

@louisliu2048
Copy link
Author

Currently, we charge the gas fee: gas_price * gas_wanted,not the gas_price * gas_used, that means the end user need to pay more money. The Ethereum platform only charge gas_used * gas_price and will return the money that unused.

@alexanderbez
Copy link
Contributor

We already have an issue to discuss refunding gas here. That being said, the fee computation will not change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants