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.
CNS-213: add support for filters in pairing
- Loading branch information
Showing
3 changed files
with
87 additions
and
88 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,68 @@ | ||
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, stakeEntry []epochstoragetypes.StakeEntry) []bool | ||
Filter(ctx sdk.Context, providers []epochstoragetypes.StakeEntry) []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} | ||
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) []epochstoragetypes.StakeEntry { | ||
filters = initFilters(filters, strictestPolicy) | ||
|
||
filtersResult := make([]bool, len(providers)) | ||
for i := range filtersResult { | ||
filtersResult[i] = true | ||
} | ||
|
||
for _, filter := range filters { | ||
res := filter.Filter(ctx, providers) | ||
if len(res) != len(providers) { | ||
utils.LavaFormatError("filter result length is not equal to providers list length", fmt.Errorf("filter failed"), | ||
utils.Attribute{}, | ||
) | ||
} | ||
|
||
for i := range res { | ||
filtersResult[i] = filtersResult[i] && res[i] | ||
} | ||
} | ||
|
||
filteredProviders := []epochstoragetypes.StakeEntry{} | ||
for i := range filtersResult { | ||
if filtersResult[i] { | ||
filteredProviders = append(filteredProviders, providers[i]) | ||
} | ||
} | ||
|
||
return filteredProviders | ||
} |
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