From c7d88f50783c57084c6860b150d0e4cdc5b9030f Mon Sep 17 00:00:00 2001 From: jed Date: Mon, 12 Nov 2012 07:48:04 -0800 Subject: [PATCH 1/7] in progress --- src/cpp/ripple/RPCHandler.cpp | 4 +- src/cpp/ripple/Transaction.cpp | 432 -------------------------- src/cpp/ripple/Transaction.h | 200 ------------ src/cpp/ripple/TransactionAction.cpp | 96 ++---- src/cpp/ripple/TransactionEngine.cpp | 22 +- src/cpp/ripple/TransactionEngine.h | 7 +- src/cpp/ripple/TransactionFormats.cpp | 38 +-- src/cpp/ripple/TransactionFormats.h | 10 +- 8 files changed, 46 insertions(+), 763 deletions(-) diff --git a/src/cpp/ripple/RPCHandler.cpp b/src/cpp/ripple/RPCHandler.cpp index 5d8ca3ef4..3102eb408 100644 --- a/src/cpp/ripple/RPCHandler.cpp +++ b/src/cpp/ripple/RPCHandler.cpp @@ -541,6 +541,7 @@ Json::Value RPCHandler::doPeers(const Json::Value& params) // Prior to running allow each to have a credit line of what they will be getting from the other account. Json::Value RPCHandler::doProfile(const Json::Value ¶ms) { + /* need to fix now that sharedOfferCreate is gone int iArgs = params.size(); RippleAddress naSeedA; RippleAddress naAccountA; @@ -620,7 +621,8 @@ Json::Value RPCHandler::doProfile(const Json::Value ¶ms) obj["end"] = boost::posix_time::to_simple_string(ptEnd); obj["interval"] = boost::posix_time::to_simple_string(tdInterval); obj["rate_per_second"] = fRate; - + */ + Json::Value obj(Json::objectValue); return obj; } diff --git a/src/cpp/ripple/Transaction.cpp b/src/cpp/ripple/Transaction.cpp index ab8714392..2d15b0d50 100644 --- a/src/cpp/ripple/Transaction.cpp +++ b/src/cpp/ripple/Transaction.cpp @@ -106,441 +106,9 @@ bool Transaction::sign(const RippleAddress& naAccountPrivate) return bResult; } -// -// AccountSet -// -Transaction::pointer Transaction::setAccountSet( - const RippleAddress& naPrivateKey, - bool bEmailHash, - const uint128& uEmailHash, - bool bWalletLocator, - const uint256& uWalletLocator, - const uint32 uWalletSize, - const RippleAddress& naMessagePublic, - bool bDomain, - const std::vector& vucDomain, - bool bTransferRate, - const uint32 uTransferRate) -{ - if (!bEmailHash) - mTransaction->setFieldH128(sfEmailHash, uEmailHash); - if (!bWalletLocator) - { - mTransaction->setFieldH256(sfWalletLocator, uWalletLocator); - mTransaction->setFieldU32(sfWalletSize, uWalletSize); - } - if (naMessagePublic.isValid()) - mTransaction->setFieldVL(sfMessageKey, naMessagePublic.getAccountPublic()); - - if (bDomain) - mTransaction->setFieldVL(sfDomain, vucDomain); - - if (bTransferRate) - mTransaction->setFieldU32(sfTransferRate, uTransferRate); - - sign(naPrivateKey); - - return shared_from_this(); -} - -Transaction::pointer Transaction::sharedAccountSet( - const RippleAddress& naPublicKey, const RippleAddress& naPrivateKey, - const RippleAddress& naSourceAccount, - uint32 uSeq, - const STAmount& saFee, - uint32 uSourceTag, - bool bEmailHash, - const uint128& uEmailHash, - bool bWalletLocator, - const uint256& uWalletLocator, - const uint32 uWalletSize, - const RippleAddress& naMessagePublic, - bool bDomain, - const std::vector& vucDomain, - bool bTransferRate, - const uint32 uTransferRate) -{ - pointer tResult = boost::make_shared(ttACCOUNT_SET, naPublicKey, naSourceAccount, uSeq, saFee, uSourceTag); - - return tResult->setAccountSet(naPrivateKey, bEmailHash, uEmailHash, - bWalletLocator, uWalletLocator, uWalletSize, - naMessagePublic, - bDomain, vucDomain, bTransferRate, uTransferRate); -} - -// -// Claim -// - -Transaction::pointer Transaction::setClaim( - const RippleAddress& naPrivateKey, - const std::vector& vucGenerator, - const std::vector& vucPubKey, - const std::vector& vucSignature) -{ - mTransaction->setFieldVL(sfGenerator, vucGenerator); - mTransaction->setFieldVL(sfPublicKey, vucPubKey); - mTransaction->setFieldVL(sfSignature, vucSignature); - - sign(naPrivateKey); - - return shared_from_this(); -} - -Transaction::pointer Transaction::sharedClaim( - const RippleAddress& naPublicKey, const RippleAddress& naPrivateKey, - uint32 uSourceTag, - const std::vector& vucGenerator, - const std::vector& vucPubKey, - const std::vector& vucSignature) -{ - pointer tResult = boost::make_shared(ttCLAIM, - naPublicKey, naPublicKey, - 0, // Sequence of 0. - 0, // Free. - uSourceTag); - - return tResult->setClaim(naPrivateKey, vucGenerator, vucPubKey, vucSignature); -} - -// -// Create -// - -Transaction::pointer Transaction::setCreate( - const RippleAddress& naPrivateKey, - const RippleAddress& naCreateAccountID, - const STAmount& saFund) -{ - mTransaction->setFieldU32(sfFlags, tfCreateAccount); - mTransaction->setFieldAccount(sfDestination, naCreateAccountID); - mTransaction->setFieldAmount(sfAmount, saFund); - - sign(naPrivateKey); - - return shared_from_this(); -} - -Transaction::pointer Transaction::sharedCreate( - const RippleAddress& naPublicKey, const RippleAddress& naPrivateKey, - const RippleAddress& naSourceAccount, - uint32 uSeq, - const STAmount& saFee, - uint32 uSourceTag, - const RippleAddress& naCreateAccountID, - const STAmount& saFund) -{ - pointer tResult = boost::make_shared(ttPAYMENT, naPublicKey, naSourceAccount, uSeq, saFee, uSourceTag); - - return tResult->setCreate(naPrivateKey, naCreateAccountID, saFund); -} - -// -// CreditSet -// - -Transaction::pointer Transaction::setCreditSet( - const RippleAddress& naPrivateKey, - const STAmount& saLimitAmount, - bool bQualityIn, - uint32 uQualityIn, - bool bQualityOut, - uint32 uQualityOut) -{ - mTransaction->setFieldAmount(sfLimitAmount, saLimitAmount); - - if (bQualityIn) - mTransaction->setFieldU32(sfQualityIn, uQualityIn); - - if (bQualityOut) - mTransaction->setFieldU32(sfQualityOut, uQualityOut); - - sign(naPrivateKey); - - return shared_from_this(); -} - -Transaction::pointer Transaction::sharedCreditSet( - const RippleAddress& naPublicKey, const RippleAddress& naPrivateKey, - const RippleAddress& naSourceAccount, - uint32 uSeq, - const STAmount& saFee, - uint32 uSourceTag, - const STAmount& saLimitAmount, - bool bQualityIn, - uint32 uQualityIn, - bool bQualityOut, - uint32 uQualityOut) -{ - pointer tResult = boost::make_shared(ttCREDIT_SET, naPublicKey, naSourceAccount, uSeq, saFee, uSourceTag); - - return tResult->setCreditSet(naPrivateKey, - saLimitAmount, - bQualityIn, uQualityIn, - bQualityOut, uQualityOut); -} - -// -// NicknameSet -// - -Transaction::pointer Transaction::setNicknameSet( - const RippleAddress& naPrivateKey, - const uint256& uNickname, - bool bSetOffer, - const STAmount& saMinimumOffer) -{ - mTransaction->setFieldH256(sfNickname, uNickname); - - // XXX Make sure field is present even for 0! - if (bSetOffer) - mTransaction->setFieldAmount(sfMinimumOffer, saMinimumOffer); - - sign(naPrivateKey); - - return shared_from_this(); -} - -// --> bSetOffer: true, change offer -// --> saMinimumOffer: 0 to remove. -Transaction::pointer Transaction::sharedNicknameSet( - const RippleAddress& naPublicKey, const RippleAddress& naPrivateKey, - const RippleAddress& naSourceAccount, - uint32 uSeq, - const STAmount& saFee, - uint32 uSourceTag, - const uint256& uNickname, - bool bSetOffer, - const STAmount& saMinimumOffer) -{ - pointer tResult = boost::make_shared(ttNICKNAME_SET, naPublicKey, naSourceAccount, uSeq, saFee, uSourceTag); - - return tResult->setNicknameSet(naPrivateKey, uNickname, bSetOffer, saMinimumOffer); -} - -// -// OfferCreate -// - -Transaction::pointer Transaction::setOfferCreate( - const RippleAddress& naPrivateKey, - bool bPassive, - const STAmount& saTakerPays, - const STAmount& saTakerGets, - uint32 uExpiration) -{ - if (bPassive) - mTransaction->setFieldU32(sfFlags, tfPassive); - - mTransaction->setFieldAmount(sfTakerPays, saTakerPays); - mTransaction->setFieldAmount(sfTakerGets, saTakerGets); - - if (uExpiration) - mTransaction->setFieldU32(sfExpiration, uExpiration); - - sign(naPrivateKey); - - return shared_from_this(); -} - -Transaction::pointer Transaction::sharedOfferCreate( - const RippleAddress& naPublicKey, const RippleAddress& naPrivateKey, - const RippleAddress& naSourceAccount, - uint32 uSeq, - const STAmount& saFee, - uint32 uSourceTag, - bool bPassive, - const STAmount& saTakerPays, - const STAmount& saTakerGets, - uint32 uExpiration) -{ - pointer tResult = boost::make_shared(ttOFFER_CREATE, naPublicKey, naSourceAccount, uSeq, saFee, uSourceTag); - - return tResult->setOfferCreate(naPrivateKey, bPassive, saTakerPays, saTakerGets, uExpiration); -} - -// -// OfferCancel -// - -Transaction::pointer Transaction::setOfferCancel( - const RippleAddress& naPrivateKey, - uint32 uSequence) -{ - mTransaction->setFieldU32(sfOfferSequence, uSequence); - - sign(naPrivateKey); - - return shared_from_this(); -} - -Transaction::pointer Transaction::sharedOfferCancel( - const RippleAddress& naPublicKey, const RippleAddress& naPrivateKey, - const RippleAddress& naSourceAccount, - uint32 uSeq, - const STAmount& saFee, - uint32 uSourceTag, - uint32 uSequence) -{ - pointer tResult = boost::make_shared(ttOFFER_CANCEL, naPublicKey, naSourceAccount, uSeq, saFee, uSourceTag); - - return tResult->setOfferCancel(naPrivateKey, uSequence); -} - -// -// PasswordFund -// - -Transaction::pointer Transaction::setPasswordFund( - const RippleAddress& naPrivateKey, - const RippleAddress& naDstAccountID) -{ - mTransaction->setFieldAccount(sfDestination, naDstAccountID); - - sign(naPrivateKey); - - return shared_from_this(); -} - -Transaction::pointer Transaction::sharedPasswordFund( - const RippleAddress& naPublicKey, const RippleAddress& naPrivateKey, - const RippleAddress& naSourceAccount, - uint32 uSeq, - const STAmount& saFee, - uint32 uSourceTag, - const RippleAddress& naDstAccountID) -{ - pointer tResult = boost::make_shared(ttPASSWORD_FUND, naPublicKey, naSourceAccount, uSeq, saFee, uSourceTag); - - return tResult->setPasswordFund(naPrivateKey, naDstAccountID); -} - -// -// PasswordSet -// - -Transaction::pointer Transaction::setPasswordSet( - const RippleAddress& naPrivateKey, - const RippleAddress& naAuthKeyID, - const std::vector& vucGenerator, - const std::vector& vucPubKey, - const std::vector& vucSignature) -{ - mTransaction->setFieldAccount(sfAuthorizedKey, naAuthKeyID); - mTransaction->setFieldVL(sfGenerator, vucGenerator); - mTransaction->setFieldVL(sfPublicKey, vucPubKey); - mTransaction->setFieldVL(sfSignature, vucSignature); - - sign(naPrivateKey); - - return shared_from_this(); -} - -Transaction::pointer Transaction::sharedPasswordSet( - const RippleAddress& naPublicKey, const RippleAddress& naPrivateKey, - uint32 uSourceTag, - const RippleAddress& naAuthKeyID, - const std::vector& vucGenerator, - const std::vector& vucPubKey, - const std::vector& vucSignature) -{ - pointer tResult = boost::make_shared(ttPASSWORD_SET, - naPublicKey, naPublicKey, - 0, // Sequence of 0. - 0, // Free. - uSourceTag); - - return tResult->setPasswordSet(naPrivateKey, naAuthKeyID, vucGenerator, vucPubKey, vucSignature); -} - -// -// Payment -// - -Transaction::pointer Transaction::setPayment( - const RippleAddress& naPrivateKey, - const RippleAddress& naDstAccountID, - const STAmount& saAmount, - const STAmount& saSendMax, - const STPathSet& spsPaths, - const bool bPartial, - const bool bLimit) -{ - mTransaction->setFieldAccount(sfDestination, naDstAccountID); - mTransaction->setFieldAmount(sfAmount, saAmount); - - if (saAmount != saSendMax || saAmount.getCurrency() != saSendMax.getCurrency()) - { - mTransaction->setFieldAmount(sfSendMax, saSendMax); - } - - if (spsPaths.getPathCount()) - { - mTransaction->setFieldPathSet(sfPaths, spsPaths); - } - - sign(naPrivateKey); - - return shared_from_this(); -} - -Transaction::pointer Transaction::sharedPayment( - const RippleAddress& naPublicKey, const RippleAddress& naPrivateKey, - const RippleAddress& naSourceAccount, - uint32 uSeq, - const STAmount& saFee, - uint32 uSourceTag, - const RippleAddress& naDstAccountID, - const STAmount& saAmount, - const STAmount& saSendMax, - const STPathSet& spsPaths, - const bool bPartial, - const bool bLimit) -{ - pointer tResult = boost::make_shared(ttPAYMENT, naPublicKey, naSourceAccount, uSeq, saFee, uSourceTag); - - return tResult->setPayment(naPrivateKey, naDstAccountID, saAmount, saSendMax, spsPaths, bPartial, bLimit); -} - -// -// WalletAdd -// - -Transaction::pointer Transaction::setWalletAdd( - const RippleAddress& naPrivateKey, - const STAmount& saAmount, - const RippleAddress& naAuthKeyID, - const RippleAddress& naNewPubKey, - const std::vector& vucSignature) -{ - mTransaction->setFieldAmount(sfAmount, saAmount); - mTransaction->setFieldAccount(sfAuthorizedKey, naAuthKeyID); - mTransaction->setFieldVL(sfPublicKey, naNewPubKey.getAccountPublic()); - mTransaction->setFieldVL(sfSignature, vucSignature); - - sign(naPrivateKey); - - return shared_from_this(); -} - -Transaction::pointer Transaction::sharedWalletAdd( - const RippleAddress& naPublicKey, const RippleAddress& naPrivateKey, - const RippleAddress& naSourceAccount, - uint32 uSeq, - const STAmount& saFee, - uint32 uSourceTag, - const STAmount& saAmount, - const RippleAddress& naAuthKeyID, - const RippleAddress& naNewPubKey, - const std::vector& vucSignature) -{ - pointer tResult = boost::make_shared(ttWALLET_ADD, naPublicKey, naSourceAccount, uSeq, saFee, uSourceTag); - - return tResult->setWalletAdd(naPrivateKey, saAmount, naAuthKeyID, naNewPubKey, vucSignature); -} // // Misc. diff --git a/src/cpp/ripple/Transaction.h b/src/cpp/ripple/Transaction.h index 4f37bfab5..0f208d2b5 100644 --- a/src/cpp/ripple/Transaction.h +++ b/src/cpp/ripple/Transaction.h @@ -58,82 +58,6 @@ private: SerializedTransaction::pointer mTransaction; - Transaction::pointer setAccountSet( - const RippleAddress& naPrivateKey, - bool bEmailHash, - const uint128& uEmailHash, - bool bWalletLocator, - const uint256& uWalletLocator, - const uint32 uWalletSize, - const RippleAddress& naMessagePublic, - bool bDomain, - const std::vector& vucDomain, - bool bTransferRate, - const uint32 uTransferRate); - - Transaction::pointer setClaim( - const RippleAddress& naPrivateKey, - const std::vector& vucGenerator, - const std::vector& vucPubKey, - const std::vector& vucSignature); - - Transaction::pointer setCreate( - const RippleAddress& naPrivateKey, - const RippleAddress& naCreateAccountID, - const STAmount& saFund); - - Transaction::pointer setCreditSet( - const RippleAddress& naPrivateKey, - const STAmount& saLimitAmount, - bool bQualityIn, - uint32 uQualityIn, - bool bQualityOut, - uint32 uQualityOut); - - Transaction::pointer setNicknameSet( - const RippleAddress& naPrivateKey, - const uint256& uNickname, - bool bSetOffer, - const STAmount& saMinimumOffer); - - Transaction::pointer setOfferCreate( - const RippleAddress& naPrivateKey, - bool bPassive, - const STAmount& saTakerPays, - const STAmount& saTakerGets, - uint32 uExpiration); - - Transaction::pointer setOfferCancel( - const RippleAddress& naPrivateKey, - uint32 uSequence); - - Transaction::pointer setPasswordFund( - const RippleAddress& naPrivateKey, - const RippleAddress& naDstAccountID); - - Transaction::pointer setPasswordSet( - const RippleAddress& naPrivateKey, - const RippleAddress& naAuthKeyID, - const std::vector& vucGenerator, - const std::vector& vucPubKey, - const std::vector& vucSignature); - - Transaction::pointer setPayment( - const RippleAddress& naPrivateKey, - const RippleAddress& naDstAccountID, - const STAmount& saAmount, - const STAmount& saSendMax, - const STPathSet& spsPaths, - const bool bPartial, - const bool bLimit); - - Transaction::pointer setWalletAdd( - const RippleAddress& naPrivateKey, - const STAmount& saAmount, - const RippleAddress& naAuthKeyID, - const RippleAddress& naNewPubKey, - const std::vector& vucSignature); - public: Transaction(SerializedTransaction::ref st, bool bValidate); @@ -148,130 +72,6 @@ public: const STAmount& saFee, // Transaction fee. uint32 uSourceTag); // User call back value. - // Change account settings. - static Transaction::pointer sharedAccountSet( - const RippleAddress& naPublicKey, const RippleAddress& naPrivateKey, - const RippleAddress& naSourceAccount, - uint32 uSeq, - const STAmount& saFee, - uint32 uSourceTag, - bool bEmailHash, - const uint128& uEmailHash, - bool bWalletLocator, - const uint256& uWalletLocator, - const uint32 uWalletSize, - const RippleAddress& naMessagePublic, - bool bDomain, - const std::vector& vucDomain, - bool bTransferRate, - const uint32 uTransferRate); - - // Claim a wallet. - static Transaction::pointer sharedClaim( - const RippleAddress& naPublicKey, const RippleAddress& naPrivateKey, - uint32 uSourceTag, - const std::vector& vucGenerator, - const std::vector& vucPubKey, - const std::vector& vucSignature); - - // Create an account. - static Transaction::pointer sharedCreate( - const RippleAddress& naPublicKey, const RippleAddress& naPrivateKey, - const RippleAddress& naSourceAccount, - uint32 uSeq, - const STAmount& saFee, - uint32 uSourceTag, - const RippleAddress& naCreateAccountID, // Account to create. - const STAmount& saFund); // Initial funds in XNC. - - // Set credit limit and borrow fees. - static Transaction::pointer sharedCreditSet( - const RippleAddress& naPublicKey, const RippleAddress& naPrivateKey, - const RippleAddress& naSourceAccount, - uint32 uSeq, - const STAmount& saFee, - uint32 uSourceTag, - const STAmount& saLimitAmount, - bool bQualityIn, - uint32 uQualityIn, - bool bQualityOut, - uint32 uQualityOut); - - // Set Nickname - static Transaction::pointer sharedNicknameSet( - const RippleAddress& naPublicKey, const RippleAddress& naPrivateKey, - const RippleAddress& naSourceAccount, - uint32 uSeq, - const STAmount& saFee, - uint32 uSourceTag, - const uint256& uNickname, - bool bSetOffer, - const STAmount& saMinimumOffer); - - // Pre-fund password change. - static Transaction::pointer sharedPasswordFund( - const RippleAddress& naPublicKey, const RippleAddress& naPrivateKey, - const RippleAddress& naSourceAccount, - uint32 uSeq, - const STAmount& saFee, - uint32 uSourceTag, - const RippleAddress& naDstAccountID); - - // Change a password. - static Transaction::pointer sharedPasswordSet( - const RippleAddress& naPublicKey, const RippleAddress& naPrivateKey, - uint32 uSourceTag, - const RippleAddress& naAuthKeyID, // ID of regular public to auth. - const std::vector& vucGenerator, - const std::vector& vucPubKey, - const std::vector& vucSignature); - - // Make a payment. - static Transaction::pointer sharedPayment( - const RippleAddress& naPublicKey, const RippleAddress& naPrivateKey, - const RippleAddress& naSourceAccount, - uint32 uSeq, - const STAmount& saFee, - uint32 uSourceTag, - const RippleAddress& naDstAccountID, - const STAmount& saAmount, - const STAmount& saSendMax, - const STPathSet& spsPaths, - const bool bPartial = false, - const bool bLimit = false); - - // Place an offer. - static Transaction::pointer sharedOfferCreate( - const RippleAddress& naPublicKey, const RippleAddress& naPrivateKey, - const RippleAddress& naSourceAccount, - uint32 uSeq, - const STAmount& saFee, - uint32 uSourceTag, - bool bPassive, - const STAmount& saTakerPays, - const STAmount& saTakerGets, - uint32 uExpiration); - - // Cancel an offer - static Transaction::pointer sharedOfferCancel( - const RippleAddress& naPublicKey, const RippleAddress& naPrivateKey, - const RippleAddress& naSourceAccount, - uint32 uSeq, - const STAmount& saFee, - uint32 uSourceTag, - uint32 uSequence); - - // Add an account to a wallet. - static Transaction::pointer sharedWalletAdd( - const RippleAddress& naPublicKey, const RippleAddress& naPrivateKey, - const RippleAddress& naSourceAccount, - uint32 uSeq, - const STAmount& saFee, - uint32 uSourceTag, - const STAmount& saAmount, // Initial funds in XNC. - const RippleAddress& naAuthKeyID, // ID of regular public to auth. - const RippleAddress& naNewPubKey, // Public key of new account - const std::vector& vucSignature); // Proof know new account's private key. bool sign(const RippleAddress& naAccountPrivate); bool checkSign() const; diff --git a/src/cpp/ripple/TransactionAction.cpp b/src/cpp/ripple/TransactionAction.cpp index f7d26eeee..591cfc6b0 100644 --- a/src/cpp/ripple/TransactionAction.cpp +++ b/src/cpp/ripple/TransactionAction.cpp @@ -28,7 +28,7 @@ TER TransactionEngine::setAuthorized(const SerializedTransaction& txn, bool bMus // Verify that submitter knows the private key for the generator. // Otherwise, people could deny access to generators. // - + /* JED: taking out generator stuff until we have a better idea of how people will use this std::vector vucCipher = txn.getFieldVL(sfGenerator); std::vector vucPubKey = txn.getFieldVL(sfPublicKey); std::vector vucSignature = txn.getFieldVL(sfSignature); @@ -42,6 +42,7 @@ TER TransactionEngine::setAuthorized(const SerializedTransaction& txn, bool bMus return tefBAD_GEN_AUTH; } + // Create generator. uint160 hGeneratorID = naAccountPublic.getAccountID(); @@ -69,6 +70,8 @@ TER TransactionEngine::setAuthorized(const SerializedTransaction& txn, bool bMus ? hGeneratorID // Claim : txn.getFieldAccount160(sfAuthorizedKey); // PasswordSet + */ + uint160 uAuthKeyID=txn.getFieldAccount160(sfAuthorizedKey); mTxnAccount->setFieldAccount(sfAuthorizedKey, uAuthKeyID); return tesSUCCESS; @@ -196,17 +199,18 @@ TER TransactionEngine::doClaim(const SerializedTransaction& txn) { Log(lsINFO) << "doClaim>"; - TER terResult = setAuthorized(txn, true); + //TER terResult = setAuthorized(txn, true); + TER terResult=tefEXCEPTION; Log(lsINFO) << "doClaim<"; return terResult; } -TER TransactionEngine::doCreditSet(const SerializedTransaction& txn) +TER TransactionEngine::doTrustSet(const SerializedTransaction& txn) { TER terResult = tesSUCCESS; - Log(lsINFO) << "doCreditSet>"; + Log(lsINFO) << "doTrustSet>"; const STAmount saLimitAmount = txn.getFieldAmount(sfLimitAmount); const bool bQualityIn = txn.isFieldPresent(sfQualityIn); @@ -222,19 +226,19 @@ TER TransactionEngine::doCreditSet(const SerializedTransaction& txn) if (saLimitAmount.isNegative()) { - Log(lsINFO) << "doCreditSet: Malformed transaction: Negatived credit limit."; + Log(lsINFO) << "doTrustSet: Malformed transaction: Negatived credit limit."; return temBAD_AMOUNT; } else if (!uDstAccountID) { - Log(lsINFO) << "doCreditSet: Malformed transaction: Destination account not specifed."; + Log(lsINFO) << "doTrustSet: Malformed transaction: Destination account not specified."; return temDST_NEEDED; } else if (mTxnAccountID == uDstAccountID) { - Log(lsINFO) << "doCreditSet: Malformed transaction: Can not extend credit to self."; + Log(lsINFO) << "doTrustSet: Malformed transaction: Can not extend credit to self."; return temDST_IS_SRC; } @@ -242,7 +246,7 @@ TER TransactionEngine::doCreditSet(const SerializedTransaction& txn) SLE::pointer sleDst = entryCache(ltACCOUNT_ROOT, Ledger::getAccountRootIndex(uDstAccountID)); if (!sleDst) { - Log(lsINFO) << "doCreditSet: Delay transaction: Destination account does not exist."; + Log(lsINFO) << "doTrustSet: Delay transaction: Destination account does not exist."; return terNO_DST; } @@ -311,12 +315,12 @@ TER TransactionEngine::doCreditSet(const SerializedTransaction& txn) entryModify(sleRippleState); } - Log(lsINFO) << "doCreditSet: Modifying ripple line: bDelIndex=" << bDelIndex; + Log(lsINFO) << "doTrustSet: Modifying ripple line: bDelIndex=" << bDelIndex; } // Line does not exist. else if (!saLimitAmount) { - Log(lsINFO) << "doCreditSet: Redundant: Setting non-existant ripple line to 0."; + Log(lsINFO) << "doTrustSet: Redundant: Setting non-existent ripple line to 0."; return terNO_LINE_NO_ZERO; } @@ -325,7 +329,7 @@ TER TransactionEngine::doCreditSet(const SerializedTransaction& txn) // Create a new ripple line. sleRippleState = entryCreate(ltRIPPLE_STATE, Ledger::getRippleStateIndex(mTxnAccountID, uDstAccountID, uCurrencyID)); - Log(lsINFO) << "doCreditSet: Creating ripple line: " << sleRippleState->getIndex().ToString(); + Log(lsINFO) << "doTrustSet: Creating ripple line: " << sleRippleState->getIndex().ToString(); sleRippleState->setFieldAmount(sfBalance, STAmount(uCurrencyID, ACCOUNT_ONE)); // Zero balance in currency. sleRippleState->setFieldAmount(bFlipped ? sfHighLimit : sfLowLimit, saLimitAllow); @@ -344,57 +348,13 @@ TER TransactionEngine::doCreditSet(const SerializedTransaction& txn) terResult = mNodes.dirAdd(uSrcRef, Ledger::getOwnerDirIndex(uDstAccountID), sleRippleState->getIndex()); } - Log(lsINFO) << "doCreditSet<"; + Log(lsINFO) << "doTrustSet<"; return terResult; } -TER TransactionEngine::doNicknameSet(const SerializedTransaction& txn) -{ - std::cerr << "doNicknameSet>" << std::endl; - - const uint256 uNickname = txn.getFieldH256(sfNickname); - const bool bMinOffer = txn.isFieldPresent(sfMinimumOffer); - const STAmount saMinOffer = bMinOffer ? txn.getFieldAmount(sfAmount) : STAmount(); - - SLE::pointer sleNickname = entryCache(ltNICKNAME, uNickname); - - if (sleNickname) - { - // Edit old entry. - sleNickname->setFieldAccount(sfAccount, mTxnAccountID); - - if (bMinOffer && saMinOffer) - { - sleNickname->setFieldAmount(sfMinimumOffer, saMinOffer); - } - else - { - sleNickname->makeFieldAbsent(sfMinimumOffer); - } - - entryModify(sleNickname); - } - else - { - // Make a new entry. - // XXX Need to include authorization limiting for first year. - - sleNickname = entryCreate(ltNICKNAME, Ledger::getNicknameIndex(uNickname)); - - std::cerr << "doNicknameSet: Creating nickname node: " << sleNickname->getIndex().ToString() << std::endl; - - sleNickname->setFieldAccount(sfAccount, mTxnAccountID); - - if (bMinOffer && saMinOffer) - sleNickname->setFieldAmount(sfMinimumOffer, saMinOffer); - } - - std::cerr << "doNicknameSet<" << std::endl; - - return tesSUCCESS; -} +/* TER TransactionEngine::doPasswordFund(const SerializedTransaction& txn) { std::cerr << "doPasswordFund>" << std::endl; @@ -428,14 +388,16 @@ TER TransactionEngine::doPasswordFund(const SerializedTransaction& txn) return tesSUCCESS; } +*/ -TER TransactionEngine::doPasswordSet(const SerializedTransaction& txn) +// TODO: change to take a fee if there is one there +TER TransactionEngine::doRegularKeySet(const SerializedTransaction& txn) { - std::cerr << "doPasswordSet>" << std::endl; + std::cerr << "doRegularKeySet>" << std::endl; if (mTxnAccount->getFlags() & lsfPasswordSpent) { - std::cerr << "doPasswordSet: Delay transaction: Funds already spent." << std::endl; + std::cerr << "doRegularKeySet: Delay transaction: Funds already spent." << std::endl; return terFUNDS_SPENT; } @@ -444,7 +406,7 @@ TER TransactionEngine::doPasswordSet(const SerializedTransaction& txn) TER terResult = setAuthorized(txn, false); - std::cerr << "doPasswordSet<" << std::endl; + std::cerr << "doRegularKeySet<" << std::endl; return terResult; } @@ -477,7 +439,7 @@ TER TransactionEngine::doPayment(const SerializedTransaction& txn, const Transac if (!uDstAccountID) { - Log(lsINFO) << "doPayment: Invalid transaction: Payment destination account not specifed."; + Log(lsINFO) << "doPayment: Invalid transaction: Payment destination account not specified."; return temDST_NEEDED; } @@ -495,7 +457,7 @@ TER TransactionEngine::doPayment(const SerializedTransaction& txn, const Transac } else if (mTxnAccountID == uDstAccountID && uSrcCurrency == uDstCurrency && !bPaths) { - Log(lsINFO) << boost::str(boost::format("doPayment: Invalid transaction: Redunant transaction: src=%s, dst=%s, src_cur=%s, dst_cur=%s") + Log(lsINFO) << boost::str(boost::format("doPayment: Invalid transaction: Redundant transaction: src=%s, dst=%s, src_cur=%s, dst_cur=%s") % mTxnAccountID.ToString() % uDstAccountID.ToString() % uSrcCurrency.ToString() @@ -577,7 +539,7 @@ TER TransactionEngine::doPayment(const SerializedTransaction& txn, const Transac if (saSrcXRPBalance < saDstAmount) { // Transaction might succeed, if applied in a different order. - Log(lsINFO) << "doPayment: Delay transaction: Insufficent funds."; + Log(lsINFO) << "doPayment: Delay transaction: Insufficient funds."; terResult = terUNFUNDED; } @@ -638,7 +600,7 @@ TER TransactionEngine::doWalletAdd(const SerializedTransaction& txn) if (saSrcBalance < saAmount) { std::cerr - << boost::str(boost::format("WalletAdd: Delay transaction: insufficent balance: balance=%s amount=%s") + << boost::str(boost::format("WalletAdd: Delay transaction: insufficient balance: balance=%s amount=%s") % saSrcBalance.getText() % saAmount.getText()) << std::endl; @@ -662,10 +624,6 @@ TER TransactionEngine::doWalletAdd(const SerializedTransaction& txn) return tesSUCCESS; } -TER TransactionEngine::doInvoice(const SerializedTransaction& txn) -{ - return temUNKNOWN; -} // Take as much as possible. Adjusts account balances. Charges fees on top to taker. // --> uBookBase: The order book to take against. diff --git a/src/cpp/ripple/TransactionEngine.cpp b/src/cpp/ripple/TransactionEngine.cpp index 3c6789ce5..cb2ffe72c 100644 --- a/src/cpp/ripple/TransactionEngine.cpp +++ b/src/cpp/ripple/TransactionEngine.cpp @@ -127,7 +127,7 @@ TER TransactionEngine::applyTransaction(const SerializedTransaction& txn, Transa switch (txn.getTxnType()) { case ttCLAIM: - case ttPASSWORD_SET: + case ttREGULAR_KEY_SET: saCost = 0; break; @@ -148,7 +148,7 @@ TER TransactionEngine::applyTransaction(const SerializedTransaction& txn, Transa break; case ttACCOUNT_SET: - case ttCREDIT_SET: + case ttTRUST_SET: case ttOFFER_CREATE: case ttOFFER_CANCEL: case ttPASSWORD_FUND: @@ -268,7 +268,7 @@ TER TransactionEngine::applyTransaction(const SerializedTransaction& txn, Transa } break; - case ttPASSWORD_SET: + case ttREGULAR_KEY_SET: // Transaction's signing public key must be for the source account. // To prove the master private key made this transaction. if (naSigningPubKey.getAccountID() != mTxnAccountID) @@ -388,8 +388,8 @@ TER TransactionEngine::applyTransaction(const SerializedTransaction& txn, Transa terResult = doClaim(txn); break; - case ttCREDIT_SET: - terResult = doCreditSet(txn); + case ttTRUST_SET: + terResult = doTrustSet(txn); break; case ttINVALID: @@ -409,16 +409,8 @@ TER TransactionEngine::applyTransaction(const SerializedTransaction& txn, Transa terResult = doOfferCancel(txn); break; - case ttNICKNAME_SET: - terResult = doNicknameSet(txn); - break; - - case ttPASSWORD_FUND: - terResult = doPasswordFund(txn); - break; - - case ttPASSWORD_SET: - terResult = doPasswordSet(txn); + case ttREGULAR_KEY_SET: + terResult = doRegularKeySet(txn); break; case ttPAYMENT: diff --git a/src/cpp/ripple/TransactionEngine.h b/src/cpp/ripple/TransactionEngine.h index e16755d30..13e5729f2 100644 --- a/src/cpp/ripple/TransactionEngine.h +++ b/src/cpp/ripple/TransactionEngine.h @@ -64,13 +64,10 @@ protected: TER doAccountSet(const SerializedTransaction& txn); TER doClaim(const SerializedTransaction& txn); - TER doCreditSet(const SerializedTransaction& txn); - TER doInvoice(const SerializedTransaction& txn); + TER doTrustSet(const SerializedTransaction& txn); TER doOfferCreate(const SerializedTransaction& txn); TER doOfferCancel(const SerializedTransaction& txn); - TER doNicknameSet(const SerializedTransaction& txn); - TER doPasswordFund(const SerializedTransaction& txn); - TER doPasswordSet(const SerializedTransaction& txn); + TER doRegularKeySet(const SerializedTransaction& txn); TER doPayment(const SerializedTransaction& txn, const TransactionEngineParams params); TER doWalletAdd(const SerializedTransaction& txn); TER doContractAdd(const SerializedTransaction& txn); diff --git a/src/cpp/ripple/TransactionFormats.cpp b/src/cpp/ripple/TransactionFormats.cpp index a6ad09e42..7c5a2157a 100644 --- a/src/cpp/ripple/TransactionFormats.cpp +++ b/src/cpp/ripple/TransactionFormats.cpp @@ -28,34 +28,12 @@ static bool TFInit() << SOElement(sfTransferRate, SOE_OPTIONAL) ; - DECLARE_TF(Claim, ttCLAIM) - << SOElement(sfGenerator, SOE_REQUIRED) - << SOElement(sfPublicKey, SOE_REQUIRED) - << SOElement(sfSignature, SOE_REQUIRED) - ; - - DECLARE_TF(CreditSet, ttCREDIT_SET) + DECLARE_TF(TrustSet, ttTRUST_SET) << SOElement(sfLimitAmount, SOE_OPTIONAL) << SOElement(sfQualityIn, SOE_OPTIONAL) << SOElement(sfQualityOut, SOE_OPTIONAL) ; - - /* - DECLARE_TF(Invoice, ttINVOICE) - << SOElement(sfTarget, SOE_REQUIRED) - << SOElement(sfAmount, SOE_REQUIRED) - << SOElement(sfDestination, SOE_OPTIONAL) - << SOElement(sfIdentifier, SOE_OPTIONAL) - ; - ) - */ - - DECLARE_TF(NicknameSet, ttNICKNAME_SET) - << SOElement(sfNickname, SOE_REQUIRED) - << SOElement(sfMinimumOffer, SOE_OPTIONAL) - ; - DECLARE_TF(OfferCreate, ttOFFER_CREATE) << SOElement(sfTakerPays, SOE_REQUIRED) << SOElement(sfTakerGets, SOE_REQUIRED) @@ -66,14 +44,8 @@ static bool TFInit() << SOElement(sfOfferSequence, SOE_REQUIRED) ; - DECLARE_TF(PasswordFund, ttPASSWORD_FUND) - << SOElement(sfDestination, SOE_REQUIRED) - ; - - DECLARE_TF(PasswordSet, ttPASSWORD_SET) + DECLARE_TF(SetRegularKey, ttREGULAR_KEY_SET) << SOElement(sfAuthorizedKey, SOE_REQUIRED) - << SOElement(sfGenerator, SOE_REQUIRED) - << SOElement(sfPublicKey, SOE_REQUIRED) ; DECLARE_TF(Payment, ttPAYMENT) @@ -84,12 +56,6 @@ static bool TFInit() << SOElement(sfInvoiceID, SOE_OPTIONAL) ; - DECLARE_TF(WalletAdd, ttWALLET_ADD) - << SOElement(sfAmount, SOE_REQUIRED) - << SOElement(sfAuthorizedKey, SOE_REQUIRED) - << SOElement(sfPublicKey, SOE_REQUIRED) - ; - DECLARE_TF(Contract, ttCONTRACT) << SOElement(sfExpiration, SOE_REQUIRED) << SOElement(sfBondAmount, SOE_REQUIRED) diff --git a/src/cpp/ripple/TransactionFormats.h b/src/cpp/ripple/TransactionFormats.h index 857a43189..0235320e4 100644 --- a/src/cpp/ripple/TransactionFormats.h +++ b/src/cpp/ripple/TransactionFormats.h @@ -7,18 +7,18 @@ enum TransactionType { ttINVALID = -1, ttPAYMENT = 0, - ttCLAIM = 1, + ttCLAIM = 1, // open ttWALLET_ADD = 2, ttACCOUNT_SET = 3, - ttPASSWORD_FUND = 4, - ttPASSWORD_SET = 5, - ttNICKNAME_SET = 6, + ttPASSWORD_FUND = 4, // open + ttREGULAR_KEY_SET = 5, + ttNICKNAME_SET = 6, // open ttOFFER_CREATE = 7, ttOFFER_CANCEL = 8, ttCONTRACT = 9, ttCONTRACT_REMOVE = 10, // can we use the same msg as offer cancel - ttCREDIT_SET = 20, + ttTRUST_SET = 20, }; class TransactionFormat From 433cbc4d929e91ce6510b3863448a096660dc47d Mon Sep 17 00:00:00 2001 From: jed Date: Mon, 12 Nov 2012 15:45:50 -0800 Subject: [PATCH 2/7] vs 2012 --- newcoin.vcxproj | 30 +++++++++-------- newcoin.vcxproj.filters | 54 +++++++++++++++++------------- src/cpp/ripple/LedgerConsensus.cpp | 6 ++-- src/cpp/ripple/RPC.h | 5 +++ src/cpp/ripple/TaggedCache.h | 23 +++++++------ 5 files changed, 66 insertions(+), 52 deletions(-) diff --git a/newcoin.vcxproj b/newcoin.vcxproj index 56aa43c99..32ee35c9e 100644 --- a/newcoin.vcxproj +++ b/newcoin.vcxproj @@ -20,12 +20,14 @@ Application true MultiByte + v110 Application false true MultiByte + v110 @@ -49,13 +51,13 @@ Level3 Disabled BOOST_TEST_ALTERNATIVE_INIT_API;BOOST_TEST_NO_MAIN;_CRT_SECURE_NO_WARNINGS;_WIN32_WINNT=0x0501;WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) - .\src;..\OpenSSL\include;..\boost_1_47_0;..\protobuf-2.4.1\src\ + .\;..\OpenSSL\include;..\boost_1_52_0;..\protobuf\src ProgramDatabase Console true - ..\OpenSSL\lib\VC;..\boost_1_47_0\stage\lib;..\protobuf-2.4.1\vsprojects\Debug + ..\OpenSSL\lib\VC;..\boost_1_52_0\stage\lib;..\protobuf\vsprojects\Debug ssleay32MDd.lib;libeay32MTd.lib;libprotobuf.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies) @@ -137,9 +139,7 @@ - - .\;..\OpenSSL\include;..\boost_1_47_0;..\protobuf-2.4.1\src\ - + @@ -175,14 +175,16 @@ - - - - - - - - + + + + + + + + + + @@ -287,7 +289,7 @@ Document - /code/protoc-2.4.1-win32/protoc -I=..\newcoin --cpp_out=\code\newcoin\ ..\newcoin/src/cpp/ripple/ripple.proto + /code/protobuf/protoc -I=..\newcoin --cpp_out=\code\newcoin\ ..\newcoin/src/cpp/ripple/ripple.proto \code\newcoin\src\ripple.pb.h diff --git a/newcoin.vcxproj.filters b/newcoin.vcxproj.filters index 33693e035..0380497b0 100644 --- a/newcoin.vcxproj.filters +++ b/newcoin.vcxproj.filters @@ -33,30 +33,6 @@ - - Source Files\websocketpp - - - Source Files\websocketpp - - - Source Files\websocketpp - - - Source Files\websocketpp - - - Source Files\websocketpp - - - Source Files\websocketpp - - - Source Files\websocketpp - - - Source Files\websocketpp - Source Files\database @@ -318,6 +294,36 @@ Source Files\database + + Source Files\websocketpp + + + Source Files\websocketpp + + + Source Files\websocketpp + + + Source Files\websocketpp + + + Source Files\websocketpp + + + Source Files\websocketpp + + + Source Files\websocketpp + + + Source Files\websocketpp + + + Source Files\websocketpp + + + Source Files\websocketpp + diff --git a/src/cpp/ripple/LedgerConsensus.cpp b/src/cpp/ripple/LedgerConsensus.cpp index 9f9e09945..e5a877b3b 100644 --- a/src/cpp/ripple/LedgerConsensus.cpp +++ b/src/cpp/ripple/LedgerConsensus.cpp @@ -181,9 +181,9 @@ bool TransactionAcquire::takeNodes(const std::list& nodeIDs, } void LCTransaction::setVote(const uint160& peer, bool votesYes) -{ // Tracke a peer's yes/no vote on a particular disputed transaction - std::pair::iterator, bool> res = - mVotes.insert(std::make_pair(peer, votesYes)); +{ // Track a peer's yes/no vote on a particular disputed transaction + std::pair::iterator, bool> res = + mVotes.insert(std::pair(peer, votesYes)); if (res.second) { // new vote diff --git a/src/cpp/ripple/RPC.h b/src/cpp/ripple/RPC.h index f4f75f79f..7d0406a20 100644 --- a/src/cpp/ripple/RPC.h +++ b/src/cpp/ripple/RPC.h @@ -1,3 +1,6 @@ +#ifndef __RPC_h__ +#define __RPC_h__ + #include #include @@ -37,3 +40,5 @@ extern std::string HTTPReply(int nStatus, const std::string& strMsg); extern std::string JSONRPCReply(const Json::Value& result, const Json::Value& error, const Json::Value& id); extern Json::Value JSONRPCError(int code, const std::string& message); + +#endif diff --git a/src/cpp/ripple/TaggedCache.h b/src/cpp/ripple/TaggedCache.h index aee675042..2cdfc708c 100644 --- a/src/cpp/ripple/TaggedCache.h +++ b/src/cpp/ripple/TaggedCache.h @@ -29,9 +29,10 @@ public: typedef c_Key key_type; typedef c_Data data_type; - typedef boost::weak_ptr weak_data_ptr; - typedef boost::shared_ptr data_ptr; - typedef std::pair cache_entry; + typedef boost::weak_ptr weak_data_ptr; + typedef boost::shared_ptr data_ptr; + typedef std::pair cache_entry; + typedef std::pair cache_pair; protected: mutable boost::recursive_mutex mLock; @@ -130,7 +131,7 @@ template void TaggedCache::sweep if (TaggedCachePartition.doLog(lsTRACE) && (mapRemovals || cacheRemovals)) Log(lsTRACE, TaggedCachePartition) << mName << ": cache = " << mCache.size() << "-" << cacheRemovals << - ", map = " << mMap.size() << "-" << mapRemovals; + ", map = " << mMap.size() << "-" << mapRemovals; } template bool TaggedCache::touch(const key_type& key) @@ -156,7 +157,7 @@ template bool TaggedCache::touch } // In map but not cache, put in cache - mCache.insert(std::make_pair(key, std::make_pair(time(NULL), weak_data_ptr(cit->second.second)))); + mCache.insert(cache_pair(key, cache_entry(time(NULL), weak_data_ptr(cit->second.second)))); return true; } @@ -180,7 +181,7 @@ bool TaggedCache::canonicalize(const key_type& key, boost::shared typename boost::unordered_map::iterator mit = mMap.find(key); if (mit == mMap.end()) { // not in map - mCache.insert(std::make_pair(key, std::make_pair(time(NULL), data))); + mCache.insert(cache_pair(key, cache_entry(time(NULL), data))); mMap.insert(std::make_pair(key, data)); return false; } @@ -189,7 +190,7 @@ bool TaggedCache::canonicalize(const key_type& key, boost::shared if (!cachedData) { // in map, but expired. Update in map, insert in cache mit->second = data; - mCache.insert(std::make_pair(key, std::make_pair(time(NULL), data))); + mCache.insert(cache_pair(key, cache_entry(time(NULL), data))); return true; } @@ -208,7 +209,7 @@ bool TaggedCache::canonicalize(const key_type& key, boost::shared cit->second.second = data; } else // no, add to cache - mCache.insert(std::make_pair(key, std::make_pair(time(NULL), data))); + mCache.insert(cache_pair(key, cache_entry(time(NULL), data))); return true; } @@ -229,13 +230,13 @@ boost::shared_ptr TaggedCache::fetch(const key_type& key) mMap.erase(mit); return cachedData; } - + // Valid in map, is it in the cache? typename boost::unordered_map::iterator cit = mCache.find(key); if (cit != mCache.end()) cit->second.first = time(NULL); // Yes, refresh else // No, add to cache - mCache.insert(std::make_pair(key, std::make_pair(time(NULL), cachedData))); + mCache.insert(cache_pair(key, cache_entry(time(NULL), cachedData))); return cachedData; } @@ -257,4 +258,4 @@ bool TaggedCache::retrieve(const key_type& key, c_Data& data) return true; } -#endif +#endif \ No newline at end of file From a6e709e5ffbc1ab680dd5eb74206de749d829c01 Mon Sep 17 00:00:00 2001 From: JoelKatz Date: Mon, 12 Nov 2012 15:55:38 -0800 Subject: [PATCH 3/7] Some fixes for broken math in this code. --- src/cpp/ripple/uint256.h | 37 +++++++++++++++++++++++-------------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/src/cpp/ripple/uint256.h b/src/cpp/ripple/uint256.h index 3e2ab10e6..e7e5e449e 100644 --- a/src/cpp/ripple/uint256.h +++ b/src/cpp/ripple/uint256.h @@ -23,7 +23,7 @@ #endif // These classes all store their values internally -// in little-endian form +// in big-endian form inline int Testuint256AdHoc(std::vector vArg); @@ -36,7 +36,7 @@ protected: enum { WIDTH=BITS/32 }; // This is really big-endian in byte order. - // We use unsigned int for speed. + // We sometimes use unsigned int for speed. unsigned int pn[WIDTH]; public: @@ -73,7 +73,7 @@ public: zero(); // Put in least significant bits. - ((uint64_t *) end())[-1] = htobe64(uHost); + ((uint64_t *) end())[-1] = htobe64(uHost); return *this; } @@ -105,10 +105,12 @@ public: base_uint& operator++() { // prefix operator - int i = WIDTH; - - while (i-- && !++pn[i]) - ; + for (int i = WIDTH - 1; i >= 0; --i) + { + pn[i] = htobe32(be32toh(pn[i]) + 1); + if (pn[i] != 0) + break; + } return *this; } @@ -124,10 +126,13 @@ public: base_uint& operator--() { - int i = WIDTH; - - while (i-- && !pn[i]--) - ; + for (int i = WIDTH - 1; i >= 0; --i) + { + uint32 prev = pn[i]; + pn[i] = htobe32(be32toh(pn[i]) - 1); + if (prev != 0) + break; + } return *this; } @@ -169,10 +174,14 @@ public: const unsigned char* pAEnd = a.end(); const unsigned char* pB = b.begin(); - while (pA != pAEnd && *pA == *pB) - pA++, pB++; + while (*pA == *pB) + { + if (++pA == pAEnd) + return 0; + ++pB; + } - return pA == pAEnd ? 0 : *pA < *pB ? -1 : *pA > *pB ? 1 : 0; + return (*pA < *pB) ? -1 : 1; } friend inline bool operator<(const base_uint& a, const base_uint& b) From 35905edd773a132f63ff29a2d6b1468c9c53ded0 Mon Sep 17 00:00:00 2001 From: JoelKatz Date: Mon, 12 Nov 2012 15:55:58 -0800 Subject: [PATCH 4/7] Proof of work finding engine. --- src/cpp/ripple/ProofOfWork.cpp | 79 ++++++++++++++++++++++++++-------- src/cpp/ripple/ProofOfWork.h | 12 ++++++ 2 files changed, 73 insertions(+), 18 deletions(-) diff --git a/src/cpp/ripple/ProofOfWork.cpp b/src/cpp/ripple/ProofOfWork.cpp index 9ccb29eb2..7d1df2656 100644 --- a/src/cpp/ripple/ProofOfWork.cpp +++ b/src/cpp/ripple/ProofOfWork.cpp @@ -2,9 +2,14 @@ #include +#include + #include #include "Serializer.h" +#include "Log.h" + +SETUP_LOG(); const uint256 ProofOfWork::sMinTarget("00000000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"); const int ProofOfWork::sMaxIterations(1 << 23); @@ -16,26 +21,38 @@ bool ProofOfWork::isValid() const uint64 ProofOfWork::getDifficulty(const uint256& target, int iterations) { // calculate the approximate number of hashes required to solve this proof of work - if ((iterations > sMaxIterations) || (target < sMinTarget)); + if ((iterations > sMaxIterations) || (target < sMinTarget)) + { + cLog(lsINFO) << "Iterations:" << iterations; + cLog(lsINFO) << "MaxIterat: " << sMaxIterations; + cLog(lsINFO) << "Target: " << target; + cLog(lsINFO) << "MinTarget: " << sMinTarget; throw std::runtime_error("invalid proof of work target/iteration"); + } // more iterations means more hashes per iteration but also a larger final hash uint64 difficulty = iterations * (iterations / 4 + 1); - // Multiply the number of hashes needed by 16 for each leading zero in the hex difficulty - const unsigned char *ptr = target.begin(); + // Multiply the number of hashes needed by 256 for each leading zero byte in the hex difficulty + const unsigned char *ptr = target.end() - 1; while (*ptr == 0) { - difficulty *= 16; - ptr++; + cLog(lsINFO) << "getDif: " << (int) *ptr; + difficulty *= 256; + --ptr; } // If the first digit after a zero isn't an F, multiply - difficulty *= (16 - *ptr); + difficulty *= (256 - *ptr); return difficulty; } +static uint256 getSHA512Half(const std::vector& vec) +{ + return Serializer::getSHA512Half(vec.front().begin(), vec.size() * (256 / 8)); +} + uint256 ProofOfWork::solve(int maxIterations) const { if (!isValid()) @@ -44,23 +61,49 @@ uint256 ProofOfWork::solve(int maxIterations) const uint256 nonce; RAND_bytes(nonce.begin(), nonce.size()); - Serializer s1, s2; - std::vector buf; - buf.reserve((256 / 8) * mIterations); + std::vector buf2; + buf2.resize(mIterations); - while (maxIterations > 8) + std::vector buf1; + buf1.resize(3); + buf1[0] = mChallenge; + + while (maxIterations > 0) { - s1.add256(mChallenge); - s1.add256(nonce); -// uint256 base = s1.getSHA512Half(); - - for (int i = 0; i < mIterations; ++i) + buf1[1] = nonce; + buf1[2] = uint256(); + for (int i = (mIterations - 1); i >= 0; --i) { - // WRITEME + if (buf1.size() != 3) + Log(lsINFO) << "buf1.size=" << buf1.size(); + buf1[2] = getSHA512Half(buf1); + buf2[i] = buf1[2]; } + if (buf2.size() != mIterations) + Log(lsINFO) << "buf2.size=" << buf2.size(); - s1.erase(); - nonce++; + uint256 hash = getSHA512Half(buf2); + if (hash <= mTarget) + return nonce; + + ++nonce; + --maxIterations; } return uint256(); } + +BOOST_AUTO_TEST_SUITE(ProofOfWork_suite) + +BOOST_AUTO_TEST_CASE( ProofOfWork_test ) +{ + ProofOfWork pow("test", 32, uint256(), + uint256("00FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF")); + cLog(lsINFO) << "Estimated difficulty: " << pow.getDifficulty(); + uint256 solution = pow.solve(16777216); + if (solution.isZero()) + BOOST_FAIL("Unable to solve proof of work"); +} + +BOOST_AUTO_TEST_SUITE_END() + +// vim:ts=4 diff --git a/src/cpp/ripple/ProofOfWork.h b/src/cpp/ripple/ProofOfWork.h index 1391be91c..6540b0713 100644 --- a/src/cpp/ripple/ProofOfWork.h +++ b/src/cpp/ripple/ProofOfWork.h @@ -10,6 +10,15 @@ #include "uint256.h" +enum POWResult +{ + powOK = 0, + powREUSED = 1, + powBADNONCE = 2, + powBADTOKEN = 3, + powEXPIRED = 4, +}; + class ProofOfWork { protected: @@ -48,6 +57,7 @@ protected: int mIterations; uint256 mTarget; time_t mLastDifficultyChange; + int mValidTime; powMap_t mSolvedChallenges; boost::mutex mLock; @@ -64,3 +74,5 @@ public: }; #endif + +// vim:ts=4 From 82ef1d45820069f618d6d1b287b384c7053596ae Mon Sep 17 00:00:00 2001 From: JoelKatz Date: Mon, 12 Nov 2012 22:51:48 -0800 Subject: [PATCH 5/7] More proof of work stuff. --- src/cpp/ripple/ProofOfWork.cpp | 44 ++++++++++++++++++++++------------ 1 file changed, 29 insertions(+), 15 deletions(-) diff --git a/src/cpp/ripple/ProofOfWork.cpp b/src/cpp/ripple/ProofOfWork.cpp index 7d1df2656..2fad06d48 100644 --- a/src/cpp/ripple/ProofOfWork.cpp +++ b/src/cpp/ripple/ProofOfWork.cpp @@ -31,19 +31,16 @@ uint64 ProofOfWork::getDifficulty(const uint256& target, int iterations) } // more iterations means more hashes per iteration but also a larger final hash - uint64 difficulty = iterations * (iterations / 4 + 1); + uint64 difficulty = iterations + (iterations / 4); - // Multiply the number of hashes needed by 256 for each leading zero byte in the hex difficulty - const unsigned char *ptr = target.end() - 1; + // Multiply the number of hashes needed by 256 for each leading zero byte in the difficulty + const unsigned char *ptr = target.begin(); while (*ptr == 0) { - cLog(lsINFO) << "getDif: " << (int) *ptr; difficulty *= 256; - --ptr; + ++ptr; } - - // If the first digit after a zero isn't an F, multiply - difficulty *= (256 - *ptr); + difficulty = (difficulty * 256) / (*ptr + 1); return difficulty; } @@ -74,16 +71,11 @@ uint256 ProofOfWork::solve(int maxIterations) const buf1[2] = uint256(); for (int i = (mIterations - 1); i >= 0; --i) { - if (buf1.size() != 3) - Log(lsINFO) << "buf1.size=" << buf1.size(); buf1[2] = getSHA512Half(buf1); buf2[i] = buf1[2]; } - if (buf2.size() != mIterations) - Log(lsINFO) << "buf2.size=" << buf2.size(); - uint256 hash = getSHA512Half(buf2); - if (hash <= mTarget) + if (getSHA512Half(buf2) <= mTarget) return nonce; ++nonce; @@ -92,16 +84,38 @@ uint256 ProofOfWork::solve(int maxIterations) const return uint256(); } +bool ProofOfWork::checkSolution(const uint256& solution) const +{ + if (mIterations > sMaxIterations) + return false; + + std::vector buf1; + buf1.push_back(mChallenge); + buf1.push_back(solution); + buf1.push_back(uint256()); + + std::vector buf2; + buf2.resize(mIterations); + for (int i = (mIterations - 1); i >= 0; --i) + { + buf1[2] = getSHA512Half(buf1); + buf2[i] = buf1[2]; + } + return getSHA512Half(buf2) <= mTarget; +} + BOOST_AUTO_TEST_SUITE(ProofOfWork_suite) BOOST_AUTO_TEST_CASE( ProofOfWork_test ) { ProofOfWork pow("test", 32, uint256(), - uint256("00FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF")); + uint256("000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF")); cLog(lsINFO) << "Estimated difficulty: " << pow.getDifficulty(); uint256 solution = pow.solve(16777216); if (solution.isZero()) BOOST_FAIL("Unable to solve proof of work"); + if (!pow.checkSolution(solution)) + BOOST_FAIL("Solution did not check"); } BOOST_AUTO_TEST_SUITE_END() From d339eb8cadbaa4708ab96a8d62ff9d9b21f6c28c Mon Sep 17 00:00:00 2001 From: JoelKatz Date: Mon, 12 Nov 2012 22:58:29 -0800 Subject: [PATCH 6/7] Fix unit tests. --- src/cpp/ripple/SerializedTransaction.cpp | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/cpp/ripple/SerializedTransaction.cpp b/src/cpp/ripple/SerializedTransaction.cpp index bdc26d77e..835339cac 100644 --- a/src/cpp/ripple/SerializedTransaction.cpp +++ b/src/cpp/ripple/SerializedTransaction.cpp @@ -8,13 +8,17 @@ #include "Log.h" #include "HashPrefixes.h" +SETUP_LOG(); DECLARE_INSTANCE(SerializedTransaction); SerializedTransaction::SerializedTransaction(TransactionType type) : STObject(sfTransaction), mType(type) { mFormat = TransactionFormat::getTxnFormat(type); if (mFormat == NULL) + { + cLog(lsWARNING) << "Transaction type: " << type; throw std::runtime_error("invalid transaction type"); + } set(mFormat->elements); setFieldU16(sfTransactionType, mFormat->t_type); } @@ -24,7 +28,10 @@ SerializedTransaction::SerializedTransaction(const STObject& object) : STObject( mType = static_cast(getFieldU16(sfTransactionType)); mFormat = TransactionFormat::getTxnFormat(mType); if (!mFormat) + { + cLog(lsWARNING) << "Transaction type: " << mType; throw std::runtime_error("invalid transaction type"); + } if (!setType(mFormat->elements)) { throw std::runtime_error("transaction not valid"); @@ -45,7 +52,10 @@ SerializedTransaction::SerializedTransaction(SerializerIterator& sit) : STObject mFormat = TransactionFormat::getTxnFormat(mType); if (!mFormat) + { + cLog(lsWARNING) << "Transaction type: " << mType; throw std::runtime_error("invalid transaction type"); + } if (!setType(mFormat->elements)) { assert(false); @@ -206,10 +216,10 @@ BOOST_AUTO_TEST_CASE( STrans_test ) RippleAddress publicAcct = RippleAddress::createAccountPublic(generator, 1); RippleAddress privateAcct = RippleAddress::createAccountPrivate(generator, seed, 1); - SerializedTransaction j(ttCLAIM); + SerializedTransaction j(ttACCOUNT_SET); j.setSourceAccount(publicAcct); j.setSigningPubKey(publicAcct); - j.setFieldVL(sfPublicKey, publicAcct.getAccountPublic()); + j.setFieldVL(sfMessageKey, publicAcct.getAccountPublic()); j.sign(privateAcct); if (!j.checkSign()) BOOST_FAIL("Transaction fails signature test"); From 210170a2cb91874dd7b8038d46d70e17cda12b25 Mon Sep 17 00:00:00 2001 From: JoelKatz Date: Tue, 13 Nov 2012 02:16:22 -0800 Subject: [PATCH 7/7] More work on proof of work. --- src/cpp/ripple/ProofOfWork.cpp | 34 ++++++++++++++++++++++++++++++++-- src/cpp/ripple/ProofOfWork.h | 2 +- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/src/cpp/ripple/ProofOfWork.cpp b/src/cpp/ripple/ProofOfWork.cpp index 2fad06d48..f31723a11 100644 --- a/src/cpp/ripple/ProofOfWork.cpp +++ b/src/cpp/ripple/ProofOfWork.cpp @@ -4,6 +4,8 @@ #include +#include + #include #include "Serializer.h" @@ -104,12 +106,40 @@ bool ProofOfWork::checkSolution(const uint256& solution) const return getSHA512Half(buf2) <= mTarget; } +ProofOfWorkGenerator::ProofOfWorkGenerator() : + mIterations(128), + mTarget("0003FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"), + mLastDifficultyChange(time(NULL)), + mValidTime(180) +{ + RAND_bytes(mSecret.begin(), mSecret.size()); +} + +ProofOfWork ProofOfWorkGenerator::getProof() +{ + // challenge - target - iterations - time - validator + static boost::format f("%s-%s-%d-%d"); + + int now = static_cast(time(NULL) / 4); + + uint256 challenge; + RAND_bytes(challenge.begin(), challenge.size()); + + boost::mutex::scoped_lock sl(mLock); + + std::string s = boost::str(f % challenge.GetHex() % mTarget.GetHex() % mIterations % now); + std::string c = mSecret.GetHex() + s; + s += "-" + Serializer::getSHA512Half(c).GetHex(); + + return ProofOfWork(s, mIterations, challenge, mTarget); +} + BOOST_AUTO_TEST_SUITE(ProofOfWork_suite) BOOST_AUTO_TEST_CASE( ProofOfWork_test ) { - ProofOfWork pow("test", 32, uint256(), - uint256("000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF")); + ProofOfWorkGenerator gen; + ProofOfWork pow = gen.getProof(); cLog(lsINFO) << "Estimated difficulty: " << pow.getDifficulty(); uint256 solution = pow.solve(16777216); if (solution.isZero()) diff --git a/src/cpp/ripple/ProofOfWork.h b/src/cpp/ripple/ProofOfWork.h index 6540b0713..0e9a9337f 100644 --- a/src/cpp/ripple/ProofOfWork.h +++ b/src/cpp/ripple/ProofOfWork.h @@ -63,7 +63,7 @@ protected: boost::mutex mLock; public: - ProofOfWorkGenerator(const uint256& secret); + ProofOfWorkGenerator(); ProofOfWork getProof(); bool checkProof(const std::string& token, const uint256& solution);