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

feat: add indexer tests #115

Merged
merged 16 commits into from
Dec 3, 2024
1 change: 1 addition & 0 deletions .codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,4 @@ ignore:
- "x/ibc/testing"
- "x/evm/contracts"
- "**/*.sol"
- "tests/"
4 changes: 3 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ jobs:
make build
- name: test & coverage report creation
run: |
go test ./... -mod=readonly -timeout 12m -race -coverprofile=coverage.txt -covermode=atomic -tags='ledger test_ledger_mock'
# temporary disable -race flag until this issue fixed:
# - https://github.com/cosmos/cosmos-sdk/issues/22650
go test ./... -mod=readonly -timeout 12m -coverprofile=coverage.txt -covermode=atomic -tags='ledger test_ledger_mock'
if: env.GIT_DIFF
env:
GIT_DIFF: ${{ env.GIT_DIFF }}
Expand Down
7 changes: 7 additions & 0 deletions app/ante/ante_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ func (suite *AnteTestSuite) createTestApp(tempDir string) (*minievmapp.MinitiaAp
err = app.EVMKeeper.Params.Set(ctx, params)
suite.NoError(err)

err = app.EVMKeeper.Initialize(ctx)
suite.NoError(err)

return app, ctx
}

Expand Down Expand Up @@ -131,3 +134,7 @@ func (suite *AnteTestSuite) CreateTestTx(privs []cryptotypes.PrivKey, accNums []
func TestAnteTestSuite(t *testing.T) {
suite.Run(t, new(AnteTestSuite))
}

func noopAnteHandler(ctx sdk.Context, _ sdk.Tx, _ bool) (sdk.Context, error) {
return ctx, nil
}
27 changes: 15 additions & 12 deletions app/ante/fee.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ import (
"github.com/cosmos/cosmos-sdk/x/auth/types"
)

// feeDeductionGasAmount is a estimated gas amount of fee payment
const feeDeductionGasAmount = 250_000

// GasFreeFeeDecorator is a decorator that sets the gas meter to infinite before calling the inner DeductFeeDecorator
// and then resets the gas meter to the original value after the inner DeductFeeDecorator is called.
//
Expand All @@ -34,6 +31,9 @@ func NewGasFreeFeeDecorator(
}
}

// gasLimitForFeeDeduction is the gas limit used for fee deduction.
const gasLimitForFeeDeduction = 1_000_000

func (fd GasFreeFeeDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (sdk.Context, error) {
feeTx, ok := tx.(sdk.FeeTx)
if !ok {
Expand All @@ -43,19 +43,22 @@ func (fd GasFreeFeeDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bo
fees := feeTx.GetFee()
feeDenom, err := fd.ek.GetFeeDenom(ctx.WithGasMeter(storetypes.NewInfiniteGasMeter()))
if !(err == nil && len(fees) == 1 && fees[0].Denom == feeDenom) {
if simulate && fees.IsZero() {
// Charge gas for fee deduction simulation
//
// At gas simulation normally gas amount is zero, so the gas is not charged in the simulation.
ctx.GasMeter().ConsumeGas(feeDeductionGasAmount, "fee deduction")
}

return fd.inner.AnteHandle(ctx, tx, simulate, next)
}

// If the fee contains only one denom and it is the fee denom, set the gas meter to infinite
// to avoid gas consumption for fee deduction.
gasMeter := ctx.GasMeter()
ctx, err = fd.inner.AnteHandle(ctx.WithGasMeter(storetypes.NewInfiniteGasMeter()), tx, simulate, next)
return ctx.WithGasMeter(gasMeter), err
ctx, err = fd.inner.AnteHandle(ctx.WithGasMeter(storetypes.NewGasMeter(gasLimitForFeeDeduction)), tx, simulate, noopAnteHandler)
// restore the original gas meter
ctx = ctx.WithGasMeter(gasMeter)
if err != nil {
return ctx, err
}

return next(ctx, tx, simulate)
}

func noopAnteHandler(ctx sdk.Context, _ sdk.Tx, _ bool) (sdk.Context, error) {
return ctx, nil
}
19 changes: 11 additions & 8 deletions app/ante/fee_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ func (suite *AnteTestSuite) Test_NotSpendingGasForTxWithFeeDenom() {
gasLimit := uint64(200_000)
atomFeeAmount := sdk.NewCoins(sdk.NewCoin("atom", math.NewInt(200)))

suite.app.EVMKeeper.ERC20Keeper().MintCoins(suite.ctx, addr1, feeAmount.MulInt(math.NewInt(10)))
suite.app.EVMKeeper.ERC20Keeper().MintCoins(suite.ctx, addr1, atomFeeAmount.MulInt(math.NewInt(10)))
err := suite.app.EVMKeeper.ERC20Keeper().MintCoins(suite.ctx, addr1, feeAmount.MulInt(math.NewInt(10)))
suite.Require().NoError(err)
err = suite.app.EVMKeeper.ERC20Keeper().MintCoins(suite.ctx, addr1, atomFeeAmount.MulInt(math.NewInt(10)))
suite.Require().NoError(err)

// Case 1. only fee denom
suite.Require().NoError(suite.txBuilder.SetMsgs(msg))
Expand All @@ -40,34 +42,35 @@ func (suite *AnteTestSuite) Test_NotSpendingGasForTxWithFeeDenom() {
suite.Require().NoError(err)

gasMeter := storetypes.NewGasMeter(500000)
feeAnte.AnteHandle(suite.ctx.WithGasMeter(gasMeter), tx, false, nil)
_, err = feeAnte.AnteHandle(suite.ctx.WithGasMeter(gasMeter), tx, false, noopAnteHandler)
suite.Require().NoError(err)
suite.Require().Zero(gasMeter.GasConsumed(), "should not consume gas for fee deduction")

// Case 2. fee denom and other denom
suite.txBuilder.SetFeeAmount(feeAmount.Add(atomFeeAmount...))

gasMeter = storetypes.NewGasMeter(500000)
feeAnte.AnteHandle(suite.ctx.WithGasMeter(gasMeter), tx, false, nil)
feeAnte.AnteHandle(suite.ctx.WithGasMeter(gasMeter), tx, false, noopAnteHandler)
suite.Require().NotZero(gasMeter.GasConsumed(), "should consume gas for fee deduction")

// Case 3. other denom
suite.txBuilder.SetFeeAmount(feeAmount.Add(atomFeeAmount...))

gasMeter = storetypes.NewGasMeter(500000)
feeAnte.AnteHandle(suite.ctx.WithGasMeter(gasMeter), tx, false, nil)
feeAnte.AnteHandle(suite.ctx.WithGasMeter(gasMeter), tx, false, noopAnteHandler)
suite.Require().NotZero(gasMeter.GasConsumed(), "should consume gas for fee deduction")

// Case 4. no fee
suite.txBuilder.SetFeeAmount(sdk.NewCoins())

gasMeter = storetypes.NewGasMeter(500000)
feeAnte.AnteHandle(suite.ctx.WithGasMeter(gasMeter), tx, false, nil)
feeAnte.AnteHandle(suite.ctx.WithGasMeter(gasMeter), tx, false, noopAnteHandler)
suite.Require().NotZero(gasMeter.GasConsumed(), "should consume gas for fee deduction")

// Case 5. simulate gas consumption
suite.txBuilder.SetFeeAmount(sdk.NewCoins())

gasMeter = storetypes.NewGasMeter(500000)
feeAnte.AnteHandle(suite.ctx.WithGasMeter(gasMeter), tx, true, nil)
suite.Require().Greater(gasMeter.GasConsumed(), uint64(250000), "should consume gas for fee deduction")
feeAnte.AnteHandle(suite.ctx.WithGasMeter(gasMeter), tx, true, noopAnteHandler)
suite.Require().NotZero(gasMeter.GasConsumed(), "should consume gas for fee deduction")
}
19 changes: 12 additions & 7 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,18 +249,19 @@ func NewMinitiaApp(
}

// setup indexer
if evmIndexer, kvIndexerKeeper, kvIndexerModule, streamingManager, err := setupIndexer(app, appOpts, indexerDB, kvindexerDB); err != nil {
evmIndexer, kvIndexerKeeper, kvIndexerModule, streamingManager, err := setupIndexer(app, appOpts, indexerDB, kvindexerDB)
if err != nil {
tmos.Exit(err.Error())
} else if kvIndexerKeeper != nil && kvIndexerModule != nil && streamingManager != nil {
} else if kvIndexerKeeper != nil && kvIndexerModule != nil {
// register kvindexer keeper and module, and register services.
app.SetKVIndexer(kvIndexerKeeper, kvIndexerModule)
}

// register evm indexer
app.SetEVMIndexer(evmIndexer)
// register evm indexer
app.SetEVMIndexer(evmIndexer)

// override base-app's streaming manager
app.SetStreamingManager(*streamingManager)
}
// override base-app's streaming manager
app.SetStreamingManager(*streamingManager)

// register upgrade handler for later use
app.RegisterUpgradeHandlers(app.configurator)
Expand Down Expand Up @@ -602,6 +603,10 @@ func (app *MinitiaApp) Close() error {
return err
}

if app.evmIndexer != nil {
app.evmIndexer.Stop()
}

return nil
}

Expand Down
3 changes: 0 additions & 3 deletions app/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,4 @@ const (
moveMsgPublishModuleBundle = "/initia.move.v1.MsgPublish"
moveMsgExecuteEntryFunction = "/initia.move.v1.MsgExecute"
moveMsgExecuteScript = "/initia.move.v1.MsgScript"

// UpgradeName gov proposal name
UpgradeName = "0.0.0"
)
2 changes: 1 addition & 1 deletion app/indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func setupIndexer(
indexerDB, kvindexerDB dbm.DB,
) (evmindexer.EVMIndexer, *kvindexerkeeper.Keeper, *kvindexermodule.AppModuleBasic, *storetypes.StreamingManager, error) {
// setup evm indexer
evmIndexer, err := evmindexer.NewEVMIndexer(indexerDB, app.appCodec, app.Logger(), app.txConfig, app.EVMKeeper, app.OPChildKeeper)
evmIndexer, err := evmindexer.NewEVMIndexer(indexerDB, app.appCodec, app.Logger(), app.txConfig, app.EVMKeeper)
if err != nil {
return nil, nil, nil, nil, err
}
Expand Down
12 changes: 11 additions & 1 deletion app/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
tmtypes "github.com/cometbft/cometbft/types"
dbm "github.com/cosmos/cosmos-db"

"cosmossdk.io/math"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
Expand All @@ -22,7 +23,9 @@ import (

opchildtypes "github.com/initia-labs/OPinit/x/opchild/types"

"github.com/initia-labs/minievm/types"
evmconfig "github.com/initia-labs/minievm/x/evm/config"
evmtypes "github.com/initia-labs/minievm/x/evm/types"
)

// defaultConsensusParams defines the default Tendermint consensus params used in
Expand Down Expand Up @@ -65,7 +68,7 @@ func setup(db *dbm.DB, withGenesis bool) (*MinitiaApp, GenesisState) {
)

if withGenesis {
return app, NewDefaultGenesisState(encCdc.Codec, app.BasicModuleManager, sdk.DefaultBondDenom)
return app, NewDefaultGenesisState(encCdc.Codec, app.BasicModuleManager, types.BaseDenom)
beer-1 marked this conversation as resolved.
Show resolved Hide resolved
}

return app, GenesisState{}
Expand Down Expand Up @@ -128,11 +131,18 @@ func SetupWithGenesisAccounts(
app.AppCodec().MustUnmarshalJSON(genesisState[opchildtypes.ModuleName], &opchildGenesis)
opchildGenesis.Params.Admin = sdk.AccAddress(valSet.Validators[0].Address.Bytes()).String()
opchildGenesis.Params.BridgeExecutors = []string{sdk.AccAddress(valSet.Validators[0].Address.Bytes()).String()}
opchildGenesis.Params.MinGasPrices = sdk.NewDecCoins(sdk.NewDecCoin(types.BaseDenom, math.NewInt(1_000_000_000)))

// set validators and delegations
opchildGenesis = *opchildtypes.NewGenesisState(opchildGenesis.Params, validators, nil)
genesisState[opchildtypes.ModuleName] = app.AppCodec().MustMarshalJSON(&opchildGenesis)

// set evm genesis params
var evmGenesis evmtypes.GenesisState
app.AppCodec().MustUnmarshalJSON(genesisState[evmtypes.ModuleName], &evmGenesis)
evmGenesis.Params.GasRefundRatio = math.LegacyZeroDec()
genesisState[evmtypes.ModuleName] = app.AppCodec().MustMarshalJSON(&evmGenesis)

// update total supply
bankGenesis := banktypes.NewGenesisState(banktypes.DefaultGenesisState().Params, balances, sdk.NewCoins(), []banktypes.Metadata{}, []banktypes.SendEnabled{})
genesisState[banktypes.ModuleName] = app.AppCodec().MustMarshalJSON(bankGenesis)
Expand Down
12 changes: 7 additions & 5 deletions app/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
opchildtypes "github.com/initia-labs/OPinit/x/opchild/types"
)

const upgradeName = "0.6.6"
const upgradeName = "0.6.7"
beer-1 marked this conversation as resolved.
Show resolved Hide resolved

// RegisterUpgradeHandlers returns upgrade handlers
func (app *MinitiaApp) RegisterUpgradeHandlers(cfg module.Configurator) {
Expand All @@ -38,11 +38,13 @@ func (app *MinitiaApp) RegisterUpgradeHandlers(cfg module.Configurator) {
}

// set non-zero default values for new params
params.HookMaxGas = opchildtypes.DefaultHookMaxGas
if params.HookMaxGas == 0 {
params.HookMaxGas = opchildtypes.DefaultHookMaxGas

err = app.OPChildKeeper.SetParams(ctx, params)
if err != nil {
return nil, err
err = app.OPChildKeeper.SetParams(ctx, params)
if err != nil {
return nil, err
}
}

//////////////////////////// MINIEVM ///////////////////////////////////
Expand Down
4 changes: 4 additions & 0 deletions indexer/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ func (e *EVMIndexerImpl) ListenCommit(ctx context.Context, res abci.ResponseComm

// IndexBlock implements EVMIndexer.
func (e *EVMIndexerImpl) ListenFinalizeBlock(ctx context.Context, req abci.RequestFinalizeBlock, res abci.ResponseFinalizeBlock) error {
if !e.enabled {
return nil
}

sdkCtx := sdk.UnwrapSDKContext(ctx)

// load base fee from evm keeper
Expand Down
Loading
Loading