state

package
v0.0.0-...-c97bdb9 Latest Latest
Warning

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

Go to latest
Published: Dec 29, 2022 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

Package state is the core API for the blockchain and implements all the business rules and processing.

Index

Constants

View Source
const (
	ConsensusPOW = "POW"
	ConsensusPOA = "POA"
)

The set of different consensus protocols that can be used.

View Source
const QueryLastest = ^uint64(0) >> 1

QueryLastest represents to query the latest block in the chain.

Variables

View Source
var ErrNoTransactions = errors.New("no transactions in mempool")

ErrNoTransactions is returned when a block is requested to be created and there are not enough transactions.

Functions

This section is empty.

Types

type Config

type Config struct {
	BeneficiaryID  database.AccountID
	Host           string
	Storage        database.Storage
	Genesis        genesis.Genesis
	SelectStrategy string
	KnownPeers     *peer.PeerSet
	EvHandler      EventHandler
	Consensus      string
}

Config represents the configuration required to start the blockchain node.

type EventHandler

type EventHandler func(v string, args ...any)

EventHandler defines a function that is called when events occur in the processing of persisting blocks.

type State

type State struct {
	Worker Worker
	// contains filtered or unexported fields
}

State manages the blockchain database.

func New

func New(cfg Config) (*State, error)

New constructs a new blockchain for data management.

func (*State) Accounts

func (s *State) Accounts() map[database.AccountID]database.Account

Accounts returns a copy of the database accounts.

func (*State) AddKnownPeer

func (s *State) AddKnownPeer(peer peer.Peer) bool

AddKnownPeer provides the ability to add a new peer to the known peer list.

func (*State) Consensus

func (s *State) Consensus() string

Consensus returns a copy of consensus algorithm being used.

func (*State) Genesis

func (s *State) Genesis() genesis.Genesis

Genesis returns a copy of the genesis information.

func (*State) Host

func (s *State) Host() string

Host returns a copy of host information.

func (*State) IsMiningAllowed

func (s *State) IsMiningAllowed() bool

IsMiningAllowed identifies if we are allowed to mine blocks. This might be turned off if the blockchain needs to be re-synced.

func (*State) KnownExternalPeers

func (s *State) KnownExternalPeers() []peer.Peer

KnownExternalPeers retrieves a copy of the known peer list without including this node.

func (*State) KnownPeers

func (s *State) KnownPeers() []peer.Peer

KnownPeers retrieves a copy of the full known peer list which includes this node as well. Used by the PoA selection algorithm.

func (*State) LatestBlock

func (s *State) LatestBlock() database.Block

LatestBlock returns a copy the current latest block.

func (*State) Mempool

func (s *State) Mempool() []database.BlockTx

Mempool returns a copy of the mempool.

func (*State) MempoolLength

func (s *State) MempoolLength() int

MempoolLength returns the current length of the mempool.

func (*State) MineNewBlock

func (s *State) MineNewBlock(ctx context.Context) (database.Block, error)

MineNewBlock attempts to create a new block with a proper hash that can become the next block in the chain.

func (*State) NetRequestPeerBlocks

func (s *State) NetRequestPeerBlocks(pr peer.Peer) error

NetRequestPeerBlocks queries the specified node asking for blocks this node does not have, then writes them to disk.

func (*State) NetRequestPeerMempool

func (s *State) NetRequestPeerMempool(pr peer.Peer) ([]database.BlockTx, error)

NetRequestPeerMempool asks the peer for the transactions in their mempool.

func (*State) NetRequestPeerStatus

func (s *State) NetRequestPeerStatus(pr peer.Peer) (peer.PeerStatus, error)

NetRequestPeerStatus looks for new nodes on the blockchain by asking known nodes for their peer list. New nodes are added to the list.

func (*State) NetSendBlockToPeers

func (s *State) NetSendBlockToPeers(block database.Block) error

NetSendBlockToPeers takes the new mined block and sends it to all know peers.

func (*State) NetSendNodeAvailableToPeers

func (s *State) NetSendNodeAvailableToPeers()

NetSendNodeAvailableToPeers shares this node is available to participate in the network with the known peers.

func (*State) NetSendTxToPeers

func (s *State) NetSendTxToPeers(tx database.BlockTx)

NetSendTxToPeers shares a new block transaction with the known peers.

func (*State) ProcessProposedBlock

func (s *State) ProcessProposedBlock(block database.Block) error

ProcessProposedBlock takes a block received from a peer, validates it and if that passes, adds the block to the local blockchain.

func (*State) QueryAccount

func (s *State) QueryAccount(account database.AccountID) (database.Account, error)

QueryAccount returns a copy of the account from the database.

func (*State) QueryBlocksByAccount

func (s *State) QueryBlocksByAccount(accountID database.AccountID) ([]database.Block, error)

QueryBlocksByAccount returns the set of blocks by account. If the account is empty, all blocks are returned. This function reads the blockchain from disk first.

func (*State) QueryBlocksByNumber

func (s *State) QueryBlocksByNumber(from uint64, to uint64) []database.Block

QueryBlocksByNumber returns the set of blocks based on block numbers. This function reads the blockchain from disk first.

func (*State) RemoveKnownPeer

func (s *State) RemoveKnownPeer(peer peer.Peer)

RemoveKnownPeer provides the ability to remove a peer from the known peer list.

func (*State) Reorganize

func (s *State) Reorganize() error

Reorganize corrects an identified fork. No mining is allowed to take place while this process is running. New transactions can be placed into the mempool.

func (*State) Shutdown

func (s *State) Shutdown() error

Shutdown cleanly brings the node down.

func (*State) UpsertMempool

func (s *State) UpsertMempool(tx database.BlockTx) error

UpsertMempool adds a new transaction to the mempool.

func (*State) UpsertNodeTransaction

func (s *State) UpsertNodeTransaction(tx database.BlockTx) error

UpsertNodeTransaction accepts a transaction from a node for inclusion.

func (*State) UpsertWalletTransaction

func (s *State) UpsertWalletTransaction(signedTx database.SignedTx) error

UpsertWalletTransaction accepts a transaction from a wallet for inclusion.

type Worker

type Worker interface {
	Shutdown()
	Sync()
	SignalStartMining()
	SignalCancelMining()
	SignalShareTx(blockTx database.BlockTx)
}

Worker interface represents the behavior required to be implemented by any package providing support for mining, peer updates, and transaction sharing.

Jump to

Keyboard shortcuts

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