node

package
v0.0.0-...-8b83558 Latest Latest
Warning

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

Go to latest
Published: Mar 22, 2025 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Overview

Package node handles peer-to-peer networking for blockchain nodes. It provides functions for sharing blocks, transactions, and peer information, as well as requesting data from other peers.

Package node contains the blockchain node state and API.

Index

Constants

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

QueryLastest is a special value that represents a request for the latest block. When used as the "from" or "to" parameter in QueryBlocksByNumber, it is replaced with the number of the current latest block.

Variables

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

ErrNoTx signals that the mempool is empty.

Functions

func ReadOrCreatePrivateKey

func ReadOrCreatePrivateKey(
	ctx context.Context,
	filePath string,
) (
	*ecdsa.PrivateKey,
	error,
)

ReadOrCreatePrivateKey returns an ECDSA private key by reading from the given file path. If the file does not exist, it generates a new key, saves it, and returns the key.

Types

type Config

type Config struct {
	Ctx         context.Context
	Host        string
	Beneficiary database.AccountID
	Storage     database.Storage
	Genesis     genesis.Genesis
	Peers       *peer.PeerSet
}

type Node

type Node struct {
	Beneficiary database.AccountID

	Worker Worker
	// contains filtered or unexported fields
}

func New

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

func (*Node) Accounts

func (node *Node) Accounts() map[database.AccountID]database.Account

Accounts returns the accounts in the database.

func (*Node) BroadcastInfo

func (node *Node) BroadcastInfo()

BroadcastInfo shares this node's peer information with all known peers. It sends the current node's host information via POST to each peer's "/peers" endpoint.

func (*Node) Genesis

func (node *Node) Genesis() *genesis.Genesis

Genesis returns the genesis information.

func (*Node) GetAccount

func (node *Node) GetAccount(account database.AccountID) database.Account

GetAccount returns a copy of the account from the database.

func (*Node) Host

func (node *Node) Host() string

Host returns the host of the node.

func (*Node) ID

func (node *Node) ID() database.AccountID

ID returns the unique node identifier.

func (*Node) IsMiningAllowed

func (s *Node) IsMiningAllowed() bool

IsMiningAllowed determines if mining operations are allowed currently. This might be turned off if the blockchain needs to be re-synced.

func (*Node) LatestBlock

func (node *Node) LatestBlock() database.Block

LatestBlock returns a copy the current latest block.

func (*Node) Mempool

func (node *Node) Mempool() []database.BlockTx

Mempool returns a copy of the mempool.

func (*Node) MempoolLength

func (node *Node) MempoolLength() int

MempoolLength returns the current length of the mempool.

func (*Node) MempoolTxs

func (node *Node) MempoolTxs() map[string]database.BlockTx

MempoolTxs returns the mempool transactions.

func (*Node) MineBlock

func (node *Node) MineBlock(ctx context.Context) (database.Block, error)

MineBlock creates and mines a new block from mempool transactions.

func (*Node) PeerAdd

func (node *Node) PeerAdd(peer peer.Peer) bool

PeerAdd adds a new peer to the list of known peers.

func (*Node) PeerList

func (node *Node) PeerList() []peer.Peer

PeerList returns a list of the known external peers.

func (*Node) PeerRemove

func (node *Node) PeerRemove(peer peer.Peer)

PeerRemove removes a peer from the known peers. This can happen if the peer node is faulty.

func (*Node) ProcessProposedBlock

func (node *Node) ProcessProposedBlock(block database.Block) error

ProcessProposedBlock validates and adds a proposed block to the blockchain.

func (*Node) QueryAccount

func (node *Node) QueryAccount(account database.AccountID) database.Account

QueryAccount retrieves a copy of the specified account from the blockchain database.

func (*Node) QueryBlocksByAccount

func (node *Node) QueryBlocksByAccount(
	accountID database.AccountID,
) (
	[]database.Block, error,
)

QueryBlocksByAccount scans the blockchain for blocks that involve the given account. A block is included if any transaction in the block has the account as sender or receiver. If an empty account ID is provided, all blocks will be returned.

func (*Node) QueryBlocksByNumber

func (node *Node) QueryBlocksByNumber(from uint64, to uint64) []database.Block

QueryBlocksByNumber retrieves blocks from the blockchain within the given range [from, to]. If either 'from' or 'to' equals QueryLastest, it is replaced with the latest block number.

func (*Node) ReSync

func (node *Node) ReSync() error

ReSync resets and resyncs the blockchain state. This is required to fix a blockchain fork.

func (*Node) RequestPeerBlocks

func (node *Node) RequestPeerBlocks(p peer.Peer) error

RequestPeerBlocks requests new blocks from a given peer starting from the block following the node's latest block. It sends an HTTP GET request to the peer's "/block/list/{from}/latest" endpoint and processes each returned block using ProcessProposedBlock.

func (*Node) RequestPeerMempool

func (node *Node) RequestPeerMempool(p peer.Peer) ([]database.BlockTx, error)

RequestPeerMempool requests the mempool (list of transactions) from a given peer. It sends an HTTP GET request to the peer's "/tx/list" endpoint and decodes the response into a slice of BlockTx. Returns an error if the request fails.

func (*Node) RequestPeerStatus

func (node *Node) RequestPeerStatus(p peer.Peer) (peer.PeerStatus, error)

RequestPeerStatus requests status information from a given peer. It sends an HTTP GET request to the peer's "/status" endpoint and decodes the response into a PeerStatus struct. If the request fails, an error is returned.

func (*Node) ShareBlock

func (node *Node) ShareBlock(block database.Block) error

ShareBlock sends the given block to all known peers. It converts the block to its hex representation and serializes the block data, then POSTs it to each peer's "/block/propose" endpoint. Errors for individual peers are logged and skipped.

func (*Node) ShareTx

func (node *Node) ShareTx(tx database.BlockTx) error

ShareTx sends the given transaction to all known peers. It converts the transaction to its hex representation and POSTs it to each peer's "/tx/submit" endpoint. If sending fails for a peer, the error is logged and the process continues. Note: In this implementation the entire transaction is sent.

func (*Node) Shutdown

func (node *Node) Shutdown()

Shutdown signals to shutdown the node.

func (*Node) SubmitBtx

func (node *Node) SubmitBtx(tx database.BlockTx) error

SubmitBtx takes a block transaction and submits it to the mempool. It is intended to be used by other nodes to share a transaction that they accepted.

func (*Node) SubmitStx

func (node *Node) SubmitStx(stx database.SignedTx) error

SubmitStx submits a new transaction to the mempool. It is intended to be used by users/wallets to submit a new transaction.

func (*Node) UpsertMempool

func (node *Node) UpsertMempool(tx database.BlockTx) error

UpsertMempool adds a new transaction to the mempool.

type Worker

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

Jump to

Keyboard shortcuts

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