Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds ParamChangeProposal #467

Merged
merged 2 commits into from
Mar 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions chain/cosmos/chain_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -767,6 +767,20 @@ func (tn *ChainNode) QueryProposal(ctx context.Context, proposalID string) (*Pro
return &proposal, nil
}

// QueryParam returns the state and details of a subspace param.
func (tn *ChainNode) QueryParam(ctx context.Context, subspace, key string) (*ParamChange, error) {
stdout, _, err := tn.ExecQuery(ctx, "params", "subspace", subspace, key)
if err != nil {
return nil, err
}
var param ParamChange
err = json.Unmarshal(stdout, &param)
if err != nil {
return nil, err
}
return &param, nil
}

// UpgradeProposal submits a software-upgrade governance proposal to the chain.
func (tn *ChainNode) UpgradeProposal(ctx context.Context, keyName string, prop SoftwareUpgradeProposal) (string, error) {
command := []string{
Expand All @@ -785,6 +799,29 @@ func (tn *ChainNode) UpgradeProposal(ctx context.Context, keyName string, prop S
return tn.ExecTx(ctx, keyName, command...)
}

// ParamChangeProposal submits a param-change governance proposal to the chain.
func (tn *ChainNode) ParamChangeProposal(ctx context.Context, keyName string, prop ParamChangeProposal) (string, error) {
propBz, err := json.Marshal(prop)
if err != nil {
return "", err
}

fileName := "param-change.json"

fw := dockerutil.NewFileWriter(tn.logger(), tn.DockerClient, tn.TestName)
if err := fw.WriteFile(ctx, tn.VolumeName, fileName, propBz); err != nil {
return "", fmt.Errorf("failure writing proposal json: %w", err)
}

filePath := filepath.Join(tn.HomeDir(), fileName)

command := []string{
"gov", "submit-proposal",
"param-change", filePath,
}
return tn.ExecTx(ctx, keyName, command...)
}

// TextProposal submits a text governance proposal to the chain.
func (tn *ChainNode) TextProposal(ctx context.Context, keyName string, prop TextProposal) (string, error) {
command := []string{
Expand Down
26 changes: 26 additions & 0 deletions chain/cosmos/cosmos_chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,11 @@ func (c *CosmosChain) QueryProposal(ctx context.Context, proposalID string) (*Pr
return c.getFullNode().QueryProposal(ctx, proposalID)
}

// QueryParam returns the param state of a given key.
func (c *CosmosChain) QueryParam(ctx context.Context, subspace, key string) (*ParamChange, error) {
return c.getFullNode().QueryParam(ctx, subspace, key)
}

// UpgradeProposal submits a software-upgrade governance proposal to the chain.
func (c *CosmosChain) UpgradeProposal(ctx context.Context, keyName string, prop SoftwareUpgradeProposal) (tx TxProposal, _ error) {
txHash, err := c.getFullNode().UpgradeProposal(ctx, keyName, prop)
Expand All @@ -313,6 +318,26 @@ func (c *CosmosChain) UpgradeProposal(ctx context.Context, keyName string, prop
return c.txProposal(txHash)
}

// ParamChangeProposal submits a param-change governance proposal to the chain.
func (c *CosmosChain) ParamChangeProposal(ctx context.Context, keyName, fileLocation string) (tx TxProposal, _ error) {
dat, err := os.ReadFile(fileLocation)
if err != nil {
return tx, fmt.Errorf("failed to read file: %w", err)
}

var paramChange ParamChangeProposal
err = json.Unmarshal(dat, &paramChange)
if err != nil {
return tx, fmt.Errorf("failed to unmarshal JSON: %w", err)
}

txHash, err := c.getFullNode().ParamChangeProposal(ctx, keyName, paramChange)
if err != nil {
return tx, fmt.Errorf("failed to submit param-change proposal: %w", err)
}
return c.txProposal(txHash)
}

// TextProposal submits a text governance proposal to the chain.
func (c *CosmosChain) TextProposal(ctx context.Context, keyName string, prop TextProposal) (tx TxProposal, _ error) {
txHash, err := c.getFullNode().TextProposal(ctx, keyName, prop)
Expand All @@ -336,6 +361,7 @@ func (c *CosmosChain) txProposal(txHash string) (tx TxProposal, _ error) {
if err != nil {
return tx, fmt.Errorf("failed to get transaction %s: %w", txHash, err)
}

tx.Height = uint64(txResp.Height)
tx.TxHash = txHash
// In cosmos, user is charged for entire gas requested, not the actual gas used.
Expand Down
13 changes: 13 additions & 0 deletions chain/cosmos/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,19 @@ type ProposalResponse struct {
VotingEndTime string `json:"voting_end_time"`
}

type ParamChange struct {
Subspace string `json:"subspace"`
Key string `json:"key"`
Value any `json:"value"`
}

type ParamChangeProposal struct {
Title string `json:"title"`
Description string `json:"description"`
Changes []ParamChange `json:"changes"`
Deposit string `json:"deposit"`
}

type ProposalContent struct {
Type string `json:"@type"`
Title string `json:"title"`
Expand Down
88 changes: 88 additions & 0 deletions examples/cosmos/chain_param_change_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package cosmos_test

import (
"context"
"os"
"path"
"testing"

"github.com/strangelove-ventures/interchaintest/v4"
"github.com/strangelove-ventures/interchaintest/v4/chain/cosmos"
"github.com/strangelove-ventures/interchaintest/v4/ibc"
"github.com/stretchr/testify/require"
"go.uber.org/zap/zaptest"
)

func TestJunoParamChange(t *testing.T) {
CosmosChainParamChangeTest(t, "juno", junoVersion)
}

func CosmosChainParamChangeTest(t *testing.T, name, version string) {
if testing.Short() {
t.Skip("skipping in short mode")
}

t.Parallel()

numVals := 1
numFullNodes := 1

cf := interchaintest.NewBuiltinChainFactory(zaptest.NewLogger(t), []*interchaintest.ChainSpec{
{
Name: name,
ChainName: name,
Version: version,
ChainConfig: ibc.ChainConfig{
Denom: "ujuno",
ModifyGenesis: cosmos.ModifyGenesisProposalTime(votingPeriod, maxDepositPeriod),
},
NumValidators: &numVals,
NumFullNodes: &numFullNodes,
},
})

chains, err := cf.Chains(t.Name())
require.NoError(t, err)

chain := chains[0].(*cosmos.CosmosChain)

ic := interchaintest.NewInterchain().
AddChain(chain)

ctx := context.Background()
client, network := interchaintest.DockerSetup(t)

require.NoError(t, ic.Build(ctx, nil, interchaintest.InterchainBuildOptions{
TestName: t.Name(),
Client: client,
NetworkID: network,
// BlockDatabaseFile: interchaintest.DefaultBlockDatabaseFilepath(),
SkipPathCreation: true,
}))
t.Cleanup(func() {
_ = ic.Close()
})

const userFunds = int64(10_000_000_000)
users := interchaintest.GetAndFundTestUsers(t, ctx, t.Name(), userFunds, chain)
chainUser := users[0]

param, _ := chain.QueryParam(ctx, "staking", "MaxValidators")
require.Equal(t, "100", param.Value, "MaxValidators value is not 100")

current_directory, _ := os.Getwd()
param_change_path := path.Join(current_directory, "params", "IncreaseValidatorsParam.json")

paramTx, err := chain.ParamChangeProposal(ctx, chainUser.KeyName, param_change_path)
require.NoError(t, err, "error submitting param change proposal tx")

err = chain.VoteOnProposalAllValidators(ctx, paramTx.ProposalID, cosmos.ProposalVoteYes)
require.NoError(t, err, "failed to submit votes")

height, _ := chain.Height(ctx)
_, err = cosmos.PollForProposalStatus(ctx, chain, height, height+10, paramTx.ProposalID, cosmos.ProposalStatusPassed)
require.NoError(t, err, "proposal status did not change to passed in expected number of blocks")

param, _ = chain.QueryParam(ctx, "staking", "MaxValidators")
require.Equal(t, "110", param.Value, "MaxValidators value is not 110")
}
12 changes: 12 additions & 0 deletions examples/cosmos/params/IncreaseValidatorsParam.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"title": "Increase validator set to 110",
"description": ".",
"changes": [
{
"subspace": "staking",
"key": "MaxValidators",
"value": 110
}
],
"deposit": "10000000ujuno"
}
1 change: 1 addition & 0 deletions examples/cosmos/versions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ package cosmos_test
const (
gaiaVersion = "v7.1.0"
osmosisVersion = "v12.2.0"
junoVersion = "v13.0.1"
)