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
- Variables
- func ReadOrCreatePrivateKey(ctx context.Context, filePath string) (*ecdsa.PrivateKey, error)
- type Config
- type Node
- func (node *Node) Accounts() map[database.AccountID]database.Account
- func (node *Node) BroadcastInfo()
- func (node *Node) Genesis() *genesis.Genesis
- func (node *Node) GetAccount(account database.AccountID) database.Account
- func (node *Node) Host() string
- func (node *Node) ID() database.AccountID
- func (s *Node) IsMiningAllowed() bool
- func (node *Node) LatestBlock() database.Block
- func (node *Node) Mempool() []database.BlockTx
- func (node *Node) MempoolLength() int
- func (node *Node) MempoolTxs() map[string]database.BlockTx
- func (node *Node) MineBlock(ctx context.Context) (database.Block, error)
- func (node *Node) PeerAdd(peer peer.Peer) bool
- func (node *Node) PeerList() []peer.Peer
- func (node *Node) PeerRemove(peer peer.Peer)
- func (node *Node) ProcessProposedBlock(block database.Block) error
- func (node *Node) QueryAccount(account database.AccountID) database.Account
- func (node *Node) QueryBlocksByAccount(accountID database.AccountID) ([]database.Block, error)
- func (node *Node) QueryBlocksByNumber(from uint64, to uint64) []database.Block
- func (node *Node) ReSync() error
- func (node *Node) RequestPeerBlocks(p peer.Peer) error
- func (node *Node) RequestPeerMempool(p peer.Peer) ([]database.BlockTx, error)
- func (node *Node) RequestPeerStatus(p peer.Peer) (peer.PeerStatus, error)
- func (node *Node) ShareBlock(block database.Block) error
- func (node *Node) ShareTx(tx database.BlockTx) error
- func (node *Node) Shutdown()
- func (node *Node) SubmitBtx(tx database.BlockTx) error
- func (node *Node) SubmitStx(stx database.SignedTx) error
- func (node *Node) UpsertMempool(tx database.BlockTx) error
- type Worker
Constants ¶
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 ¶
var ErrNoTx = errors.New("no transactions in mempool")
ErrNoTx signals that the mempool is empty.
Functions ¶
func ReadOrCreatePrivateKey ¶
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 Node ¶
type Node struct {
Beneficiary database.AccountID
Worker Worker
// contains filtered or unexported fields
}
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) GetAccount ¶
GetAccount returns a copy of the account from the database.
func (*Node) IsMiningAllowed ¶
IsMiningAllowed determines if mining operations are allowed currently. This might be turned off if the blockchain needs to be re-synced.
func (*Node) LatestBlock ¶
LatestBlock returns a copy the current latest block.
func (*Node) MempoolLength ¶
MempoolLength returns the current length of the mempool.
func (*Node) MempoolTxs ¶
MempoolTxs returns the mempool transactions.
func (*Node) PeerRemove ¶
PeerRemove removes a peer from the known peers. This can happen if the peer node is faulty.
func (*Node) ProcessProposedBlock ¶
ProcessProposedBlock validates and adds a proposed block to the blockchain.
func (*Node) QueryAccount ¶
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 ¶
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 ¶
ReSync resets and resyncs the blockchain state. This is required to fix a blockchain fork.
func (*Node) RequestPeerBlocks ¶
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 ¶
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 ¶
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 ¶
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 ¶
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) SubmitBtx ¶
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.