blocktree

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Mar 1, 2024 License: LGPL-3.0 Imports: 13 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")

	ErrKeyNotFound = errors.New("key not found")
	// 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

View Source
var ErrNilBlockInRange = errors.New("nil block in range")
View Source
var (
	ErrRuntimeNotFound = errors.New("runtime not found")
)
View Source
var ErrStartGreaterThanEnd = errors.New("start greater than end")

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 added in v0.3.1

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 added in v0.7.0

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 added in v0.3.0

func (bt *BlockTree) DeepCopy() *BlockTree

DeepCopy returns a copy of the BlockTree

func (*BlockTree) GetAllBlocks added in v0.2.0

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

GetAllBlocks returns all the blocks in the tree

func (*BlockTree) GetAllBlocksAtNumber added in v0.7.0

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) GetAllDescendants added in v0.7.0

func (bt *BlockTree) GetAllDescendants(hash common.Hash) ([]Hash, error)

GetAllDescendants returns all block hashes that are descendants of the given block hash (including itself).

func (*BlockTree) GetArrivalTime added in v0.7.0

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

GetArrivalTime returns the arrival time of a block

func (*BlockTree) GetBlockRuntime added in v0.7.0

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

GetBlockRuntime returns the runtime corresponding to the given block hash. If there is no instance for the given block hash it will lookup an instance of an ancestor and return it.

func (*BlockTree) GetHashByNumber added in v0.7.0

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) 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. If parent and child are the same, we return true.

func (*BlockTree) Leaves

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

Leaves returns the leaves of the blocktree as an array

func (*BlockTree) LowestCommonAncestor added in v0.7.0

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

LowestCommonAncestor returns the lowest common ancestor block hash between two blocks in the tree.

func (*BlockTree) Prune added in v0.2.0

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) Range added in v0.8.0

func (bt *BlockTree) Range(startHash common.Hash, endHash common.Hash) (hashes []common.Hash, err error)

Range will return all the blocks between the start and end hash inclusive. If the end hash does not exist in the blocktree then an error is returned. If the start hash does not exist in the blocktree then we will return all blocks between the end and the blocktree root inclusive

func (*BlockTree) RangeInMemory added in v0.8.0

func (bt *BlockTree) RangeInMemory(startHash common.Hash, endHash common.Hash) (hashes []common.Hash, err error)

RangeInMemory returns the path from the node with Hash start to the node with Hash end. If the end hash does not exist in the blocktree then an error is returned. Different from blocktree.Range, if the start node is not found in the in memory blocktree

func (*BlockTree) StoreRuntime added in v0.7.0

func (bt *BlockTree) StoreRuntime(hash common.Hash, instance 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

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