Skip to content

Commit

Permalink
chore: e2e upgrade read height from proposal file
Browse files Browse the repository at this point in the history
  • Loading branch information
RafilxTenfen committed Aug 19, 2024
1 parent 93fc9d8 commit 6ea859b
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 11 deletions.
83 changes: 81 additions & 2 deletions test/e2e/configurer/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@ import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"sync"
"testing"
"time"

govv1 "cosmossdk.io/api/cosmos/gov/v1"
sdkmath "cosmossdk.io/math"
upgradetypes "cosmossdk.io/x/upgrade/types"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/babylonlabs-io/babylon/app"
appparams "github.com/babylonlabs-io/babylon/app/params"
"github.com/babylonlabs-io/babylon/test/e2e/configurer/chain"
"github.com/babylonlabs-io/babylon/test/e2e/configurer/config"
Expand Down Expand Up @@ -196,9 +200,13 @@ func (uc *UpgradeConfigurer) runProposalUpgrade() error {
// }
// TODO: need to make a way to update proposal height
// chainConfig.UpgradePropHeight = currentHeight + int64(chainConfig.VotingPeriod) + int64(config.PropSubmitBlocks) + int64(config.PropBufferBlocks)
chainConfig.UpgradePropHeight = 25 // at least read from the prop plan file
propID := node.TxGovPropSubmitProposal(uc.upgradeJsonFilePath, node.WalletName)
msgProp, err := uc.ParseGovPropFromFile()
if err != nil {
return err
}
chainConfig.UpgradePropHeight = msgProp.Plan.Height

propID := node.TxGovPropSubmitProposal(uc.upgradeJsonFilePath, node.WalletName)
chainConfig.TxGovVoteFromAllNodes(propID, govv1.VoteOption_VOTE_OPTION_YES)
}

Expand Down Expand Up @@ -252,3 +260,74 @@ func (uc *UpgradeConfigurer) upgradeContainers(chainConfig *chain.Config, propHe
uc.t.Logf("upgrade successful on chain %s", chainConfig.Id)
return nil
}

// ParseGovPropFromFile loads the proposal from the UpgradeSignetLaunchFilePath
func (uc *UpgradeConfigurer) ParseGovPropFromFile() (*upgradetypes.MsgSoftwareUpgrade, error) {
pwd, err := os.Getwd()
if err != nil {
return nil, err
}

upgradePath := filepath.Join(pwd, uc.upgradeJsonFilePath)
return ParseGovPropFromFile(upgradePath)
}

// ParseGovPropFromFile loads from the file the Upgrade msg.
func ParseGovPropFromFile(propFilePath string) (*upgradetypes.MsgSoftwareUpgrade, error) {
_, msgs, _, err := parseSubmitProposal(app.NewTmpBabylonApp().AppCodec(), propFilePath)
if err != nil {
return nil, err
}

upgradeMsg, ok := msgs[0].(*upgradetypes.MsgSoftwareUpgrade)
if !ok {
return nil, fmt.Errorf("unable to parse msg to upgradetypes.MsgSoftwareUpgrade")
}
return upgradeMsg, nil
}

// Copy from https://github.com/cosmos/cosmos-sdk/blob/4251905d56e0e7a3350145beedceafe786953295/x/gov/client/cli/util.go#L83
// Not exported structure and file
// proposal defines the new Msg-based proposal.
type proposal struct {
// Msgs defines an array of sdk.Msgs proto-JSON-encoded as Anys.
Messages []json.RawMessage `json:"messages,omitempty"`
Metadata string `json:"metadata"`
Deposit string `json:"deposit"`
Title string `json:"title"`
Summary string `json:"summary"`
Expedited bool `json:"expedited"`
}

// parseSubmitProposal reads and parses the proposal.
func parseSubmitProposal(cdc codec.Codec, path string) (proposal, []sdk.Msg, sdk.Coins, error) {
var proposal proposal

contents, err := os.ReadFile(path)
if err != nil {
return proposal, nil, nil, err
}

err = json.Unmarshal(contents, &proposal)
if err != nil {
return proposal, nil, nil, err
}

msgs := make([]sdk.Msg, len(proposal.Messages))
for i, anyJSON := range proposal.Messages {
var msg sdk.Msg
err := cdc.UnmarshalInterfaceJSON(anyJSON, &msg)
if err != nil {
return proposal, nil, nil, err
}

msgs[i] = msg
}

deposit, err := sdk.ParseCoinsNormalized(proposal.Deposit)
if err != nil {
return proposal, nil, nil, err
}

return proposal, msgs, deposit, nil
}
24 changes: 24 additions & 0 deletions test/e2e/configurer/upgrade_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package configurer_test

import (
"os"
"path/filepath"
"testing"

v1 "github.com/babylonlabs-io/babylon/app/upgrades/signetlaunch"
"github.com/babylonlabs-io/babylon/test/e2e/configurer"
"github.com/babylonlabs-io/babylon/test/e2e/configurer/config"
"github.com/stretchr/testify/require"
)

func TestParseGovPropFromFile(t *testing.T) {
pwd, err := os.Getwd()
require.NoError(t, err)
upgradePath := filepath.Join(pwd, "../", config.UpgradeSignetLaunchFilePath)

prop, err := configurer.ParseGovPropFromFile(upgradePath)
require.NoError(t, err)

require.Equal(t, prop.Plan.Name, v1.Upgrade.UpgradeName)
require.Equal(t, prop.Plan.Height, int64(25))
}
19 changes: 13 additions & 6 deletions test/e2e/software_upgrade_e2e_signet_launch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
type SoftwareUpgradeSignetLaunchTestSuite struct {
suite.Suite

configurer configurer.Configurer
configurer *configurer.UpgradeConfigurer
}

func (s *SoftwareUpgradeSignetLaunchTestSuite) SetupSuite() {
Expand All @@ -23,17 +23,22 @@ func (s *SoftwareUpgradeSignetLaunchTestSuite) SetupSuite() {

btcHeaderGenesis, err := app.SignetBtcHeaderGenesis(app.NewTmpBabylonApp().AppCodec())
s.NoError(err)
s.configurer, err = configurer.NewSoftwareUpgradeConfigurer(s.T(), false, config.UpgradeSignetLaunchFilePath, []*btclighttypes.BTCHeaderInfo{btcHeaderGenesis})

cfg, err := configurer.NewSoftwareUpgradeConfigurer(s.T(), true, config.UpgradeSignetLaunchFilePath, []*btclighttypes.BTCHeaderInfo{btcHeaderGenesis})
s.NoError(err)
s.configurer = cfg.(*configurer.UpgradeConfigurer)

err = s.configurer.ConfigureChains()
s.NoError(err)
err = s.configurer.RunSetup() // upgrade happens at the setup of configurer.
s.NoError(err)
s.Require().NoError(err)
}

func (s *SoftwareUpgradeSignetLaunchTestSuite) TearDownSuite() {
err := s.configurer.ClearResources()
s.Require().NoError(err)
if err != nil {
s.T().Logf("error to clear resources %s", err.Error())
}
}

// TestUpgradeSignetLaunch Checks if the BTC Headers were inserted.
Expand All @@ -45,11 +50,13 @@ func (s *SoftwareUpgradeSignetLaunchTestSuite) TestUpgradeSignetLaunch() {
n, err := chainA.GetDefaultNode()
s.NoError(err)

expectedUpgradeHeight := int64(25)
govProp, err := s.configurer.ParseGovPropFromFile()
s.NoError(err)

// makes sure that the upgrade was actually executed
expectedUpgradeHeight := govProp.Plan.Height
resp := n.QueryAppliedPlan(v1.Upgrade.UpgradeName)
s.EqualValues(expectedUpgradeHeight, resp.Height, "the plan should be applied at the height 25")
s.EqualValues(expectedUpgradeHeight, resp.Height, "the plan should be applied at the height %d", expectedUpgradeHeight)

btcHeadersInserted, err := v1.LoadBTCHeadersFromData()
s.NoError(err)
Expand Down
4 changes: 1 addition & 3 deletions test/e2e/upgrades/signet-launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,5 @@
"plan": { "name": "signet-launch", "info": "Msg info", "height": 25 }
}
],
"deposit": "500000000ubbn",
"initial_deposit": "500000000ubbn",
"initialDeposit": "500000000ubbn"
"deposit": "500000000ubbn"
}

0 comments on commit 6ea859b

Please sign in to comment.