wallet, rpc: add anti-fee-sniping to send and sendall

This commit is contained in:
ishaanam 2023-11-26 13:08:56 -05:00
parent 41544b8f96
commit 6825ae0c78
5 changed files with 27 additions and 10 deletions

View file

@ -95,8 +95,21 @@ std::set<int> InterpretSubtractFeeFromOutputInstructions(const UniValue& sffo_in
return sffo_set;
}
static UniValue FinishTransaction(const std::shared_ptr<CWallet> pwallet, const UniValue& options, const CMutableTransaction& rawTx)
static UniValue FinishTransaction(const std::shared_ptr<CWallet> pwallet, const UniValue& options, CMutableTransaction& rawTx)
{
bool can_anti_fee_snipe = !options.exists("locktime");
for (const CTxIn& tx_in : rawTx.vin) {
// Checks sequence values consistent with DiscourageFeeSniping
can_anti_fee_snipe &= (tx_in.nSequence == CTxIn::MAX_SEQUENCE_NONFINAL || tx_in.nSequence == MAX_BIP125_RBF_SEQUENCE);
}
if (can_anti_fee_snipe) {
LOCK(pwallet->cs_wallet);
FastRandomContext rng_fast;
DiscourageFeeSniping(rawTx, rng_fast, pwallet->chain(), pwallet->GetLastBlockHash(), pwallet->GetLastBlockHeight());
}
// Make a blank psbt
PartiallySignedTransaction psbtx(rawTx);
@ -1229,7 +1242,7 @@ RPCHelpMan send()
"Remember to convert serialized sizes to weight units when necessary."},
},
},
{"locktime", RPCArg::Type::NUM, RPCArg::Default{0}, "Raw locktime. Non-0 value also locktime-activates inputs"},
{"locktime", RPCArg::Type::NUM, RPCArg::DefaultHint{"locktime close to block height to prevent fee sniping"}, "Raw locktime. Non-0 value also locktime-activates inputs"},
{"lock_unspents", RPCArg::Type::BOOL, RPCArg::Default{false}, "Lock selected unspent outputs"},
{"psbt", RPCArg::Type::BOOL, RPCArg::DefaultHint{"automatic"}, "Always return a PSBT, implies add_to_wallet=false."},
{"subtract_fee_from_outputs", RPCArg::Type::ARR, RPCArg::Default{UniValue::VARR}, "Outputs to subtract the fee from, specified as integer indices.\n"
@ -1293,7 +1306,8 @@ RPCHelpMan send()
rawTx.vout.clear();
auto txr = FundTransaction(*pwallet, rawTx, recipients, options, coin_control, /*override_min_fee=*/false);
return FinishTransaction(pwallet, options, CMutableTransaction(*txr.tx));
CMutableTransaction tx = CMutableTransaction(*txr.tx);
return FinishTransaction(pwallet, options, tx);
}
};
}
@ -1341,7 +1355,7 @@ RPCHelpMan sendall()
},
},
},
{"locktime", RPCArg::Type::NUM, RPCArg::Default{0}, "Raw locktime. Non-0 value also locktime-activates inputs"},
{"locktime", RPCArg::Type::NUM, RPCArg::DefaultHint{"locktime close to block height to prevent fee sniping"}, "Raw locktime. Non-0 value also locktime-activates inputs"},
{"lock_unspents", RPCArg::Type::BOOL, RPCArg::Default{false}, "Lock selected unspent outputs"},
{"psbt", RPCArg::Type::BOOL, RPCArg::DefaultHint{"automatic"}, "Always return a PSBT, implies add_to_wallet=false."},
{"send_max", RPCArg::Type::BOOL, RPCArg::Default{false}, "When true, only use UTXOs that can pay for their own fees to maximize the output amount. When 'false' (default), no UTXO is left behind. send_max is incompatible with providing specific inputs."},

View file

@ -916,11 +916,7 @@ static bool IsCurrentForAntiFeeSniping(interfaces::Chain& chain, const uint256&
return true;
}
/**
* Set a height-based locktime for new transactions (uses the height of the
* current chain tip unless we are not synced with the current chain
*/
static void DiscourageFeeSniping(CMutableTransaction& tx, FastRandomContext& rng_fast,
void DiscourageFeeSniping(CMutableTransaction& tx, FastRandomContext& rng_fast,
interfaces::Chain& chain, const uint256& block_hash, int block_height)
{
// All inputs must be added by now

View file

@ -213,6 +213,12 @@ struct CreatedTransactionResult
: tx(_tx), fee(_fee), fee_calc(_fee_calc), change_pos(_change_pos) {}
};
/**
* Set a height-based locktime for new transactions (uses the height of the
* current chain tip unless we are not synced with the current chain
*/
void DiscourageFeeSniping(CMutableTransaction& tx, FastRandomContext& rng_fast, interfaces::Chain& chain, const uint256& block_hash, int block_height);
/**
* Create a new transaction paying the recipients with a set of coins
* selected by SelectCoins(); Also create the change output, when needed

View file

@ -300,6 +300,7 @@ class ImportRescanTest(BitcoinTestFramework):
add_to_wallet=False,
inputs=[unspent_txid_map[variant.initial_txid]],
outputs=[{ADDRESS_BCRT1_UNSPENDABLE : variant.initial_amount}],
locktime=0,
subtract_fee_from_outputs=[0]
)
variant.child_txid = child["txid"]

View file

@ -57,7 +57,7 @@ class WalletRescanUnconfirmed(BitcoinTestFramework):
# The only UTXO available to spend is tx_parent_to_reorg.
assert_equal(len(w0_utxos), 1)
assert_equal(w0_utxos[0]["txid"], tx_parent_to_reorg["txid"])
tx_child_unconfirmed_sweep = w0.sendall([ADDRESS_BCRT1_UNSPENDABLE])
tx_child_unconfirmed_sweep = w0.sendall(recipients=[ADDRESS_BCRT1_UNSPENDABLE], options={"locktime":0})
assert tx_child_unconfirmed_sweep["txid"] in node.getrawmempool()
node.syncwithvalidationinterfacequeue()