-
Notifications
You must be signed in to change notification settings - Fork 3
Apply fee abstraction on v0.5.1 #9
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
Merged
Merged
Changes from 12 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
8288315
feat: update the gas refundGas function to handle paied fees from the…
jhelison e5f134f
test: add tests to the new logic for gasRefunds
jhelison 102da88
refactor: apply typo fix
jhelison bdff4ca
feat: add check if gas is zero
jhelison 34535d5
Merge branch 'feat/fork-v0.5.1' into feat/fee-abstraction
Thaleszh f64ea66
Merge pull request #8 from KiiChain/feat/fee-abstraction
Thaleszh 42a6832
feat: fix unconsistent states
f7305fa
fix: use correct network type for test
987fef5
feat: use correct malleate and unify testdenom
77780c8
fix: correct double coin case
c602fbf
feat: use correct gas used
1d80bd3
chore: remove commented code
15fe9a2
fix: revert uneeded changes
12bde30
fix: remove uneeded comment
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,221 @@ | ||
| package vm | ||
|
|
||
| import ( | ||
| "math/big" | ||
|
|
||
| "github.com/ethereum/go-ethereum/params" | ||
|
|
||
| "github.com/cosmos/evm/testutil/integration/evm/factory" | ||
| "github.com/cosmos/evm/testutil/integration/evm/grpc" | ||
| "github.com/cosmos/evm/testutil/integration/evm/network" | ||
| testkeyring "github.com/cosmos/evm/testutil/keyring" | ||
| "github.com/cosmos/evm/x/vm/keeper" | ||
| "github.com/cosmos/evm/x/vm/types" | ||
|
|
||
| sdkmath "cosmossdk.io/math" | ||
|
|
||
| sdk "github.com/cosmos/cosmos-sdk/types" | ||
| authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" | ||
| banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" | ||
| ) | ||
|
|
||
| const ( | ||
| DefaultCoreMsgGasUsage = 21000 | ||
| DefaultGasPrice = 120000 | ||
| TestDenom = "acoin" | ||
| ) | ||
|
|
||
| // TestGasRefundGas tests the refund gas exclusively without going though the state transition | ||
| // The gas part on the name refers to the file name to not generate a duplicated test name | ||
| func (suite *KeeperTestSuite) TestGasRefundGas() { | ||
| // FeeCollector account is pre-funded with enough tokens | ||
| // for refund to work | ||
| // NOTE: everything should happen within the same block for | ||
| // feecollector account to remain funded | ||
| baseDenom := types.GetEVMCoinDenom() | ||
|
|
||
| coins := sdk.NewCoins( | ||
| sdk.NewCoin( | ||
| baseDenom, | ||
| sdkmath.NewInt(6e18), | ||
| ), | ||
| sdk.NewCoin( | ||
| TestDenom, | ||
| sdkmath.NewInt(6e18), | ||
| ), | ||
| ) | ||
| feeAddress := authtypes.NewModuleAddress(authtypes.FeeCollectorName) | ||
|
|
||
| balances := []banktypes.Balance{ | ||
| { | ||
| Address: feeAddress.String(), | ||
| Coins: coins, | ||
| }, | ||
| } | ||
| bankGenesis := banktypes.DefaultGenesisState() | ||
| bankGenesis.Balances = balances | ||
| customGenesis := network.CustomGenesisState{} | ||
| customGenesis[banktypes.ModuleName] = bankGenesis | ||
|
|
||
| // Create a txFactory | ||
| keyring := testkeyring.New(2) | ||
| unitNetwork := network.NewUnitTestNetwork( | ||
| suite.Create, | ||
| network.WithPreFundedAccounts(keyring.GetAllAccAddrs()...), | ||
| network.WithCustomGenesis(customGenesis), | ||
| ) | ||
| grpcHandler := grpc.NewIntegrationHandler(unitNetwork) | ||
| txFactory := factory.New(unitNetwork, grpcHandler) | ||
|
|
||
| // Create a core message to use for the test | ||
| sender := keyring.GetKey(0) | ||
| recipient := keyring.GetAddr(1) | ||
| coreMsg, err := txFactory.GenerateGethCoreMsg( | ||
| sender.Priv, | ||
| types.EvmTxArgs{ | ||
| To: &recipient, | ||
| Amount: big.NewInt(100), | ||
| GasPrice: big.NewInt(DefaultGasPrice), | ||
| }, | ||
| ) | ||
| suite.Require().NoError(err) | ||
|
|
||
| // Produce all the test cases | ||
| testCases := []struct { | ||
| name string | ||
| leftoverGas uint64 // The coreMsg always uses 21000 gas limit | ||
| malleate func(sdk.Context) sdk.Context | ||
| expectedRefund sdk.Coins | ||
| errContains string | ||
| }{ | ||
| { | ||
| name: "Refund the full value as no gas was used", | ||
| leftoverGas: DefaultCoreMsgGasUsage, | ||
| expectedRefund: sdk.NewCoins( | ||
| sdk.NewCoin(baseDenom, sdkmath.NewInt(DefaultCoreMsgGasUsage*DefaultGasPrice)), | ||
| ), | ||
| }, | ||
| { | ||
| name: "Refund half the value as half gas was used", | ||
| leftoverGas: DefaultCoreMsgGasUsage / 2, | ||
| expectedRefund: sdk.NewCoins( | ||
| sdk.NewCoin(baseDenom, sdkmath.NewInt((DefaultCoreMsgGasUsage*DefaultGasPrice)/2)), | ||
| ), | ||
| }, | ||
| { | ||
| name: "No refund as no gas was left over used", | ||
| leftoverGas: 0, | ||
| expectedRefund: sdk.NewCoins( | ||
| sdk.NewCoin(baseDenom, sdkmath.NewInt(0)), | ||
| ), | ||
| }, | ||
| { | ||
| name: "Refund with context fees, refunding the full value", | ||
| leftoverGas: DefaultCoreMsgGasUsage, | ||
| malleate: func(ctx sdk.Context) sdk.Context { | ||
| // Set the fee abstraction paid fee key with a single coin | ||
| return ctx.WithValue( | ||
| keeper.ContextPaidFeesKey{}, | ||
| sdk.NewCoins( | ||
| sdk.NewCoin(TestDenom, sdkmath.NewInt(750_000_000)), | ||
| ), | ||
| ) | ||
| }, | ||
| expectedRefund: sdk.NewCoins( | ||
| sdk.NewCoin(TestDenom, sdkmath.NewInt(750_000_000)), | ||
| ), | ||
| }, | ||
| { | ||
| name: "Refund with context fees, refunding the half the value", | ||
| leftoverGas: DefaultCoreMsgGasUsage / 2, | ||
| malleate: func(ctx sdk.Context) sdk.Context { | ||
| // Set the fee abstraction paid fee key with a single coin | ||
| return ctx.WithValue( | ||
| keeper.ContextPaidFeesKey{}, | ||
| sdk.NewCoins( | ||
| sdk.NewCoin(TestDenom, sdkmath.NewInt(750_000_000)), | ||
| ), | ||
| ) | ||
| }, | ||
| expectedRefund: sdk.NewCoins( | ||
| sdk.NewCoin(TestDenom, sdkmath.NewInt(750_000_000/2)), | ||
| ), | ||
| }, | ||
| { | ||
| name: "Refund with context fees, no refund", | ||
| leftoverGas: 1, | ||
| malleate: func(ctx sdk.Context) sdk.Context { | ||
| // Set the fee abstraction paid fee key with a single coin | ||
| return ctx.WithValue( | ||
| keeper.ContextPaidFeesKey{}, | ||
| sdk.NewCoins( | ||
| sdk.NewCoin(TestDenom, sdkmath.NewInt(750_000_000)), | ||
| ), | ||
| ) | ||
| }, | ||
| expectedRefund: sdk.NewCoins( | ||
| sdk.NewCoin(TestDenom, sdkmath.NewInt(0)), | ||
| ), | ||
| }, | ||
| { | ||
| name: "Error - More than one coin being passed", | ||
| leftoverGas: DefaultCoreMsgGasUsage - 1, // Using some leftover so the refund doesn't short circuit | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why did this test pass before?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above, reverted on a commit |
||
| malleate: func(ctx sdk.Context) sdk.Context { | ||
| // Set the fee abstraction paid fee key with a single coin | ||
| return ctx.WithValue( | ||
| keeper.ContextPaidFeesKey{}, | ||
| sdk.NewCoins( | ||
| sdk.NewCoin(TestDenom, sdkmath.NewInt(750_000_000)), | ||
| sdk.NewCoin("atwo", sdkmath.NewInt(750_000_000)), | ||
| ), | ||
| ) | ||
| }, | ||
| errContains: "expected a single coin for EVM refunds, got 2", | ||
| }, | ||
| } | ||
|
|
||
| // Iterate though the test cases | ||
| for _, tc := range testCases { | ||
| suite.Run(tc.name, func() { | ||
| // Generate a cached context to not leak data between tests | ||
| ctx, _ := unitNetwork.GetContext().CacheContext() | ||
|
|
||
| // Apply the malleate function to the context | ||
| if tc.malleate != nil { | ||
| ctx = tc.malleate(ctx) | ||
| } | ||
|
|
||
| vmdb := unitNetwork.GetStateDB() | ||
| vmdb.AddRefund(params.TxGas) | ||
|
Thaleszh marked this conversation as resolved.
Outdated
|
||
|
|
||
| if tc.leftoverGas > DefaultCoreMsgGasUsage { | ||
| return | ||
| } | ||
|
|
||
| initialBalances := unitNetwork.App.GetBankKeeper().GetAllBalances(ctx, feeAddress) | ||
|
|
||
| err = unitNetwork.App.GetEVMKeeper().RefundGas( | ||
| ctx, | ||
| *coreMsg, | ||
| tc.leftoverGas, | ||
| DefaultCoreMsgGasUsage, | ||
| baseDenom, | ||
| ) | ||
|
|
||
| // Check the error | ||
| if tc.errContains != "" { | ||
| suite.Require().ErrorContains(err, tc.errContains, "RefundGas should return an error") | ||
| } else { | ||
| suite.Require().NoError(err, "RefundGas should not return an error") | ||
| } | ||
|
|
||
| // Check the balance change | ||
| if !tc.expectedRefund.Empty() { | ||
| diff := initialBalances.Sub(unitNetwork.App.GetBankKeeper().GetAllBalances(ctx, feeAddress)...) | ||
| for _, coin := range tc.expectedRefund { | ||
| suite.Require().Equal(coin.Amount, diff.AmountOf(coin.Denom)) | ||
| } | ||
| } | ||
| }) | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.