Skip to content

Commit

Permalink
Merge PR: #845 fix eth_getBalance result in fast-query mode (#1187)
Browse files Browse the repository at this point in the history
app/refund/refund.go : provide a public func to Calculate refund gas
x/evm/handler.go : at the end of handleMsgEthereumTx,  fix the sender's account balance with refund gas and save it in watcher

Co-authored-by: Zhong Qiu <[email protected]>
  • Loading branch information
lcmmhcc and zhongqiuwood authored Nov 13, 2021
1 parent fa0cc6c commit 1f8be3a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 12 deletions.
33 changes: 22 additions & 11 deletions app/refund/refund.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,7 @@ func (handler Handler) GasRefund(ctx sdk.Context, tx sdk.Tx) (refundGasFee sdk.C

gas := feeTx.GetGas()
fees := feeTx.GetFee()
gasFees := make(sdk.Coins, len(fees))

for i, fee := range fees {
gasPrice := new(big.Int).Div(fee.Amount.BigInt(), new(big.Int).SetUint64(gas))
gasConsumed := new(big.Int).Mul(gasPrice, new(big.Int).SetUint64(gasUsed))
gasCost := sdk.NewCoin(fee.Denom, sdk.NewDecFromBigIntWithPrec(gasConsumed, sdk.Precision))
gasRefund := fee.Sub(gasCost)

gasFees[i] = gasRefund
}

gasFees := caculateRefundFees(ctx, gasUsed, gas, fees)
err = refund.RefundFees(handler.supplyKeeper, ctx, feePayerAcc.GetAddress(), gasFees)
if err != nil {
return nil, err
Expand All @@ -94,3 +84,24 @@ func NewGasRefundDecorator(ak auth.AccountKeeper, sk types.SupplyKeeper) sdk.Gas
return chandler.GasRefund(ctx, tx)
}
}

func caculateRefundFees(ctx sdk.Context, gasUsed uint64, gas uint64, fees sdk.DecCoins) sdk.Coins {

refundFees := make(sdk.Coins, len(fees))
for i, fee := range fees {
gasPrice := new(big.Int).Div(fee.Amount.BigInt(), new(big.Int).SetUint64(gas))
gasConsumed := new(big.Int).Mul(gasPrice, new(big.Int).SetUint64(gasUsed))
gasCost := sdk.NewCoin(fee.Denom, sdk.NewDecFromBigIntWithPrec(gasConsumed, sdk.Precision))
gasRefund := fee.Sub(gasCost)

refundFees[i] = gasRefund
}
return refundFees
}

// CaculateRefundFees provides the way to calculate the refunded gas with gasUsed, fees and gasPrice,
// as refunded gas = fees - gasPrice * gasUsed
func CaculateRefundFees(ctx sdk.Context, gasUsed uint64, fees sdk.DecCoins, gasPrice *big.Int) sdk.Coins {
gas := new(big.Int).Div(fees[0].Amount.BigInt(), gasPrice).Uint64()
return caculateRefundFees(ctx, gasUsed, gas, fees)
}
8 changes: 7 additions & 1 deletion x/evm/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package evm

import (
"github.com/ethereum/go-ethereum/common"
"github.com/okex/exchain/app/refund"
ethermint "github.com/okex/exchain/app/types"
bam "github.com/okex/exchain/libs/cosmos-sdk/baseapp"
sdk "github.com/okex/exchain/libs/cosmos-sdk/types"
Expand Down Expand Up @@ -128,8 +129,13 @@ func handleMsgEthereumTx(ctx sdk.Context, k *Keeper, msg types.MsgEthereumTx) (*
pm := k.GenerateCSDBParams()
infCtx := ctx.WithGasMeter(sdk.NewInfiniteGasMeter())
sendAcc := pm.AccountKeeper.GetAccount(infCtx, sender.Bytes())
//fix sender's balance in watcher with refund fees
gasConsumed := ctx.GasMeter().GasConsumed()
fixedFees := refund.CaculateRefundFees(ctx, gasConsumed, msg.GetFee(), msg.Data.Price)
coins := sendAcc.GetCoins().Add2(fixedFees)
_ = sendAcc.SetCoins(coins)
if sendAcc != nil {
pm.Watcher.SaveAccount(sendAcc, true)
pm.Watcher.SaveAccount(sendAcc, false)
}
ctx.WithGasMeter(currentGasMeter)
}
Expand Down

0 comments on commit 1f8be3a

Please sign in to comment.