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

Commit

Permalink
update coin day mechanism (#298)
Browse files Browse the repository at this point in the history
* LNO-157, add documentation

* LNO-157, update equation

* LNO-159, update add coin day mechanism.

* LNO-159, update height
  • Loading branch information
xiaoxiaff authored and ZhimaoL committed Oct 10, 2018
1 parent be8f263 commit 127baec
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 5 deletions.
4 changes: 2 additions & 2 deletions docs/core/ecv.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@ After the evaluate result added to the window, after 7 days, this evaluate resul
```
Where w is total amount in seven-days window and p is the penalty score. The penalty score is calculated as:
```
p = 1 - min(report/upvote, 1)
```
p = 1 - min(report/upvote, 1)
```
3 changes: 3 additions & 0 deletions types/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,4 +170,7 @@ const (

// LinoBlockchainFirstUpdateHeight - first blockchain update
LinoBlockchainFirstUpdateHeight = 156000

// LinoBlockchainSecondUpdateHeight - second blockchain update
LinoBlockchainSecondUpdateHeight = 356000
)
11 changes: 8 additions & 3 deletions x/account/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ func (accManager AccountManager) GetCoinDay(
if err != nil {
return types.NewCoinFromInt64(0), err
}

accManager.updateTXFromPendingCoinDayQueue(ctx, bank, pendingCoinDayQueue)

if err := accManager.storage.SetPendingCoinDayQueue(
Expand Down Expand Up @@ -970,8 +969,14 @@ func (accManager AccountManager) addPendingCoinDayToQueue(
return err
}
accManager.updateTXFromPendingCoinDayQueue(ctx, bank, pendingCoinDayQueue)
if len(pendingCoinDayQueue.PendingCoinDays) > 0 && pendingCoinDayQueue.PendingCoinDays[len(pendingCoinDayQueue.PendingCoinDays)-1].StartTime == pendingCoinDay.StartTime {
pendingCoinDayQueue.PendingCoinDays[len(pendingCoinDayQueue.PendingCoinDays)-1].Coin.Plus(pendingCoinDay.Coin)
idx := len(pendingCoinDayQueue.PendingCoinDays) - 1
if len(pendingCoinDayQueue.PendingCoinDays) > 0 && pendingCoinDayQueue.PendingCoinDays[idx].StartTime == pendingCoinDay.StartTime {
if ctx.BlockHeader().Height > types.LinoBlockchainSecondUpdateHeight {
pendingCoinDayQueue.PendingCoinDays[idx].Coin = pendingCoinDayQueue.PendingCoinDays[idx].Coin.Plus(pendingCoinDay.Coin)
pendingCoinDayQueue.TotalCoin = pendingCoinDayQueue.TotalCoin.Plus(pendingCoinDay.Coin)
} else {
pendingCoinDayQueue.PendingCoinDays[idx].Coin.Plus(pendingCoinDay.Coin)
}
} else {
pendingCoinDayQueue.PendingCoinDays = append(pendingCoinDayQueue.PendingCoinDays, pendingCoinDay)
pendingCoinDayQueue.TotalCoin = pendingCoinDayQueue.TotalCoin.Plus(pendingCoinDay.Coin)
Expand Down
107 changes: 107 additions & 0 deletions x/account/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,113 @@ func TestDoesAccountExist(t *testing.T) {
}
}

// https://github.com/lino-network/lino/issues/297
func TestAddCoinBundle(t *testing.T) {
ctx, am, _ := setupTest(t, 1)
testUser := types.AccountKey("testUser")
accParam, _ := am.paramHolder.GetAccountParam(ctx)
coinDayParams, _ := am.paramHolder.GetCoinDayParam(ctx)

baseTime := time.Now()
baseTimeSlot := baseTime.Unix() / types.CoinDayRecordIntervalSec * types.CoinDayRecordIntervalSec
baseTime = time.Unix(baseTimeSlot, 0)
d1 := time.Duration(types.CoinDayRecordIntervalSec/10) * time.Second
baseTime1 := baseTime.Add(d1)
baseTime2 := baseTime1.Add(d1)
baseTime3 := baseTime2.Add(d1)

ctx = ctx.WithBlockHeader(abci.Header{Time: baseTime, Height: types.LinoBlockchainSecondUpdateHeight + 1})
createTestAccount(ctx, am, string(testUser))

testCases := []struct {
testName string
amount types.Coin
atWhen time.Time
expectBank model.AccountBank
expectPendingCoinDayQueue model.PendingCoinDayQueue
}{
{
testName: "add coin to account's saving",
amount: c100,
atWhen: baseTime,
expectBank: model.AccountBank{
Saving: accParam.RegisterFee.Plus(c100),
CoinDay: accParam.RegisterFee,
NumOfTx: 2,
},
expectPendingCoinDayQueue: model.PendingCoinDayQueue{
LastUpdatedAt: baseTimeSlot,
TotalCoinDay: sdk.ZeroRat(),
TotalCoin: c100,
PendingCoinDays: []model.PendingCoinDay{
{
StartTime: baseTimeSlot,
EndTime: baseTimeSlot + coinDayParams.SecondsToRecoverCoinDay,
Coin: c100,
},
},
},
},
{
testName: "add coin to same bucket at time 2",
amount: c100,
atWhen: baseTime2,
expectBank: model.AccountBank{
Saving: accParam.RegisterFee.Plus(c200),
CoinDay: accParam.RegisterFee,
NumOfTx: 3,
},
expectPendingCoinDayQueue: model.PendingCoinDayQueue{
LastUpdatedAt: baseTimeSlot,
TotalCoinDay: sdk.ZeroRat(),
TotalCoin: c200,
PendingCoinDays: []model.PendingCoinDay{
{
StartTime: baseTimeSlot,
EndTime: baseTimeSlot + coinDayParams.SecondsToRecoverCoinDay,
Coin: c200,
},
},
},
},
{
testName: "add coin to same bucket at time 3",
amount: c100,
atWhen: baseTime3,
expectBank: model.AccountBank{
Saving: accParam.RegisterFee.Plus(c300),
CoinDay: accParam.RegisterFee,
NumOfTx: 4,
},
expectPendingCoinDayQueue: model.PendingCoinDayQueue{
LastUpdatedAt: baseTimeSlot,
TotalCoinDay: sdk.ZeroRat(),
TotalCoin: c300,
PendingCoinDays: []model.PendingCoinDay{
{
StartTime: baseTimeSlot,
EndTime: baseTimeSlot + coinDayParams.SecondsToRecoverCoinDay,
Coin: c300,
},
},
},
},
}

for _, tc := range testCases {
ctx = ctx.WithBlockHeader(abci.Header{ChainID: "Lino", Height: types.LinoBlockchainSecondUpdateHeight + 1, Time: tc.atWhen})
err := am.AddSavingCoin(
ctx, testUser, tc.amount, "", "", types.TransferIn)

if err != nil {
t.Errorf("%s: failed to add coin, got err: %v", tc.testName, err)
return
}
checkBankKVByUsername(t, ctx, tc.testName, types.AccountKey(testUser), tc.expectBank)
checkPendingCoinDay(t, ctx, tc.testName, types.AccountKey(testUser), tc.expectPendingCoinDayQueue)
}
}

func TestAddCoin(t *testing.T) {
ctx, am, _ := setupTest(t, 1)
accParam, _ := am.paramHolder.GetAccountParam(ctx)
Expand Down

0 comments on commit 127baec

Please sign in to comment.