From 596b64dc73d346c90ee75317c034c76719f8c7da Mon Sep 17 00:00:00 2001 From: aljo242 Date: Wed, 16 Oct 2024 14:28:02 -0400 Subject: [PATCH 01/10] fix --- x/marketmap/keeper/genesis.go | 4 ---- x/marketmap/keeper/keeper.go | 13 ++++++++++++- x/marketmap/keeper/msg_server.go | 6 +++--- 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/x/marketmap/keeper/genesis.go b/x/marketmap/keeper/genesis.go index e10f9e8de..75f0c75e8 100644 --- a/x/marketmap/keeper/genesis.go +++ b/x/marketmap/keeper/genesis.go @@ -22,10 +22,6 @@ func (k *Keeper) InitGenesis(ctx sdk.Context, gs types.GenesisState) { } } - if err := k.SetLastUpdated(ctx, gs.LastUpdated); err != nil { - panic(err) - } - if err := k.SetParams(ctx, gs.Params); err != nil { panic(err) } diff --git a/x/marketmap/keeper/keeper.go b/x/marketmap/keeper/keeper.go index 2abfb7253..1fceeff1b 100644 --- a/x/marketmap/keeper/keeper.go +++ b/x/marketmap/keeper/keeper.go @@ -89,7 +89,18 @@ func (k *Keeper) GetMarket(ctx context.Context, tickerStr string) (types.Market, // setMarket sets a market. func (k *Keeper) setMarket(ctx context.Context, market types.Market) error { - return k.markets.Set(ctx, types.TickerString(market.Ticker.String()), market) + if err := k.markets.Set(ctx, types.TickerString(market.Ticker.String()), market); err != nil { + return err + } + + // always set LastUpdated when the market is updated. + return k.setLastUpdatedFromContext(ctx) +} + +// setLastUpdatedFromContext calls SetLastUpdated using the ctx BlockHeight() value. +func (k *Keeper) setLastUpdatedFromContext(ctx context.Context) error { + sdkCtx := sdk.UnwrapSDKContext(ctx) + return k.SetLastUpdated(ctx, uint64(sdkCtx.BlockHeight())) } // EnableMarket sets the Enabled field of a Market Ticker to true. diff --git a/x/marketmap/keeper/msg_server.go b/x/marketmap/keeper/msg_server.go index 2de7a18ec..55e996574 100644 --- a/x/marketmap/keeper/msg_server.go +++ b/x/marketmap/keeper/msg_server.go @@ -85,7 +85,7 @@ func (ms msgServer) UpsertMarkets(goCtx context.Context, msg *types.MsgUpsertMar return nil, err } - return &types.MsgUpsertMarketsResponse{}, ms.k.SetLastUpdated(ctx, uint64(ctx.BlockHeight())) //nolint:gosec + return &types.MsgUpsertMarketsResponse{}, nil } // CreateMarkets updates the marketmap by creating markets from the given message. All updates are made to the market @@ -126,7 +126,7 @@ func (ms msgServer) CreateMarkets(goCtx context.Context, msg *types.MsgCreateMar return nil, fmt.Errorf("invalid state resulting from update: %w", err) } - return &types.MsgCreateMarketsResponse{}, ms.k.SetLastUpdated(ctx, uint64(ctx.BlockHeight())) //nolint:gosec + return &types.MsgCreateMarketsResponse{}, nil } // UpdateMarkets updates the marketmap by updating markets from the given message. All updates are made to the market @@ -166,7 +166,7 @@ func (ms msgServer) UpdateMarkets(goCtx context.Context, msg *types.MsgUpdateMar return nil, fmt.Errorf("invalid state resulting from update: %w", err) } - return &types.MsgUpdateMarketsResponse{}, ms.k.SetLastUpdated(ctx, uint64(ctx.BlockHeight())) //nolint:gosec + return &types.MsgUpdateMarketsResponse{}, nil } // verifyMarketAuthorities verifies that the msg-submitter is a market-authority From d240fb35675d4aadb03cee4ca0016f9c0e17588b Mon Sep 17 00:00:00 2001 From: aljo242 Date: Wed, 16 Oct 2024 14:33:30 -0400 Subject: [PATCH 02/10] update on delete --- x/marketmap/keeper/keeper.go | 2 +- x/marketmap/keeper/keeper_test.go | 28 ++++++++++++++++++---------- 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/x/marketmap/keeper/keeper.go b/x/marketmap/keeper/keeper.go index 1fceeff1b..1a40834c1 100644 --- a/x/marketmap/keeper/keeper.go +++ b/x/marketmap/keeper/keeper.go @@ -197,7 +197,7 @@ func (k *Keeper) DeleteMarket(ctx context.Context, tickerStr string) (bool, erro return false, err } - return true, nil + return true, k.setLastUpdatedFromContext(ctx) } // HasMarket checks if a market exists in the store. diff --git a/x/marketmap/keeper/keeper_test.go b/x/marketmap/keeper/keeper_test.go index 118d311e0..0f6df3656 100644 --- a/x/marketmap/keeper/keeper_test.go +++ b/x/marketmap/keeper/keeper_test.go @@ -3,21 +3,19 @@ package keeper_test import ( "testing" - "github.com/skip-mev/chaintestutil/sample" - - oraclekeeper "github.com/skip-mev/connect/v2/x/oracle/keeper" - oracletypes "github.com/skip-mev/connect/v2/x/oracle/types" - storetypes "cosmossdk.io/store/types" "github.com/cosmos/cosmos-sdk/runtime" "github.com/cosmos/cosmos-sdk/testutil" sdk "github.com/cosmos/cosmos-sdk/types" moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil" + "github.com/skip-mev/chaintestutil/sample" "github.com/stretchr/testify/suite" connecttypes "github.com/skip-mev/connect/v2/pkg/types" "github.com/skip-mev/connect/v2/x/marketmap/keeper" "github.com/skip-mev/connect/v2/x/marketmap/types" + oraclekeeper "github.com/skip-mev/connect/v2/x/oracle/keeper" + oracletypes "github.com/skip-mev/connect/v2/x/oracle/types" ) var r = sample.Rand() @@ -177,33 +175,43 @@ var ( ) func (s *KeeperTestSuite) TestGets() { + const testBlockHeight = 100 + ctx := s.ctx.WithBlockHeight(testBlockHeight) + s.Run("get empty market map", func() { - got, err := s.keeper.GetAllMarkets(s.ctx) + got, err := s.keeper.GetAllMarkets(ctx) s.Require().NoError(err) s.Require().Equal(map[string]types.Market{}, got) }) s.Run("setup initial markets", func() { for _, market := range markets { - s.Require().NoError(s.keeper.CreateMarket(s.ctx, market)) + s.Require().NoError(s.keeper.CreateMarket(ctx, market)) } s.Run("unable to set markets again", func() { for _, market := range markets { - s.Require().ErrorIs(s.keeper.CreateMarket(s.ctx, market), types.NewMarketAlreadyExistsError(types.TickerString(market.Ticker.String()))) + s.Require().ErrorIs(s.keeper.CreateMarket(ctx, market), types.NewMarketAlreadyExistsError(types.TickerString(market.Ticker.String()))) } }) - s.Require().NoError(s.keeper.ValidateState(s.ctx, markets)) + s.Require().NoError(s.keeper.ValidateState(ctx, markets)) }) s.Run("get all tickers", func() { - got, err := s.keeper.GetAllMarkets(s.ctx) + got, err := s.keeper.GetAllMarkets(ctx) s.Require().NoError(err) s.Require().Equal(len(markets), len(got)) s.Require().Equal(marketsMap, got) }) + + s.Run("check last updated", func() { + got, err := s.keeper.GetLastUpdated(ctx) + s.Require().NoError(err) + + s.Require().Equal(uint64(testBlockHeight), got) + }) } func (s *KeeperTestSuite) TestSetParams() { From 32b8c0555ac9422b466ffe8178f73cbe045bbb18 Mon Sep 17 00:00:00 2001 From: aljo242 Date: Wed, 16 Oct 2024 14:39:56 -0400 Subject: [PATCH 03/10] test --- x/marketmap/keeper/genesis.go | 4 +++ x/marketmap/keeper/keeper_test.go | 54 +++++++++++++++++++------------ 2 files changed, 38 insertions(+), 20 deletions(-) diff --git a/x/marketmap/keeper/genesis.go b/x/marketmap/keeper/genesis.go index 75f0c75e8..e10f9e8de 100644 --- a/x/marketmap/keeper/genesis.go +++ b/x/marketmap/keeper/genesis.go @@ -22,6 +22,10 @@ func (k *Keeper) InitGenesis(ctx sdk.Context, gs types.GenesisState) { } } + if err := k.SetLastUpdated(ctx, gs.LastUpdated); err != nil { + panic(err) + } + if err := k.SetParams(ctx, gs.Params); err != nil { panic(err) } diff --git a/x/marketmap/keeper/keeper_test.go b/x/marketmap/keeper/keeper_test.go index 0f6df3656..4b885e215 100644 --- a/x/marketmap/keeper/keeper_test.go +++ b/x/marketmap/keeper/keeper_test.go @@ -177,6 +177,11 @@ var ( func (s *KeeperTestSuite) TestGets() { const testBlockHeight = 100 ctx := s.ctx.WithBlockHeight(testBlockHeight) + defer func() { + lastUpdated, err := s.keeper.GetLastUpdated(ctx) + s.Require().NoError(err) + s.Require().Equal(lastUpdated, uint64(testBlockHeight)) + }() s.Run("get empty market map", func() { got, err := s.keeper.GetAllMarkets(ctx) @@ -205,13 +210,6 @@ func (s *KeeperTestSuite) TestGets() { s.Require().Equal(len(markets), len(got)) s.Require().Equal(marketsMap, got) }) - - s.Run("check last updated", func() { - got, err := s.keeper.GetLastUpdated(ctx) - s.Require().NoError(err) - - s.Require().Equal(uint64(testBlockHeight), got) - }) } func (s *KeeperTestSuite) TestSetParams() { @@ -330,51 +328,67 @@ func (s *KeeperTestSuite) TestInvalidCreateDisabledNormalizeBy() { } func (s *KeeperTestSuite) TestDeleteMarket() { + const testBlockHeight = 1000 + ctx := s.ctx.WithBlockHeight(testBlockHeight) + defer func() { + lastUpdated, err := s.keeper.GetLastUpdated(ctx) + s.Require().NoError(err) + s.Require().Equal(lastUpdated, uint64(testBlockHeight)) + }() + // create a valid markets btcCopy := btcusdt btcCopy.Ticker.Enabled = true - s.Require().NoError(s.keeper.CreateMarket(s.ctx, btcCopy)) + s.Require().NoError(s.keeper.CreateMarket(ctx, btcCopy)) // invalid delete will return nil - idempotent - deleted, err := s.keeper.DeleteMarket(s.ctx, "foobar") + deleted, err := s.keeper.DeleteMarket(ctx, "foobar") s.Require().NoError(err) s.Require().False(deleted) // cannot delete enabled markets - deleted, err = s.keeper.DeleteMarket(s.ctx, btcCopy.Ticker.String()) + deleted, err = s.keeper.DeleteMarket(ctx, btcCopy.Ticker.String()) s.Require().Error(err) s.Require().False(deleted) // disable market btcCopy.Ticker.Enabled = false - s.Require().NoError(s.keeper.UpdateMarket(s.ctx, btcCopy)) + s.Require().NoError(s.keeper.UpdateMarket(ctx, btcCopy)) // delete disabled markets - deleted, err = s.keeper.DeleteMarket(s.ctx, btcCopy.Ticker.String()) + deleted, err = s.keeper.DeleteMarket(ctx, btcCopy.Ticker.String()) s.Require().NoError(err) s.Require().True(deleted) - _, err = s.keeper.GetMarket(s.ctx, btcCopy.Ticker.String()) + _, err = s.keeper.GetMarket(ctx, btcCopy.Ticker.String()) s.Require().Error(err) } func (s *KeeperTestSuite) TestEnableDisableMarket() { + const testBlockHeight = 10000 + ctx := s.ctx.WithBlockHeight(testBlockHeight) + defer func() { + lastUpdated, err := s.keeper.GetLastUpdated(ctx) + s.Require().NoError(err) + s.Require().Equal(lastUpdated, uint64(testBlockHeight)) + }() + // create a valid markets - s.Require().NoError(s.keeper.CreateMarket(s.ctx, btcusdt)) + s.Require().NoError(s.keeper.CreateMarket(ctx, btcusdt)) // invalid enable/disable fails - s.Require().Error(s.keeper.EnableMarket(s.ctx, "foobar")) - s.Require().Error(s.keeper.DisableMarket(s.ctx, "foobar")) + s.Require().Error(s.keeper.EnableMarket(ctx, "foobar")) + s.Require().Error(s.keeper.DisableMarket(ctx, "foobar")) // valid enable works - s.Require().NoError(s.keeper.EnableMarket(s.ctx, btcusdt.Ticker.String())) - market, err := s.keeper.GetMarket(s.ctx, btcusdt.Ticker.String()) + s.Require().NoError(s.keeper.EnableMarket(ctx, btcusdt.Ticker.String())) + market, err := s.keeper.GetMarket(ctx, btcusdt.Ticker.String()) s.Require().NoError(err) s.Require().True(market.Ticker.Enabled) // valid disable works - s.Require().NoError(s.keeper.DisableMarket(s.ctx, btcusdt.Ticker.String())) - market, err = s.keeper.GetMarket(s.ctx, btcusdt.Ticker.String()) + s.Require().NoError(s.keeper.DisableMarket(ctx, btcusdt.Ticker.String())) + market, err = s.keeper.GetMarket(ctx, btcusdt.Ticker.String()) s.Require().NoError(err) s.Require().False(market.Ticker.Enabled) } From c99bcb43b03c26d1f92b3acd8a015c1b1d7c4e66 Mon Sep 17 00:00:00 2001 From: aljo242 Date: Wed, 16 Oct 2024 14:43:24 -0400 Subject: [PATCH 04/10] always use random blockheights --- x/marketmap/keeper/keeper_test.go | 6 +++--- x/marketmap/keeper/msg_server_test.go | 11 +++++------ 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/x/marketmap/keeper/keeper_test.go b/x/marketmap/keeper/keeper_test.go index 4b885e215..66e3a7f51 100644 --- a/x/marketmap/keeper/keeper_test.go +++ b/x/marketmap/keeper/keeper_test.go @@ -175,7 +175,7 @@ var ( ) func (s *KeeperTestSuite) TestGets() { - const testBlockHeight = 100 + testBlockHeight := r.Int63() ctx := s.ctx.WithBlockHeight(testBlockHeight) defer func() { lastUpdated, err := s.keeper.GetLastUpdated(ctx) @@ -328,7 +328,7 @@ func (s *KeeperTestSuite) TestInvalidCreateDisabledNormalizeBy() { } func (s *KeeperTestSuite) TestDeleteMarket() { - const testBlockHeight = 1000 + testBlockHeight := r.Int63() ctx := s.ctx.WithBlockHeight(testBlockHeight) defer func() { lastUpdated, err := s.keeper.GetLastUpdated(ctx) @@ -365,7 +365,7 @@ func (s *KeeperTestSuite) TestDeleteMarket() { } func (s *KeeperTestSuite) TestEnableDisableMarket() { - const testBlockHeight = 10000 + testBlockHeight := r.Int63() ctx := s.ctx.WithBlockHeight(testBlockHeight) defer func() { lastUpdated, err := s.keeper.GetLastUpdated(ctx) diff --git a/x/marketmap/keeper/msg_server_test.go b/x/marketmap/keeper/msg_server_test.go index 6ee63330a..43c64c2f1 100644 --- a/x/marketmap/keeper/msg_server_test.go +++ b/x/marketmap/keeper/msg_server_test.go @@ -7,9 +7,8 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" govtypes "github.com/cosmos/cosmos-sdk/x/gov/types" - "github.com/stretchr/testify/mock" - "github.com/skip-mev/chaintestutil/sample" + "github.com/stretchr/testify/mock" connecttypes "github.com/skip-mev/connect/v2/pkg/types" "github.com/skip-mev/connect/v2/x/marketmap/keeper" @@ -394,7 +393,7 @@ func (s *KeeperTestSuite) TestMsgServerUpsertMarkets() { hooks.On("AfterMarketCreated", mock.Anything, btcusdt).Return(nil).Once() - s.ctx = s.ctx.WithBlockHeight(12) + s.ctx = s.ctx.WithBlockHeight(r.Int63()) resp, err := msgServer.UpsertMarkets(s.ctx, msg) s.Require().NoError(err) @@ -436,7 +435,7 @@ func (s *KeeperTestSuite) TestMsgServerUpsertMarkets() { hooks.On("AfterMarketCreated", mock.Anything, usdtusd).Return(nil).Once() hooks.On("AfterMarketUpdated", mock.Anything, btcusdt).Return(err).Once() - s.ctx = s.ctx.WithBlockHeight(13) + s.ctx = s.ctx.WithBlockHeight(r.Int63()) resp, err := msgServer.UpsertMarkets(s.ctx, msg) s.Require().Error(err) @@ -457,7 +456,7 @@ func (s *KeeperTestSuite) TestMsgServerUpsertMarkets() { hooks.On("AfterMarketUpdated", mock.Anything, btcusdt).Return(nil).Once() hooks.On("AfterMarketCreated", mock.Anything, ethusdt).Return(nil).Once() - s.ctx = s.ctx.WithBlockHeight(13) + s.ctx = s.ctx.WithBlockHeight(r.Int63()) resp, err := msgServer.UpsertMarkets(s.ctx, msg) s.Require().NoError(err) @@ -512,7 +511,7 @@ func (s *KeeperTestSuite) TestMsgServerUpsertMarkets() { } hooks.On("AfterMarketUpdated", mock.Anything, mock.Anything).Return(nil).Once() - s.ctx = s.ctx.WithBlockHeight(13) + s.ctx = s.ctx.WithBlockHeight(r.Int63()) resp, err := msgServer.UpsertMarkets(s.ctx, msg) s.Require().Error(err) From b74b975ce1cf3785387c3831f0874231f78cd6ff Mon Sep 17 00:00:00 2001 From: aljo242 Date: Wed, 16 Oct 2024 14:52:10 -0400 Subject: [PATCH 05/10] update --- tests/integration/connect_setup.go | 54 +++++++++++++++++++++++++++--- 1 file changed, 49 insertions(+), 5 deletions(-) diff --git a/tests/integration/connect_setup.go b/tests/integration/connect_setup.go index d346c01b4..5f29f00e4 100644 --- a/tests/integration/connect_setup.go +++ b/tests/integration/connect_setup.go @@ -332,16 +332,45 @@ func QueryMarket(chain *cosmos.CosmosChain, cp connecttypes.CurrencyPair) (mmtyp return res.Market, nil } +// QueryMarketMap queries the market map. +func QueryMarketMap(chain *cosmos.CosmosChain) (*mmtypes.MarketMapResponse, error) { + grpcAddr := chain.GetHostGRPCAddress() + + // create the client + cc, err := grpc.Dial(grpcAddr, grpc.WithTransportCredentials(insecure.NewCredentials())) + if err != nil { + return nil, err + } + defer cc.Close() + + // create the mm client + client := mmtypes.NewQueryClient(cc) + + ctx := context.Background() + + // query the currency pairs + res, err := client.MarketMap(ctx, &mmtypes.MarketMapRequest{}) + if err != nil { + return nil, err + } + + if res == nil { + return nil, fmt.Errorf("response is nil") + } + + return res, nil +} + // SubmitProposal creates and submits a proposal to the chain func SubmitProposal(chain *cosmos.CosmosChain, deposit sdk.Coin, submitter string, msgs ...sdk.Msg) (string, error) { // build the proposal - rand := rand.Str(10) + randStr := rand.Str(10) protoMsgs := make([]cosmos.ProtoMessage, len(msgs)) for i, msg := range msgs { protoMsgs[i] = msg } - prop, err := chain.BuildProposal(protoMsgs, rand, rand, rand, deposit.String(), submitter, false) + prop, err := chain.BuildProposal(protoMsgs, randStr, randStr, randStr, deposit.String(), submitter, false) if err != nil { return "", err } @@ -413,16 +442,31 @@ func (s *ConnectIntegrationSuite) AddCurrencyPairs(chain *cosmos.CosmosChain, us // get an rpc endpoint for the chain client := chain.Nodes()[0].Client + ctx := context.Background() + // broadcast the tx - resp, err := client.BroadcastTxCommit(context.Background(), tx) + txResp, err := client.BroadcastTxCommit(ctx, tx) if err != nil { return err } - if resp.TxResult.Code != abcitypes.CodeTypeOK { - return fmt.Errorf(resp.TxResult.Log) + if txResp.TxResult.Code != abcitypes.CodeTypeOK { + return fmt.Errorf(txResp.TxResult.Log) } + // check market map and lastUpdated + mmResp, err := QueryMarketMap(chain) + s.Require().NoError(err) + + // ensure that the market exists + for _, create := range creates { + got, found := mmResp.MarketMap.Markets[create.Ticker.String()] + s.Require().True(found) + s.Require().Equal(create, got) + } + + s.Require().Equal(txResp.Height, mmResp.LastUpdated) + return nil } From 7da6a3cc4d4742cedc5a0850e8041787d8fedede Mon Sep 17 00:00:00 2001 From: aljo242 Date: Wed, 16 Oct 2024 15:02:11 -0400 Subject: [PATCH 06/10] cute --- x/marketmap/keeper/keeper_test.go | 72 +++++++++++++++++++++---------- 1 file changed, 50 insertions(+), 22 deletions(-) diff --git a/x/marketmap/keeper/keeper_test.go b/x/marketmap/keeper/keeper_test.go index 66e3a7f51..44c9a4ed9 100644 --- a/x/marketmap/keeper/keeper_test.go +++ b/x/marketmap/keeper/keeper_test.go @@ -365,30 +365,58 @@ func (s *KeeperTestSuite) TestDeleteMarket() { } func (s *KeeperTestSuite) TestEnableDisableMarket() { - testBlockHeight := r.Int63() - ctx := s.ctx.WithBlockHeight(testBlockHeight) - defer func() { - lastUpdated, err := s.keeper.GetLastUpdated(ctx) - s.Require().NoError(err) - s.Require().Equal(lastUpdated, uint64(testBlockHeight)) - }() - // create a valid markets - s.Require().NoError(s.keeper.CreateMarket(ctx, btcusdt)) + s.Run("create a valid markets", func() { + testBlockHeight := r.Int63() + ctx := s.ctx.WithBlockHeight(testBlockHeight) + defer func() { + lastUpdated, err := s.keeper.GetLastUpdated(ctx) + s.Require().NoError(err) + s.Require().Equal(lastUpdated, uint64(testBlockHeight)) + }() - // invalid enable/disable fails - s.Require().Error(s.keeper.EnableMarket(ctx, "foobar")) - s.Require().Error(s.keeper.DisableMarket(ctx, "foobar")) + s.Require().NoError(s.keeper.CreateMarket(ctx, btcusdt)) - // valid enable works - s.Require().NoError(s.keeper.EnableMarket(ctx, btcusdt.Ticker.String())) - market, err := s.keeper.GetMarket(ctx, btcusdt.Ticker.String()) - s.Require().NoError(err) - s.Require().True(market.Ticker.Enabled) + }) + + s.Run("invalid enable/disable fails", func() { + testBlockHeight := r.Int63() + ctx := s.ctx.WithBlockHeight(testBlockHeight) + + s.Require().Error(s.keeper.EnableMarket(ctx, "foobar")) + s.Require().Error(s.keeper.DisableMarket(ctx, "foobar")) + }) + + s.Run("valid enable works", func() { + testBlockHeight := r.Int63() + ctx := s.ctx.WithBlockHeight(testBlockHeight) + defer func() { + lastUpdated, err := s.keeper.GetLastUpdated(ctx) + s.Require().NoError(err) + s.Require().Equal(lastUpdated, uint64(testBlockHeight)) + }() + + // valid enable works + s.Require().NoError(s.keeper.EnableMarket(ctx, btcusdt.Ticker.String())) + market, err := s.keeper.GetMarket(ctx, btcusdt.Ticker.String()) + s.Require().NoError(err) + s.Require().True(market.Ticker.Enabled) + }) + + s.Run("valid enable works", func() { + testBlockHeight := r.Int63() + ctx := s.ctx.WithBlockHeight(testBlockHeight) + defer func() { + lastUpdated, err := s.keeper.GetLastUpdated(ctx) + s.Require().NoError(err) + s.Require().Equal(lastUpdated, uint64(testBlockHeight)) + }() + + // valid disable works + s.Require().NoError(s.keeper.DisableMarket(ctx, btcusdt.Ticker.String())) + market, err := s.keeper.GetMarket(ctx, btcusdt.Ticker.String()) + s.Require().NoError(err) + s.Require().False(market.Ticker.Enabled) + }) - // valid disable works - s.Require().NoError(s.keeper.DisableMarket(ctx, btcusdt.Ticker.String())) - market, err = s.keeper.GetMarket(ctx, btcusdt.Ticker.String()) - s.Require().NoError(err) - s.Require().False(market.Ticker.Enabled) } From 534986ee1c6894042f801dbccf5d57cfba76a888 Mon Sep 17 00:00:00 2001 From: aljo242 Date: Wed, 16 Oct 2024 15:19:13 -0400 Subject: [PATCH 07/10] add more checks --- tests/integration/connect_setup.go | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/tests/integration/connect_setup.go b/tests/integration/connect_setup.go index 5f29f00e4..756521598 100644 --- a/tests/integration/connect_setup.go +++ b/tests/integration/connect_setup.go @@ -465,7 +465,7 @@ func (s *ConnectIntegrationSuite) AddCurrencyPairs(chain *cosmos.CosmosChain, us s.Require().Equal(create, got) } - s.Require().Equal(txResp.Height, mmResp.LastUpdated) + s.Require().Equal(uint64(txResp.Height), mmResp.LastUpdated) return nil } @@ -512,15 +512,28 @@ func (s *ConnectIntegrationSuite) UpdateCurrencyPair(chain *cosmos.CosmosChain, // get an rpc endpoint for the chain client := chain.Nodes()[0].Client // broadcast the tx - resp, err := client.BroadcastTxCommit(context.Background(), tx) + txResp, err := client.BroadcastTxCommit(context.Background(), tx) if err != nil { return err } - if resp.TxResult.Code != abcitypes.CodeTypeOK { - return fmt.Errorf(resp.TxResult.Log) + if txResp.TxResult.Code != abcitypes.CodeTypeOK { + return fmt.Errorf(txResp.TxResult.Log) + } + + // check market map and lastUpdated + mmResp, err := QueryMarketMap(chain) + s.Require().NoError(err) + + // ensure that the market exists + for _, create := range markets { + got, found := mmResp.MarketMap.Markets[create.Ticker.String()] + s.Require().True(found) + s.Require().Equal(create, got) } + s.Require().Equal(uint64(txResp.Height), mmResp.LastUpdated) + return nil } From db8b60633ef2e5b704dc5b48c260badecb90baa1 Mon Sep 17 00:00:00 2001 From: aljo242 Date: Wed, 16 Oct 2024 15:27:36 -0400 Subject: [PATCH 08/10] lint --- cmd/connect/main.go | 1 + x/marketmap/keeper/keeper.go | 2 +- x/marketmap/keeper/keeper_test.go | 12 +++++------- 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/cmd/connect/main.go b/cmd/connect/main.go index 8cb1cf8f2..50e4c68eb 100644 --- a/cmd/connect/main.go +++ b/cmd/connect/main.go @@ -5,6 +5,7 @@ import ( "errors" "fmt" "net/http" + //nolint: gosec _ "net/http/pprof" "os" diff --git a/x/marketmap/keeper/keeper.go b/x/marketmap/keeper/keeper.go index 1a40834c1..3dd96d940 100644 --- a/x/marketmap/keeper/keeper.go +++ b/x/marketmap/keeper/keeper.go @@ -100,7 +100,7 @@ func (k *Keeper) setMarket(ctx context.Context, market types.Market) error { // setLastUpdatedFromContext calls SetLastUpdated using the ctx BlockHeight() value. func (k *Keeper) setLastUpdatedFromContext(ctx context.Context) error { sdkCtx := sdk.UnwrapSDKContext(ctx) - return k.SetLastUpdated(ctx, uint64(sdkCtx.BlockHeight())) + return k.SetLastUpdated(ctx, uint64(sdkCtx.BlockHeight())) //nolint:gosec } // EnableMarket sets the Enabled field of a Market Ticker to true. diff --git a/x/marketmap/keeper/keeper_test.go b/x/marketmap/keeper/keeper_test.go index 44c9a4ed9..94c835329 100644 --- a/x/marketmap/keeper/keeper_test.go +++ b/x/marketmap/keeper/keeper_test.go @@ -180,7 +180,7 @@ func (s *KeeperTestSuite) TestGets() { defer func() { lastUpdated, err := s.keeper.GetLastUpdated(ctx) s.Require().NoError(err) - s.Require().Equal(lastUpdated, uint64(testBlockHeight)) + s.Require().Equal(lastUpdated, uint64(testBlockHeight)) //nolint:gosec }() s.Run("get empty market map", func() { @@ -333,7 +333,7 @@ func (s *KeeperTestSuite) TestDeleteMarket() { defer func() { lastUpdated, err := s.keeper.GetLastUpdated(ctx) s.Require().NoError(err) - s.Require().Equal(lastUpdated, uint64(testBlockHeight)) + s.Require().Equal(lastUpdated, uint64(testBlockHeight)) //nolint:gosec }() // create a valid markets @@ -372,11 +372,10 @@ func (s *KeeperTestSuite) TestEnableDisableMarket() { defer func() { lastUpdated, err := s.keeper.GetLastUpdated(ctx) s.Require().NoError(err) - s.Require().Equal(lastUpdated, uint64(testBlockHeight)) + s.Require().Equal(lastUpdated, uint64(testBlockHeight)) //nolint:gosec }() s.Require().NoError(s.keeper.CreateMarket(ctx, btcusdt)) - }) s.Run("invalid enable/disable fails", func() { @@ -393,7 +392,7 @@ func (s *KeeperTestSuite) TestEnableDisableMarket() { defer func() { lastUpdated, err := s.keeper.GetLastUpdated(ctx) s.Require().NoError(err) - s.Require().Equal(lastUpdated, uint64(testBlockHeight)) + s.Require().Equal(lastUpdated, uint64(testBlockHeight)) //nolint:gosec }() // valid enable works @@ -409,7 +408,7 @@ func (s *KeeperTestSuite) TestEnableDisableMarket() { defer func() { lastUpdated, err := s.keeper.GetLastUpdated(ctx) s.Require().NoError(err) - s.Require().Equal(lastUpdated, uint64(testBlockHeight)) + s.Require().Equal(lastUpdated, uint64(testBlockHeight)) //nolint:gosec }() // valid disable works @@ -418,5 +417,4 @@ func (s *KeeperTestSuite) TestEnableDisableMarket() { s.Require().NoError(err) s.Require().False(market.Ticker.Enabled) }) - } From 0da86d961db947172c2c0a24baed00a194aadc15 Mon Sep 17 00:00:00 2001 From: aljo242 Date: Tue, 22 Oct 2024 15:19:34 -0400 Subject: [PATCH 09/10] clean up --- tests/integration/connect_ccv_suite.go | 3 ++- tests/integration/connect_setup.go | 14 +++++--------- 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/tests/integration/connect_ccv_suite.go b/tests/integration/connect_ccv_suite.go index d70aa029d..92431ec90 100644 --- a/tests/integration/connect_ccv_suite.go +++ b/tests/integration/connect_ccv_suite.go @@ -169,7 +169,7 @@ func (s *ConnectCCVSuite) TestCCVAggregation() { } // update the market - s.UpdateCurrencyPair(s.chain, []mmtypes.Market{ + err = s.UpdateCurrencyPair(ctx, s.chain, []mmtypes.Market{ { Ticker: mmtypes.Ticker{ CurrencyPair: ethusdc, @@ -187,6 +187,7 @@ func (s *ConnectCCVSuite) TestCCVAggregation() { }, }, }) + s.Require().NoError(err) priceDelta := big.NewInt(-60000000000) bz, err := priceDelta.GobEncode() diff --git a/tests/integration/connect_setup.go b/tests/integration/connect_setup.go index 756521598..050d3a7e4 100644 --- a/tests/integration/connect_setup.go +++ b/tests/integration/connect_setup.go @@ -306,7 +306,7 @@ func QueryCurrencyPair(chain *cosmos.CosmosChain, cp connecttypes.CurrencyPair, } // QueryMarket queries a market from the market map. -func QueryMarket(chain *cosmos.CosmosChain, cp connecttypes.CurrencyPair) (mmtypes.Market, error) { +func QueryMarket(ctx context.Context, chain *cosmos.CosmosChain, cp connecttypes.CurrencyPair) (mmtypes.Market, error) { grpcAddr := chain.GetHostGRPCAddress() // create the client @@ -319,8 +319,6 @@ func QueryMarket(chain *cosmos.CosmosChain, cp connecttypes.CurrencyPair) (mmtyp // create the mm client client := mmtypes.NewQueryClient(cc) - ctx := context.Background() - // query the currency pairs res, err := client.Market(ctx, &mmtypes.MarketRequest{ CurrencyPair: cp, @@ -333,7 +331,7 @@ func QueryMarket(chain *cosmos.CosmosChain, cp connecttypes.CurrencyPair) (mmtyp } // QueryMarketMap queries the market map. -func QueryMarketMap(chain *cosmos.CosmosChain) (*mmtypes.MarketMapResponse, error) { +func QueryMarketMap(ctx context.Context, chain *cosmos.CosmosChain) (*mmtypes.MarketMapResponse, error) { grpcAddr := chain.GetHostGRPCAddress() // create the client @@ -346,8 +344,6 @@ func QueryMarketMap(chain *cosmos.CosmosChain) (*mmtypes.MarketMapResponse, erro // create the mm client client := mmtypes.NewQueryClient(cc) - ctx := context.Background() - // query the currency pairs res, err := client.MarketMap(ctx, &mmtypes.MarketMapRequest{}) if err != nil { @@ -455,7 +451,7 @@ func (s *ConnectIntegrationSuite) AddCurrencyPairs(chain *cosmos.CosmosChain, us } // check market map and lastUpdated - mmResp, err := QueryMarketMap(chain) + mmResp, err := QueryMarketMap(ctx, chain) s.Require().NoError(err) // ensure that the market exists @@ -501,7 +497,7 @@ func (s *ConnectIntegrationSuite) RemoveMarket( return nil } -func (s *ConnectIntegrationSuite) UpdateCurrencyPair(chain *cosmos.CosmosChain, markets []mmtypes.Market) error { +func (s *ConnectIntegrationSuite) UpdateCurrencyPair(ctx context.Context, chain *cosmos.CosmosChain, markets []mmtypes.Market) error { msg := &mmtypes.MsgUpsertMarkets{ Authority: s.user.FormattedAddress(), Markets: markets, @@ -522,7 +518,7 @@ func (s *ConnectIntegrationSuite) UpdateCurrencyPair(chain *cosmos.CosmosChain, } // check market map and lastUpdated - mmResp, err := QueryMarketMap(chain) + mmResp, err := QueryMarketMap(ctx, chain) s.Require().NoError(err) // ensure that the market exists From 8054d9db99aff0b1ed45e282f09546b535ef670d Mon Sep 17 00:00:00 2001 From: aljo242 Date: Tue, 22 Oct 2024 16:28:47 -0400 Subject: [PATCH 10/10] fix --- tests/integration/connect_suite.go | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/tests/integration/connect_suite.go b/tests/integration/connect_suite.go index b63f1d621..3b1308aa9 100644 --- a/tests/integration/connect_suite.go +++ b/tests/integration/connect_suite.go @@ -337,6 +337,8 @@ func NewConnectOracleIntegrationSuite(suite *ConnectIntegrationSuite) *ConnectOr } func (s *ConnectOracleIntegrationSuite) TestOracleModule() { + ctx := context.Background() + // query the oracle module grpc service for any CurrencyPairs s.Run("QueryCurrencyPairs - no currency-pairs reported", func() { resp, err := QueryCurrencyPairs(s.chain) @@ -377,7 +379,7 @@ func (s *ConnectOracleIntegrationSuite) TestOracleModule() { s.Require().Error(s.RemoveMarket(s.chain, []connecttypes.CurrencyPair{cp1})) // check not removed - market, err := QueryMarket(s.chain, cp1) + market, err := QueryMarket(ctx, s.chain, cp1) s.Require().NoError(err) s.Require().NotNil(market) }) @@ -389,14 +391,14 @@ func (s *ConnectOracleIntegrationSuite) TestOracleModule() { disabledTicker(disabledCP), }...)) - market, err := QueryMarket(s.chain, disabledCP) + market, err := QueryMarket(ctx, s.chain, disabledCP) s.Require().NoError(err) s.Require().NotNil(market) s.Require().NoError(s.RemoveMarket(s.chain, []connecttypes.CurrencyPair{disabledCP})) // check removed - _, err = QueryMarket(s.chain, disabledCP) + _, err = QueryMarket(ctx, s.chain, disabledCP) s.Require().Error(err) }) @@ -404,7 +406,7 @@ func (s *ConnectOracleIntegrationSuite) TestOracleModule() { nonexistentCP := connecttypes.NewCurrencyPair("NON", "EXIST") // check removed doesnt exist - _, err := QueryMarket(s.chain, nonexistentCP) + _, err := QueryMarket(ctx, s.chain, nonexistentCP) s.Require().Error(err) // tx will not error