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

This commit is contained in:
JoelKatz
2013-02-15 05:39:28 -08:00
7 changed files with 140 additions and 32 deletions

View File

@@ -500,8 +500,9 @@ Json::Value RPCHandler::authorize(Ledger::ref lrLedger,
} }
// --> strIdent: public key, account ID, or regular seed. // --> strIdent: public key, account ID, or regular seed.
// --> bStrict: Only allow account id or public key.
// <-- bIndex: true if iIndex > 0 and used the index. // <-- bIndex: true if iIndex > 0 and used the index.
Json::Value RPCHandler::accountFromString(Ledger::ref lrLedger, RippleAddress& naAccount, bool& bIndex, const std::string& strIdent, const int iIndex) Json::Value RPCHandler::accountFromString(Ledger::ref lrLedger, RippleAddress& naAccount, bool& bIndex, const std::string& strIdent, const int iIndex, const bool bStrict)
{ {
RippleAddress naSeed; RippleAddress naSeed;
@@ -510,6 +511,10 @@ Json::Value RPCHandler::accountFromString(Ledger::ref lrLedger, RippleAddress& n
// Got the account. // Got the account.
bIndex = false; bIndex = false;
} }
else if (bStrict)
{
return rpcError(rpcACT_MALFORMED);
}
// Must be a seed. // Must be a seed.
else if (!naSeed.setSeedGeneric(strIdent)) else if (!naSeed.setSeedGeneric(strIdent))
{ {
@@ -570,6 +575,7 @@ Json::Value RPCHandler::doAcceptLedger(Json::Value jvRequest)
// { // {
// ident : <indent>, // ident : <indent>,
// account_index : <index> // optional // account_index : <index> // optional
// strict: <bool> // true, only allow public keys and addresses. false, default.
// ledger_hash : <ledger> // ledger_hash : <ledger>
// ledger_index : <ledger_index> // ledger_index : <ledger_index>
// } // }
@@ -587,11 +593,12 @@ Json::Value RPCHandler::doAccountInfo(Json::Value jvRequest)
std::string strIdent = jvRequest["ident"].asString(); std::string strIdent = jvRequest["ident"].asString();
bool bIndex; bool bIndex;
int iIndex = jvRequest.isMember("account_index") ? jvRequest["account_index"].asUInt() : 0; int iIndex = jvRequest.isMember("account_index") ? jvRequest["account_index"].asUInt() : 0;
bool bStrict = jvRequest.isMember("strict") && jvRequest["strict"].asBool();
RippleAddress naAccount; RippleAddress naAccount;
// Get info on account. // Get info on account.
Json::Value jvAccepted = accountFromString(lpLedger, naAccount, bIndex, strIdent, iIndex); Json::Value jvAccepted = accountFromString(lpLedger, naAccount, bIndex, strIdent, iIndex, bStrict);
if (!jvAccepted.empty()) if (!jvAccepted.empty())
return jvAccepted; return jvAccepted;
@@ -746,7 +753,7 @@ Json::Value RPCHandler::doNicknameInfo(Json::Value params)
// 'ident' : <indent>, // 'ident' : <indent>,
// 'account_index' : <index> // optional // 'account_index' : <index> // optional
// } // }
// XXX This would be better if it too the ledger. // XXX This would be better if it took the ledger.
Json::Value RPCHandler::doOwnerInfo(Json::Value jvRequest) Json::Value RPCHandler::doOwnerInfo(Json::Value jvRequest)
{ {
if (!jvRequest.isMember("ident")) if (!jvRequest.isMember("ident"))
@@ -761,11 +768,11 @@ Json::Value RPCHandler::doOwnerInfo(Json::Value jvRequest)
// Get info on account. // Get info on account.
Json::Value jAccepted = accountFromString(mNetOps->getClosedLedger(), raAccount, bIndex, strIdent, iIndex); Json::Value jAccepted = accountFromString(mNetOps->getClosedLedger(), raAccount, bIndex, strIdent, iIndex, false);
ret["accepted"] = jAccepted.empty() ? mNetOps->getOwnerInfo(mNetOps->getClosedLedger(), raAccount) : jAccepted; ret["accepted"] = jAccepted.empty() ? mNetOps->getOwnerInfo(mNetOps->getClosedLedger(), raAccount) : jAccepted;
Json::Value jCurrent = accountFromString(mNetOps->getCurrentLedger(), raAccount, bIndex, strIdent, iIndex); Json::Value jCurrent = accountFromString(mNetOps->getCurrentLedger(), raAccount, bIndex, strIdent, iIndex, false);
ret["current"] = jCurrent.empty() ? mNetOps->getOwnerInfo(mNetOps->getCurrentLedger(), raAccount) : jCurrent; ret["current"] = jCurrent.empty() ? mNetOps->getOwnerInfo(mNetOps->getCurrentLedger(), raAccount) : jCurrent;
@@ -901,7 +908,7 @@ Json::Value RPCHandler::doAccountLines(Json::Value jvRequest)
RippleAddress raAccount; RippleAddress raAccount;
jvResult = accountFromString(lpLedger, raAccount, bIndex, strIdent, iIndex); jvResult = accountFromString(lpLedger, raAccount, bIndex, strIdent, iIndex, false);
if (!jvResult.empty()) if (!jvResult.empty())
return jvResult; return jvResult;
@@ -979,7 +986,7 @@ Json::Value RPCHandler::doAccountOffers(Json::Value jvRequest)
RippleAddress raAccount; RippleAddress raAccount;
jvResult = accountFromString(lpLedger, raAccount, bIndex, strIdent, iIndex); jvResult = accountFromString(lpLedger, raAccount, bIndex, strIdent, iIndex, false);
if (!jvResult.empty()) if (!jvResult.empty())
return jvResult; return jvResult;
@@ -2090,7 +2097,7 @@ Json::Value RPCHandler::lookupLedger(Json::Value jvRequest, Ledger::pointer& lpL
Json::Value jvResult; Json::Value jvResult;
uint256 uLedger = jvRequest.isMember("ledger_hash") ? uint256(jvRequest["ledger_hash"].asString()) : 0; uint256 uLedger = jvRequest.isMember("ledger_hash") ? uint256(jvRequest["ledger_hash"].asString()) : 0;
uint32 uLedgerIndex = jvRequest.isMember("ledger_index") && jvRequest["ledger_index"].isNumeric() ? jvRequest["ledger_index"].asUInt() : 0; int32 iLedgerIndex = jvRequest.isMember("ledger_index") && jvRequest["ledger_index"].isNumeric() ? jvRequest["ledger_index"].asInt() : -2;
if (!!uLedger) if (!!uLedger)
{ {
@@ -2100,38 +2107,51 @@ Json::Value RPCHandler::lookupLedger(Json::Value jvRequest, Ledger::pointer& lpL
if (!lpLedger) if (!lpLedger)
{ {
jvResult["error"] = "ledgerNotFound"; jvResult["error"] = "ledgerNotFound";
return jvResult; return jvResult;
} }
uLedgerIndex = lpLedger->getLedgerSeq(); // Set the current index, override if needed. iLedgerIndex = lpLedger->getLedgerSeq(); // Set the current index, override if needed.
} }
else if (!!uLedgerIndex) if (-1 == iLedgerIndex)
{ {
lpLedger = mNetOps->getLedgerBySeq(uLedgerIndex); lpLedger = theApp->getLedgerMaster().getClosedLedger();
iLedgerIndex = lpLedger->getLedgerSeq();
}
if (-2 == iLedgerIndex)
{
// Default to current ledger.
lpLedger = mNetOps->getCurrentLedger();
iLedgerIndex = lpLedger->getLedgerSeq();
}
else if (iLedgerIndex <= 0)
{
jvResult["error"] = "ledgerNotFound";
return jvResult;
}
else if (iLedgerIndex)
{
lpLedger = mNetOps->getLedgerBySeq(iLedgerIndex);
if (!lpLedger) if (!lpLedger)
{ {
jvResult["error"] = "ledgerNotFound"; // ledger_index from future? jvResult["error"] = "ledgerNotFound"; // ledger_index from future?
return jvResult; return jvResult;
} }
} }
else
{
// Default to current ledger.
lpLedger = mNetOps->getCurrentLedger();
uLedgerIndex = lpLedger->getLedgerSeq(); // Set the current index.
}
if (lpLedger->isClosed()) if (lpLedger->isClosed())
{ {
if (!!uLedger) if (!!uLedger)
jvResult["ledger_hash"] = uLedger.ToString(); jvResult["ledger_hash"] = uLedger.ToString();
jvResult["ledger_index"] = uLedgerIndex; jvResult["ledger_index"] = iLedgerIndex;
} }
else else
{ {
jvResult["ledger_current_index"] = uLedgerIndex; jvResult["ledger_current_index"] = iLedgerIndex;
} }
return jvResult; return jvResult;

View File

@@ -41,7 +41,7 @@ class RPCHandler
const RippleAddress& naVerifyGenerator); const RippleAddress& naVerifyGenerator);
Json::Value accounts(Ledger::ref lrLedger, const RippleAddress& naMasterGenerator); Json::Value accounts(Ledger::ref lrLedger, const RippleAddress& naMasterGenerator);
Json::Value accountFromString(Ledger::ref lrLedger, RippleAddress& naAccount, bool& bIndex, const std::string& strIdent, const int iIndex); Json::Value accountFromString(Ledger::ref lrLedger, RippleAddress& naAccount, bool& bIndex, const std::string& strIdent, const int iIndex, const bool bStrict);
Json::Value doAcceptLedger(Json::Value jvRequest); Json::Value doAcceptLedger(Json::Value jvRequest);

View File

@@ -68,6 +68,8 @@ std::vector<RippleAddress> TransactionMetaSet::getAffectedAccounts()
std::vector<RippleAddress> accounts; std::vector<RippleAddress> accounts;
accounts.reserve(10); accounts.reserve(10);
// This code should match the behavior of the JS method:
// Meta#getAffectedAccounts
BOOST_FOREACH(const STObject& it, mNodes) BOOST_FOREACH(const STObject& it, mNodes)
{ {
int index = it.getFieldIndex((it.getFName() == sfCreatedNode) ? sfNewFields : sfFinalFields); int index = it.getFieldIndex((it.getFName() == sfCreatedNode) ? sfNewFields : sfFinalFields);

View File

@@ -1,4 +1,6 @@
var extend = require('extend'); var extend = require('extend');
var UInt160 = require('./uint160').UInt160;
var Amount = require('./amount').Amount;
/** /**
* Meta data processing facility. * Meta data processing facility.
@@ -71,6 +73,38 @@ Meta.prototype.each = function (fn)
for (var i = 0, l = this.nodes.length; i < l; i++) { for (var i = 0, l = this.nodes.length; i < l; i++) {
fn(this.nodes[i], i); fn(this.nodes[i], i);
} }
} };
var amountFieldsAffectingIssuer = [
"LowLimit", "HighLimit", "TakerPays", "TakerGets"
];
Meta.prototype.getAffectedAccounts = function ()
{
var accounts = [];
// This code should match the behavior of the C++ method:
// TransactionMetaSet::getAffectedAccounts
this.each(function (an) {
var fields = (an.diffType === "CreatedNode") ? an.fieldsNew : an.fieldsFinal;
for (var i in fields) {
var field = fields[i];
if ("string" === typeof field && UInt160.is_valid(field)) {
accounts.push(field);
} else if (amountFieldsAffectingIssuer.indexOf(i) !== -1) {
var amount = Amount.from_json(field);
var issuer = amount.issuer();
if (issuer.is_valid() && !issuer.is_zero()) {
accounts.push(issuer.to_json());
}
}
}
});
console.log("AFFECTS", accounts);
return accounts;
};
exports.Meta = Meta; exports.Meta = Meta;

View File

@@ -573,23 +573,23 @@ Remote.prototype._connect_message = function (ws, json) {
case 'account': case 'account':
// XXX If not trusted, need proof. // XXX If not trusted, need proof.
if (this.trace) utils.logObject("remote: account: %s", message);
// Process metadata // Process metadata
message.mmeta = new Meta(message.meta); message.mmeta = new Meta(message.meta);
// Pass the event on to any related Account objects // Pass the event on to any related Account objects
message.mmeta.each(function (an) { var affected = message.mmeta.getAffectedAccounts();
if (an.entryType === 'AccountRoot') { for (var i = 0, l = affected.length; i < l; i++) {
var account = self._accounts[an.fields.Account]; var account = self._accounts[affected[i]];
// Only trigger the event if the account object is actually // Only trigger the event if the account object is actually
// subscribed - this prevents some weird phantom events from // subscribed - this prevents some weird phantom events from
// occurring. // occurring.
if (account && account._subs) { if (account && account._subs) {
account.emit('transaction', message); account.emit('transaction', message);
}
} }
}); }
this.emit('account', message); this.emit('account', message);
break; break;

View File

@@ -92,6 +92,10 @@ UInt.prototype.is_valid = function () {
return this._value instanceof BigInteger; return this._value instanceof BigInteger;
}; };
UInt.prototype.is_zero = function () {
return this._value.equals(BigInteger.ZERO);
};
// value = NaN on error. // value = NaN on error.
UInt.prototype.parse_generic = function (j) { UInt.prototype.parse_generic = function (j) {
// Canonicalize and validate // Canonicalize and validate

View File

@@ -906,7 +906,6 @@ buster.testCase("Via offers", {
// 'setUp' : testutils.build_setup({ verbose: true, no_server: true }), // 'setUp' : testutils.build_setup({ verbose: true, no_server: true }),
'tearDown' : testutils.build_teardown(), 'tearDown' : testutils.build_teardown(),
// XXX Triggers bad path expansion.
"Via gateway" : "Via gateway" :
// carol holds mtgoxAUD, sells mtgoxAUD for XRP // carol holds mtgoxAUD, sells mtgoxAUD for XRP
// bob will hold mtgoxAUD // bob will hold mtgoxAUD
@@ -1138,4 +1137,53 @@ buster.testCase("Via offers", {
}, },
}); });
buster.testCase("Indirect paths", {
// 'setUp' : testutils.build_setup(),
'setUp' : testutils.build_setup({ verbose: true }),
// 'setUp' : testutils.build_setup({ verbose: true, no_server: true }),
'tearDown' : testutils.build_teardown(),
"//path find" :
function (done) {
var self = this;
async.waterfall([
function (callback) {
self.what = "Create accounts.";
testutils.create_accounts(self.remote, "root", "10000.0", ["alice", "bob", "carol"], callback);
},
function (callback) {
self.what = "Set credit limits.";
testutils.credit_limits(self.remote,
{
"bob" : "1000/USD/alice",
"carol" : "2000/USD/bob",
},
callback);
},
function (callback) {
self.what = "Find path from alice to carol";
self.remote.request_ripple_path_find("alice", "carol", "5/USD/carol",
[ { 'currency' : "USD" } ])
.on('success', function (m) {
console.log("proposed: %s", JSON.stringify(m));
// 1 alternative.
buster.assert.equals(1, m.alternatives.length)
// Path is empty.
buster.assert.equals(0, m.alternatives[0].paths_canonical.length)
callback();
})
.request();
},
], function (error) {
buster.refute(error, self.what);
done();
});
},
});
// vim:sw=2:sts=2:ts=8:et // vim:sw=2:sts=2:ts=8:et