Skip to content

Commit

Permalink
Merge pull request #216 from MrXJC/jiacheng/gov
Browse files Browse the repository at this point in the history
IRISHUB-404:fix the cycle import
  • Loading branch information
wukongcheng authored Sep 14, 2018
2 parents f7dba86 + 2fcb0f7 commit 2bc580b
Show file tree
Hide file tree
Showing 17 changed files with 154 additions and 338 deletions.
234 changes: 21 additions & 213 deletions Gopkg.lock

Large diffs are not rendered by default.

52 changes: 27 additions & 25 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,36 @@ package app

import (
"encoding/json"
"io"
"os"

bam "github.com/irisnet/irishub/baseapp"
abci "github.com/tendermint/tendermint/abci/types"
cmn "github.com/tendermint/tendermint/libs/common"
dbm "github.com/tendermint/tendermint/libs/db"
"github.com/tendermint/tendermint/libs/log"
tmtypes "github.com/tendermint/tendermint/types"

"errors"
"fmt"
"github.com/cosmos/cosmos-sdk/server"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/wire"
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/cosmos/cosmos-sdk/x/bank"
"github.com/irisnet/irishub/modules/gov"
"github.com/cosmos/cosmos-sdk/x/ibc"
"github.com/irisnet/irishub/modules/iparams"
"github.com/cosmos/cosmos-sdk/x/params"
"github.com/cosmos/cosmos-sdk/x/slashing"
"github.com/cosmos/cosmos-sdk/x/stake"
bam "github.com/irisnet/irishub/baseapp"
"github.com/irisnet/irishub/modules/gov"
"github.com/irisnet/irishub/modules/gov/params"
"github.com/irisnet/irishub/modules/iparams"
"github.com/irisnet/irishub/modules/parameter"
"github.com/irisnet/irishub/modules/upgrade"

"errors"
"fmt"
"github.com/cosmos/cosmos-sdk/server"
"github.com/spf13/viper"
abci "github.com/tendermint/tendermint/abci/types"
bc "github.com/tendermint/tendermint/blockchain"
tmcli "github.com/tendermint/tendermint/libs/cli"
cmn "github.com/tendermint/tendermint/libs/common"
dbm "github.com/tendermint/tendermint/libs/db"
"github.com/tendermint/tendermint/libs/log"
"github.com/tendermint/tendermint/node"
sm "github.com/tendermint/tendermint/state"
bc "github.com/tendermint/tendermint/blockchain"
tmtypes "github.com/tendermint/tendermint/types"
"io"
"os"
"strings"
"github.com/cosmos/cosmos-sdk/x/params"
)

const (
Expand Down Expand Up @@ -72,11 +71,11 @@ type IrisApp struct {
slashingKeeper slashing.Keeper
paramsKeeper params.Keeper
govKeeper gov.Keeper
iparamsKeeper iparams.Keeper
iparamsKeeper iparams.Keeper
upgradeKeeper upgrade.Keeper

// fee manager
feeManager bam.FeeManager
feeManager bam.FeeManager
}

func NewIrisApp(logger log.Logger, db dbm.DB, traceStore io.Writer, baseAppOptions ...func(*bam.BaseApp)) *IrisApp {
Expand All @@ -97,7 +96,7 @@ func NewIrisApp(logger log.Logger, db dbm.DB, traceStore io.Writer, baseAppOptio
keyGov: sdk.NewKVStoreKey("gov"),
keyFeeCollection: sdk.NewKVStoreKey("fee"),
keyParams: sdk.NewKVStoreKey("params"),
keyIparams: sdk.NewKVStoreKey("iparams"),
keyIparams: sdk.NewKVStoreKey("iparams"),
keyUpgrade: sdk.NewKVStoreKey("upgrade"),
}

Expand All @@ -114,7 +113,7 @@ func NewIrisApp(logger log.Logger, db dbm.DB, traceStore io.Writer, baseAppOptio
)

// add handlers
app.paramsKeeper = params.NewKeeper(cdc,app.keyParams)
app.paramsKeeper = params.NewKeeper(cdc, app.keyParams)
app.iparamsKeeper = iparams.NewKeeper(app.cdc, app.keyIparams)
app.coinKeeper = bank.NewKeeper(app.accountMapper)
app.ibcMapper = ibc.NewMapper(app.cdc, app.keyIBC, app.RegisterCodespace(ibc.DefaultCodespace))
Expand Down Expand Up @@ -144,6 +143,7 @@ func NewIrisApp(logger log.Logger, db dbm.DB, traceStore io.Writer, baseAppOptio
app.SetFeePreprocessHandler(bam.NewFeePreprocessHandler(app.feeManager))
app.MountStoresIAVL(app.keyMain, app.keyAccount, app.keyIBC, app.keyStake, app.keySlashing, app.keyGov, app.keyFeeCollection, app.keyParams, app.keyIparams, app.keyUpgrade)
app.SetRunMsg(app.runMsgs)

var err error
if viper.GetBool(FlagReplay) {
err = app.LoadVersion(lastHeight, app.keyMain)
Expand All @@ -155,6 +155,8 @@ func NewIrisApp(logger log.Logger, db dbm.DB, traceStore io.Writer, baseAppOptio
}

upgrade.RegisterModuleList(app.Router())
parameter.SetParamReadWriter(app.paramsKeeper.Setter(), &govparams.DepositProcedureParameter)
parameter.RegisterGovParamMapping(&govparams.DepositProcedureParameter)

return app
}
Expand Down Expand Up @@ -219,14 +221,14 @@ func (app *IrisApp) initChainer(ctx sdk.Context, req abci.RequestInitChain) abci
panic(err)
}

minDeposit,err := IrisCt.ConvertToMinCoin(fmt.Sprintf("%d%s",10,denom))
minDeposit, err := IrisCt.ConvertToMinCoin(fmt.Sprintf("%d%s", 10, denom))
if err != nil {
panic(err)
}

gov.InitGenesis(ctx, app.govKeeper, gov.GenesisState{
StartingProposalID: 1,
DepositProcedure: gov.DepositProcedure{
DepositProcedure: govparams.DepositProcedure{
MinDeposit: sdk.Coins{minDeposit},
MaxDepositPeriod: 1440,
},
Expand Down Expand Up @@ -348,7 +350,7 @@ func (app *IrisApp) replay() int64 {
defer func() {
stateDB.Close()
blockStoreDB.Close()
} ()
}()

curState := sm.LoadState(stateDB)
preState := sm.LoadPreState(stateDB)
Expand Down
18 changes: 9 additions & 9 deletions app/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ import (
"github.com/tendermint/tendermint/crypto"
tmtypes "github.com/tendermint/tendermint/types"

"fmt"
"github.com/cosmos/cosmos-sdk/server"
"github.com/cosmos/cosmos-sdk/server/config"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/wire"
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/cosmos/cosmos-sdk/x/stake"
"time"
"github.com/irisnet/irishub/types"
"fmt"
"time"
)

// State to Unmarshal
Expand Down Expand Up @@ -54,13 +54,13 @@ func (ga *GenesisAccount) ToAccount() (acc *auth.BaseAccount) {
}

var (
flagName = "name"
flagClientHome = "home-client"
flagOWK = "owk"
denom = "iris-atto"
feeAmt = int64(100)
IrisCt = types.NewDefaultCoinType(denom)
freeFermionVal ,_ = IrisCt.ConvertToMinCoin(fmt.Sprintf("%d%s",feeAmt,denom))
flagName = "name"
flagClientHome = "home-client"
flagOWK = "owk"
denom = "iris-atto"
feeAmt = int64(100)
IrisCt = types.NewDefaultCoinType(denom)
freeFermionVal, _ = IrisCt.ConvertToMinCoin(fmt.Sprintf("%d%s", feeAmt, denom))
)

const defaultUnbondingTime time.Duration = 60 * 10 * time.Second
Expand Down
2 changes: 1 addition & 1 deletion modules/gov/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func ErrAddressNotStaked(codespace sdk.CodespaceType, address sdk.AccAddress) sd
return sdk.NewError(codespace, CodeAddressNotStaked, fmt.Sprintf("Address %s is not staked and is thus ineligible to vote", address))
}

func ErrInvalidTitle(codespace sdk.CodespaceType, title string) sdk.Error {
func ErrInvalidTitle(codespace sdk.CodespaceType, title string) sdk.Error {
return sdk.NewError(codespace, CodeInvalidTitle, fmt.Sprintf("Proposal Title '%s' is not valid", title))
}

Expand Down
10 changes: 7 additions & 3 deletions modules/gov/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@ package gov

import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/irisnet/irishub/modules/parameter"
"github.com/irisnet/irishub/modules/gov/params"
)

// GenesisState - all staking state that must be provided at genesis
type GenesisState struct {
StartingProposalID int64 `json:"starting_proposalID"`
DepositProcedure DepositProcedure `json:"deposit_period"`
DepositProcedure govparams.DepositProcedure `json:"deposit_period"`
VotingProcedure VotingProcedure `json:"voting_period"`
TallyingProcedure TallyingProcedure `json:"tallying_procedure"`
}

func NewGenesisState(startingProposalID int64, dp DepositProcedure, vp VotingProcedure, tp TallyingProcedure) GenesisState {
func NewGenesisState(startingProposalID int64, dp govparams.DepositProcedure, vp VotingProcedure, tp TallyingProcedure) GenesisState {
return GenesisState{
StartingProposalID: startingProposalID,
DepositProcedure: dp,
Expand All @@ -28,9 +30,11 @@ func InitGenesis(ctx sdk.Context, k Keeper,data GenesisState) {
// TODO: Handle this with #870
panic(err)
}
k.setDepositProcedure(ctx, data.DepositProcedure)
//k.setDepositProcedure(ctx, data.DepositProcedure)
parameter.InitGenesisParameter(&govparams.DepositProcedureParameter, ctx, data.DepositProcedure)
k.setVotingProcedure(ctx, data.VotingProcedure)
k.setTallyingProcedure(ctx, data.TallyingProcedure)

}

// WriteGenesis - output genesis parameters
Expand Down
49 changes: 16 additions & 33 deletions modules/gov/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/wire"
"github.com/cosmos/cosmos-sdk/x/bank"
"github.com/irisnet/irishub/modules/gov/params"
"github.com/irisnet/irishub/modules/iparams"
"strconv"
"strings"
)

// nolint


// Governance Keeper
type Keeper struct {
// The reference to the ParamSetter to get and set Global Params
Expand Down Expand Up @@ -57,12 +57,12 @@ func (keeper Keeper) WireCodec() *wire.Codec {
// =====================================================
// Proposals

func (keeper Keeper) NewProposal(ctx sdk.Context, title string, description string, proposalType ProposalKind,params Params) Proposal{
func (keeper Keeper) NewProposal(ctx sdk.Context, title string, description string, proposalType ProposalKind, params Params) Proposal {
switch proposalType {
case ProposalTypeText:
return keeper.NewTextProposal(ctx, title, description, proposalType)
case ProposalTypeParameterChange:
return keeper.NewParametersProposal(ctx, title, description, proposalType,params)
return keeper.NewParametersProposal(ctx, title, description, proposalType, params)
case ProposalTypeSoftwareUpgrade:
return keeper.NewUpgradeProposal(ctx, title, description, proposalType)
}
Expand Down Expand Up @@ -91,7 +91,7 @@ func (keeper Keeper) NewTextProposal(ctx sdk.Context, title string, description
return proposal
}

func (keeper Keeper) NewParametersProposal(ctx sdk.Context, title string, description string, proposalType ProposalKind,params Params) Proposal{
func (keeper Keeper) NewParametersProposal(ctx sdk.Context, title string, description string, proposalType ProposalKind, params Params) Proposal {
proposalID, err := keeper.getNewProposalID(ctx)
if err != nil {
return nil
Expand All @@ -115,7 +115,7 @@ func (keeper Keeper) NewParametersProposal(ctx sdk.Context, title string, descri
return proposal
}

func (keeper Keeper) NewUpgradeProposal(ctx sdk.Context, title string, description string, proposalType ProposalKind) Proposal{
func (keeper Keeper) NewUpgradeProposal(ctx sdk.Context, title string, description string, proposalType ProposalKind) Proposal {
proposalID, err := keeper.getNewProposalID(ctx)
if err != nil {
return nil
Expand Down Expand Up @@ -211,26 +211,9 @@ func (keeper Keeper) activateVotingPeriod(ctx sdk.Context, proposal Proposal) {
// Procedures

// Returns the current Deposit Procedure from the global param store
func (keeper Keeper) GetDepositProcedure(ctx sdk.Context) DepositProcedure {
return DepositProcedure{
MinDeposit: keeper.getDepositProcedureDeposit(ctx),
MaxDepositPeriod: keeper.getDepositProcedureMaxDepositPeriod(ctx),
}
}

func (keeper Keeper) getDepositProcedureMaxDepositPeriod(ctx sdk.Context) (MaxDepositPeriod int64) {
var maxDepositPeriod string
if keeper.ps.Get(ctx, ParamStoreKeyDepositProcedureMaxDepositPeriod, &maxDepositPeriod) == nil {
MaxDepositPeriod, _ = strconv.ParseInt(maxDepositPeriod, 10, 64)
}
return
}

func (keeper Keeper) getDepositProcedureDeposit(ctx sdk.Context) (Deposit sdk.Coins) {
var data string
keeper.ps.Get(ctx, ParamStoreKeyDepositProcedureDeposit, &data)
Deposit, _ = sdk.ParseCoins(data)
return
func (keeper Keeper) GetDepositProcedure(ctx sdk.Context) govparams.DepositProcedure {
govparams.DepositProcedureParameter.LoadValue(ctx)
return govparams.DepositProcedureParameter.Value
}

// Returns the current Voting Procedure from the global param store
Expand Down Expand Up @@ -267,14 +250,14 @@ func (keeper Keeper) getTallyingProcedure(ctx sdk.Context, key string) sdk.Rat {

}

func (keeper Keeper) setDepositProcedure(ctx sdk.Context, depositProcedure DepositProcedure) {
minDeposit := depositProcedure.MinDeposit.String()
keeper.ps.Set(ctx, ParamStoreKeyDepositProcedureDeposit, &minDeposit)

maxDepositPeriod := strconv.FormatInt(depositProcedure.MaxDepositPeriod, 10)
keeper.ps.Set(ctx, ParamStoreKeyDepositProcedureMaxDepositPeriod, &maxDepositPeriod)

}
//func (keeper Keeper) setDepositProcedure(ctx sdk.Context, depositProcedure DepositProcedure) {
// minDeposit := depositProcedure.MinDeposit.String()
// keeper.ps.Set(ctx, ParamStoreKeyDepositProcedureDeposit, &minDeposit)
//
// maxDepositPeriod := strconv.FormatInt(depositProcedure.MaxDepositPeriod, 10)
// keeper.ps.Set (ctx, ParamStoreKeyDepositProcedureMaxDepositPeriod, &maxDepositPeriod)
//
//}

func (keeper Keeper) setVotingProcedure(ctx sdk.Context, votingProcedure VotingProcedure) {
votingPeriod := strconv.FormatInt(votingProcedure.VotingPeriod, 10)
Expand Down
13 changes: 13 additions & 0 deletions modules/gov/params/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package govparams

import (
sdk "github.com/cosmos/cosmos-sdk/types"
)

const (
DefaultCodespace sdk.CodespaceType = 5
CodeInvalidMinDeposit sdk.CodeType = 100
CodeInvalidMinDepositDenom sdk.CodeType = 101
CodeInvalidMinDepositAmount sdk.CodeType = 102
CodeInvalidDepositPeriod sdk.CodeType = 103
)
25 changes: 15 additions & 10 deletions modules/gov/params/gov_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,27 @@ import (
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/params"
"github.com/irisnet/irishub/modules/gov"
)

var DepositProcedureParameter DepositProcedureParam

// Procedure around Deposits for governance
type DepositProcedure struct {
MinDeposit sdk.Coins `json:"min_deposit"` // Minimum deposit for a proposal to enter voting period.
MaxDepositPeriod int64 `json:"max_deposit_period"` // Maximum period for Atom holders to deposit on a proposal. Initial value: 2 months
}

type DepositProcedureParam struct {
Value gov.DepositProcedure
psetter params.Setter
pgetter params.Getter
Value DepositProcedure
psetter params.Setter
pgetter params.Getter
}

func (param *DepositProcedureParam) InitGenesis(genesisState interface{}) {
if value, ok := genesisState.(gov.DepositProcedure); ok {
if value, ok := genesisState.(DepositProcedure); ok {
param.Value = value
} else {
param.Value = gov.DepositProcedure{
param.Value = DepositProcedure{
MinDeposit: sdk.Coins{sdk.NewInt64Coin("iris", 10)},
MaxDepositPeriod: 1440}
}
Expand Down Expand Up @@ -66,19 +71,19 @@ func (param *DepositProcedureParam) Valid(jsonStr string) sdk.Error {
if err = json.Unmarshal([]byte(jsonStr), &param.Value); err == nil {

if param.Value.MinDeposit[0].Denom != "iris" {
return sdk.NewError(gov.DefaultCodespace, gov.CodeInvalidMinDepositDenom, fmt.Sprintf("It should be iris "))
return sdk.NewError(DefaultCodespace, CodeInvalidMinDepositDenom, fmt.Sprintf("It should be iris "))
}

if param.Value.MinDeposit[0].Amount.GT(sdk.NewInt(10)) && param.Value.MinDeposit[0].Amount.LT(sdk.NewInt(20000)) {
return sdk.NewError(gov.DefaultCodespace, gov.CodeInvalidMinDepositAmount, fmt.Sprintf("MinDepositAmount should be larger than 10 and less than 20000"))
return sdk.NewError(DefaultCodespace, CodeInvalidMinDepositAmount, fmt.Sprintf("MinDepositAmount should be larger than 10 and less than 20000"))
}

if param.Value.MaxDepositPeriod > 20 && param.Value.MaxDepositPeriod < 20000 {
return sdk.NewError(gov.DefaultCodespace, gov.CodeInvalidDepositPeriod, fmt.Sprintf("MaxDepositPeriod should be larger than 20 and less than 20000"))
return sdk.NewError(DefaultCodespace, CodeInvalidDepositPeriod, fmt.Sprintf("MaxDepositPeriod should be larger than 20 and less than 20000"))
}

return nil

}
return sdk.NewError(gov.DefaultCodespace, gov.CodeInvalidMinDeposit, fmt.Sprintf("Json is not valid"))
return sdk.NewError(DefaultCodespace, CodeInvalidMinDeposit, fmt.Sprintf("Json is not valid"))
}
Loading

0 comments on commit 2bc580b

Please sign in to comment.