forked from lavanet/lava
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request lavanet#531 from lavanet/CNS-213-pairing-provider-…
…selection-implementation CNS-213 pairing provider selection implementation
- Loading branch information
Showing
24 changed files
with
1,013 additions
and
155 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package filters | ||
|
||
import ( | ||
"fmt" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/lavanet/lava/utils" | ||
epochstoragetypes "github.com/lavanet/lava/x/epochstorage/types" | ||
projectstypes "github.com/lavanet/lava/x/projects/types" | ||
) | ||
|
||
type Filter interface { | ||
Filter(ctx sdk.Context, providers []epochstoragetypes.StakeEntry, currentEpoch uint64) []bool | ||
InitFilter(strictestPolicy projectstypes.Policy) bool // return if filter is usable (by the policy) | ||
} | ||
|
||
func GetAllFilters() []Filter { | ||
var selectedProvidersFilter SelectedProvidersFilter | ||
var frozenProvidersFilter FrozenProvidersFilter | ||
var geolocationFilter GeolocationFilter | ||
|
||
filters := []Filter{&selectedProvidersFilter, &frozenProvidersFilter, &geolocationFilter} | ||
return filters | ||
} | ||
|
||
func initFilters(filters []Filter, strictestPolicy projectstypes.Policy) []Filter { | ||
activeFilters := []Filter{} | ||
|
||
for _, filter := range filters { | ||
active := filter.InitFilter(strictestPolicy) | ||
if active { | ||
activeFilters = append(activeFilters, filter) | ||
} | ||
} | ||
|
||
return activeFilters | ||
} | ||
|
||
func FilterProviders(ctx sdk.Context, filters []Filter, providers []epochstoragetypes.StakeEntry, strictestPolicy projectstypes.Policy, currentEpoch uint64) ([]epochstoragetypes.StakeEntry, error) { | ||
filters = initFilters(filters, strictestPolicy) | ||
|
||
var filtersResult [][]bool | ||
|
||
for _, filter := range filters { | ||
res := filter.Filter(ctx, providers, currentEpoch) | ||
if len(res) != len(providers) { | ||
return []epochstoragetypes.StakeEntry{}, utils.LavaFormatError("filter result length is not equal to providers list length", fmt.Errorf("filter failed"), | ||
utils.Attribute{Key: "filter result length", Value: len(res)}, | ||
utils.Attribute{Key: "providers length", Value: len(providers)}, | ||
) | ||
} | ||
filtersResult = append(filtersResult, res) | ||
} | ||
|
||
filteredProviders := []epochstoragetypes.StakeEntry{} | ||
for j := 0; j < len(providers); j++ { | ||
result := true | ||
for i := 0; i < len(filters); i++ { | ||
result = result && filtersResult[i][j] | ||
if !result { | ||
break | ||
} | ||
} | ||
|
||
if result { | ||
filteredProviders = append(filteredProviders, providers[j]) | ||
} | ||
} | ||
|
||
return filteredProviders, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package filters | ||
|
||
import ( | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
epochstoragetypes "github.com/lavanet/lava/x/epochstorage/types" | ||
projectstypes "github.com/lavanet/lava/x/projects/types" | ||
) | ||
|
||
type FrozenProvidersFilter struct{} | ||
|
||
func (f *FrozenProvidersFilter) InitFilter(strictestPolicy projectstypes.Policy) bool { | ||
// frozen providers (or providers that their stake is not applied yet) can't be part of the pairing - this filter is always active | ||
return true | ||
} | ||
|
||
func (f *FrozenProvidersFilter) Filter(ctx sdk.Context, providers []epochstoragetypes.StakeEntry, currentEpoch uint64) []bool { | ||
filterResult := make([]bool, len(providers)) | ||
for i := range providers { | ||
if !isProviderFrozen(ctx, providers[i], currentEpoch) { | ||
filterResult[i] = true | ||
} | ||
} | ||
|
||
return filterResult | ||
} | ||
|
||
func isProviderFrozen(ctx sdk.Context, stakeEntry epochstoragetypes.StakeEntry, currentEpoch uint64) bool { | ||
return stakeEntry.StakeAppliedBlock > currentEpoch | ||
} |
Oops, something went wrong.