Skip to content

Commit

Permalink
set upper limit for frozen money list
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhimaoL committed Sep 14, 2018
1 parent 755d8bc commit 0d72a9a
Show file tree
Hide file tree
Showing 11 changed files with 36 additions and 2 deletions.
1 change: 1 addition & 0 deletions app/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ func TestGenesisFromConfig(t *testing.T) {
MinimumBalance: types.NewCoinFromInt64(1 * types.Decimals),
RegisterFee: types.NewCoinFromInt64(0),
FirstDepositFullCoinDayLimit: types.NewCoinFromInt64(0),
MaxNumFrozenMoney: 200,
},
param.PostParam{
ReportOrUpvoteIntervalSec: 24 * 3600,
Expand Down
1 change: 1 addition & 0 deletions app/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ func LinoBlockchainGenState(cdc *wire.Codec, appGenTxs []json.RawMessage) (appSt
MinimumBalance: types.NewCoinFromInt64(0),
RegisterFee: types.NewCoinFromInt64(1 * types.Decimals),
FirstDepositFullCoinDayLimit: types.NewCoinFromInt64(1 * types.Decimals),
MaxNumFrozenMoney: 200,
},
param.PostParam{
ReportOrUpvoteIntervalSec: 24 * 3600,
Expand Down
1 change: 1 addition & 0 deletions app/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ func TestGetGenesisJson(t *testing.T) {
MinimumBalance: types.NewCoinFromInt64(0),
RegisterFee: types.NewCoinFromInt64(1 * types.Decimals),
FirstDepositFullCoinDayLimit: types.NewCoinFromInt64(1 * types.Decimals),
MaxNumFrozenMoney: 200,
},
param.PostParam{
ReportOrUpvoteIntervalSec: 24 * 3600,
Expand Down
1 change: 1 addition & 0 deletions param/holder.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ func (ph ParamHolder) InitParam(ctx sdk.Context) error {
MinimumBalance: types.NewCoinFromInt64(0),
RegisterFee: types.NewCoinFromInt64(1 * types.Decimals),
FirstDepositFullCoinDayLimit: types.NewCoinFromInt64(1 * types.Decimals),
MaxNumFrozenMoney: 200,
}
if err := ph.setAccountParam(ctx, accountParam); err != nil {
return err
Expand Down
3 changes: 3 additions & 0 deletions param/holder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ func TestAccountParam(t *testing.T) {
MinimumBalance: types.NewCoinFromInt64(1 * types.Decimals),
RegisterFee: types.NewCoinFromInt64(1 * types.Decimals),
FirstDepositFullCoinDayLimit: types.NewCoinFromInt64(1 * types.Decimals),
MaxNumFrozenMoney: 200,
}
err := ph.setAccountParam(ctx, &parameter)
assert.Nil(t, err)
Expand Down Expand Up @@ -295,6 +296,7 @@ func TestInitParam(t *testing.T) {
MinimumBalance: types.NewCoinFromInt64(0),
RegisterFee: types.NewCoinFromInt64(1 * types.Decimals),
FirstDepositFullCoinDayLimit: types.NewCoinFromInt64(1 * types.Decimals),
MaxNumFrozenMoney: 200,
}
postParam := PostParam{
ReportOrUpvoteIntervalSec: int64(24 * 3600),
Expand Down Expand Up @@ -387,6 +389,7 @@ func TestInitParamFromConfig(t *testing.T) {
MinimumBalance: types.NewCoinFromInt64(0),
RegisterFee: types.NewCoinFromInt64(1 * types.Decimals),
FirstDepositFullCoinDayLimit: types.NewCoinFromInt64(1 * types.Decimals),
MaxNumFrozenMoney: 200,
}
postParam := PostParam{
ReportOrUpvoteIntervalSec: int64(24 * 3600),
Expand Down
2 changes: 2 additions & 0 deletions param/param.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,12 @@ type BandwidthParam struct {
// MinimumBalance - minimum balance each account need to maintain
// RegisterFee - register fee need to pay to developer inflation pool for each account registration
// FirstDepositFullCoinDayLimit - when register account, some of coin day of register fee to newly open account will be fully charged
// MaxNumFrozenMoney - the upper limit for each person's ongoing frozen money
type AccountParam struct {
MinimumBalance types.Coin `json:"minimum_balance"`
RegisterFee types.Coin `json:"register_fee"`
FirstDepositFullCoinDayLimit types.Coin `json:"first_deposit_full_coin_day_limit"`
MaxNumFrozenMoney int64 `json:"max_num_frozen_money"`
}

// PostParam - post parameters
Expand Down
1 change: 1 addition & 0 deletions types/errorcode.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ const (
CodeFailedToUnmarshalRewardHistory sdk.CodeType = 359
CodeGetLastPostAt sdk.CodeType = 360
CodeUpdateLastPostAt sdk.CodeType = 361
CodeFrozenMoneyListTooLong sdk.CodeType = 362

// Lino post errors reserve 400 ~ 499
CodePostMetaNotFound sdk.CodeType = 400
Expand Down
5 changes: 5 additions & 0 deletions x/account/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ func ErrGetFrozenMoneyList(err error) sdk.Error {
return types.NewError(types.CodeGetFrozenMoneyList, fmt.Sprintf("failed to get frozen money list: %s", err.Error()))
}

// ErrFrozenMoneyListTooLong - error when the length of frozen money list exceeds the upper limit
func ErrFrozenMoneyListTooLong() sdk.Error {
return types.NewError(types.CodeFrozenMoneyListTooLong, fmt.Sprintf("frozen money list too long"))
}

// ErrIncreaseSequenceByOne - error when increase sequence number failed
func ErrIncreaseSequenceByOne(err error) sdk.Error {
return types.NewError(types.CodeIncreaseSequenceByOne, fmt.Sprintf("failed to increase sequence by one: %s", err.Error()))
Expand Down
11 changes: 10 additions & 1 deletion x/account/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -1097,8 +1097,17 @@ func (accManager AccountManager) AddFrozenMoney(
Interval: interval,
Times: times,
}
accountBank.FrozenMoneyList = append(accountBank.FrozenMoneyList, frozenMoney)

accParams, err := accManager.paramHolder.GetAccountParam(ctx)
if err != nil {
return err
}

if int64(len(accountBank.FrozenMoneyList)) >= accParams.MaxNumFrozenMoney {
return ErrFrozenMoneyListTooLong()
}

accountBank.FrozenMoneyList = append(accountBank.FrozenMoneyList, frozenMoney)
if err := accManager.storage.SetBankFromAccountKey(ctx, username, accountBank); err != nil {
return err
}
Expand Down
3 changes: 2 additions & 1 deletion x/proposal/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -863,7 +863,8 @@ func (msg ChangeAccountParamMsg) ValidateBasic() sdk.Error {

if !msg.Parameter.MinimumBalance.IsNotNegative() ||
!msg.Parameter.RegisterFee.IsNotNegative() ||
!msg.Parameter.FirstDepositFullCoinDayLimit.IsNotNegative() {
!msg.Parameter.FirstDepositFullCoinDayLimit.IsNotNegative() ||
msg.Parameter.MaxNumFrozenMoney <= 0 {
return ErrIllegalParameter()
}
if utf8.RuneCountInString(msg.Reason) > types.MaximumLengthOfProposalReason {
Expand Down
9 changes: 9 additions & 0 deletions x/proposal/msg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,7 @@ func TestChangeAccountParamMsg(t *testing.T) {
MinimumBalance: types.NewCoinFromInt64(1 * types.Decimals),
RegisterFee: types.NewCoinFromInt64(1 * types.Decimals),
FirstDepositFullCoinDayLimit: types.NewCoinFromInt64(1 * types.Decimals),
MaxNumFrozenMoney: 200,
}

p2 := p1
Expand All @@ -660,6 +661,9 @@ func TestChangeAccountParamMsg(t *testing.T) {
p5 := p1
p5.FirstDepositFullCoinDayLimit = types.NewCoinFromInt64(-1)

p6 := p1
p6.MaxNumFrozenMoney = -1

testCases := []struct {
testName string
changeAccountParamMsg ChangeAccountParamMsg
Expand Down Expand Up @@ -700,6 +704,11 @@ func TestChangeAccountParamMsg(t *testing.T) {
changeAccountParamMsg: NewChangeAccountParamMsg("user1", p5, ""),
expectedError: ErrIllegalParameter(),
},
{
testName: "negative MaxNumFrozenMoney is invalid",
changeAccountParamMsg: NewChangeAccountParamMsg("user1", p6, ""),
expectedError: ErrIllegalParameter(),
},
{
testName: "reason is too long",
changeAccountParamMsg: NewChangeAccountParamMsg(
Expand Down

0 comments on commit 0d72a9a

Please sign in to comment.