mempool

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: May 22, 2024 License: ISC Imports: 35 Imported by: 1

Documentation

Overview

Copyright (c) 2017-2018 The qitmeer developers Copyright (c) 2013-2016 The btcsuite developers Copyright (c) 2017-2018 The Decred developers Use of this source code is governed by an ISC license that can be found in the LICENSE file.

Copyright (c) 2017-2018 The qitmeer developers Copyright (c) 2013-2016 The btcsuite developers Copyright (c) 2017-2018 The Decred developers Use of this source code is governed by an ISC license that can be found in the LICENSE file.

Copyright (c) 2017-2018 The qitmeer developers Copyright (c) 2013-2016 The btcsuite developers Copyright (c) 2017-2018 The Decred developers Use of this source code is governed by an ISC license that can be found in the LICENSE file.

Copyright (c) 2017-2022 The qitmeer developers Copyright (c) 2013-2016 The btcsuite developers Copyright (c) 2017-2018 The Decred developers Use of this source code is governed by an ISC license that can be found in the LICENSE file.

Copyright (c) 2017-2018 The qitmeer developers Copyright (c) 2013-2016 The btcsuite developers Copyright (c) 2017-2018 The Decred developers Use of this source code is governed by an ISC license that can be found in the LICENSE file.

Copyright (c) 2017-2018 The qitmeer developers Copyright (c) 2013-2016 The btcsuite developers Copyright (c) 2017-2018 The Decred developers Use of this source code is governed by an ISC license that can be found in the LICENSE file.

Index

Constants

View Source
const (

	// DefaultEstimateFeeMaxRollback is the default number of rollbacks
	// allowed by the fee estimator for orphaned blocks.
	DefaultEstimateFeeMaxRollback = 2

	// DefaultEstimateFeeMinRegisteredBlocks is the default minimum
	// number of blocks which must be observed by the fee estimator before
	// it will provide fee estimations.
	DefaultEstimateFeeMinRegisteredBlocks = 3

	// UnminedHeight is the height used for the "block" main chain height field of the
	// contextual transaction information provided in a transaction store
	// when it has not yet been mined into a block.
	UnminedHeight = 0x7fffffff
)
View Source
const (
	MempoolFileName = "mempool"
	MempoolVersion  = 0x01
)
View Source
const (
	// DefaultBlockPrioritySize is the default size in bytes for high-
	// priority / low-fee transactions.  It is used to help determine which
	// are allowed into the mempool and consequently affects their relay and
	// inclusion when generating block templates.
	DefaultBlockPrioritySize = 20000

	// DefaultMinRelayTxFee is the minimum fee in atoms that is required for
	// a transaction to be treated as free.
	// It is also used to help determine if a transaction is considered dust
	// and as a base for calculating minimum required fees for larger
	// transactions.  This value is in Atom Qitmeer/kB. The default value is
	// 10000 Atoms/kB (aka. 0.0001 Qitmeer/kB)
	DefaultMinRelayTxFee = int64(1e4)

	// BaseStandardVerifyFlags defines the script flags that should be used
	// when executing transaction scripts to enforce additional checks which
	// are required for the script to be considered standard regardless of
	// the state of any agenda votes.  The full set of standard verification
	// flags must include these flags as well as any additional flags that
	// are conditionally enabled depending on the result of agenda votes.
	BaseStandardVerifyFlags = txscript.ScriptBip16 |
		txscript.ScriptVerifyDERSignatures |
		txscript.ScriptVerifyStrictEncoding |
		txscript.ScriptVerifyMinimalData |
		txscript.ScriptDiscourageUpgradableNops |
		txscript.ScriptVerifyCleanStack |
		txscript.ScriptVerifyCheckLockTimeVerify |
		txscript.ScriptVerifyCheckSequenceVerify |
		txscript.ScriptVerifyLowS

	// UnminedLayer is the layer used for the "block" layer field of the
	// contextual transaction information provided in a transaction store
	// when it has not yet been mined into a block.
	UnminedLayer = 0x7fffffff

	// MinHighPriority is the minimum priority value that allows a
	// transaction to be considered high priority.
	MinHighPriority = types.AtomsPerCoin * 144.0 / 250
)
View Source
const (

	//TODO, refactor config item
	DefaultMaxOrphanTxSize = 5000
)

Variables

This section is empty.

Functions

func CalcFee added in v1.2.0

func CalcFee(serializedSize int64, minRelayTxFee types.Amount) int64

func CalcPriority

func CalcPriority(tx *types.Transaction, utxoView *utxo.UtxoViewpoint, nextBlockHeight uint64, bc *blockchain.BlockChain) 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 ErrToRejectErr

func ErrToRejectErr(err error) (message.RejectCode, string)

ErrToRejectErr examines the underlying type of the error and returns a reject code and string appropriate to be sent in a wire.MsgReject message.

func StandardScriptVerifyFlags added in v1.2.0

func StandardScriptVerifyFlags() (txscript.ScriptFlags, error)

standardScriptVerifyFlags returns the script flags that should be used when executing transaction scripts to enforce additional checks which are required for the script to be considered standard. Note these flags are different than what is required for the consensus rules in that they are more strict.

func UseLogger

func UseLogger(logger l.Logger)

UseLogger uses a specified Logger to output package logging info.

Types

type Config

type Config struct {
	// Policy defines the various mempool configuration options related
	// to policy.
	Policy Policy

	// ChainParams identifies which chain parameters the txpool is
	// associated with.
	ChainParams *params.Params

	// FetchUtxoView defines the function to use to fetch unspent
	// transaction output information.
	FetchUtxoView func(*types.Tx) (*utxo.UtxoViewpoint, error)

	// BlockByHash defines the function use to fetch the block identified
	// by the given hash.
	BlockByHash func(*hash.Hash) (*types.SerializedBlock, error)

	// BestHash defines the function to use to access the block hash of
	// the current best chain.
	BestHash func() *hash.Hash

	// BestHeight defines the function to use to access the block height of
	// the current best chain.
	BestHeight func() uint64

	// PastMedianTime defines the function to use in order to access the
	// median time calculated from the point-of-view of the current chain
	// tip within the best chain.
	PastMedianTime func() time.Time

	// CalcSequenceLock defines the function to use in order to generate
	// the current sequence lock for the given transaction using the passed
	// utxo view.
	CalcSequenceLock func(*types.Tx, *utxo.UtxoViewpoint) (*blockchain.SequenceLock, error)

	// SubsidyCache defines a subsidy cache to use.
	SubsidyCache *blockchain.SubsidyCache

	// SigCache defines a signature cache to use.
	SigCache *txscript.SigCache

	IndexManager *index.Manager

	// block chain
	BC *blockchain.BlockChain

	// Data Directory
	DataDir string

	// mempool expiry
	Expiry time.Duration

	// persist mempool
	Persist bool

	//  no mempool bar
	NoMempoolBar bool

	Events *event.Feed

	// FeeEstimatator provides a feeEstimator. If it is not nil, the mempool
	// records all new transactions it observes into the feeEstimator.
	FeeEstimator *FeeEstimator
}

Config is a descriptor containing the memory pool configuration.

type FeeEstimator

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

FeeEstimator manages the data necessary to create fee estimations. It is safe for concurrent access.

func NewFeeEstimator

func NewFeeEstimator(maxRollback, minRegisteredBlocks uint32) *FeeEstimator

NewFeeEstimator creates a FeeEstimator for which at most maxRollback blocks can be unregistered and which returns an error unless minRegisteredBlocks have been registered with it.

func RestoreFeeEstimator

func RestoreFeeEstimator(data FeeEstimatorState) (*FeeEstimator, error)

RestoreFeeEstimator takes a FeeEstimatorState that was previously returned by Save and restores it to a FeeEstimator

func (*FeeEstimator) EstimateFee

func (ef *FeeEstimator) EstimateFee(numBlocks uint32) (MeerPerKilobyte, error)

EstimateFee estimates the fee per byte to have a tx confirmed a given number of blocks from now.

func (*FeeEstimator) LastKnownHeight

func (ef *FeeEstimator) LastKnownHeight() int32

LastKnownHeight returns the height of the last block which was registered.

func (*FeeEstimator) ObserveTransaction

func (ef *FeeEstimator) ObserveTransaction(t *TxDesc)

ObserveTransaction is called when a new transaction is observed in the mempool.

func (*FeeEstimator) RegisterBlock

func (ef *FeeEstimator) RegisterBlock(block *types.SerializedBlock, mainheight uint) error

RegisterBlock informs the fee estimator of a new block to take into account.

func (*FeeEstimator) Rollback

func (ef *FeeEstimator) Rollback(hash *hash.Hash) error

Rollback unregisters a recently registered block from the FeeEstimator. This can be used to reverse the effect of an orphaned block on the fee estimator. The maximum number of rollbacks allowed is given by maxRollbacks.

Note: not everything can be rolled back because some transactions are deleted if they have been observed too long ago. That means the result of Rollback won't always be exactly the same as if the last block had not happened, but it should be close enough.

func (*FeeEstimator) Save

func (ef *FeeEstimator) Save() FeeEstimatorState

Save records the current state of the FeeEstimator to a []byte that can be restored later.

type FeeEstimatorState

type FeeEstimatorState []byte

FeeEstimatorState represents a saved FeeEstimator that can be restored with data from an earlier session of the program.

type MeerPerKilobyte

type MeerPerKilobyte float64

type MempoolTxData

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

func (*MempoolTxData) Decode

func (mtd *MempoolTxData) Decode(bs []byte) (int, error)

decode

func (*MempoolTxData) Encode

func (mtd *MempoolTxData) Encode(w io.Writer) error

encode

type Policy

type Policy struct {
	// MaxTxVersion is the max transaction version that the mempool should
	// accept.  All transactions above this version are rejected as
	// non-standard.
	MaxTxVersion uint16

	// DisableRelayPriority defines whether to relay free or low-fee
	// transactions that do not have enough priority to be relayed.
	DisableRelayPriority bool

	// AcceptNonStd defines whether to accept and relay non-standard
	// transactions to the network. If true, non-standard transactions
	// will be accepted into the mempool and relayed to the rest of the
	// network. Otherwise, all non-standard transactions will be rejected.
	AcceptNonStd bool

	// FreeTxRelayLimit defines the given amount in thousands of bytes
	// per minute that transactions with no fee are rate limited to.
	FreeTxRelayLimit float64

	// MaxOrphanTxs is the maximum number of orphan transactions
	// that can be queued.
	MaxOrphanTxs int

	// MaxOrphanTxSize is the maximum size allowed for orphan transactions.
	// This helps prevent memory exhaustion attacks from sending a lot of
	// of big orphans.
	MaxOrphanTxSize int

	// MaxSigOpsPerTx is the maximum number of signature operations
	// in a single transaction we will relay or mine.  It is a fraction
	// of the max signature operations for a block.
	MaxSigOpsPerTx int

	// MinRelayTxFee defines the minimum transaction fee in AtomQitmeer/kB
	MinRelayTxFee types.Amount

	// TxTimeScope is the allow tx time scope with server time
	TxTimeScope int64

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

	// max mempool tx size
	MaxTxSize int64
}

Policy houses the policy (configuration parameters) which is used to control the mempool.

type PublicMempoolAPI

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

func NewPublicMempoolAPI

func NewPublicMempoolAPI(txPool *TxPool) *PublicMempoolAPI

func (*PublicMempoolAPI) CleanMempool added in v1.0.21

func (api *PublicMempoolAPI) CleanMempool() (interface{}, error)

func (*PublicMempoolAPI) EstimateFee

func (api *PublicMempoolAPI) EstimateFee(numBlocks int64) (interface{}, error)

func (*PublicMempoolAPI) GetMempool

func (api *PublicMempoolAPI) GetMempool(txType *string, verbose bool) (interface{}, error)

func (*PublicMempoolAPI) GetMempoolCount

func (api *PublicMempoolAPI) GetMempoolCount() (interface{}, error)

func (*PublicMempoolAPI) SaveMempool

func (api *PublicMempoolAPI) SaveMempool() (interface{}, error)

type QitPerByte

type QitPerByte float64

func NewQitPerByte

func NewQitPerByte(fee types.Amount, size uint32) QitPerByte

func (QitPerByte) Fee

func (rate QitPerByte) Fee(size uint32) types.Amount

func (QitPerByte) ToMeerPerKb

func (rate QitPerByte) ToMeerPerKb() MeerPerKilobyte

type RuleError

type RuleError struct {
	Err error
}

RuleError identifies a rule violation. It is used to indicate that processing of a transaction failed due to one of the many validation rules. The caller can use type assertions to determine if a failure was specifically due to a rule violation and use the Err field to access the underlying error, which will be either a TxRuleError or a blockchain.RuleError.

func (RuleError) Error

func (e RuleError) Error() string

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

type TxDesc

type TxDesc struct {
	types.TxDesc

	// StartingPriority is the priority of the transaction when it was added
	// to the pool.
	StartingPriority float64
}

TxDesc is a descriptor containing a transaction in the mempool along with additional metadata.

type TxPool

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

TxPool is used as a source of transactions that need to be mined into blocks and relayed to other peers. It is safe for concurrent access from multiple peers.

func New

func New(cfg *Config) *TxPool

New returns a new memory pool for validating and storing standalone transactions until they are mined into a block.

func (*TxPool) API

func (t *TxPool) API() api.API

func (*TxPool) AddTransaction

func (mp *TxPool) AddTransaction(tx *types.Tx, height uint64, fee int64)

Call addTransaction

func (*TxPool) AddUnconfirmedTx added in v1.0.19

func (mp *TxPool) AddUnconfirmedTx(tx *types.Tx, utxoView *utxo.UtxoViewpoint)

func (*TxPool) CleanDirty added in v1.2.0

func (mp *TxPool) CleanDirty()

func (*TxPool) Count

func (mp *TxPool) Count() int

Count returns the number of transactions in the main pool. It does not include the orphan pool.

This function is safe for concurrent access.

func (*TxPool) Dirty added in v1.2.0

func (mp *TxPool) Dirty() bool

func (*TxPool) DoPruneExpiredTx added in v1.2.0

func (mp *TxPool) DoPruneExpiredTx()

pruneExpiredTx prunes expired transactions from the mempool that may no longer be able to be included into a block.

This function MUST be called with the mempool lock held (for writes).

func (*TxPool) FetchTransaction

func (mp *TxPool) FetchTransaction(txHash *hash.Hash) (*types.Tx, error)

FetchTransaction returns the requested transaction from the transaction pool. This only fetches from the main transaction pool and does not include orphans.

This function is safe for concurrent access.

func (*TxPool) FetchTransactions

func (mp *TxPool) FetchTransactions(txHashs []*hash.Hash) ([]*types.Tx, error)

func (*TxPool) GetConfig

func (mp *TxPool) GetConfig() *Config

func (*TxPool) GetMainHeight

func (mp *TxPool) GetMainHeight() int64

func (*TxPool) HaveAllTransactions

func (mp *TxPool) HaveAllTransactions(hashes []hash.Hash) bool

HaveAllTransactions returns whether or not all of the passed transaction hashes exist in the mempool.

This function is safe for concurrent access.

func (*TxPool) HaveTransaction

func (mp *TxPool) HaveTransaction(hash *hash.Hash) bool

HaveTransaction returns whether or not the passed transaction already exists in the main pool or in the orphan pool.

This function is safe for concurrent access.

func (*TxPool) HaveTransactionUTXO added in v1.0.21

func (mp *TxPool) HaveTransactionUTXO(hash *hash.Hash) bool

func (*TxPool) IsOrphanInPool

func (mp *TxPool) IsOrphanInPool(hash *hash.Hash) bool

IsOrphanInPool returns whether or not the passed transaction already exists in the orphan pool.

This function is safe for concurrent access.

func (*TxPool) IsPersist

func (mp *TxPool) IsPersist() bool

func (*TxPool) IsSupportVMTx

func (mp *TxPool) IsSupportVMTx() bool

func (*TxPool) IsTransactionInPool

func (mp *TxPool) IsTransactionInPool(hash *hash.Hash, all bool) bool

IsTransactionInPool returns whether or not the passed transaction already exists in the main pool.

This function is safe for concurrent access.

func (*TxPool) LastUpdated

func (mp *TxPool) LastUpdated() time.Time

LastUpdated returns the last time a transaction was added to or removed from the main pool. It does not include the orphan pool.

This function is safe for concurrent access.

func (*TxPool) Load

func (mp *TxPool) Load() error

func (*TxPool) MaybeAcceptTransaction

func (mp *TxPool) MaybeAcceptTransaction(tx *types.Tx, isNew, rateLimit bool) ([]*hash.Hash, error)

MaybeAcceptTransaction is the main workhorse for handling insertion of new free-standing transactions into a memory pool. It includes functionality such as rejecting duplicate transactions, ensuring transactions follow all rules, orphan transaction handling, and insertion into the memory pool. The isOrphan parameter can be nil if the caller does not need to know whether or not the transaction is an orphan.

This function is safe for concurrent access.

func (*TxPool) MiningDescs

func (mp *TxPool) MiningDescs() []*types.TxDesc

MiningDescs returns a slice of mining descriptors for all the transactions in the pool.

This is part of the mining.TxSource interface implementation and is safe for concurrent access as required by the interface contract.

func (*TxPool) Perisit

func (mp *TxPool) Perisit() (int, error)

func (*TxPool) ProcessOrphans

func (mp *TxPool) ProcessOrphans(tx *types.Tx) []*types.TxDesc

ProcessOrphans determines if there are any orphans which depend on the passed transaction hash (it is possible that they are no longer orphans) and potentially accepts them to the memory pool. It repeats the process for the newly accepted transactions (to detect further orphans which may no longer be orphans) until there are no more.

It returns a slice of transactions added to the mempool. A nil slice means no transactions were moved from the orphan pool to the mempool.

This function is safe for concurrent access.

func (*TxPool) ProcessTransaction

func (mp *TxPool) ProcessTransaction(tx *types.Tx, allowOrphan, rateLimit, allowHighFees bool) ([]*types.TxDesc, error)

ProcessTransaction is the main workhorse for handling insertion of new free-standing transactions into the memory pool. It includes functionality such as rejecting duplicate transactions, ensuring transactions follow all rules, orphan transaction handling, and insertion into the memory pool.

It returns a slice of transactions added to the mempool. When the error is nil, the list will include the passed transaction itself along with any additional orphan transaactions that were added as a result of the passed one being accepted.

This function is safe for concurrent access.

func (*TxPool) PruneDirty added in v1.2.0

func (mp *TxPool) PruneDirty() bool

func (*TxPool) PruneExpiredTx

func (mp *TxPool) PruneExpiredTx()

PruneExpiredTx prunes expired transactions from the mempool that may no longer be able to be included into a block.

This function is safe for concurrent access.

func (*TxPool) RemoveDoubleSpends

func (mp *TxPool) RemoveDoubleSpends(tx *types.Tx)

RemoveDoubleSpends removes all transactions which spend outputs spent by the passed transaction from the memory pool. Removing those transactions then leads to removing all transactions which rely on them, recursively. This is necessary when a block is connected to the main chain because the block may contain transactions which were previously unknown to the memory pool.

This function is safe for concurrent access.

func (*TxPool) RemoveOrphan

func (mp *TxPool) RemoveOrphan(tx *types.Tx)

RemoveOrphan removes the passed orphan transaction from the orphan pool and previous orphan index.

This function is safe for concurrent access.

func (*TxPool) RemoveTransaction

func (mp *TxPool) RemoveTransaction(tx *types.Tx, removeRedeemers bool)

RemoveTransaction removes the passed transaction from the mempool. When the removeRedeemers flag is set, any transactions that redeem outputs from the removed transaction will also be removed recursively from the mempool, as they would otherwise become orphans.

This function is safe for concurrent access.

func (*TxPool) Save

func (mp *TxPool) Save() (int, error)

func (*TxPool) TxDescs

func (mp *TxPool) TxDescs() []*TxDesc

TxDescs returns a slice of descriptors for all the transactions in the pool. The descriptors are to be treated as read only.

This function is safe for concurrent access.

type TxRuleError

type TxRuleError struct {
	RejectCode  message.RejectCode // The code to send with reject messages
	Description string             // Human readable description of the issue
}

TxRuleError identifies a rule violation. It is used to indicate that processing of a transaction failed due to one of the many validation rules. The caller can use type assertions to determine if a failure was specifically due to a rule violation and access the ErrorCode field to ascertain the specific reason for the rule violation.

func (TxRuleError) Error

func (e TxRuleError) Error() string

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

Jump to

Keyboard shortcuts

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