Skip to content

Commit

Permalink
refactor: optimize epoch info kvstore calls
Browse files Browse the repository at this point in the history
  • Loading branch information
Ngoc-Notional committed Jan 17, 2024
1 parent 1801752 commit bf777a4
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 12 deletions.
5 changes: 2 additions & 3 deletions x/feeabs/ante/decorate.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,9 +247,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 {
nativeCoinsFees, err := famfd.feeabsKeeper.CalculateNativeFromIBCCoins(ctx, feeCoins, hostChainConfig)
if err != nil {
return ctx, sdkerrors.Wrapf(errorstypes.ErrInsufficientFee, "insufficient fees")
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
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

0 comments on commit bf777a4

Please sign in to comment.