From e8e927a0fb774d1e974eff339694f82e32a71e2a Mon Sep 17 00:00:00 2001 From: JoelKatz Date: Fri, 29 Mar 2013 16:45:31 -0700 Subject: [PATCH] Last path must provide adequate liquidity. --- src/cpp/ripple/Pathfinder.cpp | 30 ++++++++++-------------------- 1 file changed, 10 insertions(+), 20 deletions(-) diff --git a/src/cpp/ripple/Pathfinder.cpp b/src/cpp/ripple/Pathfinder.cpp index fe2e9c9ade..78280190cb 100644 --- a/src/cpp/ripple/Pathfinder.cpp +++ b/src/cpp/ripple/Pathfinder.cpp @@ -79,7 +79,7 @@ PathOption::PathOption(PathOption::pointer other) #endif // quality, length, liquidity, index -typedef boost::tuple path_LQ_t; +typedef boost::tuple path_LQ_t; // Lower numbers have better quality. Sort higher quality first. static bool bQualityCmp(const path_LQ_t& a, const path_LQ_t&b) @@ -587,7 +587,7 @@ bool Pathfinder::findPaths(const unsigned int iMaxSteps, const unsigned int iMax % uQuality % spCurrent.getJson(0)); - vMap.push_back(path_LQ_t(uQuality, spCurrent.mPath.size(), saDstAmountAct, false, i)); + vMap.push_back(path_LQ_t(uQuality, spCurrent.mPath.size(), saDstAmountAct, i)); } else { @@ -600,35 +600,25 @@ bool Pathfinder::findPaths(const unsigned int iMaxSteps, const unsigned int iMax if (vMap.size()) { - iLimit = std::min(iMaxPaths, (unsigned int) vMap.size()); - bFound = true; std::sort(vMap.begin(), vMap.end(), bQualityCmp); // Lower is better and should be first. // Output best quality entries. STAmount remaining = mDstAmount; - for (int i = 0; i < iLimit; ++i) + for (int i = 0, iPathsLeft = iMaxPaths; (iPathsLeft > 0) && (i < vMap.size()); ++i) { path_LQ_t& lqt = vMap[i]; - remaining -= lqt.get<2>(); - lqt.get<3>() = true; - spsDst.addPath(vspResults[lqt.get<4>()]); - } - - if (remaining.isGEZero()) - { // we need an extra path to ensure liquidity - for (int i = 0; i < vMap.size(); ++i) - { - path_LQ_t& lqt = vMap[i]; - if (!lqt.get<3>() && (lqt.get<2>() >= remaining)) - { - spsDst.addPath(vspResults[lqt.get<4>()]); - break; - } + if ((iPathsLeft != 1) || (lqt.get<2>() >= remaining)) + { // last path must fill + --iPathsLeft; + remaining -= lqt.get<2>(); + spsDst.addPath(vspResults[lqt.get<3>()]); } } + bFound = !remaining.isGEZero(); + cLog(lsDEBUG) << boost::str(boost::format("findPaths: RESULTS: %s") % spsDst.getJson(0)); } else