Skip to content

Commit

Permalink
Fix decoding of gov props with a ParameterChangeProposal in them. (#2198
Browse files Browse the repository at this point in the history
)

* Write a unit test that fails to parse a gov proposal with a ParameterChangeProposal in it because that type isn't being registered anymore.

* Register the params module stuff with the codecs since there's some gov props with a ParameterChangeProposal in them.

* Add changelog entry.
  • Loading branch information
SpicyLemon committed Oct 24, 2024
1 parent f6b6290 commit 63676a5
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 0 deletions.
1 change: 1 addition & 0 deletions .changelog/unreleased/bug-fixes/2198-fix-gov-props.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* Register the params types with the codecs so old gov props can be read [PR 2198](https://github.com/provenance-io/provenance/pull/2198).
5 changes: 5 additions & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ import (
"github.com/cosmos/cosmos-sdk/x/mint"
mintkeeper "github.com/cosmos/cosmos-sdk/x/mint/keeper"
minttypes "github.com/cosmos/cosmos-sdk/x/mint/types"
paramprops "github.com/cosmos/cosmos-sdk/x/params/types/proposal" //nolint:depguard // Need this here to register old types.
"github.com/cosmos/cosmos-sdk/x/slashing"
slashingkeeper "github.com/cosmos/cosmos-sdk/x/slashing/keeper"
slashingtypes "github.com/cosmos/cosmos-sdk/x/slashing/types"
Expand Down Expand Up @@ -786,6 +787,10 @@ func New(
app.BasicModuleManager.RegisterLegacyAminoCodec(legacyAmino)
app.BasicModuleManager.RegisterInterfaces(interfaceRegistry)

// We removed the params module, but have several gov props with a ParameterChangeProposal in them.
paramprops.RegisterLegacyAminoCodec(legacyAmino)
paramprops.RegisterInterfaces(interfaceRegistry)

// NOTE: upgrade module is required to be prioritized
app.mm.SetOrderPreBlockers(
upgradetypes.ModuleName,
Expand Down
86 changes: 86 additions & 0 deletions app/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"sort"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand All @@ -16,13 +17,16 @@ import (

dbm "github.com/cosmos/cosmos-db"
"github.com/cosmos/cosmos-sdk/baseapp"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
sdktypes "github.com/cosmos/cosmos-sdk/codec/types"
simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/msgservice"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
distrtypes "github.com/cosmos/cosmos-sdk/x/distribution/types"
govtypesv1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1"
paramprops "github.com/cosmos/cosmos-sdk/x/params/types/proposal"
"github.com/cosmos/gogoproto/proto"

"github.com/provenance-io/provenance/internal/pioconfig"
Expand Down Expand Up @@ -440,3 +444,85 @@ func TestMsgServerProtoAnnotations(t *testing.T) {
err = msgservice.ValidateProtoAnnotations(protoFiles)
assertions.AssertErrorValue(t, err, expErr, "ValidateProtoAnnotations")
}

func TestParamChangeInGovProp(t *testing.T) {
paramChangeProp := &paramprops.ParameterChangeProposal{
Title: "Test Prop Change",
Description: "A proposal for testing decoding",
Changes: []paramprops.ParamChange{
{
Subspace: "mymodule",
Key: "favorite_flower",
Value: "fuchsia",
},
},
}
paramChangePropAny, err := codectypes.NewAnyWithValue(paramChangeProp)
require.NoError(t, err, "codectypes.NewAnyWithValue(paramChangeProp)")
t.Logf("paramChangePropAny.TypeUrl = %q", paramChangePropAny.TypeUrl)

execLeg := &govtypesv1.MsgExecLegacyContent{
Content: paramChangePropAny,
Authority: "jerry",
}

execLegAny, err := codectypes.NewAnyWithValue(execLeg)
require.NoError(t, err, "codectypes.NewAnyWithValue(execLeg)")

submitTime := time.Unix(1618935600, 0)
depositEndTime := submitTime.Add(1 * time.Minute)
votingStartTime := depositEndTime.Add(3500 * time.Millisecond)
votingEndTime := votingStartTime.Add(48 * time.Hour)

prop := govtypesv1.Proposal{
Id: 123,
Messages: []*codectypes.Any{execLegAny},
Status: govtypesv1.StatusPassed,
FinalTallyResult: &govtypesv1.TallyResult{YesCount: "5", AbstainCount: "1", NoCount: "0", NoWithVetoCount: "0"},
SubmitTime: &submitTime,
DepositEndTime: &depositEndTime,
TotalDeposit: []sdk.Coin{sdk.NewInt64Coin("pink", 1000)},
VotingStartTime: &votingStartTime,
VotingEndTime: &votingEndTime,
Metadata: "Prop metadata",
Title: "The prop title",
Summary: "The prop summary",
Proposer: sdk.AccAddress("proposer____________").String(),
}

expJSON := `{"id":"123",` +
`"messages":[{"@type":"/cosmos.gov.v1.MsgExecLegacyContent",` +
`"content":{"@type":"/cosmos.params.v1beta1.ParameterChangeProposal",` +
`"title":"Test Prop Change",` +
`"description":"A proposal for testing decoding",` +
`"changes":[{"subspace":"mymodule","key":"favorite_flower","value":"fuchsia"}]},` +
`"authority":"jerry"}],` +
`"status":"PROPOSAL_STATUS_PASSED",` +
`"final_tally_result":{"yes_count":"5","abstain_count":"1","no_count":"0","no_with_veto_count":"0"},` +
`"submit_time":"2021-04-20T16:20:00Z",` +
`"deposit_end_time":"2021-04-20T16:21:00Z",` +
`"total_deposit":[{"denom":"pink","amount":"1000"}],` +
`"voting_start_time":"2021-04-20T16:21:03.500Z",` +
`"voting_end_time":"2021-04-22T16:21:03.500Z",` +
`"metadata":"Prop metadata",` +
`"title":"The prop title",` +
`"summary":"The prop summary",` +
`"proposer":"cosmos1wpex7ur0wdjhyh6lta047h6lta047h6ljkx24t",` +
`"expedited":false,` +
`"failed_reason":""}`

propBz, err := prop.Marshal()
require.NoError(t, err, "prop.Marshal()")

encCfg := MakeTestEncodingConfig(t)

var actProp govtypesv1.Proposal
err = encCfg.Marshaler.Unmarshal(propBz, &actProp)
require.NoError(t, err, "encCfg.Marshaler.Unmarshal(propBz, &actProp)")

propJSONBz, err := encCfg.Marshaler.MarshalJSON(&actProp)
require.NoError(t, err, "encCfg.Marshaler.MarshalJSON(&actProp)")
propJSON := string(propJSONBz)
t.Logf("prop JSON:\n%s", propJSON)
assert.Equal(t, expJSON, propJSON, "proposal JSON")
}

0 comments on commit 63676a5

Please sign in to comment.