Simple refactoring of gov to use protobuf for store encoding (for everything besides proposal)
This commit is contained in:
parent
56104c2961
commit
e8155d67d0
|
@ -1,6 +1,7 @@
|
|||
package simapp
|
||||
|
||||
import (
|
||||
"github.com/cosmos/cosmos-sdk/x/gov/types"
|
||||
"io"
|
||||
"os"
|
||||
|
||||
|
@ -207,7 +208,7 @@ func NewSimApp(
|
|||
AddRoute(distr.RouterKey, distr.NewCommunityPoolSpendProposalHandler(app.DistrKeeper)).
|
||||
AddRoute(upgrade.RouterKey, upgrade.NewSoftwareUpgradeProposalHandler(app.UpgradeKeeper))
|
||||
app.GovKeeper = gov.NewKeeper(
|
||||
app.cdc, keys[gov.StoreKey], app.subspaces[gov.ModuleName], app.SupplyKeeper,
|
||||
types.NewAminoGovCodec(app.cdc), keys[gov.StoreKey], app.subspaces[gov.ModuleName], app.SupplyKeeper,
|
||||
&stakingKeeper, govRouter,
|
||||
)
|
||||
|
||||
|
|
|
@ -23,7 +23,7 @@ func (keeper Keeper) GetDeposit(ctx sdk.Context, proposalID uint64, depositorAdd
|
|||
// SetDeposit sets a Deposit to the gov store
|
||||
func (keeper Keeper) SetDeposit(ctx sdk.Context, deposit types.Deposit) {
|
||||
store := ctx.KVStore(keeper.storeKey)
|
||||
bz := keeper.cdc.MustMarshalBinaryLengthPrefixed(deposit)
|
||||
bz := keeper.cdc.MustMarshalBinaryLengthPrefixed(&deposit)
|
||||
store.Set(types.DepositKey(deposit.ProposalID, deposit.Depositor), bz)
|
||||
}
|
||||
|
||||
|
|
|
@ -4,7 +4,6 @@ import (
|
|||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/codec"
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
"github.com/cosmos/cosmos-sdk/x/gov/types"
|
||||
"github.com/cosmos/cosmos-sdk/x/supply/exported"
|
||||
|
@ -27,7 +26,7 @@ type Keeper struct {
|
|||
storeKey sdk.StoreKey
|
||||
|
||||
// The codec codec for binary encoding/decoding.
|
||||
cdc *codec.Codec
|
||||
cdc types.GovCodec
|
||||
|
||||
// Proposal router
|
||||
router types.Router
|
||||
|
@ -41,7 +40,7 @@ type Keeper struct {
|
|||
//
|
||||
// CONTRACT: the parameter Subspace must have the param key table already initialized
|
||||
func NewKeeper(
|
||||
cdc *codec.Codec, key sdk.StoreKey, paramSpace types.ParamSubspace,
|
||||
cdc types.GovCodec, key sdk.StoreKey, paramSpace types.ParamSubspace,
|
||||
supplyKeeper types.SupplyKeeper, sk types.StakingKeeper, rtr types.Router,
|
||||
) Keeper {
|
||||
|
||||
|
|
|
@ -55,14 +55,20 @@ func (keeper Keeper) GetProposal(ctx sdk.Context, proposalID uint64) (proposal t
|
|||
if bz == nil {
|
||||
return
|
||||
}
|
||||
keeper.cdc.MustUnmarshalBinaryLengthPrefixed(bz, &proposal)
|
||||
err := keeper.cdc.UnmarshalProposal(bz, &proposal)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return proposal, true
|
||||
}
|
||||
|
||||
// SetProposal set a proposal to store
|
||||
func (keeper Keeper) SetProposal(ctx sdk.Context, proposal types.Proposal) {
|
||||
store := ctx.KVStore(keeper.storeKey)
|
||||
bz := keeper.cdc.MustMarshalBinaryLengthPrefixed(proposal)
|
||||
bz, err := keeper.cdc.MarshalProposal(proposal)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
store.Set(types.ProposalKey(proposal.ProposalID), bz)
|
||||
}
|
||||
|
||||
|
@ -86,7 +92,10 @@ func (keeper Keeper) IterateProposals(ctx sdk.Context, cb func(proposal types.Pr
|
|||
defer iterator.Close()
|
||||
for ; iterator.Valid(); iterator.Next() {
|
||||
var proposal types.Proposal
|
||||
keeper.cdc.MustUnmarshalBinaryLengthPrefixed(iterator.Value(), &proposal)
|
||||
err := keeper.cdc.UnmarshalProposal(iterator.Value(), &proposal)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
if cb(proposal) {
|
||||
break
|
||||
|
|
|
@ -161,7 +161,7 @@ func createTestInput(
|
|||
AddRoute(types.RouterKey, types.ProposalHandler)
|
||||
|
||||
keeper := NewKeeper(
|
||||
cdc, keyGov, pk.Subspace(types.DefaultParamspace).WithKeyTable(types.ParamKeyTable()), supplyKeeper, sk, rtr,
|
||||
types.NewAminoGovCodec(cdc), keyGov, pk.Subspace(types.DefaultParamspace).WithKeyTable(types.ParamKeyTable()), supplyKeeper, sk, rtr,
|
||||
)
|
||||
|
||||
keeper.SetProposalID(ctx, types.DefaultStartingProposalID)
|
||||
|
|
|
@ -69,7 +69,7 @@ func (keeper Keeper) GetVote(ctx sdk.Context, proposalID uint64, voterAddr sdk.A
|
|||
// SetVote sets a Vote to the gov store
|
||||
func (keeper Keeper) SetVote(ctx sdk.Context, vote types.Vote) {
|
||||
store := ctx.KVStore(keeper.storeKey)
|
||||
bz := keeper.cdc.MustMarshalBinaryLengthPrefixed(vote)
|
||||
bz := keeper.cdc.MustMarshalBinaryLengthPrefixed(&vote)
|
||||
store.Set(types.VoteKey(vote.ProposalID, vote.Voter), bz)
|
||||
}
|
||||
|
||||
|
|
|
@ -30,3 +30,28 @@ func RegisterProposalTypeCodec(o interface{}, name string) {
|
|||
func init() {
|
||||
RegisterCodec(ModuleCdc)
|
||||
}
|
||||
|
||||
type GovCodec interface {
|
||||
codec.Marshaler
|
||||
MarshalProposal(p Proposal) ([]byte, error)
|
||||
UnmarshalProposal(bz []byte, ptr *Proposal) error
|
||||
}
|
||||
|
||||
type AminoGovCodec struct {
|
||||
codec.Marshaler
|
||||
amino *codec.Codec
|
||||
}
|
||||
|
||||
func NewAminoGovCodec(amino *codec.Codec) AminoGovCodec {
|
||||
return AminoGovCodec{Marshaler: codec.NewHybridCodec(amino), amino: amino}
|
||||
}
|
||||
|
||||
func (a AminoGovCodec) MarshalProposal(p Proposal) ([]byte, error) {
|
||||
return a.amino.MarshalBinaryBare(p)
|
||||
}
|
||||
|
||||
func (a AminoGovCodec) UnmarshalProposal(bz []byte, ptr *Proposal) error {
|
||||
return a.amino.UnmarshalBinaryBare(bz, ptr)
|
||||
}
|
||||
|
||||
var _ GovCodec = AminoGovCodec{}
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,131 @@
|
|||
syntax = "proto3";
|
||||
package cosmos_sdk.x.gov.v1;
|
||||
|
||||
import "types/types.proto";
|
||||
import "third_party/proto/gogoproto/gogo.proto";
|
||||
import "third_party/proto/cosmos-proto/cosmos.proto";
|
||||
import "google/protobuf/timestamp.proto";
|
||||
|
||||
option go_package = "github.com/cosmos/cosmos-sdk/x/gov/types";
|
||||
option (gogoproto.goproto_stringer_all) = false;
|
||||
option (gogoproto.stringer_all) = false;
|
||||
option (gogoproto.goproto_getters_all) = false;
|
||||
|
||||
message MsgCommon {
|
||||
option (cosmos_proto.interface_type) = "github.com/cosmos/cosmos-sdk/types.Msg";
|
||||
option (gogoproto.stringer) = true;
|
||||
oneof sum {
|
||||
MsgDeposit gov_deposit = 1;
|
||||
MsgVote gov_vote = 2;
|
||||
}
|
||||
}
|
||||
|
||||
message MsgSubmitProposalBase {
|
||||
option (gogoproto.stringer) = true;
|
||||
repeated cosmos_sdk.v1.Coin intial_deposit = 1 [(gogoproto.nullable) = false
|
||||
,(gogoproto.castrepeated) = "github.com/cosmos/cosmos-sdk/types.Coins"
|
||||
,(gogoproto.moretags) = "yaml:\"initial_deposit\""];
|
||||
bytes proposer = 2 [(gogoproto.casttype) = "github.com/cosmos/cosmos-sdk/types.AccAddress"];
|
||||
}
|
||||
|
||||
// MsgDeposit defines a message to submit a deposit to an existing proposal
|
||||
message MsgDeposit {
|
||||
uint64 proposal_id = 1 [(gogoproto.customname) = "ProposalID"
|
||||
,(gogoproto.moretags) = "yaml:\"proposal_id\""
|
||||
,(gogoproto.jsontag) = "proposal_id"];
|
||||
bytes depositor = 2 [(gogoproto.casttype) = "github.com/cosmos/cosmos-sdk/types.AccAddress"];
|
||||
repeated cosmos_sdk.v1.Coin amount = 3 [(gogoproto.nullable) = false,
|
||||
(gogoproto.castrepeated) = "github.com/cosmos/cosmos-sdk/types.Coins"];
|
||||
}
|
||||
|
||||
// VoteOption defines a vote option
|
||||
enum VoteOption {
|
||||
option (gogoproto.enum_stringer) = false;
|
||||
option (gogoproto.goproto_enum_stringer) = false;
|
||||
option (gogoproto.goproto_enum_prefix) = false;
|
||||
EMPTY = 0 [(gogoproto.enumvalue_customname) = "OptionEmpty"];
|
||||
YES = 1 [(gogoproto.enumvalue_customname) = "OptionYes"];
|
||||
ABSTAIN = 2 [(gogoproto.enumvalue_customname) = "OptionAbstain"];
|
||||
NO = 3 [(gogoproto.enumvalue_customname) = "OptionNo"];
|
||||
NO_WITH_VETO = 4 [(gogoproto.enumvalue_customname) = "OptionNoWithVeto"];
|
||||
}
|
||||
|
||||
// MsgVote defines a message to cast a vote
|
||||
message MsgVote {
|
||||
uint64 proposal_id = 1 [(gogoproto.customname) = "ProposalID"
|
||||
,(gogoproto.moretags) = "yaml:\"proposal_id\""
|
||||
,(gogoproto.jsontag) = "proposal_id"];
|
||||
bytes voter = 2 [(gogoproto.casttype) = "github.com/cosmos/cosmos-sdk/types.AccAddress"];
|
||||
VoteOption option = 3;
|
||||
}
|
||||
|
||||
// TextProposal defines a standard text proposal whose changes need to be
|
||||
// manually updated in case of approval
|
||||
message TextProposal {
|
||||
string title = 1;
|
||||
string description = 2;
|
||||
}
|
||||
|
||||
// Deposit defines an amount deposited by an account address to an active proposal
|
||||
message Deposit {
|
||||
uint64 proposal_id = 1 [(gogoproto.customname) = "ProposalID"
|
||||
,(gogoproto.moretags) = "yaml:\"proposal_id\""];
|
||||
bytes depositor = 2 [(gogoproto.casttype) = "github.com/cosmos/cosmos-sdk/types.AccAddress"];
|
||||
repeated cosmos_sdk.v1.Coin amount = 3 [(gogoproto.nullable) = false, (gogoproto.castrepeated) = "github.com/cosmos/cosmos-sdk/types.Coins"];
|
||||
}
|
||||
|
||||
message ProposalBase {
|
||||
option (gogoproto.goproto_stringer) = true;
|
||||
option (gogoproto.face) = true;
|
||||
uint64 proposal_id = 1 [(gogoproto.customname) = "ProposalID"
|
||||
,(gogoproto.moretags) = "yaml:\"proposal_id\""];
|
||||
ProposalStatus status = 2;
|
||||
TallyResult final_tally_result = 3 [(gogoproto.nullable) = false];
|
||||
google.protobuf.Timestamp submit_time = 4 [(gogoproto.stdtime) = true, (gogoproto.nullable) = false];
|
||||
google.protobuf.Timestamp deposit_end_time = 5 [(gogoproto.stdtime) = true, (gogoproto.nullable) = false];
|
||||
repeated cosmos_sdk.v1.Coin total_deposit = 6 [(gogoproto.nullable) = false, (gogoproto.castrepeated) = "github.com/cosmos/cosmos-sdk/types.Coins"];
|
||||
google.protobuf.Timestamp voting_start_time = 7 [(gogoproto.stdtime) = true, (gogoproto.nullable) = false];
|
||||
google.protobuf.Timestamp voting_end_time = 8 [(gogoproto.stdtime) = true, (gogoproto.nullable) = false];
|
||||
}
|
||||
|
||||
// ProposalStatus is a type alias that represents a proposal status as a byte
|
||||
enum ProposalStatus {
|
||||
option (gogoproto.enum_stringer) = false;
|
||||
option (gogoproto.goproto_enum_stringer) = false;
|
||||
option (gogoproto.goproto_enum_prefix) = false;
|
||||
NIL = 0 [(gogoproto.enumvalue_customname) = "StatusNil"];
|
||||
DEPOSIT_PERIOD = 1 [(gogoproto.enumvalue_customname) = "StatusDepositPeriod"];
|
||||
VOTING_PERIOD = 2 [(gogoproto.enumvalue_customname) = "StatusVotingPeriod"];
|
||||
PASSED = 3 [(gogoproto.enumvalue_customname) = "StatusPassed"];
|
||||
REJECTED = 4 [(gogoproto.enumvalue_customname) = "StatusRejected"];
|
||||
FAILED = 5 [(gogoproto.enumvalue_customname) = "StatusFailed"];
|
||||
}
|
||||
|
||||
// TallyResult defines a standard tally for a proposal
|
||||
message TallyResult {
|
||||
bytes yes = 1 [(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int", (gogoproto.nullable) = false];
|
||||
bytes abstain = 2 [(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int", (gogoproto.nullable) = false];
|
||||
bytes no = 3 [(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int", (gogoproto.nullable) = false];
|
||||
bytes no_with_veto = 4 [(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int", (gogoproto.nullable) = false
|
||||
,(gogoproto.moretags) = "yaml:\"no_with_veto\""];
|
||||
}
|
||||
|
||||
message Vote {
|
||||
uint64 proposal_id = 1 [(gogoproto.customname) = "ProposalID"
|
||||
,(gogoproto.moretags) = "yaml:\"proposal_id\""];
|
||||
bytes voter = 2 [(gogoproto.casttype) = "github.com/cosmos/cosmos-sdk/types.AccAddress"];
|
||||
VoteOption option = 3;
|
||||
}
|
||||
|
||||
message BasicProposal {
|
||||
ProposalBase base = 1 [(gogoproto.embed) = true];
|
||||
BasicContent content = 2;
|
||||
}
|
||||
|
||||
message BasicContent {
|
||||
option (cosmos_proto.interface_type) = "Content";
|
||||
option (gogoproto.stringer) = true;
|
||||
oneof sum {
|
||||
TextProposal text = 1;
|
||||
}
|
||||
}
|
|
@ -6,13 +6,6 @@ import (
|
|||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
)
|
||||
|
||||
// Deposit defines an amount deposited by an account address to an active proposal
|
||||
type Deposit struct {
|
||||
ProposalID uint64 `json:"proposal_id" yaml:"proposal_id"` // proposalID of the proposal
|
||||
Depositor sdk.AccAddress `json:"depositor" yaml:"depositor"` // Address of the depositor
|
||||
Amount sdk.Coins `json:"amount" yaml:"amount"` // Deposit amount
|
||||
}
|
||||
|
||||
// NewDeposit creates a new Deposit instance
|
||||
func NewDeposit(proposalID uint64, depositor sdk.AccAddress, amount sdk.Coins) Deposit {
|
||||
return Deposit{proposalID, depositor, amount}
|
||||
|
|
|
@ -75,13 +75,6 @@ func (msg MsgSubmitProposal) GetSigners() []sdk.AccAddress {
|
|||
return []sdk.AccAddress{msg.Proposer}
|
||||
}
|
||||
|
||||
// MsgDeposit defines a message to submit a deposit to an existing proposal
|
||||
type MsgDeposit struct {
|
||||
ProposalID uint64 `json:"proposal_id" yaml:"proposal_id"` // ID of the proposal
|
||||
Depositor sdk.AccAddress `json:"depositor" yaml:"depositor"` // Address of the depositor
|
||||
Amount sdk.Coins `json:"amount" yaml:"amount"` // Coins to add to the proposal's deposit
|
||||
}
|
||||
|
||||
// NewMsgDeposit creates a new MsgDeposit instance
|
||||
func NewMsgDeposit(depositor sdk.AccAddress, proposalID uint64, amount sdk.Coins) MsgDeposit {
|
||||
return MsgDeposit{proposalID, depositor, amount}
|
||||
|
@ -128,13 +121,6 @@ func (msg MsgDeposit) GetSigners() []sdk.AccAddress {
|
|||
return []sdk.AccAddress{msg.Depositor}
|
||||
}
|
||||
|
||||
// MsgVote defines a message to cast a vote
|
||||
type MsgVote struct {
|
||||
ProposalID uint64 `json:"proposal_id" yaml:"proposal_id"` // ID of the proposal
|
||||
Voter sdk.AccAddress `json:"voter" yaml:"voter"` // address of the voter
|
||||
Option VoteOption `json:"option" yaml:"option"` // option from OptionSet chosen by the voter
|
||||
}
|
||||
|
||||
// NewMsgVote creates a message to cast a vote on an active proposal
|
||||
func NewMsgVote(voter sdk.AccAddress, proposalID uint64, option VoteOption) MsgVote {
|
||||
return MsgVote{proposalID, voter, option}
|
||||
|
|
|
@ -79,18 +79,6 @@ type (
|
|||
// ProposalQueue defines a queue for proposal ids
|
||||
ProposalQueue []uint64
|
||||
|
||||
// ProposalStatus is a type alias that represents a proposal status as a byte
|
||||
ProposalStatus byte
|
||||
)
|
||||
|
||||
// Valid Proposal statuses
|
||||
const (
|
||||
StatusNil ProposalStatus = 0x00
|
||||
StatusDepositPeriod ProposalStatus = 0x01
|
||||
StatusVotingPeriod ProposalStatus = 0x02
|
||||
StatusPassed ProposalStatus = 0x03
|
||||
StatusRejected ProposalStatus = 0x04
|
||||
StatusFailed ProposalStatus = 0x05
|
||||
)
|
||||
|
||||
// ProposalStatusFromString turns a string into a ProposalStatus
|
||||
|
@ -205,13 +193,6 @@ const (
|
|||
ProposalTypeText string = "Text"
|
||||
)
|
||||
|
||||
// TextProposal defines a standard text proposal whose changes need to be
|
||||
// manually updated in case of approval
|
||||
type TextProposal struct {
|
||||
Title string `json:"title" yaml:"title"`
|
||||
Description string `json:"description" yaml:"description"`
|
||||
}
|
||||
|
||||
// NewTextProposal creates a text proposal Content
|
||||
func NewTextProposal(title, description string) Content {
|
||||
return TextProposal{title, description}
|
||||
|
|
|
@ -28,14 +28,6 @@ func NewValidatorGovInfo(address sdk.ValAddress, bondedTokens sdk.Int, delegator
|
|||
}
|
||||
}
|
||||
|
||||
// TallyResult defines a standard tally for a proposal
|
||||
type TallyResult struct {
|
||||
Yes sdk.Int `json:"yes" yaml:"yes"`
|
||||
Abstain sdk.Int `json:"abstain" yaml:"abstain"`
|
||||
No sdk.Int `json:"no" yaml:"no"`
|
||||
NoWithVeto sdk.Int `json:"no_with_veto" yaml:"no_with_veto"`
|
||||
}
|
||||
|
||||
// NewTallyResult creates a new TallyResult instance
|
||||
func NewTallyResult(yes, abstain, no, noWithVeto sdk.Int) TallyResult {
|
||||
return TallyResult{
|
||||
|
|
|
@ -7,13 +7,6 @@ import (
|
|||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
)
|
||||
|
||||
// Vote
|
||||
type Vote struct {
|
||||
ProposalID uint64 `json:"proposal_id" yaml:"proposal_id"` // proposalID of the proposal
|
||||
Voter sdk.AccAddress `json:"voter" yaml:"voter"` // address of the voter
|
||||
Option VoteOption `json:"option" yaml:"option"` // option from OptionSet chosen by the voter
|
||||
}
|
||||
|
||||
// NewVote creates a new Vote instance
|
||||
func NewVote(proposalID uint64, voter sdk.AccAddress, option VoteOption) Vote {
|
||||
return Vote{proposalID, voter, option}
|
||||
|
@ -49,18 +42,6 @@ func (v Vote) Empty() bool {
|
|||
return v.Equals(Vote{})
|
||||
}
|
||||
|
||||
// VoteOption defines a vote option
|
||||
type VoteOption byte
|
||||
|
||||
// Vote options
|
||||
const (
|
||||
OptionEmpty VoteOption = 0x00
|
||||
OptionYes VoteOption = 0x01
|
||||
OptionAbstain VoteOption = 0x02
|
||||
OptionNo VoteOption = 0x03
|
||||
OptionNoWithVeto VoteOption = 0x04
|
||||
)
|
||||
|
||||
// VoteOptionFromString returns a VoteOption from a string. It returns an error
|
||||
// if the string is invalid.
|
||||
func VoteOptionFromString(str string) (VoteOption, error) {
|
||||
|
|
Loading…
Reference in New Issue