MCPcopy Create free account
hub / github.com/ElementsProject/elements / ApproximateBestSubset

Function ApproximateBestSubset

src/wallet/coinselection.cpp:211–255  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

209}
210
211static void ApproximateBestSubset(const std::vector<OutputGroup>& groups, const CAmount& nTotalLower, const CAmount& nTargetValue,
212 std::vector<char>& vfBest, CAmount& nBest, int iterations = 1000)
213{
214 std::vector<char> vfIncluded;
215
216 vfBest.assign(groups.size(), true);
217 nBest = nTotalLower;
218
219 FastRandomContext insecure_rand;
220
221 for (int nRep = 0; nRep < iterations && nBest != nTargetValue; nRep++)
222 {
223 vfIncluded.assign(groups.size(), false);
224 CAmount nTotal = 0;
225 bool fReachedTarget = false;
226 for (int nPass = 0; nPass < 2 && !fReachedTarget; nPass++)
227 {
228 for (unsigned int i = 0; i < groups.size(); i++)
229 {
230 //The solver here uses a randomized algorithm,
231 //the randomness serves no real security purpose but is just
232 //needed to prevent degenerate behavior and it is important
233 //that the rng is fast. We do not use a constant random sequence,
234 //because there may be some privacy improvement by making
235 //the selection random.
236 if (nPass == 0 ? insecure_rand.randbool() : !vfIncluded[i])
237 {
238 nTotal += groups[i].GetSelectionAmount();
239 vfIncluded[i] = true;
240 if (nTotal >= nTargetValue)
241 {
242 fReachedTarget = true;
243 if (nTotal < nBest)
244 {
245 nBest = nTotal;
246 vfBest = vfIncluded;
247 }
248 nTotal -= groups[i].GetSelectionAmount();
249 vfIncluded[i] = false;
250 }
251 }
252 }
253 }
254 }
255}
256
257// ELEMENTS:
258std::optional<SelectionResult> KnapsackSolver(std::vector<OutputGroup>& groups, const CAmountMap& mapTargetValue)

Callers 1

KnapsackSolverFunction · 0.85

Calls 4

randboolMethod · 0.80
GetSelectionAmountMethod · 0.80
assignMethod · 0.45
sizeMethod · 0.45

Tested by

no test coverage detected