Skip to content

Commit

Permalink
can delegate to anyone and add delegate interest test (lino-network#287
Browse files Browse the repository at this point in the history
)

* can delegate to anyone and add delegate interest test

* add voting power test

* fix voter withdraw bug
  • Loading branch information
ZhimaoL authored and xiaoxiaff committed Sep 18, 2018
1 parent 5ee20aa commit d443be3
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 1 deletion.
103 changes: 103 additions & 0 deletions test/global/delegate_interest_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package global

import (
"testing"
"time"

"github.com/lino-network/lino/test"
"github.com/lino-network/lino/types"
"github.com/tendermint/tendermint/crypto/secp256k1"

"github.com/lino-network/lino/x/post"
vote "github.com/lino-network/lino/x/vote"
)

func TestDelegateInterest(t *testing.T) {
postUserPriv := secp256k1.GenPrivKey()
donatorPriv := secp256k1.GenPrivKey()
u1Priv := secp256k1.GenPrivKey()
u2Priv := secp256k1.GenPrivKey()

postUserName := "poster"
donatorName := "donator"
u1Name := "user1"
u2Name := "user2"

postID := "New Post"

// to recover the coin day
baseTime := time.Now().Unix() + 7200
lb := test.NewTestLinoBlockchain(t, test.DefaultNumOfVal)

test.CreateAccount(t, donatorName, lb, 0,
secp256k1.GenPrivKey(), donatorPriv, secp256k1.GenPrivKey(), "100000")
test.CreateAccount(t, u1Name, lb, 1,
secp256k1.GenPrivKey(), u1Priv, secp256k1.GenPrivKey(), "100000")
test.CreateAccount(t, u2Name, lb, 2,
secp256k1.GenPrivKey(), u2Priv, secp256k1.GenPrivKey(), "100000")
test.CreateAccount(t, postUserName, lb, 3,
secp256k1.GenPrivKey(), postUserPriv, secp256k1.GenPrivKey(), "100000")
test.CreateTestPost(
t, lb, postUserName, postID, 0, postUserPriv, "", "", "", "", "0", baseTime)

donateMsg := post.NewDonateMsg(
donatorName, types.LNO("2000"), postUserName, postID, "", "")
u1DelegateMsg := vote.NewDelegateMsg(u1Name, u2Name, types.LNO("10000"))
u2DelegateMsg := vote.NewDelegateMsg(u2Name, u1Name, types.LNO("40000"))

test.SignCheckDeliver(t, lb, donateMsg, 0, true, donatorPriv, baseTime)
test.SignCheckDeliver(t, lb, u1DelegateMsg, 0, true, u1Priv, baseTime)
test.SignCheckDeliver(t, lb, u2DelegateMsg, 0, true, u2Priv, baseTime)

test.CheckBalance(t, u1Name, lb, types.NewCoinFromInt64(89999*types.Decimals))
test.CheckBalance(t, u2Name, lb, types.NewCoinFromInt64(59999*types.Decimals))

// 3rd day
baseTime += 3600 * 24 * 3
test.SimulateOneBlock(lb, baseTime)

u1ClaimInterestMsg := vote.NewClaimInterestMsg(u1Name)
u2ClaimInterestMsg := vote.NewClaimInterestMsg(u2Name)

test.SignCheckDeliver(t, lb, u1ClaimInterestMsg, 1, true, u1Priv, baseTime)
test.SignCheckDeliver(t, lb, u2ClaimInterestMsg, 1, true, u2Priv, baseTime)

test.CheckBalance(t, u1Name, lb, types.NewCoinFromInt64((89999+0.15748)*types.Decimals))
test.CheckBalance(t, u2Name, lb, types.NewCoinFromInt64((59999+0.62992)*types.Decimals))

u1DelegateMsg = vote.NewDelegateMsg(u1Name, u2Name, types.LNO("20000"))
u2StakeOutMsg := vote.NewStakeOutMsg(u2Name, types.LNO("10000"))
u2DelegatorWithdrawMsg := vote.NewDelegatorWithdrawMsg(u2Name, u1Name, types.LNO("10000"))

test.SignCheckDeliver(t, lb, u1DelegateMsg, 2, true, u1Priv, baseTime)
test.SignCheckDeliver(t, lb, u2StakeOutMsg, 2, false, u2Priv, baseTime)
test.SignCheckDeliver(t, lb, u2DelegatorWithdrawMsg, 3, true, u2Priv, baseTime)
test.SignCheckDeliver(t, lb, donateMsg, 1, true, donatorPriv, baseTime)

// 4th day
baseTime += 3600 * 24 * 1
test.SimulateOneBlock(lb, baseTime)
test.SignCheckDeliver(t, lb, donateMsg, 2, true, donatorPriv, baseTime)

// 5th day
baseTime += 3600 * 24 * 1
test.SimulateOneBlock(lb, baseTime)
test.SignCheckDeliver(t, lb, donateMsg, 3, true, donatorPriv, baseTime)

u1DelegatorWithdrawMsg := vote.NewDelegatorWithdrawMsg(u1Name, u2Name, types.LNO("30000"))
u2DelegatorWithdrawMsg = vote.NewDelegatorWithdrawMsg(u2Name, u1Name, types.LNO("30000"))

test.SignCheckDeliver(t, lb, u1DelegatorWithdrawMsg, 3, true, u1Priv, baseTime)
test.SignCheckDeliver(t, lb, u2DelegatorWithdrawMsg, 4, true, u2Priv, baseTime)

// 6th day
baseTime += 3600 * 24 * 1
test.SimulateOneBlock(lb, baseTime)

test.SignCheckDeliver(t, lb, u1ClaimInterestMsg, 4, true, u1Priv, baseTime)
test.SignCheckDeliver(t, lb, u2ClaimInterestMsg, 5, true, u2Priv, baseTime)

test.CheckBalance(t, u1Name, lb, types.NewCoinFromInt64((69999+1.10088)*types.Decimals))
test.CheckBalance(t, u2Name, lb, types.NewCoinFromInt64((59999+1.57332)*types.Decimals))

}
32 changes: 32 additions & 0 deletions x/vote/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,38 @@ func TestDelegateBasic(t *testing.T) {
assert.Equal(t, delegatedCoin, delegation2.Amount)
}

func TestVotingPowerAndStake(t *testing.T) {
ctx, am, vm, gm, rm := setupTest(t, 0)
handler := NewHandler(vm, am, gm, rm)

voteParam, _ := vm.paramHolder.GetVoteParam(ctx)
minBalance := types.NewCoinFromInt64(5000 * types.Decimals)

// create test users
user1 := createTestAccount(ctx, am, "user1", minBalance.Plus(voteParam.MinStakeIn))
createTestAccount(ctx, am, "user2", minBalance)

// let user1 stake in
msg := NewStakeInMsg("user1", coinToString(voteParam.MinStakeIn))
handler(ctx, msg)

delegatedCoin := types.NewCoinFromInt64(1300 * types.Decimals)
// let user2 delegate power to user1
msg2 := NewDelegateMsg("user2", "user1", coinToString(delegatedCoin))
handler(ctx, msg2)

// let user1 delegate power to user2
msg3 := NewDelegateMsg("user1", "user2", coinToString(delegatedCoin))
handler(ctx, msg3)

votingPower, _ := vm.GetVotingPower(ctx, "user1")
assert.Equal(t, true, votingPower.IsEqual(voteParam.MinStakeIn.Plus(delegatedCoin)))

voter, _ := vm.storage.GetVoter(ctx, user1)
assert.Equal(t, true, voter.LinoStake.IsEqual(voteParam.MinStakeIn.Plus(delegatedCoin)))

}

func TestRevokeBasic(t *testing.T) {
ctx, am, vm, gm, rm := setupTest(t, 0)
handler := NewHandler(vm, am, gm, rm)
Expand Down
10 changes: 9 additions & 1 deletion x/vote/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,9 @@ func (vm VoteManager) IsLegalVoterWithdraw(
return false
}

availableStakes := voter.LinoStake.Minus(voter.DelegateToOthers)
//reject if the remaining coins are not enough
return voter.LinoStake.IsGTE(coin) && coin.IsPositive()
return availableStakes.IsGTE(coin) && coin.IsPositive()
}

// IsLegalDelegatorWithdraw - check if delegator withdraw is valid or not
Expand Down Expand Up @@ -146,6 +147,13 @@ func (vm VoteManager) AddDelegation(ctx sdk.Context, voterName types.AccountKey,
}
}

// add voter if not exist
if !vm.DoesVoterExist(ctx, voterName) {
if err := vm.AddVoter(ctx, voterName, types.NewCoinFromInt64(0)); err != nil {
return err
}
}

// add delegatedPower for voter
voter, err := vm.storage.GetVoter(ctx, voterName)
if err != nil {
Expand Down

0 comments on commit d443be3

Please sign in to comment.