Documentation
¶
Overview ¶
Package mempool provides a policy-enforced pool of unmined kaspa transactions.
A key responsbility of the kaspa network is mining user-generated transactions into blocks. In order to facilitate this, the mining process relies on having a readily-available source of transactions to include in a block that is being solved.
At a high level, this package satisfies that requirement by providing an in-memory pool of fully validated transactions that can also optionally be further filtered based upon a configurable policy.
One of the policy configuration options controls whether or not "standard" transactions are accepted. In essence, a "standard" transaction is one that satisfies a fairly strict set of requirements that are largely intended to help provide fair use of the system to all users. It is important to note that what is considered a "standard" transaction changes over time. For some insight, at the time of this writing, an example of SOME of the criteria that are required for a transaction to be considered standard are that it is of the most-recently supported version, finalized, does not exceed a specific size, and only consists of specific script forms.
Since this package does not deal with other kaspa specifics such as network communication and transaction relay, it returns a list of transactions that were accepted which gives the caller a high level of flexibility in how they want to proceed. Typically, this will involve things such as relaying the transactions to other peers on the network and notifying the mining process that new transactions are available.
Feature Overview ¶
The following is a quick overview of the major features. It is not intended to be an exhaustive list.
- Maintain a pool of fully validated transactions
- Reject non-fully-spent duplicate transactions
- Reject coinbase transactions
- Reject double spends (both from the DAG and other transactions in pool)
- Reject invalid transactions according to the network consensus rules
- Full script execution and validation with signature cache support
- Individual transaction query support
- Orphan transaction support (transactions that spend from unknown outputs)
- Configurable limits (see transaction acceptance policy)
- Automatic addition of orphan transactions that are no longer orphans as new transactions are added to the pool
- Individual orphan transaction query support
- Configurable transaction acceptance policy
- Option to accept or reject standard transactions
- Option to accept or reject transactions based on priority calculations
- Max signature operations per transaction
- Max number of orphan transactions allowed
- Additional metadata tracking for each transaction
- Timestamp when the transaction was added to the pool
- The fee the transaction pays
- The starting priority for the transaction
- Manual control of transaction removal
- Recursive removal of all dependent transactions
Errors ¶
Errors returned by this package are either the raw errors provided by underlying calls or of type mempool.RuleError. Since there are two classes of rules (mempool acceptance rules and blockDAG (consensus) acceptance rules), the mempool.RuleError type contains a single Err field which will, in turn, either be a mempool.TxRuleError or a blockdag.RuleError. The first indicates a violation of mempool acceptance rules while the latter indicates a violation of consensus acceptance rules. This allows the caller to easily differentiate between unexpected errors, such as database errors, versus errors due to rule violations through type assertions. In addition, callers can programmatically determine the specific rule violation by type asserting the Err field to one of the aforementioned types and examining their underlying ErrorCode field.
Index ¶
- Constants
- type Config
- type NewBlockMsg
- type Policy
- type RejectCode
- type RuleError
- type TxDesc
- type TxPool
- func (mp *TxPool) CheckSpend(op appmessage.Outpoint) *util.Tx
- func (mp *TxPool) Count() int
- func (mp *TxPool) DepCount() int
- func (mp *TxPool) FetchTransaction(txID *daghash.TxID) (*util.Tx, bool)
- func (mp *TxPool) FetchTxDesc(txID *daghash.TxID) (*TxDesc, bool)
- func (mp *TxPool) HandleNewBlock(block *util.Block) ([]*util.Tx, error)
- func (mp *TxPool) HaveTransaction(txID *daghash.TxID) bool
- func (mp *TxPool) IsInDependPool(hash *daghash.TxID) bool
- func (mp *TxPool) IsOrphanInPool(hash *daghash.TxID) bool
- func (mp *TxPool) IsTransactionInPool(hash *daghash.TxID) bool
- func (mp *TxPool) LastUpdated() mstime.Time
- func (mp *TxPool) MiningDescs() []*mining.TxDesc
- func (mp *TxPool) ProcessOrphans(acceptedTx *util.Tx) []*TxDesc
- func (mp *TxPool) ProcessTransaction(tx *util.Tx, allowOrphan bool) ([]*TxDesc, error)
- func (mp *TxPool) RemoveDoubleSpends(tx *util.Tx) error
- func (mp *TxPool) RemoveOrphan(tx *util.Tx)
- func (mp *TxPool) RemoveTransaction(tx *util.Tx, removeDependants bool, restoreInputs bool) error
- func (mp *TxPool) RemoveTransactions(txs []*util.Tx) error
- func (mp *TxPool) TxDescs() []*TxDesc
- func (mp *TxPool) TxIDs() []*daghash.TxID
- type TxRuleError
Constants ¶
const ( // MaxStandardTxSize is the maximum size allowed for transactions that // are considered standard and will therefore be relayed and considered // for mining. MaxStandardTxSize = 100000 // DefaultMinRelayTxFee is the minimum fee in sompi that is required // for a transaction to be treated as free for relay and mining // purposes. 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 sompi/1000 bytes. DefaultMinRelayTxFee = util.Amount(1000) )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct { // Policy defines the various mempool configuration options related // to policy. Policy Policy // CalcSequenceLockNoLock defines the function to use in order to generate // the current sequence lock for the given transaction using the passed // utxo set. CalcSequenceLockNoLock func(*util.Tx, blockdag.UTXOSet) (*blockdag.SequenceLock, error) // SigCache defines a signature cache to use. SigCache *txscript.SigCache // DAG is the BlockDAG we want to use (mainly for UTXO checks) DAG *blockdag.BlockDAG }
Config is a descriptor containing the memory pool configuration.
type NewBlockMsg ¶
NewBlockMsg is the type that is used in NewBlockMsg to transfer data about transaction removed and added to the mempool
type Policy ¶
type Policy struct { // MaxTxVersion is the transaction version that the mempool should // accept. All transactions above this version are rejected as // non-standard. MaxTxVersion int32 // AcceptNonStd defines whether to accept non-standard transactions. If // true, non-standard transactions will be accepted into the mempool. // Otherwise, all non-standard transactions will be rejected. AcceptNonStd bool // 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 // MinRelayTxFee defines the minimum transaction fee in KAS/kB to be // considered a non-zero fee. MinRelayTxFee util.Amount }
Policy houses the policy (configuration parameters) which is used to control the mempool.
type RejectCode ¶
type RejectCode uint8
RejectCode represents a numeric value by which a remote peer indicates why a message was rejected.
const ( RejectMalformed RejectCode = 0x01 RejectInvalid RejectCode = 0x10 RejectObsolete RejectCode = 0x11 RejectDuplicate RejectCode = 0x12 RejectNotRequested RejectCode = 0x13 RejectNonstandard RejectCode = 0x40 RejectDust RejectCode = 0x41 RejectInsufficientFee RejectCode = 0x42 RejectFinality RejectCode = 0x43 RejectDifficulty RejectCode = 0x44 )
These constants define the various supported reject codes.
func ErrToRejectErr ¶
func ErrToRejectErr(err error) (RejectCode, string)
ErrToRejectErr examines the underlying type of the error and returns a reject code and string appropriate to be sent in a appmessage.MsgReject message.
func (RejectCode) String ¶
func (code RejectCode) String() string
String returns the RejectCode in human-readable form.
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 blockdag.RuleError.
type TxDesc ¶
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 ¶
New returns a new memory pool for validating and storing standalone transactions until they are mined into a block.
func (*TxPool) CheckSpend ¶
func (mp *TxPool) CheckSpend(op appmessage.Outpoint) *util.Tx
CheckSpend checks whether the passed outpoint is already spent by a transaction in the mempool. If that's the case the spending transaction will be returned, if not nil will be returned.
func (*TxPool) Count ¶
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) DepCount ¶
DepCount returns the number of dependent transactions in the main pool. It does not include the orphan pool.
This function is safe for concurrent access.
func (*TxPool) FetchTransaction ¶
FetchTransaction returns the requested transaction from the transaction pool. This only fetches from the main transaction pool and does not include orphans. returns false in the second return parameter if transaction was not found
This function is safe for concurrent access.
func (*TxPool) FetchTxDesc ¶
FetchTxDesc returns the requested TxDesc from the transaction pool. This only fetches from the main transaction pool and does not include orphans. returns false in the second return parameter if transaction was not found
This function is safe for concurrent access.
func (*TxPool) HandleNewBlock ¶
HandleNewBlock removes all the transactions in the new block from the mempool and the orphan pool, and it also removes from the mempool transactions that double spend a transaction that is already in the DAG
func (*TxPool) HaveTransaction ¶
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) IsInDependPool ¶
IsInDependPool returns whether or not the passed transaction already exists in the main pool.
This function is safe for concurrent access.
func (*TxPool) IsOrphanInPool ¶
IsOrphanInPool returns whether or not the passed transaction already exists in the orphan pool.
This function is safe for concurrent access.
func (*TxPool) IsTransactionInPool ¶
IsTransactionInPool returns whether or not the passed transaction already exists in the main pool.
This function is safe for concurrent access.
func (*TxPool) LastUpdated ¶
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) MiningDescs ¶
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) ProcessOrphans ¶
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 ¶
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) RemoveDoubleSpends ¶
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 DAG because the block may contain transactions which were previously unknown to the memory pool.
This function is safe for concurrent access.
func (*TxPool) RemoveOrphan ¶
RemoveOrphan removes the passed orphan transaction from the orphan pool and previous orphan index.
This function is safe for concurrent access.
func (*TxPool) RemoveTransaction ¶
RemoveTransaction removes the passed transaction from the mempool. When the removeDependants flag is set, any transactions that depend on the removed transaction (that is to say, redeem outputs from it) will also be removed recursively from the mempool, as they would otherwise become orphans.
This function is safe for concurrent access.
func (*TxPool) RemoveTransactions ¶
RemoveTransactions removes the passed transactions from the mempool.
This function is safe for concurrent access.
type TxRuleError ¶
type TxRuleError struct { RejectCode 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.