From f5c26845c1d854077311fa8f959c4ff76117a1a6 Mon Sep 17 00:00:00 2001 From: beer-1 <147697694+beer-1@users.noreply.github.com> Date: Thu, 4 Jan 2024 20:04:27 +0900 Subject: [PATCH] make OPinit to fit staking keeper interface --- x/opchild/ante/fee.go | 7 ++++++- x/opchild/ante/fee_test.go | 6 ++++-- x/opchild/keeper/historical_info.go | 21 ++++++--------------- x/opchild/keeper/historical_info_test.go | 16 +++++++--------- x/opchild/keeper/keeper.go | 2 ++ x/opchild/keeper/staking.go | 4 ++-- x/opchild/keeper/staking_test.go | 3 ++- x/opchild/types/exported.go | 4 +++- 8 files changed, 32 insertions(+), 31 deletions(-) diff --git a/x/opchild/ante/fee.go b/x/opchild/ante/fee.go index e3ca34b6..38e44130 100644 --- a/x/opchild/ante/fee.go +++ b/x/opchild/ante/fee.go @@ -40,7 +40,12 @@ func (mfd MempoolFeeChecker) CheckTxFeeWithMinGasPrices(ctx sdk.Context, tx sdk. minGasPrices := ctx.MinGasPrices() if mfd.keeper != nil { - minGasPrices = CombinedMinGasPrices(minGasPrices, mfd.keeper.MinGasPrices(ctx)) + paramsMinGasPrices, err := mfd.keeper.MinGasPrices(ctx) + if err != nil { + return nil, 0, err + } + + minGasPrices = CombinedMinGasPrices(minGasPrices, paramsMinGasPrices) } if !minGasPrices.IsZero() { diff --git a/x/opchild/ante/fee_test.go b/x/opchild/ante/fee_test.go index e03a395b..72868111 100644 --- a/x/opchild/ante/fee_test.go +++ b/x/opchild/ante/fee_test.go @@ -1,6 +1,8 @@ package ante_test import ( + "context" + "github.com/initia-labs/OPinit/x/opchild/ante" "cosmossdk.io/math" @@ -15,8 +17,8 @@ type TestAnteKeeper struct { minGasPrices sdk.DecCoins } -func (k TestAnteKeeper) MinGasPrices(ctx sdk.Context) sdk.DecCoins { - return k.minGasPrices +func (k TestAnteKeeper) MinGasPrices(ctx context.Context) (sdk.DecCoins, error) { + return k.minGasPrices, nil } func (suite *AnteTestSuite) TestEnsureMempoolFees() { diff --git a/x/opchild/keeper/historical_info.go b/x/opchild/keeper/historical_info.go index 7c11a369..8ce1c3b4 100644 --- a/x/opchild/keeper/historical_info.go +++ b/x/opchild/keeper/historical_info.go @@ -11,17 +11,8 @@ import ( ) // GetHistoricalInfo gets the historical info at a given height -func (k Keeper) GetHistoricalInfo(ctx context.Context, height int64) (*cosmostypes.HistoricalInfo, bool, error) { - historicalInfo, err := k.HistoricalInfos.Get(ctx, height) - if err != nil { - if errors.Is(err, collections.ErrNotFound) { - return nil, false, nil - } - - return nil, false, err - } - - return &historicalInfo, true, nil +func (k Keeper) GetHistoricalInfo(ctx context.Context, height int64) (cosmostypes.HistoricalInfo, error) { + return k.HistoricalInfos.Get(ctx, height) } // SetHistoricalInfo sets the historical info at a given height @@ -51,13 +42,13 @@ func (k Keeper) TrackHistoricalInfo(ctx context.Context) error { // over the historical entries starting from the most recent version to be pruned // and then return at the first empty entry. for i := sdkCtx.BlockHeight() - int64(entryNum); i >= 0; i-- { - _, found, err := k.GetHistoricalInfo(ctx, i) - if err != nil { - return err - } else if found { + _, err := k.GetHistoricalInfo(ctx, i) + if err == nil { if err := k.DeleteHistoricalInfo(ctx, i); err != nil { return err } + } else if err != nil && !errors.Is(err, collections.ErrNotFound) { + return err } else { break } diff --git a/x/opchild/keeper/historical_info_test.go b/x/opchild/keeper/historical_info_test.go index 77d3e7c5..43bc3141 100644 --- a/x/opchild/keeper/historical_info_test.go +++ b/x/opchild/keeper/historical_info_test.go @@ -3,6 +3,7 @@ package keeper_test import ( "testing" + "cosmossdk.io/collections" "github.com/stretchr/testify/require" sdk "github.com/cosmos/cosmos-sdk/types" @@ -22,23 +23,20 @@ func Test_HistoricalInfo(t *testing.T) { input.OPChildKeeper.TrackHistoricalInfo(sdkCtx.WithBlockHeight(2)) input.OPChildKeeper.TrackHistoricalInfo(sdkCtx.WithBlockHeight(3)) - _, found, err := input.OPChildKeeper.GetHistoricalInfo(ctx, 1) - require.NoError(t, err) - require.False(t, found) + _, err = input.OPChildKeeper.GetHistoricalInfo(ctx, 1) + require.ErrorIs(t, err, collections.ErrNotFound) - historicalInfo, found, err := input.OPChildKeeper.GetHistoricalInfo(ctx, 2) + historicalInfo, err := input.OPChildKeeper.GetHistoricalInfo(ctx, 2) require.NoError(t, err) - require.True(t, found) require.Equal(t, cosmostypes.HistoricalInfo{ Header: sdkCtx.WithBlockHeight(2).BlockHeader(), Valset: nil, - }, *historicalInfo) + }, historicalInfo) - historicalInfo, found, err = input.OPChildKeeper.GetHistoricalInfo(ctx, 3) + historicalInfo, err = input.OPChildKeeper.GetHistoricalInfo(ctx, 3) require.NoError(t, err) - require.True(t, found) require.Equal(t, cosmostypes.HistoricalInfo{ Header: sdkCtx.WithBlockHeight(3).BlockHeader(), Valset: nil, - }, *historicalInfo) + }, historicalInfo) } diff --git a/x/opchild/keeper/keeper.go b/x/opchild/keeper/keeper.go index 0800d7d7..bec0f011 100644 --- a/x/opchild/keeper/keeper.go +++ b/x/opchild/keeper/keeper.go @@ -17,6 +17,8 @@ import ( "github.com/initia-labs/OPinit/x/opchild/types" ) +var _ types.AnteKeeper = Keeper{} + type Keeper struct { cdc codec.Codec storeService corestoretypes.KVStoreService diff --git a/x/opchild/keeper/staking.go b/x/opchild/keeper/staking.go index 25d6141e..59c4d9ff 100644 --- a/x/opchild/keeper/staking.go +++ b/x/opchild/keeper/staking.go @@ -29,6 +29,6 @@ func (k Keeper) HistoricalEntries(ctx context.Context) (uint32, error) { } // UnbondingTime - The time duration for unbonding -func (k Keeper) UnbondingTime(ctx context.Context) time.Duration { - return unbondingTime +func (k Keeper) UnbondingTime(ctx context.Context) (time.Duration, error) { + return unbondingTime, nil } diff --git a/x/opchild/keeper/staking_test.go b/x/opchild/keeper/staking_test.go index 5802fcb6..83588b12 100644 --- a/x/opchild/keeper/staking_test.go +++ b/x/opchild/keeper/staking_test.go @@ -36,6 +36,7 @@ func Test_HistoricalEntries(t *testing.T) { func Test_UnbondingTime(t *testing.T) { ctx, input := createDefaultTestInput(t) - unbondingTime := input.OPChildKeeper.UnbondingTime(ctx) + unbondingTime, err := input.OPChildKeeper.UnbondingTime(ctx) + require.NoError(t, err) require.Equal(t, (60 * 60 * 24 * 7 * time.Second), unbondingTime) } diff --git a/x/opchild/types/exported.go b/x/opchild/types/exported.go index fc619175..1de0f35f 100644 --- a/x/opchild/types/exported.go +++ b/x/opchild/types/exported.go @@ -1,6 +1,8 @@ package types import ( + context "context" + tmprotocrypto "github.com/cometbft/cometbft/proto/tendermint/crypto" "cosmossdk.io/math" @@ -27,5 +29,5 @@ type ValidatorI interface { } type AnteKeeper interface { - MinGasPrices(ctx sdk.Context) sdk.DecCoins + MinGasPrices(ctx context.Context) (sdk.DecCoins, error) }