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

Optimize kvstore calls #123

Merged
merged 4 commits into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions x/feeabs/ante/decorate.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,11 @@ func (fadfd FeeAbstractionDeductFeeDecorate) AnteHandle(ctx sdk.Context, tx sdk.
}

feeDenom := fee.GetDenomByIndex(0)
hasHostChainConfig := fadfd.feeabsKeeper.HasHostZoneConfig(ctx, feeDenom)
if !hasHostChainConfig {
hostChainConfig, found := fadfd.feeabsKeeper.GetHostZoneConfig(ctx, feeDenom)
if !found {
return fadfd.normalDeductFeeAnteHandle(ctx, tx, simulate, next, feeTx)
}

hostChainConfig, _ := fadfd.feeabsKeeper.GetHostZoneConfig(ctx, feeDenom)
return fadfd.abstractionDeductFeeHandler(ctx, tx, simulate, next, feeTx, hostChainConfig)
}

Expand Down Expand Up @@ -251,9 +250,8 @@ func (famfd FeeAbstrationMempoolFeeDecorator) AnteHandle(ctx sdk.Context, tx sdk
// TODO: Support more fee token in feeRequired for fee-abstraction
if feeCoinsNonZeroDenom.Len() == 1 {
feeDenom := feeCoinsNonZeroDenom.GetDenomByIndex(0)
hasHostChainConfig := famfd.feeabsKeeper.HasHostZoneConfig(ctx, feeDenom)
if hasHostChainConfig {
hostChainConfig, _ := famfd.feeabsKeeper.GetHostZoneConfig(ctx, feeDenom)
hostChainConfig, found := famfd.feeabsKeeper.GetHostZoneConfig(ctx, feeDenom)
if found {
if hostChainConfig.Frozen {
return ctx, sdkerrors.Wrapf(feeabstypes.ErrHostZoneFrozen, "cannot deduct fee as host zone is frozen")
}
Expand Down
13 changes: 8 additions & 5 deletions x/feeabs/keeper/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,17 @@ func (k Keeper) SetHostZoneConfig(ctx sdk.Context, chainConfig types.HostChainFe
}

func (k Keeper) DeleteHostZoneConfig(ctx sdk.Context, ibcDenom string) error {
store := ctx.KVStore(k.storeKey)
hostZoneConfig, ok := k.GetHostZoneConfig(ctx, ibcDenom)
if ok {
key := types.GetKeyHostZoneConfigByOsmosisIBCDenom(hostZoneConfig.OsmosisPoolTokenDenomIn)
store.Delete(key)
if !ok {
return types.ErrHostZoneConfigNotFound
}

key := types.GetKeyHostZoneConfigByFeeabsIBCDenom(ibcDenom)
store := ctx.KVStore(k.storeKey)

key := types.GetKeyHostZoneConfigByOsmosisIBCDenom(hostZoneConfig.OsmosisPoolTokenDenomIn)
store.Delete(key)

key = types.GetKeyHostZoneConfigByFeeabsIBCDenom(ibcDenom)
store.Delete(key)

return nil
Expand Down
8 changes: 4 additions & 4 deletions x/feeabs/keeper/epoch.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,18 @@ func (k Keeper) HasEpochInfo(ctx sdk.Context, identifier string) bool {
}

// GetEpochInfo returns epoch info by identifier.
func (k Keeper) GetEpochInfo(ctx sdk.Context, identifier string) types.EpochInfo {
func (k Keeper) GetEpochInfo(ctx sdk.Context, identifier string) (types.EpochInfo, bool) {
epoch := types.EpochInfo{}
store := ctx.KVStore(k.storeKey)
b := store.Get(append(types.KeyPrefixEpoch, []byte(identifier)...))
if b == nil {
return epoch
return epoch, false
}
err := proto.Unmarshal(b, &epoch)
if err != nil {
panic(err)
}
return epoch
return epoch, true
}

// AddEpochInfo adds a new epoch info. Will return an error if the epoch fails validation,
Expand All @@ -41,7 +41,7 @@ func (k Keeper) AddEpochInfo(ctx sdk.Context, epoch types.EpochInfo) error {
return err
}
// Check if identifier already exists
if (k.GetEpochInfo(ctx, epoch.Identifier) != types.EpochInfo{}) {
if k.HasEpochInfo(ctx, epoch.Identifier) {
return fmt.Errorf("epoch with identifier %s already exists", epoch.Identifier)
}

Expand Down
53 changes: 53 additions & 0 deletions x/feeabs/keeper/epoch_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package keeper_test

import (
"testing"
"time"

"github.com/stretchr/testify/require"

sdk "github.com/cosmos/cosmos-sdk/types"

apphelpers "github.com/osmosis-labs/fee-abstraction/v7/app/helpers"
feeabskeeper "github.com/osmosis-labs/fee-abstraction/v7/x/feeabs/keeper"
"github.com/osmosis-labs/fee-abstraction/v7/x/feeabs/types"
)

func createEpoch(t *testing.T, keeper *feeabskeeper.Keeper, ctx sdk.Context) types.EpochInfo {
t.Helper()
expected := types.EpochInfo{
Identifier: "Test",
StartTime: time.Now().UTC(),
Duration: 10,
CurrentEpoch: 0,
CurrentEpochStartTime: time.Now().UTC(),
EpochCountingStarted: false,
CurrentEpochStartHeight: 0,
}
err := keeper.AddEpochInfo(ctx, expected)
require.NoError(t, err)

err = keeper.AddEpochInfo(ctx, expected)
require.Error(t, err, "epoch with identifier Test already exists")

return expected
}

func TestGetEpochInfo(t *testing.T) {
app := apphelpers.Setup(t, false, 1)
ctx := apphelpers.NewContextForApp(*app)
expected := createEpoch(t, &app.FeeabsKeeper, ctx)
got, found := app.FeeabsKeeper.GetEpochInfo(ctx, expected.Identifier)
require.True(t, found)
require.Equal(t, expected.StartTime, got.StartTime)
require.Equal(t, expected.Duration, got.Duration)
require.Equal(t, expected.EpochCountingStarted, got.EpochCountingStarted)
}

func TestHasEpochInfo(t *testing.T) {
app := apphelpers.Setup(t, false, 1)
ctx := apphelpers.NewContextForApp(*app)
expected := createEpoch(t, &app.FeeabsKeeper, ctx)
found := app.FeeabsKeeper.HasEpochInfo(ctx, expected.Identifier)
require.True(t, found)
}
8 changes: 3 additions & 5 deletions x/feeabs/keeper/ibc.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,14 +257,12 @@ func (k Keeper) executeTransferMsg(ctx sdk.Context, transferMsg *transfertypes.M
}

func (k Keeper) handleOsmosisIbcQuery(ctx sdk.Context) error {
hasQueryEpochInfo := k.HasEpochInfo(ctx, types.DefaultQueryEpochIdentifier)
if !hasQueryEpochInfo {
// set startTime for query twap
queryTwapEpochInfo, found := k.GetEpochInfo(ctx, types.DefaultQueryEpochIdentifier)
if !found {
k.Logger(ctx).Error(fmt.Sprintf("Don't have query epoch information: %s", types.DefaultQueryEpochIdentifier))
return nil
}

// set startTime for query twap
queryTwapEpochInfo := k.GetEpochInfo(ctx, types.DefaultQueryEpochIdentifier)
startTime := ctx.BlockTime().Add(-queryTwapEpochInfo.Duration)
k.Logger(ctx).Info(fmt.Sprintf("Start time: %v", startTime.Unix()))

Expand Down
24 changes: 5 additions & 19 deletions x/feeabs/keeper/proposal.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,41 +7,27 @@ import (
)

func (k Keeper) AddHostZoneProposal(ctx sdk.Context, p *types.AddHostZoneProposal) error {
_, found := k.GetHostZoneConfig(ctx, p.HostChainConfig.IbcDenom)
if found {
if k.HasHostZoneConfig(ctx, p.HostChainConfig.IbcDenom) {
return types.ErrDuplicateHostZoneConfig
}

err := k.SetHostZoneConfig(ctx, *p.HostChainConfig)
if err != nil {
if err := k.SetHostZoneConfig(ctx, *p.HostChainConfig); err != nil {
return err
}

return nil
}

func (k Keeper) DeleteHostZoneProposal(ctx sdk.Context, p *types.DeleteHostZoneProposal) error {
_, found := k.GetHostZoneConfig(ctx, p.IbcDenom)
if !found {
return types.ErrHostZoneConfigNotFound
}

err := k.DeleteHostZoneConfig(ctx, p.IbcDenom)
if err != nil {
return err
}

return nil
return k.DeleteHostZoneConfig(ctx, p.IbcDenom)
}

func (k Keeper) SetHostZoneProposal(ctx sdk.Context, p *types.SetHostZoneProposal) error {
_, found := k.GetHostZoneConfig(ctx, p.HostChainConfig.IbcDenom)
if !found {
if !k.HasHostZoneConfig(ctx, p.HostChainConfig.IbcDenom) {
return types.ErrHostZoneConfigNotFound
}

err := k.SetHostZoneConfig(ctx, *p.HostChainConfig)
if err != nil {
if err := k.SetHostZoneConfig(ctx, *p.HostChainConfig); err != nil {
return err
}

Expand Down
2 changes: 1 addition & 1 deletion x/feeabs/types/build_memo.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package types

import (
"encoding/json"
fmt "fmt"
"fmt"
"time"
)

Expand Down