mining

package
v0.0.0-...-cf10ea2 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2023 License: ISC Imports: 22 Imported by: 0

README

mining

Build Status ISC License Doc

Overview

This package is currently a work in progress.

License

Package mining is licensed under the copyfree ISC License.

Documentation

Overview

Package mining includes all mining and policy types, and will house all mining code in the future.

It is currently a work in progress.

Index

Constants

View Source
const (
	// ErrNotEnoughVoters indicates that there were not enough voters to
	// build a block on top of HEAD.
	ErrNotEnoughVoters = ErrorKind("ErrNotEnoughVoters")

	// ErrFailedToGetGeneration specifies that the current generation for
	// a block could not be obtained from blockchain.
	ErrFailedToGetGeneration = ErrorKind("ErrFailedToGetGeneration")

	// ErrGetTopBlock indicates that the current top block of the
	// blockchain could not be obtained.
	ErrGetTopBlock = ErrorKind("ErrGetTopBlock")

	// ErrGettingDifficulty indicates that there was an error getting the
	// PoW difficulty.
	ErrGettingDifficulty = ErrorKind("ErrGettingDifficulty")

	// ErrTransactionAppend indicates there was a problem adding a msgtx
	// to a msgblock.
	ErrTransactionAppend = ErrorKind("ErrTransactionAppend")

	// ErrTicketExhaustion indicates that there will not be enough mature
	// tickets by the end of the next ticket maturity period to progress the
	// chain.
	ErrTicketExhaustion = ErrorKind("ErrTicketExhaustion")

	// ErrCheckConnectBlock indicates that a newly created block template
	// failed blockchain.CheckConnectBlock.
	ErrCheckConnectBlock = ErrorKind("ErrCheckConnectBlock")

	// ErrFraudProofIndex indicates that there was an error finding the index
	// for a fraud proof.
	ErrFraudProofIndex = ErrorKind("ErrFraudProofIndex")

	// ErrFetchTxStore indicates a transaction store failed to fetch.
	ErrFetchTxStore = ErrorKind("ErrFetchTxStore")

	// ErrCalcCommitmentRoot indicates that creating the header commitments and
	// calculating the associated commitment root for a newly created block
	// template failed.
	ErrCalcCommitmentRoot = ErrorKind("ErrCalcCommitmentRoot")

	// ErrGetTicketInfo indicates that ticket information could not be retreived
	// in order to connect a transaction.
	ErrGetTicketInfo = ErrorKind("ErrGetTicketInfo")

	// ErrSerializeHeader indicates an attempt to serialize a block header failed.
	ErrSerializeHeader = ErrorKind("ErrSerializeHeader")
)

These constants are used to identify a specific RuleError.

View Source
const (
	// MinHighPriority is the minimum priority value that allows a
	// transaction to be considered high priority.
	MinHighPriority = dcrutil.AtomsPerCoin * 144.0 / 250
)
View Source
const (
	// UnminedHeight is the height used for the "block" height field of the
	// contextual transaction information provided in a transaction store
	// when it has not yet been mined into a block.
	UnminedHeight = 0x7fffffff
)

Variables

This section is empty.

Functions

func CalcPriority

func CalcPriority(tx *wire.MsgTx, prioInputs PriorityInputser, nextBlockHeight int64) float64

CalcPriority returns a transaction priority given a transaction and the sum of each of its input values multiplied by their age (# of confirmations). Thus, the final formula for the priority is: sum(inputValue * inputAge) / adjustedTxSize

func SortParentsByVotes

func SortParentsByVotes(txSource TxSource, currentTopBlock chainhash.Hash, blocks []chainhash.Hash, params *chaincfg.Params) []chainhash.Hash

SortParentsByVotes takes a list of block header hashes and sorts them by the number of votes currently available for them in the votes map of mempool. It then returns all blocks that are eligible to be used (have at least a majority number of votes) sorted by number of votes, descending.

This function is safe for concurrent access.

func UseLogger

func UseLogger(logger slog.Logger)

UseLogger uses a specified Logger to output package logging info.

Types

type BgBlkTmplConfig

type BgBlkTmplConfig struct {
	// TemplateGenerator specifies the generator to use when generating the
	// block templates.
	TemplateGenerator *BlkTmplGenerator

	// MiningAddrs specifies the addresses to choose from when paying mining
	// rewards in generated templates.
	MiningAddrs []stdaddr.Address

	// AllowUnsyncedMining indicates block templates should be created even when
	// the chain is not fully synced.
	AllowUnsyncedMining bool

	// IsCurrent defines the function to use to determine whether or not the
	// chain is current (synced).
	IsCurrent func() bool
}

BgBlkTmplConfig holds the configuration options related to the background block template generator.

type BgBlkTmplGenerator

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

BgBlkTmplGenerator provides facilities for asynchronously generating block templates in response to various relevant events and allowing clients to subscribe for updates when new templates are generated as well as access the most recently-generated template in a concurrency-safe manner.

An example of some of the events that trigger a new block template to be generated are modifications to the current best chain, receiving relevant votes, and periodic timeouts to allow inclusion of new transactions.

The templates are generated based on a given block template generator instance which itself is based on a given mining policy and transaction source. See the NewBlockTemplate method for a detailed description of how the block template is generated.

The background generation makes use of three main goroutines -- a regen event queue to allow asynchronous non-blocking signalling, a regen event handler to process the aforementioned queue and react accordingly, and a subscriber notification controller. In addition, the templates themselves are generated in their own goroutines with a cancellable context.

A high level overview of the semantics are as follows:

  • Ignore all vote handling when prior to stake validation height
  • Generate templates building on the current tip at startup with a fall back to generate a template on its parent if the current tip does not receive enough votes within a timeout
  • Continue monitoring for votes on any blocks that extend said parent to potentially switch to them and generate a template building on them when possible
  • Generate new templates building on new best chain tip blocks once they have received the minimum votes after a timeout to provide the additional votes an opportunity to propagate, except when it is an intermediate block in a chain reorganization
  • In the event the current tip fails to receive the minimum number of required votes, monitor side chain blocks which are siblings of it for votes in order to potentially switch to them and generate a template building on them when possible
  • Generate new templates on blocks disconnected from the best chain tip, except when it is an intermediate block in a chain reorganization
  • Generate new templates periodically when there are new regular transactions to include
  • Bias templates towards building on the first seen block when possible in order to prevent PoW miners from being able to gain an advantage through vote withholding
  • Schedule retries in the rare event template generation fails
  • Allow clients to subscribe for updates every time a new template is successfully generated along with a reason why it was generated
  • Provide direct access to the most-recently generated template
  • Block while generating new templates that will make the current template stale (e.g. new parent or new votes)

func NewBgBlkTmplGenerator

func NewBgBlkTmplGenerator(cfg *BgBlkTmplConfig) *BgBlkTmplGenerator

NewBgBlkTmplGenerator initializes a background block template generator with the provided parameters. The returned instance must be started with the Run method to allowing processing.

func (*BgBlkTmplGenerator) BestSnapshot

func (g *BgBlkTmplGenerator) BestSnapshot() *blockchain.BestState

func (*BgBlkTmplGenerator) BlockAccepted

func (g *BgBlkTmplGenerator) BlockAccepted(block *dcrutil.Block)

BlockAccepted informs the background block template generator that a block has been accepted to the block chain. It is caller's responsibility to ensure this is only invoked as described.

This function is safe for concurrent access.

func (*BgBlkTmplGenerator) BlockConnected

func (g *BgBlkTmplGenerator) BlockConnected(block *dcrutil.Block)

BlockConnected informs the background block template generator that a block has been connected to the main chain. It is caller's responsibility to ensure this is only invoked as described.

This function is safe for concurrent access.

func (*BgBlkTmplGenerator) BlockDisconnected

func (g *BgBlkTmplGenerator) BlockDisconnected(block *dcrutil.Block)

BlockDisconnected informs the background block template generator that a block has been disconnected from the main chain. It is caller's responsibility to ensure this is only invoked as described.

This function is safe for concurrent access.

func (*BgBlkTmplGenerator) ChainReorgDone

func (g *BgBlkTmplGenerator) ChainReorgDone()

ChainReorgDone informs the background block template generator that a chain reorganization has completed. It is caller's responsibility to ensure this is only invoked as described.

func (*BgBlkTmplGenerator) ChainReorgStarted

func (g *BgBlkTmplGenerator) ChainReorgStarted()

ChainReorgStarted informs the background block template generator that a chain reorganization has started. It is caller's responsibility to ensure this is only invoked as described.

func (*BgBlkTmplGenerator) CurrentTemplate

func (g *BgBlkTmplGenerator) CurrentTemplate() (*BlockTemplate, error)

CurrentTemplate returns the current template associated with the background template generator along with any associated error.

NOTE: The returned template and block that it contains MUST be treated as immutable since they are shared by all callers.

NOTE: The returned template might be nil even if there is no error. It is the responsibility of the caller to properly handle nil templates.

This function is safe for concurrent access.

func (*BgBlkTmplGenerator) ForceRegen

func (g *BgBlkTmplGenerator) ForceRegen()

ForceRegen asks the background block template generator to generate a new template, independently of most of its internal timers.

Note that there is no guarantee on whether a new template will actually be generated or when. This function does _not_ block until a new template is generated.

This function is safe for concurrent access.

func (*BgBlkTmplGenerator) Run

func (g *BgBlkTmplGenerator) Run(ctx context.Context)

Run starts the background block template generator and all other goroutines necessary for it to function properly and blocks until the provided context is cancelled.

func (*BgBlkTmplGenerator) Subscribe

func (g *BgBlkTmplGenerator) Subscribe() *TemplateSubscription

Subscribe subscribes a client for block template updates. The returned template subscription contains functions to retrieve a channel that produces the stream of block templates and to stop the stream when the caller no longer wishes to receive new templates.

The current template associated with the background block template generator, if any, is immediately sent to the returned subscription stream.

func (*BgBlkTmplGenerator) TxSource

func (g *BgBlkTmplGenerator) TxSource() TxSource

func (*BgBlkTmplGenerator) UpdateBlockTime

func (g *BgBlkTmplGenerator) UpdateBlockTime(header *wire.BlockHeader) error

UpdateBlockTime updates the timestamp in the passed header to the current time while taking into account the median time of the last several blocks to ensure the new time is after that time per the chain consensus rules.

Finally, it will update the target difficulty if needed based on the new time for the test networks since their target difficulty can change based upon time.

func (*BgBlkTmplGenerator) VoteReceived

func (g *BgBlkTmplGenerator) VoteReceived(tx *dcrutil.Tx)

VoteReceived informs the background block template generator that a new vote has been received. It is the caller's responsibility to ensure this is only invoked with valid votes.

This function is safe for concurrent access.

type BlkTmplGenerator

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

BlkTmplGenerator generates block templates based on a given mining policy and a transactions source. It also houses additional state required in order to ensure the templates adhere to the consensus rules and are built on top of the best chain tip or its parent if the best chain tip is unable to get enough votes.

See the NewBlockTemplate method for a detailed description of how the block template is generated.

func NewBlkTmplGenerator

func NewBlkTmplGenerator(cfg *Config) *BlkTmplGenerator

NewBlkTmplGenerator returns a new block template generator for the given policy using transactions from the provided transaction source.

func (*BlkTmplGenerator) NewBlockTemplate

func (g *BlkTmplGenerator) NewBlockTemplate(payToAddress stdaddr.Address) (*BlockTemplate, error)

NewBlockTemplate returns a new block template that is ready to be solved using the transactions from the passed transaction source pool and a coinbase that either pays to the passed address if it is not nil, or a coinbase that is redeemable by anyone if the passed address is nil. The nil address functionality is useful since there are cases such as the getblocktemplate RPC where external mining software is responsible for creating their own coinbase which will replace the one generated for the block template. Thus the need to have configured address can be avoided.

The transactions selected and included are prioritized according to several factors. First, each transaction has a priority calculated based on its value, age of inputs, and size. Transactions which consist of larger amounts, older inputs, and small sizes have the highest priority. Second, a fee per kilobyte is calculated for each transaction. Transactions with a higher fee per kilobyte are preferred. Finally, the block generation related policy settings are all taken into account.

Transactions which only spend outputs from other transactions already in the block chain are immediately added to a priority queue which either prioritizes based on the priority (then fee per kilobyte) or the fee per kilobyte (then priority) depending on whether or not the BlockPrioritySize policy setting allots space for high-priority transactions. Transactions which spend outputs from other transactions in the source pool are added to a dependency map so they can be added to the priority queue once the transactions they depend on have been included.

Once the high-priority area (if configured) has been filled with transactions, or the priority falls below what is considered high-priority, the priority queue is updated to prioritize by fees per kilobyte (then priority).

When the fees per kilobyte drop below the TxMinFreeFee policy setting, the transaction will be skipped unless the BlockMinSize policy setting is nonzero, in which case the block will be filled with the low-fee/free transactions until the block size reaches that minimum size.

Any transactions which would cause the block to exceed the BlockMaxSize policy setting, exceed the maximum allowed signature operations per block, or otherwise cause the block to be invalid are skipped.

Given the above, a block generated by this function is of the following form:

 -----------------------------------  --  --
|      Coinbase Transaction         |   |   |
|-----------------------------------|   |   |
|                                   |   |   | ----- policy.BlockPrioritySize
|   High-priority Transactions      |   |   |
|                                   |   |   |
|-----------------------------------|   | --
|                                   |   |
|                                   |   |
|                                   |   |--- (policy.BlockMaxSize) / 2
|  Transactions prioritized by fee  |   |
|  until <= policy.TxMinFreeFee     |   |
|                                   |   |
|                                   |   |
|                                   |   |
|-----------------------------------|   |
|  Low-fee/Non high-priority (free) |   |
|  transactions (while block size   |   |
|  <= policy.BlockMinSize)          |   |
 -----------------------------------  --

Which also includes a stake tree that looks like the following:

 -----------------------------------  --  --
|                                   |   |   |
|             Votes                 |   |   | --- >= (chaincfg.TicketsPerBlock/2) + 1
|                                   |   |   |
|-----------------------------------|   | --
|                                   |   |   |
|            Tickets                |   |   | --- <= chaincfg.MaxFreshStakePerBlock
|                                   |   |   |
|-----------------------------------|   | --
|                                   |   |
|          Revocations              |   |
|                                   |   |
 -----------------------------------  --

This function returns nil, nil if there are not enough voters on any of
the current top blocks to create a new block template.

func (*BlkTmplGenerator) UpdateBlockTime

func (g *BlkTmplGenerator) UpdateBlockTime(header *wire.BlockHeader) error

UpdateBlockTime updates the timestamp in the passed header to the current time while taking into account the median time of the last several blocks to ensure the new time is after that time per the chain consensus rules.

Finally, it will update the target difficulty if needed based on the new time for the test networks since their target difficulty can change based upon time.

type BlockTemplate

type BlockTemplate struct {
	// Block is a block that is ready to be solved by miners.  Thus, it is
	// completely valid with the exception of satisfying the proof-of-work
	// requirement.
	Block *wire.MsgBlock

	// Fees contains the amount of fees each transaction in the generated
	// template pays in base units.  Since the first transaction is the
	// coinbase, the first entry (offset 0) will contain the negative of the
	// sum of the fees of all other transactions.
	Fees []int64

	// SigOpCounts contains the number of signature operations each
	// transaction in the generated template performs.
	SigOpCounts []int64

	// Height is the height at which the block template connects to the main
	// chain.
	Height int64

	// ValidPayAddress indicates whether or not the template coinbase pays
	// to an address or is redeemable by anyone.  See the documentation on
	// NewBlockTemplate for details on which this can be useful to generate
	// templates without a coinbase payment address.
	ValidPayAddress bool
}

BlockTemplate houses a block that has yet to be solved along with additional details about the fees and the number of signature operations for each transaction in the block.

type Config

type Config struct {
	// Policy houses the policy (configuration parameters) which is used to control
	// the generation of block templates.
	Policy *Policy

	// TxSource represents a source of transactions to consider for inclusion in
	// new blocks.
	TxSource TxSource

	// TimeSource defines the median time source which is used to retrieve the
	// current time adjusted by the median time offset.  This is used when setting
	// the timestamp in the header of new blocks.
	TimeSource blockchain.MedianTimeSource

	// SubsidyCache defines a subsidy cache to use when calculating and validating
	// block and vote subsidies.
	SubsidyCache *standalone.SubsidyCache

	// ChainParams identifies which chain parameters should be used while
	// generating block templates.
	ChainParams *chaincfg.Params

	// MiningTimeOffset defines the number of seconds to offset the mining
	// timestamp of a block by (positive values are in the past).
	MiningTimeOffset int

	// BestSnapshot defines the function to use to access information about the
	// current best block.  The returned instance should be treated as immutable.
	BestSnapshot func() *blockchain.BestState

	// BlockByHash defines the function to use to search the internal chain block
	// stores and the database in an attempt to find the requested block and return
	// it.  This function should return blocks regardless of whether or not they
	// are part of the main chain.
	BlockByHash func(hash *chainhash.Hash) (*dcrutil.Block, error)

	// CalcNextRequiredDifficulty defines the function to use to calculate the
	// required difficulty for the block after the given block based on the
	// difficulty retarget rules.
	CalcNextRequiredDifficulty func(hash *chainhash.Hash, timestamp time.Time) (uint32, error)

	// CalcStakeVersionByHash defines the function to use to calculate the expected
	// stake version for the block AFTER the provided block hash.
	CalcStakeVersionByHash func(hash *chainhash.Hash) (uint32, error)

	// CheckConnectBlockTemplate defines the function to use to fully validate that
	// connecting the passed block to either the tip of the main chain or its
	// parent does not violate any consensus rules, aside from the proof of work
	// requirement.
	CheckConnectBlockTemplate func(block *dcrutil.Block) error

	// CheckTicketExhaustion defines the function to use to ensure that extending
	// the block associated with the provided hash with a block that contains the
	// specified number of ticket purchases will not result in a chain that is
	// unrecoverable due to inevitable ticket exhaustion.  This scenario happens
	// when the number of live tickets drops below the number of tickets that is
	// needed to reach the next block at which any outstanding immature ticket
	// purchases that would provide the necessary live tickets mature.
	CheckTicketExhaustion func(hash *chainhash.Hash, ticketPurchases uint8) error

	// CheckTransactionInputs defines the function to use to perform a series of
	// checks on the inputs to a transaction to ensure they are valid.
	CheckTransactionInputs func(tx *dcrutil.Tx, txHeight int64,
		view *blockchain.UtxoViewpoint, checkFraudProof bool,
		prevHeader *wire.BlockHeader, isTreasuryEnabled,
		isAutoRevocationsEnabled, isSubsidyEnabled bool) (int64, error)

	// CountSigOps defines the function to use to count the number of signature
	// operations for all transaction input and output scripts in the provided
	// transaction.
	CountSigOps func(tx *dcrutil.Tx, isCoinBaseTx bool, isSSGen bool, isTreasuryEnabled bool) int

	// FetchUtxoEntry defines the function to use to load and return the requested
	// unspent transaction output from the point of view of the main chain tip.
	//
	// NOTE: Requesting an output for which there is no data will NOT return an
	// error.  Instead both the entry and the error will be nil.  This is done to
	// allow pruning of spent transaction outputs.  In practice this means the
	// caller must check if the returned entry is nil before invoking methods on
	// it.
	//
	// This function is safe for concurrent access however the returned entry (if
	// any) is NOT.
	FetchUtxoEntry func(outpoint wire.OutPoint) (*blockchain.UtxoEntry, error)

	// FetchUtxoView defines the function to use to fetch unspent transaction
	// output information.  The returned instance should be treated as immutable.
	FetchUtxoView func(tx *dcrutil.Tx, includeRegularTxns bool) (*blockchain.UtxoViewpoint, error)

	// FetchUtxoViewParentTemplate defines the function to use to fetch unspent
	// transaction output information from the point of view of just having
	// connected the given block, which must be a block template that connects to
	// the parent of the tip of the main chain.  In other words, the given block
	// must be a sibling of the current tip of the main chain.
	//
	// This should typically only be used by mining code when it is unable to
	// generate a template that extends the current tip due to being unable to
	// acquire the minimum required number of votes to extend it.
	//
	// The returned instance should be treated as immutable.
	FetchUtxoViewParentTemplate func(block *wire.MsgBlock) (*blockchain.UtxoViewpoint, error)

	// ForceHeadReorganization defines the function to use to force a
	// reorganization of the block chain to the block hash requested, so long as
	// it matches up with the current organization of the best chain.
	ForceHeadReorganization func(formerBest chainhash.Hash, newBest chainhash.Hash) error

	// HeaderByHash returns the block header identified by the given hash or an
	// error if it doesn't exist.  Note that this will return headers from both
	// the main chain and any side chains.
	HeaderByHash func(hash *chainhash.Hash) (wire.BlockHeader, error)

	// IsFinalizedTransaction defines the function to use to determine whether or
	// not a transaction is finalized.
	IsFinalizedTransaction func(tx *dcrutil.Tx, blockHeight int64, blockTime time.Time) bool

	// IsHeaderCommitmentsAgendaActive defines the function to use to determine
	// whether or not the header commitments agenda is active or not for the block
	// AFTER the given block.
	IsHeaderCommitmentsAgendaActive func(prevHash *chainhash.Hash) (bool, error)

	// IsTreasuryAgendaActive defines the function to use to determine if the
	// treasury agenda is active or not for the block AFTER the given block.
	IsTreasuryAgendaActive func(prevHash *chainhash.Hash) (bool, error)

	// IsAutoRevocationsAgendaActive defines the function to use to determine if
	// the automatic ticket revocations agenda is active or not for the block
	// AFTER the given block.
	IsAutoRevocationsAgendaActive func(prevHash *chainhash.Hash) (bool, error)

	// IsSubsidySplitAgendaActive defines the function to use to determine if
	// the modified subsidy split agenda is active or not for the block AFTER
	// the given block.
	IsSubsidySplitAgendaActive func(prevHash *chainhash.Hash) (bool, error)

	// NewUtxoViewpoint defines the function to use to create a new empty unspent
	// transaction output view.
	NewUtxoViewpoint func() *blockchain.UtxoViewpoint

	// TipGeneration defines the function to use to get the entire generation of
	// blocks stemming from the parent of the current tip.
	TipGeneration func() ([]chainhash.Hash, error)

	// ValidateTransactionScripts defines the function to use to validate the
	// scripts for the passed transaction.
	ValidateTransactionScripts func(tx *dcrutil.Tx,
		utxoView *blockchain.UtxoViewpoint, flags txscript.ScriptFlags,
		isAutoRevocationsEnabled bool) error
}

Config is a descriptor containing the mining configuration.

type Error

type Error struct {
	Err         error
	Description string
}

Error identifies a mining rule rule violation. It has full support for errors.Is and errors.As, so the caller can ascertain the specific reason for the error by checking the underlying error. It is used to indicate that processing of a block or transaction failed due to one of the many validation rules.

func (Error) Error

func (e Error) Error() string

Error satisfies the error interface and prints human-readable errors.

func (Error) Unwrap

func (e Error) Unwrap() error

Unwrap returns the underlying wrapped error.

type ErrorKind

type ErrorKind string

ErrorKind identifies a kind of error. It has full support for errors.Is and errors.As, so the caller can directly check against an error kind when determining the reason for an error.

func (ErrorKind) Error

func (e ErrorKind) Error() string

Error satisfies the error interface and prints human-readable errors.

type Policy

type Policy struct {
	// BlockMinSize is the minimum block size in bytes to be used when
	// generating a block template.
	BlockMinSize uint32

	// BlockMaxSize is the maximum block size in bytes to be used when
	// generating a block template.
	BlockMaxSize uint32

	// BlockPrioritySize is the size in bytes for high-priority / low-fee
	// transactions to be used when generating a block template.
	BlockPrioritySize uint32

	// TxMinFreeFee is the minimum fee in Atoms/1000 bytes that is
	// required for a transaction to be treated as free for mining purposes
	// (block template generation).
	TxMinFreeFee dcrutil.Amount

	AggressiveMining bool

	// StandardVerifyFlags defines the function to retrieve the flags to
	// use for verifying scripts for the block after the current best block.
	// It must set the verification flags properly depending on the result
	// of any agendas that affect them.
	//
	// This function must be safe for concurrent access.
	StandardVerifyFlags func() (txscript.ScriptFlags, error)
}

Policy houses the policy (configuration parameters) which is used to control the generation of block templates. See the documentation for NewBlockTemplate for more details on each of these parameters are used.

type PriorityInputser

type PriorityInputser interface {
	PriorityInput(prevOut *wire.OutPoint) (blockHeight int64, amount int64, ok bool)
}

PriorityInputser defines an interface that provides access to information about an transaction output needed to calculate a priority based on the input age of a transaction. It is used within this package as a generic means to provide the block heights and amounts referenced by all of the inputs to a transaction that are needed to calculate an input age. The boolean return indicates whether or not the information for the provided outpoint was found.

type TemplateNtfn

type TemplateNtfn struct {
	Template *BlockTemplate
	Reason   TemplateUpdateReason
}

TemplateNtfn represents a notification of a new template along with the reason it was generated. It is sent to subscribers on the channel obtained from the TemplateSubscription instance returned by Subscribe.

type TemplateSubscription

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

TemplateSubscription defines a subscription to receive block template updates from the background block template generator. The caller must call Stop on the subscription when it is no longer needed to free resources.

NOTE: Notifications are dropped to make up for slow receivers to ensure notifications to other subscribers, as well as senders, are not blocked indefinitely. Since templates are typically only generated infrequently and receives must fall several templates behind before new ones are dropped, this should not affect callers in practice, however, if a caller wishes to guarantee that no templates are being dropped, they will need to ensure the channel is always processed quickly.

func (*TemplateSubscription) C

func (s *TemplateSubscription) C() <-chan *TemplateNtfn

C returns a channel that produces a stream of block templates as each new template is generated. Successive calls to C return the same channel.

NOTE: Notifications are dropped to make up for slow receivers. See the template subscription type documentation for more details.

func (*TemplateSubscription) Stop

func (s *TemplateSubscription) Stop()

Stop prevents any future template updates from being delivered and unsubscribes the associated subscription.

NOTE: The channel is not closed to prevent a read from the channel succeeding incorrectly.

type TemplateUpdateReason

type TemplateUpdateReason int

TemplateUpdateReason represents the type of a reason why a template is being updated.

const (
	// TURNewParent indicates the associated template has been updated because
	// it builds on a new block as compared to the previous template.
	TURNewParent TemplateUpdateReason = iota

	// TURNewVotes indicates the associated template has been updated because a
	// new vote for the block it builds on has been received.
	TURNewVotes

	// TURNewTxns indicates the associated template has been updated because new
	// non-vote transactions are available and have potentially been included.
	TURNewTxns
)

Constants for the type of template update reasons.

type TxAncestorStats

type TxAncestorStats struct {
	// Fees is the sum of all fees of unconfirmed ancestors.
	Fees int64

	// SizeBytes is the total size of all unconfirmed ancestors.
	SizeBytes int64

	// TotalSigOps is the total number of signature operations of all ancestors.
	TotalSigOps int

	// NumAncestors is the total number of ancestors for a given transaction.
	NumAncestors int

	// NumDescendants is the total number of descendants that have ancestor
	// statistics tracked for a given transaction.
	NumDescendants int
}

TxAncestorStats is a descriptor that stores aggregated statistics for the unconfirmed ancestors of a transaction.

type TxDesc

type TxDesc struct {
	// Tx is the transaction associated with the entry.
	Tx *dcrutil.Tx

	// Type is the type of the transaction associated with the entry.
	Type stake.TxType

	// Added is the time when the entry was added to the source pool.
	Added time.Time

	// Height is the block height when the entry was added to the source
	// pool.
	Height int64

	// Fee is the total fee the transaction associated with the entry pays.
	Fee int64

	// TotalSigOps is the total signature operations for this transaction.
	TotalSigOps int

	// TxSize is the size of the transaction.
	TxSize int64
}

TxDesc is a descriptor about a transaction in a transaction source along with additional metadata.

type TxDescFind

type TxDescFind func(txHash *chainhash.Hash) *TxDesc

TxDescFind is used to inject a transaction repository into the mining view. This might either come from the mempool's outpoint map or from the mining view itself.

type TxMiningView

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

TxMiningView represents a snapshot of all transactions, as well as the hierarchy of transactions, that are ready to be mined.

func NewTxMiningView

func NewTxMiningView(enableAncestorTracking bool,
	forEachRedeemer func(tx *dcrutil.Tx, f func(redeemerTx *TxDesc))) *TxMiningView

NewTxMiningView creates a new mining view instance. The forEachRedeemer parameter should define the function to be used for finding transactions that spend a given transaction.

func (*TxMiningView) AddTransaction

func (mv *TxMiningView) AddTransaction(txDesc *TxDesc, findTx TxDescFind)

AddTransaction inserts a TxDesc into the mining view if it has a parent or child in the view, or has a relationship with another transaction through findTx.

This function is NOT safe for concurrent access.

func (*TxMiningView) AncestorStats

func (mv *TxMiningView) AncestorStats(txHash *chainhash.Hash) (*TxAncestorStats, bool)

AncestorStats returns the view's cached statistics for all of the provided transaction's ancestors.

This function is NOT safe for concurrent access.

func (*TxMiningView) Clone

func (mv *TxMiningView) Clone(txDescs []*TxDesc, fetchTx TxDescFind) *TxMiningView

Clone makes a deep copy of the mining view and underlying transaction graph. fetchTx is the locator functon that is used to find transactions that should be included in the mining view.

This function is NOT safe for concurrent access.

func (*TxMiningView) RemoveTransaction

func (mv *TxMiningView) RemoveTransaction(txHash *chainhash.Hash, updateDescendantStats bool)

RemoveTransaction stops tracking the transaction in the mining view if it exists. If updateDescendantStats is true, then the statistics for all descendant transactions are updated to account for the removal of an ancestor.

This function is NOT safe for concurrent access.

func (*TxMiningView) TxDescs

func (mv *TxMiningView) TxDescs() []*TxDesc

TxDescs returns a collection of all transactions available in the view.

type TxSource

type TxSource interface {
	// LastUpdated returns the last time a transaction was added to or
	// removed from the source pool.
	LastUpdated() time.Time

	// HaveTransaction returns whether or not the passed transaction hash
	// exists in the source pool.
	HaveTransaction(hash *chainhash.Hash) bool

	// HaveAllTransactions returns whether or not all of the passed
	// transaction hashes exist in the source pool.
	HaveAllTransactions(hashes []chainhash.Hash) bool

	// VoteHashesForBlock returns the hashes for all votes on the provided
	// block hash that are currently available in the source pool.
	VoteHashesForBlock(hash *chainhash.Hash) []chainhash.Hash

	// VotesForBlocks returns a slice of vote descriptors for all votes on
	// the provided block hashes that are currently available in the source
	// pool.
	VotesForBlocks(hashes []chainhash.Hash) [][]VoteDesc

	// IsRegTxTreeKnownDisapproved returns whether or not the regular
	// transaction tree of the block represented by the provided hash is
	// known to be disapproved according to the votes currently in the
	// source pool.
	IsRegTxTreeKnownDisapproved(hash *chainhash.Hash) bool

	// MiningView returns a snapshot of the underlying TxSource.
	MiningView() *TxMiningView
}

TxSource represents a source of transactions to consider for inclusion in new blocks.

The interface contract requires that all of these methods are safe for concurrent access with respect to the source.

type VoteDesc

type VoteDesc struct {
	VoteHash       chainhash.Hash
	TicketHash     chainhash.Hash
	ApprovesParent bool
}

VoteDesc is a descriptor about a vote transaction in a transaction source along with additional metadata.

Directories

Path Synopsis
Package cpuminer provides facilities for solving blocks (mining) using the CPU.
Package cpuminer provides facilities for solving blocks (mining) using the CPU.

Jump to

Keyboard shortcuts

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