Skip to content

fuzz: improve coinselection - #27585

Merged
achow101 merged 9 commits into
bitcoin:masterfrom
brunoerg:2023-05-fuzz-coinselection
Aug 24, 2023
Merged

achow101 merged 9 commits into
bitcoin:masterfrom
brunoerg:2023-05-fuzz-coinselection

Conversation

@brunoerg

@brunoerg brunoerg commented May 5, 2023

Copy link
Copy Markdown
Contributor

This PR:

  • Moves coin creation to its own function called CreateCoins.
  • Add coverage for EligibleForSpending
  • Add coverage for AddInputs: get result of each algorithm (srd, knapsack and bnb), call CreateCoins and add into them.
  • Add coverage for GetShuffledInputVector and GetInputSet using the result of each algorithm (srd, knapsack and bnb).
  • Add coverage for Merge: Call SRD with the new utxos and, if successful, try to merge with the previous SRD result.

@DrahtBot

DrahtBot commented May 5, 2023 •

Copy link
Copy Markdown
Contributor

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

Reviews

See the guideline for information on the review process.

Type Reviewers
ACK furszy, murchandamus, achow101

If your review is incorrectly listed, please react with 👎 to this comment and the bot will ignore it on the next update.

Conflicts

No conflicts as of last run.

@DrahtBot DrahtBot added the Tests label May 5, 2023
@brunoerg
brunoerg force-pushed the 2023-05-fuzz-coinselection branch from c59582a to d5282a4 Compare May 5, 2023 18:06
@maflcko maflcko added the Wallet label May 8, 2023
@fanquake

fanquake commented May 8, 2023

Copy link
Copy Markdown
Member

cc @achow101 @xekyo @furszy

Comment thread src/wallet/test/fuzz/coinselection.cpp Resolved

@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.

I’m pretty new to fuzz testing, so I am not sure whether we want to have all these additional calls for each selection result. It seems to me that if we wanted to test the behavior of SelectionResult, it might make more sense to separately fuzz that instead of fuzzing that in the context of running all the different coin selection algorithms, since it might lower the number of executions we get on fuzzing coin selection.

OTOH, it’s great to get fuzzing coverage for these codepaths.

Code changes look good to me.

Tentative ACK d5282a45bd452844dc815181825e3dd46133ac51, except that I’d be curious to hear what others think about expanding the responsibility of this fuzz target vs adding a second fuzz target for testing SelectionResult.

Comment thread src/wallet/test/fuzz/coinselection.cpp Resolved
Comment thread src/wallet/test/fuzz/coinselection.cpp Resolved
@brunoerg
brunoerg force-pushed the 2023-05-fuzz-coinselection branch from d5282a4 to 82cd264 Compare June 1, 2023 21:39
@brunoerg

brunoerg commented Jun 1, 2023

Copy link
Copy Markdown
Contributor Author

Force-pushed addressing #27585 (comment) and #27585 (comment)

@brunoerg
brunoerg force-pushed the 2023-05-fuzz-coinselection branch from 82cd264 to 2d739a4 Compare June 1, 2023 21:41
Comment thread src/wallet/test/fuzz/coinselection.cpp Resolved

@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.

ACK 2d739a425c92e2dd409c45273e8e376d39ead1cc

My concern about the min_viable_change and change_fee causing issues did not substantiate after some light fuzzing. Since it does not seem to cause crashes, having independent values for the two parameters may be better. Thanks for adding more coverage.

Comment thread src/wallet/test/fuzz/coinselection.cpp Resolved
@brunoerg

Copy link
Copy Markdown
Contributor Author

Rebased

@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.

reACK bd6315a2a75d2891a8d3476c492d6b53309b7296

@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.

Left few comments. I think that we could go further in few places and check that the called functions are actually doing what they suppose to be doing.

For instance, all valid results must have a target below the sum of the selected inputs amounts. Also, waste on results with more difference between target and inputs sum should be greater than results with closer diff between target and inputs sum.

Comment thread src/wallet/test/fuzz/coinselection.cpp Resolved
Comment thread src/wallet/test/fuzz/coinselection.cpp Resolved
Comment thread src/wallet/test/fuzz/coinselection.cpp Resolved
@brunoerg
brunoerg force-pushed the 2023-05-fuzz-coinselection branch from bd6315a to 1e0f5f0 Compare August 3, 2023 20:46
@brunoerg

brunoerg commented Aug 3, 2023

Copy link
Copy Markdown
Contributor Author

Force-pushed:

Addressed:

Also, I added support for GetSelectedValue(), asserting that its value is always greater or equal than target in a valid result.

@brunoerg

brunoerg commented Aug 4, 2023

Copy link
Copy Markdown
Contributor Author

Force-pushed addressing @furszy's suggestion to check whether GetChange is always 0 for any BnB result.

@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.

Code review ACK 1e0f5f088.

Comment thread src/wallet/test/fuzz/coinselection.cpp Resolved
Comment thread src/wallet/test/fuzz/coinselection.cpp Resolved
Comment thread src/wallet/test/fuzz/coinselection.cpp Resolved
@brunoerg
brunoerg force-pushed the 2023-05-fuzz-coinselection branch from 8c76af1 to 9e2b805 Compare August 9, 2023 22:55
@brunoerg
brunoerg force-pushed the 2023-05-fuzz-coinselection branch from d501e1a to 5f0237f Compare August 22, 2023 17:56
@brunoerg

Copy link
Copy Markdown
Contributor Author

Force-pushed fixing next_locktime in CreateCoins. Thanks, @furszy

@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.

Code review ACK 5f0237f2

As an extra small nit, I would merge the GetInputSet and GetShuffledInputSet commits into one (ec82d1c3a523a9b28adf66f840e239f4adead37a, 4531ab1aee85a6801e0de00816b3764b8462771c) as they are basically doing the same. But, not a big deal if you don't do it anyway.

Comment thread src/wallet/test/fuzz/coinselection.cpp Resolved
Comment thread src/wallet/test/fuzz/coinselection.cpp Resolved
@DrahtBot
DrahtBot requested a review from murchandamus August 23, 2023 16:01
Instead of using `cost_of_change` for `min_viable_change`
and `change_cost`, and 0 for `change_fee`, use values from
`coin_params`. The previous values don't generate any effects
that is relevant for that context.
The valid results should have a target below the sum of
the selected inputs amounts. Also, it increases the
minimum value for target to make it more realistic.
`m_cost_of_change` must not be generated randomly
independent from m_change_fee. This commit changes
it to set it up according to `wallet/spend`.
@brunoerg
brunoerg force-pushed the 2023-05-fuzz-coinselection branch from 5f0237f to bf26f97 Compare August 23, 2023 20:23
@brunoerg

brunoerg commented Aug 23, 2023 •

Copy link
Copy Markdown
Contributor Author

Force-pushed: changed to use DUST_RELAY_TX_FEE instead of hardcoding the value and squashed GetInputSet and GetShuffledInputSet commits.

@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.

re-ACK bf26f97

@murchandamus

Copy link
Copy Markdown
Member

reACK with some minimal fuzzing bf26f97

@DrahtBot
DrahtBot removed the request for review from murchandamus August 24, 2023 14:30
@achow101

Copy link
Copy Markdown
Member

ACK bf26f97

@achow101
achow101 merged commit 74d6635 into bitcoin:master Aug 24, 2023
@maflcko

maflcko commented Aug 25, 2023

Copy link
Copy Markdown
Member

This crashes:

fuzz: wallet/test/fuzz/coinselection.cpp:121: void wallet::coinselection_fuzz_target(FuzzBufferType): Assertion `result_bnb->GetChange(coin_params.m_cost_of_change, CAmount{0}) == 0' failed.


ACwnYWEAAAABAAABAAAgAAAAAI8AAAABAAABIAAAAACPAQAAAQAAAQB6AAAAAAAAAAAAAQAAAQAA
jwAAAAEAAAE=

@fanquake

Copy link
Copy Markdown
Member

This crashes:

Yea. OSS-Fuzz has this this now as well https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=61790:

Crash Type: ASSERT
Crash Address: 
Crash State:
  result_bnb->GetChange(coin_params.m_cost_of_change, CAmount{0}) == 0
  wallet::coinselection_fuzz_target
  std::__1::__function::__func<void

@maflcko maflcko added the Bug label Aug 27, 2023
@brunoerg

Copy link
Copy Markdown
Contributor Author

Investigating.

@murchandamus

Copy link
Copy Markdown
Member

Follow-up in #28372

@brunoerg

brunoerg commented Sep 2, 2023

Copy link
Copy Markdown
Contributor Author

Fuzz found an issue! Fix #28395

Frank-GER pushed a commit to syscoin/syscoin that referenced this pull request Sep 8, 2023
bf26f97 fuzz: coinselection, fix `m_cost_of_change` (brunoerg)
6d9b26d fuzz: coinselection, BnB should never produce change (brunoerg)
b2eb558 fuzz: coinselection, compare `GetSelectedValue` with target (brunoerg)
0df0438 fuzz: coinselection, improve `ComputeAndSetWaste` (brunoerg)
1e351e5 fuzz: coinselection, add coverage for `Merge` (brunoerg)
f0244a8 fuzz: coinselection, add coverage for `GetShuffledInputVector`/`GetInputSet` (brunoerg)
808618b fuzz: coinselection, add coverage for `AddInputs` (brunoerg)
90c4e6a fuzz: coinselection, add coverage for `EligibleForSpending` (brunoerg)
2a031cb fuzz: coinselection, add `CreateCoins` (brunoerg)

Pull request description:

  This PR:

  - Moves coin creation to its own function called `CreateCoins`.
  - Add coverage for `EligibleForSpending`
  - Add coverage for `AddInputs`: get result of each algorithm (srd, knapsack and bnb), call `CreateCoins` and add into them.
  - Add coverage for `GetShuffledInputVector` and `GetInputSet` using the result of each algorithm (srd, knapsack and bnb).
  - Add coverage for `Merge`: Call SRD with the new utxos and, if successful, try to merge with the previous SRD result.

ACKs for top commit:
  murchandamus:
    reACK with some minimal fuzzing bf26f97
  achow101:
    ACK bf26f97
  furszy:
    re-ACK bf26f97

Tree-SHA512: bdd2b0a39de37be0a9b21a7c51260b6b8abe538cc0ea74312eb658b90a121a1ae07306c09fb0e75e93b531ce9ea2402feb041b0d852902d07739257f792e64ab
maflcko pushed a commit to maflcko/DrahtBot that referenced this pull request Sep 14, 2023
@bitcoin bitcoin locked and limited conversation to collaborators Sep 1, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants