This repository has been archived by the owner on Jun 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
manager.go
116 lines (100 loc) · 3.56 KB
/
manager.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package reputation
import (
codec "github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/lino-network/lino/param"
"github.com/lino-network/lino/types"
repv2 "github.com/lino-network/lino/x/reputation/repv2"
)
// ReputationManager - adaptor for reputation math model and cosmos application.
type ReputationManager struct {
storeKey sdk.StoreKey
paramHolder param.ParamHolder
}
// NewReputationManager - require holder for BestContentIndexN
func NewReputationManager(storeKey sdk.StoreKey, holder param.ParamHolder) ReputationKeeper {
return ReputationManager{
storeKey: storeKey,
paramHolder: holder,
}
}
// construct a handler.
func (rep ReputationManager) getHandlerV2(ctx sdk.Context) repv2.Reputation {
store := ctx.KVStore(rep.storeKey)
repStore := repv2.NewReputationStore(store, repv2.DefaultInitialReputation)
param := rep.paramHolder.GetReputationParam(ctx)
handler := repv2.NewReputation(
repStore, param.BestContentIndexN, param.UserMaxN,
repv2.DefaultRoundDurationSeconds,
repv2.DefaultSampleWindowSize,
repv2.DefaultDecayFactor)
return handler
}
func (rep ReputationManager) checkUsername(uid repv2.Uid) sdk.Error {
if len(uid) == 0 {
return ErrAccountNotFound("")
}
return nil
}
func (rep ReputationManager) checkPost(pid repv2.Pid) sdk.Error {
if len(pid) == 0 {
return ErrPostNotFound("")
}
return nil
}
func (rep ReputationManager) basicCheck(uid repv2.Uid, pid repv2.Pid) sdk.Error {
err := rep.checkUsername(uid)
if err != nil {
return err
}
err = rep.checkPost(pid)
return err
}
// DonateAt - It's caller's responsibility that parameters are all correct,
// although we do have some checks.
func (rep ReputationManager) DonateAt(ctx sdk.Context,
username types.AccountKey, post types.Permlink, amount types.MiniDollar) (types.MiniDollar, sdk.Error) {
uid := string(username)
pid := string(post)
err := rep.basicCheck(repv2.Uid(uid), repv2.Pid(pid))
if err != nil {
return types.NewMiniDollar(0), err
}
// Update6, start to use new reputation algorithm.
handler := rep.getHandlerV2(ctx)
dp := handler.DonateAt(repv2.Uid(uid), repv2.Pid(pid), repv2.NewIntFromBig(amount.Int.BigInt()))
return types.NewMiniDollarFromBig(dp.Int), nil
}
// Update - on blocker end, update reputation time related information.
func (rep ReputationManager) Update(ctx sdk.Context) sdk.Error {
handler := rep.getHandlerV2(ctx)
handler.Update(repv2.Time(ctx.BlockHeader().Time.Unix()))
return nil
}
// GetRepution - return reputation of @p username, costomnerScore + freeScore.
func (rep ReputationManager) GetReputation(ctx sdk.Context, username types.AccountKey) (types.MiniDollar, sdk.Error) {
uid := string(username)
err := rep.checkUsername(repv2.Uid(uid))
if err != nil {
return types.NewMiniDollar(0), err
}
handler := rep.getHandlerV2(ctx)
return types.NewMiniDollarFromBig(handler.GetReputation(repv2.Uid(uid)).Int), nil
}
// GetCurrentRound of now
func (rep ReputationManager) GetCurrentRound(ctx sdk.Context) (int64, sdk.Error) {
repv2 := rep.getHandlerV2(ctx)
_, ts := repv2.GetCurrentRound()
return int64(ts), nil
}
// ExportToFile state of reputation system.
func (rep ReputationManager) ExportToFile(ctx sdk.Context, _ *codec.Codec, file string) error {
repv2 := rep.getHandlerV2(ctx)
return repv2.ExportToFile(file)
}
// ImportFromFile state of reputation system.
// after update6's code is merged, V2 is the only version that will exist.
func (rep ReputationManager) ImportFromFile(ctx sdk.Context, _ *codec.Codec, file string) error {
handler := rep.getHandlerV2(ctx)
return handler.ImportFromFile(file)
}