beacon

package
v0.2.6 Latest Latest
Warning

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

Go to latest
Published: Jun 5, 2019 License: MIT Imports: 19 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func InitializeState

func InitializeState(c *config.Config, initialValidators []InitialValidatorEntry, genesisTime uint64, skipValidation bool) (*primitives.State, error)

InitializeState initializes state to the genesis state according to the config.

Types

type BlockFilter added in v0.2.4

type BlockFilter interface {
	Has(chainhash.Hash) bool
}

BlockFilter is a filter for block hashes that returns whether the block hash is in the filter or not.

type BlockIndex

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

BlockIndex is an index from Hash to block node.

func NewBlockIndex

func NewBlockIndex() *BlockIndex

NewBlockIndex creates a new block index.

func (*BlockIndex) AddBlockNodeToIndex

func (bi *BlockIndex) AddBlockNodeToIndex(block *primitives.Block, blockHash chainhash.Hash, stateRoot chainhash.Hash) (*BlockNode, error)

AddBlockNodeToIndex adds a new block ot the blockchain.

func (*BlockIndex) GetBlockNodeByHash

func (bi *BlockIndex) GetBlockNodeByHash(hash chainhash.Hash) *BlockNode

GetBlockNodeByHash gets a block node by hash.

func (*BlockIndex) Has

func (bi *BlockIndex) Has(h chainhash.Hash) bool

Has returns true if the block index has the specified block.

func (*BlockIndex) LoadBlockNode

func (bi *BlockIndex) LoadBlockNode(blockNodeDisk *db.BlockNodeDisk) (*BlockNode, error)

LoadBlockNode loads a block node from disk. The parent must have already been added.

type BlockNode

type BlockNode struct {
	Hash      chainhash.Hash
	Height    uint64
	Slot      uint64
	Parent    *BlockNode
	StateRoot chainhash.Hash
	Children  []*BlockNode
}

BlockNode is an in-memory representation of a block.

func (*BlockNode) GetAncestorAtHeight

func (node *BlockNode) GetAncestorAtHeight(height uint64) *BlockNode

GetAncestorAtHeight gets the ancestor of a block at a certain height.

func (*BlockNode) GetAncestorAtSlot

func (node *BlockNode) GetAncestorAtSlot(slot uint64) *BlockNode

GetAncestorAtSlot gets the ancestor of a block at a certain slot.

type Blockchain

type Blockchain struct {
	View *BlockchainView
	DB   db.Database

	Notifees []BlockchainNotifee
	// contains filtered or unexported fields
}

Blockchain handles the communication between 3 main components.

  1. BlockchainView stores block nodes as a chain and an index to allow easy access. All block nodes are stored in memory and only store information needed often.
  2. DB keeps track of the persistent data. It stores large block files, a representation of the chain through block nodes, and important information about the state of the chain.
  3. StateManager stores information about the state of certain blocks. The state manager stores the state and derived states of every block after the finalized block.

func NewBlockchainWithInitialValidators

func NewBlockchainWithInitialValidators(db db.Database, config *config.Config, validators []InitialValidatorEntry, skipValidation bool, genesisTime uint64) (*Blockchain, error)

NewBlockchainWithInitialValidators creates a new blockchain with the specified initial validators.

func (*Blockchain) AddBlockToStateMap

func (b *Blockchain) AddBlockToStateMap(block *primitives.Block, verifySignature bool) ([]primitives.Receipt, *primitives.State, error)

AddBlockToStateMap calculates the state after applying block and adds it to the state map.

func (*Blockchain) GenesisHash added in v0.2.4

func (b *Blockchain) GenesisHash() chainhash.Hash

GenesisHash gets the genesis hash for the chain.

func (*Blockchain) GetBlockByHash

func (b *Blockchain) GetBlockByHash(h chainhash.Hash) (*primitives.Block, error)

GetBlockByHash gets a block by Hash.

func (*Blockchain) GetConfig

func (b *Blockchain) GetConfig() *config.Config

GetConfig returns the config used by this blockchain

func (*Blockchain) GetCurrentSlot

func (b *Blockchain) GetCurrentSlot() uint64

GetCurrentSlot gets the current slot according to the time.

func (*Blockchain) GetEpochBoundaryHash

func (b *Blockchain) GetEpochBoundaryHash(slot uint64) (chainhash.Hash, error)

GetEpochBoundaryHash gets the Hash of the parent block at the epoch boundary.

func (*Blockchain) GetNextSlotTime

func (b *Blockchain) GetNextSlotTime() time.Time

GetNextSlotTime returns the timestamp of the next slot.

func (*Blockchain) GetState

func (b *Blockchain) GetState() primitives.State

GetState gets a copy of the current state of the blockchain.

func (*Blockchain) GetSubView

func (b *Blockchain) GetSubView(tip chainhash.Hash) (ChainView, error)

GetSubView gets a view of the blockchain at a certain tip.

func (*Blockchain) GetUpdatedState

func (b *Blockchain) GetUpdatedState(upTo uint64) (*primitives.State, error)

GetUpdatedState gets the tip, but with processed slots/epochs as appropriate.

func (*Blockchain) Height

func (b *Blockchain) Height() uint64

Height returns the height of the chain.

func (*Blockchain) ProcessBlock

func (b *Blockchain) ProcessBlock(block *primitives.Block, checkTime bool, verifySignature bool) ([]primitives.Receipt, *primitives.State, error)

ProcessBlock is called when a block is received from a peer.

func (*Blockchain) RegisterNotifee added in v0.2.4

func (b *Blockchain) RegisterNotifee(n BlockchainNotifee)

RegisterNotifee registers a notifee for blockchain

func (*Blockchain) StoreBlock

func (b *Blockchain) StoreBlock(block *primitives.Block) error

StoreBlock adds a block header to the current chain. The block should already have been validated by this point.

func (*Blockchain) UpdateChainHead

func (b *Blockchain) UpdateChainHead() error

UpdateChainHead updates the blockchain head if needed

type BlockchainNotifee added in v0.2.4

type BlockchainNotifee interface {
	ConnectBlock(*primitives.Block)
}

BlockchainNotifee is a blockchain notifee.

type BlockchainView

type BlockchainView struct {
	Chain *Chain
	Index *BlockIndex
	// contains filtered or unexported fields
}

BlockchainView is the state of GHOST-LMD

func NewBlockchainView

func NewBlockchainView() *BlockchainView

NewBlockchainView creates a new blockchain view.

func (*BlockchainView) GetFinalizedHead

func (bv *BlockchainView) GetFinalizedHead() (*BlockNode, primitives.State)

GetFinalizedHead gets the finalized head of the blockchain.

func (*BlockchainView) GetJustifiedHead

func (bv *BlockchainView) GetJustifiedHead() (*BlockNode, primitives.State)

GetJustifiedHead gets the justified head of the blockchain.

func (*BlockchainView) SetFinalizedHead

func (bv *BlockchainView) SetFinalizedHead(finalizedHash chainhash.Hash, finalizedState primitives.State)

SetFinalizedHead sets the finalized head of the blockchain.

func (*BlockchainView) SetJustifiedHead

func (bv *BlockchainView) SetJustifiedHead(justifiedHash chainhash.Hash, justifiedState primitives.State)

SetJustifiedHead sets the justified head of the blockchain.

type Chain

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

Chain is a representation of the current main chain.

func NewChain

func NewChain() *Chain

NewChain creates a new chain.

func (*Chain) Contains

func (c *Chain) Contains(node *BlockNode) bool

Contains checks if the chain contains a BlockNode.

func (*Chain) Genesis

func (c *Chain) Genesis() *BlockNode

Genesis gets the genesis of the chain

func (*Chain) GetBlockByHeight

func (c *Chain) GetBlockByHeight(height int) *BlockNode

GetBlockByHeight gets a block at a certain height or if it doesn't exist, returns nil.

func (*Chain) GetBlockBySlot added in v0.2.4

func (c *Chain) GetBlockBySlot(slot uint64) (*BlockNode, error)

GetBlockBySlot gets the block node at a certain slot.

func (*Chain) GetChainLocator

func (c *Chain) GetChainLocator() [][]byte

GetChainLocator gets a chain locator by requesting blocks at certain heights. This code is basically copied from the Bitcoin code.

func (*Chain) Height

func (c *Chain) Height() int64

Height returns the height of the chain.

func (*Chain) Next

func (c *Chain) Next(node *BlockNode) *BlockNode

Next gets the next node in the chain.

func (*Chain) SetTip

func (c *Chain) SetTip(node *BlockNode)

SetTip sets the tip of the chain.

func (*Chain) Tip

func (c *Chain) Tip() *BlockNode

Tip gets the tip of the chain.

type ChainView

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

ChainView is a view of a certain chain in the block tree so that block processing can access valid blocks.

func NewChainView

func NewChainView(tip *BlockNode) ChainView

NewChainView creates a new chain view with a certain tip

func (*ChainView) GetHashBySlot

func (c *ChainView) GetHashBySlot(slot uint64) (chainhash.Hash, error)

GetHashBySlot gets a hash of a block in a certain slot.

func (*ChainView) GetLastStateRoot added in v0.2.4

func (c *ChainView) GetLastStateRoot() (chainhash.Hash, error)

GetLastStateRoot gets the state root of the tip.

func (*ChainView) SetTipSlot

func (c *ChainView) SetTipSlot(slot uint64)

SetTipSlot sets the effective tip slot (which may be updated due to slot transitions)

func (*ChainView) Tip

func (c *ChainView) Tip() (chainhash.Hash, error)

Tip gets the tip of the blockchain.

type InitialValidatorEntry

type InitialValidatorEntry struct {
	PubKey                [96]byte
	ProofOfPossession     [48]byte
	WithdrawalShard       uint32
	WithdrawalCredentials chainhash.Hash
	DepositSize           uint64
}

InitialValidatorEntry is the validator entry to be added at the beginning of a blockchain.

type Mempool

type Mempool struct {
	AttestationMempool *attestationMempool
	// contains filtered or unexported fields
}

Mempool keeps track of actions (attestations, deposits, exits, slashings) to include in blocks.

func NewMempool

func NewMempool(blockchain *Blockchain) *Mempool

NewMempool creates a new mempool.

func (*Mempool) ConnectBlock added in v0.2.4

func (m *Mempool) ConnectBlock(b *primitives.Block)

ConnectBlock is part of the blockchain notifee.

func (*Mempool) GetAttestationsToInclude

func (m *Mempool) GetAttestationsToInclude(slot uint64, c *config.Config) ([]primitives.Attestation, error)

GetAttestationsToInclude gets attestations to include in a block.

func (*Mempool) ProcessNewAttestation

func (m *Mempool) ProcessNewAttestation(att primitives.Attestation) error

ProcessNewAttestation processes a new attestation to be included in a block.

func (*Mempool) RemoveAttestationsFromBitfield

func (m *Mempool) RemoveAttestationsFromBitfield(slot uint64, shard uint64, bitfield []uint8)

RemoveAttestationsFromBitfield removes attestations that have already been included.

type StateManager

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

StateManager handles all state transitions, storing of states for different forks, and time-based state updates.

func NewStateManager

func NewStateManager(c *config.Config, genesisTime uint64, blockchain *Blockchain, db db.Database) (*StateManager, error)

NewStateManager creates a new state manager.

func (*StateManager) AddBlockToStateMap

func (sm *StateManager) AddBlockToStateMap(block *primitives.Block, verifySignature bool) ([]primitives.Receipt, *primitives.State, error)

AddBlockToStateMap processes the block and adds it to the state map.

func (*StateManager) DeleteStateBeforeFinalizedSlot

func (sm *StateManager) DeleteStateBeforeFinalizedSlot(finalizedSlot uint64) error

DeleteStateBeforeFinalizedSlot deletes any states before the current finalized slot.

func (*StateManager) GetGenesisTime

func (sm *StateManager) GetGenesisTime() uint64

GetGenesisTime gets the time of the genesis slot.

func (*StateManager) GetStateForHash

func (sm *StateManager) GetStateForHash(blockHash chainhash.Hash) (*primitives.State, bool)

GetStateForHash gets the state for a certain block Hash.

func (*StateManager) GetStateForHashAtSlot added in v0.2.4

func (sm *StateManager) GetStateForHashAtSlot(blockHash chainhash.Hash, slot uint64, view primitives.BlockView, c *config.Config) (*primitives.State, error)

GetStateForHashAtSlot gets the state derived from a certain block Hash at a given slot.

func (*StateManager) SetBlockState

func (sm *StateManager) SetBlockState(blockHash chainhash.Hash, state *primitives.State) error

SetBlockState sets the state for a certain block. This SHOULD ONLY BE USED FOR THE GENESIS BLOCK!

type SyncManager

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

SyncManager is responsible for requesting blocks from clients and determining whether the client should be considered in sync (for attached validators).

The basic algorithm for syncing is as follows: func onReceive(newBlock):

    if haveBlock(newBlock):
        processBlock(newBlock)
    else:
        locator = generateLocator(chain) - locator starts from tip and goes back exponentially including the genesis block
				 askForBlocks(locator)

func NewSyncManager

func NewSyncManager(hostNode *p2p.HostNode, blockchain *Blockchain) SyncManager

NewSyncManager creates a new sync manager

func (SyncManager) Connected

func (s SyncManager) Connected() bool

Connected returns whether the client should be considered connected to the network.

func (SyncManager) ListenForBlocks

func (s SyncManager) ListenForBlocks() error

ListenForBlocks listens for new blocks over the pub-sub network being broadcast as a result of finding them.

func (*SyncManager) RegisterPostProcessHook added in v0.1.2

func (s *SyncManager) RegisterPostProcessHook(hook func(*primitives.Block, *primitives.State, []primitives.Receipt))

RegisterPostProcessHook registers a hook called after a block has been processed.

func (SyncManager) Start

func (s SyncManager) Start()

Start starts the sync manager by registering message handlers

func (SyncManager) TryInitialSync

func (s SyncManager) TryInitialSync()

TryInitialSync tries to select a peer to sync with and start downloading blocks.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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