-
Notifications
You must be signed in to change notification settings - Fork 128
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
Changes from 1 commit
4db5c8f
c9eda4b
9f1d11a
29e37cb
72c8e4d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
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 | ||
} | ||
|
||
func (param *DepositProcedureParam) InitGenesis() { | ||
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(), ¶m.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), ¶m.Value); err == nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should return error if Unmarshal failed? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function |
||
param.SaveValue(ctx) | ||
} | ||
} | ||
|
||
func (param *DepositProcedureParam) Valid(jsonStr string) sdk.Error { | ||
|
||
var err error | ||
|
||
if err = json.Unmarshal([]byte(jsonStr), ¶m.Value); err == nil { | ||
|
||
if param.Value.MinDeposit[0].Denom != "iris" { | ||
return sdk.NewError(DefaultCodespace, 102, fmt.Sprintf("It should be iris ")) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should use the enum instead of the magic number 102 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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")) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
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() | ||
|
||
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() | ||
require.Equal(t, p1, DepositProcedureParameter.Value) | ||
DepositProcedureParameter.LoadValue(ctx) | ||
require.Equal(t, p2, DepositProcedureParameter.Value) | ||
|
||
} | ||
|
||
|
||
|
||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 { | ||
|
There was a problem hiding this comment.
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