vote

package
v0.2.4-0...-2c2ceb1 Latest Latest
Warning

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

Go to latest
Published: Nov 30, 2020 License: Apache-2.0, BSD-2-Clause Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrVotingNotFound is returned when a voting for a given id wasn't found.
	ErrVotingNotFound = errors.New("no voting found")
)

Functions

func ConvertOpinionToInt32

func ConvertOpinionToInt32(x Opinion) int32

ConvertOpinionToInt32 converts the given Opinion to an int32.

func ConvertOpinionsToInts32

func ConvertOpinionsToInts32(opinions []Opinion) []int32

ConvertOpinionsToInts32 converts the given slice of Opinion to a slice of int32.

func OpinionCaller

func OpinionCaller(handler interface{}, params ...interface{})

OpinionCaller calls the given handler with an OpinionEvent (containing its opinions, its associated ID and context).

func RoundStatsCaller

func RoundStatsCaller(handler interface{}, params ...interface{})

RoundStatsCaller calls the given handler with a RoundStats.

Types

type Context

type Context struct {
	ID string
	// The percentage of OpinionGivers who liked this item on the last query.
	Liked float64
	// The number of voting rounds performed.
	Rounds int
	// Append-only list of opinions formed after each round.
	// the first opinion is the initial opinion when this vote context was created.
	Opinions []Opinion
}

Context is the context of votes from multiple rounds about a given item.

func NewContext

func NewContext(id string, initOpn Opinion) *Context

NewContext creates a new vote context.

func (*Context) AddOpinion

func (vc *Context) AddOpinion(opn Opinion)

AddOpinion adds the given opinion to this vote context.

func (*Context) HadFirstRound

func (vc *Context) HadFirstRound() bool

HadFirstRound tells whether the vote context just had its first round.

func (*Context) IsFinalized

func (vc *Context) IsFinalized(coolingOffPeriod int, finalizationThreshold int) bool

IsFinalized tells whether this vote context is finalized by checking whether the opinion was held for finalizationThreshold number of rounds.

func (*Context) IsNew

func (vc *Context) IsNew() bool

IsNew tells whether the vote context is new.

func (*Context) LastOpinion

func (vc *Context) LastOpinion() Opinion

LastOpinion returns the last formed opinion.

type DRNGRoundBasedVoter

type DRNGRoundBasedVoter interface {
	Voter
	// Round starts a new round.
	Round(rand float64) error
}

DRNGRoundBasedVoter is a Voter which votes in rounds and uses random numbers which were generated in a decentralized fashion.

type Events

type Events struct {
	// Fired when an Opinion has been finalized.
	Finalized *events.Event
	// Fired when an Opinion couldn't be finalized.
	Failed *events.Event
	// Fired when a DRNGRoundBasedVoter has executed a round.
	RoundExecuted *events.Event
	// Fired when internal errors occur.
	Error *events.Event
}

Events defines events which happen on a Voter.

type Opinion

type Opinion byte

Opinion is an opinion about a given thing.

const (
	// Like defines a Like opinion.
	Like Opinion = 1 << 0
	// Dislike defines a Dislike opinion.
	Dislike Opinion = 1 << 1
	// Unknown defines an unknown opinion.
	Unknown Opinion = 1 << 2
)

func ConvertInt32Opinion

func ConvertInt32Opinion(x int32) Opinion

ConvertInt32Opinion converts the given int32 to an Opinion.

func ConvertInts32ToOpinions

func ConvertInts32ToOpinions(opinions []int32) []Opinion

ConvertInts32ToOpinions converts the given slice of int32 to a slice of Opinion.

func (Opinion) String

func (o Opinion) String() string

type OpinionEvent

type OpinionEvent struct {
	// ID is the of the conflict.
	ID string
	// Opinion is an opinion about a conflict.
	Opinion Opinion
	// Ctx contains all relevant infos regarding the conflict.
	Ctx Context
}

OpinionEvent is the struct containing data to be passed around with Finalized and Failed events.

type OpinionGiver

type OpinionGiver interface {
	// Query queries the OpinionGiver for its opinions on the given IDs.
	// The passed in context can be used to signal cancellation of the query.
	Query(ctx context.Context, ids []string) (Opinions, error)
	// ID returns the ID of the opinion giver.
	ID() string
}

OpinionGiver gives opinions about the given IDs.

type OpinionGiverFunc

type OpinionGiverFunc func() ([]OpinionGiver, error)

OpinionGiverFunc is a function which gives a slice of OpinionGivers or an error.

type Opinions

type Opinions []Opinion

Opinions is a slice of Opinion.

type QueriedOpinions

type QueriedOpinions struct {
	// The ID of the opinion giver.
	OpinionGiverID string `json:"opinion_giver_id"`
	// The map of IDs to opinions.
	Opinions map[string]Opinion `json:"opinions"`
	// The amount of times the opinion giver's opinion has counted.
	// Usually this number is 1 but due to randomization of the queried opinion givers,
	// the same opinion giver's opinions might be taken into account multiple times.
	TimesCounted int `json:"times_counted"`
}

QueriedOpinions represents queried opinions from a given opinion giver.

type RoundStats

type RoundStats struct {
	// The time it took to complete a round.
	Duration time.Duration `json:"duration"`
	// The rand number used during the round.
	RandUsed float64 `json:"rand_used"`
	// The vote contexts on which opinions were formed and queried.
	// This list does not include the vote contexts which were finalized/aborted
	// during the execution of the round.
	// Create a copy of this map if you need to modify any of its elements.
	ActiveVoteContexts map[string]*Context `json:"active_vote_contexts"`
	// The opinions which were queried during the round per opinion giver.
	QueriedOpinions []QueriedOpinions `json:"queried_opinions"`
}

RoundStats encapsulates data about an executed round.

type Voter

type Voter interface {
	// Vote submits the given ID for voting with its initial Opinion.
	Vote(id string, initOpn Opinion) error
	// IntermediateOpinion gets intermediate Opinion about the given ID.
	IntermediateOpinion(id string) (Opinion, error)
	// Events returns the Events instance of the given Voter.
	Events() Events
}

Voter votes on hashes.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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