mining

package
v0.0.0-...-7ee7fde Latest Latest
Warning

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

Go to latest
Published: Jun 28, 2020 License: Apache-2.0 Imports: 18 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// BlockHeaderOverhead is the max number of bytes it takes to serialize
	// a block header and max possible transaction count.
	BlockHeaderOverhead = protos.BlockHeaderPayload + serialization.MaxVarIntPayload

	// CoinbaseFlags is added to the coinbase script of a generated block.
	CoinbaseFlags = "/P2SH/asimovd/"

	// Init status of minging source tx
	MiningTxInit = 1 << iota

	// Processed status of minging source tx
	MiningTxProcessed
)
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 CreateCoinbaseTx

func CreateCoinbaseTx(params *chaincfg.Params, nextBlockHeight int32, addr common.IAddress,
	contractOut *protos.TxOut) (*asiutil.Tx, *protos.TxOut, error)

CreateCoinbaseTx returns a coinbase transaction paying an appropriate subsidy based on the passed block height to the provided address. When the address is nil, the coinbase transaction will instead be redeemable by anyone.

See the comment for NewBlockTemplate for more information about why the nil address handling is useful.

func NewTxPriorityQueue

func NewTxPriorityQueue(reserve int) *txPriorityQueue

NewTxPriorityQueue returns a new transaction priority queue that reserves the passed amount of space for the elements. The new priority queue uses either the txPQByPriority or the txPQByFee compare function depending on the sortByFee parameter and is already initialized for use with heap.Push/Pop. The priority queue can grow larger than the reserved space, but extra copies of the underlying array can be avoided by reserving a sane value.

func StandardCoinbaseScript

func StandardCoinbaseScript(nextBlockHeight int32, extraNonce uint64) ([]byte, error)

StandardCoinbaseScript returns a standard script suitable for use as the signature script of the coinbase transaction of a new block. In particular, it starts with the block height that is required by version 2 blocks and adds the extra nonce as well as additional coinbase flags.

Types

type BlkTmplGenerator

type BlkTmplGenerator struct {
	FetchUtxoView func(tx *asiutil.Tx, dolock bool) (*txo.UtxoViewpoint, error)
	// contains filtered or unexported fields
}

BlkTmplGenerator provides a type that can be used to generate block templates based on a given mining policy and source of transactions to choose from. It also houses additional state required in order to ensure the templates are built on top of the current best chain and adhere to the consensus rules.

func NewBlkTmplGenerator

func NewBlkTmplGenerator(policy *Policy,
	txSource TxSource, sigSource SigSource, chain *blockchain.BlockChain) *BlkTmplGenerator

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

The additional state-related fields are required in order to ensure the templates are built on top of the current best chain and adhere to the consensus rules.

func (*BlkTmplGenerator) ProduceNewBlock

func (g *BlkTmplGenerator) ProduceNewBlock(account *crypto.Account, gasFloor, gasCeil uint64,
	blockTime int64,
	round uint32, slotIndex uint16, blockInterval float64) (
	blockTemplate *BlockTemplate, err error)

ProduceNewBlock 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.

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).

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

 -----------------------------------  --  --
|                                   |   |
|                                   |   |
|                                   |   |
|  Transactions prioritized by price|   |
|                                   |   |
|-----------------------------------| --|
|      Coinbase Transaction         |   |
 -----------------------------------  --

type BlockTemplate

type BlockTemplate struct {
	// Block is a block that is ready to be processed, it can't be mined or
	// passed from other nodes.
	Block *asiutil.Block

	// VBlock is a virtual block that is ready to be processed, it is mined.
	VBlock *asiutil.VBlock

	Receipts types.Receipts

	Logs []*types.Log
}

BlockTemplate houses a block and a relation data including a virtual block, reciepts, logs.

type Policy

type Policy struct {
	// TxMinPrice is the minimum price in Xing per byte that is
	// required for a transaction to be treated as free for mining purposes
	// (block template generation).
	TxMinPrice float64

	// BlockProductedTimeOut limits a block producing time.
	// It is the maximum percent (default 0.5) of producing block interval.
	BlockProductedTimeOut float64

	// TxConnectTimeOut limits a tx connecting time, include executing vm.
	// It is the maximum percent (default 0.7) of producing block producing
	// interval.
	TxConnectTimeOut float64

	// UtxoValidateTimeOut limits source txs' utxo validating time.
	// It is the maximum percent (default 0.35) of producing block interval.
	UtxoValidateTimeOut float64

	// BlockSyncTime is the time for synchronizing a block.
	// The default value is 1000 ms.
	BlockSyncTime float64
}

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 SigDesc

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

type SigSource

type SigSource interface {
	MiningDescs(height int32) []*asiutil.BlockSign
}

type TxDesc

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

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

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

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

	// FeeList is the list of all asset fee with the entry pays.
	FeeList *map[protos.Asset]int64

	// GasPrice is the price of fee the transaction pays.
	// GasPrice = fee / (size * common.GasPerByte + gaslimit)
	GasPrice float64

	// UtxoFetchCount is count number of utxo validation when producing
	// new block for this txdesc
	UtxoFetchCount float64
}

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

type TxDescList

type TxDescList []*TxDesc

A list of TxDesc, this type is only used for sort.

func (TxDescList) Len

func (l TxDescList) Len() int

Len returns the number of items in the list.

func (TxDescList) Less

func (l TxDescList) Less(i, j int) bool

Less returns whether the item in the list with index i should sort before the item with index j by great GasPrice / UtxoFetchCount.

func (TxDescList) Swap

func (l TxDescList) Swap(i, j int)

Swap swaps the items at the passed indices in the list.

type TxPrioItem

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

TxPrioItem houses a transaction along with extra information that allows the transaction to be prioritized and track dependencies on other transactions which have not been mined into a block yet.

type TxSource

type TxSource interface {
	// MiningDescs returns a slice of mining descriptors for all the
	// transactions in the source pool.
	TxDescs() TxDescList

	// UpdateForbiddenTxs put given txhashes into forbiddenTxs.
	// If size of forbiddenTxs exceed limit, clear some olders.
	UpdateForbiddenTxs(txHashes []*common.Hash, height int64)
}

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.

Jump to

Keyboard shortcuts

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