Skip to content

Commit

Permalink
[OTE-218] Add state validity test for perpetual market type (#1170)
Browse files Browse the repository at this point in the history
  • Loading branch information
affanv14 authored Mar 25, 2024
1 parent 0dbaf0b commit b2d63af
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 52 deletions.
4 changes: 2 additions & 2 deletions protocol/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -234,10 +234,10 @@ benchmark:
@VERSION=$(VERSION) go test -mod=readonly -tags='$(build_tags)' -bench=. ./...

test-container:
@SKIP_DISABLED=true VERSION=$(VERSION) go test -mod=readonly -tags='container_test $(build_tags)' ./testing/containertest
@SKIP_DISABLED=true VERSION=$(VERSION) go test -mod=readonly -tags='container_test $(build_tags)' ./...

test-container-accept:
@SKIP_DISABLED=true VERSION=$(VERSION) go test -mod=readonly -tags='container_test $(build_tags)' ./testing/containertest -args -accept
@SKIP_DISABLED=true VERSION=$(VERSION) go test -mod=readonly -tags='container_test $(build_tags)' ./... -args -accept

test-container-build:
$(MAKE) localnet-build
Expand Down
74 changes: 74 additions & 0 deletions protocol/app/upgrades/v5.0.0/upgrade_container_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
//go:build all || container_test

package v_5_0_0_test

import (
"testing"

"github.com/cosmos/gogoproto/proto"
v_5_0_0 "github.com/dydxprotocol/v4-chain/protocol/app/upgrades/v5.0.0"
containertest "github.com/dydxprotocol/v4-chain/protocol/testing/containertest"
"github.com/dydxprotocol/v4-chain/protocol/testutil/constants"
perpetuals "github.com/dydxprotocol/v4-chain/protocol/x/perpetuals/types"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestStateUpgrade(t *testing.T) {
testnet, err := containertest.NewTestnetWithPreupgradeGenesis()
require.NoError(t, err, "failed to create testnet - is docker daemon running?")
err = testnet.Start()
require.NoError(t, err)
defer testnet.MustCleanUp()
node := testnet.Nodes["alice"]
nodeAddress := constants.AliceAccAddress.String()

preUpgradeChecks(node, t)

err = containertest.UpgradeTestnet(nodeAddress, t, node, v_5_0_0.UpgradeName)
require.NoError(t, err)

postUpgradeChecks(node, t)
}

func preUpgradeChecks(node *containertest.Node, t *testing.T) {
preUpgradeCheckPerpetualMarketType(node, t)
// Add test for your upgrade handler logic below
}

func postUpgradeChecks(node *containertest.Node, t *testing.T) {
postUpgradecheckPerpetualMarketType(node, t)
// Add test for your upgrade handler logic below
}

func preUpgradeCheckPerpetualMarketType(node *containertest.Node, t *testing.T) {
perpetualsList := &perpetuals.QueryAllPerpetualsResponse{}
resp, err := containertest.Query(
node,
perpetuals.NewQueryClient,
perpetuals.QueryClient.AllPerpetuals,
&perpetuals.QueryAllPerpetualsRequest{},
)
require.NoError(t, err)
err = proto.UnmarshalText(resp.String(), perpetualsList)
require.NoError(t, err)
for _, perpetual := range perpetualsList.Perpetual {
assert.Equal(t, perpetuals.PerpetualMarketType_PERPETUAL_MARKET_TYPE_UNSPECIFIED, perpetual.Params.MarketType)
}
}

func postUpgradecheckPerpetualMarketType(node *containertest.Node, t *testing.T) {
perpetualsList := &perpetuals.QueryAllPerpetualsResponse{}
resp, err := containertest.Query(
node,
perpetuals.NewQueryClient,
perpetuals.QueryClient.AllPerpetuals,
&perpetuals.QueryAllPerpetualsRequest{},
)
require.NoError(t, err)
err = proto.UnmarshalText(resp.String(), perpetualsList)
require.NoError(t, err)
for _, perpetual := range perpetualsList.Perpetual {
assert.Equal(t, perpetuals.PerpetualMarketType_PERPETUAL_MARKET_TYPE_CROSS, perpetual.Params.MarketType)
}
}
51 changes: 1 addition & 50 deletions protocol/testing/containertest/testnet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,11 @@ import (
"time"

sdkmath "cosmossdk.io/math"
upgrade "cosmossdk.io/x/upgrade/types"
sdk "github.com/cosmos/cosmos-sdk/types"
bank "github.com/cosmos/cosmos-sdk/x/bank/types"
gov "github.com/cosmos/cosmos-sdk/x/gov/types/v1"
"github.com/cosmos/gogoproto/jsonpb"
"github.com/cosmos/gogoproto/proto"
"github.com/dydxprotocol/v4-chain/protocol/daemons/pricefeed/client/types"
testapp "github.com/dydxprotocol/v4-chain/protocol/testutil/app"
"github.com/dydxprotocol/v4-chain/protocol/testutil/constants"
"github.com/dydxprotocol/v4-chain/protocol/testutil/daemons/pricefeed/exchange_config"
assets "github.com/dydxprotocol/v4-chain/protocol/x/assets/types"
Expand All @@ -31,15 +28,8 @@ import (
)

const expectDirName = "expect"
const govModuleAddress = "dydx10d07y265gmmuvt4z0w9aw880jnsr700jnmapky"

var acceptFlag = flag.Bool("accept", false, "Accept new values for expect files")
var nodeAddresses = []string{
constants.AliceAccAddress.String(),
constants.BobAccAddress.String(),
constants.CarlAccAddress.String(),
constants.DaveAccAddress.String(),
}

// Compare a message against an expected output. Use flag `-accept` to write or modify expected output.
// Expected output will read/written from `expect/{testName}_{tag}.expect`.
Expand Down Expand Up @@ -292,45 +282,6 @@ func TestUpgrade(t *testing.T) {
defer testnet.MustCleanUp()
node := testnet.Nodes["alice"]

proposal, err := gov.NewMsgSubmitProposal(
[]sdk.Msg{
&upgrade.MsgSoftwareUpgrade{
Authority: govModuleAddress,
Plan: upgrade.Plan{
Name: UpgradeToVersion,
Height: 10,
},
},
},
testapp.TestDeposit,
constants.AliceAccAddress.String(),
testapp.TestMetadata,
testapp.TestTitle,
testapp.TestSummary,
false,
)
require.NoError(t, err)

require.NoError(t, BroadcastTx(
node,
proposal,
constants.AliceAccAddress.String(),
))
err = node.Wait(2)
require.NoError(t, err)

for _, address := range nodeAddresses {
require.NoError(t, BroadcastTx(
node,
&gov.MsgVote{
ProposalId: 1,
Voter: address,
Option: gov.VoteOption_VOTE_OPTION_YES,
},
address,
))
}

err = node.WaitUntilBlockHeight(12)
err = UpgradeTestnet(constants.AliceAccAddress.String(), t, node, UpgradeToVersion)
require.NoError(t, err)
}
66 changes: 66 additions & 0 deletions protocol/testing/containertest/testnet_utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package containertest

import (
"testing"

"github.com/dydxprotocol/v4-chain/protocol/testutil/constants"
"github.com/stretchr/testify/require"

upgrade "cosmossdk.io/x/upgrade/types"
sdk "github.com/cosmos/cosmos-sdk/types"
gov "github.com/cosmos/cosmos-sdk/x/gov/types/v1"
testapp "github.com/dydxprotocol/v4-chain/protocol/testutil/app"
)

const GovModuleAddress = "dydx10d07y265gmmuvt4z0w9aw880jnsr700jnmapky"

var NodeAddresses = []string{
constants.AliceAccAddress.String(),
constants.BobAccAddress.String(),
constants.CarlAccAddress.String(),
constants.DaveAccAddress.String(),
}

func UpgradeTestnet(nodeAddress string, t *testing.T, node *Node, upgradeToVersion string) error {
proposal, err := gov.NewMsgSubmitProposal(
[]sdk.Msg{
&upgrade.MsgSoftwareUpgrade{
Authority: GovModuleAddress,
Plan: upgrade.Plan{
Name: upgradeToVersion,
Height: 10,
},
},
},
testapp.TestDeposit,
nodeAddress,
testapp.TestMetadata,
testapp.TestTitle,
testapp.TestSummary,
false,
)
require.NoError(t, err)

require.NoError(t, BroadcastTx(
node,
proposal,
nodeAddress,
))
err = node.Wait(2)
require.NoError(t, err)

for _, address := range NodeAddresses {
require.NoError(t, BroadcastTx(
node,
&gov.MsgVote{
ProposalId: 1,
Voter: address,
Option: gov.VoteOption_VOTE_OPTION_YES,
},
address,
))
}

err = node.WaitUntilBlockHeight(12)
return err
}

0 comments on commit b2d63af

Please sign in to comment.