Skip to content

wallet: Use correct effective value when checking target - #26203

Merged
glozow merged 2 commits into
bitcoin:masterfrom
aureleoules:2022-09-fix-26185
Oct 4, 2022
Merged

glozow merged 2 commits into
bitcoin:masterfrom
aureleoules:2022-09-fix-26185

Conversation

@aureleoules

@aureleoules aureleoules commented Sep 29, 2022 •

Copy link
Copy Markdown
Contributor

Fixes #26185. The following assert failed because it was not checked in the parent function.

assert(selected_effective_value >= target);

@aureleoules

Copy link
Copy Markdown
Contributor Author

@1440000bytes could you check if this fixes your issue? Thanks.

@fanquake

Copy link
Copy Markdown
Member

Also includes a quick refactor.

Generally, we avoid throwing "quick refactors" into bug fixes. Especially if they are going to be backported. Or, at least the refactor should be a separate commit, so if it doesn't need backporting, it doesn't have to be.

@aureleoules

aureleoules commented Sep 29, 2022 •

Copy link
Copy Markdown
Contributor Author

Generally, we avoid throwing "quick refactors" into bug fixes.

Alright, removed.

@aureleoules

Copy link
Copy Markdown
Contributor Author

Added a unit test.

@ghost

ghost commented Sep 29, 2022 •

Copy link
Copy Markdown

@aureleoules I want to clarify one thing:

Is this a bug only on Windows or other platforms? If its a bug on some linux distro as well it will be easier for me to test

@ghost

ghost commented Sep 29, 2022

Copy link
Copy Markdown

@aureleoules I want to clarify one thing:

Is this a bug only on Windows or other platforms? If its a bug on some linux distro as well it will be easier for me to test

Or maybe that should be job of testers.. Give me one day I will test it.

@sipa

sipa commented Sep 29, 2022

Copy link
Copy Markdown
Member

This is a logic bug in the wallet. It's entirely platform independent.

@DrahtBot

Copy link
Copy Markdown
Contributor

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #25729 (wallet: Check max transaction weight in CoinSelection by aureleoules)
  • #25685 (wallet: Faster transaction creation by removing pre-set-inputs fetching responsibility from Coin Selection by furszy)

If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

@ghost

ghost commented Sep 30, 2022

Copy link
Copy Markdown

I don't understand whats happening in this issue. I compiled PR branch and tested it on Windows, it fixes the previous error and bitcoind or bitcoin-qt does not crash. However, returns "insufficient funds".

@sipa said this is platform independent but I tried to run the same command on Fedora with v24.0rc1 and it returns PSBT with a change:

Windows (PR Branch):

bitcoin-cli listunspent

[
  {
    "txid": "e911734bf711009dd6057c88bc598df2d31c18d1cc6bd6c790a704b519d77e68",
    "vout": 0,
    "address": "2N7uBTqqQ99ArbgmBuw2AANsf24bMVdYxgp",
    "label": "",
    "redeemScript": "522102de62833c851509cf13c0c63e0dc3ef7b74f7d86b17e9090fab68c09830cb2f2e2103cf11a2bc1371dad3691bce59e37d85b12aae6b030b533d4e7c5aedeb47a5ec0e52ae",
    "scriptPubKey": "a914a0bf75baf8f3a4ae761c2faa2516b8540e993d8987",
    "amount": 0.00100000,
    "confirmations": 595,
    "spendable": false,
    "solvable": true,
    "desc": "sh(multi(2,[aa130fed]02de62833c851509cf13c0c63e0dc3ef7b74f7d86b17e9090fab68c09830cb2f2e,[00890d8d]03cf11a2bc1371dad3691bce59e37d85b12aae6b030b533d4e7c5aedeb47a5ec0e))#j8w6g6zg",
    "parent_descs": [
    ],
    "safe": true
  }
]

bitcoin-cli walletcreatefundedpsbt "[{\"txid\":\"e911734bf711009dd6057c88bc598df2d31c18d1cc6bd6c790a704b519d77e68\",\"vout\":0}]" "[{\"tb1q9fxp2p0sm93jdlul8v3svzn7x69zcd9ljsgans\":\"0.0009\"}]"

Insufficient funds (code -4)

Fedora (v24.0rc1):

bitcoin-cli listunspent

[
  {
    "txid": "e911734bf711009dd6057c88bc598df2d31c18d1cc6bd6c790a704b519d77e68",
    "vout": 0,
    "address": "2N7uBTqqQ99ArbgmBuw2AANsf24bMVdYxgp",
    "label": "",
    "redeemScript": "522102de62833c851509cf13c0c63e0dc3ef7b74f7d86b17e9090fab68c09830cb2f2e2103cf11a2bc1371dad3691bce59e37d85b12aae6b030b533d4e7c5aedeb47a5ec0e52ae",
    "scriptPubKey": "a914a0bf75baf8f3a4ae761c2faa2516b8540e993d8987",
    "amount": 0.00100000,
    "confirmations": 595,
    "spendable": false,
    "solvable": true,
    "desc": "sh(multi(2,[aa130fed]02de62833c851509cf13c0c63e0dc3ef7b74f7d86b17e9090fab68c09830cb2f2e,[00890d8d]03cf11a2bc1371dad3691bce59e37d85b12aae6b030b533d4e7c5aedeb47a5ec0e))#j8w6g6zg",
    "parent_descs": [
    ],
    "safe": true
  }
]

bitcoin-cli  walletcreatefundedpsbt "[{\"txid\":\"e911734bf711009dd6057c88bc598df2d31c18d1cc6bd6c790a704b519d77e68\",\"vout\":0}]" "[{\"tb1q9fxp2p0sm93jdlul8v3svzn7x69zcd9ljsgans\":\"0.0009\"}]"

{
  "psbt": "cHNidP8BAHECAAAAAWh+1xm1BKeQx9ZrzNEYHNPyjVm8iHwF1p0AEfdLcxHpAAAAAAD9////AsYlAAAAAAAAFgAUJT0nNy2sgypq59m0DmCbgIWp0m6QXwEAAAAAABYAFCpMFQXw2WMm/587IwYKfjaKLDS/AAAAAAABAHICAAAAAQzC3aNgVZV/fAJNlVsvxAMFuTtvRKqlPocBAGlOSHxDAAAAAAD+////AqCGAQAAAAAAF6kUoL91uvjzpK52HC+qJRa4VA6ZPYmHm52Yh1IGAAAWABQN4fccLGiwTrWIb1+7fsWapgno0aOsAQABBEdSIQLeYoM8hRUJzxPAxj4Nw+97dPfYaxfpCQ+raMCYMMsvLiEDzxGivBNx2tNpG85Z432FsSquawMLUz1OfFrt60el7A5SriIGAt5igzyFFQnPE8DGPg3D73t099hrF+kJD6towJgwyy8uBKoTD+0iBgPPEaK8E3Ha02kbzlnjfYWxKq5rAwtTPU58Wu3rR6XsDgQAiQ2NACICAiOsbsXVfUGqw6ybbE+LIjii6xEJVH7K9GgwIxu6pruVEMw4p/EAAACAAQAAgAcAAIAAAA==",
  "fee": 0.00000330,
  "changepos": 0
}

@glozow

glozow commented Sep 30, 2022 •

Copy link
Copy Markdown
Contributor

@1440000bytes So IIUC you have 1 coin worth 0.001 BTC (as shown by your listunspent). You tried to create a transaction paying 0.0009 using walletcreatefundedpsbt, passing that coin in manually. Since you specified an input manually, m_allow_other_inputs is false.

// Automatically select coins, unless at least one is manually selected. Can
// be overridden by options.add_inputs.
coin_control.m_allow_other_inputs = rawTx.vin.size() == 0;
SetOptionsInputWeights(request.params[0], options);
FundTransaction(wallet, rawTx, fee, change_position, options, coin_control, /*override_min_fee=*/true);

This needs to fund the payment and the fees for the transaction, including the cost to spend the coin. 0.001 covers the payment and non-input fees, but not the cost to spend. So it is indeed the case that the wallet has insufficient funds.

cause of the crash and why the fix works:

SelectionResult::AddInput changes m_use_effective to true:

result.AddInput(preset_inputs);

void SelectionResult::AddInput(const OutputGroup& group)
{
util::insert(m_selected_inputs, group.m_outputs);
m_use_effective = !group.m_subtract_fee_outputs;
}

GetSelectedValue() is 0.001 and GetSelectedEffectiveValue() is something less than 0.0009.
So this condition doesn't hit:
if (result.GetSelectedValue() < nTargetValue) return std::nullopt;

But the assertion does, since selected_effective_value is something less than 0.0009.

@glozow glozow left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

concept ACK 1e60f5d4d5e9990e00ddbaa0cfacf3c5731d73f9
whether or not it's the cause of the crash in #26185, this is a bug fix, as AFAICT it's incorrect to not use effective value here

Comment thread src/wallet/test/coinselector_tests.cpp Resolved
@glozow
glozow requested review from S3RK and murchandamus September 30, 2022 17:36
@murchandamus

Copy link
Copy Markdown
Member

I don't understand whats happening in this issue. I compiled PR branch and tested it on Windows, it fixes the previous error and bitcoind or bitcoin-qt does not crash. However, returns "insufficient funds".

@sipa said this is platform independent but I tried to run the same command on Fedora with v24.0rc1 and it returns PSBT with a change:

@1440000bytes: I missed this at first, but could you confirm that the first output you posted is resulting from running PR/26203 (this branch) on Windows, and the second output is from running v24.0rc1 on Fedora?

@mzumsande

mzumsande commented Sep 30, 2022 •

Copy link
Copy Markdown
Contributor

Concept ACK.

The assert can also be reproduced by adjusting the amount in the psbt just between the regimes where it succeeds and the regime with insufficient funds. E.g. on an empty regtest datadir:

./bitcoind -regtest -fallbackfee=0.001
./bitcoin-cli -regtest -generate 101
./bitcoin-cli -regtest listunspent -> use txid in next call
./bitcoin-cli -regtest walletcreatefundedpsbt "[{\"txid\":\"<txid>\",\"vout\":0}]" "[{\"<some address>\":\"49.9999\"}]"

will lead to a crash on master (which is fixed by this PR).

@ghost

ghost commented Sep 30, 2022

Copy link
Copy Markdown

This needs to fund the payment and the fees for the transaction, including the cost to spend the coin. 0.001 covers the payment and non-input fees, but not the cost to spend. So it is indeed the case that the wallet has insufficient funds.

What is cost to spend?

@1440000bytes: I missed this at first, but could you confirm that the first output you posted is resulting from running PR/26203 (this branch) on Windows, and the second output is from running v24.0rc1 on Fedora?

Yes

and both have same config:

# Windows

signet=1
txindex=1

signet.rpcport=18332
rpcuser=user
rpcpassword=pass

fallbackfee=0.0004


# Fedora

signet=1
txindex=1

[signet] 
rpcport=18332
rpcuser=user
rpcpassword=pass

fallbackfee=0.0004

@murchandamus murchandamus left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Concept ACK, have a couple small improvement suggestions:

Comment thread src/wallet/coinselection.h Resolved
Comment thread src/wallet/spend.cpp Resolved
Comment thread src/wallet/test/coinselector_tests.cpp Resolved
@aureleoules
aureleoules force-pushed the 2022-09-fix-26185 branch 3 times, most recently from 721db50 to 0af938b Compare October 1, 2022 22:55
@aureleoules

Copy link
Copy Markdown
Contributor Author

@S3RK

S3RK commented Oct 3, 2022

Copy link
Copy Markdown
Contributor

Overall the change looks correct to me. I was trying to backport the test to see where the bug was introduced, but didn't succeed due to too many refactors. Is it a regression from 23.0 or not?

@glozow glozow left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reACK d0d9cf7

@glozow
glozow requested a review from murchandamus October 3, 2022 14:58

@furszy furszy left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK d0d9cf7

Happily, #25685 fixes this too. I introduced the same preset-inputs result selection target check inside aa5fdbad for another reason there. So.. the unit test introduced here is also passing there without having to cherry-pick the bug fix 76b79c1.

@glozow

glozow commented Oct 4, 2022

Copy link
Copy Markdown
Contributor

Happily, #25685 fixes this too.

That's good to hear, though I think this is more appropriate for a backport.
#26242

glozow added a commit that referenced this pull request Oct 5, 2022
…g target

2730ed2 test: Check external coin effective value is used in CoinSelection (Aurèle Oulès)
21f96f4 wallet: Use correct effective value when checking target (Aurèle Oulès)

Pull request description:

  backport of #26203

ACKs for top commit:
  jarolrod:
    ACK 2730ed2

Tree-SHA512: ce84ac8d47861f290a26d572512467e89ec6ac27973d954d76245b6c6fdea01e36f2e0bce41599abfe14d0014335ebd17b990177771803de39406097973186ca

@murchandamus murchandamus left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Post-merge ACK d0d9cf7

@achow101

achow101 commented Oct 6, 2022

Copy link
Copy Markdown
Member

Post merge ACK d0d9cf7

@aureleoules
aureleoules deleted the 2022-09-fix-26185 branch November 2, 2022 10:52
@bitcoin bitcoin locked and limited conversation to collaborators Nov 2, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bitcoind and bitcoin-qt crashes while creating PSBT