consensus

package module
v0.0.0-...-2d6bae1 Latest Latest
Warning

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

Go to latest
Published: Oct 20, 2018 License: GPL-3.0 Imports: 19 Imported by: 1

Documentation

Overview

Package consensus provides a blockchain consensus finding system. It allows client programs to submit blocks to be tracked and to retrieve the 'best' head block for the next round of block computation. A lightweight block interface is provided to allow the package to retrieve basic information about the block.

The default criteria for branch comparison is blockchain height. Client programs may provide a comparator function to inject their own criteria for evaluation.

Example pseudo code of client program:

var c *Consensus = consensus.NewConsensusConsensus(0, nil)
...

// This function is called whenever a block is received from the network
// MyBlock implements the consensus.Block interface
func receiveBlockFromNetwork(b MyBlock) {
	if !c.WasSeen(b) {
		// preform any validation or processing required to
		// ensure block is worthy of submission
		if validate(b) {
			c.AddBlock(b)
		}
	}
}

// This function continuously competes for the next block
func competeForBlocks() {
	for {
		// don't peg the CPU
		// MyBlock implements the consensus.Block interface

		branch := c.GetBestBranch()
		if branch == nil {
			// no best branch is available, wait for more blocks
			time.Sleep(50)
			// to come in from the network
			continue
		}

		// generate the next block based on the head of the best branch
		var head MyBlock = branch[0].(MyBlock)
		var nextBlock MyBlock = generateNextBlock(head)
		c.setCompeted(head)
		sendBlockToNetwork(nextBlock)
		c.AddBlock(nextBlock)
	}
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CompetingBranch

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

func (*CompetingBranch) Blocks

func (b *CompetingBranch) Blocks() []spec.Block

func (*CompetingBranch) ConsecutiveLocalHits

func (b *CompetingBranch) ConsecutiveLocalHits() int

func (*CompetingBranch) HitRate

func (b *CompetingBranch) HitRate() float64

func (*CompetingBranch) RootID

func (b *CompetingBranch) RootID() int

type Competition

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

Competition isolates the data that needs to be communicated to the blockchain for competition from the rest of the consensus system.

func (*Competition) Branches

func (c *Competition) Branches() map[int]spec.CompetingBranch

Branches returns the current competing branches being tracked in the consensus system indexed by RootID.

type Consensus

type Consensus struct {
	sync.Mutex
	// contains filtered or unexported fields
}

Consensus tracks incoming blocks that are younger than Depth. It tracks all the branches of blocks and prunes according to rules defined in the compareBlocks function. Client programs can retrieve the most favorable head for its next block computation using the GetBestBranch method.

func NewConsensus

func NewConsensus(blockComparator spec.BlockComparator) *Consensus

func (*Consensus) AddBlocks

func (c *Consensus) AddBlocks(blocks []spec.Block, isLocal bool) (res *spec.AddBlocksResponse)

AddBlock adds the given block to the consensus tracking. Sibling and children branches are pruned according to the rules in the spec.BlockComparator function.

func (*Consensus) ConfirmBlocks

func (c *Consensus) ConfirmBlocks()

This function runs within the mutex lock of c.AddBlock

func (*Consensus) Evaluate

func (c *Consensus) Evaluate() spec.Competition

func (*Consensus) OnBlockConfirmed

func (c *Consensus) OnBlockConfirmed(f spec.BlockConfirmationHandler)

func (*Consensus) OnLocalBlockConfirmed

func (c *Consensus) OnLocalBlockConfirmed(f spec.BlockConfirmationHandler)

func (*Consensus) SetCompeted

func (c *Consensus) SetCompeted(head spec.Block)

SetCompeted is called by the client program to tell the Consensus instance that the client program has already generated a block for the given head. This block will no longer be returned as a head by GetBestBranch.

func (*Consensus) SetConfirmingRoot

func (c *Consensus) SetConfirmingRoot(rootID int)

func (*Consensus) WasSeen

func (c *Consensus) WasSeen(block spec.Block) bool

WasSeen returns true if the given block has already been sent to the AddBlock method.

type RPC

type RPC struct {
}

func (*RPC) GetMetrics

func (h *RPC) GetMetrics(r *http.Request, args *rpcconsensus.GetMetricsArgs, reply *rpcconsensus.GetMetricsReply) error

func (*RPC) GetTree

func (h *RPC) GetTree(r *http.Request, args *rpcconsensus.GetTreeArgs, reply *rpcconsensus.GetTreeReply) error

Jump to

Keyboard shortcuts

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