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

R4R: Make gov data types codec wires usable across different protocol versions #861

Merged
merged 2 commits into from
Dec 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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: 2 additions & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
dbm "github.com/tendermint/tendermint/libs/db"
"github.com/tendermint/tendermint/libs/log"
tmtypes "github.com/tendermint/tendermint/types"
govtypes "github.com/irisnet/irishub/types/gov"
)

const (
Expand Down Expand Up @@ -96,6 +97,7 @@ func MakeCodec() *codec.Codec {
distr.RegisterCodec(cdc)
slashing.RegisterCodec(cdc)
gov.RegisterCodec(cdc)
govtypes.RegisterCodec(cdc)
record.RegisterCodec(cdc)
upgrade.RegisterCodec(cdc)
service.RegisterCodec(cdc)
Expand Down
5 changes: 3 additions & 2 deletions app/v0/protocol_v0.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/irisnet/irishub/modules/bank"
distr "github.com/irisnet/irishub/modules/distribution"
"github.com/irisnet/irishub/modules/gov"
"github.com/irisnet/irishub/modules/gov/params"
"github.com/irisnet/irishub/types/gov/params"
"github.com/irisnet/irishub/modules/guardian"
"github.com/irisnet/irishub/modules/mint"
"github.com/irisnet/irishub/modules/params"
Expand All @@ -26,6 +26,7 @@ import (
abci "github.com/tendermint/tendermint/abci/types"
"sort"
"time"
govtypes "github.com/irisnet/irishub/types/gov"
)

var _ protocol.Protocol = (*ProtocolVersion0)(nil)
Expand Down Expand Up @@ -156,7 +157,7 @@ func (p *ProtocolVersion0) configKeepers(protocolkeeper protocolKeeper.Keeper) {
p.guardianKeeper,
&stakeKeeper,
protocolkeeper,
gov.DefaultCodespace,
govtypes.DefaultCodespace,
)

p.recordKeeper = record.NewKeeper(
Expand Down
9 changes: 5 additions & 4 deletions client/gov/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ import (
client "github.com/irisnet/irishub/client/gov"
"github.com/irisnet/irishub/codec"
"github.com/irisnet/irishub/modules/gov"
"github.com/irisnet/irishub/modules/gov/params"
"github.com/irisnet/irishub/types/gov/params"
"github.com/irisnet/irishub/modules/params"
sdk "github.com/irisnet/irishub/types"
"github.com/spf13/cobra"
"github.com/spf13/viper"
govtypes "github.com/irisnet/irishub/types/gov"
)

// GetCmdQueryProposal implements the query proposal command.
Expand Down Expand Up @@ -84,7 +85,7 @@ func GetCmdQueryProposals(queryRoute string, cdc *codec.Codec) *cobra.Command {
}

if len(strProposalStatus) != 0 {
proposalStatus, err := gov.ProposalStatusFromString(client.NormalizeProposalStatus(strProposalStatus))
proposalStatus, err := govtypes.ProposalStatusFromString(client.NormalizeProposalStatus(strProposalStatus))
if err != nil {
return err
}
Expand Down Expand Up @@ -426,7 +427,7 @@ func printModuleList(res []sdk.KVPair) (err error) {
}

func printParamStr(p params.GovParameter, keyStr string) {
var param gov.Param
var param govtypes.Param
param.Key = keyStr
param.Value = p.ToJson("")
param.Op = ""
Expand All @@ -444,7 +445,7 @@ func GetCmdPullGovConfig(storeName string, cdc *codec.Codec) *cobra.Command {
ctx := context.NewCLIContext().WithCodec(cdc)
res, err := ctx.QuerySubspace([]byte("Gov/"), storeName)
if err == nil && len(res) != 0 {
var pd gov.ParameterConfigFile
var pd govtypes.ParameterConfigFile
pathStr := viper.GetString(flagPath)
err := pd.WriteFile(cdc, res, pathStr)
return err
Expand Down
21 changes: 11 additions & 10 deletions client/gov/cli/sendtx.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/viper"
govtypes "github.com/irisnet/irishub/types/gov"
)

// GetCmdSubmitProposal implements submitting a proposal transaction command.
Expand Down Expand Up @@ -47,13 +48,13 @@ func GetCmdSubmitProposal(cdc *codec.Codec) *cobra.Command {
return err
}

proposalType, err := gov.ProposalTypeFromString(strProposalType)
proposalType, err := govtypes.ProposalTypeFromString(strProposalType)
if err != nil {
return err
}
//////////////////// iris begin ///////////////////////////
var param gov.Param
if proposalType == gov.ProposalTypeParameterChange {
var param govtypes.Param
if proposalType == govtypes.ProposalTypeParameterChange {
pathStr := viper.GetString(flagPath)
keyStr := viper.GetString(flagKey)
opStr := viper.GetString(flagOp)
Expand All @@ -65,9 +66,9 @@ func GetCmdSubmitProposal(cdc *codec.Codec) *cobra.Command {
//////////////////// iris end /////////////////////////////

msg := gov.NewMsgSubmitProposal(title, description, proposalType, fromAddr, amount, param)
if proposalType == gov.ProposalTypeTxTaxUsage {
if proposalType == govtypes.ProposalTypeTxTaxUsage {
usageStr := viper.GetString(flagUsage)
usage, err := gov.UsageTypeFromString(usageStr)
usage, err := govtypes.UsageTypeFromString(usageStr)
if err != nil {
return err
}
Expand All @@ -88,7 +89,7 @@ func GetCmdSubmitProposal(cdc *codec.Codec) *cobra.Command {
return utils.SendOrPrintTx(txCtx, cliCtx, []sdk.Msg{taxMsg})
}

if proposalType == gov.ProposalTypeSoftwareUpgrade {
if proposalType == govtypes.ProposalTypeSoftwareUpgrade {
version := uint64(viper.GetInt64(flagVersion))
software := viper.GetString(flagSoftware)
switchHeight := uint64(viper.GetInt64(flagSwitchHeight))
Expand Down Expand Up @@ -125,15 +126,15 @@ func GetCmdSubmitProposal(cdc *codec.Codec) *cobra.Command {
}

//////////////////// iris begin ///////////////////////////
func getParamFromString(paramStr string, pathStr string, keyStr string, opStr string, cdc *codec.Codec) (gov.Param, error) {
var param gov.Param
func getParamFromString(paramStr string, pathStr string, keyStr string, opStr string, cdc *codec.Codec) (govtypes.Param, error) {
var param govtypes.Param

if paramStr != "" {
err := json.Unmarshal([]byte(paramStr), &param)
return param, err

} else if pathStr != "" {
paramDoc := gov.ParameterConfigFile{}
paramDoc := govtypes.ParameterConfigFile{}
err := paramDoc.ReadFile(cdc, pathStr)
if err != nil {
return param, err
Expand Down Expand Up @@ -216,7 +217,7 @@ func GetCmdVote(cdc *codec.Codec) *cobra.Command {
proposalID := uint64(viper.GetInt64(flagProposalID))
option := viper.GetString(flagOption)

byteVoteOption, err := gov.VoteOptionFromString(client.NormalizeVoteOption(option))
byteVoteOption, err := govtypes.VoteOptionFromString(client.NormalizeVoteOption(option))
if err != nil {
return err
}
Expand Down
9 changes: 5 additions & 4 deletions client/gov/lcd/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/irisnet/irishub/modules/gov"
sdk "github.com/irisnet/irishub/types"
"github.com/pkg/errors"
govtypes "github.com/irisnet/irishub/types/gov"
)

func queryProposalHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext) http.HandlerFunc {
Expand Down Expand Up @@ -126,7 +127,7 @@ func queryDepositHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext) http.Han
return
}

var deposit gov.Deposit
var deposit govtypes.Deposit
cdc.UnmarshalJSON(res, &deposit)
if deposit.Empty() {
res, err := cliCtx.QueryWithData("custom/gov/proposal", cdc.MustMarshalBinaryLengthPrefixed(gov.QueryProposalParams{params.ProposalID}))
Expand Down Expand Up @@ -189,7 +190,7 @@ func queryVoteHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext) http.Handle
return
}

var vote gov.Vote
var vote govtypes.Vote
cdc.UnmarshalJSON(res, &vote)
if vote.Empty() {
bz, err := cdc.MarshalJSON(gov.QueryProposalParams{params.ProposalID})
Expand Down Expand Up @@ -280,7 +281,7 @@ func queryProposalsWithParameterFn(cdc *codec.Codec, cliCtx context.CLIContext)
}

if len(strProposalStatus) != 0 {
proposalStatus, err := gov.ProposalStatusFromString(client.NormalizeProposalStatus(strProposalStatus))
proposalStatus, err := govtypes.ProposalStatusFromString(client.NormalizeProposalStatus(strProposalStatus))
if err != nil {
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
Expand Down Expand Up @@ -359,7 +360,7 @@ func queryParamsHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext) http.Hand
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
var pd gov.ParameterConfigFile
var pd govtypes.ParameterConfigFile
for _, kv := range res {
switch string(kv.Key) {
case "Gov/govDepositProcedure":
Expand Down
11 changes: 6 additions & 5 deletions client/gov/lcd/sendtx.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/irisnet/irishub/codec"
"github.com/irisnet/irishub/modules/gov"
sdk "github.com/irisnet/irishub/types"
govtypes "github.com/irisnet/irishub/types/gov"
)

type postProposalReq struct {
Expand All @@ -20,8 +21,8 @@ type postProposalReq struct {
ProposalType string `json:"proposal_type"` // Type of proposal. Initial set {PlainTextProposal, SoftwareUpgradeProposal}
Proposer sdk.AccAddress `json:"proposer"` // Address of the proposer
InitialDeposit string `json:"initial_deposit"` // Coins to add to the proposal's deposit
Param gov.Param `json:"param"`
Usage gov.UsageType `json:"usage"`
Param govtypes.Param `json:"param"`
Usage govtypes.UsageType `json:"usage"`
DestAddress sdk.AccAddress `json:"dest_address"`
Percent sdk.Dec `json:"percent"`
}
Expand Down Expand Up @@ -54,7 +55,7 @@ func postProposalHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext) http.Han
return
}

proposalType, err := gov.ProposalTypeFromString(client.NormalizeProposalType(req.ProposalType))
proposalType, err := govtypes.ProposalTypeFromString(client.NormalizeProposalType(req.ProposalType))
if err != nil {
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
Expand All @@ -68,7 +69,7 @@ func postProposalHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext) http.Han

// create the message
msg := gov.NewMsgSubmitProposal(req.Title, req.Description, proposalType, req.Proposer, initDepositAmount, req.Param)
if msg.ProposalType == gov.ProposalTypeTxTaxUsage {
if msg.ProposalType == govtypes.ProposalTypeTxTaxUsage {
taxMsg := gov.NewMsgSubmitTaxUsageProposal(msg, req.Usage, req.DestAddress, req.Percent)
err = msg.ValidateBasic()
if err != nil {
Expand Down Expand Up @@ -162,7 +163,7 @@ func voteHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext) http.HandlerFunc
return
}

voteOption, err := gov.VoteOptionFromString(client.NormalizeVoteOption(req.Option))
voteOption, err := govtypes.VoteOptionFromString(client.NormalizeVoteOption(req.Option))
if err != nil {
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
Expand Down
Empty file.
Empty file.
14 changes: 0 additions & 14 deletions modules/gov/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package gov

import (
"github.com/irisnet/irishub/codec"
"github.com/irisnet/irishub/modules/gov/params"
)

// Register concrete types on codec codec
Expand All @@ -13,19 +12,6 @@ func RegisterCodec(cdc *codec.Codec) {
cdc.RegisterConcrete(MsgSubmitSoftwareUpgradeProposal{}, "gov/MsgSubmitSoftwareUpgradeProposal", nil)
cdc.RegisterConcrete(MsgDeposit{}, "cosmos-sdk/MsgDeposit", nil)
cdc.RegisterConcrete(MsgVote{}, "cosmos-sdk/MsgVote", nil)

cdc.RegisterInterface((*Proposal)(nil), nil)
cdc.RegisterConcrete(&TextProposal{}, "gov/TextProposal", nil)

//////////////////// iris begin ///////////////////////////
cdc.RegisterConcrete(&govparams.DepositProcedure{}, "cosmos-sdk/DepositProcedure", nil)
cdc.RegisterConcrete(&govparams.TallyingProcedure{}, "cosmos-sdk/TallyingProcedure", nil)
cdc.RegisterConcrete(&govparams.VotingProcedure{}, "cosmos-sdk/VotingProcedure", nil)
cdc.RegisterConcrete(&ParameterProposal{}, "gov/ParameterProposal", nil)
cdc.RegisterConcrete(&SoftwareUpgradeProposal{}, "gov/SoftwareUpgradeProposal", nil)
cdc.RegisterConcrete(&HaltProposal{}, "gov/TerminatorProposal", nil)
cdc.RegisterConcrete(&TaxUsageProposal{}, "gov/TaxUsageProposal", nil)
//////////////////// iris end ///////////////////////////
}

var msgCdc = codec.New()
89 changes: 89 additions & 0 deletions modules/gov/execute.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package gov

import (
"fmt"
"github.com/irisnet/irishub/modules/params"
"github.com/irisnet/irishub/types/common"
sdk "github.com/irisnet/irishub/types"
govtypes "github.com/irisnet/irishub/types/gov"
protocolKeeper "github.com/irisnet/irishub/app/protocol/keeper"
)


func Execute(ctx sdk.Context, k Keeper, p govtypes.Proposal) (err error){
switch p.GetProposalType(){
case govtypes.ProposalTypeParameterChange:
return ParameterProposalExecute(ctx, k, p.(*govtypes.ParameterProposal))
case govtypes.ProposalTypeSoftwareHalt:
return HaltProposalExecute(ctx, k)
case govtypes.ProposalTypeTxTaxUsage:
return TaxUsageProposalExecute(ctx,k, p.(*govtypes.TaxUsageProposal))
case govtypes.ProposalTypeSoftwareUpgrade:
return SoftwareUpgradeProposalExecute(ctx, k, p.(*govtypes.SoftwareUpgradeProposal))
}
return nil
}

func TaxUsageProposalExecute(ctx sdk.Context, k Keeper, p *govtypes.TaxUsageProposal) (err error) {
burn := false
if p.Usage == govtypes.UsageTypeBurn {
burn = true
}
k.dk.AllocateFeeTax(ctx, p.DestAddress, p.Percent, burn)
return
}

func ParameterProposalExecute(ctx sdk.Context, k Keeper, pp *govtypes.ParameterProposal) (err error) {

logger := ctx.Logger().With("module", "x/gov")
logger.Info("Execute ParameterProposal begin", "info", fmt.Sprintf("current height:%d", ctx.BlockHeight()))
if pp.Param.Op == govtypes.Update {
params.ParamMapping[pp.Param.Key].Update(ctx, pp.Param.Value)
} else if pp.Param.Op == govtypes.Insert {
//Todo: insert
}
return
}

func SoftwareUpgradeProposalExecute(ctx sdk.Context, k Keeper, sp *govtypes.SoftwareUpgradeProposal) error {

logger := ctx.Logger().With("module", "x/gov")

if _, ok := k.pk.GetUpgradeConfig(ctx); ok {
logger.Info("Execute SoftwareProposal Failure", "info",
fmt.Sprintf("Software Upgrade Switch Period is in process. current height:%d", ctx.BlockHeight()))
return nil
}
if !k.pk.IsValidProtocolVersion(ctx, sp.Version) {
logger.Info("Execute SoftwareProposal Failure", "info",
fmt.Sprintf("version [%v] in SoftwareUpgradeProposal isn't valid ", sp.ProposalID))
return nil
}
if uint64(ctx.BlockHeight())+1 >= sp.SwitchHeight {
logger.Info("Execute SoftwareProposal Failure", "info",
fmt.Sprintf("switch height must be more than blockHeight + 1"))
return nil
}

k.pk.SetUpgradeConfig(ctx,
protocolKeeper.UpgradeConfig{sp.ProposalID,
common.ProtocolDefinition{sp.Version, sp.Software, sp.SwitchHeight}})

logger.Info("Execute SoftwareProposal Success", "info",
fmt.Sprintf("current height:%d", ctx.BlockHeight()))

return nil
}

func HaltProposalExecute(ctx sdk.Context, k Keeper) error {
logger := ctx.Logger().With("module", "x/gov")

if k.GetTerminatorHeight(ctx) == -1 {
k.SetTerminatorHeight(ctx, ctx.BlockHeight()+k.GetTerminatorPeriod(ctx))
logger.Info("Execute TerminatorProposal begin", "info", fmt.Sprintf("Terminator height:%d", k.GetTerminatorHeight(ctx)))
} else {
logger.Info("Terminator Period is in process.", "info", fmt.Sprintf("Terminator height:%d", k.GetTerminatorHeight(ctx)))

}
return nil
}
Loading