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

This commit is contained in:
JoelKatz
2013-03-19 23:05:07 -07:00
2 changed files with 111 additions and 46 deletions

View File

@@ -18,6 +18,7 @@
// L=current | closed | validated | index | hash
//
var async = require("async");
var extend = require("extend");
var http = require("http");
var url = require("url");
@@ -224,10 +225,9 @@ var rewrite_object = function (obj, opts) {
// Its a ledger entry.
if (obj.node.LedgerEntryType === 'AccountRoot') {
out.node.Account = rewrite_type('address', obj.node.Account, opts);
// out.node.hash = rewrite_type('transaction', obj.node.hash, opts);
out.node.PreviousTxnID = rewrite_type('transaction', out.node.PreviousTxnID, opts);
out.node.PreviousTxnLgrSeq = rewrite_type('ledger', out.node.PreviousTxnLgrSeq, opts);
rewrite_field('address', out.node, 'Account', opts);
rewrite_field('transaction', out.node, 'PreviousTxnID', opts);
rewrite_field('ledger', out.node, 'PreviousTxnLgrSeq', opts);
}
out.node.LedgerEntryType = '<B>' + out.node.LedgerEntryType + '</B>';
@@ -236,6 +236,86 @@ var rewrite_object = function (obj, opts) {
return out;
};
var augment_object = function (obj, opts, done) {
if (obj.node.LedgerEntryType == 'AccountRoot') {
var tx_hash = obj.node.PreviousTxnID;
var tx_ledger = obj.node.PreviousTxnLgrSeq;
obj.history = [];
async.whilst(
function () { return tx_hash; },
function (callback) {
// console.log("augment_object: request: %s %s", tx_hash, tx_ledger);
opts.remote.request_tx(tx_hash)
.on('success', function (m) {
tx_hash = undefined;
tx_ledger = undefined;
//console.log("augment_object: ", JSON.stringify(m));
m.meta.AffectedNodes.filter(function(n) {
// console.log("augment_object: ", JSON.stringify(n));
// if (n.ModifiedNode)
// console.log("augment_object: %s %s %s %s %s %s/%s", 'ModifiedNode' in n, n.ModifiedNode && (n.ModifiedNode.LedgerEntryType === 'AccountRoot'), n.ModifiedNode && n.ModifiedNode.FinalFields && (n.ModifiedNode.FinalFields.Account === obj.node.Account), Object.keys(n)[0], n.ModifiedNode && (n.ModifiedNode.LedgerEntryType), obj.node.Account, n.ModifiedNode && n.ModifiedNode.FinalFields && n.ModifiedNode.FinalFields.Account);
// if ('ModifiedNode' in n && n.ModifiedNode.LedgerEntryType === 'AccountRoot')
// {
// console.log("***: ", JSON.stringify(m));
// console.log("***: ", JSON.stringify(n));
// }
return 'ModifiedNode' in n
&& n.ModifiedNode.LedgerEntryType === 'AccountRoot'
&& n.ModifiedNode.FinalFields
&& n.ModifiedNode.FinalFields.Account === obj.node.Account;
})
.forEach(function (n) {
tx_hash = n.ModifiedNode.PreviousTxnID;
tx_ledger = n.ModifiedNode.PreviousTxnLgrSeq;
obj.history.push({
tx_hash: tx_hash,
tx_ledger: tx_ledger
});
console.log("augment_object: next: %s %s", tx_hash, tx_ledger);
});
callback();
})
.on('error', function (m) {
callback(m);
})
.request();
},
function (err) {
if (err) {
done();
}
else {
async.forEach(obj.history, function (o, callback) {
opts.remote.request_account_info(obj.node.Account)
.ledger_index(o.tx_ledger)
.on('success', function (m) {
//console.log("augment_object: ", JSON.stringify(m));
o.Balance = m.account_data.Balance;
// o.account_data = m.account_data;
callback();
})
.on('error', function (m) {
o.error = m;
callback();
})
.request();
},
function (err) {
done(err);
});
}
});
}
else {
done();
}
};
if (process.argv.length < 4 || process.argv.length > 7) {
console.log("Usage: %s ws_ip ws_port [<ip> [<port> [<start>]]]", program);
}
@@ -248,15 +328,10 @@ else {
// console.log("START");
var self = this;
self.base = {
hostname: ip,
port: port,
};
var remote = (new Remote({
websocket_ip: ws_ip,
websocket_port: ws_port,
// trace: true
trace: false
}))
.on('state', function (m) {
console.log("STATE: %s", m);
@@ -267,6 +342,12 @@ else {
.connect()
;
self.base = {
hostname: ip,
port: port,
remote: remote,
};
// console.log("SERVE");
var server = http.createServer(function (req, res) {
var input = "";
@@ -294,14 +375,16 @@ else {
.on('success', function (m) {
// console.log("account_root: %s", JSON.stringify(m, undefined, 2));
httpd_response(res,
{
statusCode: 200,
url: _url,
body: "<PRE>"
+ JSON.stringify(rewrite_object(m, self.base), undefined, 2)
+ "</PRE>"
});
augment_object(m, self.base, function() {
httpd_response(res,
{
statusCode: 200,
url: _url,
body: "<PRE>"
+ JSON.stringify(rewrite_object(m, self.base), undefined, 2)
+ "</PRE>"
});
});
})
.request();

View File

@@ -65,19 +65,11 @@ std::string EncodeBase64(const std::string& s)
// TODO New routine for parsing ledger parameters, other routines should standardize on this.
static bool jvParseLedger(Json::Value& jvRequest, const std::string& strLedger)
{
if (strLedger == "closed")
if (strLedger == "current" || strLedger == "closed" || strLedger == "validated")
{
jvRequest["ledger_index"] = -1;
jvRequest["ledger_index"] = strLedger;
}
else if (strLedger == "current")
{
jvRequest["ledger_index"] = -2;
}
else if (strLedger == "validated")
{
jvRequest["ledger_index"] = -3;
}
else if (strLedger.length() > 12)
else if (strLedger.length() == 32)
{
// YYY Could confirm this is a uint256.
jvRequest["ledger_hash"] = strLedger;
@@ -143,7 +135,7 @@ Json::Value RPCParser::parseInternal(const Json::Value& jvParams)
}
// account_info <account>|<nickname>|<account_public_key>
// account_info <seed>|<pass_phrase>|<key> [<index>]
// account_info <seed>|<pass_phrase>|<key> [[<index>] <ledger>]
Json::Value RPCParser::parseAccountInfo(const Json::Value& jvParams)
{
Json::Value jvRequest(Json::objectValue);
@@ -159,6 +151,9 @@ Json::Value RPCParser::parseAccountInfo(const Json::Value& jvParams)
jvRequest["ident"] = strIdent;
jvRequest["account_index"] = iIndex;
if (jvParams.size() == 3 && !jvParseLedger(jvRequest, jvParams[2u].asString()))
return rpcError(rpcLGR_IDX_MALFORMED);
return jvRequest;
}
@@ -343,20 +338,7 @@ Json::Value RPCParser::parseLedger(const Json::Value& jvParams)
return jvRequest;
}
std::string strLedger = jvParams[0u].asString();
if (strLedger == "current" || strLedger == "closed" || strLedger == "validated")
{
jvRequest["ledger_index"] = strLedger;
}
else if (strLedger.length() > 12)
{
jvRequest["ledger_hash"] = strLedger;
}
else
{
jvRequest["ledger_index"] = lexical_cast_s<uint32>(strLedger);
}
jvParseLedger(jvRequest, jvParams[0u].asString());
if (2 == jvParams.size() && jvParams[1u].asString() == "full")
{
@@ -373,7 +355,7 @@ Json::Value RPCParser::parseLedgerId(const Json::Value& jvParams)
std::string strLedger = jvParams[0u].asString();
if (strLedger.length() > 32)
if (strLedger.length() == 32)
{
jvRequest["ledger_hash"] = strLedger;
}
@@ -641,7 +623,7 @@ Json::Value RPCParser::parseCommand(std::string strMethod, Json::Value jvParams)
// Request-response methods
// - Returns an error, or the request.
// - To modify the method, provide a new method in the request.
{ "account_info", &RPCParser::parseAccountInfo, 1, 2 },
{ "account_info", &RPCParser::parseAccountInfo, 1, 3 },
{ "account_lines", &RPCParser::parseAccountItems, 1, 2 },
{ "account_offers", &RPCParser::parseAccountItems, 1, 2 },
{ "account_tx", &RPCParser::parseAccountTransactions, 2, 4 },