Merge branch 'master' of github.com:jedmccaleb/NewCoin

This commit is contained in:
JoelKatz
2012-10-04 23:44:25 -07:00
10 changed files with 629 additions and 359 deletions

View File

@@ -84,24 +84,65 @@ Pathfinder::Pathfinder(NewcoinAddress& srcAccountID, NewcoinAddress& dstAccountI
bool Pathfinder::findPaths(int maxSearchSteps, int maxPay, STPathSet& retPathSet)
{
if(mLedger)
{
PathOption::pointer head(new PathOption(mSrcAccountID,mSrcCurrencyID,mDstAmount.getCurrency()));
addOptions(head);
if(mLedger) {
std::queue<STPath> pqueue;
STPathElement ele(mSrcAccountID,
mSrcCurrencyID,
uint160());
STPath path;
path.addElement(ele);
pqueue.push(path);
while(pqueue.size()) {
for(int n=0; n<maxSearchSteps; n++)
{
std::list<PathOption::pointer> tempPaths=mBuildingPaths;
mBuildingPaths.clear();
BOOST_FOREACH(PathOption::pointer path,tempPaths)
{
addOptions(path);
}
if(checkComplete(retPathSet)) return(true);
}
}
STPath path = pqueue.front();
pqueue.pop();
// get the first path from the queue
return(false);
ele = path.mPath.back();
// get the last node from the path
if (ele.mAccountID == mDstAccountID) {
path.mPath.erase(path.mPath.begin());
path.mPath.erase(path.mPath.begin() + path.mPath.size()-1);
retPathSet.addPath(path);
return true;
}
// found the destination
if (!ele.mCurrencyID) {
BOOST_FOREACH(OrderBook::pointer book,mOrderBook.getXNSInBooks())
{
//if (!path.hasSeen(line->getAccountIDPeer().getAccountID()))
{
STPath new_path(path);
STPathElement new_ele(uint160(), book->getCurrencyOut(), book->getIssuerOut());
new_path.mPath.push_back(new_ele);
pqueue.push(new_path);
}
}
} else {
RippleLines rippleLines(ele.mAccountID);
BOOST_FOREACH(RippleState::pointer line,rippleLines.getLines())
{
if (!path.hasSeen(line->getAccountIDPeer().getAccountID()))
{
STPath new_path(path);
STPathElement new_ele(line->getAccountIDPeer().getAccountID(),
ele.mCurrencyID,
uint160());
new_path.mPath.push_back(new_ele);
pqueue.push(new_path);
}
}
}
// enumerate all adjacent nodes, construct a new path and push it into the queue
} // While
} // if there is a ledger
return false;
}
bool Pathfinder::checkComplete(STPathSet& retPathSet)

View File

@@ -1890,20 +1890,16 @@ Json::Value RPCServer::doSend(const Json::Value& params)
// XXX Don't allow send to self of same currency.
Transaction::pointer trans;
if (asDst) {
// Destination exists, ordinary send.
STPathSet spsPaths;
uint160 srcCurrencyID;
// bool ret_b;
// ret_b = false;
STPathSet spsPaths;
uint160 srcCurrencyID;
if (!saSrcAmountMax.isNative() || !saDstAmount.isNative())
{
STAmount::currencyFromString(srcCurrencyID, sSrcCurrency);
Pathfinder pf(naSrcAccountID, naDstAccountID, srcCurrencyID, saDstAmount);
// ret_b = pf.findPaths(5, 1, spsPaths);
pf.findPaths(5, 1, spsPaths);
}

View File

@@ -8,6 +8,13 @@ RippleLines::RippleLines(const uint160& accountID, Ledger::pointer ledger)
fillLines(accountID,ledger);
}
void RippleLines::printRippleLines() {
for (int i =0; i < mLines.size(); i++) {
std::cout << i << ": " << mLines[i]->getAccountID().humanAccountID() << std::endl;
}
std::cout << std::endl;
}
RippleLines::RippleLines(const uint160& accountID )
{
fillLines(accountID,theApp->getMasterLedger().getCurrentLedger());

View File

@@ -16,4 +16,5 @@ public:
RippleLines(const uint160& accountID ); // looks in the current ledger
std::vector<RippleState::pointer>& getLines(){ return(mLines); }
};
void printRippleLines();
};

View File

@@ -10,10 +10,35 @@
#include "Log.h"
#include "NewcoinAddress.h"
#include "utils.h"
#include "NewcoinAddress.h"
STAmount saZero(CURRENCY_ONE, ACCOUNT_ONE, 0);
STAmount saOne(CURRENCY_ONE, ACCOUNT_ONE, 1);
void STPathSet::printDebug() {
for (int i = 0; i < value.size(); i++) {
std::cout << i << ": ";
for (int j = 0; j < value[i].mPath.size(); j++) {
//STPathElement pe = value[i].mPath[j];
NewcoinAddress nad;
nad.setAccountID(value[i].mPath[j].mAccountID);
std::cout << " " << nad.humanAccountID();
//std::cout << " " << pe.mAccountID.GetHex();
}
std::cout << std::endl;
}
}
void STPath::printDebug() {
std::cout << "STPath:" << std::endl;
for(int i =0; i < mPath.size(); i++) {
NewcoinAddress nad;
nad.setAccountID(mPath[i].mAccountID);
std::cout << " " << i << ": " << nad.humanAccountID() << std::endl;
}
}
std::string SerializedType::getFullText() const
{
std::string ret;
@@ -331,6 +356,16 @@ bool STPathSet::isEquivalent(const SerializedType& t) const
return v && (value == v->value);
}
bool STPath::hasSeen(const uint160 &acct) {
for (int i = 0; i < mPath.size();i++) {
STPathElement ele = getElement(i);
if (ele.getAccountID() == acct)
return true;
}
return false;
}
int STPath::getSerializeSize() const
{
int iBytes = 0;

View File

@@ -518,6 +518,9 @@ public:
class STPathElement
{
friend class STPathSet;
friend class STPath;
friend class Pathfinder;
public:
enum {
typeEnd = 0x00,
@@ -567,6 +570,8 @@ public:
class STPath
{
friend class STPathSet;
friend class Pathfinder;
protected:
std::vector<STPathElement> mPath;
@@ -574,12 +579,14 @@ public:
STPath() { ; }
STPath(const std::vector<STPathElement>& p) : mPath(p) { ; }
void printDebug();
int getElementCount() const { return mPath.size(); }
bool isEmpty() const { return mPath.empty(); }
const STPathElement& getElement(int offset) const { return mPath[offset]; }
const STPathElement& getElemet(int offset) { return mPath[offset]; }
void addElement(const STPathElement& e) { mPath.push_back(e); }
void addElement(const STPathElement &e) { mPath.push_back(e); }
void clear() { mPath.clear(); }
bool hasSeen(const uint160 &acct);
int getSerializeSize() const;
// std::string getText() const;
Json::Value getJson(int) const;
@@ -636,7 +643,6 @@ protected:
static STPathSet* construct(SerializerIterator&, SField::ref);
public:
STPathSet() { ; }
STPathSet(SField::ref n) : SerializedType(n) { ; }
STPathSet(const std::vector<STPath>& v) : value(v) { ; }
@@ -658,6 +664,8 @@ public:
virtual bool isEquivalent(const SerializedType& t) const;
void printDebug();
std::vector<STPath>::iterator begin() { return value.begin(); }
std::vector<STPath>::iterator end() { return value.end(); }
std::vector<STPath>::const_iterator begin() const { return value.begin(); }

View File

@@ -89,8 +89,8 @@ public:
void doAccountTransactionSubscribe(Json::Value& jvResult, const Json::Value& jvRequest);
void doAccountTransactionUnsubscribe(Json::Value& jvResult, const Json::Value& jvRequest);
void doLedgerSubcribe(Json::Value& jvResult, const Json::Value& jvRequest);
void doLedgerUnsubscribe(Json::Value& jvResult, const Json::Value& jvRequest);
void doServerSubscribe(Json::Value& jvResult, const Json::Value& jvRequest);
void doServerUnsubscribe(Json::Value& jvResult, const Json::Value& jvRequest);
void doLedgerAccountsSubcribe(Json::Value& jvResult, const Json::Value& jvRequest);
void doLedgerAccountsUnsubscribe(Json::Value& jvResult, const Json::Value& jvRequest);
void doTransactionSubcribe(Json::Value& jvResult, const Json::Value& jvRequest);
@@ -311,10 +311,10 @@ Json::Value WSConnection::invokeCommand(const Json::Value& jvRequest)
{ "account_info_unsubscribe", &WSConnection::doAccountInfoUnsubscribe },
{ "account_transaction_subscribe", &WSConnection::doAccountTransactionSubscribe },
{ "account_transaction_unsubscribe", &WSConnection::doAccountTransactionUnsubscribe },
{ "ledger_subscribe", &WSConnection::doLedgerSubcribe },
{ "ledger_unsubscribe", &WSConnection::doLedgerUnsubscribe },
{ "ledger_accounts_subscribe", &WSConnection::doLedgerAccountsSubcribe },
{ "ledger_accounts_unsubscribe", &WSConnection::doLedgerAccountsUnsubscribe },
{ "server_subscribe", &WSConnection::doServerSubscribe },
{ "server_unsubscribe", &WSConnection::doServerUnsubscribe },
{ "transaction_subscribe", &WSConnection::doTransactionSubcribe },
{ "transaction_unsubscribe", &WSConnection::doTransactionUnsubscribe },
};
@@ -523,22 +523,6 @@ void WSConnection::doAccountTransactionUnsubscribe(Json::Value& jvResult, const
}
}
void WSConnection::doLedgerSubcribe(Json::Value& jvResult, const Json::Value& jvRequest)
{
if (!theApp->getOPs().subLedger(this))
{
jvResult["error"] = "ledgerSubscribed";
}
}
void WSConnection::doLedgerUnsubscribe(Json::Value& jvResult, const Json::Value& jvRequest)
{
if (!theApp->getOPs().unsubLedger(this))
{
jvResult["error"] = "ledgerNotSubscribed";
}
}
void WSConnection::doLedgerAccountsSubcribe(Json::Value& jvResult, const Json::Value& jvRequest)
{
if (!theApp->getOPs().subLedgerAccounts(this))
@@ -559,13 +543,13 @@ void WSConnection::doLedgerClosed(Json::Value& jvResult, const Json::Value& jvRe
{
uint256 uLedger = theApp->getOPs().getClosedLedger();
jvResult["ledger_index"] = theApp->getOPs().getLedgerID(uLedger);
jvResult["ledger"] = uLedger.ToString();
jvResult["ledger_closed_index"] = theApp->getOPs().getLedgerID(uLedger);
jvResult["ledger_closed"] = uLedger.ToString();
}
void WSConnection::doLedgerCurrent(Json::Value& jvResult, const Json::Value& jvRequest)
{
jvResult["ledger_index"] = theApp->getOPs().getCurrentLedgerID();
jvResult["ledger_current_index"] = theApp->getOPs().getCurrentLedgerID();
}
void WSConnection::doLedgerEntry(Json::Value& jvResult, const Json::Value& jvRequest)
@@ -606,10 +590,17 @@ void WSConnection::doLedgerEntry(Json::Value& jvResult, const Json::Value& jvReq
uLedgerIndex = lpLedger->getLedgerSeq(); // Set the current index.
}
if (!!uLedger)
jvResult["ledger"] = uLedger.ToString();
if (lpLedger->isClosed())
{
if (!!uLedger)
jvResult["ledger_closed"] = uLedger.ToString();
jvResult["ledger_index"] = uLedgerIndex;
jvResult["ledger_closed_index"] = uLedgerIndex;
}
else
{
jvResult["ledger_current_index"] = uLedgerIndex;
}
uint256 uNodeIndex;
bool bNodeBinary = false;
@@ -788,6 +779,32 @@ void WSConnection::doLedgerEntry(Json::Value& jvResult, const Json::Value& jvReq
}
}
void WSConnection::doServerSubscribe(Json::Value& jvResult, const Json::Value& jvRequest)
{
if (!theApp->getOPs().subLedger(this))
{
jvResult["error"] = "serverSubscribed";
}
else
{
if (theConfig.RUN_STANDALONE)
jvResult["stand_alone"] = 1;
// XXX Make sure these values are available before returning them.
// XXX return connected status.
jvResult["ledger_closed"] = theApp->getOPs().getClosedLedger().ToString();
jvResult["ledger_current_index"] = theApp->getOPs().getCurrentLedgerID();
}
}
void WSConnection::doServerUnsubscribe(Json::Value& jvResult, const Json::Value& jvRequest)
{
if (!theApp->getOPs().unsubLedger(this))
{
jvResult["error"] = "serverNotSubscribed";
}
}
void WSConnection::doTransactionSubcribe(Json::Value& jvResult, const Json::Value& jvRequest)
{
if (!theApp->getOPs().subTransaction(this))