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

IRISHUB-317,318: realize the depositProcedureParameter and its UnitTest #210

Merged
merged 5 commits into from
Sep 14, 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
6 changes: 5 additions & 1 deletion modules/gov/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ const (
CodeInvalidVote sdk.CodeType = 9
CodeInvalidGenesis sdk.CodeType = 10
CodeInvalidProposalStatus sdk.CodeType = 11
CodeInvalidMinDeposit sdk.CodeType = 100
CodeInvalidMinDepositDenom sdk.CodeType = 101
CodeInvalidMinDepositAmount sdk.CodeType = 102
CodeInvalidDepositPeriod sdk.CodeType = 103
)

//----------------------------------------
Expand All @@ -46,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
4 changes: 3 additions & 1 deletion modules/gov/msgs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,10 @@ func TestMsgSubmitProposal(t *testing.T) {
{"Test Proposal", "the purpose of this proposal is to test", ProposalTypeText, addrs[0], coinsMulti, true},
}

var params Params
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should create a new folder "params" under the "gov", and put all these source files in it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I will do it tomorrow.


for i, tc := range tests {
msg := NewMsgSubmitProposal(tc.title, tc.description, tc.proposalType, tc.proposerAddr, tc.initialDeposit)
msg := NewMsgSubmitProposal(tc.title, tc.description, tc.proposalType, tc.proposerAddr, tc.initialDeposit, params)
if tc.expectPass {
require.Nil(t, msg.ValidateBasic(), "test: %v", i)
} else {
Expand Down
84 changes: 84 additions & 0 deletions modules/gov/params/gov_params.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package govparams

import (
"encoding/json"
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/params"
"github.com/irisnet/irishub/modules/gov"
)

var DepositProcedureParameter DepositProcedureParam

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

func (param *DepositProcedureParam) InitGenesis(genesisState interface{}) {
if value, ok := genesisState.(gov.DepositProcedure); ok {
param.Value = value
} else {
param.Value = gov.DepositProcedure{
MinDeposit: sdk.Coins{sdk.NewInt64Coin("iris", 10)},
MaxDepositPeriod: 1440}
}
}

func (param *DepositProcedureParam) SetReadWriter(setter params.Setter) {
param.psetter = setter
param.pgetter = setter.Getter
}

func (param *DepositProcedureParam) GetStoreKey() string {
return "Gov/gov/depositProcedure"

}

func (param *DepositProcedureParam) SaveValue(ctx sdk.Context) {
param.psetter.Set(ctx, param.GetStoreKey(), param.Value)
}

func (param *DepositProcedureParam) LoadValue(ctx sdk.Context) bool {
err := param.pgetter.Get(ctx, param.GetStoreKey(), &param.Value)
if err != nil {
return false
}
return true
}

func (param *DepositProcedureParam) ToJson() string {
jsonBytes, _ := json.Marshal(param.Value)
return string(jsonBytes)
}

func (param *DepositProcedureParam) Update(ctx sdk.Context, jsonStr string) {
if err := json.Unmarshal([]byte(jsonStr), &param.Value); err == nil {
param.SaveValue(ctx)
}
}

func (param *DepositProcedureParam) Valid(jsonStr string) sdk.Error {

var err 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 "))
}

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"))
}

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 nil

}
return sdk.NewError(gov.DefaultCodespace, gov.CodeInvalidMinDeposit, fmt.Sprintf("Json is not valid"))
}
61 changes: 61 additions & 0 deletions modules/gov/params/gov_params_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package govparams

import (
"github.com/cosmos/cosmos-sdk/store"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/wire"
"github.com/cosmos/cosmos-sdk/x/params"
"github.com/irisnet/irishub/modules/gov"
"github.com/stretchr/testify/require"
abci "github.com/tendermint/tendermint/abci/types"
dbm "github.com/tendermint/tendermint/libs/db"
"github.com/tendermint/tendermint/libs/log"
"testing"
)

func defaultContext(key sdk.StoreKey) sdk.Context {
db := dbm.NewMemDB()
cms := store.NewCommitMultiStore(db)
cms.MountStoreWithDB(key, sdk.StoreTypeIAVL, db)
cms.LoadLatestVersion()
ctx := sdk.NewContext(cms, abci.Header{}, false, log.NewNopLogger())
return ctx
}

func TestDepositProcedureParam(t *testing.T) {
skey := sdk.NewKVStoreKey("params")
ctx := defaultContext(skey)
paramKeeper := params.NewKeeper(wire.NewCodec(), skey)

p1 := gov.DepositProcedure{
MinDeposit: sdk.Coins{sdk.NewInt64Coin("iris", 10)},
MaxDepositPeriod: 1440}

p2 := gov.DepositProcedure{
MinDeposit: sdk.Coins{sdk.NewInt64Coin("iris", 30)},
MaxDepositPeriod: 1440}

DepositProcedureParameter.SetReadWriter(paramKeeper.Setter())
find := DepositProcedureParameter.LoadValue(ctx)
require.Equal(t, find, false)

DepositProcedureParameter.InitGenesis(nil)
require.Equal(t, p1, DepositProcedureParameter.Value)

require.Equal(t, DepositProcedureParameter.ToJson(), "{\"min_deposit\":[{\"denom\":\"iris\",\"amount\":\"10\"}],\"max_deposit_period\":1440}")
DepositProcedureParameter.Update(ctx, "{\"min_deposit\":[{\"denom\":\"iris\",\"amount\":\"30\"}],\"max_deposit_period\":1440}")
require.NotEqual(t, p1, DepositProcedureParameter.Value)
require.Equal(t, p2, DepositProcedureParameter.Value)

result := DepositProcedureParameter.Valid("{\"min_deposit\":[{\"denom\":\"atom\",\"amount\":\"30\"}],\"max_deposit_period\":1440}")
require.Error(t, result)

DepositProcedureParameter.InitGenesis(p2)
require.Equal(t, p2, DepositProcedureParameter.Value)
DepositProcedureParameter.InitGenesis(p1)
require.Equal(t, p1, DepositProcedureParameter.Value)

DepositProcedureParameter.LoadValue(ctx)
require.Equal(t, p2, DepositProcedureParameter.Value)

}
16 changes: 7 additions & 9 deletions modules/parameter/parameter.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
package parameter

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

type Parameter interface {

InitGenesis()
InitGenesis(interface{})

GetStoreKey() string

SetReadWriter(setter params.Setter)

SaveValue(ctx sdk.Context)

LoadValue(ctx sdk.Context) bool
Expand All @@ -20,23 +22,19 @@ type SignalParameter interface {
}

type GovParameter interface {

Parameter

Valid(json string) error

Update(ctx sdk.Context, json string)

ToJson() string

}

type GovArrayParameter interface {

GovParameter

LoadValueByKey(ctx sdk.Context, key string) bool

Insert(ctx sdk.Context, json string)

}
}