Skip to content

Commit

Permalink
Unnecessary host zone freezing (#125)
Browse files Browse the repository at this point in the history
* Send package in batch

* test: add query osmosis twap test

* test: add makefile and github worklow

* handle error while sending in batch

* update query osmosis twap test

* fix makefile

* test: temp disable query osmosis twap assertion
  • Loading branch information
minhngoc274 authored Jan 31, 2024
1 parent b06d7b4 commit 98a1a3d
Show file tree
Hide file tree
Showing 7 changed files with 227 additions and 7 deletions.
18 changes: 17 additions & 1 deletion .github/workflows/interchaintest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,20 @@ jobs:

- run: make ictest-feeabs
env:
BRANCH_CI: 'latest'
BRANCH_CI: 'latest'

test-query-osmosis-twap:
runs-on: ubuntu-latest
needs: build-and-push-image
steps:
- name: Set up Go 1.21
uses: actions/setup-go@v3
with:
go-version: 1.21

- name: checkout code
uses: actions/checkout@v3

- run: make ictest-query-osmosis-twap
env:
BRANCH_CI: 'latest'
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ build:
go build $(BUILD_FLAGS) -o bin/feeappd ./cmd/feeappd

test:
go test -race -v ./...
@GOWORK=off go test -race -v ./...

docker-build-debug:
@DOCKER_BUILDKIT=1 docker build -t feeapp:debug -f Dockerfile .
Expand Down Expand Up @@ -127,6 +127,9 @@ ictest-host-zone-proposal:
ictest-feeabs:
cd tests/interchaintest && go test -timeout=25m -race -v -run TestFeeabs .

ictest-query-osmosis-twap:
cd tests/interchaintest && go test -timeout=25m -race -v -run TestQueryOsmosisTwap .

# Executes all tests via interchaintest after compling a local image as juno:local
ictest-all: ictest-basic ictest-ibc ictest-packet-forward

Expand Down
15 changes: 15 additions & 0 deletions tests/interchaintest/feeabs/tx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package feeabs

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

func QueryOsmosisTWAP(c *cosmos.CosmosChain, ctx context.Context, keyName string) (string, error) {
tn := c.Validators[0]
if len(c.FullNodes) > 0 {
tn = c.FullNodes[0]
}
cmd := []string{"feeabs", "query-osmosis-twap"}
return tn.ExecTx(ctx, keyName, cmd...)
}
158 changes: 158 additions & 0 deletions tests/interchaintest/query_osmosis_twap_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
package interchaintest

import (
"context"
"fmt"
"os"
"path"
"testing"

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"
)

func TestQueryOsmosisTwap(t *testing.T) {
if testing.Short() {
t.Skip("skipping in short mode")
}
// Set up chains, users and channels
ctx := context.Background()
chains, users, channels := SetupChain(t, ctx)
feeabs, gaia, osmosis := chains[0].(*cosmos.CosmosChain), chains[1].(*cosmos.CosmosChain), chains[2].(*cosmos.CosmosChain)

feeabsUser, _, osmosisUser := users[0], users[1], users[2]

channFeeabsOsmosis, channOsmosisFeeabs, channFeeabsGaia, channGaiaFeeabs, channOsmosisGaia, channGaiaOsmosis := channels[0], channels[1], channels[2], channels[3], channels[4], channels[5]

// Setup contract on Osmosis
// Store code crosschain Registry
crossChainRegistryContractID, err := osmosis.StoreContract(ctx, osmosisUser.KeyName(), "./bytecode/crosschain_registry.wasm")
require.NoError(t, err)
_ = crossChainRegistryContractID
// // Instatiate
owner := sdktypes.MustBech32ifyAddressBytes(osmosis.Config().Bech32Prefix, osmosisUser.Address())
initMsg := fmt.Sprintf("{\"owner\":\"%s\"}", owner)
registryContractAddress, err := osmosis.InstantiateContract(ctx, osmosisUser.KeyName(), crossChainRegistryContractID, initMsg, true)
require.NoError(t, err)
// Execute
msg := fmt.Sprintf("{\"modify_chain_channel_links\": {\"operations\": [{\"operation\": \"set\",\"source_chain\": \"feeabs\",\"destination_chain\": \"osmosis\",\"channel_id\": \"%s\"},{\"operation\": \"set\",\"source_chain\": \"osmosis\",\"destination_chain\": \"feeabs\",\"channel_id\": \"%s\"},{\"operation\": \"set\",\"source_chain\": \"feeabs\",\"destination_chain\": \"gaia\",\"channel_id\": \"%s\"},{\"operation\": \"set\",\"source_chain\": \"gaia\",\"destination_chain\": \"feeabs\",\"channel_id\": \"%s\"},{\"operation\": \"set\",\"source_chain\": \"osmosis\",\"destination_chain\": \"gaia\",\"channel_id\": \"%s\"},{\"operation\": \"set\",\"source_chain\": \"gaia\",\"destination_chain\": \"osmosis\",\"channel_id\": \"%s\"}]}}",
channFeeabsOsmosis.ChannelID,
channOsmosisFeeabs.ChannelID,
channFeeabsGaia.ChannelID,
channGaiaFeeabs.ChannelID,
channOsmosisGaia.ChannelID,
channGaiaOsmosis.ChannelID)
_, err = osmosis.ExecuteContract(ctx, osmosisUser.KeyName(), registryContractAddress, msg)
require.NoError(t, err)
// Execute
msg = `{
"modify_bech32_prefixes":
{
"operations":
[
{"operation": "set", "chain_name": "feeabs", "prefix": "feeabs"},
{"operation": "set", "chain_name": "osmosis", "prefix": "osmo"},
{"operation": "set", "chain_name": "gaia", "prefix": "cosmos"}
]
}
}`
_, err = osmosis.ExecuteContract(ctx, osmosisUser.KeyName(), registryContractAddress, msg)
require.NoError(t, err)

// Create pool Osmosis(uatom)/Osmosis(stake) on Osmosis
denomTrace := transfertypes.ParseDenomTrace(transfertypes.GetPrefixedDenom(channOsmosisGaia.PortID, channOsmosisGaia.ChannelID, gaia.Config().Denom))
uatomOnOsmosis := denomTrace.IBCDenom()
osmosisUserBalance, err := osmosis.GetBalance(ctx, sdktypes.MustBech32ifyAddressBytes(osmosis.Config().Bech32Prefix, osmosisUser.Address()), uatomOnOsmosis)
require.NoError(t, err)
require.Equal(t, amountToSend, osmosisUserBalance)

denomTrace = transfertypes.ParseDenomTrace(transfertypes.GetPrefixedDenom(channOsmosisFeeabs.PortID, channOsmosisFeeabs.ChannelID, feeabs.Config().Denom))
stakeOnOsmosis := denomTrace.IBCDenom()
osmosisUserBalance, err = osmosis.GetBalance(ctx, sdktypes.MustBech32ifyAddressBytes(osmosis.Config().Bech32Prefix, osmosisUser.Address()), stakeOnOsmosis)
require.NoError(t, err)
require.Equal(t, amountToSend, osmosisUserBalance)

poolID, err := feeabsCli.CreatePool(osmosis, ctx, osmosisUser.KeyName(), cosmos.OsmosisPoolParams{
Weights: fmt.Sprintf("5%s,5%s", stakeOnOsmosis, uatomOnOsmosis),
InitialDeposit: fmt.Sprintf("95000000%s,950000000%s", stakeOnOsmosis, uatomOnOsmosis),
SwapFee: "0.01",
ExitFee: "0",
FutureGovernor: "",
})
require.NoError(t, err)
require.Equal(t, poolID, "1")

// Setup propose_pfm
// propose_pfm for feeabs
_, err = feeabsCli.SetupProposePFM(osmosis, ctx, osmosisUser.KeyName(), registryContractAddress, `{"propose_pfm":{"chain": "feeabs"}}`, stakeOnOsmosis)
require.NoError(t, err)
err = testutil.WaitForBlocks(ctx, 15, feeabs, gaia, osmosis)
require.NoError(t, err)
queryMsg := QuerySmartMsg{
Packet: HasPacketForwarding{
ChainID: "feeabs",
},
}
res := QuerySmartMsgResponse{}
err = osmosis.QueryContract(ctx, registryContractAddress, queryMsg, &res)
require.NoError(t, err)
// propose_pfm for gaia
_, err = feeabsCli.SetupProposePFM(osmosis, ctx, osmosisUser.KeyName(), registryContractAddress, `{"propose_pfm":{"chain": "gaia"}}`, uatomOnOsmosis)
require.NoError(t, err)
err = testutil.WaitForBlocks(ctx, 15, feeabs, gaia, osmosis)
require.NoError(t, err)
queryMsg = QuerySmartMsg{
Packet: HasPacketForwarding{
ChainID: "gaia",
},
}
res = QuerySmartMsgResponse{}
err = osmosis.QueryContract(ctx, registryContractAddress, queryMsg, &res)
require.NoError(t, err)

denomTrace = transfertypes.ParseDenomTrace(transfertypes.GetPrefixedDenom(channFeeabsGaia.PortID, channFeeabsGaia.ChannelID, gaia.Config().Denom))
uatomOnFeeabs := denomTrace.IBCDenom()

current_directory, _ := os.Getwd()
param_change_path := path.Join(current_directory, "proposal", "proposal.json")

changeParamProposal, err := paramsutils.ParseParamChangeProposalJSON(feeabs.Config().EncodingConfig.Amino, param_change_path)
require.NoError(t, err)

paramTx, err := feeabsCli.ParamChangeProposal(feeabs, ctx, feeabsUser.KeyName(), &changeParamProposal)
require.NoError(t, err, "error submitting param change proposal tx")

err = feeabs.VoteOnProposalAllValidators(ctx, paramTx.ProposalID, cosmos.ProposalVoteYes)
require.NoError(t, err, "failed to submit votes")

height, err := feeabs.Height(ctx)
require.NoError(t, err)

_, err = cosmos.PollForProposalStatus(ctx, feeabs, height, height+10, paramTx.ProposalID, cosmos.ProposalStatusPassed)
require.NoError(t, err, "proposal status did not change to passed in expected number of blocks")

_, err = feeabsCli.AddHostZoneProposal(feeabs, ctx, feeabsUser.KeyName(), "./proposal/add_host_zone.json")
require.NoError(t, err)

err = feeabs.VoteOnProposalAllValidators(ctx, "2", cosmos.ProposalVoteYes)
require.NoError(t, err, "failed to submit votes")

height, err = feeabs.Height(ctx)
require.NoError(t, err)

_, err = cosmos.PollForProposalStatus(ctx, feeabs, height, height+10, "2", cosmos.ProposalStatusPassed)
require.NoError(t, err, "proposal status did not change to passed in expected number of blocks")

_, err = feeabsCli.QueryHostZoneConfig(feeabs, ctx)
require.NoError(t, err)

twap, err := feeabsCli.QueryOsmosisArithmeticTwap(feeabs, ctx, uatomOnFeeabs)
fmt.Println(err)
fmt.Println(twap)
//require.NoError(t, err)
}
8 changes: 6 additions & 2 deletions tests/interchaintest/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type QuerySmartMsgResponse struct {
const (
votingPeriod = "10s"
maxDepositPeriod = "10s"
queryEpochTime = "10s"
)

var (
Expand Down Expand Up @@ -69,7 +70,7 @@ var (
GasAdjustment: 1.1,
TrustingPeriod: "112h",
NoHostMount: false,
ModifyGenesis: modifyGenesisShortProposals(votingPeriod, maxDepositPeriod),
ModifyGenesis: modifyGenesisShortProposals(votingPeriod, maxDepositPeriod, queryEpochTime),
ConfigFileOverrides: nil,
EncodingConfig: feeabsEncoding(),
}
Expand Down Expand Up @@ -118,7 +119,7 @@ func GetDockerImageInfo() (repo, version string) {
return repo, branchVersion
}

func modifyGenesisShortProposals(votingPeriod string, maxDepositPeriod string) func(ibc.ChainConfig, []byte) ([]byte, error) {
func modifyGenesisShortProposals(votingPeriod string, maxDepositPeriod string, queryEpochTime string) func(ibc.ChainConfig, []byte) ([]byte, error) {
return func(chainConfig ibc.ChainConfig, genbz []byte) ([]byte, error) {
g := make(map[string]interface{})
if err := json.Unmarshal(genbz, &g); err != nil {
Expand All @@ -133,6 +134,9 @@ func modifyGenesisShortProposals(votingPeriod string, maxDepositPeriod string) f
if err := dyno.Set(g, chainConfig.Denom, "app_state", "gov", "params", "min_deposit", 0, "denom"); err != nil {
return nil, fmt.Errorf("failed to set voting period in genesis json: %w", err)
}
if err := dyno.Set(g, queryEpochTime, "app_state", "feeabs", "epochs", 0, "duration"); err != nil {
return nil, fmt.Errorf("failed to set query epoch time in genesis json: %w", err)
}
out, err := json.Marshal(g)
if err != nil {
return nil, fmt.Errorf("failed to marshal genesis bytes to json: %w", err)
Expand Down
1 change: 1 addition & 0 deletions x/feeabs/ibc_module.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ func (am IBCModule) OnTimeoutPacket(
packet channeltypes.Packet,
relayer sdk.AccAddress,
) error {
ctx.Logger().Info("Timeout packet", "packet", packet)
var icqPacketData types.InterchainQueryPacketData
if err := types.ModuleCdc.UnmarshalJSON(packet.GetData(), &icqPacketData); err != nil {
return sdkerrors.Wrapf(errorstypes.ErrUnknownRequest, "cannot unmarshal packet data: %v", err)
Expand Down
29 changes: 26 additions & 3 deletions x/feeabs/keeper/ibc.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ func (k Keeper) OnAcknowledgementPacket(ctx sdk.Context, ack channeltypes.Acknow

// OnTimeoutPacket resend packet when timeout
func (k Keeper) OnTimeoutPacket(ctx sdk.Context) error {
ctx.Logger().Info("IBC Timeout packet")
return k.handleOsmosisIbcQuery(ctx)
}

Expand Down Expand Up @@ -268,22 +269,44 @@ func (k Keeper) handleOsmosisIbcQuery(ctx sdk.Context) error {

params := k.GetParams(ctx)

batchSize := 10
var reqs []types.QueryArithmeticTwapToNowRequest
batchCounter := 0
var errorFound error
k.IterateHostZone(ctx, func(hostZoneConfig types.HostChainFeeAbsConfig) (stop bool) {
if hostZoneConfig.Frozen {
return false
}
req := types.NewQueryArithmeticTwapToNowRequest(
hostZoneConfig.PoolId,
params.NativeIbcedInOsmosis,
hostZoneConfig.OsmosisPoolTokenDenomIn,
startTime,
)
reqs = append(reqs, req)
batchCounter++
if batchCounter == batchSize {
err := k.SendOsmosisQueryRequest(ctx, reqs, types.IBCPortID, params.IbcQueryIcqChannel)
if err != nil {
errorFound = err
return true
}
reqs = []types.QueryArithmeticTwapToNowRequest{}
batchCounter = 0
}
return false
})
err := k.SendOsmosisQueryRequest(ctx, reqs, types.IBCPortID, params.IbcQueryIcqChannel)
if err != nil {
return err

if errorFound != nil {
return errorFound
}

if len(reqs) > 0 {
err := k.SendOsmosisQueryRequest(ctx, reqs, types.IBCPortID, params.IbcQueryIcqChannel)
if err != nil {
return err
}
}
return nil
}

Expand Down

0 comments on commit 98a1a3d

Please sign in to comment.