-
Notifications
You must be signed in to change notification settings - Fork 127
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
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
4db5c8f
IRISHUB-317,IRISHUB-318: realize the depositProcedureParameter and it…
MrXJC c9eda4b
IRISHUB-317: add the input parameter to the InitGenesis()
MrXJC 9f1d11a
IRISHUB-317:refactor the structure of gov_params
MrXJC 29e37cb
IRISHUB-317:make the errors readable
MrXJC 72c8e4d
IRISHUB-317:change the package name
MrXJC File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(), ¶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 { | ||
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(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")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
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 comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, I will do it tomorrow.