@@ -583,12 +583,13 @@ static bool IsCurrentForAntiFeeSniping(interfaces::Chain& chain, const uint256&
583
583
}
584
584
585
585
/* *
586
- * Return a height-based locktime for new transactions (uses the height of the
586
+ * Set a height-based locktime for new transactions (uses the height of the
587
587
* current chain tip unless we are not synced with the current chain
588
588
*/
589
- static uint32_t GetLocktimeForNewTransaction ( interfaces::Chain& chain, const uint256& block_hash, int block_height)
589
+ static void DiscourageFeeSniping (CMutableTransaction& tx, interfaces::Chain& chain, const uint256& block_hash, int block_height)
590
590
{
591
- uint32_t locktime;
591
+ // All inputs must be added by now
592
+ assert (!tx.vin .empty ());
592
593
// Discourage fee sniping.
593
594
//
594
595
// For a large miner the value of the transactions in the best block and
@@ -610,22 +611,34 @@ static uint32_t GetLocktimeForNewTransaction(interfaces::Chain& chain, const uin
610
611
// now we ensure code won't be written that makes assumptions about
611
612
// nLockTime that preclude a fix later.
612
613
if (IsCurrentForAntiFeeSniping (chain, block_hash)) {
613
- locktime = block_height;
614
+ tx. nLockTime = block_height;
614
615
615
616
// Secondly occasionally randomly pick a nLockTime even further back, so
616
617
// that transactions that are delayed after signing for whatever reason,
617
618
// e.g. high-latency mix networks and some CoinJoin implementations, have
618
619
// better privacy.
619
- if (GetRandInt (10 ) == 0 )
620
- locktime = std::max (0 , (int )locktime - GetRandInt (100 ));
620
+ if (GetRandInt (10 ) == 0 ) {
621
+ tx.nLockTime = std::max (0 , int (tx.nLockTime ) - GetRandInt (100 ));
622
+ }
621
623
} else {
622
624
// If our chain is lagging behind, we can't discourage fee sniping nor help
623
625
// the privacy of high-latency transactions. To avoid leaking a potentially
624
626
// unique "nLockTime fingerprint", set nLockTime to a constant.
625
- locktime = 0 ;
627
+ tx.nLockTime = 0 ;
628
+ }
629
+ // Sanity check all values
630
+ assert (tx.nLockTime < LOCKTIME_THRESHOLD); // Type must be block height
631
+ assert (tx.nLockTime <= uint64_t (block_height));
632
+ for (const auto & in : tx.vin ) {
633
+ // Can not be FINAL for locktime to work
634
+ assert (in.nSequence != CTxIn::SEQUENCE_FINAL);
635
+ // May be MAX NONFINAL to disable both BIP68 and BIP125
636
+ if (in.nSequence == CTxIn::MAX_SEQUENCE_NONFINAL) continue ;
637
+ // May be MAX BIP125 to disable BIP68 and enable BIP125
638
+ if (in.nSequence == MAX_BIP125_RBF_SEQUENCE) continue ;
639
+ // The wallet does not support any other sequence-use right now.
640
+ assert (false );
626
641
}
627
- assert (locktime < LOCKTIME_THRESHOLD);
628
- return locktime;
629
642
}
630
643
631
644
static bool CreateTransactionInternal (
@@ -642,7 +655,6 @@ static bool CreateTransactionInternal(
642
655
AssertLockHeld (wallet.cs_wallet );
643
656
644
657
CMutableTransaction txNew; // The resulting transaction that we make
645
- txNew.nLockTime = GetLocktimeForNewTransaction (wallet.chain (), wallet.GetLastBlockHash (), wallet.GetLastBlockHeight ());
646
658
647
659
CoinSelectionParams coin_selection_params; // Parameters for coin selection, init with dummy
648
660
coin_selection_params.m_avoid_partial_spends = coin_control.m_avoid_partial_spends ;
@@ -788,8 +800,8 @@ static bool CreateTransactionInternal(
788
800
// Shuffle selected coins and fill in final vin
789
801
std::vector<CInputCoin> selected_coins = result->GetShuffledInputVector ();
790
802
791
- // Note how the sequence number is set to non-maxint so that
792
- // the nLockTime set above actually works.
803
+ // The sequence number is set to non-maxint so that DiscourageFeeSniping
804
+ // works.
793
805
//
794
806
// BIP125 defines opt-in RBF as any nSequence < maxint-1, so
795
807
// we use the highest possible value in that range (maxint-2)
@@ -800,6 +812,7 @@ static bool CreateTransactionInternal(
800
812
for (const auto & coin : selected_coins) {
801
813
txNew.vin .push_back (CTxIn (coin.outpoint , CScript (), nSequence));
802
814
}
815
+ DiscourageFeeSniping (txNew, wallet.chain (), wallet.GetLastBlockHash (), wallet.GetLastBlockHeight ());
803
816
804
817
// Calculate the transaction fee
805
818
TxSize tx_sizes = CalculateMaximumSignedTxSize (CTransaction (txNew), &wallet, &coin_control);
0 commit comments