Skip to content

Commit

Permalink
move claimInterest to vote module and add basic stake interest test (l…
Browse files Browse the repository at this point in the history
…ino-network#285)

* add basic stake interest test

* move claimInterest to vote module

* finish basic stake interest test

* adjust test case
  • Loading branch information
ZhimaoL authored Sep 17, 2018
1 parent 35da8f5 commit 6799c70
Show file tree
Hide file tree
Showing 20 changed files with 306 additions and 200 deletions.
101 changes: 101 additions & 0 deletions test/global/stake_interest_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
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 TestStakeInterest(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, "", "")
u1StakeInMsg := vote.NewStakeInMsg(u1Name, types.LNO("10000"))
u2StakeInMsg := vote.NewStakeInMsg(u2Name, types.LNO("40000"))

test.SignCheckDeliver(t, lb, donateMsg, 0, true, donatorPriv, baseTime)
test.SignCheckDeliver(t, lb, u1StakeInMsg, 0, true, u1Priv, baseTime)
test.SignCheckDeliver(t, lb, u2StakeInMsg, 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))

u1StakeInMsg = vote.NewStakeInMsg(u1Name, types.LNO("20000"))
u2StakeOutMsg := vote.NewStakeOutMsg(u2Name, types.LNO("10000"))

test.SignCheckDeliver(t, lb, u1StakeInMsg, 2, true, u1Priv, baseTime)
test.SignCheckDeliver(t, lb, u2StakeOutMsg, 2, 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)

u1StakeOutMsg := vote.NewStakeOutMsg(u1Name, types.LNO("30000"))
u2StakeOutMsg = vote.NewStakeOutMsg(u2Name, types.LNO("30000"))

test.SignCheckDeliver(t, lb, u1StakeOutMsg, 3, true, u1Priv, baseTime)
test.SignCheckDeliver(t, lb, u2StakeOutMsg, 3, 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, 4, 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))

}
10 changes: 0 additions & 10 deletions x/account/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ func NewHandler(am AccountManager, gm global.GlobalManager) sdk.Handler {
return handleTransferMsg(ctx, am, msg)
case ClaimMsg:
return handleClaimMsg(ctx, am, msg)
case ClaimInterestMsg:
return handleClaimInterestMsg(ctx, am, msg)
case RecoverMsg:
return handleRecoverMsg(ctx, am, msg)
case RegisterMsg:
Expand Down Expand Up @@ -114,14 +112,6 @@ func handleClaimMsg(ctx sdk.Context, am AccountManager, msg ClaimMsg) sdk.Result
return sdk.Result{}
}

func handleClaimInterestMsg(ctx sdk.Context, am AccountManager, msg ClaimInterestMsg) sdk.Result {
// claim interest
if err := am.ClaimInterest(ctx, msg.Username); err != nil {
return err.Result()
}
return sdk.Result{}
}

func handleRecoverMsg(ctx sdk.Context, am AccountManager, msg RecoverMsg) sdk.Result {
// recover
if !am.DoesAccountExist(ctx, msg.Username) {
Expand Down
32 changes: 0 additions & 32 deletions x/account/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -658,38 +658,6 @@ func (accManager AccountManager) ClaimReward(
return nil
}

// ClaimInterest - add lino power interst to user balance
func (accManager AccountManager) ClaimInterest(
ctx sdk.Context, username types.AccountKey) sdk.Error {
reward, err := accManager.storage.GetReward(ctx, username)
if err != nil {
return err
}
if err := accManager.AddSavingCoin(
ctx, username, reward.Interest, "", "", types.ClaimInterest); err != nil {
return err
}
reward.Interest = types.NewCoinFromInt64(0)
if err := accManager.storage.SetReward(ctx, username, reward); err != nil {
return err
}
return nil
}

// AddInterest - add interst
func (accManager AccountManager) AddInterest(
ctx sdk.Context, username types.AccountKey, interest types.Coin) sdk.Error {
reward, err := accManager.storage.GetReward(ctx, username)
if err != nil {
return err
}
reward.Interest = reward.Interest.Plus(interest)
if err := accManager.storage.SetReward(ctx, username, reward); err != nil {
return err
}
return nil
}

// ClearRewardHistory - clear user reward history
func (accManager AccountManager) ClearRewardHistory(
ctx sdk.Context, username types.AccountKey) sdk.Error {
Expand Down
58 changes: 0 additions & 58 deletions x/account/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1033,7 +1033,6 @@ func TestCreateAccountNormalCase(t *testing.T) {
InflationIncome: types.NewCoinFromInt64(0),
FrictionIncome: types.NewCoinFromInt64(0),
UnclaimReward: types.NewCoinFromInt64(0),
Interest: types.NewCoinFromInt64(0),
}
checkAccountReward(t, ctx, tc.testName, tc.username, reward)

Expand Down Expand Up @@ -1134,7 +1133,6 @@ func TestCreateAccountWithLargeRegisterFee(t *testing.T) {
InflationIncome: types.NewCoinFromInt64(0),
FrictionIncome: types.NewCoinFromInt64(0),
UnclaimReward: types.NewCoinFromInt64(0),
Interest: types.NewCoinFromInt64(0),
}
checkAccountReward(t, ctx, testName, accKey, reward)

Expand Down Expand Up @@ -1434,7 +1432,6 @@ func TestAddIncomeAndReward(t *testing.T) {
FrictionIncome: c200,
InflationIncome: c300,
UnclaimReward: c300,
Interest: c0,
}
checkAccountReward(t, ctx, testName, accKey, reward)
checkRewardHistory(t, ctx, testName, accKey, 0, 1)
Expand All @@ -1450,7 +1447,6 @@ func TestAddIncomeAndReward(t *testing.T) {
FrictionIncome: c500,
InflationIncome: c500,
UnclaimReward: c500,
Interest: c0,
}
checkAccountReward(t, ctx, testName, accKey, reward)
checkRewardHistory(t, ctx, testName, accKey, 0, 2)
Expand All @@ -1466,7 +1462,6 @@ func TestAddIncomeAndReward(t *testing.T) {
FrictionIncome: c500,
InflationIncome: c500,
UnclaimReward: c500,
Interest: c0,
}
checkAccountReward(t, ctx, testName, accKey, reward)
checkRewardHistory(t, ctx, testName, accKey, 0, 2)
Expand Down Expand Up @@ -1494,59 +1489,6 @@ func TestAddIncomeAndReward(t *testing.T) {
FrictionIncome: c500,
InflationIncome: c500,
UnclaimReward: c0,
Interest: c0,
}
checkAccountReward(t, ctx, testName, accKey, reward)
}

func TestAddAndClaimInterest(t *testing.T) {
testName := "TestAddAndClaimInterest"

ctx, am, _ := setupTest(t, 1)
accParam, _ := am.paramHolder.GetAccountParam(ctx)
accKey := types.AccountKey("accKey")

createTestAccount(ctx, am, string(accKey))

err := am.AddInterest(ctx, accKey, c500)
if err != nil {
t.Errorf("%s: failed to add interest, got err %v", testName, err)
}

reward := model.Reward{
TotalIncome: c0,
OriginalIncome: c0,
FrictionIncome: c0,
InflationIncome: c0,
UnclaimReward: c0,
Interest: c500,
}
checkAccountReward(t, ctx, testName, accKey, reward)

bank := model.AccountBank{
Saving: accParam.RegisterFee,
NumOfTx: 1,
NumOfReward: 0,
CoinDay: accParam.RegisterFee,
}
checkBankKVByUsername(t, ctx, testName, accKey, bank)

err = am.ClaimInterest(ctx, accKey)
if err != nil {
t.Errorf("%s: failed to add claim interest, got err %v", testName, err)
}

bank.Saving = accParam.RegisterFee.Plus(c500)
bank.NumOfTx += 1
checkBankKVByUsername(t, ctx, testName, accKey, bank)

reward = model.Reward{
TotalIncome: c0,
OriginalIncome: c0,
FrictionIncome: c0,
InflationIncome: c0,
UnclaimReward: c0,
Interest: c0,
}
checkAccountReward(t, ctx, testName, accKey, reward)
}
Expand Down
1 change: 0 additions & 1 deletion x/account/model/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ type FollowingMeta struct {

// Reward - get from the inflation pool
type Reward struct {
Interest types.Coin `json:"interest"`
TotalIncome types.Coin `json:"total_income"`
OriginalIncome types.Coin `json:"original_income"`
FrictionIncome types.Coin `json:"friction_income"`
Expand Down
1 change: 0 additions & 1 deletion x/account/model/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ func TestAccountReward(t *testing.T) {
ctx := getContext()

reward := Reward{
Interest: types.NewCoinFromInt64(0),
TotalIncome: types.NewCoinFromInt64(0),
OriginalIncome: types.NewCoinFromInt64(0),
FrictionIncome: types.NewCoinFromInt64(0),
Expand Down
53 changes: 0 additions & 53 deletions x/account/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
var _ types.Msg = FollowMsg{}
var _ types.Msg = UnfollowMsg{}
var _ types.Msg = ClaimMsg{}
var _ types.Msg = ClaimInterestMsg{}
var _ types.Msg = TransferMsg{}
var _ types.Msg = RecoverMsg{}
var _ types.Msg = RegisterMsg{}
Expand Down Expand Up @@ -47,11 +46,6 @@ type ClaimMsg struct {
Username types.AccountKey `json:"username"`
}

// ClaimInterestMsg - claim interest generated from lino power
type ClaimInterestMsg struct {
Username types.AccountKey `json:"username"`
}

// RecoverMsg - replace three public keys
type RecoverMsg struct {
Username types.AccountKey `json:"username"`
Expand Down Expand Up @@ -221,53 +215,6 @@ func (msg ClaimMsg) GetConsumeAmount() types.Coin {
return types.NewCoinFromInt64(0)
}

// NewClaimInterestMsg - return a ClaimInterestMsg
func NewClaimInterestMsg(username string) ClaimInterestMsg {
return ClaimInterestMsg{
Username: types.AccountKey(username),
}
}

// Type - implements sdk.Msg
func (msg ClaimInterestMsg) Type() string { return types.AccountRouterName }

// ValidateBasic - implements sdk.Msg
func (msg ClaimInterestMsg) ValidateBasic() sdk.Error {
if len(msg.Username) < types.MinimumUsernameLength ||
len(msg.Username) > types.MaximumUsernameLength {
return ErrInvalidUsername("illegal length")
}
return nil
}

func (msg ClaimInterestMsg) String() string {
return fmt.Sprintf("ClaimInterestMsg{Username:%v}", msg.Username)
}

// GetPermission - implements types.Msg
func (msg ClaimInterestMsg) GetPermission() types.Permission {
return types.AppPermission
}

// GetSignBytes - implements sdk.Msg
func (msg ClaimInterestMsg) GetSignBytes() []byte {
b, err := msgCdc.MarshalJSON(msg) // XXX: ensure some canonical form
if err != nil {
panic(err)
}
return b
}

// GetSigners - implements sdk.Msg
func (msg ClaimInterestMsg) GetSigners() []sdk.AccAddress {
return []sdk.AccAddress{sdk.AccAddress(msg.Username)}
}

// GetConsumeAmount - implements types.Msg
func (msg ClaimInterestMsg) GetConsumeAmount() types.Coin {
return types.NewCoinFromInt64(0)
}

// NewTransferMsg - return a TransferMsg
func NewTransferMsg(sender, receiver string, amount types.LNO, memo string) TransferMsg {
return TransferMsg{
Expand Down
34 changes: 0 additions & 34 deletions x/account/msg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,40 +269,6 @@ func TestClaimMsg(t *testing.T) {
}
}

func TestClaimInterestMsg(t *testing.T) {
testCases := map[string]struct {
msg ClaimInterestMsg
wantCode sdk.CodeType
}{
"normal case": {
msg: NewClaimInterestMsg("test"),
wantCode: sdk.CodeOK,
},
"invalid claim interest - Username is too short": {
msg: NewClaimInterestMsg("te"),
wantCode: types.CodeInvalidUsername,
},
"invalid claim interest - Username is too long": {
msg: NewClaimInterestMsg("testtesttesttesttesttest"),
wantCode: types.CodeInvalidUsername,
},
}

for testName, tc := range testCases {
got := tc.msg.ValidateBasic()

if got == nil {
if tc.wantCode != sdk.CodeOK {
t.Errorf("%s: diff error: got %v, want %v", testName, tc.wantCode, tc.wantCode)
}
continue
}
if got.Code() != tc.wantCode {
t.Errorf("%s: diff error code: got %v, want %v", testName, got.Code(), tc.wantCode)
}
}
}

func TestUpdateAccountMsg(t *testing.T) {
testCases := map[string]struct {
msg UpdateAccountMsg
Expand Down
Loading

0 comments on commit 6799c70

Please sign in to comment.