Skip to content

Commit 73966f7

Browse files
committed
Merge bitcoin/bitcoin#25344: New outputs argument for bumpfee/psbtbumpfee
4c8eccc test: add tests for `outputs` argument to `bumpfee`/`psbtbumpfee` (Seibart Nedor) c0ebb98 wallet: add `outputs` arguments to `bumpfee` and `psbtbumpfee` (Seibart Nedor) a804f3c wallet: extract and reuse RPC argument format definition for outputs (Seibart Nedor) Pull request description: This implements a modification of the proposal in #22007: instead of **adding** outputs to the set of outputs in the original transaction, the outputs given by `outputs` argument **completely replace** the outputs in the original transaction. As noted below, this makes it easier to "cancel" a transaction or to reduce the amounts in the outputs, which is not the case with the original proposal in #22007, but it seems from the discussion in this PR that the **replace** behavior is more desirable than **add** one. ACKs for top commit: achow101: ACK 4c8eccc 1440000bytes: Code Review ACK bitcoin/bitcoin@4c8eccc ishaanam: reACK 4c8eccc Tree-SHA512: 31361f4a9b79c162bda7929583b0a3fd200e09f4c1a5378b12007576d6b14e02e9e4f0bab8aa209f08f75ac25a1f4805ad16ebff4a0334b07ad2378cc0090103
2 parents 75f0e0b + 4c8eccc commit 73966f7

File tree

7 files changed

+112
-56
lines changed

7 files changed

+112
-56
lines changed

src/rpc/rawtransaction_util.cpp

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,31 +21,15 @@
2121
#include <util/strencodings.h>
2222
#include <util/translation.h>
2323

24-
CMutableTransaction ConstructTransaction(const UniValue& inputs_in, const UniValue& outputs_in, const UniValue& locktime, std::optional<bool> rbf)
24+
void AddInputs(CMutableTransaction& rawTx, const UniValue& inputs_in, std::optional<bool> rbf)
2525
{
26-
if (outputs_in.isNull()) {
27-
throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, output argument must be non-null");
28-
}
29-
3026
UniValue inputs;
3127
if (inputs_in.isNull()) {
3228
inputs = UniValue::VARR;
3329
} else {
3430
inputs = inputs_in.get_array();
3531
}
3632

37-
const bool outputs_is_obj = outputs_in.isObject();
38-
UniValue outputs = outputs_is_obj ? outputs_in.get_obj() : outputs_in.get_array();
39-
40-
CMutableTransaction rawTx;
41-
42-
if (!locktime.isNull()) {
43-
int64_t nLockTime = locktime.getInt<int64_t>();
44-
if (nLockTime < 0 || nLockTime > LOCKTIME_MAX)
45-
throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, locktime out of range");
46-
rawTx.nLockTime = nLockTime;
47-
}
48-
4933
for (unsigned int idx = 0; idx < inputs.size(); idx++) {
5034
const UniValue& input = inputs[idx];
5135
const UniValue& o = input.get_obj();
@@ -84,6 +68,16 @@ CMutableTransaction ConstructTransaction(const UniValue& inputs_in, const UniVal
8468

8569
rawTx.vin.push_back(in);
8670
}
71+
}
72+
73+
void AddOutputs(CMutableTransaction& rawTx, const UniValue& outputs_in)
74+
{
75+
if (outputs_in.isNull()) {
76+
throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, output argument must be non-null");
77+
}
78+
79+
const bool outputs_is_obj = outputs_in.isObject();
80+
UniValue outputs = outputs_is_obj ? outputs_in.get_obj() : outputs_in.get_array();
8781

8882
if (!outputs_is_obj) {
8983
// Translate array of key-value pairs into dict
@@ -132,6 +126,21 @@ CMutableTransaction ConstructTransaction(const UniValue& inputs_in, const UniVal
132126
rawTx.vout.push_back(out);
133127
}
134128
}
129+
}
130+
131+
CMutableTransaction ConstructTransaction(const UniValue& inputs_in, const UniValue& outputs_in, const UniValue& locktime, std::optional<bool> rbf)
132+
{
133+
CMutableTransaction rawTx;
134+
135+
if (!locktime.isNull()) {
136+
int64_t nLockTime = locktime.getInt<int64_t>();
137+
if (nLockTime < 0 || nLockTime > LOCKTIME_MAX)
138+
throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, locktime out of range");
139+
rawTx.nLockTime = nLockTime;
140+
}
141+
142+
AddInputs(rawTx, inputs_in, rbf);
143+
AddOutputs(rawTx, outputs_in);
135144

136145
if (rbf.has_value() && rbf.value() && rawTx.vin.size() > 0 && !SignalsOptInRBF(CTransaction(rawTx))) {
137146
throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter combination: Sequence number(s) contradict replaceable option");

src/rpc/rawtransaction_util.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ void SignTransactionResultToJSON(CMutableTransaction& mtx, bool complete, const
3838
*/
3939
void ParsePrevouts(const UniValue& prevTxsUnival, FillableSigningProvider* keystore, std::map<COutPoint, Coin>& coins);
4040

41+
42+
/** Normalize univalue-represented inputs and add them to the transaction */
43+
void AddInputs(CMutableTransaction& rawTx, const UniValue& inputs_in, bool rbf);
44+
45+
/** Normalize univalue-represented outputs and add them to the transaction */
46+
void AddOutputs(CMutableTransaction& rawTx, const UniValue& outputs_in);
47+
4148
/** Create a transaction from univalue parameters */
4249
CMutableTransaction ConstructTransaction(const UniValue& inputs_in, const UniValue& outputs_in, const UniValue& locktime, std::optional<bool> rbf);
4350

src/wallet/feebumper.cpp

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ bool TransactionCanBeBumped(const CWallet& wallet, const uint256& txid)
155155
}
156156

157157
Result CreateRateBumpTransaction(CWallet& wallet, const uint256& txid, const CCoinControl& coin_control, std::vector<bilingual_str>& errors,
158-
CAmount& old_fee, CAmount& new_fee, CMutableTransaction& mtx, bool require_mine)
158+
CAmount& old_fee, CAmount& new_fee, CMutableTransaction& mtx, bool require_mine, const std::vector<CTxOut>& outputs)
159159
{
160160
// We are going to modify coin control later, copy to re-use
161161
CCoinControl new_coin_control(coin_control);
@@ -222,11 +222,19 @@ Result CreateRateBumpTransaction(CWallet& wallet, const uint256& txid, const CCo
222222
return result;
223223
}
224224

225-
// Fill in recipients(and preserve a single change key if there is one)
226-
// While we're here, calculate the output amount
227-
std::vector<CRecipient> recipients;
225+
// Calculate the old output amount.
228226
CAmount output_value = 0;
229-
for (const auto& output : wtx.tx->vout) {
227+
for (const auto& old_output : wtx.tx->vout) {
228+
output_value += old_output.nValue;
229+
}
230+
231+
old_fee = input_value - output_value;
232+
233+
// Fill in recipients (and preserve a single change key if there
234+
// is one). If outputs vector is non-empty, replace original
235+
// outputs with its contents, otherwise use original outputs.
236+
std::vector<CRecipient> recipients;
237+
for (const auto& output : outputs.empty() ? wtx.tx->vout : outputs) {
230238
if (!OutputIsChange(wallet, output)) {
231239
CRecipient recipient = {output.scriptPubKey, output.nValue, false};
232240
recipients.push_back(recipient);
@@ -235,11 +243,8 @@ Result CreateRateBumpTransaction(CWallet& wallet, const uint256& txid, const CCo
235243
ExtractDestination(output.scriptPubKey, change_dest);
236244
new_coin_control.destChange = change_dest;
237245
}
238-
output_value += output.nValue;
239246
}
240247

241-
old_fee = input_value - output_value;
242-
243248
if (coin_control.m_feerate) {
244249
// The user provided a feeRate argument.
245250
// We calculate this here to avoid compiler warning on the cs_wallet lock

src/wallet/feebumper.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ Result CreateRateBumpTransaction(CWallet& wallet,
5151
CAmount& old_fee,
5252
CAmount& new_fee,
5353
CMutableTransaction& mtx,
54-
bool require_mine);
54+
bool require_mine,
55+
const std::vector<CTxOut>& outputs);
5556

5657
//! Sign the new transaction,
5758
//! @return false if the tx couldn't be found or if it was

src/wallet/interfaces.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,8 @@ class WalletImpl : public Wallet
291291
CAmount& new_fee,
292292
CMutableTransaction& mtx) override
293293
{
294-
return feebumper::CreateRateBumpTransaction(*m_wallet.get(), txid, coin_control, errors, old_fee, new_fee, mtx, /* require_mine= */ true) == feebumper::Result::OK;
294+
std::vector<CTxOut> outputs; // just an empty list of new recipients for now
295+
return feebumper::CreateRateBumpTransaction(*m_wallet.get(), txid, coin_control, errors, old_fee, new_fee, mtx, /* require_mine= */ true, outputs) == feebumper::Result::OK;
295296
}
296297
bool signBumpTransaction(CMutableTransaction& mtx) override { return feebumper::SignTransaction(*m_wallet.get(), mtx); }
297298
bool commitBumpTransaction(const uint256& txid,

src/wallet/rpc/spend.cpp

Lines changed: 42 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -956,6 +956,26 @@ RPCHelpMan signrawtransactionwithwallet()
956956
};
957957
}
958958

959+
// Definition of allowed formats of specifying transaction outputs in
960+
// `bumpfee`, `psbtbumpfee`, `send` and `walletcreatefundedpsbt` RPCs.
961+
static std::vector<RPCArg> OutputsDoc()
962+
{
963+
return
964+
{
965+
{"", RPCArg::Type::OBJ_USER_KEYS, RPCArg::Optional::OMITTED, "",
966+
{
967+
{"address", RPCArg::Type::AMOUNT, RPCArg::Optional::NO, "A key-value pair. The key (string) is the bitcoin address,\n"
968+
"the value (float or string) is the amount in " + CURRENCY_UNIT + ""},
969+
},
970+
},
971+
{"", RPCArg::Type::OBJ, RPCArg::Optional::OMITTED, "",
972+
{
973+
{"data", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "A key-value pair. The key must be \"data\", the value is hex-encoded data"},
974+
},
975+
},
976+
};
977+
}
978+
959979
static RPCHelpMan bumpfee_helper(std::string method_name)
960980
{
961981
const bool want_psbt = method_name == "psbtbumpfee";
@@ -992,7 +1012,12 @@ static RPCHelpMan bumpfee_helper(std::string method_name)
9921012
"still be replaceable in practice, for example if it has unconfirmed ancestors which\n"
9931013
"are replaceable).\n"},
9941014
{"estimate_mode", RPCArg::Type::STR, RPCArg::Default{"unset"}, "The fee estimate mode, must be one of (case insensitive):\n"
995-
"\"" + FeeModes("\"\n\"") + "\""},
1015+
"\"" + FeeModes("\"\n\"") + "\""},
1016+
{"outputs", RPCArg::Type::ARR, RPCArg::Default{UniValue::VARR}, "New outputs (key-value pairs) which will replace\n"
1017+
"the original ones, if provided. Each address can only appear once and there can\n"
1018+
"only be one \"data\" object.\n",
1019+
OutputsDoc(),
1020+
RPCArgOptions{.skip_type_check = true}},
9961021
},
9971022
RPCArgOptions{.oneline_description="options"}},
9981023
},
@@ -1029,6 +1054,7 @@ static RPCHelpMan bumpfee_helper(std::string method_name)
10291054
coin_control.fAllowWatchOnly = pwallet->IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS);
10301055
// optional parameters
10311056
coin_control.m_signal_bip125_rbf = true;
1057+
std::vector<CTxOut> outputs;
10321058

10331059
if (!request.params[1].isNull()) {
10341060
UniValue options = request.params[1];
@@ -1039,6 +1065,7 @@ static RPCHelpMan bumpfee_helper(std::string method_name)
10391065
{"fee_rate", UniValueType()}, // will be checked by AmountFromValue() in SetFeeEstimateMode()
10401066
{"replaceable", UniValueType(UniValue::VBOOL)},
10411067
{"estimate_mode", UniValueType(UniValue::VSTR)},
1068+
{"outputs", UniValueType()}, // will be checked by AddOutputs()
10421069
},
10431070
true, true);
10441071

@@ -1052,6 +1079,16 @@ static RPCHelpMan bumpfee_helper(std::string method_name)
10521079
coin_control.m_signal_bip125_rbf = options["replaceable"].get_bool();
10531080
}
10541081
SetFeeEstimateMode(*pwallet, coin_control, conf_target, options["estimate_mode"], options["fee_rate"], /*override_min_fee=*/false);
1082+
1083+
// Prepare new outputs by creating a temporary tx and calling AddOutputs().
1084+
if (!options["outputs"].isNull()) {
1085+
if (options["outputs"].isArray() && options["outputs"].empty()) {
1086+
throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, output argument cannot be an empty array");
1087+
}
1088+
CMutableTransaction tempTx;
1089+
AddOutputs(tempTx, options["outputs"]);
1090+
outputs = tempTx.vout;
1091+
}
10551092
}
10561093

10571094
// Make sure the results are valid at least up to the most recent block
@@ -1069,7 +1106,7 @@ static RPCHelpMan bumpfee_helper(std::string method_name)
10691106
CMutableTransaction mtx;
10701107
feebumper::Result res;
10711108
// Targeting feerate bump.
1072-
res = feebumper::CreateRateBumpTransaction(*pwallet, hash, coin_control, errors, old_fee, new_fee, mtx, /*require_mine=*/ !want_psbt);
1109+
res = feebumper::CreateRateBumpTransaction(*pwallet, hash, coin_control, errors, old_fee, new_fee, mtx, /*require_mine=*/ !want_psbt, outputs);
10731110
if (res != feebumper::Result::OK) {
10741111
switch(res) {
10751112
case feebumper::Result::INVALID_ADDRESS_OR_KEY:
@@ -1144,18 +1181,7 @@ RPCHelpMan send()
11441181
{"outputs", RPCArg::Type::ARR, RPCArg::Optional::NO, "The outputs (key-value pairs), where none of the keys are duplicated.\n"
11451182
"That is, each address can only appear once and there can only be one 'data' object.\n"
11461183
"For convenience, a dictionary, which holds the key-value pairs directly, is also accepted.",
1147-
{
1148-
{"", RPCArg::Type::OBJ_USER_KEYS, RPCArg::Optional::OMITTED, "",
1149-
{
1150-
{"address", RPCArg::Type::AMOUNT, RPCArg::Optional::NO, "A key-value pair. The key (string) is the bitcoin address, the value (float or string) is the amount in " + CURRENCY_UNIT + ""},
1151-
},
1152-
},
1153-
{"", RPCArg::Type::OBJ, RPCArg::Optional::OMITTED, "",
1154-
{
1155-
{"data", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "A key-value pair. The key must be \"data\", the value is hex-encoded data"},
1156-
},
1157-
},
1158-
},
1184+
OutputsDoc(),
11591185
RPCArgOptions{.skip_type_check = true}},
11601186
{"conf_target", RPCArg::Type::NUM, RPCArg::DefaultHint{"wallet -txconfirmtarget"}, "Confirmation target in blocks"},
11611187
{"estimate_mode", RPCArg::Type::STR, RPCArg::Default{"unset"}, "The fee estimate mode, must be one of (case insensitive):\n"
@@ -1606,19 +1632,8 @@ RPCHelpMan walletcreatefundedpsbt()
16061632
"That is, each address can only appear once and there can only be one 'data' object.\n"
16071633
"For compatibility reasons, a dictionary, which holds the key-value pairs directly, is also\n"
16081634
"accepted as second parameter.",
1609-
{
1610-
{"", RPCArg::Type::OBJ_USER_KEYS, RPCArg::Optional::OMITTED, "",
1611-
{
1612-
{"address", RPCArg::Type::AMOUNT, RPCArg::Optional::NO, "A key-value pair. The key (string) is the bitcoin address, the value (float or string) is the amount in " + CURRENCY_UNIT + ""},
1613-
},
1614-
},
1615-
{"", RPCArg::Type::OBJ, RPCArg::Optional::OMITTED, "",
1616-
{
1617-
{"data", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "A key-value pair. The key must be \"data\", the value is hex-encoded data"},
1618-
},
1619-
},
1620-
},
1621-
RPCArgOptions{.skip_type_check = true}},
1635+
OutputsDoc(),
1636+
RPCArgOptions{.skip_type_check = true}},
16221637
{"locktime", RPCArg::Type::NUM, RPCArg::Default{0}, "Raw locktime. Non-0 value also locktime-activates inputs"},
16231638
{"options", RPCArg::Type::OBJ, RPCArg::Optional::OMITTED, "",
16241639
Cat<std::vector<RPCArg>>(

test/functional/wallet_bumpfee.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def run_test(self):
8181

8282
self.log.info("Running tests")
8383
dest_address = peer_node.getnewaddress()
84-
for mode in ["default", "fee_rate"]:
84+
for mode in ["default", "fee_rate", "new_outputs"]:
8585
test_simple_bumpfee_succeeds(self, mode, rbf_node, peer_node, dest_address)
8686
self.test_invalid_parameters(rbf_node, peer_node, dest_address)
8787
test_segwit_bumpfee_succeeds(self, rbf_node, dest_address)
@@ -157,6 +157,14 @@ def test_invalid_parameters(self, rbf_node, peer_node, dest_address):
157157
assert_raises_rpc_error(-8, 'Invalid estimate_mode parameter, must be one of: "unset", "economical", "conservative"',
158158
rbf_node.bumpfee, rbfid, {"estimate_mode": mode})
159159

160+
self.log.info("Test invalid outputs values")
161+
assert_raises_rpc_error(-8, "Invalid parameter, output argument cannot be an empty array",
162+
rbf_node.bumpfee, rbfid, {"outputs": []})
163+
assert_raises_rpc_error(-8, "Invalid parameter, duplicated address: " + dest_address,
164+
rbf_node.bumpfee, rbfid, {"outputs": [{dest_address: 0.1}, {dest_address: 0.2}]})
165+
assert_raises_rpc_error(-8, "Invalid parameter, duplicate key: data",
166+
rbf_node.bumpfee, rbfid, {"outputs": [{"data": "deadbeef"}, {"data": "deadbeef"}]})
167+
160168
self.clear_mempool()
161169

162170

@@ -169,6 +177,10 @@ def test_simple_bumpfee_succeeds(self, mode, rbf_node, peer_node, dest_address):
169177
if mode == "fee_rate":
170178
bumped_psbt = rbf_node.psbtbumpfee(rbfid, {"fee_rate": str(NORMAL)})
171179
bumped_tx = rbf_node.bumpfee(rbfid, {"fee_rate": NORMAL})
180+
elif mode == "new_outputs":
181+
new_address = peer_node.getnewaddress()
182+
bumped_psbt = rbf_node.psbtbumpfee(rbfid, {"outputs": {new_address: 0.0003}})
183+
bumped_tx = rbf_node.bumpfee(rbfid, {"outputs": {new_address: 0.0003}})
172184
else:
173185
bumped_psbt = rbf_node.psbtbumpfee(rbfid)
174186
bumped_tx = rbf_node.bumpfee(rbfid)
@@ -192,6 +204,10 @@ def test_simple_bumpfee_succeeds(self, mode, rbf_node, peer_node, dest_address):
192204
bumpedwtx = rbf_node.gettransaction(bumped_tx["txid"])
193205
assert_equal(oldwtx["replaced_by_txid"], bumped_tx["txid"])
194206
assert_equal(bumpedwtx["replaces_txid"], rbfid)
207+
# if this is a new_outputs test, check that outputs were indeed replaced
208+
if mode == "new_outputs":
209+
assert len(bumpedwtx["details"]) == 1
210+
assert bumpedwtx["details"][0]["address"] == new_address
195211
self.clear_mempool()
196212

197213

@@ -628,12 +644,14 @@ def get_change_address(tx):
628644
self.clear_mempool()
629645

630646

631-
def spend_one_input(node, dest_address, change_size=Decimal("0.00049000")):
647+
def spend_one_input(node, dest_address, change_size=Decimal("0.00049000"), data=None):
632648
tx_input = dict(
633649
sequence=MAX_BIP125_RBF_SEQUENCE, **next(u for u in node.listunspent() if u["amount"] == Decimal("0.00100000")))
634650
destinations = {dest_address: Decimal("0.00050000")}
635651
if change_size > 0:
636652
destinations[node.getrawchangeaddress()] = change_size
653+
if data:
654+
destinations['data'] = data
637655
rawtx = node.createrawtransaction([tx_input], destinations)
638656
signedtx = node.signrawtransactionwithwallet(rawtx)
639657
txid = node.sendrawtransaction(signedtx["hex"])

0 commit comments

Comments
 (0)