github.com/lino-network/lino@v0.6.11/x/reputation/manager.go (about)

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