0g-chain/x/pricefeed/keeper/grpc_query_test.go

271 lines
8.6 KiB
Go
Raw Normal View History

package keeper_test
import (
"testing"
"time"
2024-05-01 03:17:24 +00:00
"github.com/0glabs/0g-chain/app"
"github.com/0glabs/0g-chain/x/pricefeed/keeper"
"github.com/0glabs/0g-chain/x/pricefeed/types"
Update cosmos-sdk to v0.47.7 (#1811) * Update cometbft, cosmos, ethermint, and ibc-go * Replace github.com/tendermint/tendermint by github.com/cometbft/cometbft * Replace github.com/tendermint/tm-db by github.com/cometbft/cometbft-db * Replace gogo/protobuf with cosmos/gogoproto & simapp replacement * Replace cosmos-sdk/simapp/helpers with cosmos-sdk/testutil/sims * Remove no longer used simulations * Replace ibchost with ibcexported See https://github.com/cosmos/ibc-go/blob/v7.2.2/docs/migrations/v6-to-v7.md#ibc-module-constants * Add new consensus params keeper * Add consensus keeper to blockers * Fix keeper and module issues in app.go * Add IsSendEnabledCoins and update SetParams interface changes * Fix protobuf build for cosmos 47 (#1800) * fix cp errors by using -f; fix lint by only linting our proto dir; and use proofs.proto directly from ics23 for ibc-go v7 * run proto-all; commit updated third party deps and swagger changes * regenerate proto files * use correct gocosmos build plugin for buf * re-gen all protobuf files to update paths for new gocosmos plugin * update protoc and buf to latest versions * fix staking keeper issues in app.go * update tally handler for gov changes * chain id fix and flag fixes * update deps for cometbft 47.7 upgrade * remove all module legacy queriers * update stakingKeeper to pointer * Replace ModuleCdc from govv1beta1 to govcodec * remove simulations * abci.LastCommitInfo → abci.CommitInfo * Remove unused code in keys.go * simapp.MakeTestEncodingConfig -> moduletestutil.MakeTestEncodingConfi * Fix chain id issues in tests * Fix remaining unit test issues * Update changelog for upgrade * Fix e2e tests using updated kvtool * Update protonet to v47 compatible genesis * Bump cometbft-db to v0.9.1-kava.1 * Update kvtool * Remove extra changelog * Fix merged rocksdb issues * go mod cleanup * Bump cometbft-db to v9 and go to 1.21 * Bump rocksdb version to v8.10.0 * Update kvtool to latest version * Update gin to v1.9.0 * Use ibctm.ModuleName in app_test * Fallback to genesis chain id instead of client toml * Remove all simulations * Fix cdp migrations issue with v47 * Update dependencies to correct tags --------- Co-authored-by: Nick DeLuca <nickdeluca08@gmail.com>
2024-02-06 22:54:10 +00:00
tmprototypes "github.com/cometbft/cometbft/proto/tendermint/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/suite"
)
type grpcQueryTestSuite struct {
suite.Suite
tApp app.TestApp
ctx sdk.Context
keeper keeper.Keeper
queryServer types.QueryServer
addrs []sdk.AccAddress
strAddrs []string
now time.Time
}
func (suite *grpcQueryTestSuite) SetupTest() {
suite.tApp = app.NewTestApp()
suite.ctx = suite.tApp.NewContext(true, tmprototypes.Header{}).
WithBlockTime(time.Now().UTC())
suite.keeper = suite.tApp.GetPriceFeedKeeper()
suite.queryServer = keeper.NewQueryServerImpl(suite.keeper)
_, addrs := app.GeneratePrivKeyAddressPairs(5)
suite.addrs = addrs
var strAddrs []string
for _, a := range addrs {
strAddrs = append(strAddrs, a.String())
}
suite.strAddrs = strAddrs
suite.now = time.Now().UTC()
}
func (suite *grpcQueryTestSuite) setTestParams() {
params := types.NewParams([]types.Market{
{MarketID: "tstusd", BaseAsset: "tst", QuoteAsset: "usd", Oracles: []sdk.AccAddress{}, Active: true},
})
suite.keeper.SetParams(suite.ctx, params)
}
func (suite *grpcQueryTestSuite) TestGrpcParams() {
tests := []struct {
giveMsg string
giveParams types.Params
wantAccepted bool
}{
{"default params", types.DefaultParams(), true},
{"test params", types.NewParams([]types.Market{
{MarketID: "tstusd", BaseAsset: "tst", QuoteAsset: "usd", Oracles: []sdk.AccAddress{}, Active: true},
}), true},
}
for _, tt := range tests {
suite.Run(tt.giveMsg, func() {
suite.keeper.SetParams(suite.ctx, tt.giveParams)
res, err := suite.queryServer.Params(sdk.WrapSDKContext(suite.ctx), &types.QueryParamsRequest{})
if tt.wantAccepted {
suite.NoError(err)
suite.NoError(tt.giveParams.VerboseEqual(res.Params), "params query should respond with set params")
} else {
suite.Error(err)
}
})
}
}
func (suite *grpcQueryTestSuite) TestGrpcPrice() {
suite.setTestParams()
suite.setTstPrice()
expectedPrice := types.NewCurrentPriceResponse("tstusd", sdk.MustNewDecFromStr("0.34"))
res, err := suite.queryServer.Price(sdk.WrapSDKContext(suite.ctx), &types.QueryPriceRequest{MarketId: "tstusd"})
suite.NoError(err)
suite.Equal(expectedPrice, res.Price)
}
func (suite *grpcQueryTestSuite) TestGrpcPrice_NoPriceSet() {
suite.setTestParams()
// No prices set yet, should error
_, err := suite.queryServer.Price(sdk.WrapSDKContext(suite.ctx), &types.QueryPriceRequest{MarketId: "tstusd"})
suite.ErrorIs(types.ErrNoValidPrice, err)
}
func (suite *grpcQueryTestSuite) TestGrpcPrice_InvalidMarket() {
suite.setTestParams()
suite.setTstPrice()
_, err := suite.queryServer.Price(sdk.WrapSDKContext(suite.ctx), &types.QueryPriceRequest{MarketId: "invalid"})
suite.Equal("rpc error: code = NotFound desc = invalid market ID", err.Error())
}
func (suite *grpcQueryTestSuite) TestGrpcPrices() {
suite.setTestParams()
suite.setTstPrice()
expectedPrice := types.NewCurrentPriceResponse("tstusd", sdk.MustNewDecFromStr("0.34"))
prices, err := suite.queryServer.Prices(sdk.WrapSDKContext(suite.ctx), &types.QueryPricesRequest{})
suite.NoError(err)
suite.Contains(prices.Prices, expectedPrice, "all prices should include the tstusd price")
}
func (suite *grpcQueryTestSuite) TestGrpcPrices_NoPriceSet() {
params := types.NewParams([]types.Market{
{MarketID: "tst:usd", BaseAsset: "tst", QuoteAsset: "usd", Oracles: []sdk.AccAddress{}, Active: true},
{MarketID: "other:usd", BaseAsset: "other", QuoteAsset: "usd", Oracles: []sdk.AccAddress{}, Active: true},
})
suite.keeper.SetParams(suite.ctx, params)
_, err := suite.keeper.SetPrice(
suite.ctx, suite.addrs[2], "tst:usd",
sdk.MustNewDecFromStr("0.34"),
suite.now.Add(time.Hour*1))
suite.NoError(err)
err = suite.keeper.SetCurrentPrices(suite.ctx, "tst:usd")
suite.NoError(err)
// Set current price of "other:usd" with no individual prices in store
_ = suite.keeper.SetCurrentPrices(suite.ctx, "other:usd")
expectedPrice := types.NewCurrentPriceResponse("tst:usd", sdk.MustNewDecFromStr("0.34"))
prices, err := suite.queryServer.Prices(sdk.WrapSDKContext(suite.ctx), &types.QueryPricesRequest{})
suite.NoError(err)
suite.Equal(len(prices.Prices), 1)
suite.Equal(prices.Prices, types.CurrentPriceResponses{expectedPrice}, "should only contain tst:usd price")
}
func (suite *grpcQueryTestSuite) TestGrpcRawPrices() {
suite.setTestParams()
suite.setTstPrice()
res, err := suite.queryServer.RawPrices(sdk.WrapSDKContext(suite.ctx), &types.QueryRawPricesRequest{MarketId: "tstusd"})
suite.NoError(err)
suite.Equal(3, len(res.RawPrices))
suite.ElementsMatch(
res.RawPrices,
[]types.PostedPriceResponse{
types.NewPostedPriceResponse(
"tstusd",
suite.addrs[0],
sdk.MustNewDecFromStr("0.33"),
suite.now.Add(time.Hour*1),
),
types.NewPostedPriceResponse(
"tstusd",
suite.addrs[1],
sdk.MustNewDecFromStr("0.35"),
suite.now.Add(time.Hour*1),
),
types.NewPostedPriceResponse(
"tstusd",
suite.addrs[2],
sdk.MustNewDecFromStr("0.34"),
suite.now.Add(time.Hour*1),
),
},
)
}
func (suite *grpcQueryTestSuite) TestGrpcRawPrices_InvalidMarket() {
suite.setTestParams()
suite.setTstPrice()
_, err := suite.queryServer.RawPrices(sdk.WrapSDKContext(suite.ctx), &types.QueryRawPricesRequest{MarketId: "invalid"})
suite.Equal("rpc error: code = NotFound desc = invalid market ID", err.Error())
}
func (suite *grpcQueryTestSuite) TestGrpcOracles_Empty() {
params := types.NewParams([]types.Market{
{MarketID: "tstusd", BaseAsset: "tst", QuoteAsset: "usd", Oracles: []sdk.AccAddress{}, Active: true},
})
suite.keeper.SetParams(suite.ctx, params)
res, err := suite.queryServer.Oracles(sdk.WrapSDKContext(suite.ctx), &types.QueryOraclesRequest{MarketId: "tstusd"})
suite.NoError(err)
suite.Empty(res.Oracles)
params = types.NewParams([]types.Market{
{MarketID: "tstusd", BaseAsset: "tst", QuoteAsset: "usd", Oracles: suite.addrs, Active: true},
})
suite.keeper.SetParams(suite.ctx, params)
res, err = suite.queryServer.Oracles(sdk.WrapSDKContext(suite.ctx), &types.QueryOraclesRequest{MarketId: "tstusd"})
suite.NoError(err)
suite.ElementsMatch(suite.strAddrs, res.Oracles)
_, err = suite.queryServer.Oracles(sdk.WrapSDKContext(suite.ctx), &types.QueryOraclesRequest{MarketId: "invalid"})
suite.Equal("rpc error: code = NotFound desc = invalid market ID", err.Error())
}
func (suite *grpcQueryTestSuite) TestGrpcOracles() {
params := types.NewParams([]types.Market{
{MarketID: "tstusd", BaseAsset: "tst", QuoteAsset: "usd", Oracles: suite.addrs, Active: true},
})
suite.keeper.SetParams(suite.ctx, params)
res, err := suite.queryServer.Oracles(sdk.WrapSDKContext(suite.ctx), &types.QueryOraclesRequest{MarketId: "tstusd"})
suite.NoError(err)
suite.ElementsMatch(suite.strAddrs, res.Oracles)
}
func (suite *grpcQueryTestSuite) TestGrpcOracles_InvalidMarket() {
suite.setTestParams()
_, err := suite.queryServer.Oracles(sdk.WrapSDKContext(suite.ctx), &types.QueryOraclesRequest{MarketId: "invalid"})
suite.Equal("rpc error: code = NotFound desc = invalid market ID", err.Error())
}
func (suite *grpcQueryTestSuite) TestGrpcMarkets() {
params := types.NewParams([]types.Market{
{MarketID: "tstusd", BaseAsset: "tst", QuoteAsset: "usd", Oracles: []sdk.AccAddress{}, Active: true},
{MarketID: "btcusd", BaseAsset: "btc", QuoteAsset: "usd", Oracles: []sdk.AccAddress{}, Active: true},
})
suite.keeper.SetParams(suite.ctx, params)
res, err := suite.queryServer.Markets(sdk.WrapSDKContext(suite.ctx), &types.QueryMarketsRequest{})
suite.NoError(err)
suite.Len(res.Markets, 2)
suite.Equal(len(res.Markets), len(params.Markets))
suite.NoError(res.Markets[0].VerboseEqual(params.Markets[0].ToMarketResponse()))
suite.NoError(res.Markets[1].VerboseEqual(params.Markets[1].ToMarketResponse()))
}
func (suite *grpcQueryTestSuite) setTstPrice() {
_, err := suite.keeper.SetPrice(
suite.ctx, suite.addrs[0], "tstusd",
sdk.MustNewDecFromStr("0.33"),
suite.now.Add(time.Hour*1))
suite.NoError(err)
_, err = suite.keeper.SetPrice(
suite.ctx, suite.addrs[1], "tstusd",
sdk.MustNewDecFromStr("0.35"),
suite.now.Add(time.Hour*1))
suite.NoError(err)
_, err = suite.keeper.SetPrice(
suite.ctx, suite.addrs[2], "tstusd",
sdk.MustNewDecFromStr("0.34"),
suite.now.Add(time.Hour*1))
suite.NoError(err)
err = suite.keeper.SetCurrentPrices(suite.ctx, "tstusd")
suite.NoError(err)
}
func TestGrpcQueryTestSuite(t *testing.T) {
suite.Run(t, new(grpcQueryTestSuite))
}