voting

package
v0.7.0-beta Latest Latest
Warning

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

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

Documentation

Index

Constants

View Source
const (
	ValidatorVoteBodyBytePrice = 1000      // Per byte cost
	ValidatorVoteIDPrice       = 1000 * 16 // 16 bytes for the UUID
)

Variables

View Source
var (
	ErrAlreadyVoted       = errors.New("vote already exists from voter")
	ErrResolutionNotFound = errors.New("resolution not found")
)

Functions

func OrderedListOfVoters

func OrderedListOfVoters(voters map[string]int64) []string

func RegisterPayload

func RegisterPayload(payload ResolutionPayload) error

Types

type AccountStore

type AccountStore interface {
	// Account gets an account by its identifier
	GetAccount(ctx context.Context, db sql.DB, identifier []byte) (*accounts.Account, error)

	// Credit credits an account with a given amount
	Credit(ctx context.Context, db sql.DB, account []byte, amount *big.Int) error
}

type Datastores

type Datastores struct {
	Accounts AccountStore
	Engine   Engine
}

Datastores provides implementers of ResolutionPayload with access to different datastore interfaces

type Engine

type Engine interface {
	// CreateDataset creates a new dataset.
	// The passed caller will be the owner of the dataset.
	CreateDataset(ctx context.Context, tx sql.DB, schema *types.Schema, caller []byte) (err error)
	// DeleteDataset deletes a dataset.
	// The passed caller must be the owner of the dataset.
	DeleteDataset(ctx context.Context, tx sql.DB, dbid string, caller []byte) error
	// Execute executes a procedure (aka action) that exists in a dataset's schema.
	Execute(ctx context.Context, tx sql.DB, options *types.ExecutionData) (*sql.ResultSet, error)
	// GetSchema returns the schema of a dataset.
	GetSchema(ctx context.Context, dbid string) (*types.Schema, error)
	// Query executes a read-only query on a dataset.
	Query(ctx context.Context, tx sql.DB, dbid string, query string) (*sql.ResultSet, error)
}

Engine is the Kwil database engine. It is capable of deploying datasets, executing actions, and reading data.

type Resolution

type Resolution struct {
	// ID is the unique identifier of the vote
	ID types.UUID

	// Type is a string to identify the type of a vote.
	// This can be nil, if no body has been added yet.
	Type string

	// Body is the actual vote
	// This can be nil, if the vote was began before a
	// body was added.
	Body []byte

	// Expiration is the blockheight at which the vote expires
	Expiration int64
}

Resolution is a struct that contains information about a resolution

type ResolutionPayload

type ResolutionPayload interface {
	// Type returns the type of the payload.
	// This should be constant for a given payload implementation.
	Type() string
	// UnmarshalBinary unmarshals the payload from binary data.
	UnmarshalBinary(data []byte) error

	// MarshalBinary marshals the payload into binary data.
	MarshalBinary() ([]byte, error)

	// Apply is called when a resolution is approved. Voters is the list of all voters voted for the resolution, including the proposer.
	// Ensure that all changes to the datastores should be deterministic, else it will lead to consensus failures.
	Apply(ctx context.Context, db sql.DB, datastores Datastores, proposer []byte, voters []Voter, logger log.Logger) error
}

ResolutionPayload is a payload that can be used as the body of a resolution

type ResolutionVoteInfo

type ResolutionVoteInfo struct {
	Resolution

	// ApprovedPower is the aggregate amount of power that has approved
	// the vote.
	ApprovedPower int64

	// Proposer
	VoteBodyProposer []byte

	// Voters
	Voters []Voter

	// used to indicate if proposer has submitted both the Body and ID transactions
	SubmittedBodyAndID bool
}

ResolutionVoteInfo is a struct that contains information about the status of a resolution

type Threshold

type Threshold struct {
	Num   int64
	Denom int64
}

Threshold is a struct that contains the numerator and denominator of a fraction It is used to define the percentage of total voting power required for taking a specific action

type VoteProcessor

type VoteProcessor struct {
	// contains filtered or unexported fields
}

VoteProcessor manages in-process votes, and manages how they get processed. It is responsible for tracking the relative power of voters, and for expiring resolutions.

func NewVoteProcessor

func NewVoteProcessor(ctx context.Context, db sql.DB, accounts AccountStore,
	engine Engine, threshold Threshold, logger log.Logger) (*VoteProcessor, error)

NewVoteProcessor creates a new vote processor. It initializes the database with the required tables. The threshold is the percentThreshold of votes required to approve a resolution It must be an integer between 0 and 1000000. This defines the percentage

func (*VoteProcessor) Approve

func (v *VoteProcessor) Approve(ctx context.Context, db sql.DB, resolutionID types.UUID, expiration int64, from []byte) error

Approve approves a resolution from a voter. If the resolution does not yet exist, it will be created. If created, it will not be given a body, and can be given a body later. If the resolution already exists, it will simply track that the voter has approved the resolution, and will not change the body or expiration. If the voter does not exist, an error will be returned. If the voter has already approved the resolution, no error will be returned. If the resolution has already been processed, no error will be returned.

func (*VoteProcessor) ContainsBody

func (v *VoteProcessor) ContainsBody(ctx context.Context, db sql.DB, id types.UUID) (bool, error)

ContainsBody returns true if the resolution has a body.

func (*VoteProcessor) ContainsBodyOrFinished

func (v *VoteProcessor) ContainsBodyOrFinished(ctx context.Context, db sql.DB, id types.UUID) (bool, error)

ContainsBodyOrFinished returns true if (any of the following are true): 1. the resolution has a body 2. the resolution has expired 3. the resolution has been approved

func (*VoteProcessor) CreateResolution

func (v *VoteProcessor) CreateResolution(ctx context.Context, db sql.DB, event *types.VotableEvent, expiration int64, voteBodyProposer []byte) error

CreateResolution creates a vote, by submitting a body of a vote, a topic and an expiration. The expiration should be a blockheight. If the resolution already exists, it will not be changed. If the resolution was already processed, nothing will happen.

func (*VoteProcessor) Expire

func (v *VoteProcessor) Expire(ctx context.Context, db sql.DB, blockheight int64) error

Expire expires all votes at or before a given blockheight. All expired votes will be removed from the system.

func (*VoteProcessor) GetResolutionVoteInfo

func (v *VoteProcessor) GetResolutionVoteInfo(ctx context.Context, db sql.DB, id types.UUID) (info *ResolutionVoteInfo, err error)

GetResolutionVoteInfo gets a resolution, identified by the ID. It does not read uncommitted data.

func (*VoteProcessor) GetVoterPower

func (v *VoteProcessor) GetVoterPower(ctx context.Context, db sql.DB, identifier []byte) (power int64, err error)

GetVoterPower gets the power of a voter. If the voter does not exist, it will return 0.

func (*VoteProcessor) GetVotesByCategory

func (v *VoteProcessor) GetVotesByCategory(ctx context.Context, db sql.DB, category string) (votes []*Resolution, err error)

GetVotesByCategory gets all votes of a specific category. It does not read uncommitted data. (what is the indented consumer?)

func (*VoteProcessor) HasVoted

func (v *VoteProcessor) HasVoted(ctx context.Context, tx sql.DB, resolutionID types.UUID, from []byte) (bool, error)

HasVoted checks if a voter has voted on a resolution.

func (*VoteProcessor) IsProcessed

func (v *VoteProcessor) IsProcessed(ctx context.Context, tx sql.DB, resolutionID types.UUID) (bool, error)

alreadyProcessed checks if a vote has either already succeeded, or expired.

func (*VoteProcessor) ProcessConfirmedResolutions

func (v *VoteProcessor) ProcessConfirmedResolutions(ctx context.Context, db sql.DB) ([]types.UUID, error)

ProcessConfirmedResolutions processes all resolutions that have exceeded the threshold of votes, and have a non-nil body. It sorts them lexicographically by ID, and processes them in that order.

func (*VoteProcessor) RequiredPower

func (v *VoteProcessor) RequiredPower(ctx context.Context, db sql.DB, num, denom int64) (int64, error)

requiredPower gets the required power to meet the threshold requirements.

func (*VoteProcessor) UpdateVoter

func (v *VoteProcessor) UpdateVoter(ctx context.Context, db sql.DB, identifier []byte, power int64) error

UpdateVoter adds a voter to the system, with a given voting power. If the voter already exists, it will add the power to the existing power. If the power is 0, it will remove the voter from the system. If negative power is given, it will subtract the power from the existing power.

type VoteProcessorStub

type VoteProcessorStub interface {
	AddVoter(ctx context.Context, identifier []byte, power int64) error
	Approve(ctx context.Context, resolutionID types.UUID, expiration int64, from []byte) error
	CreateVote(ctx context.Context, body []byte, category string, expiration int64) error
	Expire(ctx context.Context, blockHeight int64) error
	GetResolution(ctx context.Context, id types.UUID) (info *ResolutionVoteInfo, err error)
	GetVotesByCategory(ctx context.Context, category string) (votes []*Resolution, err error)
	ProcessConfirmedResolutions(ctx context.Context) error
	RemoveVoter(ctx context.Context, identifier []byte) error
}

type Voter

type Voter struct {
	PubKey []byte
	Power  int64
}

Jump to

Keyboard shortcuts

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