blocktree

package
v0.0.0-...-c69f244 Latest Latest
Warning

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

Go to latest
Published: Jul 16, 2022 License: LGPL-3.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrParentNotFound = errors.New("cannot find parent block in blocktree")

	// ErrBlockExists is returned if attempting to re-add a block
	ErrBlockExists = errors.New("cannot add block to blocktree that already exists")

	// ErrStartNodeNotFound is returned if the start of a subchain does not exist
	ErrStartNodeNotFound = errors.New("start node does not exist")

	// ErrEndNodeNotFound is returned if the end of a subchain does not exist
	ErrEndNodeNotFound = errors.New("end node does not exist")

	// ErrNilDatabase is returned in the database is nil
	ErrNilDatabase = errors.New("blocktree database is nil")

	// ErrNilDescendant is returned if calling subchain with a nil node
	ErrNilDescendant = errors.New("descendant node is nil")

	// ErrDescendantNotFound is returned if a descendant in a subchain cannot be found
	ErrDescendantNotFound = errors.New("could not find descendant node")

	// ErrNodeNotFound is returned if a node with given hash doesn't exist
	ErrNodeNotFound = errors.New("could not find node")

	// ErrFailedToGetRuntime is returned when runtime doesn't exist in blockTree for corresponding block.
	ErrFailedToGetRuntime = errors.New("failed to get runtime instance")

	// ErrNumGreaterThanHighest is returned when attempting to get a
	// hash by number that is higher than any in the blocktree
	ErrNumGreaterThanHighest = errors.New("cannot find node with number greater than highest in blocktree")

	// ErrNumLowerThanRoot is returned when attempting to get a hash by number that is lower than the root node
	ErrNumLowerThanRoot = errors.New("cannot find node with number lower than root node")

	// ErrNoCommonAncestor is returned when a common ancestor cannot be found between two nodes
	ErrNoCommonAncestor = errors.New("no common ancestor between two nodes")
)

ErrParentNotFound is returned if the parent hash does not exist in the blocktree

Functions

This section is empty.

Types

type BlockTree

type BlockTree struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

BlockTree represents the current state with all possible blocks

func NewBlockTreeFromRoot

func NewBlockTreeFromRoot(root *types.Header) *BlockTree

NewBlockTreeFromRoot initialises a blocktree with a root block. The root block is always the most recently finalised block (ie the genesis block if the node is just starting.)

func NewEmptyBlockTree

func NewEmptyBlockTree() *BlockTree

NewEmptyBlockTree creates a BlockTree with a nil head

func (*BlockTree) AddBlock

func (bt *BlockTree) AddBlock(header *types.Header, arrivalTime time.Time) (err error)

AddBlock inserts the block as child of its parent node Note: Assumes block has no children

func (*BlockTree) BestBlockHash

func (bt *BlockTree) BestBlockHash() Hash

BestBlockHash returns the hash of the block that is considered "best" based on the fork-choice rule. It returns the head of the chain with the most primary blocks. If there are multiple chains with the same number of primaries, it returns the one with the highest head number. If there are multiple chains with the same number of primaries and the same height, it returns the one with the head block that arrived the earliest.

func (*BlockTree) DeepCopy

func (bt *BlockTree) DeepCopy() *BlockTree

DeepCopy returns a copy of the BlockTree

func (*BlockTree) GetAllBlocks

func (bt *BlockTree) GetAllBlocks() []Hash

GetAllBlocks returns all the blocks in the tree

func (*BlockTree) GetAllBlocksAtNumber

func (bt *BlockTree) GetAllBlocksAtNumber(hash common.Hash) (hashes []common.Hash)

GetAllBlocksAtNumber will return all blocks hashes with the number of the given hash plus one. To find all blocks at a number matching a certain block, pass in that block's parent hash

func (*BlockTree) GetArrivalTime

func (bt *BlockTree) GetArrivalTime(hash common.Hash) (time.Time, error)

GetArrivalTime returns the arrival time of a block

func (*BlockTree) GetBlockRuntime

func (bt *BlockTree) GetBlockRuntime(hash common.Hash) (runtime.Instance, error)

GetBlockRuntime returns block runtime for corresponding block hash.

func (*BlockTree) GetHashByNumber

func (bt *BlockTree) GetHashByNumber(num uint) (common.Hash, error)

GetHashByNumber returns the block hash with the given number that is on the best chain. If the number is lower or higher than the numbers in the blocktree, an error is returned.

func (*BlockTree) HighestCommonAncestor

func (bt *BlockTree) HighestCommonAncestor(a, b Hash) (Hash, error)

HighestCommonAncestor returns the highest block that is a Ancestor to both a and b

func (*BlockTree) IsDescendantOf

func (bt *BlockTree) IsDescendantOf(parent, child Hash) (bool, error)

IsDescendantOf returns true if the child is a descendant of parent, false otherwise. it returns an error if either the child or parent are not in the blocktree.

func (*BlockTree) Leaves

func (bt *BlockTree) Leaves() []Hash

Leaves returns the leaves of the blocktree as an array

func (*BlockTree) Prune

func (bt *BlockTree) Prune(finalised Hash) (pruned []Hash)

Prune sets the given hash as the new blocktree root, removing all nodes that are not the new root node or its descendant It returns an array of hashes that have been pruned

func (*BlockTree) StoreRuntime

func (bt *BlockTree) StoreRuntime(hash common.Hash, in runtime.Instance)

StoreRuntime stores the runtime for corresponding block hash.

func (*BlockTree) String

func (bt *BlockTree) String() string

String utilises github.com/disiqueira/gotree to create a printable tree

func (*BlockTree) SubBlockchain

func (bt *BlockTree) SubBlockchain(start, end Hash) ([]Hash, error)

SubBlockchain returns the path from the node with Hash start to the node with Hash end

type Hash

type Hash = common.Hash

Hash common.Hash

Jump to

Keyboard shortcuts

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