github.com/KiraCore/sekai@v0.3.43/x/layer2/keeper/dapp_operator.go (about)

     1  package keeper
     2  
     3  import (
     4  	"github.com/KiraCore/sekai/x/layer2/types"
     5  	sdk "github.com/cosmos/cosmos-sdk/types"
     6  )
     7  
     8  func (k Keeper) SetDappOperator(ctx sdk.Context, operator types.DappOperator) {
     9  	bz := k.cdc.MustMarshal(&operator)
    10  	store := ctx.KVStore(k.storeKey)
    11  	store.Set(types.DappOperatorKey(operator.DappName, operator.Operator), bz)
    12  }
    13  
    14  func (k Keeper) DeleteDappOperator(ctx sdk.Context, name, address string) {
    15  	store := ctx.KVStore(k.storeKey)
    16  	store.Delete(types.DappOperatorKey(name, address))
    17  }
    18  
    19  func (k Keeper) GetDappOperator(ctx sdk.Context, name string, user string) types.DappOperator {
    20  	store := ctx.KVStore(k.storeKey)
    21  	bz := store.Get(types.DappOperatorKey(name, user))
    22  	if bz == nil {
    23  		return types.DappOperator{}
    24  	}
    25  
    26  	operator := types.DappOperator{}
    27  	k.cdc.MustUnmarshal(bz, &operator)
    28  	return operator
    29  }
    30  
    31  func (k Keeper) GetDappOperators(ctx sdk.Context, name string) []types.DappOperator {
    32  	store := ctx.KVStore(k.storeKey)
    33  
    34  	operators := []types.DappOperator{}
    35  	it := sdk.KVStorePrefixIterator(store, append([]byte(types.PrefixDappOperatorKey), name...))
    36  	defer it.Close()
    37  
    38  	for ; it.Valid(); it.Next() {
    39  		operator := types.DappOperator{}
    40  		k.cdc.MustUnmarshal(it.Value(), &operator)
    41  		operators = append(operators, operator)
    42  	}
    43  	return operators
    44  }
    45  
    46  func (k Keeper) GetDappExecutors(ctx sdk.Context, name string) []types.DappOperator {
    47  	operators := k.GetDappOperators(ctx, name)
    48  	executors := []types.DappOperator{}
    49  	for _, operator := range operators {
    50  		if operator.Executor {
    51  			executors = append(executors, operator)
    52  		}
    53  	}
    54  	return executors
    55  }
    56  
    57  func (k Keeper) GetDappVerifiers(ctx sdk.Context, name string) []types.DappOperator {
    58  	operators := k.GetDappOperators(ctx, name)
    59  	verifiers := []types.DappOperator{}
    60  	for _, operator := range operators {
    61  		if operator.Verifier {
    62  			verifiers = append(verifiers, operator)
    63  		}
    64  	}
    65  	return verifiers
    66  }
    67  
    68  func (k Keeper) GetAllDappOperators(ctx sdk.Context) []types.DappOperator {
    69  	store := ctx.KVStore(k.storeKey)
    70  
    71  	operators := []types.DappOperator{}
    72  	it := sdk.KVStorePrefixIterator(store, []byte(types.PrefixDappOperatorKey))
    73  	defer it.Close()
    74  
    75  	for ; it.Valid(); it.Next() {
    76  		operator := types.DappOperator{}
    77  		k.cdc.MustUnmarshal(it.Value(), &operator)
    78  		operators = append(operators, operator)
    79  	}
    80  	return operators
    81  }
    82  
    83  func (k Keeper) ExecuteJoinDappProposal(ctx sdk.Context, p *types.ProposalJoinDapp) error {
    84  	dapp := k.GetDapp(ctx, p.DappName)
    85  	if dapp.Name == "" {
    86  		return types.ErrDappDoesNotExist
    87  	}
    88  
    89  	if p.Executor {
    90  		// ensure executor is a validator
    91  		addr, err := sdk.AccAddressFromBech32(p.Sender)
    92  		if err != nil {
    93  			return err
    94  		}
    95  		_, err = k.sk.GetValidator(ctx, sdk.ValAddress(addr))
    96  		if err != nil {
    97  			return err
    98  		}
    99  		executors := k.GetDappExecutors(ctx, p.DappName)
   100  		if len(executors) >= int(dapp.ExecutorsMax) {
   101  			return types.ErrNumberOfOperatorsExceedsExecutorsMax
   102  		}
   103  	}
   104  
   105  	k.SetDappOperator(ctx, types.DappOperator{
   106  		DappName:       p.DappName,
   107  		Operator:       p.Sender,
   108  		Executor:       p.Executor,
   109  		Verifier:       p.Verifier,
   110  		Interx:         p.Interx,
   111  		Status:         types.OperatorActive,
   112  		BondedLpAmount: sdk.ZeroInt(),
   113  	})
   114  
   115  	// when executors_min reaches, session is created
   116  	executors := k.GetDappExecutors(ctx, p.DappName)
   117  	verifiers := k.GetDappVerifiers(ctx, p.DappName)
   118  	if len(executors) >= int(dapp.ExecutorsMin) && len(executors) >= 1 && len(verifiers) >= int(dapp.VerifiersMin) {
   119  		session := k.GetDappSession(ctx, p.DappName)
   120  		if session.DappName == "" {
   121  			k.CreateNewSession(ctx, p.DappName, "")
   122  		}
   123  	}
   124  
   125  	return nil
   126  }
   127  
   128  func (k Keeper) HandleSessionParticipation(ctx sdk.Context, operator types.DappOperator, participated bool) {
   129  	properties := k.gk.GetNetworkProperties(ctx)
   130  	if participated {
   131  		// If the dApp operator participates in the production of a dApp session (sends session or verification tx) his
   132  		// `rank` and `streak` must be increased by `1` while `mischance` re-set to 0,
   133  
   134  		operator.Rank += 1
   135  		operator.Streak += 1
   136  		operator.VerifiedSessions += 1
   137  		operator.Mischance = 0
   138  		k.SetDappOperator(ctx, operator)
   139  	} else if operator.Status == types.OperatorActive {
   140  		// otherwise in the case of failure to participate the `mischance` counter must be increased by `1`,
   141  		// the `streak` re-set to `0` and `rank` decreased by `dAppMischanceRankDecreaseAmount`.
   142  		operator.Rank -= int64(properties.DappMischanceRankDecreaseAmount)
   143  		if operator.Rank < 0 {
   144  			operator.Rank = 0
   145  		}
   146  		operator.Streak = 0
   147  		operator.Mischance += 1
   148  		operator.MissedSessions += 1
   149  
   150  		// If the dApp operator does not enable maintenance mode by using the `pause-dapp-tx` and the `mischance` counter
   151  		// exceeds `dAppMaxMischance` then his ranks should be slashed by `dAppInactiveRankDecreasePercent`.
   152  		if operator.Mischance > int64(properties.DappMaxMischance) {
   153  			operator.Rank = operator.Rank * (100 - int64(properties.AbstentionRankDecreaseAmount)) / 100
   154  		}
   155  		k.SetDappOperator(ctx, operator)
   156  	}
   157  }