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

This commit is contained in:
JoelKatz
2012-09-24 23:26:13 -07:00
6 changed files with 143 additions and 101 deletions

View File

@@ -1,15 +1,22 @@
// YYY Should probably have two versions: node vs browser
var fs = require("fs");
var path = require("path");
Function.prototype.method = function(name,func) {
this.prototype[name] = func;
return this;
};
var filterErr = function(code, done) {
return function (e) {
return function(e) {
done(e.code !== code ? e : undefined);
};
};
var throwErr = function(done) {
return function (e) {
return function(e) {
if (e)
throw e;
@@ -20,7 +27,7 @@ var throwErr = function(done) {
// apply function to elements of array. Return first true value to done or undefined.
var mapOr = function(func, array, done) {
if (array.length) {
func(array[array.length-1], function (v) {
func(array[array.length-1], function(v) {
if (v) {
done(v);
}
@@ -37,7 +44,7 @@ var mapOr = function(func, array, done) {
// Make a directory and sub-directories.
var mkPath = function(dirPath, mode, done) {
fs.mkdir(dirPath, typeof mode === "string" ? parseInt(mode, 8) : mode, function (e) {
fs.mkdir(dirPath, typeof mode === "string" ? parseInt(mode, 8) : mode, function(e) {
if (!e || e.code === "EEXIST") {
// Created or already exists, done.
done();
@@ -62,7 +69,7 @@ var mkPath = function(dirPath, mode, done) {
// Empty a directory.
var emptyPath = function(dirPath, done) {
fs.readdir(dirPath, function (err, files) {
fs.readdir(dirPath, function(err, files) {
if (err) {
done(err);
}
@@ -76,7 +83,7 @@ var emptyPath = function(dirPath, done) {
var rmPath = function(dirPath, done) {
// console.log("rmPath: %s", dirPath);
fs.lstat(dirPath, function (err, stats) {
fs.lstat(dirPath, function(err, stats) {
if (err && err.code == "ENOENT") {
done();
}
@@ -84,7 +91,7 @@ var rmPath = function(dirPath, done) {
done(err);
}
else if (stats.isDirectory()) {
emptyPath(dirPath, function (e) {
emptyPath(dirPath, function(e) {
if (e) {
done(e);
}
@@ -103,7 +110,7 @@ var rmPath = function(dirPath, done) {
// Create directory if needed and empty if needed.
var resetPath = function(dirPath, mode, done) {
mkPath(dirPath, mode, function (e) {
mkPath(dirPath, mode, function(e) {
if (e) {
done(e);
}

View File

@@ -112,7 +112,8 @@ Json::Value callRPC(const std::string& strMethod, const Json::Value& params)
"If the file does not exist, create it with owner-readable-only file permissions.");
// Connect to localhost
std::cout << "Connecting to port:" << theConfig.RPC_PORT << std::endl;
std::cout << "Connecting to: " << theConfig.RPC_IP << ":" << theConfig.RPC_PORT << std::endl;
boost::asio::ip::tcp::endpoint
endpoint(boost::asio::ip::address::from_string(theConfig.RPC_IP), theConfig.RPC_PORT);
boost::asio::ip::tcp::iostream stream;

View File

@@ -941,24 +941,19 @@ STAmount LedgerEntrySet::accountHolds(const uint160& uAccountID, const uint160&
if (!uCurrencyID)
{
SLE::pointer sleAccount = entryCache(ltACCOUNT_ROOT, Ledger::getAccountRootIndex(uAccountID));
SLE::pointer sleAccount = entryCache(ltACCOUNT_ROOT, Ledger::getAccountRootIndex(uAccountID));
saAmount = sleAccount->getIValueFieldAmount(sfBalance);
Log(lsINFO) << "accountHolds: stamps: " << saAmount.getText();
}
else
{
saAmount = rippleHolds(uAccountID, uCurrencyID, uIssuerID);
Log(lsINFO) << "accountHolds: "
<< saAmount.getFullText()
<< " : "
<< STAmount::createHumanCurrency(uCurrencyID)
<< "/"
<< NewcoinAddress::createHumanAccountID(uIssuerID);
}
Log(lsINFO) << boost::str(boost::format("accountHolds: uAccountID=%s saAmount=%s")
% NewcoinAddress::createHumanAccountID(uAccountID)
% saAmount.getFullText());
return saAmount;
}
@@ -971,30 +966,22 @@ STAmount LedgerEntrySet::accountFunds(const uint160& uAccountID, const STAmount&
{
STAmount saFunds;
Log(lsINFO) << "accountFunds: uAccountID="
<< NewcoinAddress::createHumanAccountID(uAccountID);
Log(lsINFO) << "accountFunds: saDefault.isNative()=" << saDefault.isNative();
Log(lsINFO) << "accountFunds: saDefault.getIssuer()="
<< NewcoinAddress::createHumanAccountID(saDefault.getIssuer());
if (!saDefault.isNative() && saDefault.getIssuer() == uAccountID)
{
saFunds = saDefault;
Log(lsINFO) << "accountFunds: offer funds: ripple self-funded: " << saFunds.getText();
Log(lsINFO) << boost::str(boost::format("accountFunds: uAccountID=%s saDefault=%s SELF-FUNDED")
% NewcoinAddress::createHumanAccountID(uAccountID)
% saDefault.getFullText());
}
else
{
saFunds = accountHolds(uAccountID, saDefault.getCurrency(), saDefault.getIssuer());
Log(lsINFO) << "accountFunds: offer funds: uAccountID ="
<< NewcoinAddress::createHumanAccountID(uAccountID)
<< " : "
<< saFunds.getText()
<< "/"
<< saDefault.getHumanCurrency()
<< "/"
<< NewcoinAddress::createHumanAccountID(saDefault.getIssuer());
Log(lsINFO) << boost::str(boost::format("accountFunds: uAccountID=%s saDefault=%s saFunds=%s")
% NewcoinAddress::createHumanAccountID(uAccountID)
% saDefault.getFullText()
% saFunds.getFullText());
}
return saFunds;

View File

@@ -710,8 +710,8 @@ TER TransactionEngine::takeOffers(
boost::unordered_set<uint256> usOfferUnfundedBecame; // Offers that became unfunded.
boost::unordered_set<uint160> usAccountTouched; // Accounts touched.
saTakerPaid = 0;
saTakerGot = 0;
saTakerPaid = STAmount(saTakerPays.getCurrency(), saTakerPays.getIssuer());
saTakerGot = STAmount(saTakerGets.getCurrency(), saTakerGets.getIssuer());
while (temUNCERTAIN == terResult)
{
@@ -915,8 +915,11 @@ Log(lsWARNING) << "doOfferCreate> " << txn.getJson(0);
const bool bPassive = isSetBit(txFlags, tfPassive);
STAmount saTakerPays = txn.getITFieldAmount(sfTakerPays);
STAmount saTakerGets = txn.getITFieldAmount(sfTakerGets);
Log(lsWARNING) << "doOfferCreate: saTakerPays=" << saTakerPays.getFullText();
Log(lsWARNING) << "doOfferCreate: saTakerGets=" << saTakerGets.getFullText();
Log(lsINFO) << boost::str(boost::format("doOfferCreate: saTakerPays=%s saTakerGets=%s")
% saTakerPays.getFullText()
% saTakerGets.getFullText());
const uint160 uPaysIssuerID = saTakerPays.getIssuer();
const uint160 uGetsIssuerID = saTakerGets.getIssuer();
const uint32 uExpiration = txn.getITFieldU32(sfExpiration);
@@ -999,14 +1002,14 @@ Log(lsWARNING) << "doOfferCreate: saTakerGets=" << saTakerGets.getFullText();
STAmount saOfferGot;
const uint256 uTakeBookBase = Ledger::getBookBase(uGetsCurrency, uGetsIssuerID, uPaysCurrency, uPaysIssuerID);
Log(lsINFO) << boost::str(boost::format("doOfferCreate: take against book: %s : %s/%s -> %s/%s")
Log(lsINFO) << boost::str(boost::format("doOfferCreate: take against book: %s for %s -> %s")
% uTakeBookBase.ToString()
% saTakerGets.getHumanCurrency()
% NewcoinAddress::createHumanAccountID(saTakerGets.getIssuer())
% saTakerPays.getHumanCurrency()
% NewcoinAddress::createHumanAccountID(saTakerPays.getIssuer()));
% saTakerGets.getFullText()
% saTakerPays.getFullText());
// Take using the parameters of the offer.
#if 1
Log(lsWARNING) << "doOfferCreate: takeOffers: BEFORE saTakerGets=" << saTakerGets.getFullText();
terResult = takeOffers(
bPassive,
uTakeBookBase,
@@ -1017,12 +1020,14 @@ Log(lsWARNING) << "doOfferCreate: saTakerGets=" << saTakerGets.getFullText();
saOfferPaid, // How much was spent.
saOfferGot // How much was got.
);
#else
terResult = tesSUCCESS;
#endif
Log(lsWARNING) << "doOfferCreate: takeOffers=" << terResult;
Log(lsWARNING) << "doOfferCreate: takeOffers: saOfferPaid=" << saOfferPaid.getFullText();
Log(lsWARNING) << "doOfferCreate: takeOffers: saOfferGot=" << saOfferGot.getFullText();
Log(lsWARNING) << "doOfferCreate: takeOffers: saTakerPays=" << saTakerPays.getFullText();
Log(lsWARNING) << "doOfferCreate: takeOffers: saTakerGets=" << saTakerGets.getFullText();
Log(lsWARNING) << "doOfferCreate: takeOffers: AFTER saTakerGets=" << saTakerGets.getFullText();
if (tesSUCCESS == terResult)
{
@@ -1033,19 +1038,21 @@ Log(lsWARNING) << "doOfferCreate: saTakerGets=" << saTakerGets.getFullText();
Log(lsWARNING) << "doOfferCreate: takeOffers: saTakerPays=" << saTakerPays.getFullText();
Log(lsWARNING) << "doOfferCreate: takeOffers: saTakerGets=" << saTakerGets.getFullText();
Log(lsWARNING) << "doOfferCreate: takeOffers: saTakerGets=" << NewcoinAddress::createHumanAccountID(saTakerGets.getIssuer());
Log(lsWARNING) << "doOfferCreate: takeOffers: mTxnAccountID=" << NewcoinAddress::createHumanAccountID(mTxnAccountID);
Log(lsWARNING) << "doOfferCreate: takeOffers: funds=" << mNodes.accountFunds(mTxnAccountID, saTakerGets).getFullText();
Log(lsWARNING) << "doOfferCreate: takeOffers: FUNDS=" << mNodes.accountFunds(mTxnAccountID, saTakerGets).getFullText();
// Log(lsWARNING) << "doOfferCreate: takeOffers: uPaysIssuerID=" << NewcoinAddress::createHumanAccountID(uPaysIssuerID);
// Log(lsWARNING) << "doOfferCreate: takeOffers: uGetsIssuerID=" << NewcoinAddress::createHumanAccountID(uGetsIssuerID);
if (tesSUCCESS == terResult
&& saTakerPays // Still wanting something.
&& saTakerGets // Still offering something.
&& saTakerPays // Still wanting something.
&& saTakerGets // Still offering something.
&& mNodes.accountFunds(mTxnAccountID, saTakerGets).isPositive()) // Still funded.
{
// We need to place the remainder of the offer into its order book.
Log(lsINFO) << boost::str(boost::format("doOfferCreate: offer not fully consumed: saTakerPays=%s saTakerGets=%s")
% saTakerPays.getFullText()
% saTakerGets.getFullText());
// Add offer to owner's directory.
terResult = mNodes.dirAdd(uOwnerNode, Ledger::getOwnerDirIndex(mTxnAccountID), uLedgerIndex);
@@ -1069,12 +1076,13 @@ Log(lsWARNING) << "doOfferCreate: saTakerGets=" << saTakerGets.getFullText();
if (tesSUCCESS == terResult)
{
// Log(lsWARNING) << "doOfferCreate: uPaysIssuerID=" << NewcoinAddress::createHumanAccountID(uPaysIssuerID);
// Log(lsWARNING) << "doOfferCreate: uGetsIssuerID=" << NewcoinAddress::createHumanAccountID(uGetsIssuerID);
// Log(lsWARNING) << "doOfferCreate: saTakerPays.isNative()=" << saTakerPays.isNative();
// Log(lsWARNING) << "doOfferCreate: saTakerGets.isNative()=" << saTakerGets.isNative();
// Log(lsWARNING) << "doOfferCreate: uPaysCurrency=" << saTakerPays.getHumanCurrency();
// Log(lsWARNING) << "doOfferCreate: uGetsCurrency=" << saTakerGets.getHumanCurrency();
Log(lsWARNING) << "doOfferCreate: sfAccount=" << NewcoinAddress::createHumanAccountID(mTxnAccountID);
Log(lsWARNING) << "doOfferCreate: uPaysIssuerID=" << NewcoinAddress::createHumanAccountID(uPaysIssuerID);
Log(lsWARNING) << "doOfferCreate: uGetsIssuerID=" << NewcoinAddress::createHumanAccountID(uGetsIssuerID);
Log(lsWARNING) << "doOfferCreate: saTakerPays.isNative()=" << saTakerPays.isNative();
Log(lsWARNING) << "doOfferCreate: saTakerGets.isNative()=" << saTakerGets.isNative();
Log(lsWARNING) << "doOfferCreate: uPaysCurrency=" << saTakerPays.getHumanCurrency();
Log(lsWARNING) << "doOfferCreate: uGetsCurrency=" << saTakerGets.getHumanCurrency();
sleOffer->setIFieldAccount(sfAccount, mTxnAccountID);
sleOffer->setIFieldU32(sfSequence, uSequence);
@@ -1092,6 +1100,8 @@ Log(lsWARNING) << "doOfferCreate: saTakerGets=" << saTakerGets.getFullText();
}
}
Log(lsINFO) << "doOfferCreate: final sleOffer=" << sleOffer->getJson(0);
return terResult;
}

View File

@@ -6,7 +6,7 @@
//
var config = require("./config.js");
var utils = require("./utils.js");
var utils = require("../js/utils.js");
var fs = require("fs");
var path = require("path");
@@ -15,99 +15,137 @@ var child = require("child_process");
var servers = {};
var serverPath = function(name) {
return "tmp/server/" + name;
// Create a server object
var Server = function(name) {
this.name = name;
};
// Return a server's newcoind.cfg as string.
var configContent = function(name) {
var cfg = config.servers[name];
Server.method('configContent', function() {
var cfg = config.servers[this.name];
return Object.keys(cfg).map(function (o) {
return Object.keys(cfg).map(function(o) {
return util.format("[%s]\n%s\n", o, cfg[o]);
}).join("");
};
});
var configPath = function(name) {
return path.join(serverPath(name), "newcoind.cfg");
};
Server.method('serverPath', function() {
return "tmp/server/" + this.name;
});
Server.method('configPath', function() {
return path.join(this.serverPath(), "newcoind.cfg");
});
// Write a server's newcoind.cfg.
var writeConfig = function(name, done) {
fs.writeFile(configPath(name), configContent(name), 'utf8', done);
};
Server.method('writeConfig', function(done) {
fs.writeFile(this.configPath(), this.configContent(), 'utf8', done);
});
var serverSpawnSync = function(name) {
// Spawn the server.
Server.method('serverSpawnSync', function() {
// Spawn in standalone mode for now.
var server = child.spawn(
this.child = child.spawn(
config.newcoind,
[
"-a",
"--conf=newcoind.cfg"
],
{
cwd: serverPath(name),
cwd: this.serverPath(),
env: process.env,
stdio: 'inherit'
});
servers[name] = server;
console.log("server: %s: %s -a --conf=%s", server.pid, config.newcoind, configPath(name));
console.log("sever: start: servers = %s", Object.keys(servers).toString());
console.log("server: start %s: %s -a --conf=%s", this.child.pid, config.newcoind, this.configPath());
server.on('exit', function (code, signal) {
// By default, just log exits.
this.child.on('exit', function(code, signal) {
// If could not exec: code=127, signal=null
// If regular exit: code=0, signal=null
console.log("sever: spawn: server exited code=%s: signal=%s", code, signal);
delete servers[name];
console.log("server: spawn: server exited code=%s: signal=%s", code, signal);
});
};
});
var makeBase = function(name, done) {
var path = serverPath(name);
// Prepare server's working directory.
Server.method('makeBase', function(done) {
var path = this.serverPath();
var self = this;
// Reset the server directory, build it if needed.
utils.resetPath(path, '0777', function (e) {
utils.resetPath(path, '0777', function(e) {
if (e) {
throw e;
}
else {
writeConfig(name, done);
self.writeConfig(done);
}
});
};
});
// Create a standalone server.
// Prepare the working directory and spawn the server.
exports.start = function(name, done) {
makeBase(name, function (e) {
Server.method('start', function(done) {
var self = this;
this.makeBase(function(e) {
if (e) {
throw e;
}
else {
serverSpawnSync(name);
self.serverSpawnSync();
done();
}
});
};
});
exports.stop = function(name, done) {
console.log("sever: stop: servers = %s", Object.keys(servers).toString());
var server = servers[name];
if (server) {
server.on('exit', function (code, signal) {
console.log("sever: stop: server exited");
delete servers[name];
// Stop a standalone server.
Server.method('stop', function(done) {
if (this.child) {
// Update the on exit to invoke done.
this.child.on('exit', function(code, signal) {
console.log("server: stop: server exited");
done();
});
server.kill();
this.child.kill();
}
else
{
console.log("sever: stop: no such server");
console.log("server: stop: no such server");
done();
}
});
// Start the named server.
exports.start = function(name, done) {
if (servers[name])
{
console.log("server: start: server already started.");
}
else
{
var server = new Server(name);
servers[name] = server;
console.log("server: start: %s", JSON.stringify(server));
server.start(done);
}
};
// Delete the named server.
exports.stop = function(name, done) {
console.log("server: stop: %s of %s", name, Object.keys(servers).toString());
var server = servers[name];
if (server) {
server.stop(done);
delete servers[name];
}
};
exports.Server = Server;
// vim:ts=4

View File

@@ -6,11 +6,11 @@ var buster = require("buster");
var server = require("./server.js");
buster.testCase("Check standalone server startup", {
"server start and stop": function (done) {
"server start and stop": function(done) {
server.start("alpha",
function (e) {
function(e) {
buster.refute(e);
server.stop("alpha", function (e) {
server.stop("alpha", function(e) {
buster.refute(e);
done();
});
@@ -18,5 +18,4 @@ buster.testCase("Check standalone server startup", {
}
});
// console.log("standalone-test.js<");
// vim:ts=4