From c09cb303bbf875fecfb5ca935020dfc0b5351042 Mon Sep 17 00:00:00 2001 From: zhimao liu Date: Sat, 15 Sep 2018 12:30:20 -0700 Subject: [PATCH] remove min withdraw for voter and delegator --- app/app_test.go | 2 -- app/genesis.go | 2 -- app/genesis_test.go | 2 -- param/holder.go | 2 -- param/holder_test.go | 6 ------ param/param.go | 12 ++++-------- x/proposal/msg.go | 4 ---- x/proposal/msg_test.go | 34 ++++++++-------------------------- x/vote/handler_test.go | 40 +++++++++++++--------------------------- x/vote/manager.go | 23 ++--------------------- x/vote/manager_test.go | 24 ++++++++---------------- 11 files changed, 35 insertions(+), 116 deletions(-) diff --git a/app/app_test.go b/app/app_test.go index c6cc7b1f..4d0739a8 100644 --- a/app/app_test.go +++ b/app/app_test.go @@ -205,8 +205,6 @@ func TestGenesisFromConfig(t *testing.T) { CDNAllocation: sdk.NewRat(50, 100), }, param.VoteParam{ - VoterMinWithdraw: types.NewCoinFromInt64(2 * types.Decimals), - DelegatorMinWithdraw: types.NewCoinFromInt64(2 * types.Decimals), VoterCoinReturnIntervalSec: int64(7 * 24 * 3600), VoterCoinReturnTimes: int64(7), DelegatorCoinReturnIntervalSec: int64(7 * 24 * 3600), diff --git a/app/genesis.go b/app/genesis.go index 2bfe7891..8342b7bf 100644 --- a/app/genesis.go +++ b/app/genesis.go @@ -165,8 +165,6 @@ func LinoBlockchainGenState(cdc *wire.Codec, appGenTxs []json.RawMessage) (appSt CDNAllocation: sdk.NewRat(50, 100), }, param.VoteParam{ - VoterMinWithdraw: types.NewCoinFromInt64(2 * types.Decimals), - DelegatorMinWithdraw: types.NewCoinFromInt64(2 * types.Decimals), VoterCoinReturnIntervalSec: int64(7 * 24 * 3600), VoterCoinReturnTimes: int64(7), DelegatorCoinReturnIntervalSec: int64(7 * 24 * 3600), diff --git a/app/genesis_test.go b/app/genesis_test.go index d26362bb..6fda6c6a 100644 --- a/app/genesis_test.go +++ b/app/genesis_test.go @@ -66,8 +66,6 @@ func TestGetGenesisJson(t *testing.T) { CDNAllocation: sdk.NewRat(50, 100), }, param.VoteParam{ - VoterMinWithdraw: types.NewCoinFromInt64(2 * types.Decimals), - DelegatorMinWithdraw: types.NewCoinFromInt64(2 * types.Decimals), VoterCoinReturnIntervalSec: int64(7 * 24 * 3600), VoterCoinReturnTimes: int64(7), DelegatorCoinReturnIntervalSec: int64(7 * 24 * 3600), diff --git a/param/holder.go b/param/holder.go index 87c1f50d..5e544231 100644 --- a/param/holder.go +++ b/param/holder.go @@ -110,8 +110,6 @@ func (ph ParamHolder) InitParam(ctx sdk.Context) error { } voteParam := &VoteParam{ - VoterMinWithdraw: types.NewCoinFromInt64(2 * types.Decimals), - DelegatorMinWithdraw: types.NewCoinFromInt64(2 * types.Decimals), VoterCoinReturnIntervalSec: int64(7 * 24 * 3600), VoterCoinReturnTimes: int64(7), DelegatorCoinReturnIntervalSec: int64(7 * 24 * 3600), diff --git a/param/holder_test.go b/param/holder_test.go index 43a77f37..e4f969cd 100644 --- a/param/holder_test.go +++ b/param/holder_test.go @@ -121,8 +121,6 @@ func TestVoteParam(t *testing.T) { ph := NewParamHolder(TestKVStoreKey) ctx := getContext() parameter := VoteParam{ - VoterMinWithdraw: types.NewCoinFromInt64(1 * types.Decimals), - DelegatorMinWithdraw: types.NewCoinFromInt64(1 * types.Decimals), VoterCoinReturnIntervalSec: int64(7 * 24 * 3600), VoterCoinReturnTimes: int64(7), DelegatorCoinReturnIntervalSec: int64(7 * 24 * 3600), @@ -259,8 +257,6 @@ func TestInitParam(t *testing.T) { } voteParam := VoteParam{ - VoterMinWithdraw: types.NewCoinFromInt64(2 * types.Decimals), - DelegatorMinWithdraw: types.NewCoinFromInt64(2 * types.Decimals), VoterCoinReturnIntervalSec: int64(7 * 24 * 3600), VoterCoinReturnTimes: int64(7), DelegatorCoinReturnIntervalSec: int64(7 * 24 * 3600), @@ -352,8 +348,6 @@ func TestInitParamFromConfig(t *testing.T) { } voteParam := VoteParam{ - VoterMinWithdraw: types.NewCoinFromInt64(2 * types.Decimals), - DelegatorMinWithdraw: types.NewCoinFromInt64(2 * types.Decimals), VoterCoinReturnIntervalSec: int64(7 * 24 * 3600), VoterCoinReturnTimes: int64(7), DelegatorCoinReturnIntervalSec: int64(7 * 24 * 3600), diff --git a/param/param.go b/param/param.go index 3b056700..2a02532f 100644 --- a/param/param.go +++ b/param/param.go @@ -41,19 +41,15 @@ type InfraInternalAllocationParam struct { } // VoteParam - vote paramters -// VoterMinWithdraw - minimum amount of Coin to withdraw from a voter -// DelegatorMinWithdraw - minimum amount of Coin to withdraw from a delegation // VoterCoinReturnIntervalSec - when withdraw or revoke, the deposit return to voter by return event // VoterCoinReturnTimes - when withdraw or revoke, the deposit return to voter by return event // DelegatorCoinReturnIntervalSec - when withdraw or revoke, the deposit return to delegator by return event // DelegatorCoinReturnTimes - when withdraw or revoke, the deposit return to delegator by return event type VoteParam struct { - VoterMinWithdraw types.Coin `json:"voter_min_withdraw"` - DelegatorMinWithdraw types.Coin `json:"delegator_min_withdraw"` - VoterCoinReturnIntervalSec int64 `json:"voter_coin_return_interval_second"` - VoterCoinReturnTimes int64 `json:"voter_coin_return_times"` - DelegatorCoinReturnIntervalSec int64 `json:"delegator_coin_return_interval_second"` - DelegatorCoinReturnTimes int64 `json:"delegator_coin_return_times"` + VoterCoinReturnIntervalSec int64 `json:"voter_coin_return_interval_second"` + VoterCoinReturnTimes int64 `json:"voter_coin_return_times"` + DelegatorCoinReturnIntervalSec int64 `json:"delegator_coin_return_interval_second"` + DelegatorCoinReturnTimes int64 `json:"delegator_coin_return_times"` } // ProposalParam - proposal parameters diff --git a/x/proposal/msg.go b/x/proposal/msg.go index 65cb6986..254e653d 100644 --- a/x/proposal/msg.go +++ b/x/proposal/msg.go @@ -548,10 +548,6 @@ func (msg ChangeVoteParamMsg) ValidateBasic() sdk.Error { return ErrIllegalParameter() } - if !msg.Parameter.DelegatorMinWithdraw.IsPositive() || - !msg.Parameter.VoterMinWithdraw.IsPositive() { - return ErrIllegalParameter() - } if utf8.RuneCountInString(msg.Reason) > types.MaximumLengthOfProposalReason { return ErrReasonTooLong() } diff --git a/x/proposal/msg_test.go b/x/proposal/msg_test.go index 6a7dd554..a7251f01 100644 --- a/x/proposal/msg_test.go +++ b/x/proposal/msg_test.go @@ -197,8 +197,6 @@ func TestChangeInfraInternalAllocationParamMsg(t *testing.T) { func TestChangeVoteParamMsg(t *testing.T) { p1 := param.VoteParam{ - VoterMinWithdraw: types.NewCoinFromInt64(1 * types.Decimals), - DelegatorMinWithdraw: types.NewCoinFromInt64(1 * types.Decimals), VoterCoinReturnIntervalSec: int64(7 * 24 * 3600), VoterCoinReturnTimes: int64(7), DelegatorCoinReturnIntervalSec: int64(7 * 24 * 3600), @@ -206,22 +204,16 @@ func TestChangeVoteParamMsg(t *testing.T) { } p2 := p1 - p2.VoterMinWithdraw = types.NewCoinFromInt64(0 * types.Decimals) + p2.VoterCoinReturnIntervalSec = int64(0) p3 := p1 - p3.DelegatorMinWithdraw = types.NewCoinFromInt64(0 * types.Decimals) + p3.VoterCoinReturnTimes = int64(0) p4 := p1 - p4.VoterCoinReturnIntervalSec = int64(0) + p4.DelegatorCoinReturnIntervalSec = int64(-1) p5 := p1 - p5.VoterCoinReturnTimes = int64(0) - - p6 := p1 - p6.DelegatorCoinReturnIntervalSec = int64(-1) - - p7 := p1 - p7.DelegatorCoinReturnTimes = int64(0) + p5.DelegatorCoinReturnTimes = int64(0) testCases := []struct { testName string @@ -233,34 +225,24 @@ func TestChangeVoteParamMsg(t *testing.T) { ChangeVoteParamMsg: NewChangeVoteParamMsg("user1", p1, ""), expectedError: nil, }, - { - testName: "zero voter min withdraw is illegal", - ChangeVoteParamMsg: NewChangeVoteParamMsg("user1", p2, ""), - expectedError: ErrIllegalParameter(), - }, - { - testName: "zero delegator min withdraw is illegal", - ChangeVoteParamMsg: NewChangeVoteParamMsg("user1", p3, ""), - expectedError: ErrIllegalParameter(), - }, { testName: "zero VoterCoinReturnIntervalHr is illegal", - ChangeVoteParamMsg: NewChangeVoteParamMsg("user1", p4, ""), + ChangeVoteParamMsg: NewChangeVoteParamMsg("user1", p2, ""), expectedError: ErrIllegalParameter(), }, { testName: "zero VoterCoinReturnTimes is illegal", - ChangeVoteParamMsg: NewChangeVoteParamMsg("user1", p5, ""), + ChangeVoteParamMsg: NewChangeVoteParamMsg("user1", p3, ""), expectedError: ErrIllegalParameter(), }, { testName: "negative DelegatorCoinReturnIntervalHr is illegal", - ChangeVoteParamMsg: NewChangeVoteParamMsg("user1", p6, ""), + ChangeVoteParamMsg: NewChangeVoteParamMsg("user1", p4, ""), expectedError: ErrIllegalParameter(), }, { testName: "zero DelegatorCoinReturnTimes is illegal", - ChangeVoteParamMsg: NewChangeVoteParamMsg("user1", p7, ""), + ChangeVoteParamMsg: NewChangeVoteParamMsg("user1", p5, ""), expectedError: ErrIllegalParameter(), }, { diff --git a/x/vote/handler_test.go b/x/vote/handler_test.go index 4190dc49..c7280b9a 100644 --- a/x/vote/handler_test.go +++ b/x/vote/handler_test.go @@ -159,47 +159,43 @@ func TestRevokeBasic(t *testing.T) { func TestVoterWithdraw(t *testing.T) { ctx, am, vm, gm := setupTest(t, 0) handler := NewHandler(vm, am, gm) - voteParam, _ := vm.paramHolder.GetVoteParam(ctx) minBalance := types.NewCoinFromInt64(30 * types.Decimals) deposit := types.NewCoinFromInt64(10 * types.Decimals) + withdraw := types.NewCoinFromInt64(1 * types.Decimals) // create test users createTestAccount(ctx, am, "user1", minBalance.Plus(deposit)) // withdraw will fail if hasn't registered as voter - illegalWithdrawMsg := NewStakeOutMsg("user1", coinToString(voteParam.VoterMinWithdraw)) + illegalWithdrawMsg := NewStakeOutMsg("user1", coinToString(deposit)) res := handler(ctx, illegalWithdrawMsg) assert.Equal(t, ErrIllegalWithdraw().Result(), res) // let user1 register as voter - msg := NewStakeInMsg("user1", coinToString(deposit.Plus(voteParam.VoterMinWithdraw))) + msg := NewStakeInMsg("user1", coinToString(deposit)) handler(ctx, msg) day, _ := gm.GetPastDay(ctx, ctx.BlockHeader().Time.Unix()) gs := globalModel.NewGlobalStorage(testGlobalKVStoreKey) linoStat, _ := gs.GetLinoStakeStat(ctx, day) - assert.Equal(t, linoStat.TotalLinoStake, deposit.Plus(voteParam.VoterMinWithdraw)) - assert.Equal(t, linoStat.UnclaimedLinoStake, deposit.Plus(voteParam.VoterMinWithdraw)) + assert.Equal(t, linoStat.TotalLinoStake, deposit) + assert.Equal(t, linoStat.UnclaimedLinoStake, deposit) // invalid deposit invalidDepositMsg := NewStakeInMsg("1du1i2bdi12bud", coinToString(deposit)) res = handler(ctx, invalidDepositMsg) assert.Equal(t, ErrAccountNotFound().Result(), res) - msg2 := NewStakeOutMsg("user1", coinToString(minBalance.Plus(voteParam.VoterMinWithdraw))) + msg2 := NewStakeOutMsg("user1", coinToString(withdraw)) result2 := handler(ctx, msg2) - assert.Equal(t, ErrIllegalWithdraw().Result(), result2) - - msg3 := NewStakeOutMsg("user1", coinToString(voteParam.VoterMinWithdraw)) - result3 := handler(ctx, msg3) - assert.Equal(t, sdk.Result{}, result3) + assert.Equal(t, sdk.Result{}, result2) voter, _ := vm.storage.GetVoter(ctx, "user1") - assert.Equal(t, deposit, voter.LinoStake) + assert.Equal(t, deposit.Minus(withdraw), voter.LinoStake) linoStat, _ = gs.GetLinoStakeStat(ctx, day) - assert.Equal(t, linoStat.TotalLinoStake, deposit) - assert.Equal(t, linoStat.UnclaimedLinoStake, deposit) + assert.Equal(t, linoStat.TotalLinoStake, deposit.Minus(withdraw)) + assert.Equal(t, linoStat.UnclaimedLinoStake, deposit.Minus(withdraw)) } func TestDelegatorWithdraw(t *testing.T) { @@ -209,7 +205,6 @@ func TestDelegatorWithdraw(t *testing.T) { user2 := createTestAccount(ctx, am, "user2", minBalance) handler := NewHandler(vm, am, gm) - param, _ := vm.paramHolder.GetVoteParam(ctx) delegatedCoin := types.NewCoinFromInt64(100 * types.Decimals) delta := types.NewCoinFromInt64(1 * types.Decimals) deposit := types.NewCoinFromInt64(10 * types.Decimals) @@ -230,29 +225,20 @@ func TestDelegatorWithdraw(t *testing.T) { delegatedCoin: types.NewCoinFromInt64(0), delegator: user2, voter: user1, - withdraw: param.DelegatorMinWithdraw, + withdraw: delta, expectedResult: ErrIllegalWithdraw().Result(), }, { - testName: "withdraw amount is less than minimum requirement", + testName: "can't withdraw delegatedCoin+delta", addDelegation: true, delegatedCoin: delegatedCoin, delegator: user2, voter: user1, - withdraw: param.DelegatorMinWithdraw.Minus(delta), - expectedResult: ErrIllegalWithdraw().Result(), - }, - { - testName: "no delegation exist, can't withdraw delegatedCoin+delta", - addDelegation: false, - delegatedCoin: types.NewCoinFromInt64(0), - delegator: user2, - voter: user1, withdraw: delegatedCoin.Plus(delta), expectedResult: ErrIllegalWithdraw().Result(), }, { - testName: "no delegation exist, can't withdraw delegatedCoin-delta", + testName: "normal withdraw", addDelegation: false, delegatedCoin: types.NewCoinFromInt64(0), delegator: user2, diff --git a/x/vote/manager.go b/x/vote/manager.go index f685b1e9..9c63b4a5 100644 --- a/x/vote/manager.go +++ b/x/vote/manager.go @@ -69,17 +69,8 @@ func (vm VoteManager) IsLegalVoterWithdraw( if vm.IsInValidatorList(ctx, username) { return false } - - param, err := vm.paramHolder.GetVoteParam(ctx) - if err != nil { - return false - } - // reject if withdraw is less than minimum voter withdraw - if !coin.IsGTE(param.VoterMinWithdraw) { - return false - } //reject if the remaining coins are not enough - return voter.LinoStake.IsGTE(coin) + return voter.LinoStake.IsGTE(coin) && coin.IsPositive() } // IsLegalDelegatorWithdraw - check if delegator withdraw is valid or not @@ -90,18 +81,8 @@ func (vm VoteManager) IsLegalDelegatorWithdraw( return false } - param, err := vm.paramHolder.GetVoteParam(ctx) - if err != nil { - return false - } - - // reject if withdraw is less than minimum delegator withdraw - if !coin.IsGTE(param.DelegatorMinWithdraw) { - return false - } //reject if the remaining delegation are less than zero - res := delegation.Amount.Minus(coin) - return res.IsNotNegative() + return delegation.Amount.IsGTE(coin) && coin.IsPositive() } // CanBecomeValidator - check if vote deposit meet requirement or not diff --git a/x/vote/manager_test.go b/x/vote/manager_test.go index 4b7a7599..1b8d7fc4 100644 --- a/x/vote/manager_test.go +++ b/x/vote/manager_test.go @@ -132,7 +132,6 @@ func TestIsLegalVoterWithdraw(t *testing.T) { ctx, am, vm, _ := setupTest(t, 0) minBalance := types.NewCoinFromInt64(1 * types.Decimals) user1 := createTestAccount(ctx, am, "user1", minBalance) - param, _ := vm.paramHolder.GetVoteParam(ctx) vm.AddVoter(ctx, user1, types.NewCoinFromInt64(100*types.Decimals)) @@ -143,32 +142,25 @@ func TestIsLegalVoterWithdraw(t *testing.T) { withdraw types.Coin expectedResult bool }{ - { - testName: "illegal withdraw that less than minimum withdraw", - allValidators: []types.AccountKey{}, - username: user1, - withdraw: param.VoterMinWithdraw.Minus(types.NewCoinFromInt64(1 * types.Decimals)), - expectedResult: false, - }, { testName: "normal case", allValidators: []types.AccountKey{}, username: user1, - withdraw: param.VoterMinWithdraw, + withdraw: types.NewCoinFromInt64(1), expectedResult: true, }, { testName: "validator can't withdraw", allValidators: []types.AccountKey{user1}, username: user1, - withdraw: param.VoterMinWithdraw, + withdraw: types.NewCoinFromInt64(1), expectedResult: false, }, { testName: "illegal withdraw", allValidators: []types.AccountKey{}, username: user1, - withdraw: types.NewCoinFromInt64(101), + withdraw: types.NewCoinFromInt64(101 * types.Decimals), expectedResult: false, }, } @@ -193,9 +185,9 @@ func TestIsLegalVoterWithdraw(t *testing.T) { func TestIsLegalDelegatorWithdraw(t *testing.T) { ctx, am, vm, _ := setupTest(t, 0) minBalance := types.NewCoinFromInt64(1 * types.Decimals) + withdraw := types.NewCoinFromInt64(10 * types.Decimals) user1 := createTestAccount(ctx, am, "user1", minBalance) user2 := createTestAccount(ctx, am, "user2", minBalance) - param, _ := vm.paramHolder.GetVoteParam(ctx) vm.AddVoter(ctx, user1, types.NewCoinFromInt64(101*types.Decimals)) @@ -214,7 +206,7 @@ func TestIsLegalDelegatorWithdraw(t *testing.T) { delegatedCoin: types.NewCoinFromInt64(0), delegator: user2, voter: user1, - withdraw: param.DelegatorMinWithdraw, + withdraw: withdraw, expectedResult: false, }, { @@ -223,11 +215,11 @@ func TestIsLegalDelegatorWithdraw(t *testing.T) { delegatedCoin: types.NewCoinFromInt64(100 * types.Decimals), delegator: user2, voter: user1, - withdraw: param.DelegatorMinWithdraw, + withdraw: withdraw, expectedResult: true, }, { - testName: "no delegation exist, can't withdraw 0", + testName: "can't withdraw 0", addDelegation: false, delegatedCoin: types.NewCoinFromInt64(0), delegator: user2, @@ -236,7 +228,7 @@ func TestIsLegalDelegatorWithdraw(t *testing.T) { expectedResult: false, }, { - testName: "no delegation exist, can't withdraw 101", + testName: "can't withdraw 101", addDelegation: false, delegatedCoin: types.NewCoinFromInt64(0), delegator: user2,