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 2 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
83 changes: 83 additions & 0 deletions modules/gov/gov_params.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package gov

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

var DepositProcedureParameter DepositProcedureParam

type DepositProcedureParam struct {
Value DepositProcedure
ps params.Setter
pg params.Getter
Copy link
Contributor

Choose a reason for hiding this comment

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

psetter & pgetter will be more readable

}

func (param *DepositProcedureParam) InitGenesis(genesisState interface{}) {
if value, ok := genesisState.(DepositProcedure); ok {
Copy link
Contributor

Choose a reason for hiding this comment

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

What happens if the genesisState is nil?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You can see the UnitTest. The value of ok is false.

param.Value = value
} else {
param.Value = DepositProcedure{
MinDeposit: sdk.Coins{sdk.NewInt64Coin("iris", 10)},
MaxDepositPeriod: 1440}
}
}

func (param *DepositProcedureParam) SetReadWriter(setter params.Setter) {
param.ps = setter
param.pg = setter.Getter
}

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

}

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

func (param *DepositProcedureParam) LoadValue(ctx sdk.Context) bool {
err := param.pg.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 {
Copy link
Contributor

Choose a reason for hiding this comment

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

Should return error if Unmarshal failed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Function Valid have done !

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(DefaultCodespace, 102, fmt.Sprintf("It should be iris "))
Copy link
Contributor

Choose a reason for hiding this comment

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

Should use the enum instead of the magic number 102

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It maybe need more design.

}

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

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

return nil

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

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/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 := DepositProcedure{
MinDeposit: sdk.Coins{sdk.NewInt64Coin("iris", 10)},
MaxDepositPeriod: 1440}

p2 := 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)

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

}
}