Skip to content

Commit

Permalink
CNS-391 jailing limiting needs to be per geolocation (#449)
Browse files Browse the repository at this point in the history
* now unresponsiveness takes into account geolocatio

* add tests

---------

Co-authored-by: Yarom Swisa <yarom@lavanet.xyz git config --global user.name Yarom>
  • Loading branch information
Yaroms and Yarom Swisa authored Apr 30, 2023
1 parent 21aa097 commit bf83724
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 deletions.
8 changes: 6 additions & 2 deletions x/pairing/keeper/msg_server_relay_payment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ func (ts *testStruct) addClient(amount int) error {
}

func (ts *testStruct) addProvider(amount int) error {
return ts.addProviderGeolocation(amount, 1)
}

func (ts *testStruct) addProviderGeolocation(amount int, geolocation uint64) error {
for i := 0; i < amount; i++ {
sk, address := sigs.GenerateFloatingKey()
ts.providers = append(ts.providers, &common.Account{SK: sk, Addr: address})
Expand All @@ -70,8 +74,8 @@ func (ts *testStruct) addProvider(amount int) error {
return err
}
endpoints := []epochstoragetypes.Endpoint{}
endpoints = append(endpoints, epochstoragetypes.Endpoint{IPPORT: "123", UseType: ts.spec.GetApis()[0].ApiInterfaces[0].Interface, Geolocation: 1})
_, err = ts.servers.PairingServer.StakeProvider(ts.ctx, &types.MsgStakeProvider{Creator: address.String(), ChainID: ts.spec.Name, Amount: sdk.NewCoin(epochstoragetypes.TokenDenom, sdk.NewInt(stake)), Geolocation: 1, Endpoints: endpoints})
endpoints = append(endpoints, epochstoragetypes.Endpoint{IPPORT: "123", UseType: ts.spec.GetApis()[0].ApiInterfaces[0].Interface, Geolocation: geolocation})
_, err = ts.servers.PairingServer.StakeProvider(ts.ctx, &types.MsgStakeProvider{Creator: address.String(), ChainID: ts.spec.Name, Amount: sdk.NewCoin(epochstoragetypes.TokenDenom, sdk.NewInt(stake)), Geolocation: geolocation, Endpoints: endpoints})
if err != nil {
return err
}
Expand Down
15 changes: 8 additions & 7 deletions x/pairing/keeper/unresponsive_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,13 @@ func (k Keeper) UnstakeUnresponsiveProviders(ctx sdk.Context, epochsNumToCheckCU
// Go over the staked provider entries (on all chains)
for _, providerStakeStorage := range providerStakeStorageList {
providerStakeEntriesForChain := providerStakeStorage.GetStakeEntries()
existingProviders := uint64(len(providerStakeEntriesForChain))
existingProviders := map[uint64]uint64{}
// count providers per geolocation
for _, providerStakeEntry := range providerStakeEntriesForChain {
existingProviders[providerStakeEntry.Geolocation]++
}

for _, providerStakeEntry := range providerStakeEntriesForChain {
if existingProviders <= minimumProvidersCount {
// not enough providers, skip jailing any more providers
break
}
if minHistoryBlock < providerStakeEntry.StakeAppliedBlock {
// this staked provider has too short history (either since staking
// or since it was last unfrozen) - do not consider for jailing
Expand All @@ -79,9 +80,9 @@ func (k Keeper) UnstakeUnresponsiveProviders(ctx sdk.Context, epochsNumToCheckCU
}

// providerPaymentStorageKeyList is not empty -> provider should be punished
if len(providerPaymentStorageKeyList) != 0 {
if len(providerPaymentStorageKeyList) != 0 && existingProviders[providerStakeEntry.Geolocation] > minimumProvidersCount {
err = k.punishUnresponsiveProvider(ctx, minPaymentBlock, providerPaymentStorageKeyList, providerStakeEntry.GetAddress(), providerStakeEntry.GetChain())
existingProviders--
existingProviders[providerStakeEntry.Geolocation]--
if err != nil {
return utils.LavaError(ctx, k.Logger(ctx), "punish_unresponsive_provider", map[string]string{"err": err.Error()}, "couldn't punish unresponsive provider")
}
Expand Down
2 changes: 2 additions & 0 deletions x/pairing/keeper/unresponsive_provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,8 @@ func TestNotUnstakingProviderForUnresponsivenessWithMinProviders(t *testing.T) {
testClientAmount := 1
testProviderAmount := 2
ts := setupClientsAndProvidersForUnresponsiveness(t, testClientAmount, testProviderAmount)
err := ts.addProviderGeolocation(2, 2)
require.Nil(t, err)

// get recommendedEpochNumToCollectPayment
recommendedEpochNumToCollectPayment := ts.keepers.Pairing.RecommendedEpochNumToCollectPayment(sdk.UnwrapSDKContext(ts.ctx))
Expand Down

0 comments on commit bf83724

Please sign in to comment.