scores

package
v1.2.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 2, 2024 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// TODO: temp strategy weight until we implement taking it out of the policy
	UNIFORM_WEIGHT uint64 = 1
)

Variables

GEO_LATENCY_MAP is a map of lists of GeoLatency that defines the cost of geo mismatch for each single geolocation. The map key is a single geolocation and the value is an ordered list of neighbors and their latency (ordered by latency) latency data from: https://wondernetwork.com/pings (July 2023)

Functions

func AddProviderToSelection

func AddProviderToSelection(providerScore *PairingScore, groupIndexes []int, totalScore math.Uint, slotIndexScore map[int]math.Uint) (math.Uint, map[int]math.Uint)

func CalcGeoCost

func CalcGeoCost(reqGeo planstypes.Geolocation, providerGeos []planstypes.Geolocation) (minLatencyGeo planstypes.Geolocation, minLatencyCost math.Uint)

CalcGeoCost() finds the minimal latency between the required geo and the provider's supported geolocations

func CalcGeoLatency

func CalcGeoLatency(reqGeo planstypes.Geolocation, providerGeos []planstypes.Geolocation) (planstypes.Geolocation, uint64)

func CalcPairingScore

func CalcPairingScore(scores []*PairingScore, strategy ScoreStrategy, diffSlot PairingSlotInf) error

CalcPairingScore calculates the final pairing score for a pairing slot (with strategy) For efficiency purposes, we calculate the score on a diff slot which represents the diff reqs of the current slot and the previous slot

func CalculateTotalScoresForGroup

func CalculateTotalScoresForGroup(scores []*PairingScore, groupIndexes []int) (totalScore math.Uint, slotIndexScore map[int]math.Uint, err error)

this function calculates the total score, and negative score modifiers for each slot. the negative score modifiers contain the total stake of providers that are not allowed so if a provider is not allowed in slot X, it will be added to the total score but will have slotIndexScore[X]+= providerStake and during the selection of slot X we will have the selection effective sum as: totalScore - slotIndexScore[X], and that provider that is not allowed can't be selected

func PickProviders

func PickProviders(ctx sdk.Context, scores []*PairingScore, groupIndexes []int, hashData []byte) (returnedProviders []epochstoragetypes.StakeEntry)

PickProviders pick a <group-count> providers set with a pseudo-random weighted choice (using the providers' score list and hashData) selection is done by adding all of the scores in the sorted list and generating a random value

func PrepareHashData

func PrepareHashData(projectIndex, chainID string, epochHash []byte, idx int) []byte

PrepareHashData prepares the hash needed in the pseudo-random choice of providers

func RemoveProviderFromSelection

func RemoveProviderFromSelection(providerScore *PairingScore, groupIndexes []int, totalScore math.Uint, slotIndexScore map[int]math.Uint) (math.Uint, map[int]math.Uint)

Types

type GeoReq

type GeoReq struct {
	Geo int32
}

geo requirement that implements the ScoreReq interface

func (GeoReq) Equal

func (gr GeoReq) Equal(other ScoreReq) bool

Equal() used to compare slots to determine slot groups

func (GeoReq) GetName

func (gr GeoReq) GetName() string

func (GeoReq) GetReqForSlot

func (gr GeoReq) GetReqForSlot(policy planstypes.Policy, slotIdx int) ScoreReq

TODO: this function doesn't return the optimal geo reqs for the case that there are more required geos than providers to pair

func (GeoReq) Init

func (gr GeoReq) Init(policy planstypes.Policy) bool

func (GeoReq) Score

func (gr GeoReq) Score(score PairingScore) math.Uint

Score calculates the geo score of a provider based on preset latency data Note: each GeoReq must have exactly a single geolocation (bit)

type PairingScore

type PairingScore struct {
	Provider            *epochstoragetypes.StakeEntry
	Score               math.Uint
	ScoreComponents     map[string]math.Uint
	SkipForSelection    bool
	SlotFiltering       map[int]struct{} // slot indexes here are skipped
	QosExcellenceReport pairingtypes.QualityOfServiceReport
}

PairingScore holds a provider's score with respect to a set of requirements (ScoreReq), indexed by their unique name.

func (*PairingScore) InvalidIndexes

func (ps *PairingScore) InvalidIndexes(possibleIndexes []int) []int

func (*PairingScore) IsValidForSelection

func (ps *PairingScore) IsValidForSelection(slotIndex int) bool

type PairingSlot

type PairingSlot struct {
	Reqs  map[string]ScoreReq
	Index int
}

func CalcSlots

func CalcSlots(policy *planstypes.Policy) []*PairingSlot

get the overall requirements from the policy and assign slots that'll fulfil them

func NewPairingSlot

func NewPairingSlot(slotIndex int) *PairingSlot

func (PairingSlot) Equal

func (s PairingSlot) Equal(other PairingSlotInf) bool

func (PairingSlot) Requirements

func (s PairingSlot) Requirements() map[string]ScoreReq

type PairingSlotGroup

type PairingSlotGroup struct {
	Reqs map[string]ScoreReq
	// contains filtered or unexported fields
}

func GroupSlots

func GroupSlots(slots []*PairingSlot) []*PairingSlotGroup

group the slots

func NewPairingSlotGroup

func NewPairingSlotGroup(pairingSlot *PairingSlot) *PairingSlotGroup

func (*PairingSlotGroup) AddToGroup

func (psg *PairingSlotGroup) AddToGroup(pairingSlot *PairingSlot)

func (PairingSlotGroup) Indexes

func (s PairingSlotGroup) Indexes() []int

func (PairingSlotGroup) Requirements

func (s PairingSlotGroup) Requirements() map[string]ScoreReq

func (PairingSlotGroup) Subtract

func (psg PairingSlotGroup) Subtract(other *PairingSlotGroup) *PairingSlot

Subtract generates a diff slot that contains the reqs that are in the slot receiver but not in the "other" slot

type PairingSlotInf

type PairingSlotInf interface {
	Requirements() map[string]ScoreReq
}

type QosGetter

type QosGetter interface {
	GetQos(ctx sdk.Context, chainID string, cluster string, provider string) (pairingtypes.QualityOfServiceReport, error)
}

type QosReq

type QosReq struct{}

QosReq implements the ScoreReq interface for provider staking requirement(s)

func (*QosReq) Equal

func (qr *QosReq) Equal(other ScoreReq) bool

Equal used to compare slots to determine slot groups. Equal always returns true (there are no different "types" of qos)

func (*QosReq) GetName

func (qr *QosReq) GetName() string

func (*QosReq) GetReqForSlot

func (qr *QosReq) GetReqForSlot(policy planstypes.Policy, slotIdx int) ScoreReq

func (*QosReq) Init

func (qr *QosReq) Init(policy planstypes.Policy) bool

func (*QosReq) Score

func (qr *QosReq) Score(score PairingScore) math.Uint

Score calculates the the provider's qos score

type ScoreReq

type ScoreReq interface {
	// Init() initializes the ScoreReq object and returns whether it's active
	Init(policy planstypes.Policy) bool
	// Score() calculates a provider's score according to the requirement
	Score(score PairingScore) math.Uint
	// GetName returns the unique name of the ScoreReq implementation
	GetName() string
	// Equal compares two ScoreReq objects
	Equal(other ScoreReq) bool
	// GetReqForSlot gets ScoreReq for slot by its index
	GetReqForSlot(policy planstypes.Policy, slotIdx int) ScoreReq
}

ScoreReq is an interface for pairing requirement scoring

func GetAllReqs

func GetAllReqs() []ScoreReq

type ScoreStrategy

type ScoreStrategy map[string]uint64

map: key: ScoreReq name, value: weight in the final pairing score

func GetStrategy

func GetStrategy() ScoreStrategy

TODO: currently we'll use weight=1 for all reqs. In the future, we'll get it from policy

type StakeReq

type StakeReq struct{}

StakeReq implements the ScoreReq interface for provider staking requirement(s)

func (*StakeReq) Equal

func (sr *StakeReq) Equal(other ScoreReq) bool

Equal used to compare slots to determine slot groups. Equal always returns true (StakeReq score of an entry is the entry's stake)

func (*StakeReq) GetName

func (sr *StakeReq) GetName() string

func (*StakeReq) GetReqForSlot

func (sr *StakeReq) GetReqForSlot(policy planstypes.Policy, slotIdx int) ScoreReq

func (*StakeReq) Init

func (sr *StakeReq) Init(policy planstypes.Policy) bool

func (*StakeReq) Score

func (sr *StakeReq) Score(score PairingScore) math.Uint

Score calculates the the provider score as the normalized stake

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL