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

x/gov: Proto Tx CLI command #5942

Merged
merged 42 commits into from
Apr 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
2b8afe5
WIP on x/gov protobuf cli tx cmd's
aaronc Apr 6, 2020
40143b4
Implement gov.Codec in codec/std
aaronc Apr 6, 2020
ecf248a
Add protobuf x/gov REST tx handlers
aaronc Apr 7, 2020
23b7c90
Add deprecation notice and RegisterHandlers
aaronc Apr 7, 2020
6801eeb
Optimize imports
aaronc Apr 8, 2020
03c594a
Reorder imports
aaronc Apr 8, 2020
aa27dd1
Fix imports
aaronc Apr 8, 2020
a4dab80
Switch to non-codec based handling of NewMsgSubmitProposal
aaronc Apr 8, 2020
9437697
Tweak gov.Codec interface
aaronc Apr 9, 2020
43ce8b3
Fix errors and update tests
aaronc Apr 9, 2020
e54dd31
Merge branch 'master' into aaronc/5864-x-gov
aaronc Apr 9, 2020
22ea39c
Fix tests
aaronc Apr 9, 2020
7f6fbc1
Merge branch 'master' into aaronc/5864-x-gov
aaronc Apr 9, 2020
dd4d834
Fix simapp
aaronc Apr 9, 2020
267383b
Update ADR and docs
aaronc Apr 9, 2020
c0f9965
Resolve cyclic import issues
aaronc Apr 9, 2020
b2d2a22
Handle pointer proposal types because of how protobuf oneof's are gen…
aaronc Apr 9, 2020
6183dc7
Fix errors
aaronc Apr 9, 2020
978bca5
Fix tests
aaronc Apr 10, 2020
63354d0
Spelling
aaronc Apr 10, 2020
83ff6cc
Merge branch 'master' into aaronc/5864-x-gov
aaronc Apr 10, 2020
b85ae8c
Merge branch 'aaronc/5864-x-gov' of github.com:cosmos/cosmos-sdk into…
aaronc Apr 10, 2020
0a65118
Make MsgSubmitProposal implement MsgSubmitProposalI again so that ami…
aaronc Apr 10, 2020
5dd7c66
Merge branch 'master' into aaronc/5864-x-gov
aaronc Apr 10, 2020
57961df
Always use pointer types for gov.Content instances
aaronc Apr 10, 2020
63580d3
Fix test
aaronc Apr 10, 2020
71e9e6e
Fix test
aaronc Apr 10, 2020
4366886
Remove unused constructor
aaronc Apr 10, 2020
96feec9
Add invalid test case to fix coverage gap
aaronc Apr 10, 2020
1cad064
Merge branch 'master' into aaronc/5864-x-gov
aaronc Apr 10, 2020
35f3662
Update ADR 020 Changelog
aaronc Apr 10, 2020
218de47
Fix golangci issue
aaronc Apr 10, 2020
06ca9fa
Regenerate proto files
aaronc Apr 13, 2020
4a91774
Merge branch 'master' into aaronc/5864-x-gov
aaronc Apr 13, 2020
7a99bd7
Refactor MsgSubmitProposalI constructor out of gov.Codec and into sta…
aaronc Apr 13, 2020
64e194c
Fix lint error
aaronc Apr 13, 2020
609f99f
Update ADR
aaronc Apr 13, 2020
620f436
Update ADR
aaronc Apr 13, 2020
029a5e6
Update docs/architecture/adr-020-protobuf-transaction-encoding.md
aaronc Apr 14, 2020
7a374d5
Address PR feedback
aaronc Apr 14, 2020
9d182fc
Merge branch 'master' into aaronc/5864-x-gov
aaronc Apr 14, 2020
cac9b12
Rename ctr to newMsgFn
aaronc Apr 14, 2020
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
2 changes: 1 addition & 1 deletion codec/std/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/cosmos/cosmos-sdk/x/auth/vesting"
"github.com/cosmos/cosmos-sdk/x/evidence"
eviexported "github.com/cosmos/cosmos-sdk/x/evidence/exported"
"github.com/cosmos/cosmos-sdk/x/gov"
gov "github.com/cosmos/cosmos-sdk/x/gov/types"
"github.com/cosmos/cosmos-sdk/x/supply"
supplyexported "github.com/cosmos/cosmos-sdk/x/supply/exported"
)
Expand Down
203 changes: 94 additions & 109 deletions codec/std/codec.pb.go

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion codec/std/codec.proto
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ message Proposal {
// governance proposal.
message Content {
option (gogoproto.equal) = true;
option (cosmos_proto.interface_type) = "github.com/cosmos/cosmos-sdk/x/gov/types.Content";
option (cosmos_proto.interface_type) = "*github.com/cosmos/cosmos-sdk/x/gov/types.Content";

// sum defines a set of all acceptable concrete governance proposal Content types.
oneof sum {
Expand Down
23 changes: 15 additions & 8 deletions codec/std/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import (
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/x/evidence"
eviexported "github.com/cosmos/cosmos-sdk/x/evidence/exported"
"github.com/cosmos/cosmos-sdk/x/gov"
gov "github.com/cosmos/cosmos-sdk/x/gov/types"
)

var (
_ eviexported.MsgSubmitEvidence = MsgSubmitEvidence{}
_ gov.MsgSubmitProposalI = MsgSubmitProposal{}
_ gov.MsgSubmitProposalI = &MsgSubmitProposal{}
aaronc marked this conversation as resolved.
Show resolved Hide resolved
)

// NewMsgSubmitEvidence returns a new MsgSubmitEvidence.
Expand Down Expand Up @@ -47,13 +47,13 @@ func (msg MsgSubmitEvidence) GetEvidence() eviexported.Evidence { return msg.Evi
func (msg MsgSubmitEvidence) GetSubmitter() sdk.AccAddress { return msg.Submitter }

// NewMsgSubmitProposal returns a new MsgSubmitProposal.
func NewMsgSubmitProposal(c gov.Content, d sdk.Coins, p sdk.AccAddress) (MsgSubmitProposal, error) {
aaronc marked this conversation as resolved.
Show resolved Hide resolved
func NewMsgSubmitProposal(c gov.Content, d sdk.Coins, p sdk.AccAddress) (gov.MsgSubmitProposalI, error) {
content := &Content{}
if err := content.SetContent(c); err != nil {
return MsgSubmitProposal{}, err
return nil, err
}

return MsgSubmitProposal{
return &MsgSubmitProposal{
Content: content,
MsgSubmitProposalBase: gov.NewMsgSubmitProposalBase(d, p),
}, nil
Expand All @@ -79,6 +79,13 @@ func (msg MsgSubmitProposal) ValidateBasic() error {
}

// nolint
func (msg MsgSubmitProposal) GetContent() gov.Content { return msg.Content.GetContent() }
func (msg MsgSubmitProposal) GetInitialDeposit() sdk.Coins { return msg.InitialDeposit }
func (msg MsgSubmitProposal) GetProposer() sdk.AccAddress { return msg.Proposer }
func (msg *MsgSubmitProposal) GetContent() gov.Content { return msg.Content.GetContent() }
func (msg *MsgSubmitProposal) SetContent(content gov.Content) error {
stdContent := &Content{}
err := stdContent.SetContent(content)
if err != nil {
return err
}
msg.Content = stdContent
return nil
}
39 changes: 35 additions & 4 deletions codec/std/msgs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import (
"testing"
"time"

gov "github.com/cosmos/cosmos-sdk/x/gov/types"

"github.com/stretchr/testify/require"

"github.com/cosmos/cosmos-sdk/codec/std"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/evidence"
"github.com/cosmos/cosmos-sdk/x/gov"
)

func TestNewMsgSubmitEvidence(t *testing.T) {
Expand All @@ -28,15 +29,45 @@ func TestNewMsgSubmitEvidence(t *testing.T) {
require.NoError(t, msg.ValidateBasic())
}

func TestNewNewMsgSubmitProposal(t *testing.T) {
type invalidProposal struct {
*gov.TextProposal
}

func TestMsgSubmitProposal(t *testing.T) {
p := sdk.AccAddress("foo")
d := sdk.NewCoins(sdk.NewInt64Coin("stake", 1000))
c := gov.TextProposal{Title: "title", Description: "description"}
c := gov.NewTextProposal("title", "description")

//
// test constructor
//

msg, err := std.NewMsgSubmitProposal(c, d, p)
require.NoError(t, err)
require.Equal(t, msg.GetContent(), &c)
require.Equal(t, msg.GetContent(), c)
require.Equal(t, msg.GetProposer(), p)
require.Equal(t, msg.GetInitialDeposit(), d)
require.NoError(t, msg.ValidateBasic())

_, err = std.NewMsgSubmitProposal(invalidProposal{}, d, p)
require.Error(t, err)

//
// test setter methods
//

msg = &std.MsgSubmitProposal{}
msg.SetProposer(p)
msg.SetInitialDeposit(d)
err = msg.SetContent(c)
require.NoError(t, err)
require.Equal(t, msg.GetContent(), c)
require.Equal(t, msg.GetProposer(), p)
require.Equal(t, msg.GetInitialDeposit(), d)
require.NoError(t, msg.ValidateBasic())

msg = &std.MsgSubmitProposal{}
err = msg.SetContent(invalidProposal{})
require.Error(t, err)

}
40 changes: 37 additions & 3 deletions docs/architecture/adr-020-protobuf-transaction-encoding.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- 2020 March 06: Initial Draft
- 2020 March 12: API Updates
- 2020 April 13: Added details on interface `oneof` handling

## Status

Expand Down Expand Up @@ -132,9 +133,42 @@ We then update `CLIContext` to have a new field: `Marshaler`.

Then, each module's client handler will at the minimum accept a `Marshaler` instead
of a concrete Amino codec and a `Generator` along with an `AccountRetriever` so
that account fields can be retrieved for signing. If the module needs to work with
any interface types, it will use the `Codec` interface defined by the module which
also extends `Marshaler`.
that account fields can be retrieved for signing.

#### Interface `oneof` Handling

If the module needs to work with any `sdk.Msg`s that use interface types, that
`sdk.Msg` should be implemented as an interface with getters and setters on the
module level and a no-arg constructor function should be passed around to
required CLI and REST client commands.

For example, in `x/gov`, `Content` is an interface type, so `MsgSubmitProposalI`
should also be an interface and implement setter methods:

```go
// x/gov/types/msgs.go
type MsgSubmitProposalI interface {
sdk.Msg

GetContent() Content
// SetContent returns an error if the underlying oneof does not support
// the concrete Content passed in
SetContent(Content) error

GetInitialDeposit() sdk.Coins
SetInitialDeposit(sdk.Coins)

GetProposer() sdk.AccAddress
SetProposer(sdk.AccAddress)
}
```

Note that the implementation of `MsgSubmitProposalI` can be simplified by
using an embedded base struct which implements most of that interface - in this
case `MsgSubmitProposalBase`.

A parameter `ctr func() MsgSubmitProposalI` would then be passed to CLI client
methods in order to construct a concrete instance.

## Future Improvements

Expand Down
2 changes: 1 addition & 1 deletion x/distribution/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func handleMsgFundCommunityPool(ctx sdk.Context, msg types.MsgFundCommunityPool,
func NewCommunityPoolSpendProposalHandler(k Keeper) govtypes.Handler {
return func(ctx sdk.Context, content govtypes.Content) error {
switch c := content.(type) {
case types.CommunityPoolSpendProposal:
case *types.CommunityPoolSpendProposal:
return keeper.HandleCommunityPoolSpendProposal(ctx, k, c)

default:
Expand Down
2 changes: 1 addition & 1 deletion x/distribution/keeper/proposal_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

// HandleCommunityPoolSpendProposal is a handler for executing a passed community spend proposal
func HandleCommunityPoolSpendProposal(ctx sdk.Context, k Keeper, p types.CommunityPoolSpendProposal) error {
func HandleCommunityPoolSpendProposal(ctx sdk.Context, k Keeper, p *types.CommunityPoolSpendProposal) error {
if k.blacklistedAddrs[p.Recipient.String()] {
return sdkerrors.Wrapf(sdkerrors.ErrUnauthorized, "%s is blacklisted from receiving external funds", p.Recipient)
}
Expand Down
2 changes: 1 addition & 1 deletion x/distribution/proposal_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ var (
amount = sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(1)))
)

func testProposal(recipient sdk.AccAddress, amount sdk.Coins) types.CommunityPoolSpendProposal {
func testProposal(recipient sdk.AccAddress, amount sdk.Coins) *types.CommunityPoolSpendProposal {
return types.NewCommunityPoolSpendProposal("Test", "description", recipient, amount)
}

Expand Down
2 changes: 1 addition & 1 deletion x/distribution/types/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ func RegisterCodec(cdc *codec.Codec) {
cdc.RegisterConcrete(MsgWithdrawDelegatorReward{}, "cosmos-sdk/MsgWithdrawDelegationReward", nil)
cdc.RegisterConcrete(MsgWithdrawValidatorCommission{}, "cosmos-sdk/MsgWithdrawValidatorCommission", nil)
cdc.RegisterConcrete(MsgSetWithdrawAddress{}, "cosmos-sdk/MsgModifyWithdrawAddress", nil)
cdc.RegisterConcrete(CommunityPoolSpendProposal{}, "cosmos-sdk/CommunityPoolSpendProposal", nil)
cdc.RegisterConcrete(&CommunityPoolSpendProposal{}, "cosmos-sdk/CommunityPoolSpendProposal", nil)
}

var (
Expand Down
18 changes: 9 additions & 9 deletions x/distribution/types/proposal.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,32 @@ const (
)

// Assert CommunityPoolSpendProposal implements govtypes.Content at compile-time
var _ govtypes.Content = CommunityPoolSpendProposal{}
var _ govtypes.Content = &CommunityPoolSpendProposal{}

func init() {
govtypes.RegisterProposalType(ProposalTypeCommunityPoolSpend)
govtypes.RegisterProposalTypeCodec(CommunityPoolSpendProposal{}, "cosmos-sdk/CommunityPoolSpendProposal")
govtypes.RegisterProposalTypeCodec(&CommunityPoolSpendProposal{}, "cosmos-sdk/CommunityPoolSpendProposal")
}

// NewCommunityPoolSpendProposal creates a new community pool spned proposal.
func NewCommunityPoolSpendProposal(title, description string, recipient sdk.AccAddress, amount sdk.Coins) CommunityPoolSpendProposal {
return CommunityPoolSpendProposal{title, description, recipient, amount}
func NewCommunityPoolSpendProposal(title, description string, recipient sdk.AccAddress, amount sdk.Coins) *CommunityPoolSpendProposal {
return &CommunityPoolSpendProposal{title, description, recipient, amount}
}

// GetTitle returns the title of a community pool spend proposal.
func (csp CommunityPoolSpendProposal) GetTitle() string { return csp.Title }
func (csp *CommunityPoolSpendProposal) GetTitle() string { return csp.Title }

// GetDescription returns the description of a community pool spend proposal.
func (csp CommunityPoolSpendProposal) GetDescription() string { return csp.Description }
func (csp *CommunityPoolSpendProposal) GetDescription() string { return csp.Description }

// GetDescription returns the routing key of a community pool spend proposal.
func (csp CommunityPoolSpendProposal) ProposalRoute() string { return RouterKey }
func (csp *CommunityPoolSpendProposal) ProposalRoute() string { return RouterKey }

// ProposalType returns the type of a community pool spend proposal.
func (csp CommunityPoolSpendProposal) ProposalType() string { return ProposalTypeCommunityPoolSpend }
func (csp *CommunityPoolSpendProposal) ProposalType() string { return ProposalTypeCommunityPoolSpend }

// ValidateBasic runs basic stateless validity checks
func (csp CommunityPoolSpendProposal) ValidateBasic() error {
func (csp *CommunityPoolSpendProposal) ValidateBasic() error {
err := govtypes.ValidateAbstract(csp)
if err != nil {
return err
Expand Down
3 changes: 2 additions & 1 deletion x/gov/abci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import (
"testing"
"time"

codecstd "github.com/cosmos/cosmos-sdk/codec/std"

"github.com/stretchr/testify/require"
abci "github.com/tendermint/tendermint/abci/types"

codecstd "github.com/cosmos/cosmos-sdk/codec/std"
"github.com/cosmos/cosmos-sdk/simapp"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/gov"
Expand Down
2 changes: 1 addition & 1 deletion x/gov/alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ var (
SplitInactiveProposalQueueKey = types.SplitInactiveProposalQueueKey
SplitKeyDeposit = types.SplitKeyDeposit
SplitKeyVote = types.SplitKeyVote
NewMsgSubmitProposalBase = types.NewMsgSubmitProposalBase
NewMsgSubmitProposal = types.NewMsgSubmitProposal
NewMsgSubmitProposalBase = types.NewMsgSubmitProposalBase
NewMsgDeposit = types.NewMsgDeposit
NewMsgVote = types.NewMsgVote
ParamKeyTable = types.ParamKeyTable
Expand Down
Loading