Cosmetic changes for path finding.

This commit is contained in:
Arthur Britto
2012-11-20 18:52:42 -08:00
parent 50cdc0c49f
commit 9755563e18
3 changed files with 160 additions and 126 deletions

View File

@@ -36,27 +36,24 @@ Test XRP to USD
Test USD to EUR Test USD to EUR
*/ */
// we sort the options by: // we sort the options by:
// cost of path // cost of path
// length of path // length of path
// width of path // width of path
// correct currency at the end // correct currency at the end
bool sortPathOptions(PathOption::pointer first, PathOption::pointer second) bool sortPathOptions(PathOption::pointer first, PathOption::pointer second)
{ {
if(first->mTotalCost<second->mTotalCost) return(true); if (first->mTotalCost<second->mTotalCost) return(true);
if(first->mTotalCost>second->mTotalCost) return(false); if (first->mTotalCost>second->mTotalCost) return(false);
if(first->mCorrectCurrency && !second->mCorrectCurrency) return(true); if (first->mCorrectCurrency && !second->mCorrectCurrency) return(true);
if(!first->mCorrectCurrency && second->mCorrectCurrency) return(false); if (!first->mCorrectCurrency && second->mCorrectCurrency) return(false);
if(first->mPath.getElementCount()<second->mPath.getElementCount()) return(true); if (first->mPath.getElementCount()<second->mPath.getElementCount()) return(true);
if(first->mPath.getElementCount()>second->mPath.getElementCount()) return(false); if (first->mPath.getElementCount()>second->mPath.getElementCount()) return(false);
if(first->mMinWidth<second->mMinWidth) return true; if (first->mMinWidth<second->mMinWidth) return true;
return false; return false;
} }
@@ -82,56 +79,73 @@ Pathfinder::Pathfinder(RippleAddress& srcAccountID, RippleAddress& dstAccountID,
mLedger=theApp->getMasterLedger().getCurrentLedger(); mLedger=theApp->getMasterLedger().getCurrentLedger();
} }
// Returns a single path, if possible.
// --> maxSearchSteps: unused
// --> maxPay: unused
bool Pathfinder::findPaths(int maxSearchSteps, int maxPay, STPathSet& retPathSet) bool Pathfinder::findPaths(int maxSearchSteps, int maxPay, STPathSet& retPathSet)
{ {
if(mLedger) { if (mLedger) {
std::queue<STPath> pqueue; std::queue<STPath> pqueue;
STPathElement ele(mSrcAccountID, STPathElement ele(mSrcAccountID,
mSrcCurrencyID, mSrcCurrencyID,
uint160()); uint160());
STPath path; STPath path;
path.addElement(ele);
path.addElement(ele); // Add the source.
pqueue.push(path); pqueue.push(path);
while(pqueue.size()) {
while (pqueue.size()) {
STPath path = pqueue.front(); STPath path = pqueue.front();
pqueue.pop();
// get the first path from the queue
ele = path.mPath.back(); pqueue.pop(); // Pop the first path from the queue.
// get the last node from the path
ele = path.mPath.back(); // Get the last node from the path.
// Determine if path is solved.
if (ele.mAccountID == mDstAccountID) { if (ele.mAccountID == mDstAccountID) {
path.mPath.erase(path.mPath.begin()); // Found a path to the destination.
path.mPath.erase(path.mPath.begin() + path.mPath.size()-1);
if (path.mPath.size() == 0) { if (2 == path.mPath.size()) {
// Empty path is default. Drop it.
continue; continue;
} }
// Remove implied first and last nodes.
path.mPath.erase(path.mPath.begin());
path.mPath.erase(path.mPath.begin() + path.mPath.size()-1);
// Return the path.
retPathSet.addPath(path); retPathSet.addPath(path);
return true; return true;
} }
// found the destination
if (!ele.mCurrencyID) { if (!ele.mCurrencyID) {
BOOST_FOREACH(OrderBook::pointer book,mOrderBook.getXRPInBooks()) // Last element is for XRP continue with qualifying books.
BOOST_FOREACH(OrderBook::pointer book, mOrderBook.getXRPInBooks())
{ {
//if (!path.hasSeen(line->getAccountIDPeer().getAccountID())) //if (!path.hasSeen(line->getAccountIDPeer().getAccountID()))
{ {
STPath new_path(path); STPath new_path(path);
STPathElement new_ele(uint160(), book->getCurrencyOut(), book->getIssuerOut()); STPathElement new_ele(uint160(), book->getCurrencyOut(), book->getIssuerOut());
new_path.mPath.push_back(new_ele); new_path.mPath.push_back(new_ele);
new_path.mCurrencyID = book->getCurrencyOut(); new_path.mCurrencyID = book->getCurrencyOut();
new_path.mCurrentAccount = book->getCurrencyOut(); new_path.mCurrentAccount = book->getCurrencyOut();
pqueue.push(new_path); pqueue.push(new_path);
} }
} }
} else { } else {
// Last element is for non-XRP continue by adding ripple lines and order books.
// Create new paths for each outbound account not already in the path.
RippleLines rippleLines(ele.mAccountID); RippleLines rippleLines(ele.mAccountID);
BOOST_FOREACH(RippleState::pointer line,rippleLines.getLines())
BOOST_FOREACH(RippleState::pointer line, rippleLines.getLines())
{ {
if (!path.hasSeen(line->getAccountIDPeer().getAccountID())) if (!path.hasSeen(line->getAccountIDPeer().getAccountID()))
{ {
@@ -143,10 +157,11 @@ bool Pathfinder::findPaths(int maxSearchSteps, int maxPay, STPathSet& retPathSet
new_path.mPath.push_back(new_ele); new_path.mPath.push_back(new_ele);
pqueue.push(new_path); pqueue.push(new_path);
} }
} // BOOST_FOREACHE }
// every offer that wants the source currency // Every book that wants the source currency.
std::vector<OrderBook::pointer> books; std::vector<OrderBook::pointer> books;
mOrderBook.getBooks(path.mCurrentAccount, path.mCurrencyID, books); mOrderBook.getBooks(path.mCurrentAccount, path.mCurrencyID, books);
BOOST_FOREACH(OrderBook::pointer book,books) BOOST_FOREACH(OrderBook::pointer book,books)
@@ -159,10 +174,9 @@ bool Pathfinder::findPaths(int maxSearchSteps, int maxPay, STPathSet& retPathSet
new_path.mCurrencyID=book->getCurrencyOut(); new_path.mCurrencyID=book->getCurrencyOut();
pqueue.push(new_path); pqueue.push(new_path);
}
} }
} // else
// enumerate all adjacent nodes, construct a new path and push it into the queue // enumerate all adjacent nodes, construct a new path and push it into the queue
} // While } // While
} // if there is a ledger } // if there is a ledger
@@ -196,9 +210,9 @@ bool Pathfinder::checkComplete(STPathSet& retPathSet)
void Pathfinder::addOptions(PathOption::pointer tail) void Pathfinder::addOptions(PathOption::pointer tail)
{ {
if(!tail->mCurrencyID) if (!tail->mCurrencyID)
{ // source XRP { // source XRP
BOOST_FOREACH(OrderBook::pointer book,mOrderBook.getXRPInBooks()) BOOST_FOREACH(OrderBook::pointer book, mOrderBook.getXRPInBooks())
{ {
PathOption::pointer pathOption(new PathOption(tail)); PathOption::pointer pathOption(new PathOption(tail));
@@ -209,7 +223,8 @@ void Pathfinder::addOptions(PathOption::pointer tail)
pathOption->mCurrencyID=book->getCurrencyOut(); pathOption->mCurrencyID=book->getCurrencyOut();
addPathOption(pathOption); addPathOption(pathOption);
} }
}else }
else
{ // ripple { // ripple
RippleLines rippleLines(tail->mCurrentAccount); RippleLines rippleLines(tail->mCurrentAccount);
BOOST_FOREACH(RippleState::pointer line,rippleLines.getLines()) BOOST_FOREACH(RippleState::pointer line,rippleLines.getLines())

View File

@@ -526,11 +526,19 @@ Json::Value RPCHandler::doOwnerInfo(const Json::Value& params)
} }
Json::Value RPCHandler::doPathFind(const Json::Value& params)
{
Json::Value ret(Json::objectValue);
return ret;
}
Json::Value RPCHandler::doPeers(const Json::Value& params) Json::Value RPCHandler::doPeers(const Json::Value& params)
{ {
// peers
Json::Value obj(Json::objectValue); Json::Value obj(Json::objectValue);
obj["peers"]=theApp->getConnectionPool().getPeersJson(); obj["peers"]=theApp->getConnectionPool().getPeersJson();
return obj; return obj;
} }
@@ -759,20 +767,21 @@ Json::Value RPCHandler::handleJSONSubmit(const Json::Value& jvRequest)
return rpcError(rpcDST_ACT_MALFORMED); return rpcError(rpcDST_ACT_MALFORMED);
} }
if(!txJSON.isMember("Fee")) if (!txJSON.isMember("Fee"))
{ {
if(mNetOps->getAccountState(uint256(0), dstAccountID)) if (mNetOps->getAccountState(uint256(0), dstAccountID))
txJSON["Fee"]=(int)theConfig.FEE_DEFAULT; txJSON["Fee"]=(int)theConfig.FEE_DEFAULT;
else txJSON["Fee"]=(int)theConfig.FEE_ACCOUNT_CREATE; else txJSON["Fee"]=(int)theConfig.FEE_ACCOUNT_CREATE;
} }
if(!txJSON.isMember("Paths") && jvRequest.isMember("build_path") ) if (!txJSON.isMember("Paths") && jvRequest.isMember("build_path"))
{ {
if(txJSON["Amount"].isObject() || txJSON.isMember("SendMax") ) if (txJSON["Amount"].isObject() || txJSON.isMember("SendMax"))
{ // we need a ripple path { // we need a ripple path
STPathSet spsPaths; STPathSet spsPaths;
uint160 srcCurrencyID; uint160 srcCurrencyID;
if(txJSON.isMember("SendMax") && txJSON["SendMax"].isMember("currency"))
if (txJSON.isMember("SendMax") && txJSON["SendMax"].isMember("currency"))
{ {
STAmount::currencyFromString(srcCurrencyID, txJSON["SendMax"]["currency"].asString()); STAmount::currencyFromString(srcCurrencyID, txJSON["SendMax"]["currency"].asString());
} }
@@ -782,7 +791,8 @@ Json::Value RPCHandler::handleJSONSubmit(const Json::Value& jvRequest)
} }
STAmount dstAmount; STAmount dstAmount;
if(txJSON["Amount"].isObject())
if (txJSON["Amount"].isObject())
{ {
std::string issuerStr; std::string issuerStr;
if( txJSON["Amount"].isMember("issuer")) issuerStr=txJSON["Amount"]["issuer"].asString(); if( txJSON["Amount"].isMember("issuer")) issuerStr=txJSON["Amount"]["issuer"].asString();
@@ -791,14 +801,17 @@ Json::Value RPCHandler::handleJSONSubmit(const Json::Value& jvRequest)
{ {
return rpcError(rpcDST_AMT_MALFORMED); return rpcError(rpcDST_AMT_MALFORMED);
} }
}else if (!dstAmount.setFullValue(txJSON["Amount"].asString())) }
else if (!dstAmount.setFullValue(txJSON["Amount"].asString()))
{ {
return rpcError(rpcDST_AMT_MALFORMED); return rpcError(rpcDST_AMT_MALFORMED);
} }
Pathfinder pf(srcAddress, dstAccountID, srcCurrencyID, dstAmount); Pathfinder pf(srcAddress, dstAccountID, srcCurrencyID, dstAmount);
pf.findPaths(5, 1, spsPaths); pf.findPaths(5, 1, spsPaths);
if(!spsPaths.isEmpty())
if (!spsPaths.isEmpty())
{ {
txJSON["Paths"]=spsPaths.getJson(0); txJSON["Paths"]=spsPaths.getJson(0);
if(txJSON.isMember("Flags")) txJSON["Flags"]=txJSON["Flags"].asUInt() | 2; if(txJSON.isMember("Flags")) txJSON["Flags"]=txJSON["Flags"].asUInt() | 2;
@@ -806,16 +819,18 @@ Json::Value RPCHandler::handleJSONSubmit(const Json::Value& jvRequest)
} }
} }
} }
}
}else if( txJSON["type"]=="OfferCreate" ) else if( txJSON["type"]=="OfferCreate" )
{ {
txJSON["TransactionType"]=7; txJSON["TransactionType"]=7;
if(!txJSON.isMember("Fee")) txJSON["Fee"]=(int)theConfig.FEE_DEFAULT; if(!txJSON.isMember("Fee")) txJSON["Fee"]=(int)theConfig.FEE_DEFAULT;
}else if( txJSON["type"]=="TrustSet") }
else if( txJSON["type"]=="TrustSet")
{ {
txJSON["TransactionType"]=20; txJSON["TransactionType"]=20;
if(!txJSON.isMember("Fee")) txJSON["Fee"]=(int)theConfig.FEE_DEFAULT; if(!txJSON.isMember("Fee")) txJSON["Fee"]=(int)theConfig.FEE_DEFAULT;
}else if( txJSON["type"]=="OfferCancel") }
else if( txJSON["type"]=="OfferCancel")
{ {
txJSON["TransactionType"]=8; txJSON["TransactionType"]=8;
if(!txJSON.isMember("Fee")) txJSON["Fee"]=(int)theConfig.FEE_DEFAULT; if(!txJSON.isMember("Fee")) txJSON["Fee"]=(int)theConfig.FEE_DEFAULT;
@@ -1332,55 +1347,57 @@ Json::Value RPCHandler::doCommand(const std::string& command, Json::Value& param
unsigned int iOptions; unsigned int iOptions;
} commandsA[] = { } commandsA[] = {
// Request-response methods // Request-response methods
{ "accept_ledger", &RPCHandler::doAcceptLedger, 0, 0, true }, { "accept_ledger", &RPCHandler::doAcceptLedger, 0, 0, true, false, optNone },
{ "account_info", &RPCHandler::doAccountInfo, 1, 2, false, false, optCurrent }, { "account_info", &RPCHandler::doAccountInfo, 1, 2, false, false, optCurrent },
{ "account_tx", &RPCHandler::doAccountTransactions, 2, 3, false, false, optNetwork }, { "account_tx", &RPCHandler::doAccountTransactions, 2, 3, false, false, optNetwork },
{ "connect", &RPCHandler::doConnect, 1, 2, true }, { "connect", &RPCHandler::doConnect, 1, 2, true, false, optNone },
{ "data_delete", &RPCHandler::doDataDelete, 1, 1, true }, { "data_delete", &RPCHandler::doDataDelete, 1, 1, true, false, optNone },
{ "data_fetch", &RPCHandler::doDataFetch, 1, 1, true }, { "data_fetch", &RPCHandler::doDataFetch, 1, 1, true, false, optNone },
{ "data_store", &RPCHandler::doDataStore, 2, 2, true }, { "data_store", &RPCHandler::doDataStore, 2, 2, true, false, optNone },
{ "get_counts", &RPCHandler::doGetCounts, 0, 1, true }, { "get_counts", &RPCHandler::doGetCounts, 0, 1, true, false, optNone },
{ "ledger", &RPCHandler::doLedger, 0, 2, false, false, optNetwork }, { "ledger", &RPCHandler::doLedger, 0, 2, false, false, optNetwork },
{ "ledger_accept", &RPCHandler::doLedgerAccept, 0, 0, true, false, optCurrent }, { "ledger_accept", &RPCHandler::doLedgerAccept, 0, 0, true, false, optCurrent },
{ "ledger_closed", &RPCHandler::doLedgerClosed, 0, 0, false, false, optClosed }, { "ledger_closed", &RPCHandler::doLedgerClosed, 0, 0, false, false, optClosed },
{ "ledger_current", &RPCHandler::doLedgerCurrent, 0, 0, false, false, optCurrent }, { "ledger_current", &RPCHandler::doLedgerCurrent, 0, 0, false, false, optCurrent },
{ "ledger_entry", &RPCHandler::doLedgerEntry, -1, -1, false, false, optCurrent }, { "ledger_entry", &RPCHandler::doLedgerEntry, -1, -1, false, false, optCurrent },
{ "ledger_header", &RPCHandler::doLedgerHeader, -1, -1, false, false, optCurrent }, { "ledger_header", &RPCHandler::doLedgerHeader, -1, -1, false, false, optCurrent },
{ "log_level", &RPCHandler::doLogLevel, 0, 2, true }, { "log_level", &RPCHandler::doLogLevel, 0, 2, true, false, optNone },
{ "logrotate", &RPCHandler::doLogRotate, 0, 0, true }, { "logrotate", &RPCHandler::doLogRotate, 0, 0, true, false, optNone },
{ "nickname_info", &RPCHandler::doNicknameInfo, 1, 1, false, false, optCurrent }, { "nickname_info", &RPCHandler::doNicknameInfo, 1, 1, false, false, optCurrent },
{ "owner_info", &RPCHandler::doOwnerInfo, 1, 2, false, false, optCurrent }, { "owner_info", &RPCHandler::doOwnerInfo, 1, 2, false, false, optCurrent },
{ "peers", &RPCHandler::doPeers, 0, 0, true }, { "path_find", &RPCHandler::doPathFind, -1, -1, false, false, optCurrent },
{ "peers", &RPCHandler::doPeers, 0, 0, true, false, optNone },
{ "profile", &RPCHandler::doProfile, 1, 9, false, false, optCurrent }, { "profile", &RPCHandler::doProfile, 1, 9, false, false, optCurrent },
{ "ripple_lines_get", &RPCHandler::doRippleLinesGet, 1, 2, false, false, optCurrent }, { "ripple_lines_get", &RPCHandler::doRippleLinesGet, 1, 2, false, false, optCurrent },
{ "submit", &RPCHandler::doSubmit, 2, 2, false, false, optCurrent }, { "submit", &RPCHandler::doSubmit, 2, 2, false, false, optCurrent },
{ "submit_json", &RPCHandler::doSubmitJson, -1, -1, false, false, optCurrent }, { "submit_json", &RPCHandler::doSubmitJson, -1, -1, false, false, optCurrent },
{ "server_info", &RPCHandler::doServerInfo, 0, 0, true }, { "server_info", &RPCHandler::doServerInfo, 0, 0, true, false, optNone },
{ "stop", &RPCHandler::doStop, 0, 0, true }, { "stop", &RPCHandler::doStop, 0, 0, true, false, optNone },
{ "transaction_entry", &RPCHandler::doTransactionEntry, -1, -1, false, false, optCurrent }, { "transaction_entry", &RPCHandler::doTransactionEntry, -1, -1, false, false, optCurrent },
{ "tx", &RPCHandler::doTx, 1, 1, true }, { "tx", &RPCHandler::doTx, 1, 1, true, false, optNone },
{ "tx_history", &RPCHandler::doTxHistory, 1, 1, false, }, { "tx_history", &RPCHandler::doTxHistory, 1, 1, false, false, optNone },
{ "unl_add", &RPCHandler::doUnlAdd, 1, 2, true }, { "unl_add", &RPCHandler::doUnlAdd, 1, 2, true, false, optNone },
{ "unl_delete", &RPCHandler::doUnlDelete, 1, 1, true }, { "unl_delete", &RPCHandler::doUnlDelete, 1, 1, true, false, optNone },
{ "unl_list", &RPCHandler::doUnlList, 0, 0, true }, { "unl_list", &RPCHandler::doUnlList, 0, 0, true, false, optNone },
{ "unl_load", &RPCHandler::doUnlLoad, 0, 0, true }, { "unl_load", &RPCHandler::doUnlLoad, 0, 0, true, false, optNone },
{ "unl_network", &RPCHandler::doUnlNetwork, 0, 0, true }, { "unl_network", &RPCHandler::doUnlNetwork, 0, 0, true, false, optNone },
{ "unl_reset", &RPCHandler::doUnlReset, 0, 0, true }, { "unl_reset", &RPCHandler::doUnlReset, 0, 0, true, false, optNone },
{ "unl_score", &RPCHandler::doUnlScore, 0, 0, true }, { "unl_score", &RPCHandler::doUnlScore, 0, 0, true, false, optNone },
{ "validation_create", &RPCHandler::doValidationCreate, 0, 1, false }, { "validation_create", &RPCHandler::doValidationCreate, 0, 1, false, false, optNone },
{ "validation_seed", &RPCHandler::doValidationSeed, 0, 1, false }, { "validation_seed", &RPCHandler::doValidationSeed, 0, 1, false, false, optNone },
{ "wallet_accounts", &RPCHandler::doWalletAccounts, 1, 1, false, false, optCurrent }, { "wallet_accounts", &RPCHandler::doWalletAccounts, 1, 1, false, false, optCurrent },
{ "wallet_propose", &RPCHandler::doWalletPropose, 0, 1, false, }, { "wallet_propose", &RPCHandler::doWalletPropose, 0, 1, false, false, optNone },
{ "wallet_seed", &RPCHandler::doWalletSeed, 0, 1, false, }, { "wallet_seed", &RPCHandler::doWalletSeed, 0, 1, false, false, optNone },
{ "login", &RPCHandler::doLogin, 2, 2, true }, { "login", &RPCHandler::doLogin, 2, 2, true, false, optNone },
// Evented methods // Evented methods
{ "subscribe", &RPCHandler::doSubscribe, -1, -1, false, true }, { "subscribe", &RPCHandler::doSubscribe, -1, -1, false, true, optNone },
{ "unsubscribe", &RPCHandler::doUnsubscribe, -1, -1, false, true }, }; { "unsubscribe", &RPCHandler::doUnsubscribe, -1, -1, false, true, optNone },
};
int i = NUMBER(commandsA); int i = NUMBER(commandsA);

View File

@@ -12,6 +12,7 @@ class RPCHandler
typedef Json::Value (RPCHandler::*doFuncPtr)(const Json::Value &params); typedef Json::Value (RPCHandler::*doFuncPtr)(const Json::Value &params);
enum { enum {
optNone = 0,
optNetwork = 1, // Need network optNetwork = 1, // Need network
optCurrent = 2+optNetwork, // Need current ledger optCurrent = 2+optNetwork, // Need current ledger
optClosed = 4+optNetwork, // Need closed ledger optClosed = 4+optNetwork, // Need closed ledger
@@ -50,6 +51,7 @@ class RPCHandler
Json::Value doOwnerInfo(const Json::Value& params); Json::Value doOwnerInfo(const Json::Value& params);
Json::Value doProfile(const Json::Value& params); Json::Value doProfile(const Json::Value& params);
Json::Value doPathFind(const Json::Value& params);
Json::Value doPeers(const Json::Value& params); Json::Value doPeers(const Json::Value& params);
Json::Value doRippleLinesGet(const Json::Value &params); Json::Value doRippleLinesGet(const Json::Value &params);