Skip to content

Commit 25d045a

Browse files
committed
Merge bitcoin/bitcoin#24225: wallet: Add sanity checks to DiscourageFeeSniping
fa8e76b wallet: Add sanity checks to AntiFeeSnipe (MarcoFalke) Pull request description: I added those sanity checks as part of implementing BIP 326, but I think they make sense on their own. The checks require the transaction to be passed in to `DiscourageFeeSniping`. Also, replace `(int)locktime` cast with the equivalent `int(locktime)` cast. ACKs for top commit: chris-belcher: ACK bitcoin/bitcoin@fa8e76b S3RK: Code review ACK fa8e76b achow101: ACK fa8e76b w0xlt: Code Review ACK bitcoin/bitcoin@fa8e76b Tree-SHA512: 6fe37c85f074851ef09cae8addcb836257089290fecec515c129c3180e9ccb64740aaac76043af10ce7c213e5001568ca6b4b62ae9631f0994ed676b07118074
2 parents e0881aa + fa8e76b commit 25d045a

File tree

1 file changed

+25
-12
lines changed

1 file changed

+25
-12
lines changed

src/wallet/spend.cpp

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -582,12 +582,13 @@ static bool IsCurrentForAntiFeeSniping(interfaces::Chain& chain, const uint256&
582582
}
583583

584584
/**
585-
* Return a height-based locktime for new transactions (uses the height of the
585+
* Set a height-based locktime for new transactions (uses the height of the
586586
* current chain tip unless we are not synced with the current chain
587587
*/
588-
static uint32_t GetLocktimeForNewTransaction(interfaces::Chain& chain, const uint256& block_hash, int block_height)
588+
static void DiscourageFeeSniping(CMutableTransaction& tx, interfaces::Chain& chain, const uint256& block_hash, int block_height)
589589
{
590-
uint32_t locktime;
590+
// All inputs must be added by now
591+
assert(!tx.vin.empty());
591592
// Discourage fee sniping.
592593
//
593594
// For a large miner the value of the transactions in the best block and
@@ -609,22 +610,34 @@ static uint32_t GetLocktimeForNewTransaction(interfaces::Chain& chain, const uin
609610
// now we ensure code won't be written that makes assumptions about
610611
// nLockTime that preclude a fix later.
611612
if (IsCurrentForAntiFeeSniping(chain, block_hash)) {
612-
locktime = block_height;
613+
tx.nLockTime = block_height;
613614

614615
// Secondly occasionally randomly pick a nLockTime even further back, so
615616
// that transactions that are delayed after signing for whatever reason,
616617
// e.g. high-latency mix networks and some CoinJoin implementations, have
617618
// better privacy.
618-
if (GetRandInt(10) == 0)
619-
locktime = std::max(0, (int)locktime - GetRandInt(100));
619+
if (GetRandInt(10) == 0) {
620+
tx.nLockTime = std::max(0, int(tx.nLockTime) - GetRandInt(100));
621+
}
620622
} else {
621623
// If our chain is lagging behind, we can't discourage fee sniping nor help
622624
// the privacy of high-latency transactions. To avoid leaking a potentially
623625
// unique "nLockTime fingerprint", set nLockTime to a constant.
624-
locktime = 0;
626+
tx.nLockTime = 0;
627+
}
628+
// Sanity check all values
629+
assert(tx.nLockTime < LOCKTIME_THRESHOLD); // Type must be block height
630+
assert(tx.nLockTime <= uint64_t(block_height));
631+
for (const auto& in : tx.vin) {
632+
// Can not be FINAL for locktime to work
633+
assert(in.nSequence != CTxIn::SEQUENCE_FINAL);
634+
// May be MAX NONFINAL to disable both BIP68 and BIP125
635+
if (in.nSequence == CTxIn::MAX_SEQUENCE_NONFINAL) continue;
636+
// May be MAX BIP125 to disable BIP68 and enable BIP125
637+
if (in.nSequence == MAX_BIP125_RBF_SEQUENCE) continue;
638+
// The wallet does not support any other sequence-use right now.
639+
assert(false);
625640
}
626-
assert(locktime < LOCKTIME_THRESHOLD);
627-
return locktime;
628641
}
629642

630643
static bool CreateTransactionInternal(
@@ -641,7 +654,6 @@ static bool CreateTransactionInternal(
641654
AssertLockHeld(wallet.cs_wallet);
642655

643656
CMutableTransaction txNew; // The resulting transaction that we make
644-
txNew.nLockTime = GetLocktimeForNewTransaction(wallet.chain(), wallet.GetLastBlockHash(), wallet.GetLastBlockHeight());
645657

646658
CoinSelectionParams coin_selection_params; // Parameters for coin selection, init with dummy
647659
coin_selection_params.m_avoid_partial_spends = coin_control.m_avoid_partial_spends;
@@ -787,8 +799,8 @@ static bool CreateTransactionInternal(
787799
// Shuffle selected coins and fill in final vin
788800
std::vector<CInputCoin> selected_coins = result->GetShuffledInputVector();
789801

790-
// Note how the sequence number is set to non-maxint so that
791-
// the nLockTime set above actually works.
802+
// The sequence number is set to non-maxint so that DiscourageFeeSniping
803+
// works.
792804
//
793805
// BIP125 defines opt-in RBF as any nSequence < maxint-1, so
794806
// we use the highest possible value in that range (maxint-2)
@@ -799,6 +811,7 @@ static bool CreateTransactionInternal(
799811
for (const auto& coin : selected_coins) {
800812
txNew.vin.push_back(CTxIn(coin.outpoint, CScript(), nSequence));
801813
}
814+
DiscourageFeeSniping(txNew, wallet.chain(), wallet.GetLastBlockHash(), wallet.GetLastBlockHeight());
802815

803816
// Calculate the transaction fee
804817
TxSize tx_sizes = CalculateMaximumSignedTxSize(CTransaction(txNew), &wallet, &coin_control);

0 commit comments

Comments
 (0)