consensus

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Jun 28, 2023 License: MIT Imports: 19 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// AvalancheRequestTimeout is the amount of time to wait for a response to a
	// query
	AvalancheRequestTimeout = 1 * time.Minute

	// AvalancheFinalizationScore is the confidence score we consider to be final
	AvalancheFinalizationScore = 160

	// AvalancheTimeStep is the amount of time to wait between event ticks
	AvalancheTimeStep = time.Millisecond

	// AvalancheMaxInflightPoll is the max outstanding requests that we can have
	// for any inventory item.
	AvalancheMaxInflightPoll = 10

	// AvalancheMaxElementPoll is the maximum number of invs to send in a single
	// query
	AvalancheMaxElementPoll = 4096

	// DeleteInventoryAfter is the maximum time we'll keep a block in memory
	// if it hasn't been finalized by avalanche.
	DeleteInventoryAfter = time.Hour * 6

	// ConsensusProtocol is the libp2p network protocol ID
	ConsensusProtocol = "/consensus/"

	ConsensusProtocolVersion = "1.0.0"

	// MaxRejectedCache is the maximum size of the rejected cache
	MaxRejectedCache = 200
)

Variables

This section is empty.

Functions

func UpdateLogger

func UpdateLogger()

Types

type AssertError

type AssertError string

AssertError identifies an error that indicates an internal code consistency issue and should be treated as a critical and unrecoverable error.

func (AssertError) Error

func (e AssertError) Error() string

Error returns the assertion error as a human-readable string and satisfies the error interface.

type BackoffChooser

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

BackoffChooser wraps the WeightedRandomChooser with a map that tracks exponential backoffs for peer dials.

func NewBackoffChooser

func NewBackoffChooser(chooser blockchain.WeightedChooser) *BackoffChooser

NewBackoffChooser returns a new initialized BackoffChooser

func (*BackoffChooser) RegisterDialFailure

func (b *BackoffChooser) RegisterDialFailure(p peer.ID)

RegisterDialFailure increases the exponential backoff time for the given peer.

func (*BackoffChooser) RegisterDialSuccess

func (b *BackoffChooser) RegisterDialSuccess(p peer.ID)

RegisterDialSuccess deletes the exponential backoff for the given peer.

func (*BackoffChooser) WeightedRandomValidator

func (b *BackoffChooser) WeightedRandomValidator() peer.ID

WeightedRandomValidator returns a weighted random validator. If the selected validator is undergoing a backoff weight time then "" will be returned.

type ConsensusEngine

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

ConsensusEngine implements a form of the avalanche consensus protocol. It primarily consists of an event loop that polls the weighted list of validators for any unfinalized blocks and records the responses. Blocks finalize when the confidence level exceeds the threshold.

func NewConsensusEngine

func NewConsensusEngine(ctx context.Context, opts ...Option) (*ConsensusEngine, error)

NewConsensusEngine returns a new ConsensusEngine

func (*ConsensusEngine) Close

func (eng *ConsensusEngine) Close()

Close gracefully shutsdown the consensus engine

func (*ConsensusEngine) HandleNewStream

func (eng *ConsensusEngine) HandleNewStream(s inet.Stream)

HandleNewStream handles incoming streams from peers. We use one stream for incoming and a separate one for outgoing.

func (*ConsensusEngine) NewBlock

func (eng *ConsensusEngine) NewBlock(header *blocks.BlockHeader, initialAcceptancePreference bool, callback chan<- Status)

NewBlock is used to pass new work in the engine. The callback channel will return the final status (either Finalized or Rejected). Unfinalized but NotPreffered blocks will remain active in the engine until a conflicting block at the same height is finalized. At that point the block will be marked as Rejected.

type HasBlockFunc

type HasBlockFunc func(blockID types.ID) bool

HasBlockFunc checks the blockchain to see if we already have the block.

type Option

type Option func(cfg *config) error

Option is configuration option function for the blockchain

func Chooser

func Chooser(chooser blockchain.WeightedChooser) Option

Chooser is an implementation of the WeightedChooser used to select a validator to poll at random.

This option is required.

func HasBlock

func HasBlock(hasBlockFunc HasBlockFunc) Option

HasBlock is a function which checks if the blockchain contains the given block

This option is required.

func Network

func Network(n *net.Network) Option

Network is the node's network implementation.

This option is required.

func Params

func Params(params *params.NetworkParams) Option

Params identifies which chain parameters the chain is associated with.

This option is required.

func PeerID

func PeerID(self peer.ID) Option

PeerID is the node's own peerID.

This option is required.

func RequestBlock

func RequestBlock(requestBlockFunc RequestBlockFunc) Option

RequestBlock is a function which requests to download a block from the given peer.

This option is required.

type RequestBlockFunc

type RequestBlockFunc func(blockID types.ID, remotePeer peer.ID)

RequestBlockFunc is called when the engine receives a query from a peer about and unknown block. It should attempt to download the block from the remote peer, validate it, then pass it into the engine.

type RequestRecord

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

RequestRecord is a poll request for more votes

func NewRequestRecord

func NewRequestRecord(timestamp int64, invs []types.ID) RequestRecord

NewRequestRecord creates a new RequestRecord

func (RequestRecord) GetInvs

func (r RequestRecord) GetInvs() map[types.ID]bool

GetInvs returns the poll Invs for the request

func (RequestRecord) GetTimestamp

func (r RequestRecord) GetTimestamp() int64

GetTimestamp returns the timestamp that the request was created

func (RequestRecord) IsExpired

func (r RequestRecord) IsExpired() bool

IsExpired returns true if the request has expired

type Status

type Status int

Status is the status of consensus on a particular target

const (
	// StatusRejected means the target has been rejected. Note that blocks are
	// only considered rejected if a competing block has been finalized. If the
	// confidence is a 'no' result has crossed the finalization score, it will
	// remain in the StatusNotPreferred state until a competing block has been
	// finalized.
	StatusRejected Status = iota

	// StatusNotPreferred means the target is not currently preferred by the node
	StatusNotPreferred

	// StatusPreferred means the target is currently preferred by the node
	StatusPreferred

	// StatusFinalized means the target has been finalized in the affirmative.
	StatusFinalized
)

func (Status) String

func (s Status) String() string

type VoteRecord

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

VoteRecord keeps track of a series of votes for a target

func NewVoteRecord

func NewVoteRecord(blockID types.ID, height uint32, preferred bool) *VoteRecord

NewVoteRecord instantiates a new base record for voting on a target `accepted` indicates whether or not the initial state should be preferred

func (*VoteRecord) Reject

func (vr *VoteRecord) Reject()

func (*VoteRecord) Reset

func (vr *VoteRecord) Reset(preference bool)

Jump to

Keyboard shortcuts

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