Skip to content

Commit 77773b0

Browse files
author
MarcoFalke
committed
wallet: Pass FastRandomContext& to DiscourageFeeSniping
1 parent 25d045a commit 77773b0

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

src/wallet/spend.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -585,7 +585,8 @@ static bool IsCurrentForAntiFeeSniping(interfaces::Chain& chain, const uint256&
585585
* 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 void DiscourageFeeSniping(CMutableTransaction& tx, interfaces::Chain& chain, const uint256& block_hash, int block_height)
588+
static void DiscourageFeeSniping(CMutableTransaction& tx, FastRandomContext& rng_fast,
589+
interfaces::Chain& chain, const uint256& block_hash, int block_height)
589590
{
590591
// All inputs must be added by now
591592
assert(!tx.vin.empty());
@@ -616,8 +617,8 @@ static void DiscourageFeeSniping(CMutableTransaction& tx, interfaces::Chain& cha
616617
// that transactions that are delayed after signing for whatever reason,
617618
// e.g. high-latency mix networks and some CoinJoin implementations, have
618619
// better privacy.
619-
if (GetRandInt(10) == 0) {
620-
tx.nLockTime = std::max(0, int(tx.nLockTime) - GetRandInt(100));
620+
if (rng_fast.randrange(10) == 0) {
621+
tx.nLockTime = std::max(0, int(tx.nLockTime) - int(rng_fast.randrange(100)));
621622
}
622623
} else {
623624
// If our chain is lagging behind, we can't discourage fee sniping nor help
@@ -653,6 +654,7 @@ static bool CreateTransactionInternal(
653654
{
654655
AssertLockHeld(wallet.cs_wallet);
655656

657+
FastRandomContext rng_fast;
656658
CMutableTransaction txNew; // The resulting transaction that we make
657659

658660
CoinSelectionParams coin_selection_params; // Parameters for coin selection, init with dummy
@@ -782,10 +784,9 @@ static bool CreateTransactionInternal(
782784
assert(change_and_fee >= 0);
783785
CTxOut newTxOut(change_and_fee, scriptChange);
784786

785-
if (nChangePosInOut == -1)
786-
{
787+
if (nChangePosInOut == -1) {
787788
// Insert change txn at random position:
788-
nChangePosInOut = GetRandInt(txNew.vout.size()+1);
789+
nChangePosInOut = rng_fast.randrange(txNew.vout.size() + 1);
789790
}
790791
else if ((unsigned int)nChangePosInOut > txNew.vout.size())
791792
{
@@ -811,7 +812,7 @@ static bool CreateTransactionInternal(
811812
for (const auto& coin : selected_coins) {
812813
txNew.vin.push_back(CTxIn(coin.outpoint, CScript(), nSequence));
813814
}
814-
DiscourageFeeSniping(txNew, wallet.chain(), wallet.GetLastBlockHash(), wallet.GetLastBlockHeight());
815+
DiscourageFeeSniping(txNew, rng_fast, wallet.chain(), wallet.GetLastBlockHash(), wallet.GetLastBlockHeight());
815816

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

0 commit comments

Comments
 (0)