diff --git a/test/utils.js b/js/utils.js similarity index 83% rename from test/utils.js rename to js/utils.js index 0bf10b18ea..6bb4ae7a2d 100644 --- a/test/utils.js +++ b/js/utils.js @@ -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); } diff --git a/src/Amount.cpp b/src/Amount.cpp index 4c35c46a06..f0692f2af7 100644 --- a/src/Amount.cpp +++ b/src/Amount.cpp @@ -359,19 +359,16 @@ STAmount* STAmount::construct(SerializerIterator& sit, const char *name) if ((value < cMinValue) || (value > cMaxValue) || (offset < cMinOffset) || (offset > cMaxOffset)) throw std::runtime_error("invalid currency value"); - sapResult = new STAmount(name, uCurrencyID, value, offset, isNegative); + sapResult = new STAmount(name, uCurrencyID, uIssuerID, value, offset, isNegative); } else { if (offset != 512) throw std::runtime_error("invalid currency value"); - sapResult = new STAmount(name, uCurrencyID); + sapResult = new STAmount(name, uCurrencyID, uIssuerID); } - if (sapResult) - sapResult->setIssuer(uIssuerID); - return sapResult; } @@ -522,7 +519,7 @@ STAmount& STAmount::operator-=(const STAmount& a) STAmount STAmount::operator-(void) const { if (mValue == 0) return *this; - return STAmount(name, mCurrency, mValue, mOffset, mIsNative, !mIsNegative); + return STAmount(name, mCurrency, mIssuer, mValue, mOffset, mIsNative, !mIsNegative); } STAmount& STAmount::operator=(uint64 v) @@ -620,9 +617,9 @@ STAmount operator+(const STAmount& v1, const STAmount& v2) int64 fv = vv1 + vv2; if (fv >= 0) - return STAmount(v1.name, v1.mCurrency, fv, ov1, false); + return STAmount(v1.name, v1.mCurrency, v1.mIssuer, fv, ov1, false); else - return STAmount(v1.name, v1.mCurrency, -fv, ov1, true); + return STAmount(v1.name, v1.mCurrency, v1.mIssuer, -fv, ov1, true); } STAmount operator-(const STAmount& v1, const STAmount& v2) @@ -652,9 +649,9 @@ STAmount operator-(const STAmount& v1, const STAmount& v2) int64 fv = vv1 - vv2; if (fv >= 0) - return STAmount(v1.name, v1.mCurrency, fv, ov1, false); + return STAmount(v1.name, v1.mCurrency, v1.mIssuer, fv, ov1, false); else - return STAmount(v1.name, v1.mCurrency, -fv, ov1, true); + return STAmount(v1.name, v1.mCurrency, v1.mIssuer, -fv, ov1, true); } STAmount STAmount::divide(const STAmount& num, const STAmount& den, const uint160& uCurrencyID, const uint160& uIssuerID) diff --git a/src/CallRPC.cpp b/src/CallRPC.cpp index 38f697a1e7..50896b858c 100644 --- a/src/CallRPC.cpp +++ b/src/CallRPC.cpp @@ -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; diff --git a/src/FieldNames.cpp b/src/FieldNames.cpp new file mode 100644 index 0000000000..a65ce1bc93 --- /dev/null +++ b/src/FieldNames.cpp @@ -0,0 +1,13 @@ + +#include "FieldNames.h" + +#define FIELD(name, type, index) { sf##name, #name, STI_##type, index }, +#define TYPE(name, type, index) + +FieldName FieldNames[]= +{ +#include "SerializeProto.h" +}; + +#undef FIELD +#undef TYPE diff --git a/src/FieldNames.h b/src/FieldNames.h new file mode 100644 index 0000000000..af760127f6 --- /dev/null +++ b/src/FieldNames.h @@ -0,0 +1,16 @@ +#ifndef __FIELDNAMES__ +#define __FIELDNAMES__ + +#include "SerializedTypes.h" +#include "SerializedObject.h" + +struct FieldName +{ + SOE_Field field; + const char *fieldName; + SerializedTypeID fieldType; + int fieldValue; + int fieldID; +}; + +#endif diff --git a/src/LedgerEntrySet.cpp b/src/LedgerEntrySet.cpp index 5408a4dca8..dbb364ebca 100644 --- a/src/LedgerEntrySet.cpp +++ b/src/LedgerEntrySet.cpp @@ -349,14 +349,17 @@ void LedgerEntrySet::calcRawMeta(Serializer& s) switch (it->second.mAction) { case taaMODIFY: + Log(lsTRACE) << "Modified Node " << it->first; nType = TMNModifiedNode; break; case taaDELETE: + Log(lsTRACE) << "Deleted Node " << it->first; nType = TMNDeletedNode; break; case taaCREATE: + Log(lsTRACE) << "Created Node " << it->first; nType = TMNCreatedNode; break; @@ -938,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; } @@ -968,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; diff --git a/src/LedgerFormats.cpp b/src/LedgerFormats.cpp index ef991c9f62..1db310a4c1 100644 --- a/src/LedgerFormats.cpp +++ b/src/LedgerFormats.cpp @@ -20,7 +20,6 @@ LedgerEntryFormat LedgerFormats[]= { S_FIELD(Domain), STI_VL, SOE_IFFLAG, 32 }, { S_FIELD(PublishHash), STI_HASH256, SOE_IFFLAG, 64 }, { S_FIELD(PublishSize), STI_UINT32, SOE_IFFLAG, 128 }, - { S_FIELD(Extensions), STI_TL, SOE_IFFLAG, 0x01000000 }, { sfInvalid, NULL, STI_DONE, SOE_NEVER, -1 } } }, { "Contract", ltCONTRACT, { @@ -37,7 +36,6 @@ LedgerEntryFormat LedgerFormats[]= { S_FIELD(FundCode), STI_VL, SOE_REQUIRED, 0 }, { S_FIELD(RemoveCode), STI_VL, SOE_REQUIRED, 0 }, { S_FIELD(ExpireCode), STI_VL, SOE_REQUIRED, 0 }, - { S_FIELD(Extensions), STI_TL, SOE_IFFLAG, 0x01000000 }, { sfInvalid, NULL, STI_DONE, SOE_NEVER, -1 } } }, { "DirectoryNode", ltDIR_NODE, { @@ -45,20 +43,17 @@ LedgerEntryFormat LedgerFormats[]= { S_FIELD(Indexes), STI_VECTOR256, SOE_REQUIRED, 0 }, { S_FIELD(IndexNext), STI_UINT64, SOE_IFFLAG, 1 }, { S_FIELD(IndexPrevious), STI_UINT64, SOE_IFFLAG, 2 }, - { S_FIELD(Extensions), STI_TL, SOE_IFFLAG, 0x01000000 }, { sfInvalid, NULL, STI_DONE, SOE_NEVER, -1 } } }, { "GeneratorMap", ltGENERATOR_MAP, { { S_FIELD(Flags), STI_UINT32, SOE_FLAGS, 0 }, { S_FIELD(Generator), STI_VL, SOE_REQUIRED, 0 }, - { S_FIELD(Extensions), STI_TL, SOE_IFFLAG, 0x01000000 }, { sfInvalid, NULL, STI_DONE, SOE_NEVER, -1 } } }, { "Nickname", ltNICKNAME, { { S_FIELD(Flags), STI_UINT32, SOE_FLAGS, 0 }, { S_FIELD(Account), STI_ACCOUNT, SOE_REQUIRED, 0 }, { S_FIELD(MinimumOffer), STI_AMOUNT, SOE_IFFLAG, 1 }, - { S_FIELD(Extensions), STI_TL, SOE_IFFLAG, 0x01000000 }, { sfInvalid, NULL, STI_DONE, SOE_NEVER, -1 } } }, { "Offer", ltOFFER, { @@ -73,7 +68,6 @@ LedgerEntryFormat LedgerFormats[]= { S_FIELD(LastTxnID), STI_HASH256, SOE_REQUIRED, 0 }, { S_FIELD(LastTxnSeq), STI_UINT32, SOE_REQUIRED, 0 }, { S_FIELD(Expiration), STI_UINT32, SOE_IFFLAG, 1 }, - { S_FIELD(Extensions), STI_TL, SOE_IFFLAG, 0x01000000 }, { sfInvalid, NULL, STI_DONE, SOE_NEVER, -1 } } }, { "RippleState", ltRIPPLE_STATE, { @@ -89,7 +83,6 @@ LedgerEntryFormat LedgerFormats[]= { S_FIELD(LowQualityOut), STI_UINT32, SOE_IFFLAG, 2 }, { S_FIELD(HighQualityIn), STI_UINT32, SOE_IFFLAG, 4 }, { S_FIELD(HighQualityOut), STI_UINT32, SOE_IFFLAG, 8 }, - { S_FIELD(Extensions), STI_TL, SOE_IFFLAG, 0x01000000 }, { sfInvalid, NULL, STI_DONE, SOE_NEVER, -1 } } }, { NULL, ltINVALID } diff --git a/src/SerializeProto.h b/src/SerializeProto.h new file mode 100644 index 0000000000..d2336e8971 --- /dev/null +++ b/src/SerializeProto.h @@ -0,0 +1,127 @@ +// This is not really a header file, but it can be used as one with +// appropriate #define statements. + + // types (common) + TYPE("Int32", UINT32, 1) + TYPE("Int64", UINT64, 2) + TYPE("Hash128", HASH128, 3) + TYPE("Hash256", HASH256, 4) + // 5 is reserved + TYPE("Amount", AMOUNT, 6) + TYPE("VariableLength", VL, 7) + TYPE("Account", ACCOUNT, 8) + // 9-13 are reserved + TYPE("Object", OBJECT, 14) + TYPE("Array", ARRAY, 15) + + // types (uncommon) + TYPE("Int8", UINT8, 16) + TYPE("Int16", UINT16, 17) + TYPE("Hash160", HASH160, 18) + TYPE("PathSet", PATHSET, 19) + TYPE("Vector256", VECTOR256, 20) + + + + // 8-bit integers + FIELD(CloseResolution, UINT8, 1) + + // 32-bit integers (common) + FIELD(Flags, UINT32, 1) + FIELD(SourceTag, UINT32, 2) + FIELD(Sequence, UINT32, 3) + FIELD(LastTxnSeq, UINT32, 4) + FIELD(LedgerSequence, UINT32, 5) + FIELD(CloseTime, UINT32, 6) + FIELD(ParentCloseTime, UINT32, 7) + FIELD(SigningTime, UINT32, 8) + FIELD(Expiration, UINT32, 9) + FIELD(TransferRate, UINT32, 10) + FIELD(PublishSize, UINT32, 11) + + // 32-bit integers (uncommon) + FIELD(HighQualityIn, UINT32, 16) + FIELD(HighQualityOut, UINT32, 17) + FIELD(LowQualityIn, UINT32, 18) + FIELD(LowQualityOut, UINT32, 19) + FIELD(QualityIn, UINT32, 20) + FIELD(QualityOut, UINT32, 21) + FIELD(StampEscrow, UINT32, 22) + FIELD(BondAmount, UINT32, 23) + FIELD(LoadFee, UINT32, 24) + FIELD(OfferSequence, UINT32, 25) + + // 64-bit integers + FIELD(IndexNext, UINT64, 1) + FIELD(IndexPrevious, UINT64, 2) + FIELD(BookNode, UINT64, 3) + FIELD(OwnerNode, UINT64, 4) + FIELD(BaseFee, UINT64, 5) + + // 128-bit + FIELD(EmailHash, HASH128, 2) + + // 256-bit (common) + FIELD(LedgerHash, HASH256, 1) + FIELD(ParentHash, HASH256, 2) + FIELD(TransactionHash, HASH256, 3) + FIELD(AccountHash, HASH256, 4) + FIELD(LastTxnID, HASH256, 5) + FIELD(WalletLocator, HASH256, 6) + FIELD(PublishHash, HASH256, 7) + + // 256-bit (uncommon) + FIELD(BookDirectory, HASH256, 16) + FIELD(InvoiceID, HASH256, 17) + FIELD(Nickname, HASH256, 18) + + // currency amount (common) + FIELD(Amount, AMOUNT, 1) + FIELD(Balance, AMOUNT, 2) + FIELD(LimitAmount, AMOUNT, 3) + FIELD(TakerPays, AMOUNT, 4) + FIELD(TakerGets, AMOUNT, 5) + FIELD(LowLimit, AMOUNT, 6) + FIELD(HighLimit, AMOUNT, 7) + FIELD(SendMax, AMOUNT, 9) + + // current amount (uncommon) + FIELD(MinimumOffer, AMOUNT, 16) + FIELD(RippleEscrow, AMOUNT, 17) + + // variable length + FIELD(PublicKey, VL, 1) + FIELD(MessageKey, VL, 2) + FIELD(SigningKey, VL, 3) + FIELD(Signature, VL, 4) + FIELD(Generator, VL, 5) + FIELD(Domain, VL, 6) + FIELD(FundCode, VL, 7) + FIELD(RemoveCode, VL, 8) + FIELD(ExpireCode, VL, 9) + FIELD(CreateCode, VL, 10) + + // account + FIELD(Account, ACCOUNT, 1) + FIELD(Owner, ACCOUNT, 2) + FIELD(Destination, ACCOUNT, 3) + FIELD(Issuer, ACCOUNT, 4) + FIELD(HighID, ACCOUNT, 5) + FIELD(LowID, ACCOUNT, 6) + FIELD(Target, ACCOUNT, 7) + FIELD(AuthorizedKey, ACCOUNT, 8) + + // path set + FIELD(Paths, PATHSET, 1) + + // vector of 256-bit + FIELD(Indexes, VECTOR256, 1) + + // inner object + // OBJECT/1 is reserved for end of object + FIELD(MiddleTransaction, OBJECT, 2) + FIELD(InnerTransaction, OBJECT, 3) + + // array of objects + // ARRAY/1 is reserved for end of array + FIELD(SigningAccounts, ARRAY, 2) diff --git a/src/SerializedObject.h b/src/SerializedObject.h index 08377304c1..3ff352a793 100644 --- a/src/SerializedObject.h +++ b/src/SerializedObject.h @@ -18,97 +18,16 @@ enum SOE_Type SOE_IFNFLAG = 3 // present if flag not set }; -// JED: seems like there would be a better way to do this -// maybe something that inherits from SerializedTransaction enum SOE_Field { sfInvalid = -1, sfGeneric = 0, - // common fields - sfAcceptExpire, - sfAcceptRate, - sfAcceptStart, - sfAccount, - sfAmount, - sfAuthorizedKey, - sfBalance, - sfBondAmount, - sfBookDirectory, - sfBookNode, - sfBorrowExpire, - sfBorrowRate, - sfBorrowStart, - sfBorrower, - sfCreateCode, - sfCloseTime, - sfCurrency, - sfCurrencyIn, - sfCurrencyOut, - sfDestination, - sfDomain, - sfEmailHash, - sfExpiration, - sfExpireCode, - sfExtensions, - sfFirstNode, - sfFlags, - sfFundCode, - sfGenerator, - sfGeneratorID, - sfHash, - sfHighID, - sfHighLimit, - sfHighQualityIn, - sfHighQualityOut, - sfIdentifier, - sfIndexes, - sfIndexNext, - sfIndexPrevious, - sfInvoiceID, - sfIssuer, - sfLastNode, - sfLastTxnID, - sfLastTxnSeq, - sfLedgerHash, - sfLimitAmount, - sfLowID, - sfLowLimit, - sfLowQualityIn, - sfLowQualityOut, - sfMessageKey, - sfMinimumOffer, - sfNextAcceptExpire, - sfNextAcceptRate, - sfNextAcceptStart, - sfNextTransitExpire, - sfNextTransitRate, - sfNextTransitStart, - sfNickname, - sfOfferSequence, - sfOwner, - sfOwnerNode, - sfPaths, - sfPubKey, - sfPublishHash, - sfPublishSize, - sfQualityIn, - sfQualityOut, - sfRemoveCode, - sfRippleEscrow, - sfSendMax, - sfSequence, - sfSignature, - sfSigningKey, - sfSigningTime, - sfSourceTag, - sfStampEscrow, - sfTakerGets, - sfTakerPays, - sfTarget, - sfTransferRate, - sfVersion, - sfWalletLocator, +#define FIELD(name, type, index) sf##name, +#define TYPE(name, type, index) +#include "SerializeProto.h" +#undef FIELD +#undef TYPE // test fields sfTest1, sfTest2, sfTest3, sfTest4 diff --git a/src/SerializedTypes.h b/src/SerializedTypes.h index 3a6417986e..6c9099df50 100644 --- a/src/SerializedTypes.h +++ b/src/SerializedTypes.h @@ -15,25 +15,28 @@ enum SerializedTypeID STI_DONE = -1, STI_NOTPRESENT = 0, - // standard types - STI_OBJECT = 1, - STI_UINT8 = 2, - STI_UINT16 = 3, - STI_UINT32 = 4, - STI_UINT64 = 5, - STI_HASH128 = 6, - STI_HASH160 = 7, - STI_HASH256 = 8, - STI_VL = 9, - STI_TL = 10, - STI_AMOUNT = 11, - STI_PATHSET = 12, - STI_VECTOR256 = 13, + // common types + STI_UINT32 = 1, + STI_UINT64 = 2, + STI_HASH128 = 3, + STI_HASH256 = 4, + STI_TL = 5, + STI_AMOUNT = 6, + STI_VL = 7, + STI_ACCOUNT = 8, + STI_OBJECT = 14, + STI_ARRAY = 15, + + // uncommon types + STI_UINT8 = 16, + STI_UINT16 = 17, + STI_HASH160 = 18, + STI_PATHSET = 19, + STI_VECTOR256 = 20, // high level types - STI_ACCOUNT = 100, - STI_TRANSACTION = 101, - STI_LEDGERENTRY = 102 + STI_TRANSACTION = 100001, + STI_LEDGERENTRY = 100002 }; enum PathFlags @@ -247,9 +250,9 @@ protected: STAmount(const char *name, uint64 value, bool isNegative) : SerializedType(name), mValue(value), mOffset(0), mIsNative(true), mIsNegative(isNegative) { ; } - STAmount(const char *n, const uint160& cur, uint64 val, int off, bool isNative, bool isNegative) - : SerializedType(n), mCurrency(cur), mValue(val), mOffset(off), mIsNative(isNative), mIsNegative(isNegative) - { ; } + STAmount(const char *n, const uint160& cur, const uint160& iss, uint64 val, int off, bool isNat, bool isNeg) + : SerializedType(n), mCurrency(cur), mIssuer(iss), mValue(val), mOffset(off), + mIsNative(isNat), mIsNegative(isNeg) { ; } uint64 toUInt64() const; static uint64 muldiv(uint64, uint64, uint64); @@ -269,8 +272,9 @@ public: { canonicalize(); } // YYY This should probably require issuer too. - STAmount(const char* n, const uint160& currency, uint64 v = 0, int off = 0, bool isNeg = false) : - SerializedType(n), mCurrency(currency), mValue(v), mOffset(off), mIsNegative(isNeg) + STAmount(const char* n, const uint160& currency, const uint160& issuer, + uint64 v = 0, int off = 0, bool isNeg = false) : + SerializedType(n), mCurrency(currency), mIssuer(issuer), mValue(v), mOffset(off), mIsNegative(isNeg) { canonicalize(); } STAmount(const char* n, int64 v); diff --git a/src/Serializer.cpp b/src/Serializer.cpp index 51fd734668..cd7e0d6a8e 100644 --- a/src/Serializer.cpp +++ b/src/Serializer.cpp @@ -154,6 +154,57 @@ uint256 Serializer::get256(int offset) const return ret; } +int Serializer::addFieldID(int type, int name) +{ + int ret = mData.size(); + assert((type > 0) && (type < 256) && (name > 0) && (name < 256)); + if (type < 16) + { + if (name < 16) // common type, common name + mData.push_back(static_cast((type << 4) | name)); + else + { // common type, uncommon name + mData.push_back(static_cast(type << 4)); + mData.push_back(static_cast(name)); + } + } + else if (name < 16) + { // uncommon type, common name + mData.push_back(static_cast(name)); + mData.push_back(static_cast(type)); + } + else + { // uncommon type, uncommon name + mData.push_back(static_cast(0)); + mData.push_back(static_cast(type)); + mData.push_back(static_cast(name)); + } + return ret; +} + +bool Serializer::getFieldID(int& type, int & name, int offset) const +{ + if (!get8(type, offset)) + return false; + name = type & 15; + type >>= 4; + if (type == 0) + { // uncommon type + if (!get8(type, ++offset)) + return false; + if ((type == 0) || (type < 16)) + return false; + } + if (name == 0) + { // uncommon name + if (!get8(name, ++offset)) + return false; + if ((name == 0) || (name < 16)) + return false; + } + return true; +} + int Serializer::add8(unsigned char byte) { int ret = mData.size(); @@ -535,6 +586,17 @@ int SerializerIterator::getBytesLeft() return mSerializer.size() - mPos; } +void SerializerIterator::getFieldID(int& type, int& field) +{ + if (!mSerializer.getFieldID(type, field, mPos)) + throw std::runtime_error("invalid serializer getFieldID"); + ++mPos; + if (type >= 16) + ++mPos; + if (field >= 16) + ++mPos; +} + unsigned char SerializerIterator::get8() { int val; diff --git a/src/Serializer.h b/src/Serializer.h index 5c687e2984..e1c251508c 100644 --- a/src/Serializer.h +++ b/src/Serializer.h @@ -67,6 +67,9 @@ public: bool getTaggedList(std::list&, int offset, int& length) const; bool getTaggedList(std::vector&, int offset, int& length) const; + bool getFieldID(int& type, int& name, int offset) const; + int addFieldID(int type, int name); + // normal hash functions uint160 getRIPEMD160(int size=-1) const; uint256 getSHA256(int size=-1) const; @@ -156,6 +159,8 @@ public: uint160 get160(); uint256 get256(); + void getFieldID(int& type, int& field); + std::vector getRaw(int iLength); std::vector getVL(); diff --git a/src/Transaction.cpp b/src/Transaction.cpp index 00988712d9..526f433bf1 100644 --- a/src/Transaction.cpp +++ b/src/Transaction.cpp @@ -194,7 +194,7 @@ Transaction::pointer Transaction::setClaim( const std::vector& vucSignature) { mTransaction->setITFieldVL(sfGenerator, vucGenerator); - mTransaction->setITFieldVL(sfPubKey, vucPubKey); + mTransaction->setITFieldVL(sfPublicKey, vucPubKey); mTransaction->setITFieldVL(sfSignature, vucSignature); sign(naPrivateKey); @@ -455,7 +455,7 @@ Transaction::pointer Transaction::setPasswordSet( { mTransaction->setITFieldAccount(sfAuthorizedKey, naAuthKeyID); mTransaction->setITFieldVL(sfGenerator, vucGenerator); - mTransaction->setITFieldVL(sfPubKey, vucPubKey); + mTransaction->setITFieldVL(sfPublicKey, vucPubKey); mTransaction->setITFieldVL(sfSignature, vucSignature); sign(naPrivateKey); @@ -542,7 +542,7 @@ Transaction::pointer Transaction::setWalletAdd( { mTransaction->setITFieldAmount(sfAmount, saAmount); mTransaction->setITFieldAccount(sfAuthorizedKey, naAuthKeyID); - mTransaction->setITFieldVL(sfPubKey, naNewPubKey.getAccountPublic()); + mTransaction->setITFieldVL(sfPublicKey, naNewPubKey.getAccountPublic()); mTransaction->setITFieldVL(sfSignature, vucSignature); sign(naPrivateKey); diff --git a/src/TransactionAction.cpp b/src/TransactionAction.cpp index ae308351c1..8f37469169 100644 --- a/src/TransactionAction.cpp +++ b/src/TransactionAction.cpp @@ -30,7 +30,7 @@ TER TransactionEngine::setAuthorized(const SerializedTransaction& txn, bool bMus // std::vector vucCipher = txn.getITFieldVL(sfGenerator); - std::vector vucPubKey = txn.getITFieldVL(sfPubKey); + std::vector vucPubKey = txn.getITFieldVL(sfPublicKey); std::vector vucSignature = txn.getITFieldVL(sfSignature); NewcoinAddress naAccountPublic = NewcoinAddress::createAccountPublic(vucPubKey); @@ -620,7 +620,7 @@ TER TransactionEngine::doWalletAdd(const SerializedTransaction& txn) { std::cerr << "WalletAdd>" << std::endl; - const std::vector vucPubKey = txn.getITFieldVL(sfPubKey); + const std::vector vucPubKey = txn.getITFieldVL(sfPublicKey); const std::vector vucSignature = txn.getITFieldVL(sfSignature); const uint160 uAuthKeyID = txn.getITFieldAccount(sfAuthorizedKey); const NewcoinAddress naMasterPubKey = NewcoinAddress::createAccountPublic(vucPubKey); @@ -710,8 +710,8 @@ TER TransactionEngine::takeOffers( boost::unordered_set usOfferUnfundedBecame; // Offers that became unfunded. boost::unordered_set 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; } diff --git a/src/TransactionFormats.cpp b/src/TransactionFormats.cpp index de18524c7c..3ab00f7dea 100644 --- a/src/TransactionFormats.cpp +++ b/src/TransactionFormats.cpp @@ -15,16 +15,14 @@ TransactionFormat InnerTxnFormats[]= { S_FIELD(TransferRate), STI_UINT32, SOE_IFFLAG, 32 }, { S_FIELD(PublishHash), STI_HASH256, SOE_IFFLAG, 64 }, { S_FIELD(PublishSize), STI_UINT32, SOE_IFFLAG, 128 }, - { S_FIELD(Extensions), STI_TL, SOE_IFFLAG, 0x02000000 }, { sfInvalid, NULL, STI_DONE, SOE_NEVER, -1 } } }, { "Claim", ttCLAIM, { { S_FIELD(Flags), STI_UINT32, SOE_FLAGS, 0 }, { S_FIELD(Generator), STI_VL, SOE_REQUIRED, 0 }, - { S_FIELD(PubKey), STI_VL, SOE_REQUIRED, 0 }, + { S_FIELD(PublicKey), STI_VL, SOE_REQUIRED, 0 }, { S_FIELD(Signature), STI_VL, SOE_REQUIRED, 0 }, { S_FIELD(SourceTag), STI_UINT32, SOE_IFFLAG, 1 }, - { S_FIELD(Extensions), STI_TL, SOE_IFFLAG, 0x02000000 }, { sfInvalid, NULL, STI_DONE, SOE_NEVER, -1 } } }, { "CreditSet", ttCREDIT_SET, { @@ -34,7 +32,6 @@ TransactionFormat InnerTxnFormats[]= { S_FIELD(LimitAmount), STI_AMOUNT, SOE_IFFLAG, 2 }, { S_FIELD(QualityIn), STI_UINT32, SOE_IFFLAG, 4 }, { S_FIELD(QualityOut), STI_UINT32, SOE_IFFLAG, 8 }, - { S_FIELD(Extensions), STI_TL, SOE_IFFLAG, 0x02000000 }, { sfInvalid, NULL, STI_DONE, SOE_NEVER, -1 } } }, /* @@ -45,7 +42,6 @@ TransactionFormat InnerTxnFormats[]= { S_FIELD(SourceTag), STI_UINT32, SOE_IFFLAG, 1 }, { S_FIELD(Destination), STI_ACCOUNT, SOE_IFFLAG, 2 }, { S_FIELD(Identifier), STI_VL, SOE_IFFLAG, 4 }, - { S_FIELD(Extensions), STI_TL, SOE_IFFLAG, 0x02000000 }, { sfInvalid, NULL, STI_DONE, SOE_NEVER, -1 } } }, */ @@ -55,7 +51,6 @@ TransactionFormat InnerTxnFormats[]= { S_FIELD(MinimumOffer), STI_AMOUNT, SOE_IFFLAG, 1 }, { S_FIELD(Signature), STI_VL, SOE_IFFLAG, 2 }, { S_FIELD(SourceTag), STI_UINT32, SOE_IFFLAG, 4 }, - { S_FIELD(Extensions), STI_TL, SOE_IFFLAG, 0x02000000 }, { sfInvalid, NULL, STI_DONE, SOE_NEVER, -1 } } }, { "OfferCreate", ttOFFER_CREATE, { @@ -64,31 +59,27 @@ TransactionFormat InnerTxnFormats[]= { S_FIELD(TakerGets), STI_AMOUNT, SOE_REQUIRED, 0 }, { S_FIELD(SourceTag), STI_UINT32, SOE_IFFLAG, 1 }, { S_FIELD(Expiration), STI_UINT32, SOE_IFFLAG, 2 }, - { S_FIELD(Extensions), STI_TL, SOE_IFFLAG, 0x02000000 }, { sfInvalid, NULL, STI_DONE, SOE_NEVER, -1 } } }, { "OfferCancel", ttOFFER_CANCEL, { { S_FIELD(Flags), STI_UINT32, SOE_FLAGS, 0 }, { S_FIELD(OfferSequence), STI_UINT32, SOE_REQUIRED, 0 }, { S_FIELD(SourceTag), STI_UINT32, SOE_IFFLAG, 1 }, - { S_FIELD(Extensions), STI_TL, SOE_IFFLAG, 0x02000000 }, { sfInvalid, NULL, STI_DONE, SOE_NEVER, -1 } } }, { "PasswordFund", ttPASSWORD_FUND, { { S_FIELD(Flags), STI_UINT32, SOE_FLAGS, 0 }, { S_FIELD(Destination), STI_ACCOUNT, SOE_REQUIRED, 0 }, { S_FIELD(SourceTag), STI_UINT32, SOE_IFFLAG, 1 }, - { S_FIELD(Extensions), STI_TL, SOE_IFFLAG, 0x02000000 }, { sfInvalid, NULL, STI_DONE, SOE_NEVER, -1 } } }, { "PasswordSet", ttPASSWORD_SET, { { S_FIELD(Flags), STI_UINT32, SOE_FLAGS, 0 }, { S_FIELD(AuthorizedKey), STI_ACCOUNT, SOE_REQUIRED, 0 }, { S_FIELD(Generator), STI_VL, SOE_REQUIRED, 0 }, - { S_FIELD(PubKey), STI_VL, SOE_REQUIRED, 0 }, + { S_FIELD(PublicKey), STI_VL, SOE_REQUIRED, 0 }, { S_FIELD(Signature), STI_VL, SOE_REQUIRED, 0 }, { S_FIELD(SourceTag), STI_UINT32, SOE_IFFLAG, 1 }, - { S_FIELD(Extensions), STI_TL, SOE_IFFLAG, 0x02000000 }, { sfInvalid, NULL, STI_DONE, SOE_NEVER, -1 } } }, { "Payment", ttPAYMENT, { @@ -99,17 +90,15 @@ TransactionFormat InnerTxnFormats[]= { S_FIELD(Paths), STI_PATHSET, SOE_IFFLAG, 2 }, { S_FIELD(SourceTag), STI_UINT32, SOE_IFFLAG, 4 }, { S_FIELD(InvoiceID), STI_HASH256, SOE_IFFLAG, 8 }, - { S_FIELD(Extensions), STI_TL, SOE_IFFLAG, 0x02000000 }, { sfInvalid, NULL, STI_DONE, SOE_NEVER, -1 } } }, { "WalletAdd", ttWALLET_ADD, { { S_FIELD(Flags), STI_UINT32, SOE_FLAGS, 0 }, { S_FIELD(Amount), STI_AMOUNT, SOE_REQUIRED, 0 }, { S_FIELD(AuthorizedKey), STI_ACCOUNT, SOE_REQUIRED, 0 }, - { S_FIELD(PubKey), STI_VL, SOE_REQUIRED, 0 }, + { S_FIELD(PublicKey), STI_VL, SOE_REQUIRED, 0 }, { S_FIELD(Signature), STI_VL, SOE_REQUIRED, 0 }, { S_FIELD(SourceTag), STI_UINT32, SOE_IFFLAG, 1 }, - { S_FIELD(Extensions), STI_TL, SOE_IFFLAG, 0x02000000 }, { sfInvalid, NULL, STI_DONE, SOE_NEVER, -1 } } }, { "Contract", ttCONTRACT, { @@ -122,13 +111,11 @@ TransactionFormat InnerTxnFormats[]= { S_FIELD(FundCode), STI_VL, SOE_REQUIRED, 0 }, { S_FIELD(RemoveCode), STI_VL, SOE_REQUIRED, 0 }, { S_FIELD(ExpireCode), STI_VL, SOE_REQUIRED, 0 }, - { S_FIELD(Extensions), STI_TL, SOE_IFFLAG, 0x02000000 }, { sfInvalid, NULL, STI_DONE, SOE_NEVER, -1 } } }, { "Contract", ttCONTRACT_REMOVE, { { S_FIELD(Flags), STI_UINT32, SOE_FLAGS, 0 }, { S_FIELD(Target), STI_ACCOUNT, SOE_REQUIRED, 0 }, - { S_FIELD(Extensions), STI_TL, SOE_IFFLAG, 0x02000000 }, { sfInvalid, NULL, STI_DONE, SOE_NEVER, -1 } } }, { NULL, ttINVALID } diff --git a/test/server.js b/test/server.js index 0be028cc59..0bd0f582c3 100644 --- a/test/server.js +++ b/test/server.js @@ -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 diff --git a/test/standalone-test.js b/test/standalone-test.js index a3a7a561a2..d707baeca3 100644 --- a/test/standalone-test.js +++ b/test/standalone-test.js @@ -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