2015-05-01 08:28:01 +02:00
|
|
|
// Copyright (c) 2013-2014 The btcsuite developers
|
2013-09-20 19:55:27 +02:00
|
|
|
// Use of this source code is governed by an ISC
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"container/list"
|
|
|
|
"crypto/rand"
|
|
|
|
"fmt"
|
2014-07-02 15:50:08 +02:00
|
|
|
"math"
|
|
|
|
"math/big"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2015-01-30 23:25:42 +01:00
|
|
|
"github.com/btcsuite/btcd/blockchain"
|
2015-01-27 22:38:23 +01:00
|
|
|
"github.com/btcsuite/btcd/database"
|
2015-01-30 19:14:33 +01:00
|
|
|
"github.com/btcsuite/btcd/txscript"
|
2015-02-05 22:16:39 +01:00
|
|
|
"github.com/btcsuite/btcd/wire"
|
2015-01-15 17:30:38 +01:00
|
|
|
"github.com/btcsuite/btcutil"
|
2013-09-20 19:55:27 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// mempoolHeight is the height used for the "block" height field of the
|
|
|
|
// contextual transaction information provided in a transaction store.
|
|
|
|
mempoolHeight = 0x7fffffff
|
|
|
|
|
|
|
|
// maxOrphanTransactions is the maximum number of orphan transactions
|
2015-05-05 16:53:15 +02:00
|
|
|
// that can be queued.
|
|
|
|
maxOrphanTransactions = 1000
|
2013-09-20 19:55:27 +02:00
|
|
|
|
|
|
|
// maxOrphanTxSize is the maximum size allowed for orphan transactions.
|
|
|
|
// This helps prevent memory exhaustion attacks from sending a lot of
|
|
|
|
// of big orphans.
|
|
|
|
maxOrphanTxSize = 5000
|
|
|
|
|
2014-08-14 16:44:26 +02:00
|
|
|
// 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.
|
2015-01-30 23:25:42 +01:00
|
|
|
maxSigOpsPerTx = blockchain.MaxSigOpsPerBlock / 5
|
2014-08-14 16:44:26 +02:00
|
|
|
|
2013-09-20 19:55:27 +02:00
|
|
|
// maxStandardTxSize is the maximum size allowed for transactions that
|
|
|
|
// are considered standard and will therefore be relayed and considered
|
|
|
|
// for mining.
|
2014-02-19 19:17:43 +01:00
|
|
|
maxStandardTxSize = 100000
|
2013-09-20 19:55:27 +02:00
|
|
|
|
|
|
|
// maxStandardSigScriptSize is the maximum size allowed for a
|
|
|
|
// transaction input signature script to be considered standard. This
|
2014-05-23 17:37:03 +02:00
|
|
|
// value allows for a 15-of-15 CHECKMULTISIG pay-to-script-hash with
|
|
|
|
// compressed keys.
|
|
|
|
//
|
|
|
|
// The form of the overall script is: OP_0 <15 signatures> OP_PUSHDATA2
|
|
|
|
// <2 bytes len> [OP_15 <15 pubkeys> OP_15 OP_CHECKMULTISIG]
|
|
|
|
//
|
|
|
|
// For the p2sh script portion, each of the 15 compressed pubkeys are
|
|
|
|
// 33 bytes (plus one for the OP_DATA_33 opcode), and the thus it totals
|
|
|
|
// to (15*34)+3 = 513 bytes. Next, each of the 15 signatures is a max
|
|
|
|
// of 73 bytes (plus one for the OP_DATA_73 opcode). Also, there is one
|
|
|
|
// extra byte for the initial extra OP_0 push and 3 bytes for the
|
|
|
|
// OP_PUSHDATA2 needed to specify the 513 bytes for the script push.
|
|
|
|
// That brings the total to 1+(15*74)+3+513 = 1627. This value also
|
|
|
|
// adds a few extra bytes to provide a little buffer.
|
|
|
|
// (1 + 15*74 + 3) + (15*34 + 3) + 23 = 1650
|
|
|
|
maxStandardSigScriptSize = 1650
|
2013-09-20 19:55:27 +02:00
|
|
|
|
2013-11-13 17:55:58 +01:00
|
|
|
// maxStandardMultiSigKeys is the maximum number of public keys allowed
|
|
|
|
// in a multi-signature transaction output script for it to be
|
2013-09-20 19:55:27 +02:00
|
|
|
// considered standard.
|
2013-11-13 17:55:58 +01:00
|
|
|
maxStandardMultiSigKeys = 3
|
2013-09-20 19:55:27 +02:00
|
|
|
|
2013-10-31 18:19:38 +01:00
|
|
|
// minTxRelayFee is the minimum fee in satoshi that is required for a
|
2014-03-11 06:12:54 +01:00
|
|
|
// 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 Satoshi/1000 bytes.
|
2014-03-04 16:15:18 +01:00
|
|
|
minTxRelayFee = 1000
|
2013-09-20 19:55:27 +02:00
|
|
|
)
|
|
|
|
|
2013-12-11 18:32:16 +01:00
|
|
|
// TxDesc is a descriptor containing a transaction in the mempool and the
|
|
|
|
// metadata we store about it.
|
|
|
|
type TxDesc struct {
|
2014-10-29 02:39:08 +01:00
|
|
|
Tx *btcutil.Tx // Transaction.
|
|
|
|
Added time.Time // Time when added to pool.
|
|
|
|
Height int64 // Blockheight when added to pool.
|
|
|
|
Fee int64 // Transaction fees.
|
|
|
|
startingPriority float64 // Priority when added to the pool.
|
2013-12-11 18:32:16 +01:00
|
|
|
}
|
|
|
|
|
2013-09-20 19:55:27 +02:00
|
|
|
// txMemPool 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.
|
|
|
|
type txMemPool struct {
|
2013-10-22 01:20:31 +02:00
|
|
|
sync.RWMutex
|
2013-09-20 19:55:27 +02:00
|
|
|
server *server
|
2015-02-05 22:16:39 +01:00
|
|
|
pool map[wire.ShaHash]*TxDesc
|
|
|
|
orphans map[wire.ShaHash]*btcutil.Tx
|
|
|
|
orphansByPrev map[wire.ShaHash]*list.List
|
2015-03-19 14:56:06 +01:00
|
|
|
addrindex map[string]map[wire.ShaHash]struct{} // maps address to txs
|
2015-02-05 22:16:39 +01:00
|
|
|
outpoints map[wire.OutPoint]*btcutil.Tx
|
2014-03-20 08:06:10 +01:00
|
|
|
lastUpdated time.Time // last time pool was updated
|
|
|
|
pennyTotal float64 // exponentially decaying total for penny spends.
|
|
|
|
lastPennyUnix int64 // unix time of last ``penny spend''
|
2013-09-20 19:55:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// isDust returns whether or not the passed transaction output amount is
|
|
|
|
// considered dust or not. Dust is defined in terms of the minimum transaction
|
|
|
|
// relay fee. In particular, if the cost to the network to spend coins is more
|
|
|
|
// than 1/3 of the minimum transaction relay fee, it is considered dust.
|
2015-02-05 22:16:39 +01:00
|
|
|
func isDust(txOut *wire.TxOut) bool {
|
2015-08-01 16:43:06 +02:00
|
|
|
// Unspendable outputs are considered dust.
|
|
|
|
if txscript.IsUnspendable(txOut.PkScript) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2013-09-20 19:55:27 +02:00
|
|
|
// The total serialized size consists of the output and the associated
|
|
|
|
// input script to redeem it. Since there is no input script
|
|
|
|
// to redeem it yet, use the minimum size of a typical input script.
|
|
|
|
//
|
|
|
|
// Pay-to-pubkey-hash bytes breakdown:
|
|
|
|
//
|
|
|
|
// Output to hash (34 bytes):
|
|
|
|
// 8 value, 1 script len, 25 script [1 OP_DUP, 1 OP_HASH_160,
|
|
|
|
// 1 OP_DATA_20, 20 hash, 1 OP_EQUALVERIFY, 1 OP_CHECKSIG]
|
|
|
|
//
|
|
|
|
// Input with compressed pubkey (148 bytes):
|
|
|
|
// 36 prev outpoint, 1 script len, 107 script [1 OP_DATA_72, 72 sig,
|
|
|
|
// 1 OP_DATA_33, 33 compressed pubkey], 4 sequence
|
|
|
|
//
|
|
|
|
// Input with uncompressed pubkey (180 bytes):
|
|
|
|
// 36 prev outpoint, 1 script len, 139 script [1 OP_DATA_72, 72 sig,
|
|
|
|
// 1 OP_DATA_65, 65 compressed pubkey], 4 sequence
|
|
|
|
//
|
|
|
|
// Pay-to-pubkey bytes breakdown:
|
|
|
|
//
|
|
|
|
// Output to compressed pubkey (44 bytes):
|
|
|
|
// 8 value, 1 script len, 35 script [1 OP_DATA_33,
|
|
|
|
// 33 compressed pubkey, 1 OP_CHECKSIG]
|
|
|
|
//
|
|
|
|
// Output to uncompressed pubkey (76 bytes):
|
|
|
|
// 8 value, 1 script len, 67 script [1 OP_DATA_65, 65 pubkey,
|
|
|
|
// 1 OP_CHECKSIG]
|
|
|
|
//
|
|
|
|
// Input (114 bytes):
|
|
|
|
// 36 prev outpoint, 1 script len, 73 script [1 OP_DATA_72,
|
|
|
|
// 72 sig], 4 sequence
|
|
|
|
//
|
|
|
|
// Theoretically this could examine the script type of the output script
|
|
|
|
// and use a different size for the typical input script size for
|
|
|
|
// pay-to-pubkey vs pay-to-pubkey-hash inputs per the above breakdowns,
|
|
|
|
// but the only combinination which is less than the value chosen is
|
|
|
|
// a pay-to-pubkey script with a compressed pubkey, which is not very
|
|
|
|
// common.
|
|
|
|
//
|
2013-10-08 00:27:59 +02:00
|
|
|
// The most common scripts are pay-to-pubkey-hash, and as per the above
|
|
|
|
// breakdown, the minimum size of a p2pkh input script is 148 bytes. So
|
2013-09-20 19:55:27 +02:00
|
|
|
// that figure is used.
|
2013-10-31 06:28:37 +01:00
|
|
|
totalSize := txOut.SerializeSize() + 148
|
2013-09-20 19:55:27 +02:00
|
|
|
|
|
|
|
// The output is considered dust if the cost to the network to spend the
|
2013-10-31 18:19:38 +01:00
|
|
|
// coins is more than 1/3 of the minimum free transaction relay fee.
|
2014-03-11 06:12:54 +01:00
|
|
|
// minFreeTxRelayFee is in Satoshi/KB, so multiply by 1000 to
|
|
|
|
// convert to bytes.
|
2013-09-20 19:55:27 +02:00
|
|
|
//
|
2013-10-08 00:27:59 +02:00
|
|
|
// Using the typical values for a pay-to-pubkey-hash transaction from
|
2013-10-31 18:19:38 +01:00
|
|
|
// the breakdown above and the default minimum free transaction relay
|
2014-03-04 16:15:18 +01:00
|
|
|
// fee of 1000, this equates to values less than 546 satoshi being
|
2013-10-31 18:19:38 +01:00
|
|
|
// considered dust.
|
2013-09-20 19:55:27 +02:00
|
|
|
//
|
|
|
|
// The following is equivalent to (value/totalSize) * (1/3) * 1000
|
|
|
|
// without needing to do floating point math.
|
|
|
|
return txOut.Value*1000/(3*int64(totalSize)) < minTxRelayFee
|
|
|
|
}
|
|
|
|
|
|
|
|
// checkPkScriptStandard performs a series of checks on a transaction ouput
|
|
|
|
// script (public key script) to ensure it is a "standard" public key script.
|
|
|
|
// A standard public key script is one that is a recognized form, and for
|
2013-11-13 17:55:58 +01:00
|
|
|
// multi-signature scripts, only contains from 1 to maxStandardMultiSigKeys
|
|
|
|
// public keys.
|
2015-01-30 19:14:33 +01:00
|
|
|
func checkPkScriptStandard(pkScript []byte, scriptClass txscript.ScriptClass) error {
|
2013-09-20 19:55:27 +02:00
|
|
|
switch scriptClass {
|
2015-01-30 19:14:33 +01:00
|
|
|
case txscript.MultiSigTy:
|
|
|
|
numPubKeys, numSigs, err := txscript.CalcMultiSigStats(pkScript)
|
2013-11-13 17:55:58 +01:00
|
|
|
if err != nil {
|
2014-07-09 05:01:13 +02:00
|
|
|
str := fmt.Sprintf("multi-signature script parse "+
|
|
|
|
"failure: %v", err)
|
2015-02-05 22:16:39 +01:00
|
|
|
return txRuleError(wire.RejectNonstandard, str)
|
2013-11-13 17:55:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// A standard multi-signature public key script must contain
|
|
|
|
// from 1 to maxStandardMultiSigKeys public keys.
|
|
|
|
if numPubKeys < 1 {
|
2014-07-09 05:01:13 +02:00
|
|
|
str := "multi-signature script with no pubkeys"
|
2015-02-05 22:16:39 +01:00
|
|
|
return txRuleError(wire.RejectNonstandard, str)
|
2013-11-13 17:55:58 +01:00
|
|
|
}
|
|
|
|
if numPubKeys > maxStandardMultiSigKeys {
|
|
|
|
str := fmt.Sprintf("multi-signature script with %d "+
|
|
|
|
"public keys which is more than the allowed "+
|
|
|
|
"max of %d", numPubKeys, maxStandardMultiSigKeys)
|
2015-02-05 22:16:39 +01:00
|
|
|
return txRuleError(wire.RejectNonstandard, str)
|
2013-11-13 17:55:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// A standard multi-signature public key script must have at
|
|
|
|
// least 1 signature and no more signatures than available
|
|
|
|
// public keys.
|
2013-09-20 19:55:27 +02:00
|
|
|
if numSigs < 1 {
|
2015-02-05 22:16:39 +01:00
|
|
|
return txRuleError(wire.RejectNonstandard,
|
2014-07-09 05:01:13 +02:00
|
|
|
"multi-signature script with no signatures")
|
2013-09-20 19:55:27 +02:00
|
|
|
}
|
2013-11-13 17:55:58 +01:00
|
|
|
if numSigs > numPubKeys {
|
2013-10-04 20:30:50 +02:00
|
|
|
str := fmt.Sprintf("multi-signature script with %d "+
|
2013-11-13 17:55:58 +01:00
|
|
|
"signatures which is more than the available "+
|
|
|
|
"%d public keys", numSigs, numPubKeys)
|
2015-02-05 22:16:39 +01:00
|
|
|
return txRuleError(wire.RejectNonstandard, str)
|
2013-09-20 19:55:27 +02:00
|
|
|
}
|
|
|
|
|
2015-01-30 19:14:33 +01:00
|
|
|
case txscript.NonStandardTy:
|
2015-02-05 22:16:39 +01:00
|
|
|
return txRuleError(wire.RejectNonstandard,
|
2014-07-09 05:01:13 +02:00
|
|
|
"non-standard script form")
|
2013-09-20 19:55:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// checkTransactionStandard performs a series of checks on a transaction to
|
|
|
|
// ensure it is a "standard" transaction. A standard transaction is one that
|
|
|
|
// conforms to several additional limiting cases over what is considered a
|
|
|
|
// "sane" transaction such as having a version in the supported range, being
|
|
|
|
// finalized, conforming to more stringent size constraints, having scripts
|
|
|
|
// of recognized forms, and not containing "dust" outputs (those that are
|
|
|
|
// so small it costs more to process them than they are worth).
|
2015-02-28 19:17:43 +01:00
|
|
|
func (mp *txMemPool) checkTransactionStandard(tx *btcutil.Tx, height int64) error {
|
2013-10-28 21:44:38 +01:00
|
|
|
msgTx := tx.MsgTx()
|
|
|
|
|
2013-09-20 19:55:27 +02:00
|
|
|
// The transaction must be a currently supported version.
|
2015-02-05 22:16:39 +01:00
|
|
|
if msgTx.Version > wire.TxVersion || msgTx.Version < 1 {
|
2013-10-04 20:30:50 +02:00
|
|
|
str := fmt.Sprintf("transaction version %d is not in the "+
|
2013-10-28 21:44:38 +01:00
|
|
|
"valid range of %d-%d", msgTx.Version, 1,
|
2015-02-05 22:16:39 +01:00
|
|
|
wire.TxVersion)
|
|
|
|
return txRuleError(wire.RejectNonstandard, str)
|
2013-09-20 19:55:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// The transaction must be finalized to be standard and therefore
|
|
|
|
// considered for inclusion in a block.
|
2015-02-28 19:17:43 +01:00
|
|
|
adjustedTime := mp.server.timeSource.AdjustedTime()
|
2015-06-29 17:12:35 +02:00
|
|
|
if !blockchain.IsFinalizedTransaction(tx, height, adjustedTime) {
|
2015-02-05 22:16:39 +01:00
|
|
|
return txRuleError(wire.RejectNonstandard,
|
2014-07-09 05:01:13 +02:00
|
|
|
"transaction is not finalized")
|
2013-09-20 19:55:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Since extremely large transactions with a lot of inputs can cost
|
|
|
|
// almost as much to process as the sender fees, limit the maximum
|
|
|
|
// size of a transaction. This also helps mitigate CPU exhaustion
|
|
|
|
// attacks.
|
2013-10-31 06:28:37 +01:00
|
|
|
serializedLen := msgTx.SerializeSize()
|
2013-09-20 19:55:27 +02:00
|
|
|
if serializedLen > maxStandardTxSize {
|
2013-10-04 20:30:50 +02:00
|
|
|
str := fmt.Sprintf("transaction size of %v is larger than max "+
|
2013-09-20 19:55:27 +02:00
|
|
|
"allowed size of %v", serializedLen, maxStandardTxSize)
|
2015-02-05 22:16:39 +01:00
|
|
|
return txRuleError(wire.RejectNonstandard, str)
|
2013-09-20 19:55:27 +02:00
|
|
|
}
|
|
|
|
|
2013-10-28 21:44:38 +01:00
|
|
|
for i, txIn := range msgTx.TxIn {
|
2013-09-20 19:55:27 +02:00
|
|
|
// Each transaction input signature script must not exceed the
|
|
|
|
// maximum size allowed for a standard transaction. See
|
|
|
|
// the comment on maxStandardSigScriptSize for more details.
|
|
|
|
sigScriptLen := len(txIn.SignatureScript)
|
|
|
|
if sigScriptLen > maxStandardSigScriptSize {
|
2013-10-04 20:30:50 +02:00
|
|
|
str := fmt.Sprintf("transaction input %d: signature "+
|
2013-09-20 19:55:27 +02:00
|
|
|
"script size of %d bytes is large than max "+
|
|
|
|
"allowed size of %d bytes", i, sigScriptLen,
|
|
|
|
maxStandardSigScriptSize)
|
2015-02-05 22:16:39 +01:00
|
|
|
return txRuleError(wire.RejectNonstandard, str)
|
2013-09-20 19:55:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Each transaction input signature script must only contain
|
|
|
|
// opcodes which push data onto the stack.
|
2015-01-30 19:14:33 +01:00
|
|
|
if !txscript.IsPushOnlyScript(txIn.SignatureScript) {
|
2013-10-04 20:30:50 +02:00
|
|
|
str := fmt.Sprintf("transaction input %d: signature "+
|
2013-10-04 07:38:33 +02:00
|
|
|
"script is not push only", i)
|
2015-02-05 22:16:39 +01:00
|
|
|
return txRuleError(wire.RejectNonstandard, str)
|
2014-02-20 02:22:02 +01:00
|
|
|
}
|
2013-09-20 19:55:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// None of the output public key scripts can be a non-standard script or
|
2015-01-11 21:43:25 +01:00
|
|
|
// be "dust" (except when the script is a null data script).
|
2013-11-13 17:55:58 +01:00
|
|
|
numNullDataOutputs := 0
|
2013-10-28 21:44:38 +01:00
|
|
|
for i, txOut := range msgTx.TxOut {
|
2015-01-30 19:14:33 +01:00
|
|
|
scriptClass := txscript.GetScriptClass(txOut.PkScript)
|
2013-11-13 17:55:58 +01:00
|
|
|
err := checkPkScriptStandard(txOut.PkScript, scriptClass)
|
2013-09-20 19:55:27 +02:00
|
|
|
if err != nil {
|
2014-07-09 05:01:13 +02:00
|
|
|
// Attempt to extract a reject code from the error so
|
|
|
|
// it can be retained. When not possible, fall back to
|
|
|
|
// a non standard error.
|
|
|
|
rejectCode, found := extractRejectCode(err)
|
|
|
|
if !found {
|
2015-02-05 22:16:39 +01:00
|
|
|
rejectCode = wire.RejectNonstandard
|
2014-07-09 05:01:13 +02:00
|
|
|
}
|
2013-10-04 20:30:50 +02:00
|
|
|
str := fmt.Sprintf("transaction output %d: %v", i, err)
|
2014-07-09 05:01:13 +02:00
|
|
|
return txRuleError(rejectCode, str)
|
2013-09-20 19:55:27 +02:00
|
|
|
}
|
|
|
|
|
2015-01-11 21:43:25 +01:00
|
|
|
// Accumulate the number of outputs which only carry data. For
|
|
|
|
// all other script types, ensure the output value is not
|
|
|
|
// "dust".
|
2015-01-30 19:14:33 +01:00
|
|
|
if scriptClass == txscript.NullDataTy {
|
2013-11-13 17:55:58 +01:00
|
|
|
numNullDataOutputs++
|
2015-01-11 21:43:25 +01:00
|
|
|
} else if isDust(txOut) {
|
2013-10-04 20:30:50 +02:00
|
|
|
str := fmt.Sprintf("transaction output %d: payment "+
|
2013-09-20 19:55:27 +02:00
|
|
|
"of %d is dust", i, txOut.Value)
|
2015-02-05 22:16:39 +01:00
|
|
|
return txRuleError(wire.RejectDust, str)
|
2013-09-20 19:55:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-13 17:55:58 +01:00
|
|
|
// A standard transaction must not have more than one output script that
|
|
|
|
// only carries data.
|
|
|
|
if numNullDataOutputs > 1 {
|
2014-07-09 05:01:13 +02:00
|
|
|
str := "more than one transaction output in a nulldata script"
|
2015-02-05 22:16:39 +01:00
|
|
|
return txRuleError(wire.RejectNonstandard, str)
|
2013-11-13 17:55:58 +01:00
|
|
|
}
|
|
|
|
|
2013-09-20 19:55:27 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-10-11 21:12:40 +02:00
|
|
|
// checkInputsStandard performs a series of checks on a transaction's inputs
|
2013-09-20 19:55:27 +02:00
|
|
|
// to ensure they are "standard". A standard transaction input is one that
|
2013-11-13 17:55:58 +01:00
|
|
|
// that consumes the expected number of elements from the stack and that number
|
|
|
|
// is the same as the output script pushes. This help prevent resource
|
|
|
|
// exhaustion attacks by "creative" use of scripts that are super expensive to
|
|
|
|
// process like OP_DUP OP_CHECKSIG OP_DROP repeated a large number of times
|
|
|
|
// followed by a final OP_TRUE.
|
2015-01-30 23:25:42 +01:00
|
|
|
func checkInputsStandard(tx *btcutil.Tx, txStore blockchain.TxStore) error {
|
2013-11-13 17:55:58 +01:00
|
|
|
// NOTE: The reference implementation also does a coinbase check here,
|
|
|
|
// but coinbases have already been rejected prior to calling this
|
|
|
|
// function so no need to recheck.
|
|
|
|
|
|
|
|
for i, txIn := range tx.MsgTx().TxIn {
|
|
|
|
// It is safe to elide existence and index checks here since
|
|
|
|
// they have already been checked prior to calling this
|
|
|
|
// function.
|
2014-10-01 17:34:30 +02:00
|
|
|
prevOut := txIn.PreviousOutPoint
|
2013-11-13 17:55:58 +01:00
|
|
|
originTx := txStore[prevOut.Hash].Tx.MsgTx()
|
|
|
|
originPkScript := originTx.TxOut[prevOut.Index].PkScript
|
|
|
|
|
|
|
|
// Calculate stats for the script pair.
|
2015-01-30 19:14:33 +01:00
|
|
|
scriptInfo, err := txscript.CalcScriptInfo(txIn.SignatureScript,
|
2013-11-13 17:55:58 +01:00
|
|
|
originPkScript, true)
|
|
|
|
if err != nil {
|
2014-07-09 05:01:13 +02:00
|
|
|
str := fmt.Sprintf("transaction input #%d script parse "+
|
|
|
|
"failure: %v", i, err)
|
2015-02-05 22:16:39 +01:00
|
|
|
return txRuleError(wire.RejectNonstandard, str)
|
2013-11-13 17:55:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// A negative value for expected inputs indicates the script is
|
|
|
|
// non-standard in some way.
|
|
|
|
if scriptInfo.ExpectedInputs < 0 {
|
|
|
|
str := fmt.Sprintf("transaction input #%d expects %d "+
|
|
|
|
"inputs", i, scriptInfo.ExpectedInputs)
|
2015-02-05 22:16:39 +01:00
|
|
|
return txRuleError(wire.RejectNonstandard, str)
|
2013-11-13 17:55:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// The script pair is non-standard if the number of available
|
|
|
|
// inputs does not match the number of expected inputs.
|
|
|
|
if scriptInfo.NumInputs != scriptInfo.ExpectedInputs {
|
|
|
|
str := fmt.Sprintf("transaction input #%d expects %d "+
|
2014-03-17 06:17:16 +01:00
|
|
|
"inputs, but referenced output script provides "+
|
|
|
|
"%d", i, scriptInfo.ExpectedInputs,
|
2013-11-13 17:55:58 +01:00
|
|
|
scriptInfo.NumInputs)
|
2015-02-05 22:16:39 +01:00
|
|
|
return txRuleError(wire.RejectNonstandard, str)
|
2013-11-13 17:55:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-20 19:55:27 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-08-19 17:17:33 +02:00
|
|
|
// calcMinRequiredTxRelayFee returns the minimum transaction fee required for a
|
|
|
|
// transaction with the passed serialized size to be accepted into the memory
|
|
|
|
// pool and relayed.
|
|
|
|
func calcMinRequiredTxRelayFee(serializedSize int64) int64 {
|
2014-08-18 17:57:02 +02:00
|
|
|
// Calculate the minimum fee for a transaction to be allowed into the
|
|
|
|
// mempool and relayed by scaling the base fee (which is the minimum
|
|
|
|
// free transaction relay fee). minTxRelayFee is in Satoshi/KB, so
|
|
|
|
// divide the transaction size by 1000 to convert to kilobytes. Also,
|
|
|
|
// integer division is used so fees only increase on full kilobyte
|
|
|
|
// boundaries.
|
2014-08-19 17:17:33 +02:00
|
|
|
minFee := (1 + serializedSize/1000) * minTxRelayFee
|
2014-08-18 17:57:02 +02:00
|
|
|
|
|
|
|
// Set the minimum fee to the maximum possible value if the calculated
|
|
|
|
// fee is not in the valid range for monetary amounts.
|
|
|
|
if minFee < 0 || minFee > btcutil.MaxSatoshi {
|
|
|
|
minFee = btcutil.MaxSatoshi
|
|
|
|
}
|
|
|
|
|
2014-08-19 17:17:33 +02:00
|
|
|
return minFee
|
2013-10-31 18:19:38 +01:00
|
|
|
}
|
|
|
|
|
2014-09-18 16:23:36 +02:00
|
|
|
// removeOrphan is the internal function which implements the public
|
|
|
|
// RemoveOrphan. See the comment for RemoveOrphan for more details.
|
2015-03-10 17:40:44 +01:00
|
|
|
//
|
2013-10-22 01:20:31 +02:00
|
|
|
// This function MUST be called with the mempool lock held (for writes).
|
2015-02-05 22:16:39 +01:00
|
|
|
func (mp *txMemPool) removeOrphan(txHash *wire.ShaHash) {
|
2013-09-20 19:55:27 +02:00
|
|
|
// Nothing to do if passed tx is not an orphan.
|
|
|
|
tx, exists := mp.orphans[*txHash]
|
|
|
|
if !exists {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove the reference from the previous orphan index.
|
2013-10-28 21:44:38 +01:00
|
|
|
for _, txIn := range tx.MsgTx().TxIn {
|
2014-10-01 17:34:30 +02:00
|
|
|
originTxHash := txIn.PreviousOutPoint.Hash
|
2013-09-20 19:55:27 +02:00
|
|
|
if orphans, exists := mp.orphansByPrev[originTxHash]; exists {
|
2013-10-07 17:10:10 +02:00
|
|
|
for e := orphans.Front(); e != nil; e = e.Next() {
|
2013-10-28 21:44:38 +01:00
|
|
|
if e.Value.(*btcutil.Tx) == tx {
|
2013-09-20 19:55:27 +02:00
|
|
|
orphans.Remove(e)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove the map entry altogether if there are no
|
|
|
|
// longer any orphans which depend on it.
|
|
|
|
if orphans.Len() == 0 {
|
|
|
|
delete(mp.orphansByPrev, originTxHash)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove the transaction from the orphan pool.
|
|
|
|
delete(mp.orphans, *txHash)
|
|
|
|
}
|
|
|
|
|
2014-09-18 16:23:36 +02:00
|
|
|
// RemoveOrphan removes the passed orphan transaction from the orphan pool and
|
|
|
|
// previous orphan index.
|
2015-03-10 17:40:44 +01:00
|
|
|
//
|
2014-09-18 16:23:36 +02:00
|
|
|
// This function is safe for concurrent access.
|
2015-02-05 22:16:39 +01:00
|
|
|
func (mp *txMemPool) RemoveOrphan(txHash *wire.ShaHash) {
|
2014-09-18 16:23:36 +02:00
|
|
|
mp.Lock()
|
|
|
|
mp.removeOrphan(txHash)
|
|
|
|
mp.Unlock()
|
|
|
|
}
|
|
|
|
|
2013-09-20 19:55:27 +02:00
|
|
|
// limitNumOrphans limits the number of orphan transactions by evicting a random
|
|
|
|
// orphan if adding a new one would cause it to overflow the max allowed.
|
2013-10-22 01:20:31 +02:00
|
|
|
//
|
|
|
|
// This function MUST be called with the mempool lock held (for writes).
|
2013-09-20 19:55:27 +02:00
|
|
|
func (mp *txMemPool) limitNumOrphans() error {
|
2015-05-05 16:53:15 +02:00
|
|
|
if len(mp.orphans)+1 > cfg.MaxOrphanTxs && cfg.MaxOrphanTxs > 0 {
|
2013-09-20 19:55:27 +02:00
|
|
|
// Generate a cryptographically random hash.
|
2015-02-05 22:16:39 +01:00
|
|
|
randHashBytes := make([]byte, wire.HashSize)
|
2013-09-20 19:55:27 +02:00
|
|
|
_, err := rand.Read(randHashBytes)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
randHashNum := new(big.Int).SetBytes(randHashBytes)
|
|
|
|
|
|
|
|
// Try to find the first entry that is greater than the random
|
|
|
|
// hash. Use the first entry (which is already pseudorandom due
|
|
|
|
// to Go's range statement over maps) as a fallback if none of
|
|
|
|
// the hashes in the orphan pool are larger than the random
|
|
|
|
// hash.
|
2015-02-05 22:16:39 +01:00
|
|
|
var foundHash *wire.ShaHash
|
2013-09-20 19:55:27 +02:00
|
|
|
for txHash := range mp.orphans {
|
|
|
|
if foundHash == nil {
|
|
|
|
foundHash = &txHash
|
|
|
|
}
|
2015-01-30 23:25:42 +01:00
|
|
|
txHashNum := blockchain.ShaHashToBig(&txHash)
|
2013-09-20 19:55:27 +02:00
|
|
|
if txHashNum.Cmp(randHashNum) > 0 {
|
|
|
|
foundHash = &txHash
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mp.removeOrphan(foundHash)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// addOrphan adds an orphan transaction to the orphan pool.
|
2013-10-22 01:20:31 +02:00
|
|
|
//
|
|
|
|
// This function MUST be called with the mempool lock held (for writes).
|
2013-10-28 21:44:38 +01:00
|
|
|
func (mp *txMemPool) addOrphan(tx *btcutil.Tx) {
|
2013-09-20 19:55:27 +02:00
|
|
|
// Limit the number orphan transactions to prevent memory exhaustion. A
|
|
|
|
// random orphan is evicted to make room if needed.
|
|
|
|
mp.limitNumOrphans()
|
|
|
|
|
2013-10-28 21:44:38 +01:00
|
|
|
mp.orphans[*tx.Sha()] = tx
|
|
|
|
for _, txIn := range tx.MsgTx().TxIn {
|
2014-10-01 17:34:30 +02:00
|
|
|
originTxHash := txIn.PreviousOutPoint.Hash
|
2013-09-20 19:55:27 +02:00
|
|
|
if mp.orphansByPrev[originTxHash] == nil {
|
|
|
|
mp.orphansByPrev[originTxHash] = list.New()
|
|
|
|
}
|
|
|
|
mp.orphansByPrev[originTxHash].PushBack(tx)
|
|
|
|
}
|
|
|
|
|
2013-11-21 19:03:56 +01:00
|
|
|
txmpLog.Debugf("Stored orphan transaction %v (total: %d)", tx.Sha(),
|
2013-09-20 19:55:27 +02:00
|
|
|
len(mp.orphans))
|
|
|
|
}
|
|
|
|
|
|
|
|
// maybeAddOrphan potentially adds an orphan to the orphan pool.
|
2013-10-22 01:20:31 +02:00
|
|
|
//
|
|
|
|
// This function MUST be called with the mempool lock held (for writes).
|
2013-10-28 21:44:38 +01:00
|
|
|
func (mp *txMemPool) maybeAddOrphan(tx *btcutil.Tx) error {
|
2013-09-20 19:55:27 +02:00
|
|
|
// Ignore orphan transactions that are too large. This helps avoid
|
|
|
|
// a memory exhaustion attack based on sending a lot of really large
|
|
|
|
// orphans. In the case there is a valid transaction larger than this,
|
|
|
|
// it will ultimtely be rebroadcast after the parent transactions
|
|
|
|
// have been mined or otherwise received.
|
|
|
|
//
|
|
|
|
// Note that the number of orphan transactions in the orphan pool is
|
|
|
|
// also limited, so this equates to a maximum memory used of
|
2015-05-05 16:53:15 +02:00
|
|
|
// maxOrphanTxSize * cfg.MaxOrphanTxs (which is ~5MB using the default
|
|
|
|
// values at the time this comment was written).
|
2013-10-31 06:28:37 +01:00
|
|
|
serializedLen := tx.MsgTx().SerializeSize()
|
2013-09-20 19:55:27 +02:00
|
|
|
if serializedLen > maxOrphanTxSize {
|
2013-10-04 20:30:50 +02:00
|
|
|
str := fmt.Sprintf("orphan transaction size of %d bytes is "+
|
2013-09-20 19:55:27 +02:00
|
|
|
"larger than max allowed size of %d bytes",
|
|
|
|
serializedLen, maxOrphanTxSize)
|
2015-02-05 22:16:39 +01:00
|
|
|
return txRuleError(wire.RejectNonstandard, str)
|
2013-09-20 19:55:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add the orphan if the none of the above disqualified it.
|
2013-10-28 21:44:38 +01:00
|
|
|
mp.addOrphan(tx)
|
2013-09-20 19:55:27 +02:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-10-22 01:20:31 +02:00
|
|
|
// isTransactionInPool returns whether or not the passed transaction already
|
|
|
|
// exists in the main pool.
|
|
|
|
//
|
|
|
|
// This function MUST be called with the mempool lock held (for reads).
|
2015-02-05 22:16:39 +01:00
|
|
|
func (mp *txMemPool) isTransactionInPool(hash *wire.ShaHash) bool {
|
2013-10-22 01:20:31 +02:00
|
|
|
if _, exists := mp.pool[*hash]; exists {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2013-10-08 17:47:00 +02:00
|
|
|
// IsTransactionInPool returns whether or not the passed transaction already
|
2013-10-11 21:12:40 +02:00
|
|
|
// exists in the main pool.
|
2013-10-22 01:20:31 +02:00
|
|
|
//
|
|
|
|
// This function is safe for concurrent access.
|
2015-02-05 22:16:39 +01:00
|
|
|
func (mp *txMemPool) IsTransactionInPool(hash *wire.ShaHash) bool {
|
2013-10-22 01:20:31 +02:00
|
|
|
// Protect concurrent access.
|
|
|
|
mp.RLock()
|
|
|
|
defer mp.RUnlock()
|
2013-09-20 19:55:27 +02:00
|
|
|
|
2013-10-22 01:20:31 +02:00
|
|
|
return mp.isTransactionInPool(hash)
|
|
|
|
}
|
|
|
|
|
|
|
|
// isOrphanInPool returns whether or not the passed transaction already exists
|
|
|
|
// in the orphan pool.
|
|
|
|
//
|
|
|
|
// This function MUST be called with the mempool lock held (for reads).
|
2015-02-05 22:16:39 +01:00
|
|
|
func (mp *txMemPool) isOrphanInPool(hash *wire.ShaHash) bool {
|
2013-10-22 01:20:31 +02:00
|
|
|
if _, exists := mp.orphans[*hash]; exists {
|
2013-09-20 19:55:27 +02:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2013-10-11 21:12:40 +02:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsOrphanInPool returns whether or not the passed transaction already exists
|
|
|
|
// in the orphan pool.
|
2013-10-22 01:20:31 +02:00
|
|
|
//
|
|
|
|
// This function is safe for concurrent access.
|
2015-02-05 22:16:39 +01:00
|
|
|
func (mp *txMemPool) IsOrphanInPool(hash *wire.ShaHash) bool {
|
2013-10-22 01:20:31 +02:00
|
|
|
// Protect concurrent access.
|
|
|
|
mp.RLock()
|
|
|
|
defer mp.RUnlock()
|
2013-10-11 21:12:40 +02:00
|
|
|
|
2013-10-22 01:20:31 +02:00
|
|
|
return mp.isOrphanInPool(hash)
|
|
|
|
}
|
2013-09-20 19:55:27 +02:00
|
|
|
|
2013-10-22 01:20:31 +02:00
|
|
|
// haveTransaction returns whether or not the passed transaction already exists
|
|
|
|
// in the main pool or in the orphan pool.
|
|
|
|
//
|
|
|
|
// This function MUST be called with the mempool lock held (for reads).
|
2015-02-05 22:16:39 +01:00
|
|
|
func (mp *txMemPool) haveTransaction(hash *wire.ShaHash) bool {
|
2013-10-22 01:20:31 +02:00
|
|
|
return mp.isTransactionInPool(hash) || mp.isOrphanInPool(hash)
|
2013-09-20 19:55:27 +02:00
|
|
|
}
|
|
|
|
|
2013-10-11 21:12:40 +02:00
|
|
|
// HaveTransaction returns whether or not the passed transaction already exists
|
|
|
|
// in the main pool or in the orphan pool.
|
2013-10-22 01:20:31 +02:00
|
|
|
//
|
|
|
|
// This function is safe for concurrent access.
|
2015-02-05 22:16:39 +01:00
|
|
|
func (mp *txMemPool) HaveTransaction(hash *wire.ShaHash) bool {
|
2013-10-22 01:20:31 +02:00
|
|
|
// Protect concurrent access.
|
|
|
|
mp.RLock()
|
|
|
|
defer mp.RUnlock()
|
|
|
|
|
|
|
|
return mp.haveTransaction(hash)
|
2013-10-11 21:12:40 +02:00
|
|
|
}
|
|
|
|
|
2013-11-15 23:12:23 +01:00
|
|
|
// removeTransaction is the internal function which implements the public
|
|
|
|
// RemoveTransaction. See the comment for RemoveTransaction for more details.
|
2013-10-22 01:20:31 +02:00
|
|
|
//
|
|
|
|
// This function MUST be called with the mempool lock held (for writes).
|
2013-10-28 21:44:38 +01:00
|
|
|
func (mp *txMemPool) removeTransaction(tx *btcutil.Tx) {
|
2013-09-20 19:55:27 +02:00
|
|
|
// Remove any transactions which rely on this one.
|
2013-10-28 21:44:38 +01:00
|
|
|
txHash := tx.Sha()
|
|
|
|
for i := uint32(0); i < uint32(len(tx.MsgTx().TxOut)); i++ {
|
2015-02-05 22:16:39 +01:00
|
|
|
outpoint := wire.NewOutPoint(txHash, i)
|
2013-09-20 19:55:27 +02:00
|
|
|
if txRedeemer, exists := mp.outpoints[*outpoint]; exists {
|
|
|
|
mp.removeTransaction(txRedeemer)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove the transaction and mark the referenced outpoints as unspent
|
|
|
|
// by the pool.
|
2013-12-11 18:32:16 +01:00
|
|
|
if txDesc, exists := mp.pool[*txHash]; exists {
|
2015-01-04 02:42:01 +01:00
|
|
|
if cfg.AddrIndex {
|
|
|
|
mp.removeTransactionFromAddrIndex(tx)
|
|
|
|
}
|
|
|
|
|
2013-12-11 18:32:16 +01:00
|
|
|
for _, txIn := range txDesc.Tx.MsgTx().TxIn {
|
2014-10-01 17:34:30 +02:00
|
|
|
delete(mp.outpoints, txIn.PreviousOutPoint)
|
2013-09-20 19:55:27 +02:00
|
|
|
}
|
2013-10-28 21:44:38 +01:00
|
|
|
delete(mp.pool, *txHash)
|
2014-03-20 08:06:10 +01:00
|
|
|
mp.lastUpdated = time.Now()
|
2013-09-20 19:55:27 +02:00
|
|
|
}
|
2015-01-04 02:42:01 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// removeTransactionFromAddrIndex removes the passed transaction from our
|
|
|
|
// address based index.
|
|
|
|
//
|
|
|
|
// This function MUST be called with the mempool lock held (for writes).
|
|
|
|
func (mp *txMemPool) removeTransactionFromAddrIndex(tx *btcutil.Tx) error {
|
|
|
|
previousOutputScripts, err := mp.fetchReferencedOutputScripts(tx)
|
|
|
|
if err != nil {
|
|
|
|
txmpLog.Errorf("Unable to obtain referenced output scripts for "+
|
|
|
|
"the passed tx (addrindex): %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, pkScript := range previousOutputScripts {
|
|
|
|
mp.removeScriptFromAddrIndex(pkScript, tx)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, txOut := range tx.MsgTx().TxOut {
|
|
|
|
mp.removeScriptFromAddrIndex(txOut.PkScript, tx)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// removeScriptFromAddrIndex dissociates the address encoded by the
|
|
|
|
// passed pkScript from the passed tx in our address based tx index.
|
|
|
|
//
|
|
|
|
// This function MUST be called with the mempool lock held (for writes).
|
|
|
|
func (mp *txMemPool) removeScriptFromAddrIndex(pkScript []byte, tx *btcutil.Tx) error {
|
|
|
|
_, addresses, _, err := txscript.ExtractPkScriptAddrs(pkScript,
|
|
|
|
activeNetParams.Params)
|
|
|
|
if err != nil {
|
|
|
|
txmpLog.Errorf("Unable to extract encoded addresses from script "+
|
|
|
|
"for addrindex (addrindex): %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, addr := range addresses {
|
2015-03-19 14:56:06 +01:00
|
|
|
delete(mp.addrindex[addr.EncodeAddress()], *tx.Sha())
|
2015-01-04 02:42:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2013-09-20 19:55:27 +02:00
|
|
|
}
|
|
|
|
|
2013-11-15 23:12:23 +01:00
|
|
|
// RemoveTransaction removes the passed transaction and any transactions which
|
|
|
|
// depend on it from the memory pool.
|
|
|
|
//
|
|
|
|
// This function is safe for concurrent access.
|
|
|
|
func (mp *txMemPool) RemoveTransaction(tx *btcutil.Tx) {
|
|
|
|
// Protect concurrent access.
|
|
|
|
mp.Lock()
|
|
|
|
defer mp.Unlock()
|
|
|
|
|
|
|
|
mp.removeTransaction(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 (mp *txMemPool) RemoveDoubleSpends(tx *btcutil.Tx) {
|
|
|
|
// Protect concurrent access.
|
|
|
|
mp.Lock()
|
|
|
|
defer mp.Unlock()
|
|
|
|
|
|
|
|
for _, txIn := range tx.MsgTx().TxIn {
|
2014-10-01 17:34:30 +02:00
|
|
|
if txRedeemer, ok := mp.outpoints[txIn.PreviousOutPoint]; ok {
|
2013-11-15 23:12:23 +01:00
|
|
|
if !txRedeemer.Sha().IsEqual(tx.Sha()) {
|
|
|
|
mp.removeTransaction(txRedeemer)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-20 19:55:27 +02:00
|
|
|
// addTransaction adds the passed transaction to the memory pool. It should
|
|
|
|
// not be called directly as it doesn't perform any validation. This is a
|
|
|
|
// helper for maybeAcceptTransaction.
|
2013-10-22 01:20:31 +02:00
|
|
|
//
|
|
|
|
// This function MUST be called with the mempool lock held (for writes).
|
2013-12-11 18:32:16 +01:00
|
|
|
func (mp *txMemPool) addTransaction(tx *btcutil.Tx, height, fee int64) {
|
2013-09-20 19:55:27 +02:00
|
|
|
// Add the transaction to the pool and mark the referenced outpoints
|
|
|
|
// as spent by the pool.
|
2013-12-11 18:32:16 +01:00
|
|
|
mp.pool[*tx.Sha()] = &TxDesc{
|
2013-12-17 15:02:35 +01:00
|
|
|
Tx: tx,
|
|
|
|
Added: time.Now(),
|
2013-12-11 18:32:16 +01:00
|
|
|
Height: height,
|
2013-12-17 15:02:35 +01:00
|
|
|
Fee: fee,
|
2013-12-11 18:32:16 +01:00
|
|
|
}
|
2013-10-28 21:44:38 +01:00
|
|
|
for _, txIn := range tx.MsgTx().TxIn {
|
2014-10-01 17:34:30 +02:00
|
|
|
mp.outpoints[txIn.PreviousOutPoint] = tx
|
2013-09-20 19:55:27 +02:00
|
|
|
}
|
2014-03-20 08:06:10 +01:00
|
|
|
mp.lastUpdated = time.Now()
|
2015-01-04 02:42:01 +01:00
|
|
|
|
|
|
|
if cfg.AddrIndex {
|
|
|
|
mp.addTransactionToAddrIndex(tx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// addTransactionToAddrIndex adds all addresses related to the transaction to
|
|
|
|
// our in-memory address index. Note that this address is only populated when
|
|
|
|
// we're running with the optional address index activated.
|
|
|
|
//
|
|
|
|
// This function MUST be called with the mempool lock held (for writes).
|
|
|
|
func (mp *txMemPool) addTransactionToAddrIndex(tx *btcutil.Tx) error {
|
|
|
|
previousOutScripts, err := mp.fetchReferencedOutputScripts(tx)
|
|
|
|
if err != nil {
|
|
|
|
txmpLog.Errorf("Unable to obtain referenced output scripts for "+
|
|
|
|
"the passed tx (addrindex): %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Index addresses of all referenced previous output tx's.
|
|
|
|
for _, pkScript := range previousOutScripts {
|
|
|
|
mp.indexScriptAddressToTx(pkScript, tx)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Index addresses of all created outputs.
|
|
|
|
for _, txOut := range tx.MsgTx().TxOut {
|
|
|
|
mp.indexScriptAddressToTx(txOut.PkScript, tx)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// fetchReferencedOutputScripts looks up and returns all the scriptPubKeys
|
|
|
|
// referenced by inputs of the passed transaction.
|
|
|
|
//
|
|
|
|
// This function MUST be called with the mempool lock held (for reads).
|
|
|
|
func (mp *txMemPool) fetchReferencedOutputScripts(tx *btcutil.Tx) ([][]byte, error) {
|
|
|
|
txStore, err := mp.fetchInputTransactions(tx)
|
|
|
|
if err != nil || len(txStore) == 0 {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
previousOutScripts := make([][]byte, 0, len(tx.MsgTx().TxIn))
|
|
|
|
for _, txIn := range tx.MsgTx().TxIn {
|
|
|
|
outPoint := txIn.PreviousOutPoint
|
|
|
|
if txStore[outPoint.Hash].Err == nil {
|
|
|
|
referencedOutPoint := txStore[outPoint.Hash].Tx.MsgTx().TxOut[outPoint.Index]
|
|
|
|
previousOutScripts = append(previousOutScripts, referencedOutPoint.PkScript)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return previousOutScripts, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// indexScriptByAddress alters our address index by indexing the payment address
|
|
|
|
// encoded by the passed scriptPubKey to the passed transaction.
|
|
|
|
//
|
|
|
|
// This function MUST be called with the mempool lock held (for writes).
|
|
|
|
func (mp *txMemPool) indexScriptAddressToTx(pkScript []byte, tx *btcutil.Tx) error {
|
|
|
|
_, addresses, _, err := txscript.ExtractPkScriptAddrs(pkScript,
|
|
|
|
activeNetParams.Params)
|
|
|
|
if err != nil {
|
|
|
|
txmpLog.Errorf("Unable to extract encoded addresses from script "+
|
|
|
|
"for addrindex: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, addr := range addresses {
|
|
|
|
if mp.addrindex[addr.EncodeAddress()] == nil {
|
2015-03-19 14:56:06 +01:00
|
|
|
mp.addrindex[addr.EncodeAddress()] = make(map[wire.ShaHash]struct{})
|
2015-01-04 02:42:01 +01:00
|
|
|
}
|
2015-03-19 14:56:06 +01:00
|
|
|
mp.addrindex[addr.EncodeAddress()][*tx.Sha()] = struct{}{}
|
2015-01-04 02:42:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2013-09-20 19:55:27 +02:00
|
|
|
}
|
|
|
|
|
2014-10-29 02:39:08 +01:00
|
|
|
// calcInputValueAge is a helper function used to calculate the input age of
|
2014-11-17 07:18:44 +01:00
|
|
|
// a transaction. The input age for a txin is the number of confirmations
|
|
|
|
// since the referenced txout multiplied by its output value. The total input
|
|
|
|
// age is the sum of this value for each txin. Any inputs to the transaction
|
|
|
|
// which are currently in the mempool and hence not mined into a block yet,
|
|
|
|
// contribute no additional input age to the transaction.
|
2015-01-30 23:25:42 +01:00
|
|
|
func calcInputValueAge(txDesc *TxDesc, txStore blockchain.TxStore, nextBlockHeight int64) float64 {
|
2014-10-29 02:39:08 +01:00
|
|
|
var totalInputAge float64
|
|
|
|
for _, txIn := range txDesc.Tx.MsgTx().TxIn {
|
|
|
|
originHash := &txIn.PreviousOutPoint.Hash
|
|
|
|
originIndex := txIn.PreviousOutPoint.Index
|
|
|
|
|
|
|
|
// Don't attempt to accumulate the total input age if the txIn
|
|
|
|
// in question doesn't exist.
|
|
|
|
if txData, exists := txStore[*originHash]; exists && txData.Tx != nil {
|
2014-11-17 07:18:44 +01:00
|
|
|
// Inputs with dependencies currently in the mempool
|
|
|
|
// have their block height set to a special constant.
|
|
|
|
// Their input age should computed as zero since their
|
|
|
|
// parent hasn't made it into a block yet.
|
2014-10-29 02:39:08 +01:00
|
|
|
var inputAge int64
|
|
|
|
if txData.BlockHeight == mempoolHeight {
|
|
|
|
inputAge = 0
|
|
|
|
} else {
|
|
|
|
inputAge = nextBlockHeight - txData.BlockHeight
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sum the input value times age.
|
2014-11-17 07:18:44 +01:00
|
|
|
originTxOut := txData.Tx.MsgTx().TxOut[originIndex]
|
2014-10-29 02:39:08 +01:00
|
|
|
inputValue := originTxOut.Value
|
|
|
|
totalInputAge += float64(inputValue * inputAge)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return totalInputAge
|
|
|
|
}
|
|
|
|
|
2015-02-24 16:15:15 +01:00
|
|
|
// minInt is a helper function to return the minimum of two ints. This avoids
|
|
|
|
// a math import and the need to cast to floats.
|
|
|
|
func minInt(a, b int) int {
|
|
|
|
if a < b {
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
2015-03-04 17:46:20 +01:00
|
|
|
func calcPriority(tx *btcutil.Tx, inputValueAge float64) float64 {
|
2015-02-24 16:15:15 +01:00
|
|
|
// In order to encourage spending multiple old unspent transaction
|
|
|
|
// outputs thereby reducing the total set, don't count the constant
|
|
|
|
// overhead for each input as well as enough bytes of the signature
|
|
|
|
// script to cover a pay-to-script-hash redemption with a compressed
|
|
|
|
// pubkey. This makes additional inputs free by boosting the priority
|
|
|
|
// of the transaction accordingly. No more incentive is given to avoid
|
|
|
|
// encouraging gaming future transactions through the use of junk
|
|
|
|
// outputs. This is the same logic used in the reference
|
|
|
|
// implementation.
|
|
|
|
//
|
|
|
|
// The constant overhead for a txin is 41 bytes since the previous
|
|
|
|
// outpoint is 36 bytes + 4 bytes for the sequence + 1 byte the
|
|
|
|
// signature script length.
|
|
|
|
//
|
|
|
|
// A compressed pubkey pay-to-script-hash redemption with a maximum len
|
|
|
|
// signature is of the form:
|
|
|
|
// [OP_DATA_73 <73-byte sig> + OP_DATA_35 + {OP_DATA_33
|
|
|
|
// <33 byte compresed pubkey> + OP_CHECKSIG}]
|
|
|
|
//
|
|
|
|
// Thus 1 + 73 + 1 + 1 + 33 + 1 = 110
|
|
|
|
overhead := 0
|
|
|
|
for _, txIn := range tx.MsgTx().TxIn {
|
|
|
|
// Max inputs + size can't possibly overflow here.
|
|
|
|
overhead += 41 + minInt(110, len(txIn.SignatureScript))
|
|
|
|
}
|
|
|
|
|
2015-03-04 17:46:20 +01:00
|
|
|
serializedTxSize := tx.MsgTx().SerializeSize()
|
2015-02-24 16:15:15 +01:00
|
|
|
if overhead >= serializedTxSize {
|
|
|
|
return 0.0
|
|
|
|
}
|
|
|
|
|
|
|
|
return inputValueAge / float64(serializedTxSize-overhead)
|
|
|
|
}
|
|
|
|
|
2014-11-17 07:18:44 +01:00
|
|
|
// StartingPriority calculates the priority of this tx descriptor's underlying
|
|
|
|
// transaction relative to when it was first added to the mempool. The result
|
|
|
|
// is lazily computed and then cached for subsequent function calls.
|
2015-01-30 23:25:42 +01:00
|
|
|
func (txD *TxDesc) StartingPriority(txStore blockchain.TxStore) float64 {
|
2014-11-17 07:18:44 +01:00
|
|
|
// Return our cached result.
|
|
|
|
if txD.startingPriority != float64(0) {
|
|
|
|
return txD.startingPriority
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compute our starting priority caching the result.
|
|
|
|
inputAge := calcInputValueAge(txD, txStore, txD.Height)
|
2015-03-04 17:46:20 +01:00
|
|
|
txD.startingPriority = calcPriority(txD.Tx, inputAge)
|
2014-11-17 07:18:44 +01:00
|
|
|
|
|
|
|
return txD.startingPriority
|
|
|
|
}
|
|
|
|
|
|
|
|
// CurrentPriority calculates the current priority of this tx descriptor's
|
|
|
|
// underlying transaction relative to the next block height.
|
2015-01-30 23:25:42 +01:00
|
|
|
func (txD *TxDesc) CurrentPriority(txStore blockchain.TxStore, nextBlockHeight int64) float64 {
|
2014-11-17 07:18:44 +01:00
|
|
|
inputAge := calcInputValueAge(txD, txStore, nextBlockHeight)
|
2015-03-04 17:46:20 +01:00
|
|
|
return calcPriority(txD.Tx, inputAge)
|
2014-11-17 07:18:44 +01:00
|
|
|
}
|
|
|
|
|
2013-09-20 19:55:27 +02:00
|
|
|
// checkPoolDoubleSpend checks whether or not the passed transaction is
|
|
|
|
// attempting to spend coins already spent by other transactions in the pool.
|
|
|
|
// Note it does not check for double spends against transactions already in the
|
|
|
|
// main chain.
|
2013-10-22 01:20:31 +02:00
|
|
|
//
|
|
|
|
// This function MUST be called with the mempool lock held (for reads).
|
2013-10-28 21:44:38 +01:00
|
|
|
func (mp *txMemPool) checkPoolDoubleSpend(tx *btcutil.Tx) error {
|
|
|
|
for _, txIn := range tx.MsgTx().TxIn {
|
2014-10-01 17:34:30 +02:00
|
|
|
if txR, exists := mp.outpoints[txIn.PreviousOutPoint]; exists {
|
2015-01-07 01:26:26 +01:00
|
|
|
str := fmt.Sprintf("output %v already spent by "+
|
|
|
|
"transaction %v in the memory pool",
|
|
|
|
txIn.PreviousOutPoint, txR.Sha())
|
2015-02-05 22:16:39 +01:00
|
|
|
return txRuleError(wire.RejectDuplicate, str)
|
2013-09-20 19:55:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// fetchInputTransactions fetches the input transactions referenced by the
|
|
|
|
// passed transaction. First, it fetches from the main chain, then it tries to
|
|
|
|
// fetch any missing inputs from the transaction pool.
|
2013-10-22 01:20:31 +02:00
|
|
|
//
|
|
|
|
// This function MUST be called with the mempool lock held (for reads).
|
2015-01-30 23:25:42 +01:00
|
|
|
func (mp *txMemPool) fetchInputTransactions(tx *btcutil.Tx) (blockchain.TxStore, error) {
|
2013-09-20 19:55:27 +02:00
|
|
|
txStore, err := mp.server.blockManager.blockChain.FetchTransactionStore(tx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attempt to populate any missing inputs from the transaction pool.
|
|
|
|
for _, txD := range txStore {
|
2015-01-27 22:38:23 +01:00
|
|
|
if txD.Err == database.ErrTxShaMissing || txD.Tx == nil {
|
2013-12-11 18:32:16 +01:00
|
|
|
if poolTxDesc, exists := mp.pool[*txD.Hash]; exists {
|
|
|
|
poolTx := poolTxDesc.Tx
|
2013-09-20 19:55:27 +02:00
|
|
|
txD.Tx = poolTx
|
|
|
|
txD.BlockHeight = mempoolHeight
|
2013-10-28 21:44:38 +01:00
|
|
|
txD.Spent = make([]bool, len(poolTx.MsgTx().TxOut))
|
2013-09-20 19:55:27 +02:00
|
|
|
txD.Err = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return txStore, nil
|
|
|
|
}
|
|
|
|
|
2013-10-11 21:12:40 +02:00
|
|
|
// FetchTransaction returns the requested transaction from the transaction pool.
|
|
|
|
// This only fetches from the main transaction pool and does not include
|
|
|
|
// orphans.
|
2013-10-22 01:20:31 +02:00
|
|
|
//
|
|
|
|
// This function is safe for concurrent access.
|
2015-02-05 22:16:39 +01:00
|
|
|
func (mp *txMemPool) FetchTransaction(txHash *wire.ShaHash) (*btcutil.Tx, error) {
|
2013-10-22 01:20:31 +02:00
|
|
|
// Protect concurrent access.
|
|
|
|
mp.RLock()
|
|
|
|
defer mp.RUnlock()
|
2013-10-11 21:12:40 +02:00
|
|
|
|
2013-12-11 18:32:16 +01:00
|
|
|
if txDesc, exists := mp.pool[*txHash]; exists {
|
|
|
|
return txDesc.Tx, nil
|
2013-10-11 21:12:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("transaction is not in the pool")
|
|
|
|
}
|
|
|
|
|
2015-01-04 02:42:01 +01:00
|
|
|
// FilterTransactionsByAddress returns all transactions currently in the
|
|
|
|
// mempool that either create an output to the passed address or spend a
|
|
|
|
// previously created ouput to the address.
|
|
|
|
func (mp *txMemPool) FilterTransactionsByAddress(addr btcutil.Address) ([]*btcutil.Tx, error) {
|
|
|
|
// Protect concurrent access.
|
|
|
|
mp.RLock()
|
|
|
|
defer mp.RUnlock()
|
|
|
|
|
|
|
|
if txs, exists := mp.addrindex[addr.EncodeAddress()]; exists {
|
|
|
|
addressTxs := make([]*btcutil.Tx, 0, len(txs))
|
2015-03-19 14:56:06 +01:00
|
|
|
for txHash := range txs {
|
|
|
|
if tx, exists := mp.pool[txHash]; exists {
|
|
|
|
addressTxs = append(addressTxs, tx.Tx)
|
|
|
|
}
|
2015-01-04 02:42:01 +01:00
|
|
|
}
|
|
|
|
return addressTxs, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("address does not have any transactions in the pool")
|
|
|
|
}
|
|
|
|
|
2013-11-15 08:50:02 +01:00
|
|
|
// maybeAcceptTransaction is the internal function which implements the public
|
|
|
|
// MaybeAcceptTransaction. See the comment for MaybeAcceptTransaction for
|
|
|
|
// more details.
|
2013-10-22 01:20:31 +02:00
|
|
|
//
|
|
|
|
// This function MUST be called with the mempool lock held (for writes).
|
2015-02-05 22:16:39 +01:00
|
|
|
func (mp *txMemPool) maybeAcceptTransaction(tx *btcutil.Tx, isNew, rateLimit bool) ([]*wire.ShaHash, error) {
|
2013-10-28 21:44:38 +01:00
|
|
|
txHash := tx.Sha()
|
2013-09-20 19:55:27 +02:00
|
|
|
|
|
|
|
// Don't accept the transaction if it already exists in the pool. This
|
|
|
|
// applies to orphan transactions as well. This check is intended to
|
2013-10-11 21:12:40 +02:00
|
|
|
// be a quick check to weed out duplicates.
|
2013-10-28 21:44:38 +01:00
|
|
|
if mp.haveTransaction(txHash) {
|
2013-10-04 20:30:50 +02:00
|
|
|
str := fmt.Sprintf("already have transaction %v", txHash)
|
2015-02-05 22:16:39 +01:00
|
|
|
return nil, txRuleError(wire.RejectDuplicate, str)
|
2013-09-20 19:55:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Perform preliminary sanity checks on the transaction. This makes
|
|
|
|
// use of btcchain which contains the invariant rules for what
|
|
|
|
// transactions are allowed into blocks.
|
2015-01-30 23:25:42 +01:00
|
|
|
err := blockchain.CheckTransactionSanity(tx)
|
2013-09-20 19:55:27 +02:00
|
|
|
if err != nil {
|
2015-01-30 23:25:42 +01:00
|
|
|
if cerr, ok := err.(blockchain.RuleError); ok {
|
2015-01-07 01:26:26 +01:00
|
|
|
return nil, chainRuleError(cerr)
|
2013-10-04 20:30:50 +02:00
|
|
|
}
|
2015-01-07 01:26:26 +01:00
|
|
|
return nil, err
|
2013-09-20 19:55:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// A standalone transaction must not be a coinbase transaction.
|
2015-01-30 23:25:42 +01:00
|
|
|
if blockchain.IsCoinBase(tx) {
|
2013-10-04 20:30:50 +02:00
|
|
|
str := fmt.Sprintf("transaction %v is an individual coinbase",
|
2013-09-20 19:55:27 +02:00
|
|
|
txHash)
|
2015-02-05 22:16:39 +01:00
|
|
|
return nil, txRuleError(wire.RejectInvalid, str)
|
2013-09-20 19:55:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Don't accept transactions with a lock time after the maximum int32
|
|
|
|
// value for now. This is an artifact of older bitcoind clients which
|
|
|
|
// treated this field as an int32 and would treat anything larger
|
|
|
|
// incorrectly (as negative).
|
2013-10-28 21:44:38 +01:00
|
|
|
if tx.MsgTx().LockTime > math.MaxInt32 {
|
2014-06-12 20:04:05 +02:00
|
|
|
str := fmt.Sprintf("transaction %v has a lock time after "+
|
2013-09-20 19:55:27 +02:00
|
|
|
"2038 which is not accepted yet", txHash)
|
2015-02-05 22:16:39 +01:00
|
|
|
return nil, txRuleError(wire.RejectNonstandard, str)
|
2013-09-20 19:55:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get the current height of the main chain. A standalone transaction
|
2013-10-30 20:13:29 +01:00
|
|
|
// will be mined into the next block at best, so it's height is at least
|
|
|
|
// one more than the current height.
|
2013-09-20 19:55:27 +02:00
|
|
|
_, curHeight, err := mp.server.db.NewestSha()
|
|
|
|
if err != nil {
|
2014-07-09 05:01:13 +02:00
|
|
|
// This is an unexpected error so don't turn it into a rule
|
|
|
|
// error.
|
2015-01-07 01:26:26 +01:00
|
|
|
return nil, err
|
2013-09-20 19:55:27 +02:00
|
|
|
}
|
|
|
|
nextBlockHeight := curHeight + 1
|
|
|
|
|
2014-05-23 06:14:36 +02:00
|
|
|
// Don't allow non-standard transactions if the network parameters
|
|
|
|
// forbid their relaying.
|
|
|
|
if !activeNetParams.RelayNonStdTxs {
|
2015-02-28 19:17:43 +01:00
|
|
|
err := mp.checkTransactionStandard(tx, nextBlockHeight)
|
2013-09-20 19:55:27 +02:00
|
|
|
if err != nil {
|
2014-07-09 05:01:13 +02:00
|
|
|
// Attempt to extract a reject code from the error so
|
|
|
|
// it can be retained. When not possible, fall back to
|
|
|
|
// a non standard error.
|
|
|
|
rejectCode, found := extractRejectCode(err)
|
|
|
|
if !found {
|
2015-02-05 22:16:39 +01:00
|
|
|
rejectCode = wire.RejectNonstandard
|
2014-07-09 05:01:13 +02:00
|
|
|
}
|
|
|
|
str := fmt.Sprintf("transaction %v is not standard: %v",
|
|
|
|
txHash, err)
|
2015-01-07 01:26:26 +01:00
|
|
|
return nil, txRuleError(rejectCode, str)
|
2013-09-20 19:55:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// The transaction may not use any of the same outputs as other
|
|
|
|
// transactions already in the pool as that would ultimately result in a
|
|
|
|
// double spend. This check is intended to be quick and therefore only
|
|
|
|
// detects double spends within the transaction pool itself. The
|
|
|
|
// transaction could still be double spending coins from the main chain
|
|
|
|
// at this point. There is a more in-depth check that happens later
|
|
|
|
// after fetching the referenced transaction inputs from the main chain
|
|
|
|
// which examines the actual spend data and prevents double spends.
|
|
|
|
err = mp.checkPoolDoubleSpend(tx)
|
|
|
|
if err != nil {
|
2015-01-07 01:26:26 +01:00
|
|
|
return nil, err
|
2013-09-20 19:55:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch all of the transactions referenced by the inputs to this
|
|
|
|
// transaction. This function also attempts to fetch the transaction
|
|
|
|
// itself to be used for detecting a duplicate transaction without
|
|
|
|
// needing to do a separate lookup.
|
|
|
|
txStore, err := mp.fetchInputTransactions(tx)
|
|
|
|
if err != nil {
|
2015-01-30 23:25:42 +01:00
|
|
|
if cerr, ok := err.(blockchain.RuleError); ok {
|
2015-01-07 01:26:26 +01:00
|
|
|
return nil, chainRuleError(cerr)
|
2014-07-09 05:01:13 +02:00
|
|
|
}
|
2015-01-07 01:26:26 +01:00
|
|
|
return nil, err
|
2013-09-20 19:55:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Don't allow the transaction if it exists in the main chain and is not
|
|
|
|
// not already fully spent.
|
2013-10-28 21:44:38 +01:00
|
|
|
if txD, exists := txStore[*txHash]; exists && txD.Err == nil {
|
2013-09-20 19:55:27 +02:00
|
|
|
for _, isOutputSpent := range txD.Spent {
|
|
|
|
if !isOutputSpent {
|
2015-02-05 22:16:39 +01:00
|
|
|
return nil, txRuleError(wire.RejectDuplicate,
|
2014-07-09 05:01:13 +02:00
|
|
|
"transaction already exists")
|
2013-09-20 19:55:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-10-28 21:44:38 +01:00
|
|
|
delete(txStore, *txHash)
|
2013-09-20 19:55:27 +02:00
|
|
|
|
2015-01-07 01:26:26 +01:00
|
|
|
// Transaction is an orphan if any of the referenced input transactions
|
|
|
|
// don't exist. Adding orphans to the orphan pool is not handled by
|
|
|
|
// this function, and the caller should use maybeAddOrphan if this
|
|
|
|
// behavior is desired.
|
2015-02-05 22:16:39 +01:00
|
|
|
var missingParents []*wire.ShaHash
|
2013-09-20 19:55:27 +02:00
|
|
|
for _, txD := range txStore {
|
2015-01-27 22:38:23 +01:00
|
|
|
if txD.Err == database.ErrTxShaMissing {
|
2015-01-07 01:26:26 +01:00
|
|
|
missingParents = append(missingParents, txD.Hash)
|
2013-09-20 19:55:27 +02:00
|
|
|
}
|
|
|
|
}
|
2015-01-07 01:26:26 +01:00
|
|
|
if len(missingParents) != 0 {
|
|
|
|
return missingParents, nil
|
|
|
|
}
|
2013-09-20 19:55:27 +02:00
|
|
|
|
|
|
|
// Perform several checks on the transaction inputs using the invariant
|
|
|
|
// rules in btcchain for what transactions are allowed into blocks.
|
|
|
|
// Also returns the fees associated with the transaction which will be
|
|
|
|
// used later.
|
2015-01-30 23:25:42 +01:00
|
|
|
txFee, err := blockchain.CheckTransactionInputs(tx, nextBlockHeight, txStore)
|
2013-09-20 19:55:27 +02:00
|
|
|
if err != nil {
|
2015-01-30 23:25:42 +01:00
|
|
|
if cerr, ok := err.(blockchain.RuleError); ok {
|
2015-01-07 01:26:26 +01:00
|
|
|
return nil, chainRuleError(cerr)
|
2013-11-15 18:59:32 +01:00
|
|
|
}
|
2015-01-07 01:26:26 +01:00
|
|
|
return nil, err
|
2013-09-20 19:55:27 +02:00
|
|
|
}
|
|
|
|
|
2014-05-23 06:14:36 +02:00
|
|
|
// Don't allow transactions with non-standard inputs if the network
|
|
|
|
// parameters forbid their relaying.
|
|
|
|
if !activeNetParams.RelayNonStdTxs {
|
2013-11-13 17:55:58 +01:00
|
|
|
err := checkInputsStandard(tx, txStore)
|
2013-09-20 19:55:27 +02:00
|
|
|
if err != nil {
|
2014-07-09 05:01:13 +02:00
|
|
|
// Attempt to extract a reject code from the error so
|
|
|
|
// it can be retained. When not possible, fall back to
|
|
|
|
// a non standard error.
|
|
|
|
rejectCode, found := extractRejectCode(err)
|
|
|
|
if !found {
|
2015-02-05 22:16:39 +01:00
|
|
|
rejectCode = wire.RejectNonstandard
|
2014-07-09 05:01:13 +02:00
|
|
|
}
|
2013-10-04 20:30:50 +02:00
|
|
|
str := fmt.Sprintf("transaction %v has a non-standard "+
|
2013-09-20 19:55:27 +02:00
|
|
|
"input: %v", txHash, err)
|
2015-01-07 01:26:26 +01:00
|
|
|
return nil, txRuleError(rejectCode, str)
|
2013-09-20 19:55:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-31 18:19:38 +01:00
|
|
|
// NOTE: if you modify this code to accept non-standard transactions,
|
2013-09-20 19:55:27 +02:00
|
|
|
// you should add code here to check that the transaction does a
|
|
|
|
// reasonable number of ECDSA signature verifications.
|
|
|
|
|
2014-08-14 16:44:26 +02:00
|
|
|
// Don't allow transactions with an excessive number of signature
|
|
|
|
// operations which would result in making it impossible to mine. Since
|
|
|
|
// the coinbase address itself can contain signature operations, the
|
|
|
|
// maximum allowed signature operations per transaction is less than
|
|
|
|
// the maximum allowed signature operations per block.
|
2015-01-30 23:25:42 +01:00
|
|
|
numSigOps, err := blockchain.CountP2SHSigOps(tx, false, txStore)
|
2014-08-14 16:44:26 +02:00
|
|
|
if err != nil {
|
2015-01-30 23:25:42 +01:00
|
|
|
if cerr, ok := err.(blockchain.RuleError); ok {
|
2015-01-07 01:26:26 +01:00
|
|
|
return nil, chainRuleError(cerr)
|
2014-08-14 16:44:26 +02:00
|
|
|
}
|
2015-01-07 01:26:26 +01:00
|
|
|
return nil, err
|
2014-08-14 16:44:26 +02:00
|
|
|
}
|
2015-01-30 23:25:42 +01:00
|
|
|
numSigOps += blockchain.CountSigOps(tx)
|
2014-08-14 16:44:26 +02:00
|
|
|
if numSigOps > maxSigOpsPerTx {
|
|
|
|
str := fmt.Sprintf("transaction %v has too many sigops: %d > %d",
|
|
|
|
txHash, numSigOps, maxSigOpsPerTx)
|
2015-02-05 22:16:39 +01:00
|
|
|
return nil, txRuleError(wire.RejectNonstandard, str)
|
2014-08-14 16:44:26 +02:00
|
|
|
}
|
|
|
|
|
2013-10-31 18:19:38 +01:00
|
|
|
// Don't allow transactions with fees too low to get into a mined block.
|
2014-08-19 17:17:33 +02:00
|
|
|
//
|
|
|
|
// Most miners allow a free transaction area in blocks they mine to go
|
|
|
|
// alongside the area used for high-priority transactions as well as
|
|
|
|
// transactions with fees. A transaction size of up to 1000 bytes is
|
|
|
|
// considered safe to go into this section. Further, the minimum fee
|
|
|
|
// calculated below on its own would encourage several small
|
|
|
|
// transactions to avoid fees rather than one single larger transaction
|
|
|
|
// which is more desirable. Therefore, as long as the size of the
|
|
|
|
// transaction does not exceeed 1000 less than the reserved space for
|
|
|
|
// high-priority transactions, don't require a fee for it.
|
|
|
|
serializedSize := int64(tx.MsgTx().SerializeSize())
|
|
|
|
minFee := calcMinRequiredTxRelayFee(serializedSize)
|
|
|
|
if serializedSize >= (defaultBlockPrioritySize-1000) && txFee < minFee {
|
2013-10-31 18:19:38 +01:00
|
|
|
str := fmt.Sprintf("transaction %v has %d fees which is under "+
|
|
|
|
"the required amount of %d", txHash, txFee,
|
2014-08-19 17:17:33 +02:00
|
|
|
minFee)
|
2015-02-05 22:16:39 +01:00
|
|
|
return nil, txRuleError(wire.RejectInsufficientFee, str)
|
2013-10-31 18:19:38 +01:00
|
|
|
}
|
|
|
|
|
2015-02-23 16:28:31 +01:00
|
|
|
// Require that free transactions have sufficient priority to be mined
|
2015-02-26 05:01:20 +01:00
|
|
|
// in the next block. Transactions which are being added back to the
|
|
|
|
// memory pool from blocks that have been disconnected during a reorg
|
|
|
|
// are exempted.
|
|
|
|
if isNew && !cfg.NoRelayPriority && txFee < minFee {
|
2015-02-23 16:28:31 +01:00
|
|
|
txD := &TxDesc{
|
|
|
|
Tx: tx,
|
|
|
|
Added: time.Now(),
|
|
|
|
Height: curHeight,
|
|
|
|
Fee: txFee,
|
|
|
|
}
|
|
|
|
currentPriority := txD.CurrentPriority(txStore, nextBlockHeight)
|
|
|
|
if currentPriority <= minHighPriority {
|
|
|
|
str := fmt.Sprintf("transaction %v has insufficient "+
|
|
|
|
"priority (%g <= %g)", txHash,
|
|
|
|
currentPriority, minHighPriority)
|
|
|
|
return nil, txRuleError(wire.RejectInsufficientFee, str)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-19 19:15:47 +01:00
|
|
|
// Free-to-relay transactions are rate limited here to prevent
|
|
|
|
// penny-flooding with tiny transactions as a form of attack.
|
2014-08-19 17:17:33 +02:00
|
|
|
if rateLimit && txFee < minFee {
|
2014-02-19 19:15:47 +01:00
|
|
|
nowUnix := time.Now().Unix()
|
|
|
|
// we decay passed data with an exponentially decaying ~10
|
|
|
|
// minutes window - matches bitcoind handling.
|
|
|
|
mp.pennyTotal *= math.Pow(1.0-1.0/600.0,
|
|
|
|
float64(nowUnix-mp.lastPennyUnix))
|
|
|
|
mp.lastPennyUnix = nowUnix
|
|
|
|
|
|
|
|
// Are we still over the limit?
|
|
|
|
if mp.pennyTotal >= cfg.FreeTxRelayLimit*10*1000 {
|
2014-08-18 17:57:02 +02:00
|
|
|
str := fmt.Sprintf("transaction %v has been rejected "+
|
|
|
|
"by the rate limiter due to low fees", txHash)
|
2015-02-05 22:16:39 +01:00
|
|
|
return nil, txRuleError(wire.RejectInsufficientFee, str)
|
2014-02-19 19:15:47 +01:00
|
|
|
}
|
|
|
|
oldTotal := mp.pennyTotal
|
|
|
|
|
2014-08-18 17:57:02 +02:00
|
|
|
mp.pennyTotal += float64(serializedSize)
|
2014-03-17 06:23:04 +01:00
|
|
|
txmpLog.Tracef("rate limit: curTotal %v, nextTotal: %v, "+
|
2014-02-19 19:15:47 +01:00
|
|
|
"limit %v", oldTotal, mp.pennyTotal,
|
|
|
|
cfg.FreeTxRelayLimit*10*1000)
|
|
|
|
}
|
2013-09-20 19:55:27 +02:00
|
|
|
|
|
|
|
// Verify crypto signatures for each input and reject the transaction if
|
|
|
|
// any don't verify.
|
2015-01-30 23:25:42 +01:00
|
|
|
err = blockchain.ValidateTransactionScripts(tx, txStore,
|
2015-02-26 21:21:12 +01:00
|
|
|
txscript.StandardVerifyFlags)
|
2013-09-20 19:55:27 +02:00
|
|
|
if err != nil {
|
2015-01-30 23:25:42 +01:00
|
|
|
if cerr, ok := err.(blockchain.RuleError); ok {
|
2015-01-07 01:26:26 +01:00
|
|
|
return nil, chainRuleError(cerr)
|
2014-07-09 05:01:13 +02:00
|
|
|
}
|
2015-01-07 01:26:26 +01:00
|
|
|
return nil, err
|
2013-09-20 19:55:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add to transaction pool.
|
2013-12-25 19:15:59 +01:00
|
|
|
mp.addTransaction(tx, curHeight, txFee)
|
2013-09-20 19:55:27 +02:00
|
|
|
|
2013-11-21 19:03:56 +01:00
|
|
|
txmpLog.Debugf("Accepted transaction %v (pool size: %v)", txHash,
|
2013-09-20 19:55:27 +02:00
|
|
|
len(mp.pool))
|
|
|
|
|
2013-12-17 19:16:12 +01:00
|
|
|
if mp.server.rpcServer != nil {
|
2014-06-27 21:12:22 +02:00
|
|
|
// Notify websocket clients about mempool transactions.
|
2014-03-04 17:15:25 +01:00
|
|
|
mp.server.rpcServer.ntfnMgr.NotifyMempoolTx(tx, isNew)
|
2014-06-27 21:12:22 +02:00
|
|
|
|
|
|
|
// Potentially notify any getblocktemplate long poll clients
|
|
|
|
// about stale block templates due to the new transaction.
|
|
|
|
mp.server.rpcServer.gbtWorkState.NotifyMempoolTx(mp.lastUpdated)
|
2013-12-17 19:16:12 +01:00
|
|
|
}
|
2013-09-20 19:55:27 +02:00
|
|
|
|
2015-01-07 01:26:26 +01:00
|
|
|
return nil, nil
|
2013-09-20 19:55:27 +02:00
|
|
|
}
|
|
|
|
|
2013-11-15 08:50:02 +01:00
|
|
|
// 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
|
2015-01-07 01:26:26 +01:00
|
|
|
// rules, detecting orphan transactions, and insertion into the memory pool.
|
|
|
|
//
|
|
|
|
// If the transaction is an orphan (missing parent transactions), the
|
|
|
|
// transaction is NOT added to the orphan pool, but each unknown referenced
|
|
|
|
// parent is returned. Use ProcessTransaction instead if new orphans should
|
|
|
|
// be added to the orphan pool.
|
2013-11-15 08:50:02 +01:00
|
|
|
//
|
|
|
|
// This function is safe for concurrent access.
|
2015-02-05 22:16:39 +01:00
|
|
|
func (mp *txMemPool) MaybeAcceptTransaction(tx *btcutil.Tx, isNew, rateLimit bool) ([]*wire.ShaHash, error) {
|
2013-11-15 08:50:02 +01:00
|
|
|
// Protect concurrent access.
|
|
|
|
mp.Lock()
|
|
|
|
defer mp.Unlock()
|
|
|
|
|
2015-01-07 01:26:26 +01:00
|
|
|
return mp.maybeAcceptTransaction(tx, isNew, rateLimit)
|
2013-11-15 08:50:02 +01:00
|
|
|
}
|
|
|
|
|
2015-03-10 17:40:44 +01:00
|
|
|
// processOrphans is the internal function which implements the public
|
|
|
|
// ProcessOrphans. See the comment for ProcessOrphans for more details.
|
2013-10-22 01:20:31 +02:00
|
|
|
//
|
|
|
|
// This function MUST be called with the mempool lock held (for writes).
|
2015-02-05 22:16:39 +01:00
|
|
|
func (mp *txMemPool) processOrphans(hash *wire.ShaHash) error {
|
2013-09-20 19:55:27 +02:00
|
|
|
// Start with processing at least the passed hash.
|
|
|
|
processHashes := list.New()
|
|
|
|
processHashes.PushBack(hash)
|
|
|
|
for processHashes.Len() > 0 {
|
|
|
|
// Pop the first hash to process.
|
|
|
|
firstElement := processHashes.Remove(processHashes.Front())
|
2015-02-05 22:16:39 +01:00
|
|
|
processHash := firstElement.(*wire.ShaHash)
|
2013-09-20 19:55:27 +02:00
|
|
|
|
|
|
|
// Look up all orphans that are referenced by the transaction we
|
|
|
|
// just accepted. This will typically only be one, but it could
|
|
|
|
// be multiple if the referenced transaction contains multiple
|
|
|
|
// outputs. Skip to the next item on the list of hashes to
|
|
|
|
// process if there are none.
|
|
|
|
orphans, exists := mp.orphansByPrev[*processHash]
|
|
|
|
if !exists || orphans == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2013-10-07 17:10:10 +02:00
|
|
|
var enext *list.Element
|
|
|
|
for e := orphans.Front(); e != nil; e = enext {
|
|
|
|
enext = e.Next()
|
2013-10-28 21:44:38 +01:00
|
|
|
tx := e.Value.(*btcutil.Tx)
|
2013-09-20 19:55:27 +02:00
|
|
|
|
2015-01-07 01:26:26 +01:00
|
|
|
// Remove the orphan from the orphan pool. Current
|
|
|
|
// behavior requires that all saved orphans with
|
|
|
|
// a newly accepted parent are removed from the orphan
|
|
|
|
// pool and potentially added to the memory pool, but
|
|
|
|
// transactions which cannot be added to memory pool
|
|
|
|
// (including due to still being orphans) are expunged
|
|
|
|
// from the orphan pool.
|
|
|
|
//
|
|
|
|
// TODO(jrick): The above described behavior sounds
|
|
|
|
// like a bug, and I think we should investigate
|
|
|
|
// potentially moving orphans to the memory pool, but
|
|
|
|
// leaving them in the orphan pool if not all parent
|
|
|
|
// transactions are known yet.
|
2013-10-28 21:44:38 +01:00
|
|
|
orphanHash := tx.Sha()
|
|
|
|
mp.removeOrphan(orphanHash)
|
2013-09-20 19:55:27 +02:00
|
|
|
|
|
|
|
// Potentially accept the transaction into the
|
|
|
|
// transaction pool.
|
2015-01-07 01:26:26 +01:00
|
|
|
missingParents, err := mp.maybeAcceptTransaction(tx,
|
|
|
|
true, true)
|
2013-09-20 19:55:27 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-01-07 01:26:26 +01:00
|
|
|
if len(missingParents) == 0 {
|
|
|
|
// Generate and relay the inventory vector for the
|
|
|
|
// newly accepted transaction.
|
2015-02-05 22:16:39 +01:00
|
|
|
iv := wire.NewInvVect(wire.InvTypeTx, tx.Sha())
|
2015-01-29 06:22:27 +01:00
|
|
|
mp.server.RelayInventory(iv, tx)
|
2013-11-15 08:50:02 +01:00
|
|
|
} else {
|
2015-01-07 01:26:26 +01:00
|
|
|
// Transaction is still an orphan.
|
|
|
|
// TODO(jrick): This removeOrphan call is
|
|
|
|
// likely unnecessary as it was unconditionally
|
|
|
|
// removed above and maybeAcceptTransaction won't
|
|
|
|
// add it back.
|
2013-10-28 21:44:38 +01:00
|
|
|
mp.removeOrphan(orphanHash)
|
2013-09-20 19:55:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add this transaction to the list of transactions to
|
|
|
|
// process so any orphans that depend on this one are
|
|
|
|
// handled too.
|
2015-01-07 01:26:26 +01:00
|
|
|
//
|
|
|
|
// TODO(jrick): In the case that this is still an orphan,
|
|
|
|
// we know that any other transactions in the orphan
|
|
|
|
// pool with this orphan as their parent are still
|
|
|
|
// orphans as well, and should be removed. While
|
|
|
|
// recursively calling removeOrphan and
|
|
|
|
// maybeAcceptTransaction on these transactions is not
|
|
|
|
// wrong per se, it is overkill if all we care about is
|
|
|
|
// recursively removing child transactions of this
|
|
|
|
// orphan.
|
2013-10-28 21:44:38 +01:00
|
|
|
processHashes.PushBack(orphanHash)
|
2013-09-20 19:55:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-03-10 17:40:44 +01:00
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
// This function is safe for concurrent access.
|
|
|
|
func (mp *txMemPool) ProcessOrphans(hash *wire.ShaHash) error {
|
|
|
|
mp.Lock()
|
|
|
|
defer mp.Unlock()
|
|
|
|
|
|
|
|
return mp.processOrphans(hash)
|
|
|
|
}
|
|
|
|
|
2013-09-20 19:55:27 +02:00
|
|
|
// ProcessTransaction is the main workhorse for handling insertion of new
|
2013-12-25 19:28:54 +01:00
|
|
|
// free-standing transactions into the memory pool. It includes functionality
|
2013-09-20 19:55:27 +02:00
|
|
|
// such as rejecting duplicate transactions, ensuring transactions follow all
|
|
|
|
// rules, orphan transaction handling, and insertion into the memory pool.
|
2013-10-22 01:20:31 +02:00
|
|
|
//
|
|
|
|
// This function is safe for concurrent access.
|
2014-04-22 21:52:36 +02:00
|
|
|
func (mp *txMemPool) ProcessTransaction(tx *btcutil.Tx, allowOrphan, rateLimit bool) error {
|
2013-10-22 01:20:31 +02:00
|
|
|
// Protect concurrent access.
|
|
|
|
mp.Lock()
|
|
|
|
defer mp.Unlock()
|
|
|
|
|
2013-11-21 19:03:56 +01:00
|
|
|
txmpLog.Tracef("Processing transaction %v", tx.Sha())
|
2013-09-20 19:55:27 +02:00
|
|
|
|
|
|
|
// Potentially accept the transaction to the memory pool.
|
2015-01-07 01:26:26 +01:00
|
|
|
missingParents, err := mp.maybeAcceptTransaction(tx, true, rateLimit)
|
2013-09-20 19:55:27 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-01-07 01:26:26 +01:00
|
|
|
if len(missingParents) == 0 {
|
2013-11-15 08:50:02 +01:00
|
|
|
// Generate the inventory vector and relay it.
|
2015-02-05 22:16:39 +01:00
|
|
|
iv := wire.NewInvVect(wire.InvTypeTx, tx.Sha())
|
2015-01-29 06:22:27 +01:00
|
|
|
mp.server.RelayInventory(iv, tx)
|
2013-11-15 08:50:02 +01:00
|
|
|
|
2013-09-20 19:55:27 +02:00
|
|
|
// Accept any orphan transactions that depend on this
|
2015-01-07 01:26:26 +01:00
|
|
|
// transaction (they may no longer be orphans if all inputs
|
|
|
|
// are now available) and repeat for those accepted
|
|
|
|
// transactions until there are no more.
|
2013-10-30 20:13:29 +01:00
|
|
|
err := mp.processOrphans(tx.Sha())
|
2013-09-20 19:55:27 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
2014-03-17 23:32:30 +01:00
|
|
|
// The transaction is an orphan (has inputs missing). Reject
|
|
|
|
// it if the flag to allow orphans is not set.
|
|
|
|
if !allowOrphan {
|
2015-01-07 01:26:26 +01:00
|
|
|
// Only use the first missing parent transaction in
|
|
|
|
// the error message.
|
|
|
|
//
|
2014-07-09 05:01:13 +02:00
|
|
|
// NOTE: RejectDuplicate is really not an accurate
|
|
|
|
// reject code here, but it matches the reference
|
|
|
|
// implementation and there isn't a better choice due
|
|
|
|
// to the limited number of reject codes. Missing
|
|
|
|
// inputs is assumed to mean they are already spent
|
|
|
|
// which is not really always the case.
|
2015-01-07 01:26:26 +01:00
|
|
|
str := fmt.Sprintf("orphan transaction %v references "+
|
|
|
|
"outputs of unknown or fully-spent "+
|
|
|
|
"transaction %v", tx.Sha(), missingParents[0])
|
2015-02-05 22:16:39 +01:00
|
|
|
return txRuleError(wire.RejectDuplicate, str)
|
2014-03-17 23:32:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Potentially add the orphan transaction to the orphan pool.
|
2013-10-28 21:44:38 +01:00
|
|
|
err := mp.maybeAddOrphan(tx)
|
2013-09-20 19:55:27 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-05-04 06:51:54 +02:00
|
|
|
// 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 (mp *txMemPool) Count() int {
|
|
|
|
mp.RLock()
|
|
|
|
defer mp.RUnlock()
|
|
|
|
|
|
|
|
return len(mp.pool)
|
|
|
|
}
|
|
|
|
|
2013-10-08 20:34:04 +02:00
|
|
|
// TxShas returns a slice of hashes for all of the transactions in the memory
|
|
|
|
// pool.
|
2013-10-22 01:20:31 +02:00
|
|
|
//
|
|
|
|
// This function is safe for concurrent access.
|
2015-02-05 22:16:39 +01:00
|
|
|
func (mp *txMemPool) TxShas() []*wire.ShaHash {
|
2013-10-22 01:20:31 +02:00
|
|
|
mp.RLock()
|
|
|
|
defer mp.RUnlock()
|
2013-10-08 07:04:51 +02:00
|
|
|
|
2015-02-05 22:16:39 +01:00
|
|
|
hashes := make([]*wire.ShaHash, len(mp.pool))
|
2013-10-08 07:04:51 +02:00
|
|
|
i := 0
|
|
|
|
for hash := range mp.pool {
|
|
|
|
hashCopy := hash
|
|
|
|
hashes[i] = &hashCopy
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
|
|
|
|
return hashes
|
|
|
|
}
|
|
|
|
|
2013-12-11 18:32:16 +01:00
|
|
|
// 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.
|
|
|
|
func (mp *txMemPool) TxDescs() []*TxDesc {
|
|
|
|
mp.RLock()
|
|
|
|
defer mp.RUnlock()
|
|
|
|
|
|
|
|
descs := make([]*TxDesc, len(mp.pool))
|
|
|
|
i := 0
|
|
|
|
for _, desc := range mp.pool {
|
|
|
|
descs[i] = desc
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
|
|
|
|
return descs
|
|
|
|
}
|
|
|
|
|
2014-03-20 08:06:10 +01:00
|
|
|
// 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 (mp *txMemPool) LastUpdated() time.Time {
|
|
|
|
mp.RLock()
|
|
|
|
defer mp.RUnlock()
|
|
|
|
|
|
|
|
return mp.lastUpdated
|
|
|
|
}
|
|
|
|
|
2013-09-20 19:55:27 +02:00
|
|
|
// newTxMemPool returns a new memory pool for validating and storing standalone
|
|
|
|
// transactions until they are mined into a block.
|
|
|
|
func newTxMemPool(server *server) *txMemPool {
|
2015-01-04 02:42:01 +01:00
|
|
|
memPool := &txMemPool{
|
2013-09-20 19:55:27 +02:00
|
|
|
server: server,
|
2015-02-05 22:16:39 +01:00
|
|
|
pool: make(map[wire.ShaHash]*TxDesc),
|
|
|
|
orphans: make(map[wire.ShaHash]*btcutil.Tx),
|
|
|
|
orphansByPrev: make(map[wire.ShaHash]*list.List),
|
|
|
|
outpoints: make(map[wire.OutPoint]*btcutil.Tx),
|
2013-09-20 19:55:27 +02:00
|
|
|
}
|
2015-01-04 02:42:01 +01:00
|
|
|
if cfg.AddrIndex {
|
2015-03-19 14:56:06 +01:00
|
|
|
memPool.addrindex = make(map[string]map[wire.ShaHash]struct{})
|
2015-01-04 02:42:01 +01:00
|
|
|
}
|
|
|
|
return memPool
|
2013-09-20 19:55:27 +02:00
|
|
|
}
|