Skip to content

Commit

Permalink
Add upgrade test (#894)
Browse files Browse the repository at this point in the history
* Add upgrade test

* Address comments
  • Loading branch information
roy-dydx authored Dec 18, 2023
1 parent 0d121a7 commit 98e2391
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 42 deletions.
4 changes: 2 additions & 2 deletions protocol/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -238,10 +238,10 @@ benchmark:
@VERSION=$(VERSION) go test -mod=readonly -tags='$(build_tags)' -bench=. ./...

test-container:
@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)' ./testing/containertest

test-container-accept:
@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)' ./testing/containertest -args -accept

test-container-build:
$(MAKE) localnet-build
Expand Down
9 changes: 7 additions & 2 deletions protocol/app/upgrades/v3.0.0/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,18 @@ package v_3_0_0
import (
store "github.com/cosmos/cosmos-sdk/store/types"
"github.com/dydxprotocol/v4-chain/protocol/app/upgrades"
ratelimittypes "github.com/dydxprotocol/v4-chain/protocol/x/ratelimit/types"
)

const (
UpgradeName = "v3.0.0"
)

var Upgrade = upgrades.Upgrade{
UpgradeName: UpgradeName,
StoreUpgrades: store.StoreUpgrades{},
UpgradeName: UpgradeName,
StoreUpgrades: store.StoreUpgrades{
Added: []string{
ratelimittypes.StoreKey,
},
},
}
2 changes: 2 additions & 0 deletions protocol/app/upgrades/v3.0.0/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ func CreateUpgradeHandler(
return func(ctx sdk.Context, plan upgradetypes.Plan, vm module.VersionMap) (module.VersionMap, error) {
ctx.Logger().Info("Running %s Upgrade...", UpgradeName)
InitializeModuleAccs(ctx, ak)

// TODO(CORE-824): Initialize ratelimit module params to desired state.
return mm.RunMigrations(ctx, configurator, vm)
}
}
47 changes: 25 additions & 22 deletions protocol/testing/containertest/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,36 +26,34 @@ import (
const (
// When polling a node, poll every `pollFrequencyNs` and give up after `pollAttempts` attempts.
pollFrequencyNs = time.Second
pollAttempts = 30
pollAttempts = 60
cometPort = "26657/tcp"
grpcPort = "9090/tcp"
)

type Node struct {
keyring *keyring.Keyring
cometClient *comethttp.HTTP
grpcConn *grpc.ClientConn
resource *dockertest.Resource
keyring *keyring.Keyring
cometPort string
grpcPort string
resource *dockertest.Resource
}

func newNode(keyring *keyring.Keyring, resource *dockertest.Resource) (node *Node, err error) {
node = &Node{
keyring: keyring,
resource: resource,
}
cometPort := resource.GetHostPort(cometPort)
node.cometClient, err = comethttp.New("tcp://"+cometPort, "/websocket")
if err != nil {
return nil, err
}
node.cometPort = resource.GetHostPort(cometPort)
node.grpcPort = resource.GetHostPort(grpcPort)
return node, err
}

grpcPort := resource.GetHostPort(grpcPort)
node.grpcConn, err = grpc.Dial(grpcPort, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
return nil, err
}
func (n *Node) createCometClient() (*comethttp.HTTP, error) {
return comethttp.New("tcp://"+n.cometPort, "/websocket")
}

return node, err
func (n *Node) createGrpcConn() (*grpc.ClientConn, error) {
return grpc.Dial(n.grpcPort, grpc.WithTransportCredentials(insecure.NewCredentials()))
}

// Wait for current block height has advanced by at least `numBlocks`
Expand All @@ -71,11 +69,8 @@ func (n *Node) Wait(numBlocks int64) error {
func (n *Node) WaitUntilBlockHeight(height int64) error {
for i := 0; i < pollAttempts; i++ {
latestHeight, err := n.LatestBlockHeight()
if err != nil {
return err
}

if latestHeight >= height {
if err == nil && latestHeight >= height {
return nil
}
time.Sleep(pollFrequencyNs)
Expand All @@ -84,7 +79,11 @@ func (n *Node) WaitUntilBlockHeight(height int64) error {
}

func (n *Node) LatestBlockHeight() (int64, error) {
status, err := n.cometClient.Status(context.Background())
cometClient, err := n.createCometClient()
if err != nil {
return 0, err
}
status, err := cometClient.Status(context.Background())
if err != nil {
return 0, err
}
Expand Down Expand Up @@ -171,6 +170,10 @@ func Query[Request proto.Message, Response proto.Message, Client interface{}](
clientConstructor func(gogogrpc.ClientConn) Client,
requestFn func(Client, context.Context, Request, ...grpc.CallOption) (Response, error),
request Request) (proto.Message, error) {
client := clientConstructor(n.grpcConn)
conn, err := n.createGrpcConn()
if err != nil {
return nil, err
}
client := clientConstructor(conn)
return requestFn(client, context.Background(), request)
}
32 changes: 16 additions & 16 deletions protocol/testing/containertest/preupgrade_entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,30 @@ set -eo pipefail
# - Set the binary at the current commit to be the ugprade binary
# - Set the genesis to be the preupgrade genesis. This is generated by genesis generation script at the
# preupgrade commit. It is copied directly from a container.
# - Set the voting period of the preupgrade genesis to 15s.

if [[ -z "${UPGRADE_TO_VERSION}" ]]; then
echo >&2 "UPGRADE_TO_VERSION must be set"
exit 1
fi

MONIKERS=(
"alice"
"bob"
"carl"
"dave"
)
if [[ -z "${DAEMON_NAME}" ]]; then
echo >&2 "DAEMON_NAME must be set"
exit 1
fi

for i in "${!MONIKERS[@]}"; do
DAEMON_NAME="dydxprotocold"
DAEMON_HOME="$HOME/chain/.${MONIKERS[$i]}"
if [[ -z "${DAEMON_HOME}" ]]; then
echo >&2 "DAEMON_HOME must be set"
exit 1
fi

rm "$DAEMON_HOME/cosmovisor/genesis/bin/dydxprotocold"
ln -s /bin/dydxprotocold_preupgrade "$DAEMON_HOME/cosmovisor/genesis/bin/dydxprotocold"
mkdir -p "$DAEMON_HOME/cosmovisor/upgrades/$UPGRADE_TO_VERSION/bin/"
ln -s /bin/dydxprotocold "$DAEMON_HOME/cosmovisor/upgrades/$UPGRADE_TO_VERSION/bin/dydxprotocold"
rm "$DAEMON_HOME/cosmovisor/genesis/bin/dydxprotocold"
ln -s /bin/dydxprotocold_preupgrade "$DAEMON_HOME/cosmovisor/genesis/bin/dydxprotocold"
mkdir -p "$DAEMON_HOME/cosmovisor/upgrades/$UPGRADE_TO_VERSION/bin/"
ln -s /bin/dydxprotocold "$DAEMON_HOME/cosmovisor/upgrades/$UPGRADE_TO_VERSION/bin/dydxprotocold"

rm "$DAEMON_HOME/config/genesis.json"
cp "$HOME/preupgrade_genesis.json" "$DAEMON_HOME/config/genesis.json"
done
rm "$DAEMON_HOME/config/genesis.json"
cp "$HOME/preupgrade_genesis.json" "$DAEMON_HOME/config/genesis.json"
dasel put -t string -f "$DAEMON_HOME/config/genesis.json" '.app_state.gov.params.voting_period' -v '15s'

cosmovisor run "$@"
71 changes: 71 additions & 0 deletions protocol/testing/containertest/testnet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@ import (
sdkmath "cosmossdk.io/math"
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"
upgrade "github.com/cosmos/cosmos-sdk/x/upgrade/types"
"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 @@ -28,8 +31,15 @@ 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 @@ -80,6 +90,11 @@ func expectProto(t *testing.T, tag string, message proto.Message) bool {
}

func TestPlaceOrder(t *testing.T) {
// TODO(DEC-2198): Reenable these tests after fixing flakiness on CI.
// Seems to occur only because multiple container tests run.
if os.Getenv("SKIP_DISABLED") != "" {
t.Skip("Skipping disabled test")
}
testnet, err := NewTestnet()
require.NoError(t, err, "failed to create testnet - is docker daemon running?")
err = testnet.Start()
Expand Down Expand Up @@ -113,6 +128,9 @@ func TestPlaceOrder(t *testing.T) {
}

func TestBankSend(t *testing.T) {
if os.Getenv("SKIP_DISABLED") != "" {
t.Skip("Skipping disabled test")
}
testnet, err := NewTestnet()
require.NoError(t, err, "failed to create testnet - is docker daemon running?")
err = testnet.Start()
Expand Down Expand Up @@ -244,6 +262,9 @@ func assertPricesWithTimeout(t *testing.T, node *Node, marketTags map[types.Mark
}

func TestMarketPrices(t *testing.T) {
if os.Getenv("SKIP_DISABLED") != "" {
t.Skip("Skipping disabled test")
}
testnet, err := NewTestnet()
require.NoError(t, err, "failed to create testnet - is docker daemon running?")
testnet.setPrice(exchange_config.MARKET_BTC_USD, 50001)
Expand All @@ -262,3 +283,53 @@ func TestMarketPrices(t *testing.T) {
}
assertPricesWithTimeout(t, node, expectedPrices, 30*time.Second)
}

func TestUpgrade(t *testing.T) {
testnet, err := 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"]

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,
)
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)
require.NoError(t, err)
}

0 comments on commit 98e2391

Please sign in to comment.