Skip to content

Commit

Permalink
migrate from frozen to more generic host chain fee abs connection sta…
Browse files Browse the repository at this point in the history
…tus (#156)

* initial migrate from frozen to more generic host chain fee abs connection status

* more generic set state host zone

* add code for exponential backoff, no test

* add unit test

* linting

* allcaps

* error handling

---------

Co-authored-by: Jacob Gadikian <[email protected]>
  • Loading branch information
nghuyenthevinh2000 and faddat authored Mar 8, 2024
1 parent b032641 commit d2b5f20
Show file tree
Hide file tree
Showing 28 changed files with 742 additions and 157 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ build-integration-binary:
###############################################################################
### Proto ###
###############################################################################
PROTO_BUILDER_IMAGE=ghcr.io/cosmos/proto-builder:0.13.5
PROTO_BUILDER_IMAGE=ghcr.io/cosmos/proto-builder:0.14.0

proto-all: proto-format proto-gen

Expand Down
5 changes: 5 additions & 0 deletions proto/feeabstraction/feeabs/v1beta1/epoch.proto
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,9 @@ message EpochInfo {
// current_epoch_start_height is the block height at which the current epoch
// started. (The block height at which the timer last ticked)
int64 current_epoch_start_height = 8;
}

message ExponentialBackoff {
int64 jump = 1;
int64 future_epoch = 2;
}
10 changes: 8 additions & 2 deletions proto/feeabstraction/feeabs/v1beta1/proposal.proto
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,21 @@ import "google/api/annotations.proto";

option go_package = "github.com/osmosis-labs/fee-abstraction/v7/x/feeabs/types";

enum HostChainFeeAbsStatus {
UPDATED = 0;
OUTDATED = 1;
FROZEN = 2;
}

message HostChainFeeAbsConfig {
// ibc token is allowed to be used as fee token
string ibc_denom = 1 [ (gogoproto.moretags) = "yaml:\"allowed_token\"" ];
// token_in in cross_chain swap contract.
string osmosis_pool_token_denom_in = 2;
// pool id
uint64 pool_id = 3;
// Frozen
bool frozen = 4;
// Host chain fee abstraction connection status
HostChainFeeAbsStatus status = 4;
}

message AddHostZoneProposal {
Expand Down
3 changes: 2 additions & 1 deletion tests/interchaintest/feeabs/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import (
"context"
"encoding/json"

feeabstypes "github.com/osmosis-labs/fee-abstraction/v7/x/feeabs/types"
"github.com/strangelove-ventures/interchaintest/v7/chain/cosmos"

feeabstypes "github.com/osmosis-labs/fee-abstraction/v7/x/feeabs/types"
)

func QueryHostZoneConfigWithDenom(c *cosmos.CosmosChain, ctx context.Context, denom string) (*HostChainFeeAbsConfigResponse, error) {
Expand Down
1 change: 1 addition & 0 deletions tests/interchaintest/feeabs/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package feeabs

import (
"context"

"github.com/strangelove-ventures/interchaintest/v7/chain/cosmos"
)

Expand Down
3 changes: 2 additions & 1 deletion tests/interchaintest/feeabs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ import (
sdktypes "github.com/cosmos/cosmos-sdk/types"
paramsutils "github.com/cosmos/cosmos-sdk/x/params/client/utils"
transfertypes "github.com/cosmos/ibc-go/v7/modules/apps/transfer/types"
feeabsCli "github.com/osmosis-labs/fee-abstraction/tests/interchaintest/feeabs"
"github.com/strangelove-ventures/interchaintest/v7/chain/cosmos"
"github.com/strangelove-ventures/interchaintest/v7/ibc"
"github.com/strangelove-ventures/interchaintest/v7/testutil"
"github.com/stretchr/testify/require"

feeabsCli "github.com/osmosis-labs/fee-abstraction/tests/interchaintest/feeabs"
)

func TestFeeAbs(t *testing.T) {
Expand Down
3 changes: 2 additions & 1 deletion tests/interchaintest/host_zone_proposal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import (
"fmt"
"testing"

feeabsCli "github.com/osmosis-labs/fee-abstraction/tests/interchaintest/feeabs"
"github.com/strangelove-ventures/interchaintest/v7/chain/cosmos"
"github.com/stretchr/testify/require"

feeabsCli "github.com/osmosis-labs/fee-abstraction/tests/interchaintest/feeabs"
)

func TestHostZoneProposal(t *testing.T) {
Expand Down
3 changes: 2 additions & 1 deletion tests/interchaintest/packet_foward_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ import (

sdktypes "github.com/cosmos/cosmos-sdk/types"
transfertypes "github.com/cosmos/ibc-go/v7/modules/apps/transfer/types"
feeabsCli "github.com/osmosis-labs/fee-abstraction/tests/interchaintest/feeabs"
"github.com/strangelove-ventures/interchaintest/v7/chain/cosmos"
"github.com/strangelove-ventures/interchaintest/v7/testutil"
"github.com/stretchr/testify/require"

feeabsCli "github.com/osmosis-labs/fee-abstraction/tests/interchaintest/feeabs"
)

func TestPacketForwardMiddleware(t *testing.T) {
Expand Down
5 changes: 3 additions & 2 deletions tests/interchaintest/query_osmosis_twap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ import (
sdktypes "github.com/cosmos/cosmos-sdk/types"
paramsutils "github.com/cosmos/cosmos-sdk/x/params/client/utils"
transfertypes "github.com/cosmos/ibc-go/v7/modules/apps/transfer/types"
feeabsCli "github.com/osmosis-labs/fee-abstraction/tests/interchaintest/feeabs"
"github.com/strangelove-ventures/interchaintest/v7/chain/cosmos"
"github.com/strangelove-ventures/interchaintest/v7/testutil"
"github.com/stretchr/testify/require"

feeabsCli "github.com/osmosis-labs/fee-abstraction/tests/interchaintest/feeabs"
)

func TestQueryOsmosisTwap(t *testing.T) {
Expand Down Expand Up @@ -154,5 +155,5 @@ func TestQueryOsmosisTwap(t *testing.T) {
twap, err := feeabsCli.QueryOsmosisArithmeticTwap(feeabs, ctx, uatomOnFeeabs)
fmt.Println(err)
fmt.Println(twap)
//require.NoError(t, err)
// require.NoError(t, err)
}
3 changes: 2 additions & 1 deletion tests/interchaintest/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
sdktypes "github.com/cosmos/cosmos-sdk/types"
moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil"
"github.com/icza/dyno"
feeabstype "github.com/osmosis-labs/fee-abstraction/v7/x/feeabs/types"
"github.com/strangelove-ventures/interchaintest/v7"
"github.com/strangelove-ventures/interchaintest/v7/chain/cosmos"
"github.com/strangelove-ventures/interchaintest/v7/chain/cosmos/wasm"
Expand All @@ -21,6 +20,8 @@ import (
"github.com/strangelove-ventures/interchaintest/v7/testutil"
"github.com/stretchr/testify/require"
"go.uber.org/zap/zaptest"

feeabstype "github.com/osmosis-labs/fee-abstraction/v7/x/feeabs/types"
)

type HasPacketForwarding struct {
Expand Down
14 changes: 12 additions & 2 deletions x/feeabs/ante/decorate.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,14 @@ func (fadfd FeeAbstractionDeductFeeDecorate) normalDeductFeeAnteHandle(
// IBC tokens and deducts the fees accordingly if the transaction involves IBC tokens
// and the host chain configuration is set.
func (fadfd FeeAbstractionDeductFeeDecorate) abstractionDeductFeeHandler(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler, feeTx sdk.FeeTx, hostChainConfig feeabstypes.HostChainFeeAbsConfig) (newCtx sdk.Context, err error) {
if hostChainConfig.Frozen {
if hostChainConfig.Status == feeabstypes.HostChainFeeAbsStatus_FROZEN {
return ctx, sdkerrors.Wrap(feeabstypes.ErrHostZoneFrozen, "cannot deduct fee as host zone is frozen")
}

if hostChainConfig.Status == feeabstypes.HostChainFeeAbsStatus_OUTDATED {
return ctx, sdkerrors.Wrap(feeabstypes.ErrHostZoneOutdated, "cannot deduct fee as host zone is outdated")
}

fee := feeTx.GetFee()
feePayer := feeTx.FeePayer()
feeGranter := feeTx.FeeGranter()
Expand Down Expand Up @@ -268,9 +273,14 @@ func (famfd FeeAbstrationMempoolFeeDecorator) AnteHandle(ctx sdk.Context, tx sdk
feeDenom := feeCoinsNonZeroDenom.GetDenomByIndex(0)
hostChainConfig, found := famfd.feeabsKeeper.GetHostZoneConfig(ctx, feeDenom)
if found {
if hostChainConfig.Frozen {
if hostChainConfig.Status == feeabstypes.HostChainFeeAbsStatus_FROZEN {
return ctx, sdkerrors.Wrapf(feeabstypes.ErrHostZoneFrozen, "cannot deduct fee as host zone is frozen")
}

if hostChainConfig.Status == feeabstypes.HostChainFeeAbsStatus_OUTDATED {
return ctx, sdkerrors.Wrapf(feeabstypes.ErrHostZoneOutdated, "cannot deduct fee as host zone is outdated")
}

nativeCoinsFees, err := famfd.feeabsKeeper.CalculateNativeFromIBCCoins(ctx, feeCoinsNonZeroDenom, hostChainConfig)
if err != nil {
return ctx, sdkerrors.Wrapf(errorstypes.ErrInsufficientFee, "insufficient fees")
Expand Down
4 changes: 2 additions & 2 deletions x/feeabs/client/cli/tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func TestParseProposal(t *testing.T) {
IbcDenom: "ibc/123",
OsmosisPoolTokenDenomIn: "ibc/456",
PoolId: 1,
Frozen: false,
Status: types.HostChainFeeAbsStatus_UPDATED,
}
cdc := codec.NewLegacyAmino()
okJSON := testutil.WriteToNewTempFile(t, `
Expand All @@ -33,7 +33,7 @@ func TestParseProposal(t *testing.T) {
"crosschain_swap_address":"osmo123456",
"pool_id": "1",
"is_osmosis": false,
"frozen": false,
"status": 0,
"osmosis_query_channel": "channel-3"
},
Expand Down
2 changes: 1 addition & 1 deletion x/feeabs/keeper/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (k Keeper) BeginBlocker(ctx sdk.Context) {
sdk.NewAttribute(types.AttributeEpochStartTime, fmt.Sprintf("%d", epochInfo.CurrentEpochStartTime.Unix())),
),
)
k.setEpochInfo(ctx, epochInfo)
k.SetEpochInfo(ctx, epochInfo)

return false
})
Expand Down
85 changes: 69 additions & 16 deletions x/feeabs/keeper/config.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,78 @@
package keeper

import (
"fmt"

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

"github.com/osmosis-labs/fee-abstraction/v7/x/feeabs/types"
)

func (k Keeper) IncreaseBlockDelayToQuery(ctx sdk.Context, ibcDenom string) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.StoreExponentialBackoff)

// must have host zone
if !k.HasHostZoneConfig(ctx, ibcDenom) {
panic("host zone config not found")
}

// must have query epoch info
currentEpoch, exist := k.GetEpochInfo(ctx, types.DefaultQueryEpochIdentifier)
if !exist {
panic("epoch not found")
}

// get current exponential backoff
currentJump := k.GetBlockDelayToQuery(ctx, ibcDenom).Jump
nextJump := currentJump * 2
if nextJump > types.ExponentialMaxJump {
nextJump = types.ExponentialMaxJump
}

fmt.Println("currentJump", currentJump, "nextJump", nextJump, "currentEpoch.CurrentEpoch", currentEpoch.CurrentEpoch, "nextEpoch", currentEpoch.CurrentEpoch+nextJump)

next := &types.ExponentialBackoff{
Jump: nextJump,
FutureEpoch: currentEpoch.CurrentEpoch + nextJump,
}

store.Set([]byte(ibcDenom), k.cdc.MustMarshal(next))
}

func (k Keeper) ResetBlockDelayToQuery(ctx sdk.Context, ibcDenom string) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.StoreExponentialBackoff)

// must have host zone
if !k.HasHostZoneConfig(ctx, ibcDenom) {
panic("host zone config not found")
}

// FutureEpoch = 0, current epoch will always be greater, thus always querying twap
next := &types.ExponentialBackoff{
Jump: 1,
FutureEpoch: 0,
}

store.Set([]byte(ibcDenom), k.cdc.MustMarshal(next))
}

func (k Keeper) GetBlockDelayToQuery(ctx sdk.Context, ibcDenom string) types.ExponentialBackoff {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.StoreExponentialBackoff)
bz := store.Get([]byte(ibcDenom))
if bz == nil {
return types.ExponentialBackoff{
Jump: 1,
FutureEpoch: 0,
}
}

var next types.ExponentialBackoff
k.cdc.MustUnmarshal(bz, &next)

return next
}

func (k Keeper) HasHostZoneConfig(ctx sdk.Context, ibcDenom string) bool {
store := ctx.KVStore(k.storeKey)
key := types.GetKeyHostZoneConfigByFeeabsIBCDenom(ibcDenom)
Expand Down Expand Up @@ -100,26 +167,12 @@ func (k Keeper) IterateHostZone(ctx sdk.Context, cb func(hostZoneConfig types.Ho
}
}

func (k Keeper) FreezeHostZoneByIBCDenom(ctx sdk.Context, ibcDenom string) error {
hostChainConfig, found := k.GetHostZoneConfig(ctx, ibcDenom)
if !found {
return types.ErrHostZoneConfigNotFound
}
hostChainConfig.Frozen = true
err := k.SetHostZoneConfig(ctx, hostChainConfig)
if err != nil {
return err
}

return nil
}

func (k Keeper) UnFreezeHostZoneByIBCDenom(ctx sdk.Context, ibcDenom string) error {
func (k Keeper) SetStateHostZoneByIBCDenom(ctx sdk.Context, ibcDenom string, state types.HostChainFeeAbsStatus) error {
hostChainConfig, found := k.GetHostZoneConfig(ctx, ibcDenom)
if !found {
return types.ErrHostZoneConfigNotFound
}
hostChainConfig.Frozen = false
hostChainConfig.Status = state
err := k.SetHostZoneConfig(ctx, hostChainConfig)
if err != nil {
return err
Expand Down
10 changes: 5 additions & 5 deletions x/feeabs/keeper/epoch.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ func (k Keeper) AddEpochInfo(ctx sdk.Context, epoch types.EpochInfo) error {
epoch.StartTime = ctx.BlockTime()
}
epoch.CurrentEpochStartHeight = ctx.BlockHeight()
k.setEpochInfo(ctx, epoch)
k.SetEpochInfo(ctx, epoch)
return nil
}

// setEpochInfo set epoch info.
func (k Keeper) setEpochInfo(ctx sdk.Context, epoch types.EpochInfo) {
// SetEpochInfo set epoch info.
func (k Keeper) SetEpochInfo(ctx sdk.Context, epoch types.EpochInfo) {
store := ctx.KVStore(k.storeKey)
value, err := proto.Marshal(&epoch)
if err != nil {
Expand Down Expand Up @@ -102,10 +102,10 @@ func (k Keeper) AfterEpochEnd(ctx sdk.Context, epochIdentifier string) {
switch epochIdentifier {
case types.DefaultQueryEpochIdentifier:
k.Logger(ctx).Info("Epoch interchain query TWAP")
k.executeAllHostChainTWAPQuery(ctx)
k.ExecuteAllHostChainTWAPQuery(ctx)
case types.DefaultSwapEpochIdentifier:
k.Logger(ctx).Info("Epoch cross chain swap")
k.executeAllHostChainSwap(ctx)
k.ExecuteAllHostChainSwap(ctx)
default:
k.Logger(ctx).Error(fmt.Sprintf("Unknown epoch %s", epochIdentifier))
}
Expand Down
2 changes: 1 addition & 1 deletion x/feeabs/keeper/host_zone_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func createNHostZone(t *testing.T, keeper *feeabskeeper.Keeper, ctx sdk.Context,
IbcDenom: "ibc/123",
OsmosisPoolTokenDenomIn: "ibc/456",
PoolId: 1,
Frozen: false,
Status: types.HostChainFeeAbsStatus_UPDATED,
}
for i := 0; i < n; i++ {
expected = append(expected, expectedConfig)
Expand Down
Loading

0 comments on commit d2b5f20

Please sign in to comment.