-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* deteach burn permission from the owner * convert precompiles out-of-gas to evm out-of-gas * change burn function on initiaerc20 * burn left balance in the self destructed account at commit * return zero hash if the account does not have code * fix to update balance before commit * return empty code hash when account exists but code is empty * implement gas refunds at post handler * use gas consumped to limit * add IsEVMTx check at gas refunds * remove unnecessary override * call next() * remove print
- Loading branch information
Showing
36 changed files
with
690 additions
and
194 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package ante | ||
|
||
import ( | ||
"context" | ||
|
||
evmtypes "github.com/initia-labs/minievm/x/evm/types" | ||
) | ||
|
||
type EVMKeeper interface { | ||
GetFeeDenom(ctx context.Context) (string, error) | ||
TxUtils() evmtypes.TxUtils | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package posthandler | ||
|
||
import ( | ||
"fmt" | ||
|
||
errorsmod "cosmossdk.io/errors" | ||
"cosmossdk.io/math" | ||
storetypes "cosmossdk.io/store/types" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" | ||
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" | ||
|
||
evmante "github.com/initia-labs/minievm/x/evm/ante" | ||
) | ||
|
||
var _ sdk.PostDecorator = &GasRefundDecorator{} | ||
|
||
type GasRefundDecorator struct { | ||
ek EVMKeeper | ||
} | ||
|
||
func NewGasRefundDecorator(ek EVMKeeper) sdk.PostDecorator { | ||
return &GasRefundDecorator{ | ||
ek, | ||
} | ||
} | ||
|
||
// PostHandle handles the gas refund logic for EVM transactions. | ||
func (erd *GasRefundDecorator) PostHandle(ctx sdk.Context, tx sdk.Tx, simulate, success bool, next sdk.PostHandler) (newCtx sdk.Context, err error) { | ||
if success && ctx.ExecMode() == sdk.ExecModeFinalize { | ||
// Conduct gas refund only for the successful EVM transactions | ||
if ok, err := erd.ek.TxUtils().IsEthereumTx(ctx, tx); err != nil || !ok { | ||
return next(ctx, tx, simulate, success) | ||
} | ||
|
||
feeTx, ok := tx.(sdk.FeeTx) | ||
if !ok { | ||
return ctx, errorsmod.Wrap(sdkerrors.ErrTxDecode, "Tx must be a FeeTx") | ||
} | ||
|
||
value := ctx.Value(evmante.ContextKeyGasPrices) | ||
if value == nil { | ||
return next(ctx, tx, simulate, success) | ||
} | ||
gasRefundRatio, err := erd.ek.GasRefundRatio(ctx) | ||
if err != nil { | ||
return ctx, err | ||
} | ||
|
||
gasPrices := value.(sdk.DecCoins) | ||
gasLeft := ctx.GasMeter().Limit() - ctx.GasMeter().GasConsumedToLimit() | ||
gasRefund := gasRefundRatio.MulInt64(int64(gasLeft)).TruncateInt().Uint64() | ||
|
||
// gas used for refund operation | ||
coinsRefund, _ := gasPrices.MulDec(math.LegacyNewDecFromInt(math.NewIntFromUint64(gasRefund))).TruncateDecimal() | ||
if coinsRefund.Empty() || coinsRefund.IsZero() { | ||
return next(ctx, tx, simulate, success) | ||
} | ||
|
||
feePayer := feeTx.FeePayer() | ||
if feeGranter := feeTx.FeeGranter(); feeGranter != nil { | ||
feePayer = feeGranter | ||
} | ||
|
||
// emit gas refund event | ||
ctx.EventManager().EmitEvent(sdk.NewEvent( | ||
EventTypeGasRefund, | ||
sdk.NewAttribute(AttributeKeyGas, fmt.Sprintf("%d", gasRefund)), | ||
sdk.NewAttribute(AttributeKeyCoins, coinsRefund.String()), | ||
)) | ||
|
||
// TODO - should we charge gas for refund? | ||
// | ||
// for now, we use infinite gas meter to prevent out of gas error or inconsistency between | ||
// used gas and refunded gas. | ||
feeCollectorAddr := authtypes.NewModuleAddress(authtypes.FeeCollectorName) | ||
err = erd.ek.ERC20Keeper().SendCoins(ctx.WithGasMeter(storetypes.NewInfiniteGasMeter()), feeCollectorAddr, feePayer, coinsRefund) | ||
if err != nil { | ||
return ctx, err | ||
} | ||
} | ||
|
||
return next(ctx, tx, simulate, success) | ||
} | ||
|
||
const ( | ||
EventTypeGasRefund = "gas_refund" | ||
AttributeKeyGas = "gas" | ||
AttributeKeyCoins = "coins" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,28 @@ | ||
package posthandler | ||
|
||
import ( | ||
"context" | ||
|
||
"cosmossdk.io/math" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
authante "github.com/cosmos/cosmos-sdk/x/auth/ante" | ||
|
||
evmtypes "github.com/initia-labs/minievm/x/evm/types" | ||
) | ||
|
||
// NewPostHandler returns a new sdk.PostHandler that is composed of the sdk.ChainPostDecorators | ||
func NewPostHandler(ak authante.AccountKeeper) sdk.PostHandler { | ||
return sdk.ChainPostDecorators(NewSequenceIncrementDecorator(ak)) | ||
func NewPostHandler( | ||
ak authante.AccountKeeper, | ||
ek EVMKeeper, | ||
) sdk.PostHandler { | ||
return sdk.ChainPostDecorators( | ||
NewSequenceIncrementDecorator(ak), | ||
NewGasRefundDecorator(ek), | ||
) | ||
} | ||
|
||
type EVMKeeper interface { | ||
GasRefundRatio(context.Context) (math.LegacyDec, error) | ||
ERC20Keeper() evmtypes.IERC20Keeper | ||
TxUtils() evmtypes.TxUtils | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.