state

package
v0.0.0-...-297edb8 Latest Latest
Warning

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

Go to latest
Published: Feb 25, 2024 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 algorithms that can be used.

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

QueryLatest represents to query the latest block in the chain.

Variables

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

ErrNoTransactions is returned when there are no transactions

Functions

This section is empty.

Types

type Config

type Config struct {
	BeneficiaryID  database.AccountID
	Host           string
	Storage        database.Storage
	Genesis        genesis.Genesis
	KnownPeers     *peer.PeerSet
	SelectStrategy string
	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 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 adds a new peer to the known peer list.

func (*State) Consensus

func (s *State) Consensus() string

Consensus returns a copy of the used consensus alforithm.

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) 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. Used by the PoA selection algorithm.

func (*State) LatestBlock

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

LatestBlock returns a copy of 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 number of transactions in 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(p peer.Peer) error

NetRequestPeerBlocks queries the specified node for blocks this node does not have and writes them to disk

func (*State) NetRequestPeerMempool

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

NetRequestPeerMempool asks the peer for the transactions in its mempool.

func (*State) NetRequestPeerStatus

func (s *State) NetRequestPeerStatus(p 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 take the new mined block and sends it to all the known 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, if valid, 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) QueryBlocksByNumber

func (s *State) QueryBlocksByNumber(from, 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 removes a peer from the known peer list.

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 adds a transaction to the mempool.

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