From 63676a520a4730fcead17d2dc5fc416d351bc982 Mon Sep 17 00:00:00 2001 From: Daniel Wedul Date: Thu, 24 Oct 2024 14:12:07 -0600 Subject: [PATCH] Fix decoding of gov props with a ParameterChangeProposal in them. (#2198) * 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. --- .../bug-fixes/2198-fix-gov-props.md | 1 + app/app.go | 5 ++ app/app_test.go | 86 +++++++++++++++++++ 3 files changed, 92 insertions(+) create mode 100644 .changelog/unreleased/bug-fixes/2198-fix-gov-props.md diff --git a/.changelog/unreleased/bug-fixes/2198-fix-gov-props.md b/.changelog/unreleased/bug-fixes/2198-fix-gov-props.md new file mode 100644 index 000000000..9d55a7eda --- /dev/null +++ b/.changelog/unreleased/bug-fixes/2198-fix-gov-props.md @@ -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). diff --git a/app/app.go b/app/app.go index 76a628099..eb0693bec 100644 --- a/app/app.go +++ b/app/app.go @@ -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" @@ -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, diff --git a/app/app_test.go b/app/app_test.go index 737fc17a7..0dacb9e20 100644 --- a/app/app_test.go +++ b/app/app_test.go @@ -5,6 +5,7 @@ import ( "fmt" "sort" "testing" + "time" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -16,6 +17,7 @@ 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" @@ -23,6 +25,8 @@ import ( 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" @@ -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 := ¶mprops.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") +}