Skip to content

Commit

Permalink
applied changes for PR
Browse files Browse the repository at this point in the history
  • Loading branch information
lava17 committed May 23, 2022
1 parent 648f0dd commit 245d322
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 43 deletions.
12 changes: 9 additions & 3 deletions proto/pairing/params.proto
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,13 @@ message Params {
(gogoproto.customtype) = "StakeToMaxCUList",
(gogoproto.nullable) = false
];
uint64 unpayLimit = 10 [(gogoproto.moretags) = "yaml:\"unpay_limit\""];
uint64 slashLimit = 11 [(gogoproto.moretags) = "yaml:\"slash_limit\""];
uint64 limitDivisor = 12 [(gogoproto.moretags) = "yaml:\"limit_divisor\""];
uint64 unpayLimit = 10 [
(gogoproto.moretags) = "yaml:\"unpay_limit\""
];
uint64 slashLimit = 11 [
(gogoproto.moretags) = "yaml:\"slash_limit\""
];
uint64 limitDivisor = 12 [
(gogoproto.moretags) = "yaml:\"limit_divisor\""
];
}
6 changes: 3 additions & 3 deletions x/epochstorage/keeper/stake_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,21 +379,21 @@ func (k Keeper) StoreEpochStakeStorage(ctx sdk.Context, block uint64, storageTyp
func (k Keeper) GetNextEpoch(ctx sdk.Context, epoch uint64) uint64 {
epochBlocks := k.GetEpochBlocks(ctx, epoch)
if epochBlocks == 0 {
panic("epochBlocks == 0")
panic(fmt.Errorf("lava_get_epoch_blocks_error epochBlocks == 0"))
}
epoch += epochBlocks
return epoch
}

func (k Keeper) GetStakeStorageEpoch(ctx sdk.Context, block uint64, storageType string, chainID string) (stakeStorage types.StakeStorage, found bool) {
func (k Keeper) getStakeStorageEpoch(ctx sdk.Context, block uint64, storageType string, chainID string) (stakeStorage types.StakeStorage, found bool) {
key := k.stakeStorageKey(storageType, block, chainID)
return k.GetStakeStorage(ctx, key)
}

// gets chainID, clientAddress, and epoch
// returns epochstoragetypes.StakeEntry which is needed to calculate allowedCU, for the selected epoch
func (k Keeper) GetStakeEntryForClientEpoch(ctx sdk.Context, chainID string, selectedClient sdk.AccAddress, epoch uint64) (entry *types.StakeEntry, err error) {
stakeStorage, found := k.GetStakeStorageEpoch(ctx, epoch, types.ClientKey, chainID)
stakeStorage, found := k.getStakeStorageEpoch(ctx, epoch, types.ClientKey, chainID)
if !found {
return nil, fmt.Errorf("could not find stakeStorage - epoch %d, chainID %s client %s", epoch, chainID, selectedClient.String())
}
Expand Down
11 changes: 5 additions & 6 deletions x/pairing/keeper/client_payment_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,10 @@ func (k Keeper) GetTotalUsedCUForProviderEpoch(ctx sdk.Context, providerAddress
usedCUProviderTotal = 0
usedCUMap, err := k.GetEpochClientProviderUsedCUMap(ctx, userPaymentStorageInEpoch)
if err != nil {
if _, ok := usedCUMap.Providers[providerAddress.String()]; ok {
return usedCUMap.Providers[providerAddress.String()], nil
}
return 0, fmt.Errorf("provider address: %s not in found! (in usedCUMap)", providerAddress)
return 0, err
}
return 0, err

if usedProvider, ok := usedCUMap.Providers[providerAddress.String()]; ok {
return usedProvider, nil
}
return 0, nil
}
61 changes: 33 additions & 28 deletions x/pairing/keeper/limitClient.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (k Keeper) EnforceClientCUsUsageInEpoch(ctx sdk.Context, relay *types.Relay
}
allowedCUProvider := allowedCU / k.ServicersToPairCount(ctx)
if totalCUInEpochForUserProvider > allowedCUProvider {
return k.LimitClientPairingsAndMarkForPenalty(ctx, relay, clientEntry, clientAddr, totalCUInEpochForUserProvider, allowedCU, allowedCUProvider, providerAddr)
return k.LimitClientPairingsAndMarkForPenalty(ctx, clientAddr, relay.ChainID, relay.CuSum, clientEntry.Stake.Amount, relay.BlockHeight, totalCUInEpochForUserProvider, allowedCU, allowedCUProvider, providerAddr)
}

return relay.CuSum, nil
Expand Down Expand Up @@ -99,7 +99,7 @@ func (k Keeper) GetOverusedFromUsedCU(ctx sdk.Context, clientProvidersEpochUsedC
totalOverusedPercent := float64(clientProvidersEpochUsedCUMap.TotalUsed / allowedCU)
if usedCU, exist := clientProvidersEpochUsedCUMap.Providers[providerAddr.String()]; exist {
// TODO: ServicersToPairCount needs epoch !
allowedCUProvider := maxU(0, allowedCU/k.ServicersToPairCount(ctx))
allowedCUProvider := allowedCU / k.ServicersToPairCount(ctx)
overusedCU := maxU(0, usedCU-allowedCUProvider)
overusedProviderPercent = float64(overusedCU / allowedCUProvider)
}
Expand Down Expand Up @@ -146,26 +146,29 @@ func (k Keeper) getOverusedCUPercentageAllEpochs(ctx sdk.Context, chainID string
return clientProviderOverusedPercentMap, nil
}

func (k Keeper) LimitClientPairingsAndMarkForPenalty(ctx sdk.Context, relay *types.RelayRequest, clientEntry *epochstoragetypes.StakeEntry, clientAddr sdk.AccAddress, totalCUInEpochForUserProvider uint64, allowedCU uint64, allowedCUProvider uint64, providerAddr sdk.AccAddress) (amountToPay uint64, err error) {
// func (k Keeper) LimitClientPairingsAndMarkForPenalty(ctx sdk.Context, relay *types.RelayRequest, clientEntry *epochstoragetypes.StakeEntry, clientAddr sdk.AccAddress, totalCUInEpochForUserProvider uint64, allowedCU uint64, allowedCUProvider uint64, providerAddr sdk.AccAddress) (amountToPay uint64, err error) {
func (k Keeper) LimitClientPairingsAndMarkForPenalty(ctx sdk.Context, clientAddr sdk.AccAddress, chainID string, CuSum uint64, clientStakeAmount sdk.Int, BlockHeight int64, totalCUInEpochForUserProvider uint64, allowedCU uint64, allowedCUProvider uint64, providerAddr sdk.AccAddress) (CUToPay uint64, err error) {
eventType := "lava_event"
logger := k.Logger(ctx)
chainID := relay.ChainID
slashLimitPercent, unpayLimitPercent := k.getMaxCULimitsPercentage(ctx)
// clientOverusedCU, err := k.getOverusedCUPercentageAllEpochs(ctx, chainID, sdk.AccAddress(clientEntry.Address), providerAddr)
clientOverusedCU, err := k.getOverusedCUPercentageAllEpochs(ctx, chainID, clientAddr, providerAddr)
if err != nil {
utils.LavaError(ctx, logger, "lava_get_overused_cu", map[string]string{"block": strconv.FormatUint(k.epochStorageKeeper.GetEpochStart(ctx), 10),
"relay.CuSum": strconv.FormatUint(relay.CuSum, 10),
eventType = "lava_get_overused_cu"
utils.LavaError(ctx, logger, eventType, map[string]string{"block": strconv.FormatUint(k.epochStorageKeeper.GetEpochStart(ctx), 10),
"relay.CuSum": strconv.FormatUint(CuSum, 10),
"clientAddr": clientAddr.String(),
"providerAddr": providerAddr.String()},
fmt.Sprintf("user %s, could not calculate overusedCU from memory: %s", clientEntry, clientEntry.Stake.Amount))
return 0, fmt.Errorf("user %s, could not calculate overusedCU from memory: %s", clientEntry, clientEntry.Stake.Amount)
fmt.Sprintf("user %s, could not calculate overusedCU from memory: %s", clientAddr.String(), clientStakeAmount))
return 0, fmt.Errorf("user %s, could not calculate overusedCU from memory: %s", clientAddr.String(), clientStakeAmount)
}
overusedSumTotalPercent := clientOverusedCU.TotalOverusedPercent
overusedSumProviderPercent := clientOverusedCU.OverusedPercentProvider
if overusedSumTotalPercent > slashLimitPercent || overusedSumProviderPercent > slashLimitPercent {
k.SlashUser(ctx, clientEntry.Address)
k.SlashUser(ctx, clientAddr)
eventType = "lava_slash_user"
utils.LogLavaEvent(ctx, logger, "lava_slash_user", map[string]string{"block": strconv.FormatUint(k.epochStorageKeeper.GetEpochStart(ctx), 10),
"relay.CuSum": strconv.FormatUint(relay.CuSum, 10),
"relay.CuSum": strconv.FormatUint(CuSum, 10),
"overusedSumTotalPercent": strconv.FormatFloat(overusedSumTotalPercent, 'f', 6, 64),
"overusedSumProviderPercent": strconv.FormatFloat(overusedSumProviderPercent, 'f', 6, 64),
"slashLimitPercent": strconv.FormatFloat(slashLimitPercent, 'f', 6, 64)},
Expand All @@ -175,38 +178,40 @@ func (k Keeper) LimitClientPairingsAndMarkForPenalty(ctx sdk.Context, relay *typ
if overusedSumTotalPercent < unpayLimitPercent && overusedSumProviderPercent < unpayLimitPercent {
// overuse is under the limit - will allow provider to get payment
// ? maybe needs to pay the allowedCU but not pay for overuse ?
utils.LogLavaEvent(ctx, logger, "lava_slash_user", map[string]string{"block": strconv.FormatUint(k.epochStorageKeeper.GetEpochStart(ctx), 10),
"relay.CuSum": strconv.FormatUint(relay.CuSum, 10),
eventType = "lava_client_overused"
utils.LogLavaEvent(ctx, logger, eventType, map[string]string{"block": strconv.FormatUint(k.epochStorageKeeper.GetEpochStart(ctx), 10),
"relay.CuSum": strconv.FormatUint(CuSum, 10),
"overusedSumTotalPercent": strconv.FormatFloat(overusedSumTotalPercent, 'f', 6, 64),
"overusedSumProviderPercent": strconv.FormatFloat(overusedSumProviderPercent, 'f', 6, 64),
"unpayLimitPercent": strconv.FormatFloat(unpayLimitPercent, 'f', 6, 64)},
"overuse is under the unpayLimit - paying provider")
return relay.CuSum, nil
return CuSum, nil
}
// overused over the unpayLimit (under slashLimit) - paying provider upto the unpayLimit
utils.LogLavaEvent(ctx, logger, "lava_slash_user", map[string]string{"block": strconv.FormatUint(k.epochStorageKeeper.GetEpochStart(ctx), 10),
"relay.CuSum": strconv.FormatUint(relay.CuSum, 10),
"overusedSumTotalPercent": strconv.FormatFloat(overusedSumTotalPercent, 'f', 6, 64),
"overusedSumProviderPercent": strconv.FormatFloat(overusedSumProviderPercent, 'f', 6, 64),
"unpayLimitPercent": strconv.FormatFloat(unpayLimitPercent, 'f', 6, 64)},
"overuse is above the unpayLimit - paying provider only ")

// #o please advise if this is the correct way to get the epoch
// #o and did you mean the payment's epoch or the latest epoch # please advise on correct way to get latestEpcoh (future - lctx.LatestEpoch)
epoch := k.epochStorageKeeper.GetEpochBlocks(ctx, uint64(relay.BlockHeight))
epoch := uint64(ctx.BlockHeight())
clientUsedEpoch, usedCUerr := k.GetEpochClientUsedCUMap(ctx, chainID, epoch, clientAddr)
if usedCUerr != nil || clientUsedEpoch.TotalUsed == 0 {
eventType = "lava_GetEpochClientUsedCUMap"
utils.LavaError(ctx, logger, "lava_GetEpochClientUsedCUMap", map[string]string{"block": strconv.FormatUint(k.epochStorageKeeper.GetEpochStart(ctx), 10),
"relay.CuSum": strconv.FormatUint(relay.CuSum, 10),
"relay.CuSum": strconv.FormatUint(CuSum, 10),
"clientAddr": clientAddr.String(),
"providerAddr": providerAddr.String()},
fmt.Sprintf("clientUsedEpoch.TotalUsed == %d - no payments for client %s found in blockHeight %d chainID %s",
clientUsedEpoch.TotalUsed, clientAddr, relay.BlockHeight, chainID))
clientUsedEpoch.TotalUsed, clientAddr, BlockHeight, chainID))
panic("we just added an epochPayment, so the totalUsed for this epoch can't be 0")
}
return uint64(math.Floor(float64(allowedCU)*1.1)) - clientUsedEpoch.TotalUsed + relay.CuSum, nil
eventType = "lava_client_overused_unpay"
// overused over the unpayLimit (under slashLimit) - paying provider upto the unpayLimit
utils.LogLavaEvent(ctx, logger, eventType, map[string]string{"block": strconv.FormatUint(k.epochStorageKeeper.GetEpochStart(ctx), 10),
"relay.CuSum": strconv.FormatUint(CuSum, 10),
"overusedSumTotalPercent": strconv.FormatFloat(overusedSumTotalPercent, 'f', 6, 64),
"overusedSumProviderPercent": strconv.FormatFloat(overusedSumProviderPercent, 'f', 6, 64),
"unpayLimitPercent": strconv.FormatFloat(unpayLimitPercent, 'f', 6, 64)},
"overuse is above the unpayLimit - paying provider upto the unpayLimit ")

return uint64(math.Floor(float64(allowedCU)*1.1)) - clientUsedEpoch.TotalUsed + CuSum, nil
}

func (k Keeper) SlashUser(ctx sdk.Context, clientAddr string) {
func (k Keeper) SlashUser(ctx sdk.Context, clientAddr sdk.AccAddress) {
//TODO: jail user, and count problems
}

Expand Down
10 changes: 7 additions & 3 deletions x/pairing/keeper/msg_server_relay_payment.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (k msgServer) RelayPayment(goCtx context.Context, msg *types.MsgRelayPaymen
"error": err.Error(), "unique_ID": strconv.FormatUint(relay.SessionId, 16)}
return errorLogAndFormat("relay_proof_claim", details, "double spending detected")
}
ammountToPay, err := k.Keeper.EnforceClientCUsUsageInEpoch(ctx, relay, userStake, clientAddr, totalCUInEpochForUserProvider, providerAddr)
cuToPay, err := k.Keeper.EnforceClientCUsUsageInEpoch(ctx, relay, userStake, clientAddr, totalCUInEpochForUserProvider, providerAddr)
if err != nil {
//TODO: maybe give provider money but burn user, colluding?
//TODO: display correct totalCU and usedCU for provider
Expand All @@ -80,14 +80,18 @@ func (k msgServer) RelayPayment(goCtx context.Context, msg *types.MsgRelayPaymen
"provider": providerAddr.String(),
"error": err.Error(),
"CU": strconv.FormatUint(relay.CuSum, 10),
"ammountToPay": strconv.FormatUint(ammountToPay, 10),
"cuToPay": strconv.FormatUint(cuToPay, 10),
"totalCUInEpochForUserProvider": strconv.FormatUint(totalCUInEpochForUserProvider, 10)}
return errorLogAndFormat("relay_proof_user_limit", details, "user bypassed CU limit")
}
if cuToPay > relay.CuSum {
// [?] what sould happen ? event, error, or panic ?
// event + error ?
}
//
if isValidPairing {
//pairing is valid, we can pay provider for work
reward := k.Keeper.MintCoinsPerCU(ctx).MulInt64(int64(ammountToPay))
reward := k.Keeper.MintCoinsPerCU(ctx).MulInt64(int64(cuToPay))
rewardCoins := sdk.Coins{sdk.Coin{Denom: "stake", Amount: reward.TruncateInt()}}
if reward.IsZero() {
continue
Expand Down

0 comments on commit 245d322

Please sign in to comment.