From 7a87f2bb15af1aa51f69e52bd7de4e8c5923427f Mon Sep 17 00:00:00 2001 From: Julien Robert Date: Mon, 3 Jun 2024 11:33:40 +0200 Subject: [PATCH] refactor(x/gov): set environment in context for legacy proposals (#20521) --- UPGRADING.md | 1 + x/gov/CHANGELOG.md | 1 + x/gov/keeper/msg_server.go | 6 +++++- x/gov/keeper/proposal.go | 5 ++++- 4 files changed, 11 insertions(+), 2 deletions(-) diff --git a/UPGRADING.md b/UPGRADING.md index d5c9f75ff98f..48e2db4164c4 100644 --- a/UPGRADING.md +++ b/UPGRADING.md @@ -688,6 +688,7 @@ To learn more see the [docs](https://docs.cosmos.network/main/learn/advanced/tra * mention changes with sdk context removal * mention changes with environment * mention changes with environment in context in interfaces + * mention legacy proposal in gov when using server/v2 if using sdk context must be rewritten --> #### `**all**` diff --git a/x/gov/CHANGELOG.md b/x/gov/CHANGELOG.md index eb792c01ed2b..4570b966a1b5 100644 --- a/x/gov/CHANGELOG.md +++ b/x/gov/CHANGELOG.md @@ -38,6 +38,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### Improvements +* [#20521](https://github.com/cosmos/cosmos-sdk/pull/20521) Legacy proposals can now access the `appmodule.Environment` present in the `context.Context` of the handler. This is useful when migrating to server/v2 and removing the sdk context dependency. * [#19741](https://github.com/cosmos/cosmos-sdk/pull/19741) Add `ExpeditedQuorum` parameter specifying a minimum quorum for expedited proposals, that can differ from the regular quorum. * [#19352](https://github.com/cosmos/cosmos-sdk/pull/19352) `TallyResult` include vote options counts. Those counts replicates the now deprecated (but not removed) yes, no, abstain and veto count fields. * [#18976](https://github.com/cosmos/cosmos-sdk/pull/18976) Log and send an event when a proposal deposit refund or burn has failed. diff --git a/x/gov/keeper/msg_server.go b/x/gov/keeper/msg_server.go index 811d99c618d1..ec3e33a44c3d 100644 --- a/x/gov/keeper/msg_server.go +++ b/x/gov/keeper/msg_server.go @@ -7,6 +7,7 @@ import ( "google.golang.org/protobuf/runtime/protoiface" + corecontext "cosmossdk.io/core/context" "cosmossdk.io/core/event" "cosmossdk.io/errors" "cosmossdk.io/math" @@ -209,7 +210,10 @@ func (k msgServer) ExecLegacyContent(ctx context.Context, msg *v1.MsgExecLegacyC } handler := k.Keeper.legacyRouter.GetRoute(content.ProposalRoute()) - if err := handler(ctx, content); err != nil { + + // NOTE: the support of legacy gov proposal in server/v2 is different than for baseapp. + // Legacy proposal in server/v2 can only access services provided by the gov module environment. + if err := handler(context.WithValue(ctx, corecontext.EnvironmentContextKey, k.Environment), content); err != nil { return nil, errors.Wrapf(govtypes.ErrInvalidProposalContent, "failed to run legacy handler %s, %+v", content.ProposalRoute(), err) } diff --git a/x/gov/keeper/proposal.go b/x/gov/keeper/proposal.go index 08355071d582..4f378e6fcab9 100644 --- a/x/gov/keeper/proposal.go +++ b/x/gov/keeper/proposal.go @@ -10,6 +10,7 @@ import ( "time" "cosmossdk.io/collections" + corecontext "cosmossdk.io/core/context" "cosmossdk.io/core/event" errorsmod "cosmossdk.io/errors" sdkmath "cosmossdk.io/math" @@ -113,7 +114,9 @@ func (k Keeper) SubmitProposal(ctx context.Context, messages []sdk.Msg, metadata if err = k.BranchService.Execute(ctx, func(ctx context.Context) error { handler := k.legacyRouter.GetRoute(content.ProposalRoute()) - if err := handler(ctx, content); err != nil { + // NOTE: the support of legacy gov proposal in server/v2 is different than for baseapp. + // Legacy proposal in server/v2 can only access services provided by the gov module environment. + if err := handler(context.WithValue(ctx, corecontext.EnvironmentContextKey, k.Environment), content); err != nil { return types.ErrInvalidProposalContent.Wrapf("failed to run legacy handler %s, %+v", content.ProposalRoute(), err) }