Join GitHub today
GitHub is home to over 20 million developers working together to host and review code, manage projects, and build software together.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
Already on GitHub? Sign in to your account
Add 'subtractFeeFromAmount' option to 'fundrawtransaction'. #9222
Conversation
fanquake
added
RPC/REST/ZMQ
Wallet
labels
Nov 26, 2016
|
Concept ACK. |
|
Nice. Concept ACK. |
|
@jonasschnelli I tried finding the fundrawtransaction tests but couldn't. Where are they? src/test/rpc_tests.cpp seems like the natural place for them, but I see no 'fund' in there at all. |
|
@dooglus |
|
@jonasschnelli Thanks for pointing me at the I have added tests for |
|
Concept ACK |
| + if (options.exists("subtractFeeFromAmount")) { | ||
| + subtractFeeFromAmount = options["subtractFeeFromAmount"].get_array(); | ||
| + for (unsigned int idx = 0; idx < subtractFeeFromAmount.size(); idx++) { | ||
| + string strAddress(subtractFeeFromAmount[idx].get_str()); |
jonasschnelli
Nov 28, 2016
Member
This will thrown an exception if one of the elements in the array is numeric. But I think this is okay.
|
Code Review ACK a979010. |
|
To 'squash' the commits do I just rewrite the same branch with a |
|
@dooglus: Yes. I normally do a |
|
@jonasschnelli Thanks. The 'i' is lowercase and the 'HEAD' is uppercase but it was close enough. I used |
|
utACK |
| + assert(result[0]['fee'] == result[1]['fee']) | ||
| + assert(result[0]['fee'] == result[2]['fee']) | ||
| + assert(result[0]['fee'] == result[3]['fee']) | ||
| + assert(result[4]['fee'] == result[5]['fee']) |
mrbandrews
Nov 29, 2016
Contributor
nit: you can condense this to:
assert(result[0]['fee']==result[1]['fee']== result[2]['fee']==result[3]['fee'])
dooglus
Nov 29, 2016
Contributor
Interesting. I didn't know Python did that. I will do as you suggest.
| + | ||
| + # change amounts in result 0, 1, and 2 are the same | ||
| + assert(change[0] == change[1]) | ||
| + assert(change[0] == change[2]) |
| + throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, string("Invalid Bitcoin address: ")+strAddress); | ||
| + if (setSubtractFeeFromAmount.count(strAddress)) | ||
| + throw JSONRPCError(RPC_INVALID_PARAMETER, string("Invalid parameter, duplicated address: ")+strAddress); | ||
| + setSubtractFeeFromAmount.insert(strAddress); |
mrbandrews
Nov 29, 2016
Contributor
Should it throw an error if the given address is valid but is not among the outputs? (would have to check for this below, after retrieving the transaction). It seems like in this case, the user is trying to pay the fee with one of the outputs but has made an error.
dooglus
Nov 29, 2016
Contributor
I wanted it to behave the same as it does in sendmany, where it doesn't complain if you include an address that isn't a recipient at all.
The user could have a list of addresses which should pay fees when sent to, and use that same list as their subtractFeeFromAmount parameter whichever addresses they are sending to.
ryanofsky
Nov 30, 2016
Contributor
One difference between this and sendmany is that sendmany requires transaction outputs to be base58 addresses, and takes amounts and subtractfeefromamount arguments in base58 form, while fundrawtransaction allows outputs to be arbitrary scripts. This means with the PR in its current form, there may be no way for the new subtractFeeFromAmount argument to refer to certain outputs.
Instead of adding a subtractFeeFromAmount argument, I might suggest adding a subtractFeeFromPositions argument that takes a list of integer output indices. This would give callers the flexibility to refer to all outputs, be more consistent with the existing changePosition argument (which is also an integer output index), and also eliminate the need for ExtractDestination and CBitcoinAddress::ToString invocations in CWallet::FundTransaction.
instagibbs
Dec 1, 2016
Member
@ryanofsky I like the idea but am a bit worried about the interaction of subtractFeeFromPositions and changePosition. It might not be clear to the user if the position marking is done before or after change output is added, or discount the wrong output by adding a changePosition argument.
dooglus
Dec 1, 2016
Contributor
At the time of running fundrawtransaction there is no change output, and the user wouldn't know where the change will be inserted, so the position marking must be done before the change output is added.
I think since it is possible to add arbitrary hex output scripts which may not even have a corresponding address we need to be able to address the outputs by number rather than by address. It's also kind of ugly having to give the same address twice, once to createrawtransaction and then again to fundrawtransaction. I think using the output index (0 based) is cleaner.
instagibbs
Dec 1, 2016
•
Member
@dooglus the user will "know" where change is going if they attempt to set the change index they're setting in the option, which is my point. It's not plainly clear how this should interact, unless you spell it out.
dooglus
Dec 1, 2016
Contributor
Oh, I see. So I should spell it out...
I think it makes sense to use the position indices before the change output is added.
|
Addressed @mrbandrews' nits. Should I re-squash now, or leave the 'nit' commit separate for a while? |
| + self.nodes[3].fundrawtransaction(rawtx, {"feeRate": 2*min_relay_tx_fee}), | ||
| + self.nodes[3].fundrawtransaction(rawtx, {"feeRate": 2*min_relay_tx_fee, "subtractFeeFromAmount": [addr1]})] | ||
| + | ||
| + dec_tx = [self.nodes[3].decoderawtransaction(result[0]['hex']), |
ryanofsky
Nov 30, 2016
Contributor
Could use list comprehension:
dec_tx = [self.nodes[3].decoderawtransaction(tx['hex'] for tx in result]
| + self.nodes[3].decoderawtransaction(result[4]['hex']), | ||
| + self.nodes[3].decoderawtransaction(result[5]['hex'])] | ||
| + | ||
| + output = [dec_tx[0]['vout'][1 - result[0]['changepos']]['value'], |
ryanofsky
Nov 30, 2016
Contributor
Could use list comprehension (and similarly below):
output = [d['vout'][1 - r['changepos']]['value'] for d, r in zip(dec_tx, result)]
| + assert(output[0] == output[1] == output[2]) | ||
| + | ||
| + # 0's output should be equal to 3's (output plus fee) | ||
| + assert(output[0] == output[3] + result[3]['fee']) |
ryanofsky
Nov 30, 2016
Contributor
Debug output will be a little better if you use assert_equal instead of assert here and below.
dooglus
Nov 30, 2016
Contributor
It appears that assert_equal can only compare two things. For cases like assert(A == B == C == D) would you prefer 3 separate assert_equal() calls instead?
ryanofsky
Nov 30, 2016
Contributor
I actually only meant to suggest using assert_equal for binary comparisons like the one on line 698. But if you wanted to use it more broadly, you could extend the function (in util.py) to accept more arguments:
def assert_equal(thing1, thing2, *args):
if thing1 != thing2 or any(thing1 != arg for arg in args):
raise AssertionError("!(%s)" % " == ".join(str(arg) for arg in (thing1, thing2) + args))
dooglus
Nov 30, 2016
Contributor
Would it be better to extend assert_equal() to take an arbitrary number of parameters and have it compare them pairwise? Something like this would work:
def assert_equal(thing1, thing2, *other_things, depth=0):
if thing1 != thing2:
if depth or other_things:
raise AssertionError("%s != %s (positions %d and %d)"%(str(thing1),str(thing2), depth, depth+1))
else:
raise AssertionError("%s != %s"%(str(thing1),str(thing2)))
if other_things:
assert_equal(thing2, *other_things, depth = depth + 1)
>>> assert_equal(4, 4, 5)
AssertionError: 4 != 5 (positions 1 and 2)
dooglus
Nov 30, 2016
Contributor
I missed your last comment. Your solution is obviously much more elegant.
Is it acceptable to include a change like that in this pull request or should it be separate?
| + assert(change[4] + result[4]['fee'] == change[5]) | ||
| + | ||
| + inputs = [] | ||
| + addr0 = self.nodes[2].getnewaddress() |
ryanofsky
Nov 30, 2016
Contributor
Could use dictionary comprehension:
outputs = {self.nodes[2].getnewaddress(): value for value in (1.0, 1.1, 1.2, 1.3)}
| + dec_tx[1]['vout'][3]['value'], | ||
| + dec_tx[1]['vout'][4]['value']]] | ||
| + del output[0][result[0]['changepos']] | ||
| + del output[1][result[1]['changepos']] |
ryanofsky
Nov 30, 2016
Contributor
Could use list comprehension:
output = [[out[value] for i, out in enumerate(d['vout']) if i != r['changepos']]
for d, r in zip(dec_tx, result)]]
| + " \"includeWatching\" (boolean, optional, default false) Also select inputs which are watch only\n" | ||
| + " \"lockUnspents\" (boolean, optional, default false) Lock selected unspent outputs\n" | ||
| + " \"feeRate\" (numeric, optional, default not set: makes wallet determine the fee) Set a specific feerate (" + CURRENCY_UNIT + " per KB)\n" | ||
| + " \"subtractFeeFromAmount\" (array, optional) A json array with addresses.\n" |
ryanofsky
Nov 30, 2016
Contributor
Maybe mention after This will not modify existing inputs, and will add one change output to the outputs above that no existing outputs will be modified either unless subtractFeeFromAmount is specified.
| + " \"subtractFeeFromAmount\" (array, optional) A json array with addresses.\n" | ||
| + " The fee will be equally deducted from the amount of each selected address.\n" | ||
| + " Those recipients will receive less bitcoins than you enter in their corresponding amount field.\n" | ||
| + " If no addresses are specified here, the sender pays the fee.\n" |
ryanofsky
Nov 30, 2016
Contributor
Maybe s/If no addresses are specified here/If no addresses specified here are outputs in the transaction
| + throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, string("Invalid Bitcoin address: ")+strAddress); | ||
| + if (setSubtractFeeFromAmount.count(strAddress)) | ||
| + throw JSONRPCError(RPC_INVALID_PARAMETER, string("Invalid parameter, duplicated address: ")+strAddress); | ||
| + setSubtractFeeFromAmount.insert(strAddress); |
ryanofsky
Nov 30, 2016
Contributor
One difference between this and sendmany is that sendmany requires transaction outputs to be base58 addresses, and takes amounts and subtractfeefromamount arguments in base58 form, while fundrawtransaction allows outputs to be arbitrary scripts. This means with the PR in its current form, there may be no way for the new subtractFeeFromAmount argument to refer to certain outputs.
Instead of adding a subtractFeeFromAmount argument, I might suggest adding a subtractFeeFromPositions argument that takes a list of integer output indices. This would give callers the flexibility to refer to all outputs, be more consistent with the existing changePosition argument (which is also an integer output index), and also eliminate the need for ExtractDestination and CBitcoinAddress::ToString invocations in CWallet::FundTransaction.
| @@ -2181,14 +2181,16 @@ bool CWallet::SelectCoins(const vector<COutput>& vAvailableCoins, const CAmount& | ||
| return res; | ||
| } | ||
| -bool CWallet::FundTransaction(CMutableTransaction& tx, CAmount& nFeeRet, bool overrideEstimatedFeeRate, const CFeeRate& specificFeeRate, int& nChangePosInOut, std::string& strFailReason, bool includeWatching, bool lockUnspents, const CTxDestination& destChange) | ||
| +bool CWallet::FundTransaction(CMutableTransaction& tx, CAmount& nFeeRet, bool overrideEstimatedFeeRate, const CFeeRate& specificFeeRate, int& nChangePosInOut, std::string& strFailReason, bool includeWatching, bool lockUnspents, std::set<std::string>& setSubtractFeeFromAmount, const CTxDestination& destChange) |
ryanofsky
Nov 30, 2016
Contributor
Would suggest changing the new set<string> argument to set<int> to be consistent with the existing nChangePosInOut argument which refers to an output by integer index instead of base58 address string. This would give callers more flexibility in referring to outputs and also simplify handling of the new argument below.
|
I've addressed all the review comments, rebased, squashed, and pushed the resulting commit. I'm wondering whether there's a potential issue with using integers to select which outputs to subtract the fee from, since the outputs are specified by a JSON dictionary, and dictionary keys are inherently unordered. Are we guaranteed when we |
|
@dooglus good point, I don't think so. Recently ran into this writing extended rpc tests for something. |
|
@instagibbs me too:
Since the input to |
|
I don't think objects in JSON are assumed to have a meaningful ordering.
|
|
So we are saying that it's OK to use a list of integer indexes into the list of outputs because:
Right? (I tested point 3 as follows:
and found that the outputs appear in the raw transaction in the same order as listed in the input to |
|
I think reason (1) is enough to make position based indexing ok, and (2) strengthens it. I don't think (3) is a good reason or something we should ever rely on. The only reason this is brought up is because |
|
@sipa I'll look into making such a change in a separate PR. Using an object for the outputs not only means we cannot guarantee the order of the outputs but also having the addresses as dictionary keys means we can't have multiple outputs with the same address, which I sometimes like to do to (example). I assume it would be best to allow the current object format in addition to the new array format for backwards compatibility. |
|
+1 on address reuse being a sometimes valuable tool |
|
What happens next? I addressed all the comments. Is there something else I need to do? |
ryanofsky
approved these changes
Dec 13, 2016
Change looks good as is, just left a few possible suggestions.
Lightly tested ACK 56ea974.
| + assert_equal(result[0]['fee'], result[1]['fee'], result[2]['fee']) | ||
| + assert_equal(result[3]['fee'], result[4]['fee']) | ||
| + | ||
| + # change amounts in result 0 and 1 are the same |
ryanofsky
Dec 13, 2016
Contributor
This and the 5 following comments are basically just describing the asserts without adding any information. Could maybe remove the comments and condense the asserts.
| + | ||
| + dec_tx = [self.nodes[3].decoderawtransaction(result[0]['hex']), | ||
| + self.nodes[3].decoderawtransaction(result[1]['hex'])] | ||
| + |
ryanofsky
Dec 13, 2016
Contributor
Maybe add comment describing output, could be # Nested list of non-change output amounts for each transaction
| + output = [[out['value'] for i, out in enumerate(d['vout']) if i != r['changepos']] | ||
| + for d, r in zip(dec_tx, result)] | ||
| + | ||
| + share = [o0 - o1 for o0, o1 in zip(output[0], output[1])] |
ryanofsky
Dec 13, 2016
Contributor
Maybe add comment like # List of difference in output amounts between normal and subtractFee transactions.
| - raise AssertionError("%s != %s"%(str(thing1),str(thing2))) | ||
| +def assert_equal(thing1, thing2, *args): | ||
| + if thing1 != thing2 or any(thing1 != arg for arg in args): | ||
| + raise AssertionError("!(%s)" % " == ".join(str(arg) for arg in (thing1, thing2) + args)) |
dooglus
Dec 13, 2016
Contributor
I can't imagine who might have written that! ;)
Will change ! to not.
| + " \"includeWatching\" (boolean, optional, default false) Also select inputs which are watch only\n" | ||
| + " \"lockUnspents\" (boolean, optional, default false) Lock selected unspent outputs\n" | ||
| + " \"feeRate\" (numeric, optional, default not set: makes wallet determine the fee) Set a specific feerate (" + CURRENCY_UNIT + " per KB)\n" | ||
| + " \"subtractFeeFromOutputs\" (array, optional) A json array of integers.\n" |
ryanofsky
Dec 13, 2016
Contributor
Maybe just say an array instead of a json array, since the whole data structure is json.
dooglus
Dec 13, 2016
Contributor
Agree, but it appears that everywhere else we refer to 'json array' (see sendmany, addmultisigaddress, lockunspent, listunspent...). Nowhere (in rpcwallet.cpp at least) do we simply say 'an array'.
Will leave as 'json array' for the sake of consistency.
| + for (unsigned int idx = 0; idx < subtractFeeFromOutputs.size(); idx++) { | ||
| + int pos = subtractFeeFromOutputs[idx].get_int(); | ||
| + if (setSubtractFeeFromOutputs.count(pos)) | ||
| + throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("%s%d", "Invalid parameter, duplicated position: ", pos)); |
ryanofsky
Dec 13, 2016
Contributor
Little unusual to use %s for the main string instead of strprintf("Invalid parameter, duplicated position: %d", pos)
| @@ -2181,14 +2181,15 @@ bool CWallet::SelectCoins(const vector<COutput>& vAvailableCoins, const CAmount& | ||
| return res; | ||
| } | ||
| -bool CWallet::FundTransaction(CMutableTransaction& tx, CAmount& nFeeRet, bool overrideEstimatedFeeRate, const CFeeRate& specificFeeRate, int& nChangePosInOut, std::string& strFailReason, bool includeWatching, bool lockUnspents, const CTxDestination& destChange) | ||
| +bool CWallet::FundTransaction(CMutableTransaction& tx, CAmount& nFeeRet, bool overrideEstimatedFeeRate, const CFeeRate& specificFeeRate, int& nChangePosInOut, std::string& strFailReason, bool includeWatching, bool lockUnspents, std::set<int>& setSubtractFeeFromOutputs, const CTxDestination& destChange) |
| { | ||
| vector<CRecipient> vecSend; | ||
| // Turn the txout set into a CRecipient vector | ||
| - BOOST_FOREACH(const CTxOut& txOut, tx.vout) | ||
| + for (unsigned int idx = 0; idx < tx.vout.size(); idx++) |
|
Addressed @ryanofsky nits, rebased, squashed. |
|
Lightly tested ACK 453bda6 |
added a commit
to bitcoinknots/bitcoin
that referenced
this pull request
Dec 21, 2016
|
Can this be merged now? |
dooglus commentedNov 25, 2016
I notice it is now possible to have the fee subtracted from the output amounts by specifying a subtractFeeFromAmount parameter in both
sendtoaddressandsendmany, but not infundrawtransaction.This commit adds the option to
fundrawtransaction.