Skip to content

Commit

Permalink
Merge pull request lino-network#278 from lino-network/yumin/reputatio…
Browse files Browse the repository at this point in the history
…n-system

LNO-150, Add reputation system
  • Loading branch information
xiaoxiaff authored Sep 16, 2018
2 parents b33b2d8 + bcbb49e commit 63d5e58
Show file tree
Hide file tree
Showing 32 changed files with 2,280 additions and 677 deletions.
82 changes: 45 additions & 37 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
accModel "github.com/lino-network/lino/x/account/model"
developer "github.com/lino-network/lino/x/developer"
infra "github.com/lino-network/lino/x/infra"
rep "github.com/lino-network/lino/x/reputation"
val "github.com/lino-network/lino/x/validator"
vote "github.com/lino-network/lino/x/vote"

Expand Down Expand Up @@ -49,27 +50,29 @@ type LinoBlockchain struct {
cdc *wire.Codec

// keys to access the KVStore
CapKeyMainStore *sdk.KVStoreKey
CapKeyAccountStore *sdk.KVStoreKey
CapKeyPostStore *sdk.KVStoreKey
CapKeyValStore *sdk.KVStoreKey
CapKeyVoteStore *sdk.KVStoreKey
CapKeyInfraStore *sdk.KVStoreKey
CapKeyDeveloperStore *sdk.KVStoreKey
CapKeyIBCStore *sdk.KVStoreKey
CapKeyGlobalStore *sdk.KVStoreKey
CapKeyParamStore *sdk.KVStoreKey
CapKeyProposalStore *sdk.KVStoreKey
CapKeyMainStore *sdk.KVStoreKey
CapKeyAccountStore *sdk.KVStoreKey
CapKeyPostStore *sdk.KVStoreKey
CapKeyValStore *sdk.KVStoreKey
CapKeyVoteStore *sdk.KVStoreKey
CapKeyInfraStore *sdk.KVStoreKey
CapKeyDeveloperStore *sdk.KVStoreKey
CapKeyIBCStore *sdk.KVStoreKey
CapKeyGlobalStore *sdk.KVStoreKey
CapKeyParamStore *sdk.KVStoreKey
CapKeyProposalStore *sdk.KVStoreKey
CapKeyReputationStore *sdk.KVStoreKey

// manager for different KVStore
accountManager acc.AccountManager
postManager post.PostManager
valManager val.ValidatorManager
globalManager global.GlobalManager
voteManager vote.VoteManager
infraManager infra.InfraManager
developerManager developer.DeveloperManager
proposalManager proposal.ProposalManager
accountManager acc.AccountManager
postManager post.PostManager
valManager val.ValidatorManager
globalManager global.GlobalManager
voteManager vote.VoteManager
infraManager infra.InfraManager
developerManager developer.DeveloperManager
proposalManager proposal.ProposalManager
reputationManager rep.ReputationManager

// global param
paramHolder param.ParamHolder
Expand All @@ -83,18 +86,19 @@ func NewLinoBlockchain(
bApp := bam.NewBaseApp(appName, logger, db, DefaultTxDecoder(cdc), baseAppOptions...)
bApp.SetCommitMultiStoreTracer(traceStore)
var lb = &LinoBlockchain{
BaseApp: bApp,
cdc: cdc,
CapKeyMainStore: sdk.NewKVStoreKey(types.MainKVStoreKey),
CapKeyAccountStore: sdk.NewKVStoreKey(types.AccountKVStoreKey),
CapKeyPostStore: sdk.NewKVStoreKey(types.PostKVStoreKey),
CapKeyValStore: sdk.NewKVStoreKey(types.ValidatorKVStoreKey),
CapKeyVoteStore: sdk.NewKVStoreKey(types.VoteKVStoreKey),
CapKeyInfraStore: sdk.NewKVStoreKey(types.InfraKVStoreKey),
CapKeyDeveloperStore: sdk.NewKVStoreKey(types.DeveloperKVStoreKey),
CapKeyGlobalStore: sdk.NewKVStoreKey(types.GlobalKVStoreKey),
CapKeyParamStore: sdk.NewKVStoreKey(types.ParamKVStoreKey),
CapKeyProposalStore: sdk.NewKVStoreKey(types.ProposalKVStoreKey),
BaseApp: bApp,
cdc: cdc,
CapKeyMainStore: sdk.NewKVStoreKey(types.MainKVStoreKey),
CapKeyAccountStore: sdk.NewKVStoreKey(types.AccountKVStoreKey),
CapKeyPostStore: sdk.NewKVStoreKey(types.PostKVStoreKey),
CapKeyValStore: sdk.NewKVStoreKey(types.ValidatorKVStoreKey),
CapKeyVoteStore: sdk.NewKVStoreKey(types.VoteKVStoreKey),
CapKeyInfraStore: sdk.NewKVStoreKey(types.InfraKVStoreKey),
CapKeyDeveloperStore: sdk.NewKVStoreKey(types.DeveloperKVStoreKey),
CapKeyGlobalStore: sdk.NewKVStoreKey(types.GlobalKVStoreKey),
CapKeyParamStore: sdk.NewKVStoreKey(types.ParamKVStoreKey),
CapKeyProposalStore: sdk.NewKVStoreKey(types.ProposalKVStoreKey),
CapKeyReputationStore: sdk.NewKVStoreKey(types.ReputationKVStoreKey),
}
lb.paramHolder = param.NewParamHolder(lb.CapKeyParamStore)
lb.accountManager = acc.NewAccountManager(lb.CapKeyAccountStore, lb.paramHolder)
Expand All @@ -103,6 +107,7 @@ func NewLinoBlockchain(
lb.globalManager = global.NewGlobalManager(lb.CapKeyGlobalStore, lb.paramHolder)
registerEvent(lb.globalManager.WireCodec())

lb.reputationManager = rep.NewReputationManager(lb.CapKeyReputationStore, lb.paramHolder)
lb.voteManager = vote.NewVoteManager(lb.CapKeyVoteStore, lb.paramHolder)
lb.infraManager = infra.NewInfraManager(lb.CapKeyInfraStore, lb.paramHolder)
lb.developerManager = developer.NewDeveloperManager(lb.CapKeyDeveloperStore, lb.paramHolder)
Expand All @@ -111,8 +116,9 @@ func NewLinoBlockchain(
lb.Router().
AddRoute(types.AccountRouterName, acc.NewHandler(lb.accountManager, lb.globalManager)).
AddRoute(types.PostRouterName, post.NewHandler(
lb.postManager, lb.accountManager, lb.globalManager, lb.developerManager)).
AddRoute(types.VoteRouterName, vote.NewHandler(lb.voteManager, lb.accountManager, lb.globalManager)).
lb.postManager, lb.accountManager, lb.globalManager, lb.developerManager, lb.reputationManager)).
AddRoute(types.VoteRouterName, vote.NewHandler(
lb.voteManager, lb.accountManager, lb.globalManager, lb.reputationManager)).
AddRoute(types.DeveloperRouterName, developer.NewHandler(
lb.developerManager, lb.accountManager, lb.globalManager)).
AddRoute(types.ProposalRouterName, proposal.NewHandler(
Expand All @@ -131,7 +137,7 @@ func NewLinoBlockchain(
lb.MountStoresIAVL(
lb.CapKeyMainStore, lb.CapKeyAccountStore, lb.CapKeyPostStore, lb.CapKeyValStore,
lb.CapKeyVoteStore, lb.CapKeyInfraStore, lb.CapKeyDeveloperStore, lb.CapKeyGlobalStore,
lb.CapKeyParamStore, lb.CapKeyProposalStore)
lb.CapKeyParamStore, lb.CapKeyProposalStore, lb.CapKeyReputationStore)
if err := lb.LoadLatestVersion(lb.CapKeyMainStore); err != nil {
cmn.Exit(err.Error())
}
Expand Down Expand Up @@ -220,7 +226,8 @@ func (lb *LinoBlockchain) initChainer(ctx sdk.Context, req abci.RequestInitChain
genesisState.GenesisParam.ProposalParam,
genesisState.GenesisParam.CoinDayParam,
genesisState.GenesisParam.BandwidthParam,
genesisState.GenesisParam.AccountParam); err != nil {
genesisState.GenesisParam.AccountParam,
genesisState.GenesisParam.ReputationParam); err != nil {
panic(err)
}
} else {
Expand Down Expand Up @@ -417,7 +424,7 @@ func (lb *LinoBlockchain) executeEvents(ctx sdk.Context, eventList []types.Event
case post.RewardEvent:
if err := e.Execute(
ctx, lb.postManager, lb.accountManager, lb.globalManager,
lb.developerManager, lb.voteManager); err != nil {
lb.developerManager, lb.voteManager, lb.reputationManager); err != nil {
panic(err)
}
case acc.ReturnCoinEvent:
Expand All @@ -439,12 +446,13 @@ func (lb *LinoBlockchain) executeEvents(ctx sdk.Context, eventList []types.Event
return nil
}

// udpate validator set
// udpate validator set and renew reputation round
func (lb *LinoBlockchain) endBlocker(ctx sdk.Context, req abci.RequestEndBlock) abci.ResponseEndBlock {
ABCIValList, err := lb.valManager.GetUpdateValidatorList(ctx)
if err != nil {
panic(err)
}
rep.EndBlocker(ctx, req, lb.reputationManager)

return abci.ResponseEndBlock{ValidatorUpdates: ABCIValList}
}
Expand Down
4 changes: 4 additions & 0 deletions app/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,10 @@ func TestGenesisFromConfig(t *testing.T) {
param.PostParam{
ReportOrUpvoteIntervalSec: 24 * 3600,
PostIntervalSec: 600,
MaxReportReputation: types.NewCoinFromInt64(100 * types.Decimals),
},
param.ReputationParam{
BestContentIndexN: 10,
},
}
genesisState.InitGlobalMeta = globalModel.InitParamList{
Expand Down
5 changes: 5 additions & 0 deletions app/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ type GenesisParam struct {
param.BandwidthParam
param.AccountParam
param.PostParam
param.ReputationParam
}

// LinoBlockchainGenTx - init genesis account
Expand Down Expand Up @@ -221,6 +222,10 @@ 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,
},
},
InitGlobalMeta: globalModel.InitParamList{
Expand Down
4 changes: 4 additions & 0 deletions app/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ func TestGetGenesisJson(t *testing.T) {
param.PostParam{
ReportOrUpvoteIntervalSec: 24 * 3600,
PostIntervalSec: 600,
MaxReportReputation: types.NewCoinFromInt64(100 * types.Decimals),
},
param.ReputationParam{
BestContentIndexN: 10,
},
},
InitGlobalMeta: globalModel.InitParamList{
Expand Down
15 changes: 15 additions & 0 deletions param/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ func ErrEvaluateOfContentValueParamNotFound() sdk.Error {
return types.NewError(types.CodeEvaluateOfContentValueParamNotFound, fmt.Sprintf("evaluate of content value param not found"))
}

// ErrReputationParamNotFound - error when reputation param is empty.
func ErrReputationParamNotFound() sdk.Error {
return types.NewError(types.CodeReputationParamNotFound, fmt.Sprintf("reputation param not found"))
}

// ErrFailedToUnmarshalGlobalAllocationParam - error when unmarshal global allocation param failed.
func ErrFailedToUnmarshalGlobalAllocationParam(err error) sdk.Error {
return types.NewError(types.CodeFailedToUnmarshalGlobalAllocationParam, fmt.Sprintf("failed to unmarshal global allocation param: %s", err.Error()))
Expand Down Expand Up @@ -128,6 +133,11 @@ func ErrFailedToUnmarshalAccountParam(err error) sdk.Error {
return types.NewError(types.CodeFailedToUnmarshalAccountParam, fmt.Sprintf("failed to unmarshal account param: %s", err.Error()))
}

// ErrFailedToUnmarshalReputationParam - error when unmarshal reputation param failed.
func ErrFailedToUnmarshalReputationParam(err error) sdk.Error {
return types.NewError(types.CodeFailedToUnmarshalReputationParam, fmt.Sprintf("failed to unmarshal account param: %s", err.Error()))
}

// ErrFailedToUnmarshalAccountParam - error when marshal global allocation param failed.
func ErrFailedToMarshalGlobalAllocationParam(err error) sdk.Error {
return types.NewError(types.CodeFailedToMarshalGlobalAllocationParam, fmt.Sprintf("failed to marshal global allocation param: %s", err.Error()))
Expand Down Expand Up @@ -182,3 +192,8 @@ func ErrFailedToMarshalBandwidthParam(err error) sdk.Error {
func ErrFailedToMarshalAccountParam(err error) sdk.Error {
return types.NewError(types.CodeFailedToMarshalAccountParam, fmt.Sprintf("failed to marshal account param: %s", err.Error()))
}

// ErrFailedToMarshalReputationParam - error when marshal reputation failed.
func ErrFailedToMarshalReputationParam(err error) sdk.Error {
return types.NewError(types.CodeFailedToMarshalReputationParam, fmt.Sprintf("failed to marshal reputation param: %s", err.Error()))
}
45 changes: 44 additions & 1 deletion param/holder.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ var (
bandwidthParamSubStore = []byte{0x08} // Substore for bandwidth param
accountParamSubstore = []byte{0x09} // Substore for account param
postParamSubStore = []byte{0x0a} // Substore for evaluate of content value
reputationParamSubStore = []byte{0x0b} // Substore for reputation parameters

// AnnualInflationCeiling - annual inflation upper bound
AnnualInflationCeiling = sdk.NewRat(98, 1000)
Expand Down Expand Up @@ -67,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 Expand Up @@ -167,6 +169,13 @@ func (ph ParamHolder) InitParam(ctx sdk.Context) error {
return err
}

reputationParam := &ReputationParam{
BestContentIndexN: 10,
}
if err := ph.setReputationParam(ctx, reputationParam); err != nil {
return err
}

return nil
}

Expand All @@ -183,7 +192,8 @@ func (ph ParamHolder) InitParamFromConfig(
proposalParam ProposalParam,
coinDayParam CoinDayParam,
bandwidthParam BandwidthParam,
accParam AccountParam) error {
accParam AccountParam,
repParam ReputationParam) error {
if err := ph.setGlobalAllocationParam(ctx, &globalParam); err != nil {
return err
}
Expand Down Expand Up @@ -225,6 +235,10 @@ func (ph ParamHolder) InitParamFromConfig(
return err
}

if err := ph.setReputationParam(ctx, &repParam); err != nil {
return err
}

return nil
}

Expand Down Expand Up @@ -385,6 +399,20 @@ func (ph ParamHolder) GetAccountParam(ctx sdk.Context) (*AccountParam, sdk.Error
return param, nil
}

// GetReputationParam - get reputation param
func (ph ParamHolder) GetReputationParam(ctx sdk.Context) (*ReputationParam, sdk.Error) {
store := ctx.KVStore(ph.key)
paramBytes := store.Get(GetReputationParamKey())
if paramBytes == nil {
return nil, ErrReputationParamNotFound()
}
param := new(ReputationParam)
if err := ph.cdc.UnmarshalJSON(paramBytes, param); err != nil {
return nil, ErrFailedToUnmarshalReputationParam(err)
}
return param, nil
}

// UpdateGlobalGrowthRate - update global growth rate
func (ph ParamHolder) UpdateGlobalGrowthRate(ctx sdk.Context, growthRate sdk.Rat) sdk.Error {
store := ctx.KVStore(ph.key)
Expand Down Expand Up @@ -525,6 +553,16 @@ func (ph ParamHolder) setAccountParam(ctx sdk.Context, param *AccountParam) sdk.
return nil
}

func (ph ParamHolder) setReputationParam(ctx sdk.Context, param *ReputationParam) sdk.Error {
store := ctx.KVStore(ph.key)
reputationBytes, err := ph.cdc.MarshalJSON(*param)
if err != nil {
return ErrFailedToMarshalReputationParam(err)
}
store.Set(GetReputationParamKey(), reputationBytes)
return nil
}

// GetPostParamKey - "post param substore"
func GetPostParamKey() []byte {
return postParamSubStore
Expand Down Expand Up @@ -579,3 +617,8 @@ func GetBandwidthParamKey() []byte {
func GetAccountParamKey() []byte {
return accountParamSubstore
}

// GetAccountParamKey - "account param substore"
func GetReputationParamKey() []byte {
return reputationParamSubStore
}
6 changes: 6 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,10 @@ func TestInitParamFromConfig(t *testing.T) {
postParam := PostParam{
ReportOrUpvoteIntervalSec: int64(24 * 3600),
PostIntervalSec: int64(600),
MaxReportReputation: types.NewCoinFromInt64(100 * types.Decimals),
}
repParam := ReputationParam{
BestContentIndexN: 10,
}

err := ph.InitParamFromConfig(
Expand All @@ -405,6 +410,7 @@ func TestInitParamFromConfig(t *testing.T) {
coinDayParam,
bandwidthParam,
accountParam,
repParam,
)
assert.Nil(t, err)

Expand Down
10 changes: 8 additions & 2 deletions param/param.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,12 @@ 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.
type ReputationParam struct {
BestContentIndexN int `json:"best_content_index_n"`
}
21 changes: 11 additions & 10 deletions types/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@ const (
Decimals = 100000

// KVStoreKey presents store which used by app
MainKVStoreKey = "main"
AccountKVStoreKey = "account"
PostKVStoreKey = "post"
ValidatorKVStoreKey = "validator"
GlobalKVStoreKey = "global"
VoteKVStoreKey = "vote"
InfraKVStoreKey = "infra"
DeveloperKVStoreKey = "developer"
ParamKVStoreKey = "param"
ProposalKVStoreKey = "proposal"
MainKVStoreKey = "main"
AccountKVStoreKey = "account"
PostKVStoreKey = "post"
ValidatorKVStoreKey = "validator"
GlobalKVStoreKey = "global"
VoteKVStoreKey = "vote"
InfraKVStoreKey = "infra"
DeveloperKVStoreKey = "developer"
ParamKVStoreKey = "param"
ProposalKVStoreKey = "proposal"
ReputationKVStoreKey = "reputation"

// RouterName for msg routing in app
AccountRouterName = "account"
Expand Down
Loading

0 comments on commit 63d5e58

Please sign in to comment.