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

Commit

Permalink
LNO-150, add stake to report and penalty score
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoxiaff committed Sep 16, 2018
1 parent 1238377 commit bcbb49e
Show file tree
Hide file tree
Showing 12 changed files with 143 additions and 534 deletions.
1 change: 1 addition & 0 deletions app/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ func TestGenesisFromConfig(t *testing.T) {
param.PostParam{
ReportOrUpvoteIntervalSec: 24 * 3600,
PostIntervalSec: 600,
MaxReportReputation: types.NewCoinFromInt64(100 * types.Decimals),
},
param.ReputationParam{
BestContentIndexN: 10,
Expand Down
1 change: 1 addition & 0 deletions app/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ func LinoBlockchainGenState(cdc *wire.Codec, appGenTxs []json.RawMessage) (appSt
param.PostParam{
ReportOrUpvoteIntervalSec: 24 * 3600,
PostIntervalSec: 600,
MaxReportReputation: types.NewCoinFromInt64(100 * types.Decimals),
},
param.ReputationParam{
BestContentIndexN: 10,
Expand Down
1 change: 1 addition & 0 deletions app/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ func TestGetGenesisJson(t *testing.T) {
param.PostParam{
ReportOrUpvoteIntervalSec: 24 * 3600,
PostIntervalSec: 600,
MaxReportReputation: types.NewCoinFromInt64(100 * types.Decimals),
},
param.ReputationParam{
BestContentIndexN: 10,
Expand Down
1 change: 1 addition & 0 deletions param/holder.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ func (ph ParamHolder) InitParam(ctx sdk.Context) error {
postParam := &PostParam{
ReportOrUpvoteIntervalSec: 24 * 3600,
PostIntervalSec: 600,
MaxReportReputation: types.NewCoinFromInt64(100 * types.Decimals),
}
if err := ph.setPostParam(ctx, postParam); err != nil {
return err
Expand Down
2 changes: 2 additions & 0 deletions param/holder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@ func TestInitParam(t *testing.T) {
postParam := PostParam{
ReportOrUpvoteIntervalSec: int64(24 * 3600),
PostIntervalSec: int64(600),
MaxReportReputation: types.NewCoinFromInt64(100 * types.Decimals),
}
checkStorage(t, ctx, ph, globalAllocationParam, infraInternalAllocationParam,
evaluateOfContentValueParam, developerParam, validatorParam, voteParam,
Expand Down Expand Up @@ -391,6 +392,7 @@ func TestInitParamFromConfig(t *testing.T) {
postParam := PostParam{
ReportOrUpvoteIntervalSec: int64(24 * 3600),
PostIntervalSec: int64(600),
MaxReportReputation: types.NewCoinFromInt64(100 * types.Decimals),
}
repParam := ReputationParam{
BestContentIndexN: 10,
Expand Down
5 changes: 3 additions & 2 deletions param/param.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,9 @@ type AccountParam struct {
// ReportOrUpvoteIntervalSec - report interval second
// PostIntervalSec - post interval second
type PostParam struct {
ReportOrUpvoteIntervalSec int64 `json:"report_or_upvote_interval_second"`
PostIntervalSec int64 `json:"post_interval_sec"`
ReportOrUpvoteIntervalSec int64 `json:"report_or_upvote_interval_second"`
PostIntervalSec int64 `json:"post_interval_sec"`
MaxReportReputation types.Coin `json:"max_report_reputation"`
}

// BestContentIndexN - hard cap of how many content can be indexed every round.
Expand Down
8 changes: 6 additions & 2 deletions x/post/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,15 @@ func (event RewardEvent) Execute(
vm vote.VoteManager, rm rep.ReputationManager) sdk.Error {

permlink := types.GetPermlink(event.PostAuthor, event.PostID)
paneltyScore, err := pm.GetPenaltyScore(ctx, permlink)
// check if post is deleted
rep, err := rm.GetSumRep(ctx, permlink)
if err != nil {
return err
}
paneltyScore, err := pm.GetPenaltyScore(ctx, rep)
if err != nil {
return err
}
// check if post is deleted
if isDeleted, err := pm.IsDeleted(ctx, permlink); isDeleted || err != nil {
paneltyScore = sdk.OneRat()
}
Expand Down
199 changes: 24 additions & 175 deletions x/post/event_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package post

import (
"math/big"
"testing"

"github.com/lino-network/lino/types"
Expand All @@ -28,21 +27,15 @@ func TestRewardEvent(t *testing.T) {
err = dm.RegisterDeveloper(ctx, "LinoApp2", types.NewCoinFromInt64(1000000*types.Decimals), "", "", "")
assert.Nil(t, err)

bigString1 := "1000000000000000000000000"
bigString2 := "7777777777777777777777777"
bigStringInt1, _ := new(big.Int).SetString(bigString1, 10)
bigStringInt2, _ := new(big.Int).SetString(bigString2, 10)
testCases := []struct {
testName string
rewardEvent RewardEvent
totalReportOfThePost types.Coin
totalUpvoteOfThePost types.Coin
initRewardPool types.Coin
initRewardWindow types.Coin
expectPostMeta postModel.PostMeta
expectAppWeight sdk.Rat
expectAuthorReward accModel.Reward
expectVoterDeposit types.Coin
testName string
rewardEvent RewardEvent
initRewardPool types.Coin
initRewardWindow types.Coin
expectPostMeta postModel.PostMeta
expectAppWeight sdk.Rat
expectAuthorReward accModel.Reward
expectVoterDeposit types.Coin
}{
{
testName: "normal event",
Expand All @@ -55,12 +48,10 @@ func TestRewardEvent(t *testing.T) {
Friction: types.NewCoinFromInt64(15),
FromApp: types.AccountKey("LinoApp1"),
},
totalReportOfThePost: types.NewCoinFromInt64(0),
totalUpvoteOfThePost: types.NewCoinFromInt64(100),
initRewardPool: types.NewCoinFromInt64(100),
initRewardWindow: types.NewCoinFromInt64(100),
initRewardPool: types.NewCoinFromInt64(100),
initRewardWindow: types.NewCoinFromInt64(100),
expectPostMeta: postModel.PostMeta{
TotalUpvoteCoinDay: types.NewCoinFromInt64(100),
TotalUpvoteCoinDay: types.NewCoinFromInt64(0),
TotalReportCoinDay: types.NewCoinFromInt64(0),
TotalDonateCount: 1,
TotalReward: types.NewCoinFromInt64(100),
Expand All @@ -78,74 +69,6 @@ func TestRewardEvent(t *testing.T) {
},
expectVoterDeposit: types.NewCoinFromInt64(50),
},
{
testName: "100% panelty reward post",
rewardEvent: RewardEvent{
PostAuthor: user,
PostID: postID,
Consumer: user1,
Evaluate: types.NewCoinFromInt64(100),
Original: types.NewCoinFromInt64(100),
Friction: types.NewCoinFromInt64(15),
FromApp: types.AccountKey("LinoApp1"),
},
totalReportOfThePost: types.NewCoinFromInt64(100),
totalUpvoteOfThePost: types.NewCoinFromInt64(100),
initRewardPool: types.NewCoinFromInt64(100),
initRewardWindow: types.NewCoinFromInt64(100),
expectPostMeta: postModel.PostMeta{
TotalUpvoteCoinDay: types.NewCoinFromInt64(100),
TotalReportCoinDay: types.NewCoinFromInt64(100),
TotalDonateCount: 1,
TotalReward: types.NewCoinFromInt64(0),
RedistributionSplitRate: sdk.ZeroRat(),
LastActivityAt: ctx.BlockHeader().Time.Unix(),
},
expectAppWeight: sdk.OneRat(),
expectAuthorReward: accModel.Reward{
TotalIncome: types.NewCoinFromInt64(0),
InflationIncome: types.NewCoinFromInt64(0),
UnclaimReward: types.NewCoinFromInt64(0),
OriginalIncome: types.NewCoinFromInt64(15),
FrictionIncome: types.NewCoinFromInt64(15),
Interest: types.NewCoinFromInt64(0),
},
expectVoterDeposit: types.NewCoinFromInt64(0),
},
{
testName: "50% panelty reward post",
rewardEvent: RewardEvent{
PostAuthor: user,
PostID: postID,
Consumer: user1,
Evaluate: types.NewCoinFromInt64(100),
Original: types.NewCoinFromInt64(100),
Friction: types.NewCoinFromInt64(15),
FromApp: types.AccountKey("LinoApp1"),
},
totalReportOfThePost: types.NewCoinFromInt64(50),
totalUpvoteOfThePost: types.NewCoinFromInt64(100),
initRewardPool: types.NewCoinFromInt64(100),
initRewardWindow: types.NewCoinFromInt64(100),
expectPostMeta: postModel.PostMeta{
TotalUpvoteCoinDay: types.NewCoinFromInt64(100),
TotalReportCoinDay: types.NewCoinFromInt64(50),
TotalDonateCount: 1,
TotalReward: types.NewCoinFromInt64(50),
RedistributionSplitRate: sdk.ZeroRat(),
LastActivityAt: ctx.BlockHeader().Time.Unix(),
},
expectAppWeight: sdk.OneRat(),
expectAuthorReward: accModel.Reward{
TotalIncome: types.NewCoinFromInt64(25),
OriginalIncome: types.NewCoinFromInt64(15),
FrictionIncome: types.NewCoinFromInt64(15),
InflationIncome: types.NewCoinFromInt64(25),
UnclaimReward: types.NewCoinFromInt64(25),
Interest: types.NewCoinFromInt64(0),
},
expectVoterDeposit: types.NewCoinFromInt64(25),
},
{
testName: "evaluate as 1% of total window",
rewardEvent: RewardEvent{
Expand All @@ -157,12 +80,10 @@ func TestRewardEvent(t *testing.T) {
Friction: types.NewCoinFromInt64(15),
FromApp: types.AccountKey("LinoApp1"),
},
totalReportOfThePost: types.NewCoinFromInt64(0),
totalUpvoteOfThePost: types.NewCoinFromInt64(100),
initRewardPool: types.NewCoinFromInt64(100),
initRewardWindow: types.NewCoinFromInt64(100),
initRewardPool: types.NewCoinFromInt64(100),
initRewardWindow: types.NewCoinFromInt64(100),
expectPostMeta: postModel.PostMeta{
TotalUpvoteCoinDay: types.NewCoinFromInt64(100),
TotalUpvoteCoinDay: types.NewCoinFromInt64(0),
TotalReportCoinDay: types.NewCoinFromInt64(0),
TotalDonateCount: 1,
TotalReward: types.NewCoinFromInt64(1),
Expand Down Expand Up @@ -191,19 +112,17 @@ func TestRewardEvent(t *testing.T) {
Friction: types.NewCoinFromInt64(15),
FromApp: types.AccountKey("LinoApp2"),
},
totalReportOfThePost: types.NewCoinFromInt64(0),
totalUpvoteOfThePost: types.NewCoinFromInt64(100),
initRewardPool: types.NewCoinFromInt64(100),
initRewardWindow: types.NewCoinFromInt64(100),
initRewardPool: types.NewCoinFromInt64(100),
initRewardWindow: types.NewCoinFromInt64(100),
expectPostMeta: postModel.PostMeta{
TotalUpvoteCoinDay: types.NewCoinFromInt64(100),
TotalUpvoteCoinDay: types.NewCoinFromInt64(0),
TotalReportCoinDay: types.NewCoinFromInt64(0),
TotalDonateCount: 1,
TotalReward: types.NewCoinFromInt64(100),
RedistributionSplitRate: sdk.ZeroRat(),
LastActivityAt: ctx.BlockHeader().Time.Unix(),
},
expectAppWeight: sdk.NewRat(62251, 156250),
expectAppWeight: sdk.NewRat(1243781, 2500000),
expectAuthorReward: accModel.Reward{
TotalIncome: types.NewCoinFromInt64(50),
OriginalIncome: types.NewCoinFromInt64(15),
Expand All @@ -214,74 +133,6 @@ func TestRewardEvent(t *testing.T) {
},
expectVoterDeposit: types.NewCoinFromInt64(50),
},
{
testName: "test big penalty score with 1.0 report",
rewardEvent: RewardEvent{
PostAuthor: user,
PostID: postID,
Consumer: user1,
Evaluate: types.NewCoinFromInt64(50),
Original: types.NewCoinFromInt64(100),
Friction: types.NewCoinFromInt64(15),
FromApp: types.AccountKey("LinoApp2"),
},
totalReportOfThePost: types.NewCoinFromBigInt(bigStringInt2),
totalUpvoteOfThePost: types.NewCoinFromBigInt(bigStringInt1),
initRewardPool: types.NewCoinFromInt64(100),
initRewardWindow: types.NewCoinFromInt64(100),
expectPostMeta: postModel.PostMeta{
TotalUpvoteCoinDay: types.NewCoinFromBigInt(bigStringInt1),
TotalReportCoinDay: types.NewCoinFromBigInt(bigStringInt2),
TotalDonateCount: 1,
TotalReward: types.NewCoinFromInt64(0),
RedistributionSplitRate: sdk.ZeroRat(),
LastActivityAt: ctx.BlockHeader().Time.Unix(),
},
expectAppWeight: sdk.NewRat(62251, 156250),
expectAuthorReward: accModel.Reward{
TotalIncome: types.NewCoinFromInt64(0),
OriginalIncome: types.NewCoinFromInt64(15),
FrictionIncome: types.NewCoinFromInt64(15),
InflationIncome: types.NewCoinFromInt64(0),
UnclaimReward: types.NewCoinFromInt64(0),
Interest: types.NewCoinFromInt64(0),
},
expectVoterDeposit: types.NewCoinFromInt64(0),
},
{
testName: "test big penalty score with 12.857% report",
rewardEvent: RewardEvent{
PostAuthor: user,
PostID: postID,
Consumer: user1,
Evaluate: types.NewCoinFromInt64(33333),
Original: types.NewCoinFromInt64(100),
Friction: types.NewCoinFromInt64(15),
FromApp: types.AccountKey("LinoApp2"),
},
totalReportOfThePost: types.NewCoinFromBigInt(bigStringInt1),
totalUpvoteOfThePost: types.NewCoinFromBigInt(bigStringInt2),
initRewardPool: types.NewCoinFromInt64(5555),
initRewardWindow: types.NewCoinFromInt64(77777),
expectPostMeta: postModel.PostMeta{
TotalUpvoteCoinDay: types.NewCoinFromBigInt(bigStringInt2),
TotalReportCoinDay: types.NewCoinFromBigInt(bigStringInt1),
TotalDonateCount: 1,
TotalReward: types.NewCoinFromInt64(2075),
RedistributionSplitRate: sdk.ZeroRat(),
LastActivityAt: ctx.BlockHeader().Time.Unix(),
},
expectAppWeight: sdk.NewRat(9350817, 10000000),
expectAuthorReward: accModel.Reward{
TotalIncome: types.NewCoinFromInt64(1038),
OriginalIncome: types.NewCoinFromInt64(15),
FrictionIncome: types.NewCoinFromInt64(15),
InflationIncome: types.NewCoinFromInt64(1038),
UnclaimReward: types.NewCoinFromInt64(1038),
Interest: types.NewCoinFromInt64(0),
},
expectVoterDeposit: types.NewCoinFromInt64(1037),
},
{
testName: "deleted post can't get any inflation",
rewardEvent: RewardEvent{
Expand All @@ -293,20 +144,18 @@ func TestRewardEvent(t *testing.T) {
Friction: types.NewCoinFromInt64(15),
FromApp: types.AccountKey("LinoApp2"),
},
totalReportOfThePost: types.NewCoinFromInt64(0),
totalUpvoteOfThePost: types.NewCoinFromBigInt(bigStringInt2),
initRewardPool: types.NewCoinFromInt64(5555),
initRewardWindow: types.NewCoinFromInt64(77777),
initRewardPool: types.NewCoinFromInt64(5555),
initRewardWindow: types.NewCoinFromInt64(77777),
expectPostMeta: postModel.PostMeta{
TotalUpvoteCoinDay: types.NewCoinFromBigInt(bigStringInt2),
TotalUpvoteCoinDay: types.NewCoinFromInt64(0),
TotalReportCoinDay: types.NewCoinFromInt64(0),
TotalDonateCount: 1,
TotalReward: types.NewCoinFromInt64(0),
RedistributionSplitRate: sdk.ZeroRat(),
IsDeleted: true,
LastActivityAt: ctx.BlockHeader().Time.Unix(),
},
expectAppWeight: sdk.NewRat(9350817, 10000000),
expectAppWeight: sdk.NewRat(1243781, 2500000),
expectAuthorReward: accModel.Reward{
TotalIncome: types.NewCoinFromInt64(0),
OriginalIncome: types.NewCoinFromInt64(15),
Expand All @@ -326,8 +175,8 @@ func TestRewardEvent(t *testing.T) {
})
pm.postStorage.SetPostMeta(ctx, types.GetPermlink(tc.rewardEvent.PostAuthor, tc.rewardEvent.PostID),
&postModel.PostMeta{
TotalUpvoteCoinDay: tc.totalUpvoteOfThePost,
TotalReportCoinDay: tc.totalReportOfThePost,
TotalUpvoteCoinDay: types.NewCoinFromInt64(0),
TotalReportCoinDay: types.NewCoinFromInt64(0),
TotalReward: types.NewCoinFromInt64(0),
IsDeleted: tc.expectPostMeta.IsDeleted,
})
Expand Down
17 changes: 8 additions & 9 deletions x/post/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ func handleReportOrUpvoteMsg(
return ErrPostNotFound(permlink).Result()
}

coinDay, err := am.GetCoinDay(ctx, msg.Username)
postParam, err := pm.paramHolder.GetPostParam(ctx)
if err != nil {
return err.Result()
}
Expand All @@ -271,19 +271,18 @@ func handleReportOrUpvoteMsg(
return err.Result()
}

postParam, err := pm.paramHolder.GetPostParam(ctx)
if err != nil {
return err.Result()
}

if lastReportOrUpvoteAt+postParam.ReportOrUpvoteIntervalSec > ctx.BlockHeader().Time.Unix() {
return ErrReportOrUpvoteTooOften().Result()
}

if err := pm.ReportOrUpvoteToPost(
ctx, permlink, msg.Username, coinDay, msg.IsReport); err != nil {
if msg.IsReport {
if _, err := rm.ReportAt(ctx, msg.Username, permlink); err != nil {
return err.Result()
}
}
if err := pm.UpdateLastActivityAt(ctx, permlink); err != nil {
return err.Result()
}

if err := am.UpdateLastReportOrUpvoteAt(ctx, msg.Username); err != nil {
return err.Result()
}
Expand Down
Loading

0 comments on commit bcbb49e

Please sign in to comment.