github.com/BlockABC/godash@v0.0.0-20191112120524-f4aa3a32c566/mempool.go (about) 1 // Copyright (c) 2013-2016 The btcsuite developers 2 // Copyright (c) 2016 The Dash developers 3 // Use of this source code is governed by an ISC 4 // license that can be found in the LICENSE file. 5 6 package main 7 8 import ( 9 "container/list" 10 "crypto/rand" 11 "fmt" 12 "math" 13 "math/big" 14 "sync" 15 "sync/atomic" 16 "time" 17 18 "github.com/BlockABC/godash/blockchain" 19 "github.com/BlockABC/godash/blockchain/indexers" 20 "github.com/BlockABC/godash/mining" 21 "github.com/BlockABC/godash/txscript" 22 "github.com/BlockABC/godash/wire" 23 "github.com/BlockABC/godashutil" 24 ) 25 26 const ( 27 // mempoolHeight is the height used for the "block" height field of the 28 // contextual transaction information provided in a transaction view. 29 mempoolHeight = 0x7fffffff 30 ) 31 32 // mempoolTxDesc is a descriptor containing a transaction in the mempool along 33 // with additional metadata. 34 type mempoolTxDesc struct { 35 mining.TxDesc 36 37 // StartingPriority is the priority of the transaction when it was added 38 // to the pool. 39 StartingPriority float64 40 } 41 42 // mempoolConfig is a descriptor containing the memory pool configuration. 43 type mempoolConfig struct { 44 // Policy defines the various mempool configuration options related 45 // to policy. 46 Policy mempoolPolicy 47 48 // FetchUtxoView defines the function to use to fetch unspent 49 // transaction output information. 50 FetchUtxoView func(*godashutil.Tx) (*blockchain.UtxoViewpoint, error) 51 52 // Chain defines the concurrent safe block chain instance which houses 53 // the current best chain. 54 Chain *blockchain.BlockChain 55 56 // SigCache defines a signature cache to use. 57 SigCache *txscript.SigCache 58 59 // TimeSource defines the timesource to use. 60 TimeSource blockchain.MedianTimeSource 61 62 // AddrIndex defines the optional address index instance to use for 63 // indexing the unconfirmed transactions in the memory pool. 64 // This can be nil if the address index is not enabled. 65 AddrIndex *indexers.AddrIndex 66 } 67 68 // mempoolPolicy houses the policy (configuration parameters) which is used to 69 // control the mempool. 70 type mempoolPolicy struct { 71 // DisableRelayPriority defines whether to relay free or low-fee 72 // transactions that do not have enough priority to be relayed. 73 DisableRelayPriority bool 74 75 // FreeTxRelayLimit defines the given amount in thousands of bytes 76 // per minute that transactions with no fee are rate limited to. 77 FreeTxRelayLimit float64 78 79 // MaxOrphanTxs is the maximum number of orphan transactions 80 // that can be queued. 81 MaxOrphanTxs int 82 83 // MaxOrphanTxSize is the maximum size allowed for orphan transactions. 84 // This helps prevent memory exhaustion attacks from sending a lot of 85 // of big orphans. 86 MaxOrphanTxSize int 87 88 // MaxSigOpsPerTx is the maximum number of signature operations 89 // in a single transaction we will relay or mine. It is a fraction 90 // of the max signature operations for a block. 91 MaxSigOpsPerTx int 92 93 // MinRelayTxFee defines the minimum transaction fee in BTC/kB to be 94 // considered a non-zero fee. 95 MinRelayTxFee godashutil.Amount 96 } 97 98 // txMemPool is used as a source of transactions that need to be mined into 99 // blocks and relayed to other peers. It is safe for concurrent access from 100 // multiple peers. 101 type txMemPool struct { 102 // The following variables must only be used atomically. 103 lastUpdated int64 // last time pool was updated 104 105 sync.RWMutex 106 cfg mempoolConfig 107 pool map[wire.ShaHash]*mempoolTxDesc 108 orphans map[wire.ShaHash]*godashutil.Tx 109 orphansByPrev map[wire.ShaHash]map[wire.ShaHash]*godashutil.Tx 110 outpoints map[wire.OutPoint]*godashutil.Tx 111 pennyTotal float64 // exponentially decaying total for penny spends. 112 lastPennyUnix int64 // unix time of last ``penny spend'' 113 } 114 115 // Ensure the txMemPool type implements the mining.TxSource interface. 116 var _ mining.TxSource = (*txMemPool)(nil) 117 118 // removeOrphan is the internal function which implements the public 119 // RemoveOrphan. See the comment for RemoveOrphan for more details. 120 // 121 // This function MUST be called with the mempool lock held (for writes). 122 func (mp *txMemPool) removeOrphan(txHash *wire.ShaHash) { 123 // Nothing to do if passed tx is not an orphan. 124 tx, exists := mp.orphans[*txHash] 125 if !exists { 126 return 127 } 128 129 // Remove the reference from the previous orphan index. 130 for _, txIn := range tx.MsgTx().TxIn { 131 originTxHash := txIn.PreviousOutPoint.Hash 132 if orphans, exists := mp.orphansByPrev[originTxHash]; exists { 133 delete(orphans, *tx.Sha()) 134 135 // Remove the map entry altogether if there are no 136 // longer any orphans which depend on it. 137 if len(orphans) == 0 { 138 delete(mp.orphansByPrev, originTxHash) 139 } 140 } 141 } 142 143 // Remove the transaction from the orphan pool. 144 delete(mp.orphans, *txHash) 145 } 146 147 // RemoveOrphan removes the passed orphan transaction from the orphan pool and 148 // previous orphan index. 149 // 150 // This function is safe for concurrent access. 151 func (mp *txMemPool) RemoveOrphan(txHash *wire.ShaHash) { 152 mp.Lock() 153 mp.removeOrphan(txHash) 154 mp.Unlock() 155 } 156 157 // limitNumOrphans limits the number of orphan transactions by evicting a random 158 // orphan if adding a new one would cause it to overflow the max allowed. 159 // 160 // This function MUST be called with the mempool lock held (for writes). 161 func (mp *txMemPool) limitNumOrphans() error { 162 if len(mp.orphans)+1 > mp.cfg.Policy.MaxOrphanTxs && 163 mp.cfg.Policy.MaxOrphanTxs > 0 { 164 165 // Generate a cryptographically random hash. 166 randHashBytes := make([]byte, wire.HashSize) 167 _, err := rand.Read(randHashBytes) 168 if err != nil { 169 return err 170 } 171 randHashNum := new(big.Int).SetBytes(randHashBytes) 172 173 // Try to find the first entry that is greater than the random 174 // hash. Use the first entry (which is already pseudorandom due 175 // to Go's range statement over maps) as a fallback if none of 176 // the hashes in the orphan pool are larger than the random 177 // hash. 178 var foundHash *wire.ShaHash 179 for txHash := range mp.orphans { 180 if foundHash == nil { 181 foundHash = &txHash 182 } 183 txHashNum := blockchain.ShaHashToBig(&txHash) 184 if txHashNum.Cmp(randHashNum) > 0 { 185 foundHash = &txHash 186 break 187 } 188 } 189 190 mp.removeOrphan(foundHash) 191 } 192 193 return nil 194 } 195 196 // addOrphan adds an orphan transaction to the orphan pool. 197 // 198 // This function MUST be called with the mempool lock held (for writes). 199 func (mp *txMemPool) addOrphan(tx *godashutil.Tx) { 200 // Limit the number orphan transactions to prevent memory exhaustion. A 201 // random orphan is evicted to make room if needed. 202 mp.limitNumOrphans() 203 204 mp.orphans[*tx.Sha()] = tx 205 for _, txIn := range tx.MsgTx().TxIn { 206 originTxHash := txIn.PreviousOutPoint.Hash 207 if _, exists := mp.orphansByPrev[originTxHash]; !exists { 208 mp.orphansByPrev[originTxHash] = 209 make(map[wire.ShaHash]*godashutil.Tx) 210 } 211 mp.orphansByPrev[originTxHash][*tx.Sha()] = tx 212 } 213 214 txmpLog.Debugf("Stored orphan transaction %v (total: %d)", tx.Sha(), 215 len(mp.orphans)) 216 } 217 218 // maybeAddOrphan potentially adds an orphan to the orphan pool. 219 // 220 // This function MUST be called with the mempool lock held (for writes). 221 func (mp *txMemPool) maybeAddOrphan(tx *godashutil.Tx) error { 222 // Ignore orphan transactions that are too large. This helps avoid 223 // a memory exhaustion attack based on sending a lot of really large 224 // orphans. In the case there is a valid transaction larger than this, 225 // it will ultimtely be rebroadcast after the parent transactions 226 // have been mined or otherwise received. 227 // 228 // Note that the number of orphan transactions in the orphan pool is 229 // also limited, so this equates to a maximum memory used of 230 // mp.cfg.Policy.MaxOrphanTxSize * mp.cfg.Policy.MaxOrphanTxs (which is ~5MB 231 // using the default values at the time this comment was written). 232 serializedLen := tx.MsgTx().SerializeSize() 233 if serializedLen > mp.cfg.Policy.MaxOrphanTxSize { 234 str := fmt.Sprintf("orphan transaction size of %d bytes is "+ 235 "larger than max allowed size of %d bytes", 236 serializedLen, mp.cfg.Policy.MaxOrphanTxSize) 237 return txRuleError(wire.RejectNonstandard, str) 238 } 239 240 // Add the orphan if the none of the above disqualified it. 241 mp.addOrphan(tx) 242 243 return nil 244 } 245 246 // isTransactionInPool returns whether or not the passed transaction already 247 // exists in the main pool. 248 // 249 // This function MUST be called with the mempool lock held (for reads). 250 func (mp *txMemPool) isTransactionInPool(hash *wire.ShaHash) bool { 251 if _, exists := mp.pool[*hash]; exists { 252 return true 253 } 254 255 return false 256 } 257 258 // IsTransactionInPool returns whether or not the passed transaction already 259 // exists in the main pool. 260 // 261 // This function is safe for concurrent access. 262 func (mp *txMemPool) IsTransactionInPool(hash *wire.ShaHash) bool { 263 // Protect concurrent access. 264 mp.RLock() 265 defer mp.RUnlock() 266 267 return mp.isTransactionInPool(hash) 268 } 269 270 // isOrphanInPool returns whether or not the passed transaction already exists 271 // in the orphan pool. 272 // 273 // This function MUST be called with the mempool lock held (for reads). 274 func (mp *txMemPool) isOrphanInPool(hash *wire.ShaHash) bool { 275 if _, exists := mp.orphans[*hash]; exists { 276 return true 277 } 278 279 return false 280 } 281 282 // IsOrphanInPool returns whether or not the passed transaction already exists 283 // in the orphan pool. 284 // 285 // This function is safe for concurrent access. 286 func (mp *txMemPool) IsOrphanInPool(hash *wire.ShaHash) bool { 287 // Protect concurrent access. 288 mp.RLock() 289 defer mp.RUnlock() 290 291 return mp.isOrphanInPool(hash) 292 } 293 294 // haveTransaction returns whether or not the passed transaction already exists 295 // in the main pool or in the orphan pool. 296 // 297 // This function MUST be called with the mempool lock held (for reads). 298 func (mp *txMemPool) haveTransaction(hash *wire.ShaHash) bool { 299 return mp.isTransactionInPool(hash) || mp.isOrphanInPool(hash) 300 } 301 302 // HaveTransaction returns whether or not the passed transaction already exists 303 // in the main pool or in the orphan pool. 304 // 305 // This function is safe for concurrent access. 306 func (mp *txMemPool) HaveTransaction(hash *wire.ShaHash) bool { 307 // Protect concurrent access. 308 mp.RLock() 309 defer mp.RUnlock() 310 311 return mp.haveTransaction(hash) 312 } 313 314 // removeTransaction is the internal function which implements the public 315 // RemoveTransaction. See the comment for RemoveTransaction for more details. 316 // 317 // This function MUST be called with the mempool lock held (for writes). 318 func (mp *txMemPool) removeTransaction(tx *godashutil.Tx, removeRedeemers bool) { 319 txHash := tx.Sha() 320 if removeRedeemers { 321 // Remove any transactions which rely on this one. 322 for i := uint32(0); i < uint32(len(tx.MsgTx().TxOut)); i++ { 323 outpoint := wire.NewOutPoint(txHash, i) 324 if txRedeemer, exists := mp.outpoints[*outpoint]; exists { 325 mp.removeTransaction(txRedeemer, true) 326 } 327 } 328 } 329 330 // Remove the transaction if needed. 331 if txDesc, exists := mp.pool[*txHash]; exists { 332 // Remove unconfirmed address index entries associated with the 333 // transaction if enabled. 334 if mp.cfg.AddrIndex != nil { 335 mp.cfg.AddrIndex.RemoveUnconfirmedTx(txHash) 336 } 337 338 // Mark the referenced outpoints as unspent by the pool. 339 for _, txIn := range txDesc.Tx.MsgTx().TxIn { 340 delete(mp.outpoints, txIn.PreviousOutPoint) 341 } 342 delete(mp.pool, *txHash) 343 atomic.StoreInt64(&mp.lastUpdated, time.Now().Unix()) 344 } 345 } 346 347 // RemoveTransaction removes the passed transaction from the mempool. When the 348 // removeRedeemers flag is set, any transactions that redeem outputs from the 349 // removed transaction will also be removed recursively from the mempool, as 350 // they would otherwise become orphans. 351 // 352 // This function is safe for concurrent access. 353 func (mp *txMemPool) RemoveTransaction(tx *godashutil.Tx, removeRedeemers bool) { 354 // Protect concurrent access. 355 mp.Lock() 356 defer mp.Unlock() 357 358 mp.removeTransaction(tx, removeRedeemers) 359 } 360 361 // RemoveDoubleSpends removes all transactions which spend outputs spent by the 362 // passed transaction from the memory pool. Removing those transactions then 363 // leads to removing all transactions which rely on them, recursively. This is 364 // necessary when a block is connected to the main chain because the block may 365 // contain transactions which were previously unknown to the memory pool. 366 // 367 // This function is safe for concurrent access. 368 func (mp *txMemPool) RemoveDoubleSpends(tx *godashutil.Tx) { 369 // Protect concurrent access. 370 mp.Lock() 371 defer mp.Unlock() 372 373 for _, txIn := range tx.MsgTx().TxIn { 374 if txRedeemer, ok := mp.outpoints[txIn.PreviousOutPoint]; ok { 375 if !txRedeemer.Sha().IsEqual(tx.Sha()) { 376 mp.removeTransaction(txRedeemer, true) 377 } 378 } 379 } 380 } 381 382 // addTransaction adds the passed transaction to the memory pool. It should 383 // not be called directly as it doesn't perform any validation. This is a 384 // helper for maybeAcceptTransaction. 385 // 386 // This function MUST be called with the mempool lock held (for writes). 387 func (mp *txMemPool) addTransaction(utxoView *blockchain.UtxoViewpoint, tx *godashutil.Tx, height int32, fee int64) { 388 // Add the transaction to the pool and mark the referenced outpoints 389 // as spent by the pool. 390 mp.pool[*tx.Sha()] = &mempoolTxDesc{ 391 TxDesc: mining.TxDesc{ 392 Tx: tx, 393 Added: time.Now(), 394 Height: height, 395 Fee: fee, 396 }, 397 StartingPriority: calcPriority(tx.MsgTx(), utxoView, height), 398 } 399 for _, txIn := range tx.MsgTx().TxIn { 400 mp.outpoints[txIn.PreviousOutPoint] = tx 401 } 402 atomic.StoreInt64(&mp.lastUpdated, time.Now().Unix()) 403 404 // Add unconfirmed address index entries associated with the transaction 405 // if enabled. 406 if mp.cfg.AddrIndex != nil { 407 mp.cfg.AddrIndex.AddUnconfirmedTx(tx, utxoView) 408 } 409 } 410 411 // checkPoolDoubleSpend checks whether or not the passed transaction is 412 // attempting to spend coins already spent by other transactions in the pool. 413 // Note it does not check for double spends against transactions already in the 414 // main chain. 415 // 416 // This function MUST be called with the mempool lock held (for reads). 417 func (mp *txMemPool) checkPoolDoubleSpend(tx *godashutil.Tx) error { 418 for _, txIn := range tx.MsgTx().TxIn { 419 if txR, exists := mp.outpoints[txIn.PreviousOutPoint]; exists { 420 str := fmt.Sprintf("output %v already spent by "+ 421 "transaction %v in the memory pool", 422 txIn.PreviousOutPoint, txR.Sha()) 423 return txRuleError(wire.RejectDuplicate, str) 424 } 425 } 426 427 return nil 428 } 429 430 // fetchInputUtxos loads utxo details about the input transactions referenced by 431 // the passed transaction. First, it loads the details form the viewpoint of 432 // the main chain, then it adjusts them based upon the contents of the 433 // transaction pool. 434 // 435 // This function MUST be called with the mempool lock held (for reads). 436 func (mp *txMemPool) fetchInputUtxos(tx *godashutil.Tx) (*blockchain.UtxoViewpoint, error) { 437 utxoView, err := mp.cfg.FetchUtxoView(tx) 438 if err != nil { 439 return nil, err 440 } 441 442 // Attempt to populate any missing inputs from the transaction pool. 443 for originHash, entry := range utxoView.Entries() { 444 if entry != nil && !entry.IsFullySpent() { 445 continue 446 } 447 448 if poolTxDesc, exists := mp.pool[originHash]; exists { 449 utxoView.AddTxOuts(poolTxDesc.Tx, mempoolHeight) 450 } 451 } 452 return utxoView, nil 453 } 454 455 // FetchTransaction returns the requested transaction from the transaction pool. 456 // This only fetches from the main transaction pool and does not include 457 // orphans. 458 // 459 // This function is safe for concurrent access. 460 func (mp *txMemPool) FetchTransaction(txHash *wire.ShaHash) (*godashutil.Tx, error) { 461 // Protect concurrent access. 462 mp.RLock() 463 defer mp.RUnlock() 464 465 if txDesc, exists := mp.pool[*txHash]; exists { 466 return txDesc.Tx, nil 467 } 468 469 return nil, fmt.Errorf("transaction is not in the pool") 470 } 471 472 // maybeAcceptTransaction is the internal function which implements the public 473 // MaybeAcceptTransaction. See the comment for MaybeAcceptTransaction for 474 // more details. 475 // 476 // This function MUST be called with the mempool lock held (for writes). 477 func (mp *txMemPool) maybeAcceptTransaction(tx *godashutil.Tx, isNew, rateLimit bool) ([]*wire.ShaHash, error) { 478 txHash := tx.Sha() 479 480 // Don't accept the transaction if it already exists in the pool. This 481 // applies to orphan transactions as well. This check is intended to 482 // be a quick check to weed out duplicates. 483 if mp.haveTransaction(txHash) { 484 str := fmt.Sprintf("already have transaction %v", txHash) 485 return nil, txRuleError(wire.RejectDuplicate, str) 486 } 487 488 // Perform preliminary sanity checks on the transaction. This makes 489 // use of btcchain which contains the invariant rules for what 490 // transactions are allowed into blocks. 491 err := blockchain.CheckTransactionSanity(tx) 492 if err != nil { 493 if cerr, ok := err.(blockchain.RuleError); ok { 494 return nil, chainRuleError(cerr) 495 } 496 return nil, err 497 } 498 499 // A standalone transaction must not be a coinbase transaction. 500 if blockchain.IsCoinBase(tx) { 501 str := fmt.Sprintf("transaction %v is an individual coinbase", 502 txHash) 503 return nil, txRuleError(wire.RejectInvalid, str) 504 } 505 506 // Don't accept transactions with a lock time after the maximum int32 507 // value for now. This is an artifact of older bitcoind clients which 508 // treated this field as an int32 and would treat anything larger 509 // incorrectly (as negative). 510 if tx.MsgTx().LockTime > math.MaxInt32 { 511 str := fmt.Sprintf("transaction %v has a lock time after "+ 512 "2038 which is not accepted yet", txHash) 513 return nil, txRuleError(wire.RejectNonstandard, str) 514 } 515 516 // Get the current height of the main chain. A standalone transaction 517 // will be mined into the next block at best, so its height is at least 518 // one more than the current height. 519 best := mp.cfg.Chain.BestSnapshot() 520 nextBlockHeight := best.Height + 1 521 522 // Don't allow non-standard transactions if the network parameters 523 // forbid their relaying. 524 if !activeNetParams.RelayNonStdTxs { 525 err := checkTransactionStandard(tx, nextBlockHeight, 526 mp.cfg.TimeSource, mp.cfg.Policy.MinRelayTxFee) 527 if err != nil { 528 // Attempt to extract a reject code from the error so 529 // it can be retained. When not possible, fall back to 530 // a non standard error. 531 rejectCode, found := extractRejectCode(err) 532 if !found { 533 rejectCode = wire.RejectNonstandard 534 } 535 str := fmt.Sprintf("transaction %v is not standard: %v", 536 txHash, err) 537 return nil, txRuleError(rejectCode, str) 538 } 539 } 540 541 // The transaction may not use any of the same outputs as other 542 // transactions already in the pool as that would ultimately result in a 543 // double spend. This check is intended to be quick and therefore only 544 // detects double spends within the transaction pool itself. The 545 // transaction could still be double spending coins from the main chain 546 // at this point. There is a more in-depth check that happens later 547 // after fetching the referenced transaction inputs from the main chain 548 // which examines the actual spend data and prevents double spends. 549 err = mp.checkPoolDoubleSpend(tx) 550 if err != nil { 551 return nil, err 552 } 553 554 // Fetch all of the unspent transaction outputs referenced by the inputs 555 // to this transaction. This function also attempts to fetch the 556 // transaction itself to be used for detecting a duplicate transaction 557 // without needing to do a separate lookup. 558 utxoView, err := mp.fetchInputUtxos(tx) 559 if err != nil { 560 if cerr, ok := err.(blockchain.RuleError); ok { 561 return nil, chainRuleError(cerr) 562 } 563 return nil, err 564 } 565 566 // Don't allow the transaction if it exists in the main chain and is not 567 // not already fully spent. 568 txEntry := utxoView.LookupEntry(txHash) 569 if txEntry != nil && !txEntry.IsFullySpent() { 570 return nil, txRuleError(wire.RejectDuplicate, 571 "transaction already exists") 572 } 573 delete(utxoView.Entries(), *txHash) 574 575 // Transaction is an orphan if any of the referenced input transactions 576 // don't exist. Adding orphans to the orphan pool is not handled by 577 // this function, and the caller should use maybeAddOrphan if this 578 // behavior is desired. 579 var missingParents []*wire.ShaHash 580 for originHash, entry := range utxoView.Entries() { 581 if entry == nil || entry.IsFullySpent() { 582 // Must make a copy of the hash here since the iterator 583 // is replaced and taking its address directly would 584 // result in all of the entries pointing to the same 585 // memory location and thus all be the final hash. 586 hashCopy := originHash 587 missingParents = append(missingParents, &hashCopy) 588 } 589 } 590 if len(missingParents) > 0 { 591 return missingParents, nil 592 } 593 594 // Perform several checks on the transaction inputs using the invariant 595 // rules in btcchain for what transactions are allowed into blocks. 596 // Also returns the fees associated with the transaction which will be 597 // used later. 598 txFee, err := blockchain.CheckTransactionInputs(tx, nextBlockHeight, 599 utxoView) 600 if err != nil { 601 if cerr, ok := err.(blockchain.RuleError); ok { 602 return nil, chainRuleError(cerr) 603 } 604 return nil, err 605 } 606 607 // Don't allow transactions with non-standard inputs if the network 608 // parameters forbid their relaying. 609 if !activeNetParams.RelayNonStdTxs { 610 err := checkInputsStandard(tx, utxoView) 611 if err != nil { 612 // Attempt to extract a reject code from the error so 613 // it can be retained. When not possible, fall back to 614 // a non standard error. 615 rejectCode, found := extractRejectCode(err) 616 if !found { 617 rejectCode = wire.RejectNonstandard 618 } 619 str := fmt.Sprintf("transaction %v has a non-standard "+ 620 "input: %v", txHash, err) 621 return nil, txRuleError(rejectCode, str) 622 } 623 } 624 625 // NOTE: if you modify this code to accept non-standard transactions, 626 // you should add code here to check that the transaction does a 627 // reasonable number of ECDSA signature verifications. 628 629 // Don't allow transactions with an excessive number of signature 630 // operations which would result in making it impossible to mine. Since 631 // the coinbase address itself can contain signature operations, the 632 // maximum allowed signature operations per transaction is less than 633 // the maximum allowed signature operations per block. 634 numSigOps, err := blockchain.CountP2SHSigOps(tx, false, utxoView) 635 if err != nil { 636 if cerr, ok := err.(blockchain.RuleError); ok { 637 return nil, chainRuleError(cerr) 638 } 639 return nil, err 640 } 641 numSigOps += blockchain.CountSigOps(tx) 642 if numSigOps > mp.cfg.Policy.MaxSigOpsPerTx { 643 str := fmt.Sprintf("transaction %v has too many sigops: %d > %d", 644 txHash, numSigOps, mp.cfg.Policy.MaxSigOpsPerTx) 645 return nil, txRuleError(wire.RejectNonstandard, str) 646 } 647 648 // Don't allow transactions with fees too low to get into a mined block. 649 // 650 // Most miners allow a free transaction area in blocks they mine to go 651 // alongside the area used for high-priority transactions as well as 652 // transactions with fees. A transaction size of up to 1000 bytes is 653 // considered safe to go into this section. Further, the minimum fee 654 // calculated below on its own would encourage several small 655 // transactions to avoid fees rather than one single larger transaction 656 // which is more desirable. Therefore, as long as the size of the 657 // transaction does not exceeed 1000 less than the reserved space for 658 // high-priority transactions, don't require a fee for it. 659 serializedSize := int64(tx.MsgTx().SerializeSize()) 660 minFee := calcMinRequiredTxRelayFee(serializedSize, 661 mp.cfg.Policy.MinRelayTxFee) 662 if serializedSize >= (defaultBlockPrioritySize-1000) && txFee < minFee { 663 str := fmt.Sprintf("transaction %v has %d fees which is under "+ 664 "the required amount of %d", txHash, txFee, 665 minFee) 666 return nil, txRuleError(wire.RejectInsufficientFee, str) 667 } 668 669 // Require that free transactions have sufficient priority to be mined 670 // in the next block. Transactions which are being added back to the 671 // memory pool from blocks that have been disconnected during a reorg 672 // are exempted. 673 if isNew && !mp.cfg.Policy.DisableRelayPriority && txFee < minFee { 674 currentPriority := calcPriority(tx.MsgTx(), utxoView, 675 nextBlockHeight) 676 if currentPriority <= minHighPriority { 677 str := fmt.Sprintf("transaction %v has insufficient "+ 678 "priority (%g <= %g)", txHash, 679 currentPriority, minHighPriority) 680 return nil, txRuleError(wire.RejectInsufficientFee, str) 681 } 682 } 683 684 // Free-to-relay transactions are rate limited here to prevent 685 // penny-flooding with tiny transactions as a form of attack. 686 if rateLimit && txFee < minFee { 687 nowUnix := time.Now().Unix() 688 // we decay passed data with an exponentially decaying ~10 689 // minutes window - matches bitcoind handling. 690 mp.pennyTotal *= math.Pow(1.0-1.0/600.0, 691 float64(nowUnix-mp.lastPennyUnix)) 692 mp.lastPennyUnix = nowUnix 693 694 // Are we still over the limit? 695 if mp.pennyTotal >= mp.cfg.Policy.FreeTxRelayLimit*10*1000 { 696 str := fmt.Sprintf("transaction %v has been rejected "+ 697 "by the rate limiter due to low fees", txHash) 698 return nil, txRuleError(wire.RejectInsufficientFee, str) 699 } 700 oldTotal := mp.pennyTotal 701 702 mp.pennyTotal += float64(serializedSize) 703 txmpLog.Tracef("rate limit: curTotal %v, nextTotal: %v, "+ 704 "limit %v", oldTotal, mp.pennyTotal, 705 mp.cfg.Policy.FreeTxRelayLimit*10*1000) 706 } 707 708 // Verify crypto signatures for each input and reject the transaction if 709 // any don't verify. 710 err = blockchain.ValidateTransactionScripts(tx, utxoView, 711 txscript.StandardVerifyFlags, mp.cfg.SigCache) 712 if err != nil { 713 if cerr, ok := err.(blockchain.RuleError); ok { 714 return nil, chainRuleError(cerr) 715 } 716 return nil, err 717 } 718 719 // Add to transaction pool. 720 mp.addTransaction(utxoView, tx, best.Height, txFee) 721 722 txmpLog.Debugf("Accepted transaction %v (pool size: %v)", txHash, 723 len(mp.pool)) 724 725 return nil, nil 726 } 727 728 // MaybeAcceptTransaction is the main workhorse for handling insertion of new 729 // free-standing transactions into a memory pool. It includes functionality 730 // such as rejecting duplicate transactions, ensuring transactions follow all 731 // rules, detecting orphan transactions, and insertion into the memory pool. 732 // 733 // If the transaction is an orphan (missing parent transactions), the 734 // transaction is NOT added to the orphan pool, but each unknown referenced 735 // parent is returned. Use ProcessTransaction instead if new orphans should 736 // be added to the orphan pool. 737 // 738 // This function is safe for concurrent access. 739 func (mp *txMemPool) MaybeAcceptTransaction(tx *godashutil.Tx, isNew, rateLimit bool) ([]*wire.ShaHash, error) { 740 // Protect concurrent access. 741 mp.Lock() 742 defer mp.Unlock() 743 744 return mp.maybeAcceptTransaction(tx, isNew, rateLimit) 745 } 746 747 // processOrphans is the internal function which implements the public 748 // ProcessOrphans. See the comment for ProcessOrphans for more details. 749 // 750 // This function MUST be called with the mempool lock held (for writes). 751 func (mp *txMemPool) processOrphans(hash *wire.ShaHash) []*godashutil.Tx { 752 var acceptedTxns []*godashutil.Tx 753 754 // Start with processing at least the passed hash. 755 processHashes := list.New() 756 processHashes.PushBack(hash) 757 for processHashes.Len() > 0 { 758 // Pop the first hash to process. 759 firstElement := processHashes.Remove(processHashes.Front()) 760 processHash := firstElement.(*wire.ShaHash) 761 762 // Look up all orphans that are referenced by the transaction we 763 // just accepted. This will typically only be one, but it could 764 // be multiple if the referenced transaction contains multiple 765 // outputs. Skip to the next item on the list of hashes to 766 // process if there are none. 767 orphans, exists := mp.orphansByPrev[*processHash] 768 if !exists || orphans == nil { 769 continue 770 } 771 772 for _, tx := range orphans { 773 // Remove the orphan from the orphan pool. Current 774 // behavior requires that all saved orphans with 775 // a newly accepted parent are removed from the orphan 776 // pool and potentially added to the memory pool, but 777 // transactions which cannot be added to memory pool 778 // (including due to still being orphans) are expunged 779 // from the orphan pool. 780 // 781 // TODO(jrick): The above described behavior sounds 782 // like a bug, and I think we should investigate 783 // potentially moving orphans to the memory pool, but 784 // leaving them in the orphan pool if not all parent 785 // transactions are known yet. 786 orphanHash := tx.Sha() 787 mp.removeOrphan(orphanHash) 788 789 // Potentially accept the transaction into the 790 // transaction pool. 791 missingParents, err := mp.maybeAcceptTransaction(tx, 792 true, true) 793 if err != nil { 794 // TODO: Remove orphans that depend on this 795 // failed transaction. 796 txmpLog.Debugf("Unable to move "+ 797 "orphan transaction %v to mempool: %v", 798 tx.Sha(), err) 799 continue 800 } 801 802 if len(missingParents) > 0 { 803 // Transaction is still an orphan, so add it 804 // back. 805 mp.addOrphan(tx) 806 continue 807 } 808 809 // Add this transaction to the list of transactions 810 // that are no longer orphans. 811 acceptedTxns = append(acceptedTxns, tx) 812 813 // Add this transaction to the list of transactions to 814 // process so any orphans that depend on this one are 815 // handled too. 816 // 817 // TODO(jrick): In the case that this is still an orphan, 818 // we know that any other transactions in the orphan 819 // pool with this orphan as their parent are still 820 // orphans as well, and should be removed. While 821 // recursively calling removeOrphan and 822 // maybeAcceptTransaction on these transactions is not 823 // wrong per se, it is overkill if all we care about is 824 // recursively removing child transactions of this 825 // orphan. 826 processHashes.PushBack(orphanHash) 827 } 828 } 829 830 return acceptedTxns 831 } 832 833 // ProcessOrphans determines if there are any orphans which depend on the passed 834 // transaction hash (it is possible that they are no longer orphans) and 835 // potentially accepts them to the memory pool. It repeats the process for the 836 // newly accepted transactions (to detect further orphans which may no longer be 837 // orphans) until there are no more. 838 // 839 // It returns a slice of transactions added to the mempool. A nil slice means 840 // no transactions were moved from the orphan pool to the mempool. 841 // 842 // This function is safe for concurrent access. 843 func (mp *txMemPool) ProcessOrphans(hash *wire.ShaHash) []*godashutil.Tx { 844 mp.Lock() 845 acceptedTxns := mp.processOrphans(hash) 846 mp.Unlock() 847 848 return acceptedTxns 849 } 850 851 // ProcessTransaction is the main workhorse for handling insertion of new 852 // free-standing transactions into the memory pool. It includes functionality 853 // such as rejecting duplicate transactions, ensuring transactions follow all 854 // rules, orphan transaction handling, and insertion into the memory pool. 855 // 856 // It returns a slice of transactions added to the mempool. When the 857 // error is nil, the list will include the passed transaction itself along 858 // with any additional orphan transaactions that were added as a result of 859 // the passed one being accepted. 860 // 861 // This function is safe for concurrent access. 862 func (mp *txMemPool) ProcessTransaction(tx *godashutil.Tx, allowOrphan, rateLimit bool) ([]*godashutil.Tx, error) { 863 // Protect concurrent access. 864 mp.Lock() 865 defer mp.Unlock() 866 867 txmpLog.Tracef("Processing transaction %v", tx.Sha()) 868 869 // Potentially accept the transaction to the memory pool. 870 missingParents, err := mp.maybeAcceptTransaction(tx, true, rateLimit) 871 if err != nil { 872 return nil, err 873 } 874 875 if len(missingParents) == 0 { 876 // Accept any orphan transactions that depend on this 877 // transaction (they may no longer be orphans if all inputs 878 // are now available) and repeat for those accepted 879 // transactions until there are no more. 880 newTxs := mp.processOrphans(tx.Sha()) 881 acceptedTxs := make([]*godashutil.Tx, len(newTxs)+1) 882 883 // Add the parent transaction first so remote nodes 884 // do not add orphans. 885 acceptedTxs[0] = tx 886 copy(acceptedTxs[1:], newTxs) 887 888 return acceptedTxs, nil 889 } 890 891 // The transaction is an orphan (has inputs missing). Reject 892 // it if the flag to allow orphans is not set. 893 if !allowOrphan { 894 // Only use the first missing parent transaction in 895 // the error message. 896 // 897 // NOTE: RejectDuplicate is really not an accurate 898 // reject code here, but it matches the reference 899 // implementation and there isn't a better choice due 900 // to the limited number of reject codes. Missing 901 // inputs is assumed to mean they are already spent 902 // which is not really always the case. 903 str := fmt.Sprintf("orphan transaction %v references "+ 904 "outputs of unknown or fully-spent "+ 905 "transaction %v", tx.Sha(), missingParents[0]) 906 return nil, txRuleError(wire.RejectDuplicate, str) 907 } 908 909 // Potentially add the orphan transaction to the orphan pool. 910 err = mp.maybeAddOrphan(tx) 911 if err != nil { 912 return nil, err 913 } 914 915 return nil, nil 916 } 917 918 // Count returns the number of transactions in the main pool. It does not 919 // include the orphan pool. 920 // 921 // This function is safe for concurrent access. 922 func (mp *txMemPool) Count() int { 923 mp.RLock() 924 defer mp.RUnlock() 925 926 return len(mp.pool) 927 } 928 929 // TxShas returns a slice of hashes for all of the transactions in the memory 930 // pool. 931 // 932 // This function is safe for concurrent access. 933 func (mp *txMemPool) TxShas() []*wire.ShaHash { 934 mp.RLock() 935 defer mp.RUnlock() 936 937 hashes := make([]*wire.ShaHash, len(mp.pool)) 938 i := 0 939 for hash := range mp.pool { 940 hashCopy := hash 941 hashes[i] = &hashCopy 942 i++ 943 } 944 945 return hashes 946 } 947 948 // TxDescs returns a slice of descriptors for all the transactions in the pool. 949 // The descriptors are to be treated as read only. 950 // 951 // This function is safe for concurrent access. 952 func (mp *txMemPool) TxDescs() []*mempoolTxDesc { 953 mp.RLock() 954 defer mp.RUnlock() 955 956 descs := make([]*mempoolTxDesc, len(mp.pool)) 957 i := 0 958 for _, desc := range mp.pool { 959 descs[i] = desc 960 i++ 961 } 962 963 return descs 964 } 965 966 // MiningDescs returns a slice of mining descriptors for all the transactions 967 // in the pool. 968 // 969 // This is part of the mining.TxSource interface implementation and is safe for 970 // concurrent access as required by the interface contract. 971 func (mp *txMemPool) MiningDescs() []*mining.TxDesc { 972 mp.RLock() 973 defer mp.RUnlock() 974 975 descs := make([]*mining.TxDesc, len(mp.pool)) 976 i := 0 977 for _, desc := range mp.pool { 978 descs[i] = &desc.TxDesc 979 i++ 980 } 981 982 return descs 983 } 984 985 // LastUpdated returns the last time a transaction was added to or removed from 986 // the main pool. It does not include the orphan pool. 987 // 988 // This function is safe for concurrent access. 989 func (mp *txMemPool) LastUpdated() time.Time { 990 return time.Unix(atomic.LoadInt64(&mp.lastUpdated), 0) 991 } 992 993 // newTxMemPool returns a new memory pool for validating and storing standalone 994 // transactions until they are mined into a block. 995 func newTxMemPool(cfg *mempoolConfig) *txMemPool { 996 memPool := &txMemPool{ 997 cfg: *cfg, 998 pool: make(map[wire.ShaHash]*mempoolTxDesc), 999 orphans: make(map[wire.ShaHash]*godashutil.Tx), 1000 orphansByPrev: make(map[wire.ShaHash]map[wire.ShaHash]*godashutil.Tx), 1001 outpoints: make(map[wire.OutPoint]*godashutil.Tx), 1002 } 1003 return memPool 1004 }