Skip to content
This repository has been archived by the owner on Jun 12, 2024. It is now read-only.

Commit

Permalink
remove min withdraw for voter and delegator
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhimaoL committed Sep 15, 2018
1 parent b9aec53 commit c09cb30
Show file tree
Hide file tree
Showing 11 changed files with 35 additions and 116 deletions.
2 changes: 0 additions & 2 deletions app/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
2 changes: 0 additions & 2 deletions app/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
2 changes: 0 additions & 2 deletions app/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
2 changes: 0 additions & 2 deletions param/holder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
6 changes: 0 additions & 6 deletions param/holder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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),
Expand Down
12 changes: 4 additions & 8 deletions param/param.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 0 additions & 4 deletions x/proposal/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
34 changes: 8 additions & 26 deletions x/proposal/msg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,31 +197,23 @@ 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),
DelegatorCoinReturnTimes: int64(7),
}

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
Expand All @@ -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(),
},
{
Expand Down
40 changes: 13 additions & 27 deletions x/vote/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
Expand All @@ -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,
Expand Down
23 changes: 2 additions & 21 deletions x/vote/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
24 changes: 8 additions & 16 deletions x/vote/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand All @@ -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,
},
}
Expand All @@ -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))

Expand All @@ -214,7 +206,7 @@ func TestIsLegalDelegatorWithdraw(t *testing.T) {
delegatedCoin: types.NewCoinFromInt64(0),
delegator: user2,
voter: user1,
withdraw: param.DelegatorMinWithdraw,
withdraw: withdraw,
expectedResult: false,
},
{
Expand All @@ -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,
Expand All @@ -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,
Expand Down

0 comments on commit c09cb30

Please sign in to comment.