* Return a height-based locktime for new transactions (uses the height of the * current chain tip unless we are not synced with the current chain */
| 773 | * current chain tip unless we are not synced with the current chain |
| 774 | */ |
| 775 | static uint32_t GetLocktimeForNewTransaction(interfaces::Chain& chain, const uint256& block_hash, int block_height) |
| 776 | { |
| 777 | uint32_t locktime; |
| 778 | // Discourage fee sniping. |
| 779 | // |
| 780 | // For a large miner the value of the transactions in the best block and |
| 781 | // the mempool can exceed the cost of deliberately attempting to mine two |
| 782 | // blocks to orphan the current best block. By setting nLockTime such that |
| 783 | // only the next block can include the transaction, we discourage this |
| 784 | // practice as the height restricted and limited blocksize gives miners |
| 785 | // considering fee sniping fewer options for pulling off this attack. |
| 786 | // |
| 787 | // A simple way to think about this is from the wallet's point of view we |
| 788 | // always want the blockchain to move forward. By setting nLockTime this |
| 789 | // way we're basically making the statement that we only want this |
| 790 | // transaction to appear in the next block; we don't want to potentially |
| 791 | // encourage reorgs by allowing transactions to appear at lower heights |
| 792 | // than the next block in forks of the best chain. |
| 793 | // |
| 794 | // Of course, the subsidy is high enough, and transaction volume low |
| 795 | // enough, that fee sniping isn't a problem yet, but by implementing a fix |
| 796 | // now we ensure code won't be written that makes assumptions about |
| 797 | // nLockTime that preclude a fix later. |
| 798 | if (IsCurrentForAntiFeeSniping(chain, block_hash)) { |
| 799 | locktime = block_height; |
| 800 | |
| 801 | // Secondly occasionally randomly pick a nLockTime even further back, so |
| 802 | // that transactions that are delayed after signing for whatever reason, |
| 803 | // e.g. high-latency mix networks and some CoinJoin implementations, have |
| 804 | // better privacy. |
| 805 | if (GetRandInt(10) == 0) |
| 806 | locktime = std::max(0, (int)locktime - GetRandInt(100)); |
| 807 | } else { |
| 808 | // If our chain is lagging behind, we can't discourage fee sniping nor help |
| 809 | // the privacy of high-latency transactions. To avoid leaking a potentially |
| 810 | // unique "nLockTime fingerprint", set nLockTime to a constant. |
| 811 | locktime = 0; |
| 812 | } |
| 813 | assert(locktime < LOCKTIME_THRESHOLD); |
| 814 | return locktime; |
| 815 | } |
| 816 | |
| 817 | // Reset all non-global blinding details. |
| 818 | static void resetBlindDetails(BlindDetails* det, bool preserve_output_data = false) { |
no test coverage detected