From 3273625f61753fef1a95f404b0004351c941cb1b Mon Sep 17 00:00:00 2001 From: Vinnie Falco Date: Fri, 7 Jun 2013 20:59:53 -0700 Subject: [PATCH 1/3] Split up InfoSub and tidy up various files --- StyleCheatSheet.txt | 5 + modules/ripple_basics/ripple_basics.h | 2 +- .../{ripple_Types.h => ripple_BasicTypes.h} | 0 .../ripple_basics/types/ripple_UInt256.cpp | 2 +- modules/ripple_basics/types/ripple_UInt256.h | 36 +- modules/ripple_data/crypto/ripple_Base58.cpp | 8 +- modules/ripple_data/crypto/ripple_Base58.h | 8 +- modules/ripple_data/crypto/ripple_CBigNum.cpp | 2 +- modules/ripple_data/crypto/ripple_CBigNum.h | 2 +- modules/ripple_data/crypto/ripple_CKey.h | 14 +- .../crypto/ripple_CKeyDeterministic.cpp | 2 +- .../protocol/ripple_ProtocolTypes.h | 21 + .../protocol/ripple_RippleAddress.cpp | 12 +- .../protocol/ripple_RippleAddress.h | 12 +- .../protocol/ripple_SerializedObject.cpp | 2 +- .../protocol/ripple_SerializedObject.h | 2 +- .../protocol/ripple_SerializedTypes.cpp | 4 +- .../protocol/ripple_SerializedTypes.h | 16 +- .../protocol/ripple_Serializer.cpp | 8 +- .../ripple_data/protocol/ripple_Serializer.h | 10 +- modules/ripple_data/ripple_data.h | 3 + .../utility}/ripple_UptimeTimerAdapter.h | 0 modules/ripple_ledger/ripple_ledger.cpp | 18 + modules/ripple_main/ripple_main.cpp | 138 +++-- newcoin.vcxproj | 100 +++- newcoin.vcxproj.filters | 183 ++++--- src/cpp/database/SqliteDatabase.h | 8 - src/cpp/database/database.h | 3 - src/cpp/ripple/AcceptedLedger.cpp | 89 ---- src/cpp/ripple/AcceptedLedger.h | 93 ---- src/cpp/ripple/AccountItems.cpp | 2 +- src/cpp/ripple/AccountItems.h | 3 - src/cpp/ripple/AccountSetTransactor.h | 2 - src/cpp/ripple/AccountState.h | 28 +- src/cpp/ripple/Application.cpp | 12 + src/cpp/ripple/Application.h | 2 - src/cpp/ripple/CanonicalTXSet.cpp | 53 -- src/cpp/ripple/CanonicalTXSet.h | 60 --- src/cpp/ripple/Interpreter.cpp | 2 +- src/cpp/ripple/Interpreter.h | 2 +- src/cpp/ripple/Ledger.cpp | 302 +++++++---- src/cpp/ripple/Ledger.h | 82 ++- src/cpp/ripple/LedgerAcquire.h | 168 ------ src/cpp/ripple/LedgerConsensus.cpp | 28 +- src/cpp/ripple/LedgerConsensus.h | 41 +- src/cpp/ripple/LedgerEntrySet.cpp | 36 +- src/cpp/ripple/LedgerEntrySet.h | 42 +- src/cpp/ripple/LedgerHistory.h | 26 - src/cpp/ripple/LedgerMaster.cpp | 6 +- src/cpp/ripple/LedgerMaster.h | 22 +- src/cpp/ripple/LedgerProposal.cpp | 12 +- src/cpp/ripple/LedgerProposal.h | 28 +- src/cpp/ripple/LoadManager.cpp | 8 +- src/cpp/ripple/LoadManager.h | 40 +- src/cpp/ripple/NetworkOPs.cpp | 65 +-- src/cpp/ripple/NetworkOPs.h | 136 +---- src/cpp/ripple/NicknameState.h | 10 +- src/cpp/ripple/OfferCreateTransactor.cpp | 8 +- src/cpp/ripple/OfferCreateTransactor.h | 4 +- src/cpp/ripple/OrderBook.h | 5 +- src/cpp/ripple/OrderBookDB.cpp | 2 +- src/cpp/ripple/OrderBookDB.h | 7 +- src/cpp/ripple/PFRequest.cpp | 1 - src/cpp/ripple/ParameterTable.h | 10 +- src/cpp/ripple/RPCHandler.h | 1 - src/cpp/ripple/RPCServer.h | 7 - src/cpp/ripple/RPCSub.h | 4 - src/cpp/ripple/RippleCalc.cpp | 10 +- src/cpp/ripple/RippleState.h | 3 - src/cpp/ripple/SHAMap.h | 480 ------------------ src/cpp/ripple/SHAMapSync.h | 68 --- src/cpp/ripple/SerializedValidation.cpp | 4 +- src/cpp/ripple/SerializedValidation.h | 10 +- src/cpp/ripple/Transaction.cpp | 8 +- src/cpp/ripple/Transaction.h | 102 ++-- src/cpp/ripple/TransactionAcquire.cpp | 66 +-- src/cpp/ripple/TransactionEngine.h | 18 +- src/cpp/ripple/TransactionMaster.cpp | 4 +- src/cpp/ripple/TransactionMaster.h | 4 +- src/cpp/ripple/TransactionMeta.cpp | 14 +- src/cpp/ripple/TransactionMeta.h | 29 +- src/cpp/ripple/TransactionQueue.cpp | 2 +- src/cpp/ripple/TransactionQueue.h | 4 +- src/cpp/ripple/Transactor.h | 1 - src/cpp/ripple/WSConnection.h | 1 - src/cpp/ripple/WSDoor.cpp | 1 - src/cpp/ripple/ripple_AcceptedLedger.cpp | 36 ++ src/cpp/ripple/ripple_AcceptedLedger.h | 42 ++ src/cpp/ripple/ripple_AcceptedLedgerTx.cpp | 53 ++ src/cpp/ripple/ripple_AcceptedLedgerTx.h | 53 ++ src/cpp/ripple/ripple_CanonicalTXSet.cpp | 57 +++ src/cpp/ripple/ripple_CanonicalTXSet.h | 65 +++ src/cpp/ripple/ripple_Features.cpp | 42 +- src/cpp/ripple/ripple_HashRouter.cpp | 38 +- src/cpp/ripple/ripple_HashedObject.cpp | 56 +- src/cpp/ripple/ripple_HashedObject.h | 43 +- src/cpp/ripple/ripple_HashedObjectStore.cpp | 18 +- src/cpp/ripple/ripple_HashedObjectStore.h | 14 +- src/cpp/ripple/ripple_IFeatures.h | 16 +- src/cpp/ripple/ripple_IHashRouter.h | 16 +- src/cpp/ripple/ripple_IProofOfWorkFactory.h | 8 +- src/cpp/ripple/ripple_IValidations.h | 10 +- src/cpp/ripple/ripple_InfoSub.cpp | 70 +++ src/cpp/ripple/ripple_InfoSub.h | 59 +++ ...erAcquire.cpp => ripple_LedgerAcquire.cpp} | 330 +----------- src/cpp/ripple/ripple_LedgerAcquire.h | 98 ++++ src/cpp/ripple/ripple_LedgerAcquireMaster.cpp | 221 ++++++++ src/cpp/ripple/ripple_LedgerAcquireMaster.h | 42 ++ ...erHistory.cpp => ripple_LedgerHistory.cpp} | 4 +- src/cpp/ripple/ripple_LedgerHistory.h | 42 ++ src/cpp/ripple/ripple_Peer.cpp | 28 +- src/cpp/ripple/ripple_Peer.h | 6 +- src/cpp/ripple/ripple_PeerSet.cpp | 87 ++++ src/cpp/ripple/ripple_PeerSet.h | 71 +++ src/cpp/ripple/ripple_ProofOfWork.cpp | 8 +- src/cpp/ripple/ripple_ProofOfWork.h | 10 +- src/cpp/ripple/ripple_ProofOfWorkFactory.cpp | 12 +- .../ripple/{SHAMap.cpp => ripple_SHAMap.cpp} | 63 +-- src/cpp/ripple/ripple_SHAMap.h | 173 +++++++ src/cpp/ripple/ripple_SHAMapAddNode.h | 54 ++ ...{SHAMapDiff.cpp => ripple_SHAMapDelta.cpp} | 16 +- src/cpp/ripple/ripple_SHAMapItem.cpp | 16 + src/cpp/ripple/ripple_SHAMapItem.h | 52 ++ src/cpp/ripple/ripple_SHAMapMissingNode.cpp | 21 + src/cpp/ripple/ripple_SHAMapMissingNode.h | 79 +++ src/cpp/ripple/ripple_SHAMapNode.cpp | 146 ++++++ src/cpp/ripple/ripple_SHAMapNode.h | 67 +++ .../{SHAMapSync.cpp => ripple_SHAMapSync.cpp} | 56 +- src/cpp/ripple/ripple_SHAMapSyncFilter.h | 34 ++ src/cpp/ripple/ripple_SHAMapSyncFilters.cpp | 100 ++++ src/cpp/ripple/ripple_SHAMapSyncFilters.h | 67 +++ ...MapNodes.cpp => ripple_SHAMapTreeNode.cpp} | 157 +----- src/cpp/ripple/ripple_SHAMapTreeNode.h | 106 ++++ ...Ledger.cpp => ripple_SerializedLedger.cpp} | 10 +- ...izedLedger.h => ripple_SerializedLedger.h} | 23 +- ...n.cpp => ripple_SerializedTransaction.cpp} | 0 ...ction.h => ripple_SerializedTransaction.h} | 7 +- src/cpp/ripple/ripple_Validations.cpp | 14 +- 138 files changed, 3146 insertions(+), 2669 deletions(-) create mode 100644 StyleCheatSheet.txt rename modules/ripple_basics/types/{ripple_Types.h => ripple_BasicTypes.h} (100%) create mode 100644 modules/ripple_data/protocol/ripple_ProtocolTypes.h rename {src/cpp/ripple => modules/ripple_data/utility}/ripple_UptimeTimerAdapter.h (100%) delete mode 100644 src/cpp/ripple/AcceptedLedger.cpp delete mode 100644 src/cpp/ripple/AcceptedLedger.h delete mode 100644 src/cpp/ripple/CanonicalTXSet.cpp delete mode 100644 src/cpp/ripple/CanonicalTXSet.h delete mode 100644 src/cpp/ripple/LedgerAcquire.h delete mode 100644 src/cpp/ripple/LedgerHistory.h delete mode 100644 src/cpp/ripple/SHAMap.h delete mode 100644 src/cpp/ripple/SHAMapSync.h create mode 100644 src/cpp/ripple/ripple_AcceptedLedger.cpp create mode 100644 src/cpp/ripple/ripple_AcceptedLedger.h create mode 100644 src/cpp/ripple/ripple_AcceptedLedgerTx.cpp create mode 100644 src/cpp/ripple/ripple_AcceptedLedgerTx.h create mode 100644 src/cpp/ripple/ripple_CanonicalTXSet.cpp create mode 100644 src/cpp/ripple/ripple_CanonicalTXSet.h create mode 100644 src/cpp/ripple/ripple_InfoSub.cpp create mode 100644 src/cpp/ripple/ripple_InfoSub.h rename src/cpp/ripple/{LedgerAcquire.cpp => ripple_LedgerAcquire.cpp} (70%) create mode 100644 src/cpp/ripple/ripple_LedgerAcquire.h create mode 100644 src/cpp/ripple/ripple_LedgerAcquireMaster.cpp create mode 100644 src/cpp/ripple/ripple_LedgerAcquireMaster.h rename src/cpp/ripple/{LedgerHistory.cpp => ripple_LedgerHistory.cpp} (97%) create mode 100644 src/cpp/ripple/ripple_LedgerHistory.h create mode 100644 src/cpp/ripple/ripple_PeerSet.cpp create mode 100644 src/cpp/ripple/ripple_PeerSet.h rename src/cpp/ripple/{SHAMap.cpp => ripple_SHAMap.cpp} (92%) create mode 100644 src/cpp/ripple/ripple_SHAMap.h create mode 100644 src/cpp/ripple/ripple_SHAMapAddNode.h rename src/cpp/ripple/{SHAMapDiff.cpp => ripple_SHAMapDelta.cpp} (93%) create mode 100644 src/cpp/ripple/ripple_SHAMapItem.cpp create mode 100644 src/cpp/ripple/ripple_SHAMapItem.h create mode 100644 src/cpp/ripple/ripple_SHAMapMissingNode.cpp create mode 100644 src/cpp/ripple/ripple_SHAMapMissingNode.h create mode 100644 src/cpp/ripple/ripple_SHAMapNode.cpp create mode 100644 src/cpp/ripple/ripple_SHAMapNode.h rename src/cpp/ripple/{SHAMapSync.cpp => ripple_SHAMapSync.cpp} (93%) create mode 100644 src/cpp/ripple/ripple_SHAMapSyncFilter.h create mode 100644 src/cpp/ripple/ripple_SHAMapSyncFilters.cpp create mode 100644 src/cpp/ripple/ripple_SHAMapSyncFilters.h rename src/cpp/ripple/{SHAMapNodes.cpp => ripple_SHAMapTreeNode.cpp} (71%) create mode 100644 src/cpp/ripple/ripple_SHAMapTreeNode.h rename src/cpp/ripple/{SerializedLedger.cpp => ripple_SerializedLedger.cpp} (96%) rename src/cpp/ripple/{SerializedLedger.h => ripple_SerializedLedger.h} (76%) rename src/cpp/ripple/{SerializedTransaction.cpp => ripple_SerializedTransaction.cpp} (100%) rename src/cpp/ripple/{SerializedTransaction.h => ripple_SerializedTransaction.h} (96%) diff --git a/StyleCheatSheet.txt b/StyleCheatSheet.txt new file mode 100644 index 00000000000..a168e8c22c8 --- /dev/null +++ b/StyleCheatSheet.txt @@ -0,0 +1,5 @@ +- One class per header file. +- Place each data member on its own line. +- Place each ctor-initializer on its own line. +- Create typedefs for primitive types to describe them. +- Return descriptive local variables instead of constants. diff --git a/modules/ripple_basics/ripple_basics.h b/modules/ripple_basics/ripple_basics.h index b41cb5523c8..eb092665e86 100644 --- a/modules/ripple_basics/ripple_basics.h +++ b/modules/ripple_basics/ripple_basics.h @@ -134,7 +134,7 @@ namespace boost { #include "containers/ripple_SecureAllocator.h" #include "containers/ripple_TaggedCache.h" -#include "types/ripple_Types.h" +#include "types/ripple_BasicTypes.h" #include "utility/ripple_ByteOrder.h" #include "utility/ripple_DiffieHellmanUtil.h" #include "utility/ripple_InstanceCounter.h" diff --git a/modules/ripple_basics/types/ripple_Types.h b/modules/ripple_basics/types/ripple_BasicTypes.h similarity index 100% rename from modules/ripple_basics/types/ripple_Types.h rename to modules/ripple_basics/types/ripple_BasicTypes.h diff --git a/modules/ripple_basics/types/ripple_UInt256.cpp b/modules/ripple_basics/types/ripple_UInt256.cpp index 32350681fc5..2e32d37b393 100644 --- a/modules/ripple_basics/types/ripple_UInt256.cpp +++ b/modules/ripple_basics/types/ripple_UInt256.cpp @@ -1,5 +1,5 @@ -std::size_t hash_value(const uint256& u) +std::size_t hash_value(uint256 const& u) { std::size_t seed = HashMaps::getInstance ().getNonce (); diff --git a/modules/ripple_basics/types/ripple_UInt256.h b/modules/ripple_basics/types/ripple_UInt256.h index 0cfc1282b84..6ab7309907f 100644 --- a/modules/ripple_basics/types/ripple_UInt256.h +++ b/modules/ripple_basics/types/ripple_UInt256.h @@ -485,26 +485,26 @@ class uint256 : public base_uint256 }; -inline bool operator==(const uint256& a, uint64 b) { return (base_uint256)a == b; } -inline bool operator!=(const uint256& a, uint64 b) { return (base_uint256)a != b; } +inline bool operator==(uint256 const& a, uint64 b) { return (base_uint256)a == b; } +inline bool operator!=(uint256 const& a, uint64 b) { return (base_uint256)a != b; } inline const uint256 operator^(const base_uint256& a, const base_uint256& b) { return uint256(a) ^= b; } inline const uint256 operator&(const base_uint256& a, const base_uint256& b) { return uint256(a) &= b; } inline const uint256 operator|(const base_uint256& a, const base_uint256& b) { return uint256(a) |= b; } -inline bool operator==(const base_uint256& a, const uint256& b) { return (base_uint256)a == (base_uint256)b; } -inline bool operator!=(const base_uint256& a, const uint256& b) { return (base_uint256)a != (base_uint256)b; } -inline const uint256 operator^(const base_uint256& a, const uint256& b) { return (base_uint256)a ^ (base_uint256)b; } -inline const uint256 operator&(const base_uint256& a, const uint256& b) { return (base_uint256)a & (base_uint256)b; } -inline const uint256 operator|(const base_uint256& a, const uint256& b) { return (base_uint256)a | (base_uint256)b; } -inline bool operator==(const uint256& a, const base_uint256& b) { return (base_uint256)a == (base_uint256)b; } -inline bool operator!=(const uint256& a, const base_uint256& b) { return (base_uint256)a != (base_uint256)b; } -inline const uint256 operator^(const uint256& a, const base_uint256& b) { return (base_uint256)a ^ (base_uint256)b; } -inline const uint256 operator&(const uint256& a, const base_uint256& b) { return uint256(a) &= b; } -inline const uint256 operator|(const uint256& a, const base_uint256& b) { return (base_uint256)a | (base_uint256)b; } -inline bool operator==(const uint256& a, const uint256& b) { return (base_uint256)a == (base_uint256)b; } -inline bool operator!=(const uint256& a, const uint256& b) { return (base_uint256)a != (base_uint256)b; } -inline const uint256 operator^(const uint256& a, const uint256& b) { return (base_uint256)a ^ (base_uint256)b; } -inline const uint256 operator&(const uint256& a, const uint256& b) { return (base_uint256)a & (base_uint256)b; } -inline const uint256 operator|(const uint256& a, const uint256& b) { return (base_uint256)a | (base_uint256)b; } +inline bool operator==(const base_uint256& a, uint256 const& b) { return (base_uint256)a == (base_uint256)b; } +inline bool operator!=(const base_uint256& a, uint256 const& b) { return (base_uint256)a != (base_uint256)b; } +inline const uint256 operator^(const base_uint256& a, uint256 const& b) { return (base_uint256)a ^ (base_uint256)b; } +inline const uint256 operator&(const base_uint256& a, uint256 const& b) { return (base_uint256)a & (base_uint256)b; } +inline const uint256 operator|(const base_uint256& a, uint256 const& b) { return (base_uint256)a | (base_uint256)b; } +inline bool operator==(uint256 const& a, const base_uint256& b) { return (base_uint256)a == (base_uint256)b; } +inline bool operator!=(uint256 const& a, const base_uint256& b) { return (base_uint256)a != (base_uint256)b; } +inline const uint256 operator^(uint256 const& a, const base_uint256& b) { return (base_uint256)a ^ (base_uint256)b; } +inline const uint256 operator&(uint256 const& a, const base_uint256& b) { return uint256(a) &= b; } +inline const uint256 operator|(uint256 const& a, const base_uint256& b) { return (base_uint256)a | (base_uint256)b; } +inline bool operator==(uint256 const& a, uint256 const& b) { return (base_uint256)a == (base_uint256)b; } +inline bool operator!=(uint256 const& a, uint256 const& b) { return (base_uint256)a != (base_uint256)b; } +inline const uint256 operator^(uint256 const& a, uint256 const& b) { return (base_uint256)a ^ (base_uint256)b; } +inline const uint256 operator&(uint256 const& a, uint256 const& b) { return (base_uint256)a & (base_uint256)b; } +inline const uint256 operator|(uint256 const& a, uint256 const& b) { return (base_uint256)a | (base_uint256)b; } template inline std::ostream& operator<<(std::ostream& out, const base_uint& u) { @@ -737,7 +737,7 @@ inline const std::string strHex(const uint160& ui) extern std::size_t hash_value (const uint160&); -extern std::size_t hash_value (const uint256&); +extern std::size_t hash_value (uint256 const& ); #endif diff --git a/modules/ripple_data/crypto/ripple_Base58.cpp b/modules/ripple_data/crypto/ripple_Base58.cpp index 3a7924b3b12..27722267dde 100644 --- a/modules/ripple_data/crypto/ripple_Base58.cpp +++ b/modules/ripple_data/crypto/ripple_Base58.cpp @@ -103,7 +103,7 @@ std::string Base58::encodeWithCheck (Blob const& vchIn) return encode (vch); } -bool Base58::decode (const char* psz, Blob & vchRet, const char* pAlpha) +bool Base58::decode (const char* psz, Blob& vchRet, const char* pAlpha) { assert (pAlpha != 0); @@ -151,12 +151,12 @@ bool Base58::decode (const char* psz, Blob & vchRet, const char* pAlpha) return true; } -bool Base58::decode (const std::string& str, Blob & vchRet) +bool Base58::decode (const std::string& str, Blob& vchRet) { return decode (str.c_str(), vchRet); } -bool Base58::decodeWithCheck (const char* psz, Blob & vchRet, const char* pAlphabet) +bool Base58::decodeWithCheck (const char* psz, Blob& vchRet, const char* pAlphabet) { assert (pAlphabet != NULL); @@ -177,7 +177,7 @@ bool Base58::decodeWithCheck (const char* psz, Blob & vchRet, const char* pAlpha return true; } -bool Base58::decodeWithCheck (const std::string& str, Blob & vchRet, const char* pAlphabet) +bool Base58::decodeWithCheck (const std::string& str, Blob& vchRet, const char* pAlphabet) { return decodeWithCheck (str.c_str(), vchRet, pAlphabet); } diff --git a/modules/ripple_data/crypto/ripple_Base58.h b/modules/ripple_data/crypto/ripple_Base58.h index be402ed1a62..bfba1418453 100644 --- a/modules/ripple_data/crypto/ripple_Base58.h +++ b/modules/ripple_data/crypto/ripple_Base58.h @@ -48,10 +48,10 @@ class Base58 static std::string encode (Blob const& vch); static std::string encodeWithCheck (Blob const& vchIn); - static bool decode (const char* psz, Blob & vchRet, const char* pAlphabet=getCurrentAlphabet ()); - static bool decode (const std::string& str, Blob & vchRet); - static bool decodeWithCheck (const char* psz, Blob & vchRet, const char* pAlphabet=getCurrentAlphabet ()); - static bool decodeWithCheck (const std::string& str, Blob & vchRet, const char* pAlphabet); + static bool decode (const char* psz, Blob& vchRet, const char* pAlphabet=getCurrentAlphabet ()); + static bool decode (const std::string& str, Blob& vchRet); + static bool decodeWithCheck (const char* psz, Blob& vchRet, const char* pAlphabet=getCurrentAlphabet ()); + static bool decodeWithCheck (const std::string& str, Blob& vchRet, const char* pAlphabet); private: static char const* s_currentAlphabet; diff --git a/modules/ripple_data/crypto/ripple_CBigNum.cpp b/modules/ripple_data/crypto/ripple_CBigNum.cpp index dbda84f569d..0fc6e3918bd 100644 --- a/modules/ripple_data/crypto/ripple_CBigNum.cpp +++ b/modules/ripple_data/crypto/ripple_CBigNum.cpp @@ -139,7 +139,7 @@ void CBigNum::setuint64(uint64 n) #endif } -void CBigNum::setuint256(const uint256& n) +void CBigNum::setuint256(uint256 const& n) { BN_bin2bn(n.begin(), n.size(), NULL); } diff --git a/modules/ripple_data/crypto/ripple_CBigNum.h b/modules/ripple_data/crypto/ripple_CBigNum.h index 63259d1d461..62dbaccb08e 100644 --- a/modules/ripple_data/crypto/ripple_CBigNum.h +++ b/modules/ripple_data/crypto/ripple_CBigNum.h @@ -79,7 +79,7 @@ class CBigNum : public BIGNUM void setint64(int64 n); uint64 getuint64() const; void setuint64(uint64 n); - void setuint256(const uint256& n); + void setuint256(uint256 const& n); uint256 getuint256(); void setvch(Blob const& vch); Blob getvch() const; diff --git a/modules/ripple_data/crypto/ripple_CKey.h b/modules/ripple_data/crypto/ripple_CKey.h index 4670a29fbc9..075865aa0a0 100644 --- a/modules/ripple_data/crypto/ripple_CKey.h +++ b/modules/ripple_data/crypto/ripple_CKey.h @@ -126,7 +126,7 @@ class CKey static EC_KEY* GenerateRootPubKey(BIGNUM* pubGenerator); static EC_KEY* GeneratePublicDeterministicKey(const RippleAddress& generator, int n); static EC_KEY* GeneratePrivateDeterministicKey(const RippleAddress& family, const BIGNUM* rootPriv, int n); - static EC_KEY* GeneratePrivateDeterministicKey(const RippleAddress& family, const uint256& rootPriv, int n); + static EC_KEY* GeneratePrivateDeterministicKey(const RippleAddress& family, uint256 const& rootPriv, int n); CKey(const uint128& passPhrase) : fSet(false) { @@ -149,7 +149,7 @@ class CKey assert(pkey); } - CKey(const uint256& privateKey) : pkey(NULL), fSet(false) + CKey(uint256 const& privateKey) : pkey(NULL), fSet(false) { // XXX Broken pkey is null. SetPrivateKeyU(privateKey); @@ -194,7 +194,7 @@ class CKey BN_bn2bin(bn, privKey.begin() + (privKey.size() - BN_num_bytes(bn))); } - bool SetPrivateKeyU(const uint256& key, bool bThrow=false) + bool SetPrivateKeyU(uint256 const& key, bool bThrow=false) { // XXX Broken if pkey is not set. BIGNUM* bn = BN_bin2bn(key.begin(), key.size(), NULL); @@ -248,7 +248,7 @@ class CKey return vchPubKey; } - bool Sign(const uint256& hash, Blob & vchSig) + bool Sign(uint256 const& hash, Blob& vchSig) { unsigned char pchSig[10000]; unsigned int nSize = 0; @@ -264,7 +264,7 @@ class CKey return true; } - bool Verify(const uint256& hash, const void *sig, size_t sigLen) const + bool Verify(uint256 const& hash, const void *sig, size_t sigLen) const { // -1 = error, 0 = bad sig, 1 = good if (ECDSA_verify(0, hash.begin(), hash.size(), (const unsigned char *) sig, sigLen, pkey) != 1) @@ -272,12 +272,12 @@ class CKey return true; } - bool Verify(const uint256& hash, Blob const& vchSig) const + bool Verify(uint256 const& hash, Blob const& vchSig) const { return Verify(hash, &vchSig[0], vchSig.size()); } - bool Verify(const uint256& hash, const std::string& sig) const + bool Verify(uint256 const& hash, const std::string& sig) const { return Verify(hash, sig.data(), sig.size()); } diff --git a/modules/ripple_data/crypto/ripple_CKeyDeterministic.cpp b/modules/ripple_data/crypto/ripple_CKeyDeterministic.cpp index 48584a48ea8..08ca864e6ec 100644 --- a/modules/ripple_data/crypto/ripple_CKeyDeterministic.cpp +++ b/modules/ripple_data/crypto/ripple_CKeyDeterministic.cpp @@ -221,7 +221,7 @@ EC_KEY* CKey::GeneratePublicDeterministicKey(const RippleAddress& pubGen, int se return success ? pkey : NULL; } -EC_KEY* CKey::GeneratePrivateDeterministicKey(const RippleAddress& pubGen, const uint256& u, int seq) +EC_KEY* CKey::GeneratePrivateDeterministicKey(const RippleAddress& pubGen, uint256 const& u, int seq) { CBigNum bn(u); return GeneratePrivateDeterministicKey(pubGen, static_cast(&bn), seq); diff --git a/modules/ripple_data/protocol/ripple_ProtocolTypes.h b/modules/ripple_data/protocol/ripple_ProtocolTypes.h new file mode 100644 index 00000000000..9bec4e87211 --- /dev/null +++ b/modules/ripple_data/protocol/ripple_ProtocolTypes.h @@ -0,0 +1,21 @@ +#ifndef RIPPLE_PROTOCOLTYPES_H +#define RIPPLE_PROTOCOLTYPES_H + +/** A ledger hash. +*/ +typedef uint256 LedgerHash; + +/** A ledger index. +*/ +typedef uint32 LedgerIndex; +typedef uint32 LedgerSeq; + +/** A transaction identifier. +*/ +typedef uint256 TxID; + +/** A transaction index. +*/ +typedef uint32 TxSeq; + +#endif diff --git a/modules/ripple_data/protocol/ripple_RippleAddress.cpp b/modules/ripple_data/protocol/ripple_RippleAddress.cpp index da16b8ef2ca..924be45a4cc 100644 --- a/modules/ripple_data/protocol/ripple_RippleAddress.cpp +++ b/modules/ripple_data/protocol/ripple_RippleAddress.cpp @@ -129,7 +129,7 @@ void RippleAddress::setNodePublic(Blob const& vPublic) SetData(VER_NODE_PUBLIC, vPublic); } -bool RippleAddress::verifyNodePublic(const uint256& hash, Blob const& vchSig) const +bool RippleAddress::verifyNodePublic(uint256 const& hash, Blob const& vchSig) const { CKey pubkey = CKey(); bool bVerified; @@ -147,7 +147,7 @@ bool RippleAddress::verifyNodePublic(const uint256& hash, Blob const& vchSig) co return bVerified; } -bool RippleAddress::verifyNodePublic(const uint256& hash, const std::string& strSig) const +bool RippleAddress::verifyNodePublic(uint256 const& hash, const std::string& strSig) const { Blob vchSig(strSig.begin(), strSig.end()); @@ -234,7 +234,7 @@ void RippleAddress::setNodePrivate(uint256 hash256) SetData(VER_NODE_PRIVATE, hash256.begin(), 32); } -void RippleAddress::signNodePrivate(const uint256& hash, Blob & vchSig) const +void RippleAddress::signNodePrivate(uint256 const& hash, Blob& vchSig) const { CKey ckPrivKey; @@ -393,7 +393,7 @@ void RippleAddress::setAccountPublic(const RippleAddress& generator, int seq) setAccountPublic(pubkey.GetPubKey()); } -bool RippleAddress::accountPublicVerify(const uint256& uHash, Blob const& vucSig) const +bool RippleAddress::accountPublicVerify(uint256 const& uHash, Blob const& vucSig) const { CKey ckPublic; bool bVerified; @@ -494,7 +494,7 @@ void RippleAddress::setAccountPrivate(const RippleAddress& naGenerator, const Ri setAccountPrivate(uPrivKey); } -bool RippleAddress::accountPrivateSign(const uint256& uHash, Blob & vucSig) const +bool RippleAddress::accountPrivateSign(uint256 const& uHash, Blob& vucSig) const { CKey ckPrivate; bool bResult; @@ -515,7 +515,7 @@ bool RippleAddress::accountPrivateSign(const uint256& uHash, Blob & vucSig) cons } #if 0 -bool RippleAddress::accountPrivateVerify(const uint256& uHash, Blob const& vucSig) const +bool RippleAddress::accountPrivateVerify(uint256 const& uHash, Blob const& vucSig) const { CKey ckPrivate; bool bVerified; diff --git a/modules/ripple_data/protocol/ripple_RippleAddress.h b/modules/ripple_data/protocol/ripple_RippleAddress.h index 4526c8d94c9..85abce625e5 100644 --- a/modules/ripple_data/protocol/ripple_RippleAddress.h +++ b/modules/ripple_data/protocol/ripple_RippleAddress.h @@ -42,8 +42,8 @@ class RippleAddress : public CBase58Data bool setNodePublic(const std::string& strPublic); void setNodePublic(Blob const& vPublic); - bool verifyNodePublic(const uint256& hash, Blob const& vchSig) const; - bool verifyNodePublic(const uint256& hash, const std::string& strSig) const; + bool verifyNodePublic(uint256 const& hash, Blob const& vchSig) const; + bool verifyNodePublic(uint256 const& hash, const std::string& strSig) const; static RippleAddress createNodePublic(const RippleAddress& naSeed); static RippleAddress createNodePublic(Blob const& vPublic); @@ -60,7 +60,7 @@ class RippleAddress : public CBase58Data bool setNodePrivate(const std::string& strPrivate); void setNodePrivate(Blob const& vPrivate); void setNodePrivate(uint256 hash256); - void signNodePrivate(const uint256& hash, Blob & vchSig) const; + void signNodePrivate(uint256 const& hash, Blob& vchSig) const; static RippleAddress createNodePrivate(const RippleAddress& naSeed); @@ -96,7 +96,7 @@ class RippleAddress : public CBase58Data void setAccountPublic(Blob const& vPublic); void setAccountPublic(const RippleAddress& generator, int seq); - bool accountPublicVerify(const uint256& uHash, Blob const& vucSig) const; + bool accountPublicVerify(uint256 const& uHash, Blob const& vucSig) const; static RippleAddress createAccountPublic(Blob const& vPublic) { @@ -126,8 +126,8 @@ class RippleAddress : public CBase58Data void setAccountPrivate(uint256 hash256); void setAccountPrivate(const RippleAddress& naGenerator, const RippleAddress& naSeed, int seq); - bool accountPrivateSign(const uint256& uHash, Blob & vucSig) const; - // bool accountPrivateVerify(const uint256& uHash, Blob const& vucSig) const; + bool accountPrivateSign(uint256 const& uHash, Blob& vucSig) const; + // bool accountPrivateVerify(uint256 const& uHash, Blob const& vucSig) const; // Encrypt a message. Blob accountPrivateEncrypt(const RippleAddress& naPublicTo, Blob const& vucPlainText) const; diff --git a/modules/ripple_data/protocol/ripple_SerializedObject.cpp b/modules/ripple_data/protocol/ripple_SerializedObject.cpp index 14e7ff32ccf..08c93edd935 100644 --- a/modules/ripple_data/protocol/ripple_SerializedObject.cpp +++ b/modules/ripple_data/protocol/ripple_SerializedObject.cpp @@ -709,7 +709,7 @@ void STObject::setFieldH160(SField::ref field, const uint160& v) cf->setValue(v); } -void STObject::setFieldH256(SField::ref field, const uint256& v) +void STObject::setFieldH256(SField::ref field, uint256 const& v) { SerializedType* rf = getPField(field, true); if (!rf) throw std::runtime_error("Field not found"); diff --git a/modules/ripple_data/protocol/ripple_SerializedObject.h b/modules/ripple_data/protocol/ripple_SerializedObject.h index 9f2a89de81b..b687b2d2e68 100644 --- a/modules/ripple_data/protocol/ripple_SerializedObject.h +++ b/modules/ripple_data/protocol/ripple_SerializedObject.h @@ -99,7 +99,7 @@ class STObject : public SerializedType, private IS_INSTANCE(SerializedObject) void setFieldU64(SField::ref field, uint64); void setFieldH128(SField::ref field, const uint128&); void setFieldH160(SField::ref field, const uint160&); - void setFieldH256(SField::ref field, const uint256&); + void setFieldH256(SField::ref field, uint256 const& ); void setFieldVL(SField::ref field, Blob const&); void setFieldAccount(SField::ref field, const uint160&); void setFieldAccount(SField::ref field, const RippleAddress& addr) diff --git a/modules/ripple_data/protocol/ripple_SerializedTypes.cpp b/modules/ripple_data/protocol/ripple_SerializedTypes.cpp index 02511bd1c48..c0df623a075 100644 --- a/modules/ripple_data/protocol/ripple_SerializedTypes.cpp +++ b/modules/ripple_data/protocol/ripple_SerializedTypes.cpp @@ -301,9 +301,9 @@ bool STVector256::isEquivalent(const SerializedType& t) const return v && (mValue == v->mValue); } -bool STVector256::hasValue(const uint256& v) const +bool STVector256::hasValue(uint256 const& v) const { - BOOST_FOREACH(const uint256& hash, mValue) + BOOST_FOREACH(uint256 const& hash, mValue) { if (hash == v) return true; diff --git a/modules/ripple_data/protocol/ripple_SerializedTypes.h b/modules/ripple_data/protocol/ripple_SerializedTypes.h index cbefc7e454a..ae5aafec845 100644 --- a/modules/ripple_data/protocol/ripple_SerializedTypes.h +++ b/modules/ripple_data/protocol/ripple_SerializedTypes.h @@ -543,8 +543,8 @@ class STHash160 : public SerializedType class STHash256 : public SerializedType { public: - STHash256(const uint256& v) : value(v) { ; } - STHash256(SField::ref n, const uint256& v) : SerializedType(n), value(v) { ; } + STHash256(uint256 const& v) : value(v) { ; } + STHash256(SField::ref n, uint256 const& v) : SerializedType(n), value(v) { ; } STHash256(SField::ref n, const char *v) : SerializedType(n) { value.SetHex(v); } STHash256(SField::ref n, const std::string &v) : SerializedType(n) { value.SetHex(v); } STHash256(SField::ref n) : SerializedType(n) { ; } @@ -556,8 +556,8 @@ class STHash256 : public SerializedType std::string getText() const; void add(Serializer& s) const { s.add256(value); } - const uint256& getValue() const { return value; } - void setValue(const uint256& v) { value=v; } + uint256 const& getValue() const { return value; } + void setValue(uint256 const& v) { value=v; } operator uint256() const { return value; } virtual bool isEquivalent(const SerializedType& t) const; @@ -587,7 +587,7 @@ class STVariableLength : public SerializedType void add(Serializer& s) const { s.addVL(value); } Blob const& peekValue() const { return value; } - Blob & peekValue() { return value; } + Blob& peekValue() { return value; } Blob getValue() const { return value; } void setValue(Blob const& v) { value=v; } @@ -864,13 +864,13 @@ class STVector256 : public SerializedType int size() const { return mValue.size(); } bool isEmpty() const { return mValue.empty(); } - const uint256& at(int i) const { assert((i >= 0) && (i < size())); return mValue.at(i); } + uint256 const& at(int i) const { assert((i >= 0) && (i < size())); return mValue.at(i); } uint256& at(int i) { assert((i >= 0) && (i < size())); return mValue.at(i); } void setValue(const STVector256& v) { mValue = v.mValue; } void setValue(const std::vector& v) { mValue = v; } - void addValue(const uint256& v) { mValue.push_back(v); } - bool hasValue(const uint256& v) const; + void addValue(uint256 const& v) { mValue.push_back(v); } + bool hasValue(uint256 const& v) const; void sort() { std::sort(mValue.begin(), mValue.end()); } Json::Value getJson(int) const; diff --git a/modules/ripple_data/protocol/ripple_Serializer.cpp b/modules/ripple_data/protocol/ripple_Serializer.cpp index edf4e07bdc3..0bee7f99e9f 100644 --- a/modules/ripple_data/protocol/ripple_Serializer.cpp +++ b/modules/ripple_data/protocol/ripple_Serializer.cpp @@ -57,7 +57,7 @@ int Serializer::add160(const uint160& i) return ret; } -int Serializer::add256(const uint256& i) +int Serializer::add256(uint256 const& i) { int ret = mData.size(); mData.insert(mData.end(), i.begin(), i.end()); @@ -239,7 +239,7 @@ int Serializer::removeLastByte() return ret; } -bool Serializer::getRaw(Blob & o, int offset, int length) const +bool Serializer::getRaw(Blob& o, int offset, int length) const { if ((offset + length) > mData.size()) return false; o.assign(mData.begin() + offset, mData.begin() + offset + length); @@ -329,7 +329,7 @@ bool Serializer::checkSignature(Blob const& signature, CKey& key) const return key.Verify(getSHA512Half(), signature); } -bool Serializer::makeSignature(Blob &signature, CKey& key) const +bool Serializer::makeSignature(Blob&signature, CKey& key) const { return key.Sign(getSHA512Half(), signature); } @@ -367,7 +367,7 @@ int Serializer::addVL(const std::string& string) return ret; } -bool Serializer::getVL(Blob & objectVL, int offset, int& length) const +bool Serializer::getVL(Blob& objectVL, int offset, int& length) const { int b1; if (!get8(b1, offset++)) return false; diff --git a/modules/ripple_data/protocol/ripple_Serializer.h b/modules/ripple_data/protocol/ripple_Serializer.h index fd14a21b9c6..13495b2da59 100644 --- a/modules/ripple_data/protocol/ripple_Serializer.h +++ b/modules/ripple_data/protocol/ripple_Serializer.h @@ -27,7 +27,7 @@ class Serializer int add64(uint64); // native currency amounts int add128(const uint128&); // private key generators int add160(const uint160&); // account names, hankos - int add256(const uint256&); // transaction and ledger hashes + int add256(uint256 const& ); // transaction and ledger hashes int addRaw(Blob const& vector); int addRaw(const void *ptr, int len); int addRaw(const Serializer& s); @@ -47,10 +47,10 @@ class Serializer bool get160(uint160&, int offset) const; bool get256(uint256&, int offset) const; uint256 get256(int offset) const; - bool getRaw(Blob &, int offset, int length) const; + bool getRaw(Blob&, int offset, int length) const; Blob getRaw(int offset, int length) const; - bool getVL(Blob & objectVL, int offset, int& length) const; + bool getVL(Blob& objectVL, int offset, int& length) const; bool getVLLength(int& length, int offset) const; bool getFieldID(int& type, int& name, int offset) const; @@ -77,7 +77,7 @@ class Serializer // totality functions Blob const& peekData() const { return mData; } Blob getData() const { return mData; } - Blob & modData() { return mData; } + Blob& modData() { return mData; } int getCapacity() const { return mData.capacity(); } int getDataLength() const { return mData.size(); } const void* getDataPtr() const { return &mData.front(); } @@ -107,7 +107,7 @@ class Serializer // signature functions bool checkSignature(int pubkeyOffset, int signatureOffset) const; bool checkSignature(Blob const& signature, CKey& rkey) const; - bool makeSignature(Blob & signature, CKey& rkey) const; + bool makeSignature(Blob& signature, CKey& rkey) const; bool addSignature(CKey& rkey); // low-level VL length encode/decode functions diff --git a/modules/ripple_data/ripple_data.h b/modules/ripple_data/ripple_data.h index 7859e7ebffe..14b687c4c0a 100644 --- a/modules/ripple_data/ripple_data.h +++ b/modules/ripple_data/ripple_data.h @@ -67,6 +67,7 @@ #include "protocol/ripple_FieldNames.h" #include "protocol/ripple_PackedMessage.h" +#include "protocol/ripple_ProtocolTypes.h" #include "protocol/ripple_RippleAddress.h" #include "protocol/ripple_RippleSystem.h" #include "protocol/ripple_Serializer.h" // needs CKey @@ -77,4 +78,6 @@ #include "protocol/ripple_LedgerFormat.h" // needs SOTemplate from SerializedObject #include "protocol/ripple_TransactionFormat.h" +#include "utility/ripple_UptimeTimerAdapter.h" + #endif diff --git a/src/cpp/ripple/ripple_UptimeTimerAdapter.h b/modules/ripple_data/utility/ripple_UptimeTimerAdapter.h similarity index 100% rename from src/cpp/ripple/ripple_UptimeTimerAdapter.h rename to modules/ripple_data/utility/ripple_UptimeTimerAdapter.h diff --git a/modules/ripple_ledger/ripple_ledger.cpp b/modules/ripple_ledger/ripple_ledger.cpp index e7cce570a1b..df5da7cea54 100644 --- a/modules/ripple_ledger/ripple_ledger.cpp +++ b/modules/ripple_ledger/ripple_ledger.cpp @@ -22,4 +22,22 @@ @ingroup ripple_ledger */ +//------------------------------------------------------------------------------ + +// VFALCO TODO fix these warnings! +#ifdef _MSC_VER +//#pragma warning (push) // Causes spurious C4503 "decorated name exceeds maximum length" +#pragma warning (disable: 4018) // signed/unsigned mismatch +#pragma warning (disable: 4309) // truncation of constant value (websocket) +#pragma warning (disable: 4244) // conversion, possible loss of data +#pragma warning (disable: 4535) // call requires /EHa +#endif + +// VFALCO NOTE these includes generate warnings, unfortunately. #include "ripple_ledger.h" +#include "../ripple_data/ripple_data.h" + +#ifdef _MSC_VER +//#pragma warning (pop) +#endif + diff --git a/modules/ripple_main/ripple_main.cpp b/modules/ripple_main/ripple_main.cpp index fb997c0b9c0..249c84830f3 100644 --- a/modules/ripple_main/ripple_main.cpp +++ b/modules/ripple_main/ripple_main.cpp @@ -24,31 +24,39 @@ //------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ + #include #include #include +#include #include #include #include #include #include +#include #include #include #include +#include #include #include #include #include #include #include +#include #include #include #include #include +#include #include #include #include +#include #include #include #include @@ -56,9 +64,12 @@ #include #include #include +#include +#include #include #include #include +#include #include #include @@ -85,20 +96,41 @@ // VFALCO NOTE these includes generate warnings, unfortunately. #include "ripple_main.h" + #include "../ripple_data/ripple_data.h" //------------------------------------------------------------------------------ -// Order and position matter here +// VFALCO NOTE The order of these includes is critical, since they do not +// include their own dependencies. This is what allows us to +// linearize the include sequence and view it in one place. +// + +// VFALCO BEGIN CLEAN AREA These are all include-stripped + +#include "src/cpp/ripple/ripple_HashedObject.h" + +#include "src/cpp/ripple/ripple_SHAMapItem.h" +#include "src/cpp/ripple/ripple_SHAMapNode.h" +#include "src/cpp/ripple/ripple_SHAMapTreeNode.h" +#include "src/cpp/ripple/ripple_SHAMapMissingNode.h" +#include "src/cpp/ripple/ripple_SHAMapSyncFilter.h" +#include "src/cpp/ripple/ripple_SHAMapAddNode.h" +#include "src/cpp/ripple/ripple_SHAMap.h" + +#include "src/cpp/ripple/ripple_SerializedTransaction.h" +#include "src/cpp/ripple/ripple_SerializedLedger.h" +#include "src/cpp/ripple/TransactionMeta.h" +#include "src/cpp/ripple/Transaction.h" +#include "src/cpp/ripple/AccountState.h" +#include "src/cpp/ripple/NicknameState.h" #include "src/cpp/ripple/Ledger.h" + #include "src/cpp/ripple/SerializedValidation.h" -#include "src/cpp/database/SqliteDatabase.h" -#include "src/cpp/ripple/LoadManager.h" +#include "src/cpp/ripple/LoadManager.h" // VFALCO TODO Split this file up -//------------------------------------------------------------------------------ -// -// VFALCO BEGIN CLEAN AREA +// These have few dependencies #include "src/cpp/ripple/ripple_Config.h" #include "src/cpp/ripple/ripple_DatabaseCon.h" #include "src/cpp/ripple/ripple_LoadEvent.h" @@ -106,10 +138,12 @@ #include "src/cpp/ripple/ripple_ProofOfWork.h" #include "src/cpp/ripple/ripple_Job.h" #include "src/cpp/ripple/ripple_JobQueue.h" -#include "src/cpp/ripple/ripple_UptimeTimerAdapter.h" +#include "src/cpp/ripple/ripple_InfoSub.h" #include "src/cpp/ripple/ripple_HashedObject.h" #include "src/cpp/ripple/ripple_HashedObjectStore.h" +#include "src/cpp/ripple/ripple_SHAMapSyncFilters.h" +// Abstract interfaces #include "src/cpp/ripple/ripple_IFeatures.h" #include "src/cpp/ripple/ripple_IFeeVote.h" #include "src/cpp/ripple/ripple_IHashRouter.h" @@ -120,8 +154,15 @@ #include "src/cpp/ripple/ripple_IUniqueNodeList.h" #include "src/cpp/ripple/ripple_IValidations.h" +#include "src/cpp/ripple/ripple_PeerSet.h" +#include "src/cpp/ripple/ripple_LedgerAcquire.h" +#include "src/cpp/ripple/ripple_LedgerAcquireMaster.h" + +#include "src/cpp/database/database.h" +#include "src/cpp/database/SqliteDatabase.h" + // VFALCO END CLEAN AREA -// + //------------------------------------------------------------------------------ // VFALCO NOTE Order matters! If you get compile errors, move just 1 @@ -132,28 +173,39 @@ #include "src/cpp/ripple/Interpreter.h" #include "src/cpp/ripple/Operation.h" // VFALCO NOTE Order matters -#include "src/cpp/ripple/AcceptedLedger.h" + +// ----------- +// VFALCO NOTE These have all been include-stripped +// ORDER MATTERS A LOT! + + + + #include "src/cpp/ripple/AccountItems.h" -#include "src/cpp/ripple/AccountSetTransactor.h" -#include "src/cpp/ripple/AccountState.h" +#include "src/cpp/ripple/ripple_AcceptedLedgerTx.h" +#include "src/cpp/ripple/ripple_AcceptedLedger.h" +#include "src/cpp/ripple/LedgerEntrySet.h" +#include "src/cpp/ripple/TransactionEngine.h" +#include "src/cpp/ripple/ripple_CanonicalTXSet.h" + +#include "src/cpp/ripple/ripple_LedgerHistory.h" +#include "src/cpp/ripple/LedgerMaster.h" + +#include "src/cpp/ripple/LedgerProposal.h" +#include "src/cpp/ripple/NetworkOPs.h" + +// +// ----------- + #include "src/cpp/ripple/Application.h" #include "src/cpp/ripple/AutoSocket.h" #include "src/cpp/ripple/CallRPC.h" -#include "src/cpp/ripple/CanonicalTXSet.h" #include "src/cpp/ripple/ChangeTransactor.h" #include "src/cpp/ripple/HTTPRequest.h" #include "src/cpp/ripple/HashPrefixes.h" #include "src/cpp/ripple/HttpsClient.h" -#include "src/cpp/ripple/Ledger.h" -#include "src/cpp/ripple/LedgerAcquire.h" #include "src/cpp/ripple/LedgerConsensus.h" -#include "src/cpp/ripple/LedgerEntrySet.h" -#include "src/cpp/ripple/LedgerHistory.h" -#include "src/cpp/ripple/LedgerMaster.h" -#include "src/cpp/ripple/LedgerProposal.h" #include "src/cpp/ripple/LedgerTiming.h" -#include "src/cpp/ripple/NetworkOPs.h" -#include "src/cpp/ripple/NicknameState.h" #include "src/cpp/ripple/Offer.h" #include "src/cpp/ripple/OfferCancelTransactor.h" #include "src/cpp/ripple/OfferCreateTransactor.h" @@ -174,18 +226,12 @@ #include "src/cpp/ripple/RegularKeySetTransactor.h" #include "src/cpp/ripple/RippleCalc.h" #include "src/cpp/ripple/RippleState.h" -#include "src/cpp/ripple/SHAMap.h" -#include "src/cpp/ripple/SHAMapSync.h" #include "src/cpp/ripple/SNTPClient.h" -#include "src/cpp/ripple/SerializedLedger.h" -#include "src/cpp/ripple/SerializedTransaction.h" #include "src/cpp/ripple/SerializedValidation.h" -#include "src/cpp/ripple/Transaction.h" -#include "src/cpp/ripple/TransactionEngine.h" #include "src/cpp/ripple/TransactionMaster.h" -#include "src/cpp/ripple/TransactionMeta.h" #include "src/cpp/ripple/TransactionQueue.h" #include "src/cpp/ripple/Transactor.h" +#include "src/cpp/ripple/AccountSetTransactor.h" #include "src/cpp/ripple/TrustSetTransactor.h" #include "src/cpp/ripple/Version.h" #include "src/cpp/ripple/WSConnection.h" @@ -212,17 +258,21 @@ static DH* handleTmpDh(SSL* ssl, int is_export, int iKeyLength) return 512 == iKeyLength ? theApp->getWallet().getDh512() : theApp->getWallet().getDh1024(); } +//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ //------------------------------------------------------------------------------ #include "src/cpp/database/database.cpp" #include "src/cpp/database/SqliteDatabase.cpp" -#include "src/cpp/ripple/AcceptedLedger.cpp" // no log + #include "src/cpp/ripple/AccountItems.cpp" // no log #include "src/cpp/ripple/AccountSetTransactor.cpp" #include "src/cpp/ripple/AccountState.cpp" // no log #include "src/cpp/ripple/Application.cpp" #include "src/cpp/ripple/CallRPC.cpp" -#include "src/cpp/ripple/CanonicalTXSet.cpp" +#include "src/cpp/ripple/ripple_CanonicalTXSet.cpp" #include "src/cpp/ripple/ChangeTransactor.cpp" // no log #include "src/cpp/ripple/Contract.cpp" // no log #include "src/cpp/ripple/DBInit.cpp" @@ -230,10 +280,8 @@ static DH* handleTmpDh(SSL* ssl, int is_export, int iKeyLength) #include "src/cpp/ripple/HttpsClient.cpp" #include "src/cpp/ripple/Interpreter.cpp" // no log #include "src/cpp/ripple/Ledger.cpp" -#include "src/cpp/ripple/LedgerAcquire.cpp" #include "src/cpp/ripple/LedgerConsensus.cpp" #include "src/cpp/ripple/LedgerEntrySet.cpp" -#include "src/cpp/ripple/LedgerHistory.cpp" // no log #include "src/cpp/ripple/LedgerMaster.cpp" #include "src/cpp/ripple/LedgerProposal.cpp" // no log #include "src/cpp/ripple/LedgerTiming.cpp" @@ -263,13 +311,7 @@ static DH* handleTmpDh(SSL* ssl, int is_export, int iKeyLength) #include "src/cpp/ripple/RPCServer.cpp" #include "src/cpp/ripple/RPCSub.cpp" #include "src/cpp/ripple/ScriptData.cpp" // no log -#include "src/cpp/ripple/SerializedLedger.cpp" -#include "src/cpp/ripple/SerializedTransaction.cpp" #include "src/cpp/ripple/SerializedValidation.cpp" -#include "src/cpp/ripple/SHAMap.cpp" -#include "src/cpp/ripple/SHAMapDiff.cpp" // no log -#include "src/cpp/ripple/SHAMapNodes.cpp" // no log -#include "src/cpp/ripple/SHAMapSync.cpp" #include "src/cpp/ripple/SNTPClient.cpp" #include "src/cpp/ripple/Transaction.cpp" #include "src/cpp/ripple/TransactionAcquire.cpp" @@ -285,30 +327,46 @@ static DH* handleTmpDh(SSL* ssl, int is_export, int iKeyLength) #include "src/cpp/ripple/WalletAddTransactor.cpp" #include "src/cpp/ripple/WSDoor.cpp" // uses logging in WSConnection.h -//------------------------------------------------------------------------------ +#include "src/cpp/ripple/ripple_HashedObject.cpp" -// Refactored sources +#include "src/cpp/ripple/ripple_SHAMap.cpp" // Uses theApp +#include "src/cpp/ripple/ripple_SHAMapDelta.cpp" +#include "src/cpp/ripple/ripple_SHAMapItem.cpp" +#include "src/cpp/ripple/ripple_SHAMapNode.cpp" +#include "src/cpp/ripple/ripple_SHAMapSync.cpp" +#include "src/cpp/ripple/ripple_SHAMapTreeNode.cpp" +#include "src/cpp/ripple/ripple_SHAMapMissingNode.cpp" +#include "src/cpp/ripple/ripple_AcceptedLedgerTx.cpp" +#include "src/cpp/ripple/ripple_AcceptedLedger.cpp" #include "src/cpp/ripple/ripple_Config.cpp" #include "src/cpp/ripple/ripple_DatabaseCon.cpp" #include "src/cpp/ripple/ripple_Features.cpp" #include "src/cpp/ripple/ripple_FeeVote.cpp" -#include "src/cpp/ripple/ripple_HashedObject.cpp" #include "src/cpp/ripple/ripple_HashedObjectStore.cpp" #include "src/cpp/ripple/ripple_HashRouter.cpp" +#include "src/cpp/ripple/ripple_InfoSub.cpp" #include "src/cpp/ripple/ripple_Job.cpp" #include "src/cpp/ripple/ripple_JobQueue.cpp" +#include "src/cpp/ripple/ripple_LedgerAcquire.cpp" +#include "src/cpp/ripple/ripple_LedgerAcquireMaster.cpp" +#include "src/cpp/ripple/ripple_LedgerHistory.cpp" #include "src/cpp/ripple/ripple_LoadEvent.cpp" #include "src/cpp/ripple/ripple_LoadMonitor.cpp" #include "src/cpp/ripple/ripple_LogWebsockets.cpp" #include "src/cpp/ripple/ripple_LoadFeeTrack.cpp" #include "src/cpp/ripple/ripple_Peer.cpp" #include "src/cpp/ripple/ripple_Peers.cpp" +#include "src/cpp/ripple/ripple_PeerSet.cpp" #include "src/cpp/ripple/ripple_ProofOfWork.cpp" #include "src/cpp/ripple/ripple_ProofOfWorkFactory.cpp" +#include "src/cpp/ripple/ripple_SerializedLedger.cpp" +#include "src/cpp/ripple/ripple_SerializedTransaction.cpp" #include "src/cpp/ripple/ripple_Validations.cpp" #include "src/cpp/ripple/ripple_UniqueNodeList.cpp" +#include "src/cpp/ripple/ripple_SHAMapSyncFilters.cpp" // requires Application + //------------------------------------------------------------------------------ #ifdef _MSC_VER diff --git a/newcoin.vcxproj b/newcoin.vcxproj index a53275dda0c..b56489ad5b7 100644 --- a/newcoin.vcxproj +++ b/newcoin.vcxproj @@ -710,7 +710,7 @@ true - + true true true @@ -746,7 +746,13 @@ true true - + + true + true + true + true + + true true true @@ -776,6 +782,18 @@ true true + + true + true + true + true + + + true + true + true + true + true true @@ -816,7 +834,7 @@ true true - + true true true @@ -840,7 +858,7 @@ true true - + true true true @@ -1036,6 +1054,12 @@ true true + + true + true + true + true + true true @@ -1048,6 +1072,30 @@ true true + + true + true + true + true + + + true + true + true + true + + + true + true + true + true + + + true + true + true + true + true true @@ -1102,13 +1150,13 @@ true true - + true true true true - + true true true @@ -1120,25 +1168,25 @@ true true - + true true true true - + true true true true - + true true true true - + true true true @@ -1289,7 +1337,7 @@ - + @@ -1314,6 +1362,7 @@ + @@ -1649,18 +1698,20 @@ true true - + - + + + @@ -1668,17 +1719,18 @@ - + - + + @@ -1705,8 +1757,15 @@ + + + + + + + @@ -1715,11 +1774,11 @@ - - + + - - + + @@ -1778,6 +1837,9 @@ + + + diff --git a/newcoin.vcxproj.filters b/newcoin.vcxproj.filters index 6d662f61074..e9835473e81 100644 --- a/newcoin.vcxproj.filters +++ b/newcoin.vcxproj.filters @@ -85,9 +85,6 @@ {786657f9-b1b3-483c-a8e6-863cc2e02eb8} - - {b6175f9a-7d46-4b57-877f-f58b0b3bba89} - {becffa9b-61f7-42d4-815a-1ec822cad0e7} @@ -121,9 +118,6 @@ {1ccfc5ad-5cd7-4a8e-b305-08f663c2397c} - - {6a33d317-620f-4cf0-88a9-2d943b4c00a1} - {c441b367-d096-401f-a3f0-5ac24290d7b7} @@ -136,6 +130,9 @@ {2f3572a9-2882-4656-ab93-82b7761c9e3d} + + {b6175f9a-7d46-4b57-877f-f58b0b3bba89} + @@ -472,7 +469,7 @@ 1. Modules\ripple_basics - 2. Empty\ripple_ledger + 1. Modules\ripple_ledger 2. Empty\ripple_client @@ -480,9 +477,6 @@ 1. Modules\ripple_main\_unfactored\transactions - - 1. Modules\ripple_main\_unfactored\transactions - 2. Empty\ripple_db @@ -642,9 +636,6 @@ 1. Modules\ripple_main\refactored - - 1. Modules\ripple_main\_unfactored\ledger - 1. Modules\ripple_main\_unfactored\ledger @@ -654,9 +645,6 @@ 1. Modules\ripple_main\_unfactored\ledger - - 1. Modules\ripple_main\_unfactored\ledger - 1. Modules\ripple_main\_unfactored\ledger @@ -666,9 +654,6 @@ 1. Modules\ripple_main\_unfactored\ledger - - 1. Modules\ripple_main\_unfactored\ledger - 1. Modules\ripple_main\_unfactored\ledger @@ -702,27 +687,9 @@ 1. Modules\ripple_main\_unfactored\ledger - - 1. Modules\ripple_main\_unfactored\containers - - - 1. Modules\ripple_main\_unfactored\containers - - - 1. Modules\ripple_main\_unfactored\containers - - - 1. Modules\ripple_main\_unfactored\containers - 1. Modules\ripple_data\crypto - - 1. Modules\ripple_main\_unfactored\ledger - - - 1. Modules\ripple_main\_unfactored\ledger - 1. Modules\ripple_main\_unfactored\ledger @@ -819,6 +786,60 @@ 1. Modules\ripple_data\protocol + + 1. Modules\ripple_main\refactored + + + 1. Modules\ripple_main\refactored + + + 1. Modules\ripple_main\refactored + + + 1. Modules\ripple_main\refactored + + + 1. Modules\ripple_main\refactored + + + 1. Modules\ripple_main\refactored + + + 1. Modules\ripple_main\refactored + + + 1. Modules\ripple_main\refactored + + + 1. Modules\ripple_main\refactored + + + 1. Modules\ripple_main\refactored + + + 1. Modules\ripple_main\refactored + + + 1. Modules\ripple_main\refactored + + + 1. Modules\ripple_main\refactored + + + 1. Modules\ripple_main\refactored + + + 1. Modules\ripple_main\refactored + + + 1. Modules\ripple_main\refactored + + + 1. Modules\ripple_main\refactored + + + 1. Modules\ripple_main\refactored + @@ -1149,7 +1170,7 @@ 1. Modules\ripple_basics - 2. Empty\ripple_ledger + 1. Modules\ripple_ledger 2. Empty\ripple_client @@ -1157,9 +1178,6 @@ 1. Modules\ripple_main\_unfactored\transactions - - 1. Modules\ripple_main\_unfactored\transactions - 2. Empty\ripple_db @@ -1352,9 +1370,6 @@ 1. Modules\ripple_main\refactored - - 1. Modules\ripple_main\_unfactored\ledger - 1. Modules\ripple_main\_unfactored\ledger @@ -1364,9 +1379,6 @@ 1. Modules\ripple_main\_unfactored\ledger - - 1. Modules\ripple_main\_unfactored\ledger - 1. Modules\ripple_main\_unfactored\ledger @@ -1376,9 +1388,6 @@ 1. Modules\ripple_main\_unfactored\ledger - - 1. Modules\ripple_main\_unfactored\ledger - 1. Modules\ripple_main\_unfactored\ledger @@ -1412,21 +1421,9 @@ 1. Modules\ripple_main\_unfactored\ledger - - 1. Modules\ripple_main\_unfactored\containers - - - 1. Modules\ripple_main\_unfactored\containers - 1. Modules\ripple_main\_unfactored\ledger - - 1. Modules\ripple_main\_unfactored\ledger - - - 1. Modules\ripple_main\_unfactored\ledger - 1. Modules\ripple_main\refactored @@ -1526,9 +1523,66 @@ 1. Modules\ripple_data\protocol - + + 1. Modules\ripple_main\refactored + + + 1. Modules\ripple_main\refactored + + + 1. Modules\ripple_main\refactored + + + 1. Modules\ripple_main\refactored + + + 1. Modules\ripple_main\refactored + + + 1. Modules\ripple_main\refactored + + + 1. Modules\ripple_main\refactored + + + 1. Modules\ripple_main\refactored + + + 1. Modules\ripple_main\refactored + + + 1. Modules\ripple_main\refactored + + + 1. Modules\ripple_main\refactored + + 1. Modules\ripple_basics\types + + 1. Modules\ripple_data\protocol + + + 1. Modules\ripple_main\refactored + + + 1. Modules\ripple_main\refactored + + + 1. Modules\ripple_main\refactored + + + 1. Modules\ripple_main\refactored + + + 1. Modules\ripple_main\refactored + + + 1. Modules\ripple_main\refactored + + + 1. Modules\ripple_main\refactored + @@ -1550,4 +1604,7 @@ + + + \ No newline at end of file diff --git a/src/cpp/database/SqliteDatabase.h b/src/cpp/database/SqliteDatabase.h index b7396b43f26..f219f17b366 100644 --- a/src/cpp/database/SqliteDatabase.h +++ b/src/cpp/database/SqliteDatabase.h @@ -2,17 +2,9 @@ #ifndef RIPPLE_SQLITEDATABASE_H #define RIPPLE_SQLITEDATABASE_H -#include "database.h" - -#include -#include - -#include - struct sqlite3; struct sqlite3_stmt; - class SqliteDatabase : public Database { sqlite3* mConnection; diff --git a/src/cpp/database/database.h b/src/cpp/database/database.h index 9a63e563565..4bfb1c66849 100644 --- a/src/cpp/database/database.h +++ b/src/cpp/database/database.h @@ -1,9 +1,6 @@ #ifndef __DATABASE__ #define __DATABASE__ -#include -#include - #define SQL_FOREACH(_db, _strQuery) \ if ((_db)->executeSQL(_strQuery)) \ for (bool _bMore = (_db)->startIterRows(); _bMore; _bMore = (_db)->getNextRow()) diff --git a/src/cpp/ripple/AcceptedLedger.cpp b/src/cpp/ripple/AcceptedLedger.cpp deleted file mode 100644 index 4a3d774994f..00000000000 --- a/src/cpp/ripple/AcceptedLedger.cpp +++ /dev/null @@ -1,89 +0,0 @@ - -TaggedCache AcceptedLedger::ALCache("AcceptedLedger", 4, 60); - -ALTransaction::ALTransaction(uint32 seq, SerializerIterator& sit) -{ - Serializer txnSer(sit.getVL()); - SerializerIterator txnIt(txnSer); - - mTxn = boost::make_shared(boost::ref(txnIt)); - mRawMeta= sit.getVL(); - mMeta = boost::make_shared(mTxn->getTransactionID(), seq, mRawMeta); - mAffected = mMeta->getAffectedAccounts(); - mResult = mMeta->getResultTER(); - buildJson(); -} - -ALTransaction::ALTransaction(SerializedTransaction::ref txn, TransactionMetaSet::ref met) : - mTxn(txn), mMeta(met), mAffected(met->getAffectedAccounts()) -{ - mResult = mMeta->getResultTER(); - buildJson(); -} - -ALTransaction::ALTransaction(SerializedTransaction::ref txn, TER result) : - mTxn(txn), mResult(result), mAffected(txn->getMentionedAccounts()) -{ - buildJson(); -} - -std::string ALTransaction::getEscMeta() const -{ - assert(!mRawMeta.empty()); - return sqlEscape(mRawMeta); -} - -void ALTransaction::buildJson() -{ - mJson = Json::objectValue; - mJson["transaction"] = mTxn->getJson(0); - if (mMeta) - { - mJson["meta"] = mMeta->getJson(0); - mJson["raw_meta"] = strHex(mRawMeta); - } - mJson["result"] = transHuman(mResult); - - if (!mAffected.empty()) - { - Json::Value& affected = (mJson["affected"] = Json::arrayValue); - BOOST_FOREACH(const RippleAddress& ra, mAffected) - { - affected.append(ra.humanAccountID()); - } - } -} - -AcceptedLedger::AcceptedLedger(Ledger::ref ledger) : mLedger(ledger) -{ - SHAMap& txSet = *ledger->peekTransactionMap(); - for (SHAMapItem::pointer item = txSet.peekFirstItem(); !!item; item = txSet.peekNextItem(item->getTag())) - { - SerializerIterator sit(item->peekSerializer()); - insert(boost::make_shared(ledger->getLedgerSeq(), boost::ref(sit))); - } -} - -AcceptedLedger::pointer AcceptedLedger::makeAcceptedLedger(Ledger::ref ledger) -{ - AcceptedLedger::pointer ret = ALCache.fetch(ledger->getHash()); - if (ret) - return ret; - ret = AcceptedLedger::pointer(new AcceptedLedger(ledger)); - ALCache.canonicalize(ledger->getHash(), ret); - return ret; -} - -void AcceptedLedger::insert(ALTransaction::ref at) -{ - assert(mMap.find(at->getIndex()) == mMap.end()); - mMap.insert(std::make_pair(at->getIndex(), at)); -} - -ALTransaction::pointer AcceptedLedger::getTxn(int i) const -{ - map_t::const_iterator it = mMap.find(i); - if (it == mMap.end()) - return ALTransaction::pointer(); - return it->second; -} diff --git a/src/cpp/ripple/AcceptedLedger.h b/src/cpp/ripple/AcceptedLedger.h deleted file mode 100644 index 1298f9efc45..00000000000 --- a/src/cpp/ripple/AcceptedLedger.h +++ /dev/null @@ -1,93 +0,0 @@ -#ifndef ACCEPTED_LEDGER__H -#define ACCEPTED_LEDGER__H - -#include "SerializedTransaction.h" -#include "TransactionMeta.h" -#include "Ledger.h" - -/*============================================================================*/ -/** - A transaction that is in a closed ledger. - - Description - - @code - @endcode - - @see {uri} - - @ingroup ripple_ledger -*/ -class ALTransaction -{ -public: - typedef boost::shared_ptr pointer; - typedef const pointer& ref; - - ALTransaction(uint32 ledgerSeq, SerializerIterator& sit); - ALTransaction(SerializedTransaction::ref, TransactionMetaSet::ref); - ALTransaction(SerializedTransaction::ref, TER result); - - SerializedTransaction::ref getTxn() const { return mTxn; } - TransactionMetaSet::ref getMeta() const { return mMeta; } - const std::vector& getAffected() const { return mAffected; } - - uint256 getTransactionID() const { return mTxn->getTransactionID(); } - TransactionType getTxnType() const { return mTxn->getTxnType(); } - TER getResult() const { return mResult; } - uint32 getTxnSeq() const { return mMeta->getIndex(); } - - bool isApplied() const { return !!mMeta; } - int getIndex() const { return mMeta ? mMeta->getIndex() : 0; } - std::string getEscMeta() const; - Json::Value getJson() const { return mJson; } - -private: - SerializedTransaction::pointer mTxn; - TransactionMetaSet::pointer mMeta; - TER mResult; - std::vector mAffected; - Blob mRawMeta; - Json::Value mJson; - - void buildJson(); -}; - -/*============================================================================*/ -/** - -*/ -class AcceptedLedger -{ -public: - typedef boost::shared_ptr pointer; - typedef const pointer& ret; - typedef std::map map_t; // Must be an ordered map! - typedef map_t::value_type value_type; - typedef map_t::const_iterator const_iterator; - -public: - static pointer makeAcceptedLedger(Ledger::ref ledger); - static void sweep() { ALCache.sweep(); } - - Ledger::ref getLedger() const { return mLedger; } - const map_t& getMap() const { return mMap; } - - int getLedgerSeq() const { return mLedger->getLedgerSeq(); } - int getTxnCount() const { return mMap.size(); } - - static float getCacheHitRate() { return ALCache.getHitRate(); } - - ALTransaction::pointer getTxn(int) const; - -private: - Ledger::pointer mLedger; - map_t mMap; - - void insert(ALTransaction::ref); - - static TaggedCache ALCache; - AcceptedLedger(Ledger::ref ledger); -}; - -#endif diff --git a/src/cpp/ripple/AccountItems.cpp b/src/cpp/ripple/AccountItems.cpp index 17339173162..7ae07b5697d 100644 --- a/src/cpp/ripple/AccountItems.cpp +++ b/src/cpp/ripple/AccountItems.cpp @@ -21,7 +21,7 @@ void AccountItems::fillItems(const uint160& accountID, Ledger::ref ledger) SLE::pointer ownerDir = ledger->getDirNode(currentIndex); if (!ownerDir) return; - BOOST_FOREACH(const uint256& uNode, ownerDir->getFieldV256(sfIndexes).peekValue()) + BOOST_FOREACH(uint256 const& uNode, ownerDir->getFieldV256(sfIndexes).peekValue()) { SLE::pointer sleCur = ledger->getSLEi(uNode); diff --git a/src/cpp/ripple/AccountItems.h b/src/cpp/ripple/AccountItems.h index fef8800bb83..95dafc87bea 100644 --- a/src/cpp/ripple/AccountItems.h +++ b/src/cpp/ripple/AccountItems.h @@ -1,9 +1,6 @@ #ifndef __ACCOUNT_ITEMS__ #define __ACCOUNT_ITEMS__ -#include "Ledger.h" -#include "SerializedLedger.h" - // // Fetch ledger entries from an account's owner dir. // diff --git a/src/cpp/ripple/AccountSetTransactor.h b/src/cpp/ripple/AccountSetTransactor.h index 2eeddc53f86..9d058171653 100644 --- a/src/cpp/ripple/AccountSetTransactor.h +++ b/src/cpp/ripple/AccountSetTransactor.h @@ -1,8 +1,6 @@ #ifndef ACCOUNTSETTRANSACTOR_H #define ACCOUNTSETTRANSACTOR_H -#include "Transactor.h" - class AccountSetTransactor : public Transactor { public: diff --git a/src/cpp/ripple/AccountState.h b/src/cpp/ripple/AccountState.h index 0c412cb865c..1bfb937f310 100644 --- a/src/cpp/ripple/AccountState.h +++ b/src/cpp/ripple/AccountState.h @@ -1,31 +1,18 @@ -#ifndef __ACCOUNTSTATE__ -#define __ACCOUNTSTATE__ +#ifndef RIPPLE_ACCOUNTSTATE_H +#define RIPPLE_ACCOUNTSTATE_H // // Provide abstract access to an account's state, such that access to the serialized format is hidden. // -#include - -#include - -#include "SerializedLedger.h" - class AccountState { public: typedef boost::shared_ptr pointer; -private: - RippleAddress mAccountID; - RippleAddress mAuthorizedKey; - SerializedLedgerEntry::pointer mLedgerEntry; - - bool mValid; - public: - AccountState(const RippleAddress& naAccountID); // For new accounts - AccountState(SLE::ref ledgerEntry,const RippleAddress& naAccountI); // For accounts in a ledger + explicit AccountState(const RippleAddress& naAccountID); // For new accounts + AccountState (SLE::ref ledgerEntry,const RippleAddress& naAccountI); // For accounts in a ledger bool bHaveAuthorizedKey() { @@ -49,6 +36,13 @@ class AccountState void dump(); static std::string createGravatarUrl(uint128 uEmailHash); + +private: + RippleAddress mAccountID; + RippleAddress mAuthorizedKey; + SerializedLedgerEntry::pointer mLedgerEntry; + + bool mValid; }; #endif diff --git a/src/cpp/ripple/Application.cpp b/src/cpp/ripple/Application.cpp index 2639f6f0326..803d304c251 100644 --- a/src/cpp/ripple/Application.cpp +++ b/src/cpp/ripple/Application.cpp @@ -5,6 +5,18 @@ #define SYSTEM_CURRENCY_PARTS 1000000ull // 10^SYSTEM_CURRENCY_PRECISION #define SYSTEM_CURRENCY_START (SYSTEM_CURRENCY_GIFT*SYSTEM_CURRENCY_USERS*SYSTEM_CURRENCY_PARTS) +/* VFALCO NOTE + + The master lock protects: + + - The open ledger + - Server global state + * What the last closed ledger is + * State of the consensus engine + + other things +*/ + SETUP_LOG (Application) // VFALCO TODO fix/clean this, it might have broken with the Log changes diff --git a/src/cpp/ripple/Application.h b/src/cpp/ripple/Application.h index a11644f62e5..ba621dafc45 100644 --- a/src/cpp/ripple/Application.h +++ b/src/cpp/ripple/Application.h @@ -8,10 +8,8 @@ #include "../database/database.h" #include "LedgerMaster.h" -#include "LedgerAcquire.h" #include "TransactionMaster.h" #include "Wallet.h" -#include "NetworkOPs.h" #include "WSDoor.h" #include "SNTPClient.h" #include "RPCHandler.h" diff --git a/src/cpp/ripple/CanonicalTXSet.cpp b/src/cpp/ripple/CanonicalTXSet.cpp deleted file mode 100644 index 5f3d016f91b..00000000000 --- a/src/cpp/ripple/CanonicalTXSet.cpp +++ /dev/null @@ -1,53 +0,0 @@ - -bool CanonicalTXKey::operator<(const CanonicalTXKey& key) const -{ - if (mAccount < key.mAccount) return true; - if (mAccount > key.mAccount) return false; - if (mSeq < key.mSeq) return true; - if (mSeq > key.mSeq) return false; - return mTXid < key.mTXid; -} - -bool CanonicalTXKey::operator>(const CanonicalTXKey& key) const -{ - if (mAccount > key.mAccount) return true; - if (mAccount < key.mAccount) return false; - if (mSeq > key.mSeq) return true; - if (mSeq < key.mSeq) return false; - return mTXid > key.mTXid; -} - -bool CanonicalTXKey::operator<=(const CanonicalTXKey& key) const -{ - if (mAccount < key.mAccount) return true; - if (mAccount > key.mAccount) return false; - if (mSeq < key.mSeq) return true; - if (mSeq > key.mSeq) return false; - return mTXid <= key.mTXid; -} - -bool CanonicalTXKey::operator>=(const CanonicalTXKey& key)const -{ - if (mAccount > key.mAccount) return true; - if (mAccount < key.mAccount) return false; - if (mSeq > key.mSeq) return true; - if (mSeq < key.mSeq) return false; - return mTXid >= key.mTXid; -} - -void CanonicalTXSet::push_back(SerializedTransaction::ref txn) -{ - uint256 effectiveAccount = mSetHash; - effectiveAccount ^= txn->getSourceAccount().getAccountID().to256(); - mMap.insert(std::make_pair(CanonicalTXKey(effectiveAccount, txn->getSequence(), txn->getTransactionID()), txn)); -} - -CanonicalTXSet::iterator CanonicalTXSet::erase(const iterator& it) -{ - iterator tmp = it; - ++tmp; - mMap.erase(it); - return tmp; -} - - diff --git a/src/cpp/ripple/CanonicalTXSet.h b/src/cpp/ripple/CanonicalTXSet.h deleted file mode 100644 index bde6f3e4782..00000000000 --- a/src/cpp/ripple/CanonicalTXSet.h +++ /dev/null @@ -1,60 +0,0 @@ -#ifndef __CANONICAL_TX_SET_ -#define __CANONICAL_TX_SET_ - -#include - -#include "SerializedTransaction.h" - -class CanonicalTXKey -{ -public: - CanonicalTXKey(const uint256& account, uint32 seq, const uint256& id) - : mAccount(account), mTXid(id), mSeq(seq) { ; } - - bool operator<(const CanonicalTXKey&) const; - bool operator>(const CanonicalTXKey&) const; - bool operator<=(const CanonicalTXKey&) const; - bool operator>=(const CanonicalTXKey&) const; - - bool operator==(const CanonicalTXKey& k) const { return mTXid == k.mTXid; } - bool operator!=(const CanonicalTXKey& k) const { return mTXid != k.mTXid; } - - const uint256& getTXID() const { return mTXid; } - -private: - uint256 mAccount, mTXid; - uint32 mSeq; -}; - -class CanonicalTXSet -{ -public: - typedef std::map::iterator iterator; - typedef std::map::const_iterator const_iterator; - -public: - CanonicalTXSet(const uint256& lclHash) : mSetHash(lclHash) { ; } - - void push_back(SerializedTransaction::ref txn); - - void reset(const uint256& newLCL) - { - mSetHash = newLCL; - mMap.clear(); - } - - iterator erase(const iterator& it); - - iterator begin() { return mMap.begin(); } - iterator end() { return mMap.end(); } - const_iterator begin() const { return mMap.begin(); } - const_iterator end() const { return mMap.end(); } - size_t size() const { return mMap.size(); } - bool empty() const { return mMap.empty(); } - -private: - uint256 mSetHash; - std::map mMap; -}; - -#endif diff --git a/src/cpp/ripple/Interpreter.cpp b/src/cpp/ripple/Interpreter.cpp index d9740dc1e90..8c9235b2c28 100644 --- a/src/cpp/ripple/Interpreter.cpp +++ b/src/cpp/ripple/Interpreter.cpp @@ -111,7 +111,7 @@ bool Interpreter::endBlock() return(true); } -TER Interpreter::interpret(Contract* contract,const SerializedTransaction& txn,Blob & code) +TER Interpreter::interpret(Contract* contract,const SerializedTransaction& txn,Blob& code) { mContract=contract; mCode=&code; diff --git a/src/cpp/ripple/Interpreter.h b/src/cpp/ripple/Interpreter.h index f97f4ae8b69..d83d06b2a0b 100644 --- a/src/cpp/ripple/Interpreter.h +++ b/src/cpp/ripple/Interpreter.h @@ -44,7 +44,7 @@ class Interpreter Interpreter(); // returns a TransactionEngineResult - TER interpret(Contract* contract,const SerializedTransaction& txn,Blob & code); + TER interpret(Contract* contract,const SerializedTransaction& txn,Blob& code); void stop(); diff --git a/src/cpp/ripple/Ledger.cpp b/src/cpp/ripple/Ledger.cpp index 95339bc9584..c1ae7567b63 100644 --- a/src/cpp/ripple/Ledger.cpp +++ b/src/cpp/ripple/Ledger.cpp @@ -3,37 +3,69 @@ SETUP_LOG (Ledger) DECLARE_INSTANCE(Ledger); -Ledger::Ledger(const RippleAddress& masterID, uint64 startAmount) : mTotCoins(startAmount), mLedgerSeq(1), - mCloseTime(0), mParentCloseTime(0), mCloseResolution(LEDGER_TIME_ACCURACY), mCloseFlags(0), - mClosed(false), mValidHash(false), mAccepted(false), mImmutable(false), - mTransactionMap(boost::make_shared(smtTRANSACTION)), - mAccountStateMap(boost::make_shared(smtSTATE)) +Ledger::Ledger (const RippleAddress& masterID, uint64 startAmount) + : mTotCoins (startAmount) + , mLedgerSeq (1) + , mCloseTime (0) + , mParentCloseTime (0) + , mCloseResolution (LEDGER_TIME_ACCURACY) + , mCloseFlags (0) + , mClosed (false) + , mValidHash (false) + , mAccepted (false) + , mImmutable (false) + , mTransactionMap (boost::make_shared (smtTRANSACTION)) + , mAccountStateMap (boost::make_shared (smtSTATE)) { // special case: put coins in root account AccountState::pointer startAccount = boost::make_shared(masterID); - startAccount->peekSLE().setFieldAmount(sfBalance, startAmount); - startAccount->peekSLE().setFieldU32(sfSequence, 1); - WriteLog (lsTRACE, Ledger) << "root account: " << startAccount->peekSLE().getJson(0); + + startAccount->peekSLE().setFieldAmount(sfBalance, startAmount); + + startAccount->peekSLE().setFieldU32(sfSequence, 1); + + WriteLog (lsTRACE, Ledger) << "root account: " << startAccount->peekSLE().getJson(0); mAccountStateMap->armDirty(); - writeBack(lepCREATE, startAccount->getSLE()); - SHAMap::flushDirty(*mAccountStateMap->disarmDirty(), 256, hotACCOUNT_NODE, mLedgerSeq); - zeroFees(); -} - -Ledger::Ledger(const uint256 &parentHash, const uint256 &transHash, const uint256 &accountHash, - uint64 totCoins, uint32 closeTime, uint32 parentCloseTime, - int closeFlags, int closeResolution, uint32 ledgerSeq, bool& loaded) - : mParentHash(parentHash), mTransHash(transHash), mAccountHash(accountHash), mTotCoins(totCoins), - mLedgerSeq(ledgerSeq), mCloseTime(closeTime), mParentCloseTime(parentCloseTime), - mCloseResolution(closeResolution), mCloseFlags(closeFlags), - mClosed(false), mValidHash(false), mAccepted(false), mImmutable(true), - mTransactionMap(boost::make_shared(smtTRANSACTION, transHash)), - mAccountStateMap(boost::make_shared(smtSTATE, accountHash)) -{ // This will throw if the root nodes are not available locally + + writeBack(lepCREATE, startAccount->getSLE()); + + SHAMap::flushDirty (*mAccountStateMap->disarmDirty(), 256, hotACCOUNT_NODE, mLedgerSeq); + + initializeFees (); +} + +Ledger::Ledger (uint256 const& parentHash, + uint256 const& transHash, + uint256 const& accountHash, + uint64 totCoins, + uint32 closeTime, + uint32 parentCloseTime, + int closeFlags, + int closeResolution, + uint32 ledgerSeq, + bool& loaded) + : mParentHash (parentHash) + , mTransHash (transHash) + , mAccountHash (accountHash) + , mTotCoins (totCoins) + , mLedgerSeq (ledgerSeq) + , mCloseTime (closeTime) + , mParentCloseTime (parentCloseTime) + , mCloseResolution (closeResolution) + , mCloseFlags (closeFlags) + , mClosed (false) + , mValidHash (false) + , mAccepted (false) + , mImmutable (true) + , mTransactionMap (boost::make_shared (smtTRANSACTION, transHash)) + , mAccountStateMap (boost::make_shared (smtSTATE, accountHash)) +{ + // This will throw if the root nodes are not available locally updateHash(); loaded = true; - try + + try { if (mTransHash.isNonZero()) mTransactionMap->fetchRoot(mTransHash, NULL); @@ -41,9 +73,11 @@ Ledger::Ledger(const uint256 &parentHash, const uint256 &transHash, const uint25 catch (...) { loaded = false; - WriteLog (lsWARNING, Ledger) << "Don't have TX root for ledger"; + + WriteLog (lsWARNING, Ledger) << "Don't have TX root for ledger"; } - try + + try { if (mAccountHash.isNonZero()) mAccountStateMap->fetchRoot(mAccountHash, NULL); @@ -51,51 +85,87 @@ Ledger::Ledger(const uint256 &parentHash, const uint256 &transHash, const uint25 catch (...) { loaded = false; + WriteLog (lsWARNING, Ledger) << "Don't have AS root for ledger"; } - mTransactionMap->setImmutable(); + + mTransactionMap->setImmutable(); mAccountStateMap->setImmutable(); - zeroFees(); -} -Ledger::Ledger(Ledger& ledger, bool isMutable) : mParentHash(ledger.mParentHash), mTotCoins(ledger.mTotCoins), - mLedgerSeq(ledger.mLedgerSeq), mCloseTime(ledger.mCloseTime), mParentCloseTime(ledger.mParentCloseTime), - mCloseResolution(ledger.mCloseResolution), mCloseFlags(ledger.mCloseFlags), - mClosed(ledger.mClosed), mValidHash(false), mAccepted(ledger.mAccepted), mImmutable(!isMutable), - mTransactionMap(ledger.mTransactionMap->snapShot(isMutable)), - mAccountStateMap(ledger.mAccountStateMap->snapShot(isMutable)) -{ // Create a new ledger that's a snapshot of this one + initializeFees (); +} + +// Create a new ledger that's a snapshot of this one +Ledger::Ledger (Ledger& ledger, + bool isMutable) + : mParentHash (ledger.mParentHash) + , mTotCoins (ledger.mTotCoins) + , mLedgerSeq (ledger.mLedgerSeq) + , mCloseTime (ledger.mCloseTime) + , mParentCloseTime (ledger.mParentCloseTime) + , mCloseResolution (ledger.mCloseResolution) + , mCloseFlags (ledger.mCloseFlags) + , mClosed (ledger.mClosed) + , mValidHash (false) + , mAccepted (ledger.mAccepted) + , mImmutable (!isMutable) + , mTransactionMap (ledger.mTransactionMap->snapShot (isMutable)) + , mAccountStateMap (ledger.mAccountStateMap->snapShot (isMutable)) +{ updateHash(); - zeroFees(); -} + initializeFees (); +} + +// Create a new ledger that follows this one +Ledger::Ledger (bool /* dummy */, + Ledger& prevLedger) + : mTotCoins (prevLedger.mTotCoins) + , mLedgerSeq (prevLedger.mLedgerSeq + 1) + , mParentCloseTime (prevLedger.mCloseTime) + , mCloseResolution (prevLedger.mCloseResolution) + , mCloseFlags (0) + , mClosed (false) + , mValidHash (false) + , mAccepted (false) + , mImmutable (false) + , mTransactionMap (boost::make_shared (smtTRANSACTION)) + , mAccountStateMap (prevLedger.mAccountStateMap->snapShot (true)) +{ + prevLedger.updateHash(); + mParentHash = prevLedger.getHash(); + + assert(mParentHash.isNonZero()); -Ledger::Ledger(bool /* dummy */, Ledger& prevLedger) : - mTotCoins(prevLedger.mTotCoins), mLedgerSeq(prevLedger.mLedgerSeq + 1), - mParentCloseTime(prevLedger.mCloseTime), mCloseResolution(prevLedger.mCloseResolution), - mCloseFlags(0), mClosed(false), mValidHash(false), mAccepted(false), mImmutable(false), - mTransactionMap(boost::make_shared(smtTRANSACTION)), - mAccountStateMap(prevLedger.mAccountStateMap->snapShot(true)) -{ // Create a new ledger that follows this one - prevLedger.updateHash(); - mParentHash = prevLedger.getHash(); - assert(mParentHash.isNonZero()); + mCloseResolution = ContinuousLedgerTiming::getNextLedgerTimeResolution ( + prevLedger.mCloseResolution, + prevLedger.getCloseAgree(), + mLedgerSeq); - mCloseResolution = ContinuousLedgerTiming::getNextLedgerTimeResolution(prevLedger.mCloseResolution, - prevLedger.getCloseAgree(), mLedgerSeq); if (prevLedger.mCloseTime == 0) + { mCloseTime = roundCloseTime(theApp->getOPs().getCloseTimeNC(), mCloseResolution); + } else + { mCloseTime = prevLedger.mCloseTime + mCloseResolution; - zeroFees(); + } + + initializeFees (); } -Ledger::Ledger(Blob const& rawLedger, bool hasPrefix) : - mClosed(false), mValidHash(false), mAccepted(false), mImmutable(true) +Ledger::Ledger (Blob const& rawLedger, + bool hasPrefix) + : mClosed (false) + , mValidHash (false) + , mAccepted (false) + , mImmutable (true) { - Serializer s(rawLedger); - setRaw(s, hasPrefix); - zeroFees(); + Serializer s (rawLedger); + + setRaw (s, hasPrefix); + + initializeFees (); } Ledger::Ledger(const std::string& rawLedger, bool hasPrefix) : @@ -103,7 +173,7 @@ Ledger::Ledger(const std::string& rawLedger, bool hasPrefix) : { Serializer s(rawLedger); setRaw(s, hasPrefix); - zeroFees(); + initializeFees (); } void Ledger::setImmutable() @@ -217,7 +287,7 @@ AccountState::pointer Ledger::getAccountState(const RippleAddress& accountID) return boost::make_shared(sle,accountID); } -NicknameState::pointer Ledger::getNicknameState(const uint256& uNickname) +NicknameState::pointer Ledger::getNicknameState(uint256 const& uNickname) { SHAMapItem::pointer item = mAccountStateMap->peekItem(Ledger::getNicknameIndex(uNickname)); if (!item) @@ -231,7 +301,7 @@ NicknameState::pointer Ledger::getNicknameState(const uint256& uNickname) return boost::make_shared(sle); } -bool Ledger::addTransaction(const uint256& txID, const Serializer& txn) +bool Ledger::addTransaction(uint256 const& txID, const Serializer& txn) { // low-level - just add to table SHAMapItem::pointer item = boost::make_shared(txID, txn.peekData()); if (!mTransactionMap->addGiveItem(item, true, false)) @@ -243,7 +313,7 @@ bool Ledger::addTransaction(const uint256& txID, const Serializer& txn) return true; } -bool Ledger::addTransaction(const uint256& txID, const Serializer& txn, const Serializer& md) +bool Ledger::addTransaction(uint256 const& txID, const Serializer& txn, const Serializer& md) { // low-level - just add to table Serializer s(txn.getDataLength() + md.getDataLength() + 16); s.addVL(txn.peekData()); @@ -258,7 +328,7 @@ bool Ledger::addTransaction(const uint256& txID, const Serializer& txn, const Se return true; } -Transaction::pointer Ledger::getTransaction(const uint256& transID) const +Transaction::pointer Ledger::getTransaction(uint256 const& transID) const { SHAMapTreeNode::TNType type; SHAMapItem::pointer item = mTransactionMap->peekItem(transID, type); @@ -330,7 +400,7 @@ SerializedTransaction::pointer Ledger::getSMTransaction(SHAMapItem::ref item, SH return SerializedTransaction::pointer(); } -bool Ledger::getTransaction(const uint256& txID, Transaction::pointer& txn, TransactionMetaSet::pointer& meta) +bool Ledger::getTransaction(uint256 const& txID, Transaction::pointer& txn, TransactionMetaSet::pointer& meta) { SHAMapTreeNode::TNType type; SHAMapItem::pointer item = mTransactionMap->peekItem(txID, type); @@ -363,7 +433,7 @@ bool Ledger::getTransaction(const uint256& txID, Transaction::pointer& txn, Tran return true; } -bool Ledger::getTransactionMeta(const uint256& txID, TransactionMetaSet::pointer& meta) +bool Ledger::getTransactionMeta(uint256 const& txID, TransactionMetaSet::pointer& meta) { SHAMapTreeNode::TNType type; SHAMapItem::pointer item = mTransactionMap->peekItem(txID, type); @@ -380,7 +450,7 @@ bool Ledger::getTransactionMeta(const uint256& txID, TransactionMetaSet::pointer return true; } -bool Ledger::getMetaHex(const uint256& transID, std::string& hex) +bool Ledger::getMetaHex(uint256 const& transID, std::string& hex) { SHAMapTreeNode::TNType type; SHAMapItem::pointer item = mTransactionMap->peekItem(transID, type); @@ -539,7 +609,7 @@ Ledger::pointer Ledger::loadByIndex(uint32 ledgerIndex) return ledger; } -Ledger::pointer Ledger::loadByHash(const uint256& ledgerHash) +Ledger::pointer Ledger::loadByHash(uint256 const& ledgerHash) { Ledger::pointer ledger; { @@ -573,7 +643,7 @@ Ledger::pointer Ledger::loadByIndex(uint32 ledgerIndex) return getSQL(sql); } -Ledger::pointer Ledger::loadByHash(const uint256& ledgerHash) +Ledger::pointer Ledger::loadByHash(uint256 const& ledgerHash) { // This is a low-level function with no caching and only gets accepted ledgers std::string sql="SELECT * from Ledgers WHERE LedgerHash='"; sql.append(ledgerHash.GetHex()); @@ -992,7 +1062,7 @@ LedgerStateParms Ledger::writeBack(LedgerStateParms parms, SLE::ref entry) return lepOKAY; } -SLE::pointer Ledger::getSLE(const uint256& uHash) +SLE::pointer Ledger::getSLE(uint256 const& uHash) { SHAMapItem::pointer node = mAccountStateMap->peekItem(uHash); if (!node) @@ -1000,7 +1070,7 @@ SLE::pointer Ledger::getSLE(const uint256& uHash) return boost::make_shared(node->peekSerializer(), node->getTag()); } -SLE::pointer Ledger::getSLEi(const uint256& uId) +SLE::pointer Ledger::getSLEi(uint256 const& uId) { uint256 hash; @@ -1031,7 +1101,7 @@ void Ledger::visitAccountItems(const uint160& accountID, FUNCTION_TYPEgetType() != ltDIR_NODE)) return; - BOOST_FOREACH(const uint256& uNode, ownerDir->getFieldV256(sfIndexes).peekValue()) + BOOST_FOREACH(uint256 const& uNode, ownerDir->getFieldV256(sfIndexes).peekValue()) { func(getSLEi(uNode)); } @@ -1057,13 +1127,13 @@ uint256 Ledger::getLastLedgerIndex() return node ? node->getTag() : uint256(); } -uint256 Ledger::getNextLedgerIndex(const uint256& uHash) +uint256 Ledger::getNextLedgerIndex(uint256 const& uHash) { SHAMapItem::pointer node = mAccountStateMap->peekNextItem(uHash); return node ? node->getTag() : uint256(); } -uint256 Ledger::getNextLedgerIndex(const uint256& uHash, const uint256& uEnd) +uint256 Ledger::getNextLedgerIndex(uint256 const& uHash, uint256 const& uEnd) { SHAMapItem::pointer node = mAccountStateMap->peekNextItem(uHash); if ((!node) || (node->getTag() > uEnd)) @@ -1071,13 +1141,13 @@ uint256 Ledger::getNextLedgerIndex(const uint256& uHash, const uint256& uEnd) return node->getTag(); } -uint256 Ledger::getPrevLedgerIndex(const uint256& uHash) +uint256 Ledger::getPrevLedgerIndex(uint256 const& uHash) { SHAMapItem::pointer node = mAccountStateMap->peekPrevItem(uHash); return node ? node->getTag() : uint256(); } -uint256 Ledger::getPrevLedgerIndex(const uint256& uHash, const uint256& uBegin) +uint256 Ledger::getPrevLedgerIndex(uint256 const& uHash, uint256 const& uBegin) { SHAMapItem::pointer node = mAccountStateMap->peekNextItem(uHash); if ((!node) || (node->getTag() < uBegin)) @@ -1085,7 +1155,7 @@ uint256 Ledger::getPrevLedgerIndex(const uint256& uHash, const uint256& uBegin) return node->getTag(); } -SLE::pointer Ledger::getASNodeI(const uint256& nodeID, LedgerEntryType let) +SLE::pointer Ledger::getASNodeI(uint256 const& nodeID, LedgerEntryType let) { SLE::pointer node = getSLEi(nodeID); if (node && (node->getType() != let)) @@ -1093,7 +1163,7 @@ SLE::pointer Ledger::getASNodeI(const uint256& nodeID, LedgerEntryType let) return node; } -SLE::pointer Ledger::getASNode(LedgerStateParms& parms, const uint256& nodeID, +SLE::pointer Ledger::getASNode(LedgerStateParms& parms, uint256 const& nodeID, LedgerEntryType let ) { SHAMapItem::pointer account = mAccountStateMap->peekItem(nodeID); @@ -1141,7 +1211,7 @@ SLE::pointer Ledger::getAccountRoot(const RippleAddress& naAccountID) // Directory // -SLE::pointer Ledger::getDirNode(const uint256& uNodeIndex) +SLE::pointer Ledger::getDirNode(uint256 const& uNodeIndex) { return getASNodeI(uNodeIndex, ltDIR_NODE); } @@ -1159,7 +1229,7 @@ SLE::pointer Ledger::getGenerator(const uint160& uGeneratorID) // Nickname // -SLE::pointer Ledger::getNickname(const uint256& uNickname) +SLE::pointer Ledger::getNickname(uint256 const& uNickname) { return getASNodeI(uNickname, ltNICKNAME); } @@ -1169,7 +1239,7 @@ SLE::pointer Ledger::getNickname(const uint256& uNickname) // -SLE::pointer Ledger::getOffer(const uint256& uIndex) +SLE::pointer Ledger::getOffer(uint256 const& uIndex) { return getASNodeI(uIndex, ltOFFER); } @@ -1178,13 +1248,13 @@ SLE::pointer Ledger::getOffer(const uint256& uIndex) // Ripple State // -SLE::pointer Ledger::getRippleState(const uint256& uNode) +SLE::pointer Ledger::getRippleState(uint256 const& uNode) { return getASNodeI(uNode, ltRIPPLE_STATE); } // For an entry put in the 64 bit index or quality. -uint256 Ledger::getQualityIndex(const uint256& uBase, const uint64 uNodeDir) +uint256 Ledger::getQualityIndex(uint256 const& uBase, const uint64 uNodeDir) { // Indexes are stored in big endian format: they print as hex as stored. // Most significant bytes are first. Least significant bytes represent adjacent entries. @@ -1198,12 +1268,12 @@ uint256 Ledger::getQualityIndex(const uint256& uBase, const uint64 uNodeDir) } // Return the last 64 bits. -uint64 Ledger::getQuality(const uint256& uBase) +uint64 Ledger::getQuality(uint256 const& uBase) { return be64toh(((uint64*) uBase.end())[-1]); } -uint256 Ledger::getQualityNext(const uint256& uBase) +uint256 Ledger::getQualityNext(uint256 const& uBase) { static uint256 uNext("10000000000000000"); @@ -1400,7 +1470,7 @@ uint256 Ledger::getBookBase(const uint160& uTakerPaysCurrency, const uint160& uT return uBaseIndex; } -uint256 Ledger::getDirNodeIndex(const uint256& uDirRoot, const uint64 uNodeIndex) +uint256 Ledger::getDirNodeIndex(uint256 const& uDirRoot, const uint64 uNodeIndex) { if (uNodeIndex) { @@ -1431,7 +1501,7 @@ uint256 Ledger::getGeneratorIndex(const uint160& uGeneratorID) // What is important: // --> uNickname: is a Sha256 // <-- SHA512/2: for consistency and speed in generating indexes. -uint256 Ledger::getNicknameIndex(const uint256& uNickname) +uint256 Ledger::getNicknameIndex(uint256 const& uNickname) { Serializer s(34); @@ -1479,51 +1549,68 @@ uint256 Ledger::getRippleStateIndex(const RippleAddress& naA, const RippleAddres bool Ledger::walkLedger() { - std::vector missingNodes1, missingNodes2; - mAccountStateMap->walkMap(missingNodes1, 32); - if (ShouldLog (lsINFO, Ledger) && !missingNodes1.empty()) + std::vector missingNodes1; + std::vector missingNodes2; + + mAccountStateMap->walkMap(missingNodes1, 32); + + if (ShouldLog (lsINFO, Ledger) && !missingNodes1.empty()) { Log(lsINFO) << missingNodes1.size() << " missing account node(s)"; Log(lsINFO) << "First: " << missingNodes1[0]; } - mTransactionMap->walkMap(missingNodes2, 32); - if (ShouldLog (lsINFO, Ledger) && !missingNodes2.empty()) + + mTransactionMap->walkMap(missingNodes2, 32); + + if (ShouldLog (lsINFO, Ledger) && !missingNodes2.empty()) { Log(lsINFO) << missingNodes2.size() << " missing transaction node(s)"; Log(lsINFO) << "First: " << missingNodes2[0]; } + return missingNodes1.empty() && missingNodes2.empty(); } bool Ledger::assertSane() { - if (mHash.isNonZero() && mAccountHash.isNonZero() && mAccountStateMap && mTransactionMap && - (mAccountHash == mAccountStateMap->getHash()) && (mTransHash == mTransactionMap->getHash())) + if (mHash.isNonZero () && + mAccountHash.isNonZero () && + mAccountStateMap && + mTransactionMap && + (mAccountHash == mAccountStateMap->getHash()) && + (mTransHash == mTransactionMap->getHash())) + { return true; + } - Log(lsFATAL) << "ledger is not sane"; - Json::Value j = getJson(0); - j["accountTreeHash"] = mAccountHash.GetHex(); - j["transTreeHash"] = mTransHash.GetHex(); + Log (lsFATAL) << "ledger is not sane"; - assert(false); - return false; + Json::Value j = getJson(0); + + j ["accountTreeHash"] = mAccountHash.GetHex(); + j ["transTreeHash"] = mTransHash.GetHex(); + + assert (false); + + return false; } +// update the skip list with the information from our previous ledger void Ledger::updateSkipList() -{ // update the skip list with the information from our previous ledger - +{ if (mLedgerSeq == 0) // genesis ledger has no previous ledger return; uint32 prevIndex = mLedgerSeq - 1; + // update record of every 256th ledger if ((prevIndex & 0xff) == 0) - { // update record of every 256th ledger + { uint256 hash = getLedgerHashIndex(prevIndex); SLE::pointer skipList = getSLE(hash); std::vector hashes; + // VFALCO TODO Document this skip list concept if (!skipList) skipList = boost::make_shared(ltLEDGER_HASHES, hash); else @@ -1542,14 +1629,19 @@ void Ledger::updateSkipList() // update record of past 256 ledger uint256 hash = getLedgerHashIndex(); - SLE::pointer skipList = getSLE(hash); - std::vector hashes; + + SLE::pointer skipList = getSLE (hash); + + std::vector hashes; + if (!skipList) { skipList = boost::make_shared(ltLEDGER_HASHES, hash); } else + { hashes = skipList->getFieldV256(sfHashes).peekValue(); + } assert(hashes.size() <= 256); if (hashes.size() == 256) @@ -1576,9 +1668,11 @@ void Ledger::pendSave(bool fromConsensus) { if (!fromConsensus && !theApp->getHashRouter ().setFlag (getHash(), SF_SAVED)) return; + assert(isImmutable()); - theApp->getJobQueue().addJob(fromConsensus ? jtPUBLEDGER : jtPUBOLDLEDGER, + theApp->getJobQueue ().addJob ( + fromConsensus ? jtPUBLEDGER : jtPUBOLDLEDGER, fromConsensus ? "Ledger::pendSave" : "Ledger::pendOldSave", BIND_TYPE(&Ledger::saveAcceptedLedger, shared_from_this(), P_1, fromConsensus)); @@ -1601,7 +1695,7 @@ void Ledger::qualityDirDescriber(SLE::ref sle, sle->setFieldU64(sfExchangeRate, uRate); } -void Ledger::zeroFees() +void Ledger::initializeFees () { mBaseFee = 0; mReferenceFeeUnits = 0; diff --git a/src/cpp/ripple/Ledger.h b/src/cpp/ripple/Ledger.h index 05ebf2a49f7..a52a50bc3db 100644 --- a/src/cpp/ripple/Ledger.h +++ b/src/cpp/ripple/Ledger.h @@ -1,16 +1,6 @@ #ifndef RIPPLE_LEDGER_H #define RIPPLE_LEDGER_H -// VFALCO TODO Get this include out of here! -#include "ripple_HashedObject.h" - - -#include "Transaction.h" -#include "TransactionMeta.h" -#include "AccountState.h" -#include "NicknameState.h" -#include "SHAMap.h" - class Job; enum LedgerStateParms @@ -72,7 +62,7 @@ class Ledger : public boost::enable_shared_from_this, public IS_INSTANCE public: Ledger(const RippleAddress& masterID, uint64 startAmount); // used for the starting bootstrap ledger - Ledger(const uint256 &parentHash, const uint256 &transHash, const uint256 &accountHash, + Ledger(uint256 const& parentHash, uint256 const& transHash, uint256 const& accountHash, uint64 totCoins, uint32 closeTime, uint32 parentCloseTime, int closeFlags, int closeResolution, uint32 ledgerSeq, bool& loaded); // used for database ledgers @@ -105,9 +95,9 @@ class Ledger : public boost::enable_shared_from_this, public IS_INSTANCE void setRaw(Serializer& s, bool hasPrefix); uint256 getHash(); - const uint256& getParentHash() const { return mParentHash; } - const uint256& getTransHash() const { return mTransHash; } - const uint256& getAccountHash() const { return mAccountHash; } + uint256 const& getParentHash() const { return mParentHash; } + uint256 const& getTransHash() const { return mTransHash; } + uint256 const& getAccountHash() const { return mAccountHash; } uint64 getTotalCoins() const { return mTotCoins; } void destroyCoins(uint64 fee) { mTotCoins -= fee; } uint32 getCloseTimeNC() const { return mCloseTime; } @@ -140,13 +130,13 @@ class Ledger : public boost::enable_shared_from_this, public IS_INSTANCE bool isAcquiringAS(void); // Transaction Functions - bool addTransaction(const uint256& id, const Serializer& txn); - bool addTransaction(const uint256& id, const Serializer& txn, const Serializer& metaData); - bool hasTransaction(const uint256& TransID) const { return mTransactionMap->hasItem(TransID); } - Transaction::pointer getTransaction(const uint256& transID) const; - bool getTransaction(const uint256& transID, Transaction::pointer& txn, TransactionMetaSet::pointer& txMeta); - bool getTransactionMeta(const uint256& transID, TransactionMetaSet::pointer& txMeta); - bool getMetaHex(const uint256& transID, std::string& hex); + bool addTransaction(uint256 const& id, const Serializer& txn); + bool addTransaction(uint256 const& id, const Serializer& txn, const Serializer& metaData); + bool hasTransaction(uint256 const& TransID) const { return mTransactionMap->hasItem(TransID); } + Transaction::pointer getTransaction(uint256 const& transID) const; + bool getTransaction(uint256 const& transID, Transaction::pointer& txn, TransactionMetaSet::pointer& txMeta); + bool getTransactionMeta(uint256 const& transID, TransactionMetaSet::pointer& txMeta); + bool getMetaHex(uint256 const& transID, std::string& hex); static SerializedTransaction::pointer getSTransaction(SHAMapItem::ref, SHAMapTreeNode::TNType); SerializedTransaction::pointer getSMTransaction(SHAMapItem::ref, SHAMapTreeNode::TNType, @@ -163,21 +153,21 @@ class Ledger : public boost::enable_shared_from_this, public IS_INSTANCE // database functions (low-level) static Ledger::pointer loadByIndex(uint32 ledgerIndex); - static Ledger::pointer loadByHash(const uint256& ledgerHash); + static Ledger::pointer loadByHash(uint256 const& ledgerHash); static uint256 getHashByIndex(uint32 index); static bool getHashesByIndex(uint32 index, uint256& ledgerHash, uint256& parentHash); static std::map< uint32, std::pair > getHashesByIndex(uint32 minSeq, uint32 maxSeq); void pendSave(bool fromConsensus); // next/prev function - SLE::pointer getSLE(const uint256& uHash); // SLE is mutable - SLE::pointer getSLEi(const uint256& uHash); // SLE is immutable + SLE::pointer getSLE(uint256 const& uHash); // SLE is mutable + SLE::pointer getSLEi(uint256 const& uHash); // SLE is immutable uint256 getFirstLedgerIndex(); uint256 getLastLedgerIndex(); - uint256 getNextLedgerIndex(const uint256& uHash); // first node >hash - uint256 getNextLedgerIndex(const uint256& uHash, const uint256& uEnd); // first node >hash, begin + uint256 getNextLedgerIndex(uint256 const& uHash); // first node >hash + uint256 getNextLedgerIndex(uint256 const& uHash, uint256 const& uEnd); // first node >hash, begin // Ledger hash table function static uint256 getLedgerHashIndex(); @@ -215,14 +205,14 @@ class Ledger : public boost::enable_shared_from_this, public IS_INSTANCE static uint256 getNicknameHash(const std::string& strNickname) { Serializer s(strNickname); return s.getSHA256(); } - NicknameState::pointer getNicknameState(const uint256& uNickname); + NicknameState::pointer getNicknameState(uint256 const& uNickname); NicknameState::pointer getNicknameState(const std::string& strNickname) { return getNicknameState(getNicknameHash(strNickname)); } - SLE::pointer getNickname(const uint256& uNickname); + SLE::pointer getNickname(uint256 const& uNickname); SLE::pointer getNickname(const std::string& strNickname) { return getNickname(getNicknameHash(strNickname)); } - static uint256 getNicknameIndex(const uint256& uNickname); + static uint256 getNicknameIndex(uint256 const& uNickname); // // Order book functions @@ -239,7 +229,7 @@ class Ledger : public boost::enable_shared_from_this, public IS_INSTANCE // Offer functions // - SLE::pointer getOffer(const uint256& uIndex); + SLE::pointer getOffer(uint256 const& uIndex); SLE::pointer getOffer(const uint160& uAccountID, uint32 uSequence) { return getOffer(getOfferIndex(uAccountID, uSequence)); } @@ -259,19 +249,19 @@ class Ledger : public boost::enable_shared_from_this, public IS_INSTANCE // Directories are doubly linked lists of nodes. // Given a directory root and and index compute the index of a node. - static uint256 getDirNodeIndex(const uint256& uDirRoot, const uint64 uNodeIndex = 0); + static uint256 getDirNodeIndex(uint256 const& uDirRoot, const uint64 uNodeIndex = 0); static void ownerDirDescriber(SLE::ref, const uint160& owner); // Return a node: root or normal - SLE::pointer getDirNode(const uint256& uNodeIndex); + SLE::pointer getDirNode(uint256 const& uNodeIndex); // // Quality // - static uint256 getQualityIndex(const uint256& uBase, const uint64 uNodeDir = 0); - static uint256 getQualityNext(const uint256& uBase); - static uint64 getQuality(const uint256& uBase); + static uint256 getQualityIndex(uint256 const& uBase, const uint64 uNodeDir = 0); + static uint256 getQualityNext(uint256 const& uBase); + static uint64 getQuality(uint256 const& uBase); static void qualityDirDescriber(SLE::ref, const uint160& uTakerPaysCurrency, const uint160& uTakerPaysIssuer, const uint160& uTakerGetsCurrency, const uint160& uTakerGetsIssuer, @@ -286,7 +276,7 @@ class Ledger : public boost::enable_shared_from_this, public IS_INSTANCE static uint256 getRippleStateIndex(const uint160& uiA, const uint160& uiB, const uint160& uCurrency) { return getRippleStateIndex(RippleAddress::createAccountID(uiA), RippleAddress::createAccountID(uiB), uCurrency); } - SLE::pointer getRippleState(const uint256& uNode); + SLE::pointer getRippleState(uint256 const& uNode); SLE::pointer getRippleState(const RippleAddress& naA, const RippleAddress& naB, const uint160& uCurrency) { return getRippleState(getRippleStateIndex(naA, naB, uCurrency)); } @@ -329,18 +319,23 @@ class Ledger : public boost::enable_shared_from_this, public IS_INSTANCE bool assertSane(); protected: - SLE::pointer getASNode(LedgerStateParms& parms, const uint256& nodeID, LedgerEntryType let); + SLE::pointer getASNode(LedgerStateParms& parms, uint256 const& nodeID, LedgerEntryType let); // returned SLE is immutable - SLE::pointer getASNodeI(const uint256& nodeID, LedgerEntryType let); + SLE::pointer getASNodeI(uint256 const& nodeID, LedgerEntryType let); void saveAcceptedLedger(Job&, bool fromConsensus); void updateFees(); - void zeroFees(); private: - uint256 mHash, mParentHash, mTransHash, mAccountHash; + void initializeFees (); + +private: + uint256 mHash; + uint256 mParentHash; + uint256 mTransHash; + uint256 mAccountHash; uint64 mTotCoins; uint32 mLedgerSeq; uint32 mCloseTime; // when this ledger closed @@ -353,7 +348,8 @@ class Ledger : public boost::enable_shared_from_this, public IS_INSTANCE uint32 mReserveBase, mReserveIncrement; // Reserve basse and increment in fee units uint64 mBaseFee; // Ripple cost of the reference transaction - SHAMap::pointer mTransactionMap, mAccountStateMap; + SHAMap::pointer mTransactionMap; + SHAMap::pointer mAccountStateMap; mutable boost::recursive_mutex mLock; diff --git a/src/cpp/ripple/LedgerAcquire.h b/src/cpp/ripple/LedgerAcquire.h deleted file mode 100644 index 831da03cb5d..00000000000 --- a/src/cpp/ripple/LedgerAcquire.h +++ /dev/null @@ -1,168 +0,0 @@ -#ifndef __LEDGERACQUIRE__ -#define __LEDGERACQUIRE__ - -#include -#include -#include -#include - -#include -#include -#include -#include -#include -#include - -#include "Ledger.h" - -// How long before we try again to acquire the same ledger -#ifndef LEDGER_REACQUIRE_INTERVAL -#define LEDGER_REACQUIRE_INTERVAL 600 -#endif - -DEFINE_INSTANCE(LedgerAcquire); - -class PeerSet -{ -public: - const uint256& getHash() const { return mHash; } - bool isComplete() const { return mComplete; } - bool isFailed() const { return mFailed; } - int getTimeouts() const { return mTimeouts; } - - bool isActive(); - void progress() { mProgress = true; mAggressive = false; } - bool isProgress() { return mProgress; } - void touch() { mLastAction = UptimeTimer::getInstance().getElapsedSeconds(); } - int getLastAction() { return mLastAction; } - - void peerHas(Peer::ref); - void badPeer(Peer::ref); - void setTimer(); - - int takePeerSetFrom(const PeerSet& s); - int getPeerCount() const; - virtual bool isDone() const { return mComplete || mFailed; } - -protected: - virtual void newPeer(Peer::ref) = 0; - virtual void onTimer(bool progress) = 0; - virtual boost::weak_ptr pmDowncast() = 0; - - void setComplete() { mComplete = true; } - void setFailed() { mFailed = true; } - void invokeOnTimer(); - -protected: - uint256 mHash; - int mTimerInterval, mTimeouts; - bool mComplete, mFailed, mProgress, mAggressive; - int mLastAction; - - boost::recursive_mutex mLock; - boost::asio::deadline_timer mTimer; - boost::unordered_map mPeers; - - PeerSet(const uint256& hash, int interval); - virtual ~PeerSet() { ; } - - void sendRequest(const ripple::TMGetLedger& message); - void sendRequest(const ripple::TMGetLedger& message, Peer::ref peer); - -private: - static void TimerEntry(boost::weak_ptr, const boost::system::error_code& result); - static void TimerJobEntry(Job&, boost::shared_ptr); -}; - -class LedgerAcquire : - private IS_INSTANCE(LedgerAcquire), public PeerSet, public boost::enable_shared_from_this -{ // A ledger we are trying to acquire -public: - typedef boost::shared_ptr pointer; - -protected: - Ledger::pointer mLedger; - bool mHaveBase, mHaveState, mHaveTransactions, mAborted, mSignaled, mAccept, mByHash; - int mWaitCount; - uint32 mSeq; - - std::set mRecentTXNodes; - std::set mRecentASNodes; - - std::vector mRecentPeers; - - std::vector< FUNCTION_TYPE > mOnComplete; - - void done(); - void onTimer(bool progress); - - void newPeer(Peer::ref peer) { trigger(peer); } - - boost::weak_ptr pmDowncast(); - -public: - LedgerAcquire(const uint256& hash, uint32 seq); - virtual ~LedgerAcquire() { ; } - - bool isBase() const { return mHaveBase; } - bool isAcctStComplete() const { return mHaveState; } - bool isTransComplete() const { return mHaveTransactions; } - bool isDone() const { return mAborted || isComplete() || isFailed(); } - Ledger::ref getLedger() { return mLedger; } - void abort() { mAborted = true; } - bool setAccept() { if (mAccept) return false; mAccept = true; return true; } - - bool addOnComplete(FUNCTION_TYPE); - - bool takeBase(const std::string& data); - bool takeTxNode(const std::list& IDs, const std::list& data, - SMAddNode&); - bool takeTxRootNode(Blob const& data, SMAddNode&); - bool takeAsNode(const std::list& IDs, const std::list& data, - SMAddNode&); - bool takeAsRootNode(Blob const& data, SMAddNode&); - void trigger(Peer::ref); - bool tryLocal(); - void addPeers(); - void awaitData(); - void noAwaitData(); - void checkLocal(); - - typedef std::pair neededHash_t; - std::vector getNeededHashes(); - - static void filterNodes(std::vector& nodeIDs, std::vector& nodeHashes, - std::set& recentNodes, int max, bool aggressive); - - Json::Value getJson(int); -}; - -class LedgerAcquireMaster -{ -protected: - boost::mutex mLock; - std::map mLedgers; - KeyCache mRecentFailures; - -public: - LedgerAcquireMaster() : mRecentFailures("LedgerAcquireRecentFailures", 0, LEDGER_REACQUIRE_INTERVAL) { ; } - - LedgerAcquire::pointer findCreate(const uint256& hash, uint32 seq); - LedgerAcquire::pointer find(const uint256& hash); - bool hasLedger(const uint256& ledgerHash); - void dropLedger(const uint256& ledgerHash); - - bool awaitLedgerData(const uint256& ledgerHash); - void gotLedgerData(Job&, uint256 hash, boost::shared_ptr packet, boost::weak_ptr peer); - - int getFetchCount(int& timeoutCount); - void logFailure(const uint256& h) { mRecentFailures.add(h); } - bool isFailure(const uint256& h) { return mRecentFailures.isPresent(h, false); } - - void gotFetchPack(Job&); - void sweep(); -}; - -#endif - -// vim:ts=4 diff --git a/src/cpp/ripple/LedgerConsensus.cpp b/src/cpp/ripple/LedgerConsensus.cpp index f9c9e162932..0c2f9a37e68 100644 --- a/src/cpp/ripple/LedgerConsensus.cpp +++ b/src/cpp/ripple/LedgerConsensus.cpp @@ -126,7 +126,7 @@ Json::Value LCTransaction::getJson() return ret; } -LedgerConsensus::LedgerConsensus(const uint256& prevLCLHash, Ledger::ref previousLedger, uint32 closeTime) +LedgerConsensus::LedgerConsensus(uint256 const& prevLCLHash, Ledger::ref previousLedger, uint32 closeTime) : mState(lcsPRE_CLOSE), mCloseTime(closeTime), mPrevLedgerHash(prevLCLHash), mPreviousLedger(previousLedger), mValPublic(theConfig.VALIDATION_PUB), mValPrivate(theConfig.VALIDATION_PRIV), mConsensusFail(false), mCurrentMSeconds(0), mClosePercent(0), mHaveCloseTimeConsensus(false), @@ -253,7 +253,7 @@ void LedgerConsensus::checkLCL() handleLCL(netLgr); } -void LedgerConsensus::handleLCL(const uint256& lclHash) +void LedgerConsensus::handleLCL(uint256 const& lclHash) { assert((lclHash != mPrevLedgerHash) || (mPreviousLedger->getHash() != lclHash)); if (mPrevLedgerHash != lclHash) @@ -352,7 +352,7 @@ void LedgerConsensus::takeInitialPosition(Ledger& initialLedger) propose(); } -bool LedgerConsensus::stillNeedTXSet(const uint256& hash) +bool LedgerConsensus::stillNeedTXSet(uint256 const& hash) { if (mAcquired.find(hash) != mAcquired.end()) return false; @@ -366,10 +366,10 @@ bool LedgerConsensus::stillNeedTXSet(const uint256& hash) void LedgerConsensus::createDisputes(SHAMap::ref m1, SHAMap::ref m2) { - SHAMap::SHAMapDiff differences; + SHAMap::Delta differences; m1->compare(m2, differences, 16384); - typedef std::map::value_type u256_diff_pair; + typedef std::map::value_type u256_diff_pair; BOOST_FOREACH (u256_diff_pair& pos, differences) { // create disputed transactions (from the ledger that has them) if (pos.second.first) @@ -387,7 +387,7 @@ void LedgerConsensus::createDisputes(SHAMap::ref m1, SHAMap::ref m2) } } -void LedgerConsensus::mapComplete(const uint256& hash, SHAMap::ref map, bool acquired) +void LedgerConsensus::mapComplete(uint256 const& hash, SHAMap::ref map, bool acquired) { CondLog (acquired, lsINFO, LedgerConsensus) << "We have acquired TXS " << hash; @@ -444,7 +444,7 @@ void LedgerConsensus::mapComplete(const uint256& hash, SHAMap::ref map, bool acq sendHaveTxSet(hash, true); } -void LedgerConsensus::sendHaveTxSet(const uint256& hash, bool direct) +void LedgerConsensus::sendHaveTxSet(uint256 const& hash, bool direct) { ripple::TMHaveTransactionSet msg; msg.set_hash(hash.begin(), 256 / 8); @@ -746,7 +746,7 @@ bool LedgerConsensus::haveConsensus(bool forReal) mPreviousMSeconds, mCurrentMSeconds, forReal, mConsensusFail); } -SHAMap::pointer LedgerConsensus::getTransactionTree(const uint256& hash, bool doAcquire) +SHAMap::pointer LedgerConsensus::getTransactionTree(uint256 const& hash, bool doAcquire) { boost::unordered_map::iterator it = mAcquired.find(hash); if (it != mAcquired.end()) @@ -832,7 +832,7 @@ void LedgerConsensus::propose() boost::make_shared(prop, ripple::mtPROPOSE_LEDGER)); } -void LedgerConsensus::addDisputedTransaction(const uint256& txID, Blob const& tx) +void LedgerConsensus::addDisputedTransaction(uint256 const& txID, Blob const& tx) { if (mDisputes.find(txID) != mDisputes.end()) return; @@ -924,7 +924,7 @@ bool LedgerConsensus::peerPosition(LedgerProposal::ref newPosition) return true; } -bool LedgerConsensus::peerHasSet(Peer::ref peer, const uint256& hashSet, ripple::TxSetStatus status) +bool LedgerConsensus::peerHasSet(Peer::ref peer, uint256 const& hashSet, ripple::TxSetStatus status) { if (status != ripple::tsHAVE) // Indirect requests are for future support return true; @@ -944,14 +944,14 @@ bool LedgerConsensus::peerHasSet(Peer::ref peer, const uint256& hashSet, ripple: return true; } -SMAddNode LedgerConsensus::peerGaveNodes(Peer::ref peer, const uint256& setHash, +SHAMapAddNode LedgerConsensus::peerGaveNodes(Peer::ref peer, uint256 const& setHash, const std::list& nodeIDs, const std::list< Blob >& nodeData) { boost::unordered_map::iterator acq = mAcquiring.find(setHash); if (acq == mAcquiring.end()) { WriteLog (lsDEBUG, LedgerConsensus) << "Got TX data for set no longer acquiring: " << setHash; - return SMAddNode(); + return SHAMapAddNode(); } TransactionAcquire::pointer set = acq->second; // We must keep the set around during the function return set->takeNodes(nodeIDs, nodeData, peer); @@ -1185,8 +1185,8 @@ void LedgerConsensus::accept(SHAMap::ref set, LoadEvent::pointer) applyTransactions(set, newLCL, newLCL, failedTransactions, false); newLCL->updateSkipList(); newLCL->setClosed(); - boost::shared_ptr acctNodes = newLCL->peekAccountStateMap()->disarmDirty(); - boost::shared_ptr txnNodes = newLCL->peekTransactionMap()->disarmDirty(); + boost::shared_ptr acctNodes = newLCL->peekAccountStateMap()->disarmDirty(); + boost::shared_ptr txnNodes = newLCL->peekTransactionMap()->disarmDirty(); // write out dirty nodes (temporarily done here) Most come before setAccepted int fc; diff --git a/src/cpp/ripple/LedgerConsensus.h b/src/cpp/ripple/LedgerConsensus.h index 514fbc642c8..17fb8997355 100644 --- a/src/cpp/ripple/LedgerConsensus.h +++ b/src/cpp/ripple/LedgerConsensus.h @@ -1,17 +1,8 @@ #ifndef __LEDGER_CONSENSUS__ #define __LEDGER_CONSENSUS__ -#include -#include - -#include -#include -#include - #include "Transaction.h" -#include "LedgerAcquire.h" #include "LedgerProposal.h" -#include "CanonicalTXSet.h" #include "TransactionEngine.h" DEFINE_INSTANCE(LedgerConsensus); @@ -27,12 +18,12 @@ class TransactionAcquire typedef boost::shared_ptr pointer; public: - TransactionAcquire(const uint256& hash); + TransactionAcquire(uint256 const& hash); virtual ~TransactionAcquire() { ; } SHAMap::ref getMap() { return mMap; } - SMAddNode takeNodes(const std::list& IDs, + SHAMapAddNode takeNodes(const std::list& IDs, const std::list< Blob >& data, Peer::ref); private: @@ -53,10 +44,10 @@ class LCTransaction public: typedef boost::shared_ptr pointer; - LCTransaction(const uint256 &txID, Blob const& tx, bool ourVote) : + LCTransaction(uint256 const& txID, Blob const& tx, bool ourVote) : mTransactionID(txID), mYays(0), mNays(0), mOurVote(ourVote), transaction(tx) { ; } - const uint256& getTransactionID() const { return mTransactionID; } + uint256 const& getTransactionID() const { return mTransactionID; } bool getOurVote() const { return mOurVote; } Serializer& peekTransaction() { return transaction; } void setOurVote(bool o) { mOurVote = o; } @@ -86,7 +77,7 @@ enum LCState class LedgerConsensus : public boost::enable_shared_from_this, IS_INSTANCE(LedgerConsensus) { public: - LedgerConsensus(const uint256& prevLCLHash, Ledger::ref previousLedger, uint32 closeTime); + LedgerConsensus(uint256 const& prevLCLHash, Ledger::ref previousLedger, uint32 closeTime); int startup(); Json::Value getJson(bool full); @@ -94,12 +85,12 @@ class LedgerConsensus : public boost::enable_shared_from_this, Ledger::ref peekPreviousLedger() { return mPreviousLedger; } uint256 getLCL() { return mPrevLedgerHash; } - SHAMap::pointer getTransactionTree(const uint256& hash, bool doAcquire); - TransactionAcquire::pointer getAcquiring(const uint256& hash); - void mapComplete(const uint256& hash, SHAMap::ref map, bool acquired); - bool stillNeedTXSet(const uint256& hash); + SHAMap::pointer getTransactionTree(uint256 const& hash, bool doAcquire); + TransactionAcquire::pointer getAcquiring(uint256 const& hash); + void mapComplete(uint256 const& hash, SHAMap::ref map, bool acquired); + bool stillNeedTXSet(uint256 const& hash); void checkLCL(); - void handleLCL(const uint256& lclHash); + void handleLCL(uint256 const& lclHash); void timerEntry(); @@ -114,9 +105,9 @@ class LedgerConsensus : public boost::enable_shared_from_this, bool peerPosition(LedgerProposal::ref); - bool peerHasSet(Peer::ref peer, const uint256& set, ripple::TxSetStatus status); + bool peerHasSet(Peer::ref peer, uint256 const& set, ripple::TxSetStatus status); - SMAddNode peerGaveNodes(Peer::ref peer, const uint256& setHash, + SHAMapAddNode peerGaveNodes(Peer::ref peer, uint256 const& setHash, const std::list& nodeIDs, const std::list< Blob >& nodeData); bool isOurPubKey(const RippleAddress &k) { return k == mValPublic; } @@ -128,18 +119,18 @@ class LedgerConsensus : public boost::enable_shared_from_this, // final accept logic void accept(SHAMap::ref txSet, LoadEvent::pointer); - void weHave(const uint256& id, Peer::ref avoidPeer); + void weHave(uint256 const& id, Peer::ref avoidPeer); void startAcquiring(TransactionAcquire::pointer); - SHAMap::pointer find(const uint256& hash); + SHAMap::pointer find(uint256 const& hash); void createDisputes(SHAMap::ref, SHAMap::ref); - void addDisputedTransaction(const uint256&, Blob const& transaction); + void addDisputedTransaction(uint256 const& , Blob const& transaction); void adjustCount(SHAMap::ref map, const std::vector& peers); void propose(); void addPosition(LedgerProposal&, bool ours); void removePosition(LedgerProposal&, bool ours); - void sendHaveTxSet(const uint256& set, bool direct); + void sendHaveTxSet(uint256 const& set, bool direct); void applyTransactions(SHAMap::ref transactionSet, Ledger::ref targetLedger, Ledger::ref checkLedger, CanonicalTXSet& failedTransactions, bool openLgr); int applyTransaction(TransactionEngine& engine, SerializedTransaction::ref txn, Ledger::ref targetLedger, diff --git a/src/cpp/ripple/LedgerEntrySet.cpp b/src/cpp/ripple/LedgerEntrySet.cpp index 47625ac6e19..9c8cd743986 100644 --- a/src/cpp/ripple/LedgerEntrySet.cpp +++ b/src/cpp/ripple/LedgerEntrySet.cpp @@ -9,7 +9,7 @@ DECLARE_INSTANCE(LedgerEntrySet) #define DIR_NODE_MAX 32 -void LedgerEntrySet::init(Ledger::ref ledger, const uint256& transactionID, +void LedgerEntrySet::init(Ledger::ref ledger, uint256 const& transactionID, uint32 ledgerID, TransactionEngineParams params) { mEntries.clear(); @@ -50,7 +50,7 @@ void LedgerEntrySet::swapWith(LedgerEntrySet& e) // Find an entry in the set. If it has the wrong sequence number, copy it and update the sequence number. // This is basically: copy-on-read. -SLE::pointer LedgerEntrySet::getEntry(const uint256& index, LedgerEntryAction& action) +SLE::pointer LedgerEntrySet::getEntry(uint256 const& index, LedgerEntryAction& action) { std::map::iterator it = mEntries.find(index); if (it == mEntries.end()) @@ -68,7 +68,7 @@ SLE::pointer LedgerEntrySet::getEntry(const uint256& index, LedgerEntryAction& a return it->second.mEntry; } -SLE::pointer LedgerEntrySet::entryCreate(LedgerEntryType letType, const uint256& index) +SLE::pointer LedgerEntrySet::entryCreate(LedgerEntryType letType, uint256 const& index) { assert(index.isNonZero()); SLE::pointer sleNew = boost::make_shared(letType, index); @@ -76,7 +76,7 @@ SLE::pointer LedgerEntrySet::entryCreate(LedgerEntryType letType, const uint256& return sleNew; } -SLE::pointer LedgerEntrySet::entryCache(LedgerEntryType letType, const uint256& index) +SLE::pointer LedgerEntrySet::entryCache(LedgerEntryType letType, uint256 const& index) { assert(mLedger); SLE::pointer sleEntry; @@ -97,7 +97,7 @@ SLE::pointer LedgerEntrySet::entryCache(LedgerEntryType letType, const uint256& return sleEntry; } -LedgerEntryAction LedgerEntrySet::hasEntry(const uint256& index) const +LedgerEntryAction LedgerEntrySet::hasEntry(uint256 const& index) const { std::map::const_iterator it = mEntries.find(index); if (it == mEntries.end()) @@ -288,7 +288,7 @@ Json::Value LedgerEntrySet::getJson(int) const return ret; } -SLE::pointer LedgerEntrySet::getForMod(const uint256& node, Ledger::ref ledger, +SLE::pointer LedgerEntrySet::getForMod(uint256 const& node, Ledger::ref ledger, boost::unordered_map& newMods) { std::map::iterator it = mEntries.find(node); @@ -506,7 +506,7 @@ void LedgerEntrySet::calcRawMeta(Serializer& s, TER result, uint32 index) WriteLog (lsTRACE, LedgerEntrySet) << "Metadata:" << mSet.getJson(0); } -TER LedgerEntrySet::dirCount(const uint256& uRootIndex, uint32& uCount) +TER LedgerEntrySet::dirCount(uint256 const& uRootIndex, uint32& uCount) { uint64 uNodeDir = 0; @@ -542,8 +542,8 @@ TER LedgerEntrySet::dirCount(const uint256& uRootIndex, uint32& uCount) // Within a node with no deletions order of elements is sequential. Otherwise, order of elements is random. TER LedgerEntrySet::dirAdd( uint64& uNodeDir, - const uint256& uRootIndex, - const uint256& uLedgerIndex, + uint256 const& uRootIndex, + uint256 const& uLedgerIndex, FUNCTION_TYPE fDescriber) { WriteLog (lsTRACE, LedgerEntrySet) << boost::str(boost::format("dirAdd: uRootIndex=%s uLedgerIndex=%s") @@ -642,8 +642,8 @@ TER LedgerEntrySet::dirAdd( TER LedgerEntrySet::dirDelete( const bool bKeepRoot, // --> True, if we never completely clean up, after we overflow the root node. const uint64& uNodeDir, // --> Node containing entry. - const uint256& uRootIndex, // --> The index of the base of the directory. Nodes are based off of this. - const uint256& uLedgerIndex, // --> Value to remove from directory. + uint256 const& uRootIndex, // --> The index of the base of the directory. Nodes are based off of this. + uint256 const& uLedgerIndex, // --> Value to remove from directory. const bool bStable, // --> True, not to change relative order of entries. const bool bSoft) // --> True, uNodeDir is not hard and fast (pass uNodeDir=0). { @@ -847,7 +847,7 @@ TER LedgerEntrySet::dirDelete( // Return the first entry and advance uDirEntry. // <-- true, if had a next entry. bool LedgerEntrySet::dirFirst( - const uint256& uRootIndex, // --> Root of directory. + uint256 const& uRootIndex, // --> Root of directory. SLE::pointer& sleNode, // <-- current node unsigned int& uDirEntry, // <-- next entry uint256& uEntryIndex) // <-- The entry, if available. Otherwise, zero. @@ -863,7 +863,7 @@ bool LedgerEntrySet::dirFirst( // Return the current entry and advance uDirEntry. // <-- true, if had a next entry. bool LedgerEntrySet::dirNext( - const uint256& uRootIndex, // --> Root of directory + uint256 const& uRootIndex, // --> Root of directory SLE::pointer& sleNode, // <-> current node unsigned int& uDirEntry, // <-> next entry uint256& uEntryIndex) // <-- The entry, if available. Otherwise, zero. @@ -896,7 +896,7 @@ bool LedgerEntrySet::dirNext( return true; } -uint256 LedgerEntrySet::getNextLedgerIndex(const uint256& uHash) +uint256 LedgerEntrySet::getNextLedgerIndex(uint256 const& uHash) { // find next node in ledger that isn't deleted by LES uint256 ledgerNext = uHash; @@ -921,7 +921,7 @@ uint256 LedgerEntrySet::getNextLedgerIndex(const uint256& uHash) return ledgerNext; } -uint256 LedgerEntrySet::getNextLedgerIndex(const uint256& uHash, const uint256& uEnd) +uint256 LedgerEntrySet::getNextLedgerIndex(uint256 const& uHash, uint256 const& uEnd) { uint256 next = getNextLedgerIndex(uHash); if (next > uEnd) @@ -953,7 +953,7 @@ void LedgerEntrySet::ownerCountAdjust(const uint160& uOwnerID, int iAmount, SLE: } } -TER LedgerEntrySet::offerDelete(SLE::ref sleOffer, const uint256& uOfferIndex, const uint160& uOwnerID) +TER LedgerEntrySet::offerDelete(SLE::ref sleOffer, uint256 const& uOfferIndex, const uint160& uOwnerID) { bool bOwnerNode = sleOffer->isFieldPresent(sfOwnerNode); // Detect legacy dirs. uint64 uOwnerNode = sleOffer->getFieldU64(sfOwnerNode); @@ -975,7 +975,7 @@ TER LedgerEntrySet::offerDelete(SLE::ref sleOffer, const uint256& uOfferIndex, c return terResult; } -TER LedgerEntrySet::offerDelete(const uint256& uOfferIndex) +TER LedgerEntrySet::offerDelete(uint256 const& uOfferIndex) { SLE::pointer sleOffer = entryCache(ltOFFER, uOfferIndex); if (!sleOffer) @@ -1241,7 +1241,7 @@ TER LedgerEntrySet::trustCreate( const bool bSrcHigh, const uint160& uSrcAccountID, const uint160& uDstAccountID, - const uint256& uIndex, // --> ripple state entry + uint256 const& uIndex, // --> ripple state entry SLE::ref sleAccount, // --> the account being set. const bool bAuth, // --> authorize account. const STAmount& saBalance, // --> balance of account being set. Issuer should be ACCOUNT_ONE diff --git a/src/cpp/ripple/LedgerEntrySet.h b/src/cpp/ripple/LedgerEntrySet.h index 2d956eff296..a784b34e802 100644 --- a/src/cpp/ripple/LedgerEntrySet.h +++ b/src/cpp/ripple/LedgerEntrySet.h @@ -1,12 +1,6 @@ #ifndef __LEDGERENTRYSET__ #define __LEDGERENTRYSET__ -#include - -#include "SerializedLedger.h" -#include "TransactionMeta.h" -#include "Ledger.h" - DEFINE_INSTANCE(LedgerEntrySetEntry); DEFINE_INSTANCE(LedgerEntrySet); @@ -66,15 +60,15 @@ class LedgerEntrySet : private IS_INSTANCE(LedgerEntrySet) int getSeq() const { return mSeq; } TransactionEngineParams getParams() const { return mParams; } void bumpSeq() { ++mSeq; } - void init(Ledger::ref ledger, const uint256& transactionID, uint32 ledgerID, TransactionEngineParams params); + void init(Ledger::ref ledger, uint256 const& transactionID, uint32 ledgerID, TransactionEngineParams params); void clear(); Ledger::pointer& getLedger() { return mLedger; } Ledger::ref getLedgerRef() const { return mLedger; } // basic entry functions - SLE::pointer getEntry(const uint256& index, LedgerEntryAction&); - LedgerEntryAction hasEntry(const uint256& index) const; + SLE::pointer getEntry(uint256 const& index, LedgerEntryAction&); + LedgerEntryAction hasEntry(uint256 const& index) const; void entryCache(SLE::ref); // Add this entry to the cache void entryCreate(SLE::ref); // This entry will be created void entryDelete(SLE::ref); // This entry will be deleted @@ -82,36 +76,36 @@ class LedgerEntrySet : private IS_INSTANCE(LedgerEntrySet) bool hasChanges(); // True if LES has any changes // higher-level ledger functions - SLE::pointer entryCreate(LedgerEntryType letType, const uint256& uIndex); - SLE::pointer entryCache(LedgerEntryType letType, const uint256& uIndex); + SLE::pointer entryCreate(LedgerEntryType letType, uint256 const& uIndex); + SLE::pointer entryCache(LedgerEntryType letType, uint256 const& uIndex); // Directory functions. TER dirAdd( uint64& uNodeDir, // Node of entry. - const uint256& uRootIndex, - const uint256& uLedgerIndex, + uint256 const& uRootIndex, + uint256 const& uLedgerIndex, FUNCTION_TYPE fDescriber); TER dirDelete( const bool bKeepRoot, const uint64& uNodeDir, // Node item is mentioned in. - const uint256& uRootIndex, - const uint256& uLedgerIndex, // Item being deleted + uint256 const& uRootIndex, + uint256 const& uLedgerIndex, // Item being deleted const bool bStable, const bool bSoft); - bool dirFirst(const uint256& uRootIndex, SLE::pointer& sleNode, unsigned int& uDirEntry, uint256& uEntryIndex); - bool dirNext(const uint256& uRootIndex, SLE::pointer& sleNode, unsigned int& uDirEntry, uint256& uEntryIndex); - TER dirCount(const uint256& uDirIndex, uint32& uCount); + bool dirFirst(uint256 const& uRootIndex, SLE::pointer& sleNode, unsigned int& uDirEntry, uint256& uEntryIndex); + bool dirNext(uint256 const& uRootIndex, SLE::pointer& sleNode, unsigned int& uDirEntry, uint256& uEntryIndex); + TER dirCount(uint256 const& uDirIndex, uint32& uCount); - uint256 getNextLedgerIndex(const uint256& uHash); - uint256 getNextLedgerIndex(const uint256& uHash, const uint256& uEnd); + uint256 getNextLedgerIndex(uint256 const& uHash); + uint256 getNextLedgerIndex(uint256 const& uHash, uint256 const& uEnd); void ownerCountAdjust(const uint160& uOwnerID, int iAmount, SLE::ref sleAccountRoot=SLE::pointer()); // Offer functions. - TER offerDelete(const uint256& uOfferIndex); - TER offerDelete(SLE::ref sleOffer, const uint256& uOfferIndex, const uint160& uOwnerID); + TER offerDelete(uint256 const& uOfferIndex); + TER offerDelete(SLE::ref sleOffer, uint256 const& uOfferIndex, const uint160& uOwnerID); // Balance functions. uint32 rippleTransferRate(const uint160& uIssuerID); @@ -136,7 +130,7 @@ class LedgerEntrySet : private IS_INSTANCE(LedgerEntrySet) const bool bSrcHigh, const uint160& uSrcAccountID, const uint160& uDstAccountID, - const uint256& uIndex, + uint256 const& uIndex, SLE::ref sleAccount, const bool bAuth, const STAmount& saSrcBalance, @@ -171,7 +165,7 @@ class LedgerEntrySet : private IS_INSTANCE(LedgerEntrySet) const TransactionMetaSet& s, int m) : mLedger(ledger), mEntries(e), mSet(s), mParams(tapNONE), mSeq(m), mImmutable(false) { ; } - SLE::pointer getForMod(const uint256& node, Ledger::ref ledger, + SLE::pointer getForMod(uint256 const& node, Ledger::ref ledger, boost::unordered_map& newMods); bool threadTx(const RippleAddress& threadTo, Ledger::ref ledger, diff --git a/src/cpp/ripple/LedgerHistory.h b/src/cpp/ripple/LedgerHistory.h deleted file mode 100644 index 6543c8615bf..00000000000 --- a/src/cpp/ripple/LedgerHistory.h +++ /dev/null @@ -1,26 +0,0 @@ -#ifndef __LEDGERHISTORY__ -#define __LEDGERHISTORY__ - -#include "Ledger.h" - -class LedgerHistory -{ - TaggedCache mLedgersByHash; - std::map mLedgersByIndex; // accepted ledgers - -public: - LedgerHistory(); - - void addLedger(Ledger::pointer ledger); - void addAcceptedLedger(Ledger::pointer ledger, bool fromConsensus); - - float getCacheHitRate() { return mLedgersByHash.getHitRate(); } - uint256 getLedgerHash(uint32 index); - Ledger::pointer getLedgerBySeq(uint32 index); - Ledger::pointer getLedgerByHash(const uint256& hash); - Ledger::pointer canonicalizeLedger(Ledger::pointer, bool cache); - void tune(int size, int age); - void sweep() { mLedgersByHash.sweep(); } -}; - -#endif diff --git a/src/cpp/ripple/LedgerMaster.cpp b/src/cpp/ripple/LedgerMaster.cpp index 194b6724c27..ecc6b3d0f9e 100644 --- a/src/cpp/ripple/LedgerMaster.cpp +++ b/src/cpp/ripple/LedgerMaster.cpp @@ -234,7 +234,7 @@ void LedgerMaster::asyncAccept(Ledger::pointer ledger) resumeAcquiring(); } -bool LedgerMaster::acquireMissingLedger(Ledger::ref origLedger, const uint256& ledgerHash, uint32 ledgerSeq) +bool LedgerMaster::acquireMissingLedger(Ledger::ref origLedger, uint256 const& ledgerHash, uint32 ledgerSeq) { // return: false = already gave up recently Ledger::pointer ledger = mLedgerHistory.getLedgerBySeq(ledgerSeq); if (ledger && (Ledger::getHashByIndex(ledgerSeq) == ledgerHash)) @@ -524,14 +524,14 @@ void LedgerMaster::setFullLedger(Ledger::pointer ledger) } } -void LedgerMaster::checkAccept(const uint256& hash) +void LedgerMaster::checkAccept(uint256 const& hash) { Ledger::pointer ledger = mLedgerHistory.getLedgerByHash(hash); if (ledger) checkAccept(hash, ledger->getLedgerSeq()); } -void LedgerMaster::checkAccept(const uint256& hash, uint32 seq) +void LedgerMaster::checkAccept(uint256 const& hash, uint32 seq) { // Can we advance the last fully accepted ledger? If so, can we publish? boost::recursive_mutex::scoped_lock ml(mLock); diff --git a/src/cpp/ripple/LedgerMaster.h b/src/cpp/ripple/LedgerMaster.h index ff88376c94f..96aa89edaba 100644 --- a/src/cpp/ripple/LedgerMaster.h +++ b/src/cpp/ripple/LedgerMaster.h @@ -1,17 +1,13 @@ -#ifndef __LEDGERMASTER__ -#define __LEDGERMASTER__ - -#include "Ledger.h" -#include "LedgerHistory.h" -#include "LedgerAcquire.h" -#include "Transaction.h" -#include "TransactionEngine.h" -#include "CanonicalTXSet.h" +#ifndef RIPPLE_LEDGERMASTER_H +#define RIPPLE_LEDGERMASTER_H // Tracks the current ledger and any ledgers in the process of closing // Tracks ledger history // Tracks held transactions +// VFALCO TODO Rename to Ledgers +// It sounds like this holds all the ledgers... +// class LedgerMaster { public: @@ -84,7 +80,7 @@ class LedgerMaster return ret; } - Ledger::pointer getLedgerByHash(const uint256& hash) + Ledger::pointer getLedgerByHash(uint256 const& hash) { if (hash.isZero()) return boost::make_shared(boost::ref(*mCurrentLedger), false); @@ -119,8 +115,8 @@ class LedgerMaster void addValidateCallback(callback& c) { mOnValidate.push_back(c); } - void checkAccept(const uint256& hash); - void checkAccept(const uint256& hash, uint32 seq); + void checkAccept(uint256 const& hash); + void checkAccept(uint256 const& hash, uint32 seq); void tryPublish(); void newPFRequest(); @@ -131,7 +127,7 @@ class LedgerMaster bool isValidTransaction(Transaction::ref trans); bool isTransactionOnFutureList(Transaction::ref trans); - bool acquireMissingLedger(Ledger::ref from, const uint256& ledgerHash, uint32 ledgerSeq); + bool acquireMissingLedger(Ledger::ref from, uint256 const& ledgerHash, uint32 ledgerSeq); void asyncAccept(Ledger::pointer); void missingAcquireComplete(LedgerAcquire::pointer); void pubThread(); diff --git a/src/cpp/ripple/LedgerProposal.cpp b/src/cpp/ripple/LedgerProposal.cpp index 3a29a900e0b..20cbf26d19f 100644 --- a/src/cpp/ripple/LedgerProposal.cpp +++ b/src/cpp/ripple/LedgerProposal.cpp @@ -1,8 +1,8 @@ DECLARE_INSTANCE(LedgerProposal); -LedgerProposal::LedgerProposal(const uint256& pLgr, uint32 seq, const uint256& tx, uint32 closeTime, - const RippleAddress& naPeerPublic, const uint256& suppression) : +LedgerProposal::LedgerProposal(uint256 const& pLgr, uint32 seq, uint256 const& tx, uint32 closeTime, + const RippleAddress& naPeerPublic, uint256 const& suppression) : mPreviousLedger(pLgr), mCurrentHash(tx), mSuppression(suppression), mCloseTime(closeTime), mProposeSeq(seq), mPublicKey(naPeerPublic) { @@ -16,7 +16,7 @@ LedgerProposal::LedgerProposal(const uint256& pLgr, uint32 seq, const uint256& t LedgerProposal::LedgerProposal(const RippleAddress& naPub, const RippleAddress& naPriv, - const uint256& prevLgr, const uint256& position, uint32 closeTime) : + uint256 const& prevLgr, uint256 const& position, uint32 closeTime) : mPreviousLedger(prevLgr), mCurrentHash(position), mCloseTime(closeTime), mProposeSeq(0), mPublicKey(naPub), mPrivateKey(naPriv) { @@ -24,7 +24,7 @@ LedgerProposal::LedgerProposal(const RippleAddress& naPub, const RippleAddress& mTime = boost::posix_time::second_clock::universal_time(); } -LedgerProposal::LedgerProposal(const uint256& prevLgr, const uint256& position, uint32 closeTime) : +LedgerProposal::LedgerProposal(uint256 const& prevLgr, uint256 const& position, uint32 closeTime) : mPreviousLedger(prevLgr), mCurrentHash(position), mCloseTime(closeTime), mProposeSeq(0) { mTime = boost::posix_time::second_clock::universal_time(); @@ -43,12 +43,12 @@ uint256 LedgerProposal::getSigningHash() const return s.getSHA512Half(); } -bool LedgerProposal::checkSign(const std::string& signature, const uint256& signingHash) +bool LedgerProposal::checkSign(const std::string& signature, uint256 const& signingHash) { return mPublicKey.verifyNodePublic(signingHash, signature); } -bool LedgerProposal::changePosition(const uint256& newPosition, uint32 closeTime) +bool LedgerProposal::changePosition(uint256 const& newPosition, uint32 closeTime) { if (mProposeSeq == seqLeave) return false; diff --git a/src/cpp/ripple/LedgerProposal.h b/src/cpp/ripple/LedgerProposal.h index 0d15d5e8ff8..6aaae8b1526 100644 --- a/src/cpp/ripple/LedgerProposal.h +++ b/src/cpp/ripple/LedgerProposal.h @@ -1,12 +1,6 @@ #ifndef __PROPOSELEDGER__ #define __PROPOSELEDGER__ -#include -#include - -#include - - DEFINE_INSTANCE(LedgerProposal); class LedgerProposal : private IS_INSTANCE(LedgerProposal) @@ -18,41 +12,41 @@ class LedgerProposal : private IS_INSTANCE(LedgerProposal) typedef const pointer& ref; // proposal from peer - LedgerProposal(const uint256& prevLgr, uint32 proposeSeq, const uint256& propose, - uint32 closeTime, const RippleAddress& naPeerPublic, const uint256& suppress); + LedgerProposal(uint256 const& prevLgr, uint32 proposeSeq, uint256 const& propose, + uint32 closeTime, const RippleAddress& naPeerPublic, uint256 const& suppress); // our first proposal LedgerProposal(const RippleAddress& pubKey, const RippleAddress& privKey, - const uint256& prevLedger, const uint256& position, uint32 closeTime); + uint256 const& prevLedger, uint256 const& position, uint32 closeTime); // an unsigned "dummy" proposal for nodes not validating - LedgerProposal(const uint256& prevLedger, const uint256& position, uint32 closeTime); + LedgerProposal(uint256 const& prevLedger, uint256 const& position, uint32 closeTime); uint256 getSigningHash() const; - bool checkSign(const std::string& signature, const uint256& signingHash); + bool checkSign(const std::string& signature, uint256 const& signingHash); bool checkSign(const std::string& signature) { return checkSign(signature, getSigningHash()); } bool checkSign() { return checkSign(mSignature, getSigningHash()); } const uint160& getPeerID() const { return mPeerID; } - const uint256& getCurrentHash() const { return mCurrentHash; } - const uint256& getPrevLedger() const { return mPreviousLedger; } - const uint256& getHashRouter() const { return mSuppression; } + uint256 const& getCurrentHash() const { return mCurrentHash; } + uint256 const& getPrevLedger() const { return mPreviousLedger; } + uint256 const& getHashRouter() const { return mSuppression; } uint32 getProposeSeq() const { return mProposeSeq; } uint32 getCloseTime() const { return mCloseTime; } const RippleAddress& peekPublic() const { return mPublicKey; } Blob getPubKey() const { return mPublicKey.getNodePublic(); } Blob sign(); - void setPrevLedger(const uint256& prevLedger) { mPreviousLedger = prevLedger; } + void setPrevLedger(uint256 const& prevLedger) { mPreviousLedger = prevLedger; } void setSignature(const std::string& signature) { mSignature = signature; } bool hasSignature() { return !mSignature.empty(); } - bool isPrevLedger(const uint256& pl) { return mPreviousLedger == pl; } + bool isPrevLedger(uint256 const& pl) { return mPreviousLedger == pl; } bool isBowOut() { return mProposeSeq == seqLeave; } const boost::posix_time::ptime getCreateTime() { return mTime; } bool isStale(boost::posix_time::ptime cutoff) { return mTime <= cutoff; } - bool changePosition(const uint256& newPosition, uint32 newCloseTime); + bool changePosition(uint256 const& newPosition, uint32 newCloseTime); void bowOut(); Json::Value getJson() const; diff --git a/src/cpp/ripple/LoadManager.cpp b/src/cpp/ripple/LoadManager.cpp index 648523dd28e..6a81bf2c104 100644 --- a/src/cpp/ripple/LoadManager.cpp +++ b/src/cpp/ripple/LoadManager.cpp @@ -1,10 +1,3 @@ -#include "LoadManager.h" - -#include -#include -#include - -#include "Application.h" SETUP_LOG (LoadManager) @@ -44,6 +37,7 @@ LoadManager::~LoadManager() { UptimeTimer::getInstance().endManualUpdates (); + // VFALCO What is this loop? it doesn't seem to do anything useful. do { boost::this_thread::sleep(boost::posix_time::milliseconds(100)); diff --git a/src/cpp/ripple/LoadManager.h b/src/cpp/ripple/LoadManager.h index fc31b6ef4df..5a7afb0fb9d 100644 --- a/src/cpp/ripple/LoadManager.h +++ b/src/cpp/ripple/LoadManager.h @@ -1,14 +1,10 @@ -#ifndef LOADMANAGER__H -#define LOADMANAGER__H - -#include - -#include +#ifndef RIPPLE_LOADMANAGER_H +#define RIPPLE_LOADMANAGER_H +// types of load that can be placed on the server // VFALCO TODO replace LT_ with loadType in constants enum LoadType -{ // types of load that can be placed on the server - +{ // Bad things LT_InvalidRequest, // A request that we can immediately tell is invalid LT_RequestNoReply, // A request that we cannot satisfy @@ -58,7 +54,7 @@ class LoadSource static const int lsfOutbound = 2; // outbound connection public: - LoadSource(bool admin) + explicit LoadSource (bool admin) : mBalance(0) , mFlags(admin ? lsfPrivileged : 0) , mLastUpdate(UptimeTimer::getInstance().getElapsedSeconds ()) @@ -67,7 +63,7 @@ class LoadSource { } - LoadSource(const std::string& name) + explicit LoadSource (std::string const& name) : mName(name) , mBalance(0) , mFlags(0) @@ -77,8 +73,8 @@ class LoadSource { } - void rename(const std::string& name) { mName = name; } - const std::string& getName() { return mName; } + void rename (std::string const & name) { mName = name; } + std::string const& getName() { return mName; } bool isPrivileged() const { return (mFlags & lsfPrivileged) != 0; } void setPrivileged() { mFlags |= lsfPrivileged; } @@ -103,7 +99,6 @@ class LoadSource class LoadManager { public: - LoadManager(int creditRate = 100, int creditLimit = 500, int debitWarn = -500, int debitLimit = -1000); ~LoadManager(); void init(); @@ -130,7 +125,12 @@ class LoadManager void arm() { mArmed = true; } private: - int mCreditRate; // credits gained/lost per second + void canonicalize(LoadSource&, int upTime) const; + void addLoadCost(const LoadCost& c) { mCosts[static_cast(c.mType)] = c; } + void threadEntry(); + +private: + int mCreditRate; // credits gained/lost per second int mCreditLimit; // the most credits a source can have int mDebitWarn; // when a source drops below this, we warn int mDebitLimit; // when a source drops below this, we cut it off (should be negative) @@ -138,23 +138,11 @@ class LoadManager bool mShutdown; bool mArmed; - /* - int mSpace1[4]; // We want mUptime to have its own cache line - int mUptime; - int mSpace2[4]; - */ - int mDeadLock; // Detect server deadlocks mutable boost::mutex mLock; - void canonicalize(LoadSource&, int upTime) const; - std::vector mCosts; - - void addLoadCost(const LoadCost& c) { mCosts[static_cast(c.mType)] = c; } - - void threadEntry(); }; #endif diff --git a/src/cpp/ripple/NetworkOPs.cpp b/src/cpp/ripple/NetworkOPs.cpp index 75c6d1f8cb9..52b208b9843 100644 --- a/src/cpp/ripple/NetworkOPs.cpp +++ b/src/cpp/ripple/NetworkOPs.cpp @@ -1,8 +1,4 @@ -#include "NetworkOPs.h" - -#include - #include "Application.h" #include "Transaction.h" #include "HashPrefixes.h" @@ -22,13 +18,6 @@ SETUP_LOG (NetworkOPs) // code assumes this node is synched (and will continue to do so until // there's a functional network. -DECLARE_INSTANCE(InfoSub); - -void InfoSub::onSendEmpty() -{ - -} - NetworkOPs::NetworkOPs(boost::asio::io_service& io_service, LedgerMaster* pLedgerMaster) : mMode(omDISCONNECTED), mNeedNetworkLedger(false), mProposing(false), mValidating(false), mFeatureBlocked(false), @@ -39,9 +28,6 @@ NetworkOPs::NetworkOPs(boost::asio::io_service& io_service, LedgerMaster* pLedge { } -uint64 InfoSub::sSeq = 0; -boost::mutex InfoSub::sSeqLock; - std::string NetworkOPs::strOperatingMode() { static const char* paStatusToken[] = { @@ -100,7 +86,7 @@ void NetworkOPs::closeTimeOffset(int offset) CondLog (mCloseTimeOffset != 0, lsINFO, NetworkOPs) << "Close time offset now " << mCloseTimeOffset; } -uint32 NetworkOPs::getLedgerID(const uint256& hash) +uint32 NetworkOPs::getLedgerID(uint256 const& hash) { Ledger::pointer lrLedger = mLedgerMaster->getLedgerByHash(hash); @@ -144,7 +130,7 @@ uint32 NetworkOPs::getValidatedSeq() return mLedgerMaster->getValidatedLedger()->getLedgerSeq(); } -bool NetworkOPs::isValidated(uint32 seq, const uint256& hash) +bool NetworkOPs::isValidated(uint32 seq, uint256 const& hash) { if (!isValidated(seq)) return false; @@ -409,7 +395,7 @@ Transaction::pointer NetworkOPs::processTransaction(Transaction::pointer trans, return trans; } -Transaction::pointer NetworkOPs::findTransactionByID(const uint256& transactionID) +Transaction::pointer NetworkOPs::findTransactionByID(uint256 const& transactionID) { return Transaction::load(transactionID); } @@ -444,7 +430,7 @@ SLE::pointer NetworkOPs::getGenerator(Ledger::ref lrLedger, const uint160& uGene // <-- false : no entrieS STVector256 NetworkOPs::getDirNodeInfo( Ledger::ref lrLedger, - const uint256& uNodeIndex, + uint256 const& uNodeIndex, uint64& uNodePrevious, uint64& uNodeNext) { @@ -481,7 +467,7 @@ STVector256 NetworkOPs::getDirNodeInfo( // Nickname functions // -NicknameState::pointer NetworkOPs::getNicknameState(const uint256& uLedger, const std::string& strNickname) +NicknameState::pointer NetworkOPs::getNicknameState(uint256 const& uLedger, const std::string& strNickname) { return mLedgerMaster->getLedgerByHash(uLedger)->getNicknameState(strNickname); } @@ -508,7 +494,7 @@ Json::Value NetworkOPs::getOwnerInfo(Ledger::pointer lpLedger, const RippleAddre STVector256 svIndexes = sleNode->getFieldV256(sfIndexes); const std::vector& vuiIndexes = svIndexes.peekValue(); - BOOST_FOREACH(const uint256& uDirEntry, vuiIndexes) + BOOST_FOREACH(uint256 const& uDirEntry, vuiIndexes) { SLE::pointer sleCur = lpLedger->getSLEi(uDirEntry); @@ -825,7 +811,7 @@ void NetworkOPs::switchLastClosedLedger(Ledger::pointer newLedger, bool duringCo theApp->getPeers().relayMessage(NULL, packet); } -int NetworkOPs::beginConsensus(const uint256& networkClosed, Ledger::pointer closingLedger) +int NetworkOPs::beginConsensus(uint256 const& networkClosed, Ledger::pointer closingLedger) { WriteLog (lsINFO, NetworkOPs) << "Consensus time for ledger " << closingLedger->getLedgerSeq(); WriteLog (lsINFO, NetworkOPs) << " LCL is " << closingLedger->getParentHash(); @@ -929,7 +915,7 @@ void NetworkOPs::processTrustedProposal(LedgerProposal::pointer proposal, WriteLog (lsINFO, NetworkOPs) << "Not relaying trusted proposal"; } -SHAMap::pointer NetworkOPs::getTXMap(const uint256& hash) +SHAMap::pointer NetworkOPs::getTXMap(uint256 const& hash) { std::map >::iterator it = mRecentPositions.find(hash); if (it != mRecentPositions.end()) @@ -957,18 +943,18 @@ void NetworkOPs::takePosition(int seq, SHAMap::ref position) } } -SMAddNode NetworkOPs::gotTXData(const boost::shared_ptr& peer, const uint256& hash, +SHAMapAddNode NetworkOPs::gotTXData(const boost::shared_ptr& peer, uint256 const& hash, const std::list& nodeIDs, const std::list< Blob >& nodeData) { if (!haveConsensusObject()) { WriteLog (lsWARNING, NetworkOPs) << "Got TX data with no consensus object"; - return SMAddNode(); + return SHAMapAddNode(); } return mConsensus->peerGaveNodes(peer, hash, nodeIDs, nodeData); } -bool NetworkOPs::hasTXSet(const boost::shared_ptr& peer, const uint256& set, ripple::TxSetStatus status) +bool NetworkOPs::hasTXSet(const boost::shared_ptr& peer, uint256 const& set, ripple::TxSetStatus status) { if (!haveConsensusObject()) { @@ -978,14 +964,14 @@ bool NetworkOPs::hasTXSet(const boost::shared_ptr& peer, const uint256& se return mConsensus->peerHasSet(peer, set, status); } -bool NetworkOPs::stillNeedTXSet(const uint256& hash) +bool NetworkOPs::stillNeedTXSet(uint256 const& hash) { if (!mConsensus) return false; return mConsensus->stillNeedTXSet(hash); } -void NetworkOPs::mapComplete(const uint256& hash, SHAMap::ref map) +void NetworkOPs::mapComplete(uint256 const& hash, SHAMap::ref map) { if (haveConsensusObject()) mConsensus->mapComplete(hash, map, true); @@ -1392,9 +1378,9 @@ void NetworkOPs::pubProposedTransaction(Ledger::ref lpCurrent, SerializedTransac it = mSubRTTransactions.erase(it); } } - ALTransaction alt(stTxn, terResult); + AcceptedLedgerTx alt(stTxn, terResult); WriteLog (lsTRACE, NetworkOPs) << "pubProposed: " << alt.getJson(); - pubAccountTransaction(lpCurrent, ALTransaction(stTxn, terResult), false); + pubAccountTransaction(lpCurrent, AcceptedLedgerTx(stTxn, terResult), false); } void NetworkOPs::pubLedger(Ledger::ref accepted) @@ -1492,7 +1478,7 @@ Json::Value NetworkOPs::transJson(const SerializedTransaction& stTxn, TER terRes return jvObj; } -void NetworkOPs::pubValidatedTransaction(Ledger::ref alAccepted, const ALTransaction& alTx) +void NetworkOPs::pubValidatedTransaction(Ledger::ref alAccepted, const AcceptedLedgerTx& alTx) { Json::Value jvObj = transJson(*alTx.getTxn(), alTx.getResult(), true, alAccepted); jvObj["meta"] = alTx.getMeta()->getJson(0); @@ -1530,7 +1516,7 @@ void NetworkOPs::pubValidatedTransaction(Ledger::ref alAccepted, const ALTransac pubAccountTransaction(alAccepted, alTx, true); } -void NetworkOPs::pubAccountTransaction(Ledger::ref lpCurrent, const ALTransaction& alTx, bool bAccepted) +void NetworkOPs::pubAccountTransaction(Ledger::ref lpCurrent, const AcceptedLedgerTx& alTx, bool bAccepted) { boost::unordered_set notify; int iProposed = 0; @@ -1697,7 +1683,7 @@ bool NetworkOPs::unsubBook(uint64 uSeq, return true; } -void NetworkOPs::newLCL(int proposers, int convergeTime, const uint256& ledgerHash) +void NetworkOPs::newLCL(int proposers, int convergeTime, uint256 const& ledgerHash) { assert(convergeTime); mLastCloseProposers = proposers; @@ -1720,17 +1706,6 @@ void NetworkOPs::storeProposal(LedgerProposal::ref proposal, const RippleAddress props.push_back(proposal); } -InfoSub::~InfoSub() -{ - NetworkOPs& ops = theApp->getOPs(); - ops.unsubTransactions(mSeq); - ops.unsubRTTransactions(mSeq); - ops.unsubLedger(mSeq); - ops.unsubServer(mSeq); - ops.unsubAccount(mSeq, mSubAccountInfo, true); - ops.unsubAccount(mSeq, mSubAccountInfo, false); -} - #if 0 void NetworkOPs::subAccountChanges(InfoSub* isrListener, const uint256 uLedgerHash) { @@ -2095,12 +2070,12 @@ void NetworkOPs::sweepFetchPack() mFetchPack.sweep(); } -void NetworkOPs::addFetchPack(const uint256& hash, boost::shared_ptr< Blob >& data) +void NetworkOPs::addFetchPack(uint256 const& hash, boost::shared_ptr< Blob >& data) { mFetchPack.canonicalize(hash, data, false); } -bool NetworkOPs::getFetchPack(const uint256& hash, Blob & data) +bool NetworkOPs::getFetchPack(uint256 const& hash, Blob& data) { bool ret = mFetchPack.retrieve(hash, data); if (!ret) diff --git a/src/cpp/ripple/NetworkOPs.h b/src/cpp/ripple/NetworkOPs.h index ea4031052e1..ef8090b72c9 100644 --- a/src/cpp/ripple/NetworkOPs.h +++ b/src/cpp/ripple/NetworkOPs.h @@ -1,91 +1,11 @@ -#ifndef __NETWORK_OPS__ -#define __NETWORK_OPS__ - -#include -#include -#include -#include - -#include "AccountState.h" -#include "LedgerMaster.h" -#include "NicknameState.h" -#include "RippleState.h" -#include "SerializedValidation.h" -#include "LedgerAcquire.h" -#include "LedgerProposal.h" -#include "AcceptedLedger.h" +#ifndef RIPPLE_NETWORKOPS_H +#define RIPPLE_NETWORKOPS_H // Operations that clients may wish to perform against the network // Master operational handler, server sequencer, network tracker class Peer; class LedgerConsensus; -class PFRequest; - -DEFINE_INSTANCE(InfoSub); - -// VFALCO TODO Move InfoSub to a separate file -class InfoSub : public IS_INSTANCE(InfoSub) -{ -public: - typedef boost::shared_ptr pointer; - typedef boost::weak_ptr wptr; - typedef const boost::shared_ptr& ref; - - InfoSub() - { - boost::mutex::scoped_lock sl(sSeqLock); - mSeq = ++sSeq; - } - - virtual ~InfoSub(); - - virtual void send(const Json::Value& jvObj, bool broadcast) = 0; - virtual void send(const Json::Value& jvObj, const std::string& sObj, bool broadcast) - { send(jvObj, broadcast); } - - uint64 getSeq() - { - return mSeq; - } - - void onSendEmpty(); - - void insertSubAccountInfo(RippleAddress addr, uint32 uLedgerIndex) - { - boost::mutex::scoped_lock sl(mLockInfo); - - mSubAccountInfo.insert(addr); - } - - void clearPFRequest() - { - mPFRequest.reset(); - } - - void setPFRequest(const boost::shared_ptr& req) - { - mPFRequest = req; - } - - const boost::shared_ptr& getPFRequest() - { - return mPFRequest; - } - -protected: - // VFALCO TODO make accessor for this member - boost::mutex mLockInfo; - -private: - boost::unordered_set mSubAccountInfo; - boost::unordered_set mSubAccountTransaction; - boost::shared_ptr mPFRequest; - - uint64 mSeq; - static uint64 sSeq; - static boost::mutex sSeqLock; -}; class NetworkOPs { @@ -116,7 +36,7 @@ class NetworkOPs uint32 getValidationTimeNC(); // Use *only* to timestamp our own validation void closeTimeOffset(int); boost::posix_time::ptime getNetworkTimePT(); - uint32 getLedgerID(const uint256& hash); + uint32 getLedgerID(uint256 const& hash); uint32 getCurrentLedgerID(); OperatingMode getOperatingMode() { return mMode; } std::string strOperatingMode(); @@ -125,7 +45,7 @@ class NetworkOPs Ledger::ref getValidatedLedger() { return mLedgerMaster->getValidatedLedger(); } Ledger::ref getCurrentLedger() { return mLedgerMaster->getCurrentLedger(); } Ledger::ref getCurrentSnapshot() { return mLedgerMaster->getCurrentSnapshot(); } - Ledger::pointer getLedgerByHash(const uint256& hash) { return mLedgerMaster->getLedgerByHash(hash); } + Ledger::pointer getLedgerByHash(uint256 const& hash) { return mLedgerMaster->getLedgerByHash(hash); } Ledger::pointer getLedgerBySeq(const uint32 seq); void missingNodeInLedger(const uint32 seq); @@ -136,15 +56,15 @@ class NetworkOPs bool haveLedger(uint32 seq); uint32 getValidatedSeq(); bool isValidated(uint32 seq); - bool isValidated(uint32 seq, const uint256& hash); + bool isValidated(uint32 seq, uint256 const& hash); bool isValidated(Ledger::ref l) { return isValidated(l->getLedgerSeq(), l->getHash()); } bool getValidatedRange(uint32& minVal, uint32& maxVal) { return mLedgerMaster->getValidatedRange(minVal, maxVal); } SerializedValidation::ref getLastValidation() { return mLastValidation; } void setLastValidation(SerializedValidation::ref v) { mLastValidation = v; } - SLE::pointer getSLE(Ledger::pointer lpLedger, const uint256& uHash) { return lpLedger->getSLE(uHash); } - SLE::pointer getSLEi(Ledger::pointer lpLedger, const uint256& uHash) { return lpLedger->getSLEi(uHash); } + SLE::pointer getSLE(Ledger::pointer lpLedger, uint256 const& uHash) { return lpLedger->getSLE(uHash); } + SLE::pointer getSLEi(Ledger::pointer lpLedger, uint256 const& uHash) { return lpLedger->getSLEi(uHash); } // // Transaction operations @@ -158,9 +78,9 @@ class NetworkOPs Transaction::pointer processTransaction(Transaction::pointer transaction, bool bAdmin) { return processTransaction(transaction, bAdmin, stCallback()); } - Transaction::pointer findTransactionByID(const uint256& transactionID); + Transaction::pointer findTransactionByID(uint256 const& transactionID); #if 0 - int findTransactionsBySource(const uint256& uLedger, std::list&, const RippleAddress& sourceAccount, + int findTransactionsBySource(uint256 const& uLedger, std::list&, const RippleAddress& sourceAccount, uint32 minSeq, uint32 maxSeq); #endif int findTransactionsByDestination(std::list&, const RippleAddress& destinationAccount, @@ -177,7 +97,7 @@ class NetworkOPs // Directory functions // - STVector256 getDirNodeInfo(Ledger::ref lrLedger, const uint256& uRootIndex, + STVector256 getDirNodeInfo(Ledger::ref lrLedger, uint256 const& uRootIndex, uint64& uNodePrevious, uint64& uNodeNext); #if 0 @@ -185,7 +105,7 @@ class NetworkOPs // Nickname functions // - NicknameState::pointer getNicknameState(const uint256& uLedger, const std::string& strNickname); + NicknameState::pointer getNicknameState(uint256 const& uLedger, const std::string& strNickname); #endif // @@ -210,34 +130,34 @@ class NetworkOPs Json::Value& jvResult); // raw object operations - bool findRawLedger(const uint256& ledgerHash, Blob & rawLedger); - bool findRawTransaction(const uint256& transactionHash, Blob & rawTransaction); - bool findAccountNode(const uint256& nodeHash, Blob & rawAccountNode); - bool findTransactionNode(const uint256& nodeHash, Blob & rawTransactionNode); + bool findRawLedger(uint256 const& ledgerHash, Blob& rawLedger); + bool findRawTransaction(uint256 const& transactionHash, Blob& rawTransaction); + bool findAccountNode(uint256 const& nodeHash, Blob& rawAccountNode); + bool findTransactionNode(uint256 const& nodeHash, Blob& rawTransactionNode); // tree synchronization operations - bool getTransactionTreeNodes(uint32 ledgerSeq, const uint256& myNodeID, + bool getTransactionTreeNodes(uint32 ledgerSeq, uint256 const& myNodeID, Blob const& myNode, std::list< Blob >& newNodes); - bool getAccountStateNodes(uint32 ledgerSeq, const uint256& myNodeId, + bool getAccountStateNodes(uint32 ledgerSeq, uint256 const& myNodeId, Blob const& myNode, std::list< Blob >& newNodes); // ledger proposal/close functions void processTrustedProposal(LedgerProposal::pointer proposal, boost::shared_ptr set, RippleAddress nodePublic, uint256 checkLedger, bool sigGood); - SMAddNode gotTXData(const boost::shared_ptr& peer, const uint256& hash, + SHAMapAddNode gotTXData(const boost::shared_ptr& peer, uint256 const& hash, const std::list& nodeIDs, const std::list< Blob >& nodeData); bool recvValidation(SerializedValidation::ref val, const std::string& source); void takePosition(int seq, SHAMap::ref position); - SHAMap::pointer getTXMap(const uint256& hash); - bool hasTXSet(const boost::shared_ptr& peer, const uint256& set, ripple::TxSetStatus status); - void mapComplete(const uint256& hash, SHAMap::ref map); - bool stillNeedTXSet(const uint256& hash); + SHAMap::pointer getTXMap(uint256 const& hash); + bool hasTXSet(const boost::shared_ptr& peer, uint256 const& set, ripple::TxSetStatus status); + void mapComplete(uint256 const& hash, SHAMap::ref map); + bool stillNeedTXSet(uint256 const& hash); void makeFetchPack(Job&, boost::weak_ptr peer, boost::shared_ptr request, Ledger::pointer wantLedger, Ledger::pointer haveLedger, uint32 uUptime); bool shouldFetchPack(uint32 seq); void gotFetchPack(bool progress, uint32 seq); - void addFetchPack(const uint256& hash, boost::shared_ptr< Blob >& data); - bool getFetchPack(const uint256& hash, Blob & data); + void addFetchPack(uint256 const& hash, boost::shared_ptr< Blob >& data); + bool getFetchPack(uint256 const& hash, Blob& data); int getFetchSize(); void sweepFetchPack(); @@ -245,12 +165,12 @@ class NetworkOPs void checkState(const boost::system::error_code& result); void switchLastClosedLedger(Ledger::pointer newLedger, bool duringConsensus); // Used for the "jump" case bool checkLastClosedLedger(const std::vector&, uint256& networkClosed); - int beginConsensus(const uint256& networkClosed, Ledger::pointer closingLedger); + int beginConsensus(uint256 const& networkClosed, Ledger::pointer closingLedger); void tryStartConsensus(); void endConsensus(bool correctLCL); void setStandAlone() { setMode(omFULL); } void setStateTimer(); - void newLCL(int proposers, int convergeTime, const uint256& ledgerHash); + void newLCL(int proposers, int convergeTime, uint256 const& ledgerHash); void needNetworkLedger() { mNeedNetworkLedger = true; } void clearNeedNetworkLedger() { mNeedNetworkLedger = false; } bool isNeedNetworkLedger() { return mNeedNetworkLedger; } @@ -382,8 +302,8 @@ class NetworkOPs Json::Value pubBootstrapAccountInfo(Ledger::ref lpAccepted, const RippleAddress& naAccountID); - void pubValidatedTransaction(Ledger::ref alAccepted, const ALTransaction& alTransaction); - void pubAccountTransaction(Ledger::ref lpCurrent, const ALTransaction& alTransaction, bool isAccepted); + void pubValidatedTransaction(Ledger::ref alAccepted, const AcceptedLedgerTx& alTransaction); + void pubAccountTransaction(Ledger::ref lpCurrent, const AcceptedLedgerTx& alTransaction, bool isAccepted); void pubServer(); }; diff --git a/src/cpp/ripple/NicknameState.h b/src/cpp/ripple/NicknameState.h index 31144e8bcc3..60a73b08e12 100644 --- a/src/cpp/ripple/NicknameState.h +++ b/src/cpp/ripple/NicknameState.h @@ -1,5 +1,5 @@ -#ifndef NICKNAMESTATE_H -#define NICKNAMESTATE_H +#ifndef RIPPLE_NICKNAMESTATE_H +#define RIPPLE_NICKNAMESTATE_H // // State of a nickname node. @@ -11,9 +11,6 @@ class NicknameState public: typedef boost::shared_ptr pointer; -private: - SerializedLedgerEntry::pointer mLedgerEntry; - public: NicknameState(SerializedLedgerEntry::pointer ledgerEntry); // For accounts in a ledger @@ -27,6 +24,9 @@ class NicknameState Blob getRaw() const; void addJson(Json::Value& value); + +private: + SerializedLedgerEntry::pointer mLedgerEntry; }; #endif diff --git a/src/cpp/ripple/OfferCreateTransactor.cpp b/src/cpp/ripple/OfferCreateTransactor.cpp index 620fac726af..c10f248d5c4 100644 --- a/src/cpp/ripple/OfferCreateTransactor.cpp +++ b/src/cpp/ripple/OfferCreateTransactor.cpp @@ -4,7 +4,7 @@ SETUP_LOG (OfferCreateTransactor) // Make sure an offer is still valid. If not, mark it unfunded. bool OfferCreateTransactor::bValidOffer( SLE::ref sleOfferDir, - const uint256& uOfferIndex, + uint256 const& uOfferIndex, const uint160& uOfferOwnerID, const STAmount& saOfferPays, const STAmount& saOfferGets, @@ -87,7 +87,7 @@ TER OfferCreateTransactor::takeOffers( const bool bOpenLedger, const bool bPassive, const bool bSell, - const uint256& uBookBase, + uint256 const& uBookBase, const uint160& uTakerAccountID, SLE::ref sleTakerAccount, const STAmount& saTakerPays, @@ -341,7 +341,7 @@ TER OfferCreateTransactor::takeOffers( if (tesSUCCESS == terResult) { // On success, delete offers that became unfunded. - BOOST_FOREACH(const uint256& uOfferIndex, usOfferUnfundedBecame) + BOOST_FOREACH(uint256 const& uOfferIndex, usOfferUnfundedBecame) { WriteLog (lsDEBUG, OfferCreateTransactor) << "takeOffers: became unfunded: " << uOfferIndex.ToString(); @@ -649,7 +649,7 @@ TER OfferCreateTransactor::doApply() // On storing meta data, delete offers that were found unfunded to prevent encountering them in future. if (tesSUCCESS == terResult) { - BOOST_FOREACH(const uint256& uOfferIndex, usOfferUnfundedFound) + BOOST_FOREACH(uint256 const& uOfferIndex, usOfferUnfundedFound) { WriteLog (lsINFO, OfferCreateTransactor) << "takeOffers: found unfunded: " << uOfferIndex.ToString(); diff --git a/src/cpp/ripple/OfferCreateTransactor.h b/src/cpp/ripple/OfferCreateTransactor.h index bdce5d8f496..37fc890e238 100644 --- a/src/cpp/ripple/OfferCreateTransactor.h +++ b/src/cpp/ripple/OfferCreateTransactor.h @@ -12,7 +12,7 @@ class OfferCreateTransactor : public Transactor private: bool bValidOffer( SLE::ref sleOfferDir, - const uint256& uOffer, + uint256 const& uOffer, const uint160& uOfferOwnerID, const STAmount& saOfferPays, const STAmount& saOfferGets, @@ -26,7 +26,7 @@ class OfferCreateTransactor : public Transactor const bool bOpenLedger, const bool bPassive, const bool bSell, - const uint256& uBookBase, + uint256 const& uBookBase, const uint160& uTakerAccountID, SLE::ref sleTakerAccount, const STAmount& saTakerPays, diff --git a/src/cpp/ripple/OrderBook.h b/src/cpp/ripple/OrderBook.h index 543dafc9104..1e487cff90e 100644 --- a/src/cpp/ripple/OrderBook.h +++ b/src/cpp/ripple/OrderBook.h @@ -2,9 +2,6 @@ #ifndef ORDERBOOK_H #define ORDERBOOK_H -#include "SerializedLedger.h" -#include "NetworkOPs.h" - /* Encapsulates the SLE for an orderbook */ @@ -14,7 +11,7 @@ class OrderBook typedef boost::shared_ptr pointer; typedef const boost::shared_ptr& ref; - OrderBook(const uint256& index, const uint160& ci, const uint160& co, const uint160& ii, const uint160& io) : + OrderBook(uint256 const& index, const uint160& ci, const uint160& co, const uint160& ii, const uint160& io) : mBookBase(index), mCurrencyIn(ci), mCurrencyOut(co), diff --git a/src/cpp/ripple/OrderBookDB.cpp b/src/cpp/ripple/OrderBookDB.cpp index ce3bcbcbb53..3f58f310162 100644 --- a/src/cpp/ripple/OrderBookDB.cpp +++ b/src/cpp/ripple/OrderBookDB.cpp @@ -126,7 +126,7 @@ BookListeners::pointer OrderBookDB::getBookListeners(const uint160& currencyPays // Based on the meta, send the meta to the streams that are listening // We need to determine which streams a given meta effects -void OrderBookDB::processTxn(Ledger::ref ledger, const ALTransaction& alTx, Json::Value& jvObj) +void OrderBookDB::processTxn(Ledger::ref ledger, const AcceptedLedgerTx& alTx, Json::Value& jvObj) { boost::recursive_mutex::scoped_lock sl(mLock); if (alTx.getResult() == tesSUCCESS) diff --git a/src/cpp/ripple/OrderBookDB.h b/src/cpp/ripple/OrderBookDB.h index 43706e01a8f..a7dbedbfeb3 100644 --- a/src/cpp/ripple/OrderBookDB.h +++ b/src/cpp/ripple/OrderBookDB.h @@ -2,11 +2,6 @@ #ifndef ORDERBOOK_DB_H #define ORDERBOOK_DB_H -#include -#include - -#include "Ledger.h" -#include "AcceptedLedger.h" #include "OrderBook.h" @@ -57,7 +52,7 @@ class OrderBookDB const uint160& issuerPays, const uint160& issuerGets); // see if this txn effects any orderbook - void processTxn(Ledger::ref ledger, const ALTransaction& alTx, Json::Value& jvObj); + void processTxn(Ledger::ref ledger, const AcceptedLedgerTx& alTx, Json::Value& jvObj); private: boost::unordered_map< currencyIssuer_t, std::vector > mSourceMap; // by ci/ii diff --git a/src/cpp/ripple/PFRequest.cpp b/src/cpp/ripple/PFRequest.cpp index 32d50f08ba0..daf5df06834 100644 --- a/src/cpp/ripple/PFRequest.cpp +++ b/src/cpp/ripple/PFRequest.cpp @@ -1,6 +1,5 @@ #include "PFRequest.h" -#include "NetworkOPs.h" #include "RPCErr.h" #include "Ledger.h" #include "Application.h" diff --git a/src/cpp/ripple/ParameterTable.h b/src/cpp/ripple/ParameterTable.h index 7452058746d..4bdeb88c678 100644 --- a/src/cpp/ripple/ParameterTable.h +++ b/src/cpp/ripple/ParameterTable.h @@ -1,11 +1,5 @@ -#ifndef PARAMETER_TABLE__H -#define PARAMETER_TABLE__H - -#include -#include - -#include -#include +#ifndef RIPPLE_PARAMETERTABLE_H +#define RIPPLE_PARAMETERTABLE_H class Parameter : public boost::enable_shared_from_this { // abstract base class parameters are derived from diff --git a/src/cpp/ripple/RPCHandler.h b/src/cpp/ripple/RPCHandler.h index 4b7a2ef1163..5dbddfc98e4 100644 --- a/src/cpp/ripple/RPCHandler.h +++ b/src/cpp/ripple/RPCHandler.h @@ -4,7 +4,6 @@ #include #include "Ledger.h" -#include "NetworkOPs.h" #define LEDGER_CURRENT -1 #define LEDGER_CLOSED -2 diff --git a/src/cpp/ripple/RPCServer.h b/src/cpp/ripple/RPCServer.h index 8a0c4c2e2cc..5bf2e0898bd 100644 --- a/src/cpp/ripple/RPCServer.h +++ b/src/cpp/ripple/RPCServer.h @@ -1,14 +1,7 @@ #ifndef __RPCSERVER__ #define __RPCSERVER__ -#include -#include -#include -#include - #include "HTTPRequest.h" -#include "NetworkOPs.h" -#include "SerializedLedger.h" #include "RPCHandler.h" #include "LoadManager.h" diff --git a/src/cpp/ripple/RPCSub.h b/src/cpp/ripple/RPCSub.h index 8eb67f923d9..ccd0f289f41 100644 --- a/src/cpp/ripple/RPCSub.h +++ b/src/cpp/ripple/RPCSub.h @@ -1,10 +1,6 @@ #ifndef __RPCSUB__ #define __RPCSUB__ -#include - -#include "NetworkOPs.h" - #define RPC_EVENT_QUEUE_MAX 32 // Subscription object for JSON-RPC diff --git a/src/cpp/ripple/RippleCalc.cpp b/src/cpp/ripple/RippleCalc.cpp index 83fbef62471..1ad9d34ea10 100644 --- a/src/cpp/ripple/RippleCalc.cpp +++ b/src/cpp/ripple/RippleCalc.cpp @@ -1416,7 +1416,7 @@ TER RippleCalc::calcNodeDeliverFwd( const uint160& uNxtAccountID = pnNxt.uAccountID; const uint160& uCurCurrencyID = pnCur.uCurrencyID; const uint160& uCurIssuerID = pnCur.uIssuerID; - const uint256& uOfferIndex = pnCur.uOfferIndex; + uint256 const& uOfferIndex = pnCur.uOfferIndex; const uint160& uPrvCurrencyID = pnPrv.uCurrencyID; const uint160& uPrvIssuerID = pnPrv.uIssuerID; const STAmount& saInTransRate = pnPrv.saTransferRate; @@ -3017,7 +3017,7 @@ int iPass = 0; if (tesSUCCESS == terResult) { // Delete became unfunded offers. - BOOST_FOREACH(const uint256& uOfferIndex, vuUnfundedBecame) + BOOST_FOREACH(uint256 const& uOfferIndex, vuUnfundedBecame) { if (tesSUCCESS == terResult) { @@ -3028,7 +3028,7 @@ int iPass = 0; } // Delete found unfunded offers. - BOOST_FOREACH(const uint256& uOfferIndex, rc.musUnfundedFound) + BOOST_FOREACH(uint256 const& uOfferIndex, rc.musUnfundedFound) { if (tesSUCCESS == terResult) { @@ -3116,8 +3116,8 @@ TER calcOfferFill(PaymentNode& pnSrc, PaymentNode& pnDst, bool bAllowPartial) // Get the next offer limited by funding. // - Stop when becomes unfunded. void TransactionEngine::calcOfferBridgeNext( - const uint256& uBookRoot, // --> Which order book to look in. - const uint256& uBookEnd, // --> Limit of how far to look. + uint256 const& uBookRoot, // --> Which order book to look in. + uint256 const& uBookEnd, // --> Limit of how far to look. uint256& uBookDirIndex, // <-> Current directory. <-- 0 = no offer available. uint64& uBookDirNode, // <-> Which node. 0 = first. unsigned int& uBookDirEntry, // <-> Entry in node. 0 = first. diff --git a/src/cpp/ripple/RippleState.h b/src/cpp/ripple/RippleState.h index ca70ffd6bb5..a02678e3734 100644 --- a/src/cpp/ripple/RippleState.h +++ b/src/cpp/ripple/RippleState.h @@ -6,11 +6,8 @@ // - Isolate ledger entry format. // -#include "SerializedLedger.h" #include "AccountItems.h" -#include - class RippleState : public AccountItem { public: diff --git a/src/cpp/ripple/SHAMap.h b/src/cpp/ripple/SHAMap.h deleted file mode 100644 index 8fc159d80fc..00000000000 --- a/src/cpp/ripple/SHAMap.h +++ /dev/null @@ -1,480 +0,0 @@ -#ifndef RIPPLE_SHAMAP_H -#define RIPPLE_SHAMAP_H - -// VFALCO TODO Get this include out of here! -#include "ripple_UptimeTimerAdapter.h" - -DEFINE_INSTANCE(SHAMap); -DEFINE_INSTANCE(SHAMapItem); -DEFINE_INSTANCE(SHAMapTreeNode); - -class SHAMap; -class SHAMapSyncFilter; - -// A tree-like map of SHA256 hashes -// The trees are designed for rapid synchronization and compression of differences - -// Identifies a node in a SHA256 hash map -class SHAMapNode -{ -public: - - static const int rootDepth = 0; - - SHAMapNode() : mDepth(0), mHash(0) { ; } - SHAMapNode(int depth, const uint256& hash); - - int getDepth() const { return mDepth; } - const uint256& getNodeID() const { return mNodeID; } - bool isValid() const { return (mDepth >= 0) && (mDepth < 64); } - bool isRoot() const { return mDepth == 0; } - size_t getMHash() const { if (mHash == 0) setMHash(); return mHash; } - - virtual bool isPopulated() const { return false; } - - SHAMapNode getParentNodeID() const - { - assert(mDepth); - return SHAMapNode(mDepth - 1, mNodeID); - } - - SHAMapNode getChildNodeID(int m) const; - int selectBranch(const uint256& hash) const; - - bool operator<(const SHAMapNode&) const; - bool operator>(const SHAMapNode&) const; - bool operator<=(const SHAMapNode&) const; - bool operator>=(const SHAMapNode&) const; - - bool operator==(const SHAMapNode& n) const { return (mDepth == n.mDepth) && (mNodeID == n.mNodeID); } - bool operator==(const uint256& n) const { return n == mNodeID; } - bool operator!=(const SHAMapNode& n) const { return (mDepth != n.mDepth) || (mNodeID != n.mNodeID); } - bool operator!=(const uint256& n) const { return n != mNodeID; } - - virtual std::string getString() const; - void dump() const; - - static bool ClassInit(); - static uint256 getNodeID(int depth, const uint256& hash); - - // Convert to/from wire format (256-bit nodeID, 1-byte depth) - void addIDRaw(Serializer &s) const; - std::string getRawString() const; - static int getRawIDLength(void) { return 33; } - SHAMapNode(const void *ptr, int len); - -protected: - SHAMapNode(int depth, const uint256& id, bool) : mNodeID(id), mDepth(depth), mHash(0) { ; } - -private: - static uint256 smMasks[65]; // AND with hash to get node id - - uint256 mNodeID; - int mDepth; - mutable size_t mHash; - - void setMHash() const; -}; - -extern std::size_t hash_value(const SHAMapNode& mn); - -inline std::ostream& operator<<(std::ostream& out, const SHAMapNode& node) { return out << node.getString(); } - -class SHAMapItem : public IS_INSTANCE(SHAMapItem) -{ // an item stored in a SHAMap -public: - typedef boost::shared_ptr pointer; - typedef const boost::shared_ptr& ref; - -private: - uint256 mTag; - Serializer mData; - -public: - - SHAMapItem(const uint256& tag) : mTag(tag) { ; } - SHAMapItem(const uint256& tag, Blob const& data); - SHAMapItem(const uint256& tag, const Serializer& s); - SHAMapItem(Blob const& data); // tag by hash - - const uint256& getTag() const { return mTag; } - Blob getData() const { return mData.getData(); } - Blob const& peekData() const { return mData.peekData(); } - Serializer& peekSerializer() { return mData; } - void addRaw(Blob & s) const { s.insert(s.end(), mData.begin(), mData.end()); } - - void updateData(Blob const& data) { mData=data; } - - bool operator==(const SHAMapItem& i) const { return mTag == i.mTag; } - bool operator!=(const SHAMapItem& i) const { return mTag != i.mTag; } - bool operator==(const uint256& i) const { return mTag == i; } - bool operator!=(const uint256& i) const { return mTag != i; } -#if 0 - // This code is comment out because it is unused. It could work. - bool operator<(const SHAMapItem& i) const { return mTag < i.mTag; } - bool operator>(const SHAMapItem& i) const { return mTag > i.mTag; } - bool operator<=(const SHAMapItem& i) const { return mTag <= i.mTag; } - bool operator>=(const SHAMapItem& i) const { return mTag >= i.mTag; } - - bool operator<(const uint256& i) const { return mTag < i; } - bool operator>(const uint256& i) const { return mTag > i; } - bool operator<=(const uint256& i) const { return mTag <= i; } - bool operator>=(const uint256& i) const { return mTag >= i; } -#endif - virtual void dump(); -}; - -enum SHANodeFormat -{ - snfPREFIX = 1, // Form that hashes to its official hash - snfWIRE = 2, // Compressed form used on the wire - snfHASH = 3, // just the hash -}; - -enum SHAMapType -{ - smtTRANSACTION =1, // A tree of transactions - smtSTATE =2, // A tree of state nodes - smtFREE =3, // A tree not part of a ledger -}; - -class SHAMapTreeNode : public SHAMapNode, public IS_INSTANCE(SHAMapTreeNode) -{ - friend class SHAMap; - -public: - typedef boost::shared_ptr pointer; - typedef const boost::shared_ptr& ref; - - enum TNType - { - tnERROR = 0, - tnINNER = 1, - tnTRANSACTION_NM = 2, // transaction, no metadata - tnTRANSACTION_MD = 3, // transaction, with metadata - tnACCOUNT_STATE = 4 - }; - -private: - uint256 mHash; - uint256 mHashes[16]; - SHAMapItem::pointer mItem; - uint32 mSeq, mAccessSeq; - TNType mType; - int mIsBranch; - bool mFullBelow; - - bool updateHash(); - - SHAMapTreeNode(const SHAMapTreeNode&); // no implementation - SHAMapTreeNode& operator=(const SHAMapTreeNode&); // no implementation - -public: - SHAMapTreeNode(uint32 seq, const SHAMapNode& nodeID); // empty node - SHAMapTreeNode(const SHAMapTreeNode& node, uint32 seq); // copy node from older tree - SHAMapTreeNode(const SHAMapNode& nodeID, SHAMapItem::ref item, TNType type, uint32 seq); - - // raw node functions - SHAMapTreeNode(const SHAMapNode& id, Blob const& data, uint32 seq, - SHANodeFormat format, const uint256& hash, bool hashValid); - void addRaw(Serializer &, SHANodeFormat format); - - virtual bool isPopulated() const { return true; } - - // node functions - uint32 getSeq() const { return mSeq; } - void setSeq(uint32 s) { mAccessSeq = mSeq = s; } - void touch(uint32 s) { mAccessSeq = s; } - const uint256& getNodeHash() const { return mHash; } - TNType getType() const { return mType; } - - // type functions - bool isLeaf() const { return (mType == tnTRANSACTION_NM) || (mType == tnTRANSACTION_MD) || - (mType == tnACCOUNT_STATE); } - bool isInner() const { return mType == tnINNER; } - bool isValid() const { return mType != tnERROR; } - bool isTransaction() const { return (mType == tnTRANSACTION_NM) || (mType == tnTRANSACTION_MD); } - bool hasMetaData() const { return mType == tnTRANSACTION_MD; } - bool isAccountState() const { return mType == tnACCOUNT_STATE; } - - // inner node functions - bool isInnerNode() const { return !mItem; } - bool setChildHash(int m, const uint256& hash); - bool isEmptyBranch(int m) const { return (mIsBranch & (1 << m)) == 0; } - bool isEmpty() const; - int getBranchCount() const; - void makeInner(); - const uint256& getChildHash(int m) const - { - assert((m >= 0) && (m < 16) && (mType == tnINNER)); - return mHashes[m]; - } - - // item node function - bool hasItem() const { return !!mItem; } - SHAMapItem::ref peekItem() { return mItem; } - SHAMapItem::pointer getItem() const; - bool setItem(SHAMapItem::ref i, TNType type); - const uint256& getTag() const { return mItem->getTag(); } - Blob const& peekData() { return mItem->peekData(); } - Blob getData() const { return mItem->getData(); } - - // sync functions - bool isFullBelow(void) const { return mFullBelow; } - void setFullBelow(void) { mFullBelow = true; } - - virtual void dump(); - virtual std::string getString() const; -}; - -enum SHAMapState -{ - smsModifying = 0, // Objects can be added and removed (like an open ledger) - smsImmutable = 1, // Map cannot be changed (like a closed ledger) - smsSynching = 2, // Map's hash is locked in, valid nodes can be added (like a peer's closing ledger) - smsFloating = 3, // Map is free to change hash (like a synching open ledger) - smsInvalid = 4, // Map is known not to be valid (usually synching a corrupt ledger) -}; - -class SHAMapSyncFilter -{ -public: - SHAMapSyncFilter() { ; } - virtual ~SHAMapSyncFilter() { ; } - - virtual void gotNode(bool fromFilter, const SHAMapNode& id, const uint256& nodeHash, - Blob const& nodeData, SHAMapTreeNode::TNType type) - { ; } - - virtual bool haveNode(const SHAMapNode& id, const uint256& nodeHash, Blob & nodeData) - { return false; } -}; - -class SHAMapMissingNode : public std::runtime_error -{ -public: - SHAMapMissingNode(SHAMapType t, const SHAMapNode& nodeID, const uint256& nodeHash) : - std::runtime_error("SHAMapMissingNode"), mType(t), mNodeID(nodeID), mNodeHash(nodeHash) - { ; } - - SHAMapMissingNode(SHAMapType t, const SHAMapNode& nodeID, const uint256& nodeHash, const uint256& targetIndex) : - std::runtime_error(nodeID.getString()), mType(t), - mNodeID(nodeID), mNodeHash(nodeHash), mTargetIndex(targetIndex) - { ; } - - virtual ~SHAMapMissingNode() throw() - { ; } - - void setTargetNode(const uint256& tn) { mTargetIndex = tn; } - - SHAMapType getMapType() const { return mType; } - const SHAMapNode& getNodeID() const { return mNodeID; } - const uint256& getNodeHash() const { return mNodeHash; } - const uint256& getTargetIndex() const { return mTargetIndex; } - bool hasTargetIndex() const { return !mTargetIndex.isZero(); } - -private: - SHAMapType mType; - SHAMapNode mNodeID; - uint256 mNodeHash; - uint256 mTargetIndex; -}; - -extern std::ostream& operator<<(std::ostream&, const SHAMapMissingNode&); - -// results of adding nodes -class SMAddNode -{ -public: - SMAddNode() : mInvalid(false), mUseful(false) { ; } - - void setInvalid() { mInvalid = true; } - void setUseful() { mUseful = true; } - void reset() { mInvalid = false; mUseful = false; } - - bool isInvalid() const { return mInvalid; } - bool isUseful() const { return mUseful; } - - bool combine(const SMAddNode& n) - { - if (n.mInvalid) - { - mInvalid = true; - return false; - } - if (n.mUseful) - mUseful = true; - return true; - } - - operator bool() const { return !mInvalid; } - - static SMAddNode okay() { return SMAddNode(false, false); } - static SMAddNode useful() { return SMAddNode(false, true); } - static SMAddNode invalid() { return SMAddNode(true, false); } - -private: - bool mInvalid, mUseful; - - SMAddNode(bool i, bool u) : mInvalid(i), mUseful(u) { ; } -}; - -// VFALCO TODO tidy up this loose function -extern bool SMANCombine(SMAddNode& existing, const SMAddNode& additional); - -class SHAMap : public IS_INSTANCE(SHAMap) -{ -public: - typedef boost::shared_ptr pointer; - typedef const boost::shared_ptr& ref; - - typedef std::pair SHAMapDiffItem; - typedef std::map SHAMapDiff; - typedef boost::unordered_map SHADirtyMap; - -public: - // build new map - SHAMap(SHAMapType t, uint32 seq = 1); - SHAMap(SHAMapType t, const uint256& hash); - - ~SHAMap() { mState = smsInvalid; } - - // Returns a new map that's a snapshot of this one. Force CoW - SHAMap::pointer snapShot(bool isMutable); - - // Remove nodes from memory - void dropCache(); - - void setLedgerSeq(uint32 lseq) { mLedgerSeq = lseq; } - - // hold the map stable across operations - ScopedLock Lock() const { return ScopedLock(mLock); } - - bool hasNode(const SHAMapNode& id); - void fetchRoot(const uint256& hash, SHAMapSyncFilter* filter); - - // normal hash access functions - bool hasItem(const uint256& id); - bool delItem(const uint256& id); - bool addItem(const SHAMapItem& i, bool isTransaction, bool hasMeta); - bool updateItem(const SHAMapItem& i, bool isTransaction, bool hasMeta); - SHAMapItem getItem(const uint256& id); - uint256 getHash() const { return root->getNodeHash(); } - uint256 getHash() { return root->getNodeHash(); } - - // save a copy if you have a temporary anyway - bool updateGiveItem(SHAMapItem::ref, bool isTransaction, bool hasMeta); - bool addGiveItem(SHAMapItem::ref, bool isTransaction, bool hasMeta); - - // save a copy if you only need a temporary - SHAMapItem::pointer peekItem(const uint256& id); - SHAMapItem::pointer peekItem(const uint256& id, uint256& hash); - SHAMapItem::pointer peekItem(const uint256& id, SHAMapTreeNode::TNType& type); - - // traverse functions - SHAMapItem::pointer peekFirstItem(); - SHAMapItem::pointer peekFirstItem(SHAMapTreeNode::TNType& type); - SHAMapItem::pointer peekLastItem(); - SHAMapItem::pointer peekNextItem(const uint256&); - SHAMapItem::pointer peekNextItem(const uint256&, SHAMapTreeNode::TNType& type); - SHAMapItem::pointer peekPrevItem(const uint256&); - - // comparison/sync functions - void getMissingNodes(std::vector& nodeIDs, std::vector& hashes, int max, - SHAMapSyncFilter* filter); - bool getNodeFat(const SHAMapNode& node, std::vector& nodeIDs, - std::list& rawNode, bool fatRoot, bool fatLeaves); - bool getRootNode(Serializer& s, SHANodeFormat format); - std::vector getNeededHashes(int max, SHAMapSyncFilter* filter); - SMAddNode addRootNode(const uint256& hash, Blob const& rootNode, SHANodeFormat format, - SHAMapSyncFilter* filter); - SMAddNode addRootNode(Blob const& rootNode, SHANodeFormat format, - SHAMapSyncFilter* filter); - SMAddNode addKnownNode(const SHAMapNode& nodeID, Blob const& rawNode, - SHAMapSyncFilter* filter); - - // status functions - void setImmutable() { assert(mState != smsInvalid); mState = smsImmutable; } - void clearImmutable() { mState = smsModifying; } - bool isSynching() const { return (mState == smsFloating) || (mState == smsSynching); } - void setSynching() { mState = smsSynching; } - void setFloating() { mState = smsFloating; } - void clearSynching() { mState = smsModifying; } - bool isValid() { return mState != smsInvalid; } - - // caution: otherMap must be accessed only by this function - // return value: true=successfully completed, false=too different - bool compare(SHAMap::ref otherMap, SHAMapDiff& differences, int maxCount); - - int armDirty(); - static int flushDirty(SHADirtyMap& dirtyMap, int maxNodes, HashedObjectType t, uint32 seq); - boost::shared_ptr disarmDirty(); - - void setSeq(uint32 seq) { mSeq = seq; } - uint32 getSeq() { return mSeq; } - - // overloads for backed maps - boost::shared_ptr fetchNodeExternal(const SHAMapNode& id, const uint256& hash); - - bool operator==(const SHAMap& s) { return getHash() == s.getHash(); } - - // trusted path operations - prove a particular node is in a particular ledger - std::list getTrustedPath(const uint256& index); - static Blob checkTrustedPath(const uint256& ledgerHash, const uint256& leafIndex, - const std::list& path); - - void walkMap(std::vector& missingNodes, int maxMissing); - - bool getPath(const uint256& index, std::vector< Blob >& nodes, SHANodeFormat format); - - bool deepCompare(SHAMap& other); - virtual void dump(bool withHashes = false); - - typedef std::pair< uint256, Blob > fetchPackEntry_t; - std::list getFetchPack(SHAMap* have, bool includeLeaves, int max); - - static void sweep() { fullBelowCache.sweep(); } - -private: - uint32 mSeq; - uint32 mLedgerSeq; // sequence number of ledger this is part of - mutable boost::recursive_mutex mLock; - boost::unordered_map mTNByID; - - boost::shared_ptr mDirtyNodes; - - SHAMapTreeNode::pointer root; - - SHAMapState mState; - - SHAMapType mType; - - static KeyCache fullBelowCache; - - void dirtyUp(std::stack& stack, const uint256& target, uint256 prevHash); - std::stack getStack(const uint256& id, bool include_nonmatching_leaf, bool partialOk); - SHAMapTreeNode::pointer walkTo(const uint256& id, bool modify); - SHAMapTreeNode* walkToPointer(const uint256& id); - SHAMapTreeNode::pointer checkCacheNode(const SHAMapNode&); - void returnNode(SHAMapTreeNode::pointer&, bool modify); - void trackNewNode(SHAMapTreeNode::pointer&); - - SHAMapTreeNode::pointer getNode(const SHAMapNode& id); - SHAMapTreeNode::pointer getNode(const SHAMapNode& id, const uint256& hash, bool modify); - SHAMapTreeNode* getNodePointer(const SHAMapNode& id, const uint256& hash); - SHAMapTreeNode* getNodePointer(const SHAMapNode& id, const uint256& hash, SHAMapSyncFilter* filter); - SHAMapTreeNode* firstBelow(SHAMapTreeNode*); - SHAMapTreeNode* lastBelow(SHAMapTreeNode*); - - SHAMapItem::pointer onlyBelow(SHAMapTreeNode*); - void eraseChildren(SHAMapTreeNode::pointer); - void dropBelow(SHAMapTreeNode*); - bool hasInnerNode(const SHAMapNode& nodeID, const uint256& hash); - bool hasLeafNode(const uint256& tag, const uint256& hash); - - bool walkBranch(SHAMapTreeNode* node, SHAMapItem::ref otherMapItem, bool isFirstMap, - SHAMapDiff& differences, int& maxCount); -}; - -#endif -// vim:ts=4 diff --git a/src/cpp/ripple/SHAMapSync.h b/src/cpp/ripple/SHAMapSync.h deleted file mode 100644 index 7ed55ccb9bd..00000000000 --- a/src/cpp/ripple/SHAMapSync.h +++ /dev/null @@ -1,68 +0,0 @@ -#ifndef SHAMAPSYNC_H -#define SHAMAPSYNC_H - -#include "SHAMap.h" -#include "Application.h" - -// Sync filters allow low-level SHAMapSync code to interact correctly with -// higher-level structures such as caches and transaction stores - -// This class is needed on both add and check functions -class ConsensusTransSetSF : public SHAMapSyncFilter -{ // sync filter for transaction sets during consensus building -public: - ConsensusTransSetSF() { ; } - - virtual void gotNode(bool fromFilter, const SHAMapNode& id, const uint256& nodeHash, - Blob const& nodeData, SHAMapTreeNode::TNType); - - virtual bool haveNode(const SHAMapNode& id, const uint256& nodeHash, Blob & nodeData); -}; - -// This class is only needed on add functions -// sync filter for account state nodes during ledger sync -class AccountStateSF : public SHAMapSyncFilter -{ -public: - AccountStateSF(uint32 ledgerSeq) : mLedgerSeq(ledgerSeq) - { ; } - - virtual void gotNode(bool fromFilter, const SHAMapNode& id, const uint256& nodeHash, - Blob const& nodeData, SHAMapTreeNode::TNType) - { - theApp->getHashedObjectStore().store(hotACCOUNT_NODE, mLedgerSeq, nodeData, nodeHash); - } - virtual bool haveNode(const SHAMapNode& id, const uint256& nodeHash, Blob & nodeData) - { - return theApp->getOPs().getFetchPack(nodeHash, nodeData); - } - -private: - uint32 mLedgerSeq; -}; - -// This class is only needed on add functions -// sync filter for transactions tree during ledger sync -class TransactionStateSF : public SHAMapSyncFilter -{ -public: - TransactionStateSF(uint32 ledgerSeq) : mLedgerSeq(ledgerSeq) - { ; } - - virtual void gotNode(bool fromFilter, const SHAMapNode& id, const uint256& nodeHash, - Blob const& nodeData, SHAMapTreeNode::TNType type) - { - theApp->getHashedObjectStore().store( - (type == SHAMapTreeNode::tnTRANSACTION_NM) ? hotTRANSACTION : hotTRANSACTION_NODE, - mLedgerSeq, nodeData, nodeHash); - } - virtual bool haveNode(const SHAMapNode& id, const uint256& nodeHash, Blob & nodeData) - { - return theApp->getOPs().getFetchPack(nodeHash, nodeData); - } - -private: - uint32 mLedgerSeq; -}; - -#endif diff --git a/src/cpp/ripple/SerializedValidation.cpp b/src/cpp/ripple/SerializedValidation.cpp index d2360de5ba2..056b9585c0f 100644 --- a/src/cpp/ripple/SerializedValidation.cpp +++ b/src/cpp/ripple/SerializedValidation.cpp @@ -15,7 +15,7 @@ SerializedValidation::SerializedValidation (SerializerIterator& sit, bool checkS } SerializedValidation::SerializedValidation ( - const uint256& ledgerHash, uint32 signTime, + uint256 const& ledgerHash, uint32 signTime, const RippleAddress& raPub, bool isFull) : STObject (getFormat (), sfValidation) , mTrusted (false) @@ -70,7 +70,7 @@ bool SerializedValidation::isValid() const return isValid(getSigningHash()); } -bool SerializedValidation::isValid(const uint256& signingHash) const +bool SerializedValidation::isValid(uint256 const& signingHash) const { try { diff --git a/src/cpp/ripple/SerializedValidation.h b/src/cpp/ripple/SerializedValidation.h index 68a300af8b3..b962b95bb9e 100644 --- a/src/cpp/ripple/SerializedValidation.h +++ b/src/cpp/ripple/SerializedValidation.h @@ -17,7 +17,7 @@ class SerializedValidation SerializedValidation (SerializerIterator& sit, bool checkSignature = true); // Does not sign the validation - SerializedValidation (const uint256& ledgerHash, uint32 signTime, const RippleAddress& raPub, bool isFull); + SerializedValidation (uint256 const& ledgerHash, uint32 signTime, const RippleAddress& raPub, bool isFull); uint256 getLedgerHash() const; uint32 getSignTime() const; @@ -28,7 +28,7 @@ class SerializedValidation bool isFull() const; bool isTrusted() const { return mTrusted; } uint256 getSigningHash() const; - bool isValid(const uint256&) const; + bool isValid(uint256 const& ) const; void setTrusted() { mTrusted = true; } Blob getSigned() const; @@ -37,9 +37,9 @@ class SerializedValidation void sign(const RippleAddress& raPrivate); // The validation this replaced - const uint256& getPreviousHash() { return mPreviousHash; } - bool isPreviousHash(const uint256& h) const { return mPreviousHash == h; } - void setPreviousHash(const uint256& h) { mPreviousHash = h; } + uint256 const& getPreviousHash() { return mPreviousHash; } + bool isPreviousHash(uint256 const& h) const { return mPreviousHash == h; } + void setPreviousHash(uint256 const& h) { mPreviousHash = h; } private: static SOTemplate const& getFormat (); diff --git a/src/cpp/ripple/Transaction.cpp b/src/cpp/ripple/Transaction.cpp index 0ce91b04279..7b4bf886e78 100644 --- a/src/cpp/ripple/Transaction.cpp +++ b/src/cpp/ripple/Transaction.cpp @@ -227,7 +227,7 @@ Transaction::pointer Transaction::transactionFromSQL(const std::string& sql) } -Transaction::pointer Transaction::load(const uint256& id) +Transaction::pointer Transaction::load(uint256 const& id) { std::string sql = "SELECT LedgerSeq,Status,RawTxn FROM Transactions WHERE TransID='"; sql.append(id.GetHex()); @@ -236,14 +236,14 @@ Transaction::pointer Transaction::load(const uint256& id) } bool Transaction::convertToTransactions(uint32 firstLedgerSeq, uint32 secondLedgerSeq, - bool checkFirstTransactions, bool checkSecondTransactions, const SHAMap::SHAMapDiff& inMap, + bool checkFirstTransactions, bool checkSecondTransactions, const SHAMap::Delta& inMap, std::map >& outMap) { // convert a straight SHAMap payload difference to a transaction difference table // return value: true=ledgers are valid, false=a ledger is invalid - SHAMap::SHAMapDiff::const_iterator it; + SHAMap::Delta::const_iterator it; for(it = inMap.begin(); it != inMap.end(); ++it) { - const uint256& id = it->first; + uint256 const& id = it->first; SHAMapItem::ref first = it->second.first; SHAMapItem::ref second = it->second.second; diff --git a/src/cpp/ripple/Transaction.h b/src/cpp/ripple/Transaction.h index ee406a2d176..47e8273d13a 100644 --- a/src/cpp/ripple/Transaction.h +++ b/src/cpp/ripple/Transaction.h @@ -1,19 +1,10 @@ -#ifndef __TRANSACTION__ -#define __TRANSACTION__ +#ifndef RIPPLE_TRANSACTION_H +#define RIPPLE_TRANSACTION_H // // Transactions should be constructed in JSON with. Use STObject::parseJson to obtain a binary version. // -#include - -#include -#include -#include - -#include "SHAMap.h" -#include "SerializedTransaction.h" - class Database; enum TransStatus @@ -38,20 +29,8 @@ class Transaction : public boost::enable_shared_from_this, private typedef boost::shared_ptr pointer; typedef const pointer& ref; -private: - uint256 mTransactionID; - RippleAddress mAccountFrom; - RippleAddress mFromPubKey; // Sign transaction with this. mSignPubKey - RippleAddress mSourcePrivate; // Sign transaction with this. - - uint32 mInLedger; - TransStatus mStatus; - TER mResult; - - SerializedTransaction::pointer mTransaction; - public: - Transaction(SerializedTransaction::ref st, bool bValidate); + Transaction (SerializedTransaction::ref st, bool bValidate); static Transaction::pointer sharedTransaction(Blob const& vucTransaction, bool bValidate); static Transaction::pointer transactionFromSQL(Database* db, bool bValidate); @@ -65,38 +44,46 @@ class Transaction : public boost::enable_shared_from_this, private uint32 uSourceTag); // User call back value. - bool sign(const RippleAddress& naAccountPrivate); - bool checkSign() const; - void updateID() { mTransactionID=mTransaction->getTransactionID(); } + bool sign (const RippleAddress& naAccountPrivate); + + bool checkSign() const; + + void updateID() { mTransactionID=mTransaction->getTransactionID(); } SerializedTransaction::ref getSTransaction() { return mTransaction; } - const uint256& getID() const { return mTransactionID; } - const RippleAddress& getFromAccount() const { return mAccountFrom; } - STAmount getAmount() const { return mTransaction->getFieldU64(sfAmount); } - STAmount getFee() const { return mTransaction->getTransactionFee(); } - uint32 getFromAccountSeq() const { return mTransaction->getSequence(); } - uint32 getSourceTag() const { return mTransaction->getFieldU32(sfSourceTag); } - Blob getSignature() const { return mTransaction->getSignature(); } - uint32 getLedger() const { return mInLedger; } - TransStatus getStatus() const { return mStatus; } + uint256 const& getID() const { return mTransactionID; } + + const RippleAddress& getFromAccount() const { return mAccountFrom; } + + STAmount getAmount() const { return mTransaction->getFieldU64(sfAmount); } + + STAmount getFee() const { return mTransaction->getTransactionFee(); } + + uint32 getFromAccountSeq() const { return mTransaction->getSequence(); } + + uint32 getSourceTag() const { return mTransaction->getFieldU32(sfSourceTag); } + + // VFALCO TODO Should this return a const reference? + Blob getSignature() const { return mTransaction->getSignature(); } + + LedgerIndex getLedger() const { return mInLedger; } + + TransStatus getStatus() const { return mStatus; } TER getResult() { return mResult; } - void setResult(TER terResult) { mResult = terResult; } + + void setResult(TER terResult) { mResult = terResult; } void setStatus(TransStatus status, uint32 ledgerSeq); - void setStatus(TransStatus status) { mStatus=status; } - void setLedger(uint32 ledger) { mInLedger = ledger; } + + void setStatus(TransStatus status) { mStatus=status; } + + void setLedger(LedgerIndex ledger) { mInLedger = ledger; } // database functions void save(); - static Transaction::pointer load(const uint256& id); - - // conversion function - static bool convertToTransactions(uint32 ourLedgerSeq, uint32 otherLedgerSeq, - bool checkFirstTransactions, bool checkSecondTransactions, const SHAMap::SHAMapDiff& inMap, - std::map >& outMap); - + bool operator<(const Transaction&) const; bool operator>(const Transaction&) const; bool operator==(const Transaction&) const; @@ -104,12 +91,31 @@ class Transaction : public boost::enable_shared_from_this, private bool operator<=(const Transaction&) const; bool operator>=(const Transaction&) const; - Json::Value getJson(int options, bool binary = false) const; + Json::Value getJson(int options, bool binary = false) const; - static bool isHexTxID(const std::string&); + static Transaction::pointer load(uint256 const& id); + + // conversion function + static bool convertToTransactions(uint32 ourLedgerSeq, uint32 otherLedgerSeq, + bool checkFirstTransactions, bool checkSecondTransactions, const SHAMap::Delta& inMap, + std::map >& outMap); + + static bool isHexTxID(const std::string&); protected: static Transaction::pointer transactionFromSQL(const std::string& statement); + +private: + uint256 mTransactionID; + RippleAddress mAccountFrom; + RippleAddress mFromPubKey; // Sign transaction with this. mSignPubKey + RippleAddress mSourcePrivate; // Sign transaction with this. + + LedgerIndex mInLedger; + TransStatus mStatus; + TER mResult; + + SerializedTransaction::pointer mTransaction; }; #endif diff --git a/src/cpp/ripple/TransactionAcquire.cpp b/src/cpp/ripple/TransactionAcquire.cpp index 3024b2435ec..b636b0499be 100644 --- a/src/cpp/ripple/TransactionAcquire.cpp +++ b/src/cpp/ripple/TransactionAcquire.cpp @@ -8,7 +8,7 @@ typedef std::map::value_type u256_lct_pair; DECLARE_INSTANCE(TransactionAcquire); -TransactionAcquire::TransactionAcquire(const uint256& hash) : PeerSet(hash, TX_ACQUIRE_TIMEOUT), mHaveRoot(false) +TransactionAcquire::TransactionAcquire(uint256 const& hash) : PeerSet(hash, TX_ACQUIRE_TIMEOUT), mHaveRoot(false) { mMap = boost::make_shared(smtTRANSACTION, hash); } @@ -131,23 +131,23 @@ void TransactionAcquire::trigger(Peer::ref peer) } } -SMAddNode TransactionAcquire::takeNodes(const std::list& nodeIDs, +SHAMapAddNode TransactionAcquire::takeNodes(const std::list& nodeIDs, const std::list< Blob >& data, Peer::ref peer) { if (mComplete) { WriteLog (lsTRACE, TransactionAcquire) << "TX set complete"; - return SMAddNode(); + return SHAMapAddNode(); } if (mFailed) { WriteLog (lsTRACE, TransactionAcquire) << "TX set failed"; - return SMAddNode(); + return SHAMapAddNode(); } try { if (nodeIDs.empty()) - return SMAddNode::invalid(); + return SHAMapAddNode::invalid(); std::list::const_iterator nodeIDit = nodeIDs.begin(); std::list< Blob >::const_iterator nodeDatait = data.begin(); ConsensusTransSetSF sf; @@ -158,12 +158,12 @@ SMAddNode TransactionAcquire::takeNodes(const std::list& nodeIDs, if (mHaveRoot) { WriteLog (lsWARNING, TransactionAcquire) << "Got root TXS node, already have it"; - return SMAddNode(); + return SHAMapAddNode(); } if (!mMap->addRootNode(getHash(), *nodeDatait, snfWIRE, NULL)) { WriteLog (lsWARNING, TransactionAcquire) << "TX acquire got bad root node"; - return SMAddNode::invalid(); + return SHAMapAddNode::invalid(); } else mHaveRoot = true; @@ -171,64 +171,18 @@ SMAddNode TransactionAcquire::takeNodes(const std::list& nodeIDs, else if (!mMap->addKnownNode(*nodeIDit, *nodeDatait, &sf)) { WriteLog (lsWARNING, TransactionAcquire) << "TX acquire got bad non-root node"; - return SMAddNode::invalid(); + return SHAMapAddNode::invalid(); } ++nodeIDit; ++nodeDatait; } trigger(peer); progress(); - return SMAddNode::useful(); + return SHAMapAddNode::useful(); } catch (...) { WriteLog (lsERROR, TransactionAcquire) << "Peer sends us junky transaction node data"; - return SMAddNode::invalid(); + return SHAMapAddNode::invalid(); } } - -void ConsensusTransSetSF::gotNode(bool fromFilter, const SHAMapNode& id, const uint256& nodeHash, - Blob const& nodeData, SHAMapTreeNode::TNType type) -{ - if (fromFilter) - return; - theApp->getTempNodeCache().store(nodeHash, nodeData); - if ((type == SHAMapTreeNode::tnTRANSACTION_NM) && (nodeData.size() > 16)) - { // this is a transaction, and we didn't have it - WriteLog (lsDEBUG, TransactionAcquire) << "Node on our acquiring TX set is TXN we don't have"; - try - { - Serializer s(nodeData.begin() + 4, nodeData.end()); // skip prefix - SerializerIterator sit(s); - SerializedTransaction::pointer stx = boost::make_shared(boost::ref(sit)); - assert(stx->getTransactionID() == nodeHash); - theApp->getJobQueue().addJob(jtTRANSACTION, "TXS->TXN", - BIND_TYPE(&NetworkOPs::submitTransaction, &theApp->getOPs(), P_1, stx, NetworkOPs::stCallback())); - } - catch (...) - { - WriteLog (lsWARNING, TransactionAcquire) << "Fetched invalid transaction in proposed set"; - } - } -} - -bool ConsensusTransSetSF::haveNode(const SHAMapNode& id, const uint256& nodeHash, - Blob & nodeData) -{ - if (theApp->getTempNodeCache().retrieve(nodeHash, nodeData)) - return true; - - Transaction::pointer txn = Transaction::load(nodeHash); - if (txn) - { // this is a transaction, and we have it - WriteLog (lsDEBUG, TransactionAcquire) << "Node in our acquiring TX set is TXN we have"; - Serializer s; - s.add32(sHP_TransactionID); - txn->getSTransaction()->add(s, true); - assert(s.getSHA512Half() == nodeHash); - nodeData = s.peekData(); - return true; - } - - return false; -} diff --git a/src/cpp/ripple/TransactionEngine.h b/src/cpp/ripple/TransactionEngine.h index 5e427089dde..13664da5548 100644 --- a/src/cpp/ripple/TransactionEngine.h +++ b/src/cpp/ripple/TransactionEngine.h @@ -1,18 +1,6 @@ #ifndef __TRANSACTIONENGINE__ #define __TRANSACTIONENGINE__ -#include -#include -#include -#include -#include - -#include "Ledger.h" -#include "SerializedTransaction.h" -#include "SerializedLedger.h" -#include "LedgerEntrySet.h" - - DEFINE_INSTANCE(TransactionEngine); // A TransactionEngine applies serialized transactions to a ledger @@ -30,7 +18,7 @@ class TransactionEngine : private IS_INSTANCE(TransactionEngine) TER takeOffers( bool bPassive, - const uint256& uBookBase, + uint256 const& uBookBase, const uint160& uTakerAccountID, SLE::ref sleTakerAccount, const STAmount& saTakerPays, @@ -57,8 +45,8 @@ class TransactionEngine : private IS_INSTANCE(TransactionEngine) Ledger::ref getLedger() { return mLedger; } void setLedger(Ledger::ref ledger) { assert(ledger); mLedger = ledger; } - SLE::pointer entryCreate(LedgerEntryType type, const uint256& index) { return mNodes.entryCreate(type, index); } - SLE::pointer entryCache(LedgerEntryType type, const uint256& index) { return mNodes.entryCache(type, index); } + SLE::pointer entryCreate(LedgerEntryType type, uint256 const& index) { return mNodes.entryCreate(type, index); } + SLE::pointer entryCache(LedgerEntryType type, uint256 const& index) { return mNodes.entryCache(type, index); } void entryDelete(SLE::ref sleEntry) { mNodes.entryDelete(sleEntry); } void entryModify(SLE::ref sleEntry) { mNodes.entryModify(sleEntry); } diff --git a/src/cpp/ripple/TransactionMaster.cpp b/src/cpp/ripple/TransactionMaster.cpp index 585c9b6ee07..a0d046a1e75 100644 --- a/src/cpp/ripple/TransactionMaster.cpp +++ b/src/cpp/ripple/TransactionMaster.cpp @@ -12,7 +12,7 @@ TransactionMaster::TransactionMaster() : mCache("TransactionCache", CACHED_TRANS ; } -bool TransactionMaster::inLedger(const uint256& hash, uint32 ledger) +bool TransactionMaster::inLedger(uint256 const& hash, uint32 ledger) { Transaction::pointer txn = mCache.fetch(hash); if (!txn) @@ -21,7 +21,7 @@ bool TransactionMaster::inLedger(const uint256& hash, uint32 ledger) return true; } -Transaction::pointer TransactionMaster::fetch(const uint256& txnID, bool checkDisk) +Transaction::pointer TransactionMaster::fetch(uint256 const& txnID, bool checkDisk) { Transaction::pointer txn = mCache.fetch(txnID); if (!checkDisk || txn) diff --git a/src/cpp/ripple/TransactionMaster.h b/src/cpp/ripple/TransactionMaster.h index 38b84e930ee..d78d7f67902 100644 --- a/src/cpp/ripple/TransactionMaster.h +++ b/src/cpp/ripple/TransactionMaster.h @@ -10,12 +10,12 @@ class TransactionMaster public: TransactionMaster(); - Transaction::pointer fetch(const uint256&, bool checkDisk); + Transaction::pointer fetch(uint256 const& , bool checkDisk); SerializedTransaction::pointer fetch(SHAMapItem::ref item, SHAMapTreeNode:: TNType type, bool checkDisk, uint32 uCommitLedger); // return value: true = we had the transaction already - bool inLedger(const uint256& hash, uint32 ledger); + bool inLedger(uint256 const& hash, uint32 ledger); bool canonicalize(Transaction::pointer& txn, bool maybeNew); void sweep(void) { mCache.sweep(); } diff --git a/src/cpp/ripple/TransactionMeta.cpp b/src/cpp/ripple/TransactionMeta.cpp index f7c3ad0436d..eed2a62e09f 100644 --- a/src/cpp/ripple/TransactionMeta.cpp +++ b/src/cpp/ripple/TransactionMeta.cpp @@ -3,7 +3,7 @@ SETUP_LOG (TransactionMetaSet) -TransactionMetaSet::TransactionMetaSet(const uint256& txid, uint32 ledger, Blob const& vec) : +TransactionMetaSet::TransactionMetaSet(uint256 const& txid, uint32 ledger, Blob const& vec) : mTransactionID(txid), mLedger(ledger), mNodes(sfAffectedNodes, 32) { Serializer s(vec); @@ -19,7 +19,7 @@ TransactionMetaSet::TransactionMetaSet(const uint256& txid, uint32 ledger, Blob mNodes = * dynamic_cast(&obj->getField(sfAffectedNodes)); } -bool TransactionMetaSet::isNodeAffected(const uint256& node) const +bool TransactionMetaSet::isNodeAffected(uint256 const& node) const { BOOST_FOREACH(const STObject& it, mNodes) if (it.getFieldH256(sfLedgerIndex) == node) @@ -27,7 +27,7 @@ bool TransactionMetaSet::isNodeAffected(const uint256& node) const return false; } -void TransactionMetaSet::setAffectedNode(const uint256& node, SField::ref type, uint16 nodeType) +void TransactionMetaSet::setAffectedNode(uint256 const& node, SField::ref type, uint16 nodeType) { // make sure the node exists and force its type BOOST_FOREACH(STObject& it, mNodes) { @@ -122,7 +122,7 @@ STObject& TransactionMetaSet::getAffectedNode(SLE::ref node, SField::ref type) return obj; } -STObject& TransactionMetaSet::getAffectedNode(const uint256& node) +STObject& TransactionMetaSet::getAffectedNode(uint256 const& node) { BOOST_FOREACH(STObject& it, mNodes) { @@ -133,7 +133,7 @@ STObject& TransactionMetaSet::getAffectedNode(const uint256& node) throw std::runtime_error("Affected node not found"); } -const STObject& TransactionMetaSet::peekAffectedNode(const uint256& node) const +const STObject& TransactionMetaSet::peekAffectedNode(uint256 const& node) const { BOOST_FOREACH(const STObject& it, mNodes) if (it.getFieldH256(sfLedgerIndex) == node) @@ -141,7 +141,7 @@ const STObject& TransactionMetaSet::peekAffectedNode(const uint256& node) const throw std::runtime_error("Affected node not found"); } -void TransactionMetaSet::init(const uint256& id, uint32 ledger) +void TransactionMetaSet::init(uint256 const& id, uint32 ledger) { mTransactionID = id; mLedger = ledger; @@ -154,7 +154,7 @@ void TransactionMetaSet::swap(TransactionMetaSet& s) mNodes.swap(s.mNodes); } -bool TransactionMetaSet::thread(STObject& node, const uint256& prevTxID, uint32 prevLgrID) +bool TransactionMetaSet::thread(STObject& node, uint256 const& prevTxID, uint32 prevLgrID) { if (node.getFieldIndex(sfPreviousTxnID) == -1) { diff --git a/src/cpp/ripple/TransactionMeta.h b/src/cpp/ripple/TransactionMeta.h index 6dfd879ae7b..7e1aece218c 100644 --- a/src/cpp/ripple/TransactionMeta.h +++ b/src/cpp/ripple/TransactionMeta.h @@ -1,12 +1,5 @@ -#ifndef __TRANSACTIONMETA__ -#define __TRANSACTIONMETA__ - -#include - -#include -#include - -#include "SerializedLedger.h" +#ifndef RIPPLE_TRANSACTIONMETA_H +#define RIPPLE_TRANSACTIONMETA_H class TransactionMetaSet { @@ -16,25 +9,25 @@ class TransactionMetaSet public: TransactionMetaSet() : mLedger(0), mIndex(static_cast(-1)), mResult(255) { ; } - TransactionMetaSet(const uint256& txID, uint32 ledger, uint32 index) : + TransactionMetaSet(uint256 const& txID, uint32 ledger, uint32 index) : mTransactionID(txID), mLedger(ledger), mIndex(static_cast(-1)), mResult(255) { ; } - TransactionMetaSet(const uint256& txID, uint32 ledger, Blob const&); + TransactionMetaSet(uint256 const& txID, uint32 ledger, Blob const&); - void init(const uint256& transactionID, uint32 ledger); + void init(uint256 const& transactionID, uint32 ledger); void clear() { mNodes.clear(); } void swap(TransactionMetaSet&); - const uint256& getTxID() { return mTransactionID; } + uint256 const& getTxID() { return mTransactionID; } uint32 getLgrSeq() { return mLedger; } int getResult() const { return mResult; } TER getResultTER() const { return static_cast(mResult); } uint32 getIndex() const { return mIndex; } - bool isNodeAffected(const uint256&) const; - void setAffectedNode(const uint256&, SField::ref type, uint16 nodeType); + bool isNodeAffected(uint256 const& ) const; + void setAffectedNode(uint256 const& , SField::ref type, uint16 nodeType); STObject& getAffectedNode(SLE::ref node, SField::ref type); // create if needed - STObject& getAffectedNode(const uint256&); - const STObject& peekAffectedNode(const uint256&) const; + STObject& getAffectedNode(uint256 const& ); + const STObject& peekAffectedNode(uint256 const& ) const; std::vector getAffectedAccounts(); @@ -44,7 +37,7 @@ class TransactionMetaSet STObject getAsObject() const; STArray& getNodes(){ return(mNodes); } - static bool thread(STObject& node, const uint256& prevTxID, uint32 prevLgrID); + static bool thread(STObject& node, uint256 const& prevTxID, uint32 prevLgrID); private: uint256 mTransactionID; diff --git a/src/cpp/ripple/TransactionQueue.cpp b/src/cpp/ripple/TransactionQueue.cpp index 54a6c05e613..5b422488390 100644 --- a/src/cpp/ripple/TransactionQueue.cpp +++ b/src/cpp/ripple/TransactionQueue.cpp @@ -45,7 +45,7 @@ bool TXQueue::addEntryForExecution(TXQEntry::ref entry) return true; // A thread needs to handle this account } -TXQEntry::pointer TXQueue::removeEntry(const uint256& id) +TXQEntry::pointer TXQueue::removeEntry(uint256 const& id) { TXQEntry::pointer ret; diff --git a/src/cpp/ripple/TransactionQueue.h b/src/cpp/ripple/TransactionQueue.h index 041b6ea37ef..4d66b380a07 100644 --- a/src/cpp/ripple/TransactionQueue.h +++ b/src/cpp/ripple/TransactionQueue.h @@ -27,7 +27,7 @@ class TXQEntry Transaction::ref getTransaction() const { return mTxn; } bool getSigChecked() const { return mSigChecked; } - const uint256& getID() const { return mTxn->getID(); } + uint256 const& getID() const { return mTxn->getID(); } void doCallbacks(TER); @@ -53,7 +53,7 @@ class TXQueue bool addEntryForExecution(TXQEntry::ref); // Call if signature is bad (returns entry so you can run its callbacks) - TXQEntry::pointer removeEntry(const uint256& txID); + TXQEntry::pointer removeEntry(uint256 const& txID); // Transaction execution interface void getJob(TXQEntry::pointer&); diff --git a/src/cpp/ripple/Transactor.h b/src/cpp/ripple/Transactor.h index 0d863806944..bdb580ddf8d 100644 --- a/src/cpp/ripple/Transactor.h +++ b/src/cpp/ripple/Transactor.h @@ -1,7 +1,6 @@ #ifndef __TRANSACTOR__ #define __TRANSACTOR__ -#include "SerializedTransaction.h" #include "TransactionEngine.h" class Transactor diff --git a/src/cpp/ripple/WSConnection.h b/src/cpp/ripple/WSConnection.h index b8aa57de21a..d19cc95bfdd 100644 --- a/src/cpp/ripple/WSConnection.h +++ b/src/cpp/ripple/WSConnection.h @@ -12,7 +12,6 @@ #include "WSDoor.h" #include "Application.h" -#include "NetworkOPs.h" #include "CallRPC.h" #include "LoadManager.h" #include "RPCErr.h" diff --git a/src/cpp/ripple/WSDoor.cpp b/src/cpp/ripple/WSDoor.cpp index 2e91b5768da..fac356c1593 100644 --- a/src/cpp/ripple/WSDoor.cpp +++ b/src/cpp/ripple/WSDoor.cpp @@ -5,7 +5,6 @@ //#include "../websocketpp/src/websocketpp.hpp" #include "Application.h" -#include "NetworkOPs.h" #include "WSConnection.h" #include "WSHandler.h" diff --git a/src/cpp/ripple/ripple_AcceptedLedger.cpp b/src/cpp/ripple/ripple_AcceptedLedger.cpp new file mode 100644 index 00000000000..0f60d3983db --- /dev/null +++ b/src/cpp/ripple/ripple_AcceptedLedger.cpp @@ -0,0 +1,36 @@ + +TaggedCache AcceptedLedger::s_cache("AcceptedLedger", 4, 60); + +AcceptedLedger::AcceptedLedger(Ledger::ref ledger) : mLedger(ledger) +{ + SHAMap& txSet = *ledger->peekTransactionMap(); + for (SHAMapItem::pointer item = txSet.peekFirstItem(); !!item; item = txSet.peekNextItem(item->getTag())) + { + SerializerIterator sit(item->peekSerializer()); + insert(boost::make_shared(ledger->getLedgerSeq(), boost::ref(sit))); + } +} + +AcceptedLedger::pointer AcceptedLedger::makeAcceptedLedger(Ledger::ref ledger) +{ + AcceptedLedger::pointer ret = s_cache.fetch(ledger->getHash()); + if (ret) + return ret; + ret = AcceptedLedger::pointer(new AcceptedLedger(ledger)); + s_cache.canonicalize(ledger->getHash(), ret); + return ret; +} + +void AcceptedLedger::insert(AcceptedLedgerTx::ref at) +{ + assert(mMap.find(at->getIndex()) == mMap.end()); + mMap.insert(std::make_pair(at->getIndex(), at)); +} + +AcceptedLedgerTx::pointer AcceptedLedger::getTxn(int i) const +{ + map_t::const_iterator it = mMap.find(i); + if (it == mMap.end()) + return AcceptedLedgerTx::pointer(); + return it->second; +} diff --git a/src/cpp/ripple/ripple_AcceptedLedger.h b/src/cpp/ripple/ripple_AcceptedLedger.h new file mode 100644 index 00000000000..d723bdd5fdb --- /dev/null +++ b/src/cpp/ripple/ripple_AcceptedLedger.h @@ -0,0 +1,42 @@ +#ifndef ACCEPTED_LEDGER_H +#define ACCEPTED_LEDGER_H + +/** + +*/ +class AcceptedLedger +{ +public: + typedef boost::shared_ptr pointer; + typedef const pointer& ret; + typedef std::map map_t; // Must be an ordered map! + typedef map_t::value_type value_type; + typedef map_t::const_iterator const_iterator; + +public: + static pointer makeAcceptedLedger(Ledger::ref ledger); + static void sweep() { s_cache.sweep(); } + + Ledger::ref getLedger() const { return mLedger; } + const map_t& getMap() const { return mMap; } + + int getLedgerSeq() const { return mLedger->getLedgerSeq(); } + int getTxnCount() const { return mMap.size(); } + + static float getCacheHitRate() { return s_cache.getHitRate(); } + + AcceptedLedgerTx::pointer getTxn(int) const; + +private: + explicit AcceptedLedger (Ledger::ref ledger); + + void insert (AcceptedLedgerTx::ref); + +private: + static TaggedCache s_cache; + + Ledger::pointer mLedger; + map_t mMap; +}; + +#endif diff --git a/src/cpp/ripple/ripple_AcceptedLedgerTx.cpp b/src/cpp/ripple/ripple_AcceptedLedgerTx.cpp new file mode 100644 index 00000000000..ae36920d79a --- /dev/null +++ b/src/cpp/ripple/ripple_AcceptedLedgerTx.cpp @@ -0,0 +1,53 @@ +AcceptedLedgerTx::AcceptedLedgerTx(uint32 seq, SerializerIterator& sit) +{ + Serializer txnSer(sit.getVL()); + SerializerIterator txnIt(txnSer); + + mTxn = boost::make_shared(boost::ref(txnIt)); + mRawMeta= sit.getVL(); + mMeta = boost::make_shared(mTxn->getTransactionID(), seq, mRawMeta); + mAffected = mMeta->getAffectedAccounts(); + mResult = mMeta->getResultTER(); + buildJson(); +} + +AcceptedLedgerTx::AcceptedLedgerTx(SerializedTransaction::ref txn, TransactionMetaSet::ref met) : + mTxn(txn), mMeta(met), mAffected(met->getAffectedAccounts()) +{ + mResult = mMeta->getResultTER(); + buildJson(); +} + +AcceptedLedgerTx::AcceptedLedgerTx(SerializedTransaction::ref txn, TER result) : + mTxn(txn), mResult(result), mAffected(txn->getMentionedAccounts()) +{ + buildJson(); +} + +std::string AcceptedLedgerTx::getEscMeta() const +{ + assert(!mRawMeta.empty()); + return sqlEscape(mRawMeta); +} + +void AcceptedLedgerTx::buildJson() +{ + mJson = Json::objectValue; + mJson["transaction"] = mTxn->getJson(0); + if (mMeta) + { + mJson["meta"] = mMeta->getJson(0); + mJson["raw_meta"] = strHex(mRawMeta); + } + mJson["result"] = transHuman(mResult); + + if (!mAffected.empty()) + { + Json::Value& affected = (mJson["affected"] = Json::arrayValue); + BOOST_FOREACH(const RippleAddress& ra, mAffected) + { + affected.append(ra.humanAccountID()); + } + } +} + diff --git a/src/cpp/ripple/ripple_AcceptedLedgerTx.h b/src/cpp/ripple/ripple_AcceptedLedgerTx.h new file mode 100644 index 00000000000..a7a29e5ea9f --- /dev/null +++ b/src/cpp/ripple/ripple_AcceptedLedgerTx.h @@ -0,0 +1,53 @@ +#ifndef RIPPLE_ACCEPTEDLEDGERTX_H +#define RIPPLE_ACCEPTEDLEDGERTX_H + +/*============================================================================*/ +/** + A transaction that is in a closed ledger. + + Description + + @code + @endcode + + @see {uri} + + @ingroup ripple_ledger +*/ +class AcceptedLedgerTx +{ +public: + typedef boost::shared_ptr pointer; + typedef const pointer& ref; + +public: + AcceptedLedgerTx (LedgerIndex ledgerSeq, SerializerIterator& sit); + AcceptedLedgerTx (SerializedTransaction::ref, TransactionMetaSet::ref); + AcceptedLedgerTx (SerializedTransaction::ref, TER result); + + SerializedTransaction::ref getTxn() const { return mTxn; } + TransactionMetaSet::ref getMeta() const { return mMeta; } + std::vector const& getAffected() const { return mAffected; } + + TxID getTransactionID() const { return mTxn->getTransactionID(); } + TransactionType getTxnType() const { return mTxn->getTxnType(); } + TER getResult() const { return mResult; } + uint32 getTxnSeq() const { return mMeta->getIndex(); } + + bool isApplied() const { return !!mMeta; } + int getIndex() const { return mMeta ? mMeta->getIndex() : 0; } + std::string getEscMeta() const; + Json::Value getJson() const { return mJson; } + +private: + SerializedTransaction::pointer mTxn; + TransactionMetaSet::pointer mMeta; + TER mResult; + std::vector mAffected; + Blob mRawMeta; + Json::Value mJson; + + void buildJson(); +}; + +#endif diff --git a/src/cpp/ripple/ripple_CanonicalTXSet.cpp b/src/cpp/ripple/ripple_CanonicalTXSet.cpp new file mode 100644 index 00000000000..288c37776af --- /dev/null +++ b/src/cpp/ripple/ripple_CanonicalTXSet.cpp @@ -0,0 +1,57 @@ + +bool CanonicalTXSet::Key::operator< (Key const& rhs) const +{ + if (mAccount < rhs.mAccount) return true; + if (mAccount > rhs.mAccount) return false; + if (mSeq < rhs.mSeq) return true; + if (mSeq > rhs.mSeq) return false; + return mTXid < rhs.mTXid; +} + +bool CanonicalTXSet::Key::operator> (Key const& rhs) const +{ + if (mAccount > rhs.mAccount) return true; + if (mAccount < rhs.mAccount) return false; + if (mSeq > rhs.mSeq) return true; + if (mSeq < rhs.mSeq) return false; + return mTXid > rhs.mTXid; +} + +bool CanonicalTXSet::Key::operator<= (Key const& rhs) const +{ + if (mAccount < rhs.mAccount) return true; + if (mAccount > rhs.mAccount) return false; + if (mSeq < rhs.mSeq) return true; + if (mSeq > rhs.mSeq) return false; + return mTXid <= rhs.mTXid; +} + +bool CanonicalTXSet::Key::operator>=(Key const& rhs)const +{ + if (mAccount > rhs.mAccount) return true; + if (mAccount < rhs.mAccount) return false; + if (mSeq > rhs.mSeq) return true; + if (mSeq < rhs.mSeq) return false; + return mTXid >= rhs.mTXid; +} + +void CanonicalTXSet::push_back (SerializedTransaction::ref txn) +{ + uint256 effectiveAccount = mSetHash; + + effectiveAccount ^= txn->getSourceAccount().getAccountID().to256 (); + + mMap.insert (std::make_pair ( + Key (effectiveAccount, txn->getSequence(), txn->getTransactionID ()), + txn)); +} + +CanonicalTXSet::iterator CanonicalTXSet::erase (iterator const& it) +{ + iterator tmp = it; + ++tmp; + mMap.erase (it); + return tmp; +} + + diff --git a/src/cpp/ripple/ripple_CanonicalTXSet.h b/src/cpp/ripple/ripple_CanonicalTXSet.h new file mode 100644 index 00000000000..9e89ec7ea33 --- /dev/null +++ b/src/cpp/ripple/ripple_CanonicalTXSet.h @@ -0,0 +1,65 @@ +#ifndef RIPPLE_CANONICALTXSET_H +#define RIPPLE_CANONICALTXSET_H + +class CanonicalTXSet +{ +public: + class Key + { + public: + Key (uint256 const& account, uint32 seq, uint256 const& id) + : mAccount (account) + , mTXid (id) + , mSeq (seq) + { + } + + bool operator< (Key const& rhs) const; + bool operator> (Key const& rhs) const; + bool operator<= (Key const& rhs) const; + bool operator>= (Key const& rhs) const; + + bool operator== (Key const& rhs) const { return mTXid == rhs.mTXid; } + bool operator!= (Key const& rhs) const { return mTXid != rhs.mTXid; } + + uint256 const& getTXID() const { return mTXid; } + + private: + uint256 mAccount, mTXid; + uint32 mSeq; + }; + + typedef std::map ::iterator iterator; + typedef std::map ::const_iterator const_iterator; + +public: + CanonicalTXSet (LedgerHash const& lastClosedLedgerHash) + : mSetHash (lastClosedLedgerHash) + { + } + + void push_back (SerializedTransaction::ref txn); + + void reset (LedgerHash const& newLastClosedLedgerHash) + { + mSetHash = newLastClosedLedgerHash; + + mMap.clear(); + } + + iterator erase (iterator const& it); + + iterator begin() { return mMap.begin(); } + iterator end() { return mMap.end(); } + const_iterator begin() const { return mMap.begin(); } + const_iterator end() const { return mMap.end(); } + size_t size() const { return mMap.size(); } + bool empty() const { return mMap.empty(); } + +private: + uint256 mSetHash; + + std::map mMap; +}; + +#endif diff --git a/src/cpp/ripple/ripple_Features.cpp b/src/cpp/ripple/ripple_Features.cpp index be878cfe704..82d66b9c35e 100644 --- a/src/cpp/ripple/ripple_Features.cpp +++ b/src/cpp/ripple/ripple_Features.cpp @@ -21,7 +21,7 @@ class Features : public IFeatures uint32 mFirstReport; // close time of first majority report uint32 mLastReport; // close time of most recent majority report - FeatureState* getCreateFeature(const uint256& feature, bool create); + FeatureState* getCreateFeature(uint256 const& feature, bool create); bool shouldEnable (uint32 closeTime, const FeatureState& fs); void setJson(Json::Value& v, const FeatureState&); @@ -36,14 +36,14 @@ class Features : public IFeatures FeatureState* addKnownFeature(const char *featureID, const char *friendlyName, bool veto); uint256 getFeature(const std::string& name); - bool vetoFeature(const uint256& feature); - bool unVetoFeature(const uint256& feature); + bool vetoFeature(uint256 const& feature); + bool unVetoFeature(uint256 const& feature); - bool enableFeature(const uint256& feature); - bool disableFeature(const uint256& feature); + bool enableFeature(uint256 const& feature); + bool disableFeature(uint256 const& feature); - bool isFeatureEnabled(const uint256& feature); - bool isFeatureSupported(const uint256& feature); + bool isFeatureEnabled(uint256 const& feature); + bool isFeatureSupported(uint256 const& feature); void setEnabledFeatures(const std::vector& features); void setSupportedFeatures(const std::vector& features); @@ -56,7 +56,7 @@ class Features : public IFeatures void reportValidations(const FeatureSet&); Json::Value getJson(int); - Json::Value getJson(const uint256&); + Json::Value getJson(uint256 const& ); void doValidation(Ledger::ref lastClosedLedger, STObject& baseValidation); void doVoting(Ledger::ref lastClosedLedger, SHAMap::ref initialPosition); @@ -70,7 +70,7 @@ void Features::addInitialFeatures() testFeature = addKnownFeature("1234", "testFeature", false); } -FeatureState* Features::getCreateFeature(const uint256& featureHash, bool create) +FeatureState* Features::getCreateFeature(uint256 const& featureHash, bool create) { // call with the mutex held featureMap_t::iterator it = mFeatureMap.find(featureHash); if (it == mFeatureMap.end()) @@ -132,7 +132,7 @@ FeatureState* Features::addKnownFeature(const char *featureID, const char *frien return f; } -bool Features::vetoFeature(const uint256& feature) +bool Features::vetoFeature(uint256 const& feature) { boost::mutex::scoped_lock sl(mMutex); FeatureState *s = getCreateFeature(feature, true); @@ -142,7 +142,7 @@ bool Features::vetoFeature(const uint256& feature) return true; } -bool Features::unVetoFeature(const uint256& feature) +bool Features::unVetoFeature(uint256 const& feature) { boost::mutex::scoped_lock sl(mMutex); FeatureState *s = getCreateFeature(feature, false); @@ -152,7 +152,7 @@ bool Features::unVetoFeature(const uint256& feature) return true; } -bool Features::enableFeature(const uint256& feature) +bool Features::enableFeature(uint256 const& feature) { boost::mutex::scoped_lock sl(mMutex); FeatureState *s = getCreateFeature(feature, true); @@ -162,7 +162,7 @@ bool Features::enableFeature(const uint256& feature) return true; } -bool Features::disableFeature(const uint256& feature) +bool Features::disableFeature(uint256 const& feature) { boost::mutex::scoped_lock sl(mMutex); FeatureState *s = getCreateFeature(feature, false); @@ -172,14 +172,14 @@ bool Features::disableFeature(const uint256& feature) return true; } -bool Features::isFeatureEnabled(const uint256& feature) +bool Features::isFeatureEnabled(uint256 const& feature) { boost::mutex::scoped_lock sl(mMutex); FeatureState *s = getCreateFeature(feature, false); return s && s->mEnabled; } -bool Features::isFeatureSupported(const uint256& feature) +bool Features::isFeatureSupported(uint256 const& feature) { boost::mutex::scoped_lock sl(mMutex); FeatureState *s = getCreateFeature(feature, false); @@ -300,7 +300,7 @@ void Features::reportValidations(const FeatureSet& set) Database* db = theApp->getWalletDB()->getDB(); db->executeSQL("BEGIN TRANSACTION;"); - BOOST_FOREACH(const uint256& hash, changedFeatures) + BOOST_FOREACH(uint256 const& hash, changedFeatures) { FeatureState& fState = mFeatureMap[hash]; db->executeSQL(boost::str(boost::format( @@ -322,7 +322,7 @@ void Features::setEnabledFeatures(const std::vector& features) { it.second.mEnabled = false; } - BOOST_FOREACH(const uint256& it, features) + BOOST_FOREACH(uint256 const& it, features) { mFeatureMap[it].mEnabled = true; } @@ -335,7 +335,7 @@ void Features::setSupportedFeatures(const std::vector& features) { it.second.mSupported = false; } - BOOST_FOREACH(const uint256& it, features) + BOOST_FOREACH(uint256 const& it, features) { mFeatureMap[it].mSupported = true; } @@ -348,7 +348,7 @@ void Features::doValidation(Ledger::ref lastClosedLedger, STObject& baseValidati return; STVector256 vFeatures(sfFeatures); - BOOST_FOREACH(const uint256& uFeature, lFeatures) + BOOST_FOREACH(uint256 const& uFeature, lFeatures) { vFeatures.addValue(uFeature); } @@ -362,7 +362,7 @@ void Features::doVoting(Ledger::ref lastClosedLedger, SHAMap::ref initialPositio if (lFeatures.empty()) return; - BOOST_FOREACH(const uint256& uFeature, lFeatures) + BOOST_FOREACH(uint256 const& uFeature, lFeatures) { WriteLog (lsWARNING, Features) << "Voting for feature: " << uFeature; SerializedTransaction trans(ttFEATURE); @@ -438,7 +438,7 @@ void Features::setJson(Json::Value& v, const FeatureState& fs) v["veto"] = true; } -Json::Value Features::getJson(const uint256& feature) +Json::Value Features::getJson(uint256 const& feature) { Json::Value ret = Json::objectValue; boost::mutex::scoped_lock sl(mMutex); diff --git a/src/cpp/ripple/ripple_HashRouter.cpp b/src/cpp/ripple/ripple_HashRouter.cpp index a90b0e7c791..ee59815b078 100644 --- a/src/cpp/ripple/ripple_HashRouter.cpp +++ b/src/cpp/ripple/ripple_HashRouter.cpp @@ -10,17 +10,17 @@ class HashRouter : public IHashRouter { } - bool addSuppression(const uint256& index); + bool addSuppression(uint256 const& index); - bool addSuppressionPeer(const uint256& index, uint64 peer); - bool addSuppressionPeer(const uint256& index, uint64 peer, int& flags); - bool addSuppressionFlags(const uint256& index, int flag); - bool setFlag(const uint256& index, int flag); - int getFlags(const uint256& index); + bool addSuppressionPeer(uint256 const& index, uint64 peer); + bool addSuppressionPeer(uint256 const& index, uint64 peer, int& flags); + bool addSuppressionFlags(uint256 const& index, int flag); + bool setFlag(uint256 const& index, int flag); + int getFlags(uint256 const& index); - HashRouterEntry getEntry(const uint256&); + HashRouterEntry getEntry(uint256 const& ); - bool swapSet(const uint256& index, std::set& peers, int flag); + bool swapSet(uint256 const& index, std::set& peers, int flag); private: boost::mutex mSuppressionMutex; @@ -33,10 +33,10 @@ class HashRouter : public IHashRouter int mHoldTime; - HashRouterEntry& findCreateEntry(const uint256&, bool& created); + HashRouterEntry& findCreateEntry(uint256 const& , bool& created); }; -HashRouterEntry& HashRouter::findCreateEntry(const uint256& index, bool& created) +HashRouterEntry& HashRouter::findCreateEntry(uint256 const& index, bool& created) { boost::unordered_map::iterator fit = mSuppressionMap.find(index); @@ -54,7 +54,7 @@ HashRouterEntry& HashRouter::findCreateEntry(const uint256& index, bool& created std::map< int, std::list >::iterator it = mSuppressionTimes.begin(); if ((it != mSuppressionTimes.end()) && (it->first <= expireTime)) { - BOOST_FOREACH(const uint256& lit, it->second) + BOOST_FOREACH(uint256 const& lit, it->second) mSuppressionMap.erase(lit); mSuppressionTimes.erase(it); } @@ -63,7 +63,7 @@ HashRouterEntry& HashRouter::findCreateEntry(const uint256& index, bool& created return mSuppressionMap.emplace(index, HashRouterEntry ()).first->second; } -bool HashRouter::addSuppression(const uint256& index) +bool HashRouter::addSuppression(uint256 const& index) { boost::mutex::scoped_lock sl(mSuppressionMutex); @@ -72,7 +72,7 @@ bool HashRouter::addSuppression(const uint256& index) return created; } -HashRouterEntry HashRouter::getEntry(const uint256& index) +HashRouterEntry HashRouter::getEntry(uint256 const& index) { boost::mutex::scoped_lock sl(mSuppressionMutex); @@ -80,7 +80,7 @@ HashRouterEntry HashRouter::getEntry(const uint256& index) return findCreateEntry(index, created); } -bool HashRouter::addSuppressionPeer(const uint256& index, uint64 peer) +bool HashRouter::addSuppressionPeer(uint256 const& index, uint64 peer) { boost::mutex::scoped_lock sl(mSuppressionMutex); @@ -89,7 +89,7 @@ bool HashRouter::addSuppressionPeer(const uint256& index, uint64 peer) return created; } -bool HashRouter::addSuppressionPeer(const uint256& index, uint64 peer, int& flags) +bool HashRouter::addSuppressionPeer(uint256 const& index, uint64 peer, int& flags) { boost::mutex::scoped_lock sl(mSuppressionMutex); @@ -100,7 +100,7 @@ bool HashRouter::addSuppressionPeer(const uint256& index, uint64 peer, int& flag return created; } -int HashRouter::getFlags(const uint256& index) +int HashRouter::getFlags(uint256 const& index) { boost::mutex::scoped_lock sl(mSuppressionMutex); @@ -108,7 +108,7 @@ int HashRouter::getFlags(const uint256& index) return findCreateEntry(index, created).getFlags(); } -bool HashRouter::addSuppressionFlags(const uint256& index, int flag) +bool HashRouter::addSuppressionFlags(uint256 const& index, int flag) { boost::mutex::scoped_lock sl(mSuppressionMutex); @@ -117,7 +117,7 @@ bool HashRouter::addSuppressionFlags(const uint256& index, int flag) return created; } -bool HashRouter::setFlag(const uint256& index, int flag) +bool HashRouter::setFlag(uint256 const& index, int flag) { // return: true = changed, false = unchanged assert(flag != 0); @@ -133,7 +133,7 @@ bool HashRouter::setFlag(const uint256& index, int flag) return true; } -bool HashRouter::swapSet(const uint256& index, std::set& peers, int flag) +bool HashRouter::swapSet(uint256 const& index, std::set& peers, int flag) { boost::mutex::scoped_lock sl(mSuppressionMutex); diff --git a/src/cpp/ripple/ripple_HashedObject.cpp b/src/cpp/ripple/ripple_HashedObject.cpp index b33f1f8ae83..ca29f0358cb 100644 --- a/src/cpp/ripple/ripple_HashedObject.cpp +++ b/src/cpp/ripple/ripple_HashedObject.cpp @@ -1,14 +1,50 @@ -/* -#include "HashedObject.h" -#include "leveldb/db.h" -#include "leveldb/write_batch.h" -#include "../database/SqliteDatabase.h" -#include "Application.h" -*/ - SETUP_LOG (HashedObject) -DECLARE_INSTANCE(HashedObject); +DECLARE_INSTANCE (HashedObject); + +HashedObject::HashedObject ( + HashedObjectType type, + LedgerIndex ledgerIndex, + Blob const& binaryDataToCopy, + uint256 const& hash) + : mType (type) + , mHash (hash) + , mLedgerIndex (ledgerIndex) + , mData (binaryDataToCopy) +{ +} + +HashedObject::HashedObject ( + HashedObjectType type, + LedgerIndex ledgerIndex, + void const* bufferToCopy, + int bytesInBuffer, + uint256 const& hash) + : mType (type) + , mHash (hash) + , mLedgerIndex (ledgerIndex) + , mData (static_cast (bufferToCopy), + static_cast (bufferToCopy) + bytesInBuffer) +{ +} + +HashedObjectType HashedObject::getType () const +{ + return mType; +} + +uint256 const& HashedObject::getHash() const +{ + return mHash; +} + +LedgerIndex HashedObject::getIndex () const +{ + return mLedgerIndex; +} -// vim:ts=4 +Blob const& HashedObject::getData() const +{ + return mData; +} diff --git a/src/cpp/ripple/ripple_HashedObject.h b/src/cpp/ripple/ripple_HashedObject.h index f304bf9ad8d..4d10f5b3d73 100644 --- a/src/cpp/ripple/ripple_HashedObject.h +++ b/src/cpp/ripple/ripple_HashedObject.h @@ -38,66 +38,41 @@ class HashedObject : private IS_INSTANCE (HashedObject) @note A copy of the data is created. */ HashedObject (HashedObjectType type, - uint32 ledgerIndex, + LedgerIndex ledgerIndex, Blob const& binaryDataToCopy, - uint256 const& hash) - : mType (type) - , mHash (hash) - , mLedgerIndex (ledgerIndex) - , mData (binaryDataToCopy) - { - } + uint256 const& hash); /** Create from an area of memory. @note A copy of the data is created. */ HashedObject (HashedObjectType type, - uint32 ledgerIndex, + LedgerIndex ledgerIndex, void const* bufferToCopy, int bytesInBuffer, - uint256 const& hash) - : mType (type) - , mHash (hash) - , mLedgerIndex (ledgerIndex) - , mData (static_cast (bufferToCopy), - static_cast (bufferToCopy) + bytesInBuffer) - { - } + uint256 const& hash); /** Retrieve the type of this object. */ - HashedObjectType getType () const - { - return mType; - } + HashedObjectType getType () const; /** Retrieve the hash metadata. */ - uint256 const& getHash() const - { - return mHash; - } + uint256 const& getHash() const; /** Retrieve the ledger index in which this object appears. */ // VFALCO TODO rename to getLedgerIndex or getLedgerId - uint32 getIndex () const - { - return mLedgerIndex; - } + LedgerIndex getIndex () const; /** Retrieve the binary data. */ - Blob const& getData() const - { - return mData; - } + Blob const& getData() const; private: HashedObjectType const mType; uint256 const mHash; - uint32 const mLedgerIndex; + LedgerIndex const mLedgerIndex; Blob const mData; }; diff --git a/src/cpp/ripple/ripple_HashedObjectStore.cpp b/src/cpp/ripple/ripple_HashedObjectStore.cpp index 448c4f7a419..fbdc18614dd 100644 --- a/src/cpp/ripple/ripple_HashedObjectStore.cpp +++ b/src/cpp/ripple/ripple_HashedObjectStore.cpp @@ -1,12 +1,4 @@ -/* -#include "HashedObject.h" -#include "leveldb/db.h" -#include "leveldb/write_batch.h" -#include "../database/SqliteDatabase.h" -#include "Application.h" -*/ - HashedObjectStore::HashedObjectStore(int cacheSize, int cacheAge) : mCache("HashedObjectStore", cacheSize, cacheAge), mNegativeCache("HashedObjectNegativeCache", 0, 120), mWriteGeneration(0), mWriteLoad(0), mWritePending(false), mLevelDB(false), mEphemeralDB(false) @@ -47,7 +39,7 @@ int HashedObjectStore::getWriteLoad() } // low-level retrieve -HashedObject::pointer HashedObjectStore::LLRetrieve(const uint256& hash, leveldb::DB* db) +HashedObject::pointer HashedObjectStore::LLRetrieve(uint256 const& hash, leveldb::DB* db) { std::string sData; @@ -118,7 +110,7 @@ void HashedObjectStore::LLWrite(const std::vector< boost::shared_ptrgetHashNodeLDB()) return true; @@ -178,7 +170,7 @@ void HashedObjectStore::bulkWriteLevelDB(Job &) } } -HashedObject::pointer HashedObjectStore::retrieveLevelDB(const uint256& hash) +HashedObject::pointer HashedObjectStore::retrieveLevelDB(uint256 const& hash) { HashedObject::pointer obj = mCache.fetch(hash); if (obj || mNegativeCache.isPresent(hash) || !theApp || !theApp->getHashNodeLDB()) @@ -214,7 +206,7 @@ HashedObject::pointer HashedObjectStore::retrieveLevelDB(const uint256& hash) } bool HashedObjectStore::storeSQLite(HashedObjectType type, uint32 index, - Blob const& data, const uint256& hash) + Blob const& data, uint256 const& hash) { // return: false = already in cache, true = added to cache if (!theApp->getHashNodeDB()) { @@ -351,7 +343,7 @@ void HashedObjectStore::bulkWriteSQLite(Job&) } } -HashedObject::pointer HashedObjectStore::retrieveSQLite(const uint256& hash) +HashedObject::pointer HashedObjectStore::retrieveSQLite(uint256 const& hash) { HashedObject::pointer obj = mCache.fetch(hash); if (obj) diff --git a/src/cpp/ripple/ripple_HashedObjectStore.h b/src/cpp/ripple/ripple_HashedObjectStore.h index b6f2840edd7..06244f22201 100644 --- a/src/cpp/ripple/ripple_HashedObjectStore.h +++ b/src/cpp/ripple/ripple_HashedObjectStore.h @@ -19,7 +19,7 @@ class HashedObjectStore } bool store (HashedObjectType type, uint32 index, Blob const& data, - const uint256& hash) + uint256 const& hash) { if (mLevelDB) return storeLevelDB(type, index, data, hash); @@ -27,7 +27,7 @@ class HashedObjectStore return storeSQLite(type, index, data, hash); } - HashedObject::pointer retrieve(const uint256& hash) + HashedObject::pointer retrieve(uint256 const& hash) { if (mLevelDB) return retrieveLevelDB(hash); @@ -35,13 +35,13 @@ class HashedObjectStore } bool storeSQLite(HashedObjectType type, uint32 index, Blob const& data, - const uint256& hash); - HashedObject::pointer retrieveSQLite(const uint256& hash); + uint256 const& hash); + HashedObject::pointer retrieveSQLite(uint256 const& hash); void bulkWriteSQLite(Job&); bool storeLevelDB(HashedObjectType type, uint32 index, Blob const& data, - const uint256& hash); - HashedObject::pointer retrieveLevelDB(const uint256& hash); + uint256 const& hash); + HashedObject::pointer retrieveLevelDB(uint256 const& hash); void bulkWriteLevelDB(Job&); @@ -53,7 +53,7 @@ class HashedObjectStore int import(const std::string& fileName); private: - static HashedObject::pointer LLRetrieve(const uint256& hash, leveldb::DB* db); + static HashedObject::pointer LLRetrieve(uint256 const& hash, leveldb::DB* db); static void LLWrite(boost::shared_ptr ptr, leveldb::DB* db); static void LLWrite(const std::vector< boost::shared_ptr >& set, leveldb::DB* db); diff --git a/src/cpp/ripple/ripple_IFeatures.h b/src/cpp/ripple/ripple_IFeatures.h index bd4b23eb2d3..2a0e8505234 100644 --- a/src/cpp/ripple/ripple_IFeatures.h +++ b/src/cpp/ripple/ripple_IFeatures.h @@ -9,7 +9,7 @@ class FeatureSet boost::unordered_map mVotes; // yes votes by feature FeatureSet(uint32 ct, int tv) : mCloseTime(ct), mTrustedValidations(tv) { ; } - void addVote(const uint256& feature) { ++mVotes[feature]; } + void addVote(uint256 const& feature) { ++mVotes[feature]; } }; class FeatureState @@ -57,14 +57,14 @@ class IFeatures virtual FeatureState* addKnownFeature(const char *featureID, const char *friendlyName, bool veto) = 0; virtual uint256 getFeature(const std::string& name) = 0; - virtual bool vetoFeature(const uint256& feature) = 0; - virtual bool unVetoFeature(const uint256& feature) = 0; + virtual bool vetoFeature(uint256 const& feature) = 0; + virtual bool unVetoFeature(uint256 const& feature) = 0; - virtual bool enableFeature(const uint256& feature) = 0; - virtual bool disableFeature(const uint256& feature) = 0; + virtual bool enableFeature(uint256 const& feature) = 0; + virtual bool disableFeature(uint256 const& feature) = 0; - virtual bool isFeatureEnabled(const uint256& feature) = 0; - virtual bool isFeatureSupported(const uint256& feature) = 0; + virtual bool isFeatureEnabled(uint256 const& feature) = 0; + virtual bool isFeatureSupported(uint256 const& feature) = 0; virtual void setEnabledFeatures(const std::vector& features) = 0; virtual void setSupportedFeatures(const std::vector& features) = 0; @@ -80,7 +80,7 @@ class IFeatures virtual void reportValidations(const FeatureSet&) = 0; virtual Json::Value getJson(int) = 0; - virtual Json::Value getJson(const uint256&) = 0; + virtual Json::Value getJson(uint256 const& ) = 0; virtual void doValidation(Ledger::ref lastClosedLedger, STObject& baseValidation) = 0; virtual void doVoting(Ledger::ref lastClosedLedger, SHAMap::ref initialPosition) = 0; diff --git a/src/cpp/ripple/ripple_IHashRouter.h b/src/cpp/ripple/ripple_IHashRouter.h index acabc289364..fc3f3923d2a 100644 --- a/src/cpp/ripple/ripple_IHashRouter.h +++ b/src/cpp/ripple/ripple_IHashRouter.h @@ -48,17 +48,17 @@ class IHashRouter virtual ~IHashRouter () { } - virtual bool addSuppression(const uint256& index) = 0; + virtual bool addSuppression(uint256 const& index) = 0; - virtual bool addSuppressionPeer(const uint256& index, uint64 peer) = 0; - virtual bool addSuppressionPeer(const uint256& index, uint64 peer, int& flags) = 0; - virtual bool addSuppressionFlags(const uint256& index, int flag) = 0; - virtual bool setFlag(const uint256& index, int flag) = 0; - virtual int getFlags(const uint256& index) = 0; + virtual bool addSuppressionPeer(uint256 const& index, uint64 peer) = 0; + virtual bool addSuppressionPeer(uint256 const& index, uint64 peer, int& flags) = 0; + virtual bool addSuppressionFlags(uint256 const& index, int flag) = 0; + virtual bool setFlag(uint256 const& index, int flag) = 0; + virtual int getFlags(uint256 const& index) = 0; - virtual HashRouterEntry getEntry(const uint256&) = 0; + virtual HashRouterEntry getEntry(uint256 const& ) = 0; - virtual bool swapSet(const uint256& index, std::set& peers, int flag) = 0; + virtual bool swapSet(uint256 const& index, std::set& peers, int flag) = 0; }; diff --git a/src/cpp/ripple/ripple_IProofOfWorkFactory.h b/src/cpp/ripple/ripple_IProofOfWorkFactory.h index 9d5c656c8f8..bcb2eb87989 100644 --- a/src/cpp/ripple/ripple_IProofOfWorkFactory.h +++ b/src/cpp/ripple/ripple_IProofOfWorkFactory.h @@ -29,7 +29,7 @@ class IProofOfWorkFactory virtual ProofOfWork getProof () = 0; - virtual POWResult checkProof (const std::string& token, const uint256& solution) = 0; + virtual POWResult checkProof (const std::string& token, uint256 const& solution) = 0; virtual uint64 getDifficulty() = 0; @@ -41,12 +41,12 @@ class IProofOfWorkFactory virtual void sweep () = 0; - virtual const uint256& getSecret () const = 0; + virtual uint256 const& getSecret () const = 0; - virtual void setSecret (const uint256& secret) = 0; + virtual void setSecret (uint256 const& secret) = 0; public: - static int getPowEntry (const uint256& target, int iterations); + static int getPowEntry (uint256 const& target, int iterations); }; #endif diff --git a/src/cpp/ripple/ripple_IValidations.h b/src/cpp/ripple/ripple_IValidations.h index b6caa7e821e..ef81cf5bf17 100644 --- a/src/cpp/ripple/ripple_IValidations.h +++ b/src/cpp/ripple/ripple_IValidations.h @@ -14,14 +14,14 @@ class IValidations virtual bool addValidation (SerializedValidation::ref, const std::string& source) = 0; - virtual ValidationSet getValidations (const uint256& ledger) = 0; + virtual ValidationSet getValidations (uint256 const& ledger) = 0; - virtual void getValidationCount (const uint256& ledger, bool currentOnly, int& trusted, int& untrusted) = 0; - virtual void getValidationTypes (const uint256& ledger, int& full, int& partial) = 0; + virtual void getValidationCount (uint256 const& ledger, bool currentOnly, int& trusted, int& untrusted) = 0; + virtual void getValidationTypes (uint256 const& ledger, int& full, int& partial) = 0; - virtual int getTrustedValidationCount (const uint256& ledger) = 0; + virtual int getTrustedValidationCount (uint256 const& ledger) = 0; - virtual int getNodesAfter (const uint256& ledger) = 0; + virtual int getNodesAfter (uint256 const& ledger) = 0; virtual int getLoadRatio (bool overLoaded) = 0; // VFALCO TODO make a typedef for this ugly return value! diff --git a/src/cpp/ripple/ripple_InfoSub.cpp b/src/cpp/ripple/ripple_InfoSub.cpp new file mode 100644 index 00000000000..b108c4f6a14 --- /dev/null +++ b/src/cpp/ripple/ripple_InfoSub.cpp @@ -0,0 +1,70 @@ + +// This is the primary interface into the "client" portion of the program. +// Code that wants to do normal operations on the network such as +// creating and monitoring accounts, creating transactions, and so on +// should use this interface. The RPC code will primarily be a light wrapper +// over this code. + +// Eventually, it will check the node's operating mode (synched, unsynched, +// etectera) and defer to the correct means of processing. The current +// code assumes this node is synched (and will continue to do so until +// there's a functional network. + +DECLARE_INSTANCE(InfoSub); + +// VFALCO TODO Figure out how to clean up these globals +uint64 InfoSub::sSeq = 0; +boost::mutex InfoSub::sSeqLock; + +InfoSub::InfoSub () +{ + boost::mutex::scoped_lock sl(sSeqLock); + mSeq = ++sSeq; +} + +InfoSub::~InfoSub() +{ + NetworkOPs& ops = theApp->getOPs(); + ops.unsubTransactions(mSeq); + ops.unsubRTTransactions(mSeq); + ops.unsubLedger(mSeq); + ops.unsubServer(mSeq); + ops.unsubAccount(mSeq, mSubAccountInfo, true); + ops.unsubAccount(mSeq, mSubAccountInfo, false); +} + +void InfoSub::send(const Json::Value& jvObj, const std::string& sObj, bool broadcast) +{ + send (jvObj, broadcast); +} + +uint64 InfoSub::getSeq() +{ + return mSeq; +} + +void InfoSub::onSendEmpty () +{ +} + +void InfoSub::insertSubAccountInfo (RippleAddress addr, uint32 uLedgerIndex) +{ + boost::mutex::scoped_lock sl(mLockInfo); + + mSubAccountInfo.insert(addr); +} + +void InfoSub::clearPFRequest() +{ + mPFRequest.reset(); +} + +void InfoSub::setPFRequest(const boost::shared_ptr& req) +{ + mPFRequest = req; +} + +const boost::shared_ptr& InfoSub::getPFRequest() +{ + return mPFRequest; +} diff --git a/src/cpp/ripple/ripple_InfoSub.h b/src/cpp/ripple/ripple_InfoSub.h new file mode 100644 index 00000000000..c2914fbd1d7 --- /dev/null +++ b/src/cpp/ripple/ripple_InfoSub.h @@ -0,0 +1,59 @@ +#ifndef RIPPLE_INFOSUB_H +#define RIPPLE_INFOSUB_H + +// Operations that clients may wish to perform against the network +// Master operational handler, server sequencer, network tracker + +class PFRequest; + +DEFINE_INSTANCE(InfoSub); + +// VFALCO TODO Move InfoSub to a separate file +class InfoSub : public IS_INSTANCE(InfoSub) +{ +public: + typedef boost::shared_ptr pointer; + + // VFALCO TODO Standardize on the names of weak / strong pointer typedefs. + typedef boost::weak_ptr wptr; + + typedef const boost::shared_ptr& ref; + +public: + InfoSub (); + + virtual ~InfoSub (); + + virtual void send (const Json::Value& jvObj, bool broadcast) = 0; + + // VFALCO NOTE Why is this virtual? + virtual void send (const Json::Value& jvObj, const std::string& sObj, bool broadcast); + + uint64 getSeq (); + + void onSendEmpty (); + + void insertSubAccountInfo (RippleAddress addr, uint32 uLedgerIndex); + + void clearPFRequest(); + + void setPFRequest (const boost::shared_ptr& req); + + boost::shared_ptr const& getPFRequest (); + +protected: + // VFALCO TODO make accessor for this member + boost::mutex mLockInfo; + +private: + static uint64 sSeq; + static boost::mutex sSeqLock; + + boost::unordered_set mSubAccountInfo; + boost::unordered_set mSubAccountTransaction; + boost::shared_ptr mPFRequest; + + uint64 mSeq; +}; + +#endif diff --git a/src/cpp/ripple/LedgerAcquire.cpp b/src/cpp/ripple/ripple_LedgerAcquire.cpp similarity index 70% rename from src/cpp/ripple/LedgerAcquire.cpp rename to src/cpp/ripple/ripple_LedgerAcquire.cpp index a3f6f659c12..5b74bf564ba 100644 --- a/src/cpp/ripple/LedgerAcquire.cpp +++ b/src/cpp/ripple/ripple_LedgerAcquire.cpp @@ -3,93 +3,24 @@ SETUP_LOG (LedgerAcquire) DECLARE_INSTANCE(LedgerAcquire); +// VFALCO TODO replace macros #define LA_DEBUG #define LEDGER_ACQUIRE_TIMEOUT 2000 // millisecond for each ledger timeout #define LEDGER_TIMEOUT_COUNT 10 // how many timeouts before we giveup #define LEDGER_TIMEOUT_AGGRESSIVE 6 // how many timeouts before we get aggressive #define TRUST_NETWORK -PeerSet::PeerSet(const uint256& hash, int interval) : mHash(hash), mTimerInterval(interval), mTimeouts(0), - mComplete(false), mFailed(false), mProgress(true), mAggressive(false), mTimer(theApp->getIOService()) -{ - mLastAction = UptimeTimer::getInstance().getElapsedSeconds(); - assert((mTimerInterval > 10) && (mTimerInterval < 30000)); -} - -void PeerSet::peerHas(Peer::ref ptr) -{ - boost::recursive_mutex::scoped_lock sl(mLock); - if (!mPeers.insert(std::make_pair(ptr->getPeerId(), 0)).second) - return; - newPeer(ptr); -} - -void PeerSet::badPeer(Peer::ref ptr) -{ - boost::recursive_mutex::scoped_lock sl(mLock); - mPeers.erase(ptr->getPeerId()); -} - -void PeerSet::setTimer() -{ - mTimer.expires_from_now(boost::posix_time::milliseconds(mTimerInterval)); - mTimer.async_wait(boost::bind(&PeerSet::TimerEntry, pmDowncast(), boost::asio::placeholders::error)); -} - -void PeerSet::invokeOnTimer() -{ - boost::recursive_mutex::scoped_lock sl(mLock); - if (isDone()) - return; - - if (!mProgress) - { - ++mTimeouts; - WriteLog (lsWARNING, LedgerAcquire) << "Timeout(" << mTimeouts << ") pc=" << mPeers.size() << " acquiring " << mHash; - onTimer(false); - } - else - { - mProgress = false; - onTimer(true); - } - - if (!isDone()) - setTimer(); -} - -void PeerSet::TimerEntry(boost::weak_ptr wptr, const boost::system::error_code& result) -{ - if (result == boost::asio::error::operation_aborted) - return; - boost::shared_ptr ptr = wptr.lock(); - if (ptr) - { - int jc = theApp->getJobQueue().getJobCountTotal(jtLEDGER_DATA); - if (jc > 4) - { - WriteLog (lsDEBUG, LedgerAcquire) << "Deferring PeerSet timer due to load"; - ptr->setTimer(); - } - else - theApp->getJobQueue().addJob(jtLEDGER_DATA, "timerEntry", BIND_TYPE(&PeerSet::TimerJobEntry, P_1, ptr)); - } -} - -void PeerSet::TimerJobEntry(Job&, boost::shared_ptr ptr) -{ - ptr->invokeOnTimer(); -} - -bool PeerSet::isActive() -{ - boost::recursive_mutex::scoped_lock sl(mLock); - return !isDone(); -} - -LedgerAcquire::LedgerAcquire(const uint256& hash, uint32 seq) : PeerSet(hash, LEDGER_ACQUIRE_TIMEOUT), - mHaveBase(false), mHaveState(false), mHaveTransactions(false), mAborted(false), mSignaled(false), mAccept(false), - mByHash(true), mWaitCount(0), mSeq(seq) +LedgerAcquire::LedgerAcquire (uint256 const& hash, uint32 seq) + : PeerSet (hash, LEDGER_ACQUIRE_TIMEOUT) + , mHaveBase (false) + , mHaveState (false) + , mHaveTransactions (false) + , mAborted (false) + , mSignaled (false) + , mAccept (false) + , mByHash (true) + , mWaitCount (0) + , mSeq(seq) { #ifdef LA_DEBUG WriteLog (lsTRACE, LedgerAcquire) << "Acquiring ledger " << mHash; @@ -672,7 +603,7 @@ bool LedgerAcquire::takeBase(const std::string& data) // data must not have hash } bool LedgerAcquire::takeTxNode(const std::list& nodeIDs, - const std::list< Blob >& data, SMAddNode& san) + const std::list< Blob >& data, SHAMapAddNode& san) { boost::recursive_mutex::scoped_lock sl(mLock); if (!mHaveBase) @@ -713,7 +644,7 @@ bool LedgerAcquire::takeTxNode(const std::list& nodeIDs, } bool LedgerAcquire::takeAsNode(const std::list& nodeIDs, - const std::list< Blob >& data, SMAddNode& san) + const std::list< Blob >& data, SHAMapAddNode& san) { WriteLog (lsTRACE, LedgerAcquire) << "got ASdata (" << nodeIDs.size() <<") acquiring ledger " << mHash; CondLog (nodeIDs.size() == 1, lsTRACE, LedgerAcquire) << "got AS node: " << nodeIDs.front(); @@ -762,7 +693,7 @@ bool LedgerAcquire::takeAsNode(const std::list& nodeIDs, return true; } -bool LedgerAcquire::takeAsRootNode(Blob const& data, SMAddNode& san) +bool LedgerAcquire::takeAsRootNode(Blob const& data, SHAMapAddNode& san) { boost::recursive_mutex::scoped_lock sl(mLock); if (mFailed || mHaveState) @@ -774,7 +705,7 @@ bool LedgerAcquire::takeAsRootNode(Blob const& data, SMAddNode& san) mLedger->peekAccountStateMap()->addRootNode(mLedger->getAccountHash(), data, snfWIRE, &tFilter)); } -bool LedgerAcquire::takeTxRootNode(Blob const& data, SMAddNode& san) +bool LedgerAcquire::takeTxRootNode(Blob const& data, SHAMapAddNode& san) { boost::recursive_mutex::scoped_lock sl(mLock); if (mFailed || mHaveState) @@ -786,46 +717,6 @@ bool LedgerAcquire::takeTxRootNode(Blob const& data, SMAddNode& san) mLedger->peekTransactionMap()->addRootNode(mLedger->getTransHash(), data, snfWIRE, &tFilter)); } -LedgerAcquire::pointer LedgerAcquireMaster::findCreate(const uint256& hash, uint32 seq) -{ - assert(hash.isNonZero()); - boost::mutex::scoped_lock sl(mLock); - LedgerAcquire::pointer& ptr = mLedgers[hash]; - if (ptr) - { - ptr->touch(); - return ptr; - } - ptr = boost::make_shared(hash, seq); - if (!ptr->isDone()) - { - ptr->addPeers(); - ptr->setTimer(); // Cannot call in constructor - } - else - { - Ledger::pointer ledger = ptr->getLedger(); - ledger->setClosed(); - ledger->setImmutable(); - theApp->getLedgerMaster().storeLedger(ledger); - WriteLog (lsDEBUG, LedgerAcquire) << "Acquiring ledger we already have: " << hash; - } - return ptr; -} - -LedgerAcquire::pointer LedgerAcquireMaster::find(const uint256& hash) -{ - assert(hash.isNonZero()); - boost::mutex::scoped_lock sl(mLock); - std::map::iterator it = mLedgers.find(hash); - if (it != mLedgers.end()) - { - it->second->touch(); - return it->second; - } - return LedgerAcquire::pointer(); -} - std::vector LedgerAcquire::getNeededHashes() { std::vector ret; @@ -838,14 +729,14 @@ std::vector LedgerAcquire::getNeededHashes() { AccountStateSF filter(mLedger->getLedgerSeq()); std::vector v = mLedger->getNeededAccountStateHashes(4, &filter); - BOOST_FOREACH(const uint256& h, v) + BOOST_FOREACH(uint256 const& h, v) ret.push_back(std::make_pair(ripple::TMGetObjectByHash::otSTATE_NODE, h)); } if (!mHaveTransactions) { TransactionStateSF filter(mLedger->getLedgerSeq()); std::vector v = mLedger->getNeededAccountStateHashes(4, &filter); - BOOST_FOREACH(const uint256& h, v) + BOOST_FOREACH(uint256 const& h, v) ret.push_back(std::make_pair(ripple::TMGetObjectByHash::otTRANSACTION_NODE, h)); } return ret; @@ -869,7 +760,7 @@ Json::Value LedgerAcquire::getJson(int) { Json::Value hv(Json::arrayValue); std::vector v = mLedger->peekAccountStateMap()->getNeededHashes(16, NULL); - BOOST_FOREACH(const uint256& h, v) + BOOST_FOREACH(uint256 const& h, v) hv.append(h.GetHex()); ret["needed_state_hashes"] = hv; } @@ -877,190 +768,9 @@ Json::Value LedgerAcquire::getJson(int) { Json::Value hv(Json::arrayValue); std::vector v = mLedger->peekTransactionMap()->getNeededHashes(16, NULL); - BOOST_FOREACH(const uint256& h, v) + BOOST_FOREACH(uint256 const& h, v) hv.append(h.GetHex()); ret["needed_transaction_hashes"] = hv; } return ret; } - -bool LedgerAcquireMaster::hasLedger(const uint256& hash) -{ - assert(hash.isNonZero()); - boost::mutex::scoped_lock sl(mLock); - return mLedgers.find(hash) != mLedgers.end(); -} - -void LedgerAcquireMaster::dropLedger(const uint256& hash) -{ - assert(hash.isNonZero()); - boost::mutex::scoped_lock sl(mLock); - mLedgers.erase(hash); -} - -bool LedgerAcquireMaster::awaitLedgerData(const uint256& ledgerHash) -{ - LedgerAcquire::pointer ledger = find(ledgerHash); - if (!ledger) - return false; - ledger->awaitData(); - return true; -} - -void LedgerAcquireMaster::gotLedgerData(Job&, uint256 hash, - boost::shared_ptr packet_ptr, boost::weak_ptr wPeer) -{ - ripple::TMLedgerData& packet = *packet_ptr; - Peer::pointer peer = wPeer.lock(); - - WriteLog (lsTRACE, LedgerAcquire) << "Got data (" << packet.nodes().size() << ") for acquiring ledger: " << hash; - - LedgerAcquire::pointer ledger = find(hash); - if (!ledger) - { - WriteLog (lsTRACE, LedgerAcquire) << "Got data for ledger we're not acquiring"; - if (peer) - peer->punishPeer(LT_InvalidRequest); - return; - } - ledger->noAwaitData(); - - if (!peer) - return; - - if (packet.type() == ripple::liBASE) - { - if (packet.nodes_size() < 1) - { - WriteLog (lsWARNING, LedgerAcquire) << "Got empty base data"; - peer->punishPeer(LT_InvalidRequest); - return; - } - if (!ledger->takeBase(packet.nodes(0).nodedata())) - { - WriteLog (lsWARNING, LedgerAcquire) << "Got invalid base data"; - peer->punishPeer(LT_InvalidRequest); - return; - } - SMAddNode san = SMAddNode::useful(); - if ((packet.nodes().size() > 1) && !ledger->takeAsRootNode(strCopy(packet.nodes(1).nodedata()), san)) - { - WriteLog (lsWARNING, LedgerAcquire) << "Included ASbase invalid"; - } - if ((packet.nodes().size() > 2) && !ledger->takeTxRootNode(strCopy(packet.nodes(2).nodedata()), san)) - { - WriteLog (lsWARNING, LedgerAcquire) << "Included TXbase invalid"; - } - if (!san.isInvalid()) - { - ledger->progress(); - ledger->trigger(peer); - } - else - WriteLog (lsDEBUG, LedgerAcquire) << "Peer sends invalid base data"; - return; - } - - if ((packet.type() == ripple::liTX_NODE) || (packet.type() == ripple::liAS_NODE)) - { - std::list nodeIDs; - std::list< Blob > nodeData; - - if (packet.nodes().size() <= 0) - { - WriteLog (lsINFO, LedgerAcquire) << "Got response with no nodes"; - peer->punishPeer(LT_InvalidRequest); - return; - } - for (int i = 0; i < packet.nodes().size(); ++i) - { - const ripple::TMLedgerNode& node = packet.nodes(i); - if (!node.has_nodeid() || !node.has_nodedata()) - { - WriteLog (lsWARNING, LedgerAcquire) << "Got bad node"; - peer->punishPeer(LT_InvalidRequest); - return; - } - - nodeIDs.push_back(SHAMapNode(node.nodeid().data(), node.nodeid().size())); - nodeData.push_back(Blob (node.nodedata().begin(), node.nodedata().end())); - } - SMAddNode ret; - if (packet.type() == ripple::liTX_NODE) - ledger->takeTxNode(nodeIDs, nodeData, ret); - else - ledger->takeAsNode(nodeIDs, nodeData, ret); - if (!ret.isInvalid()) - { - ledger->progress(); - ledger->trigger(peer); - } - else - WriteLog (lsDEBUG, LedgerAcquire) << "Peer sends invalid node data"; - return; - } - - WriteLog (lsWARNING, LedgerAcquire) << "Not sure what ledger data we got"; - peer->punishPeer(LT_InvalidRequest); -} - -void LedgerAcquireMaster::sweep() -{ - mRecentFailures.sweep(); - - int now = UptimeTimer::getInstance().getElapsedSeconds(); - boost::mutex::scoped_lock sl(mLock); - - std::map::iterator it = mLedgers.begin(); - while (it != mLedgers.end()) - { - if (it->second->getLastAction() > now) - { - it->second->touch(); - ++it; - } - else if ((it->second->getLastAction() + 60) < now) - mLedgers.erase(it++); - else - ++it; - } -} - -int LedgerAcquireMaster::getFetchCount(int& timeoutCount) -{ - timeoutCount = 0; - int ret = 0; - { - typedef std::pair u256_acq_pair; - boost::mutex::scoped_lock sl(mLock); - BOOST_FOREACH(const u256_acq_pair& it, mLedgers) - { - if (it.second->isActive()) - { - ++ret; - timeoutCount += it.second->getTimeouts(); - } - } - } - return ret; -} - -void LedgerAcquireMaster::gotFetchPack(Job&) -{ - std::vector acquires; - { - boost::mutex::scoped_lock sl(mLock); - - acquires.reserve(mLedgers.size()); - typedef std::pair u256_acq_pair; - BOOST_FOREACH(const u256_acq_pair& it, mLedgers) - acquires.push_back(it.second); - } - - BOOST_FOREACH(const LedgerAcquire::pointer& acquire, acquires) - { - acquire->checkLocal(); - } -} - -// vim:ts=4 diff --git a/src/cpp/ripple/ripple_LedgerAcquire.h b/src/cpp/ripple/ripple_LedgerAcquire.h new file mode 100644 index 00000000000..2755d1200f5 --- /dev/null +++ b/src/cpp/ripple/ripple_LedgerAcquire.h @@ -0,0 +1,98 @@ +#ifndef RIPPLE_LEDGERACQUIRE_H +#define RIPPLE_LEDGERACQUIRE_H + +DEFINE_INSTANCE(LedgerAcquire); + +// VFALCO TODO Rename to IncomingLedger +// A ledger we are trying to acquire +class LedgerAcquire : private IS_INSTANCE(LedgerAcquire) + , public PeerSet + , public boost::enable_shared_from_this +{ +public: + typedef boost::shared_ptr pointer; + +public: + LedgerAcquire(uint256 const& hash, uint32 seq); + + virtual ~LedgerAcquire() { ; } + + bool isBase() const { return mHaveBase; } + bool isAcctStComplete() const { return mHaveState; } + bool isTransComplete() const { return mHaveTransactions; } + bool isDone() const { return mAborted || isComplete() || isFailed(); } + Ledger::ref getLedger() { return mLedger; } + void abort() { mAborted = true; } + + // VFALCO NOTE what is the meaning of the return value? + bool setAccept () + { + if (mAccept) + return false; + + mAccept = true; + + return true; + } + + // VFALCO TODO Make thise the Listener / Observer pattern + bool addOnComplete (FUNCTION_TYPE); + + bool takeBase(const std::string& data); + bool takeTxNode(const std::list& IDs, const std::list& data, + SHAMapAddNode&); + bool takeTxRootNode(Blob const& data, SHAMapAddNode&); + bool takeAsNode(const std::list& IDs, const std::list& data, + SHAMapAddNode&); + bool takeAsRootNode(Blob const& data, SHAMapAddNode&); + void trigger(Peer::ref); + bool tryLocal(); + void addPeers(); + void awaitData(); + void noAwaitData(); + void checkLocal(); + + typedef std::pair neededHash_t; + + std::vector getNeededHashes(); + + static void filterNodes(std::vector& nodeIDs, std::vector& nodeHashes, + std::set& recentNodes, int max, bool aggressive); + + Json::Value getJson(int); + +private: + void done (); + + void onTimer (bool progress); + + void newPeer (Peer::ref peer) + { + trigger(peer); + } + + boost::weak_ptr pmDowncast(); + +private: + Ledger::pointer mLedger; + bool mHaveBase; + bool mHaveState; + bool mHaveTransactions; + bool mAborted; + bool mSignaled; + bool mAccept; + bool mByHash; + int mWaitCount; + uint32 mSeq; + + std::set mRecentTXNodes; + std::set mRecentASNodes; + + std::vector mRecentPeers; + + std::vector > mOnComplete; +}; + +#endif + +// vim:ts=4 diff --git a/src/cpp/ripple/ripple_LedgerAcquireMaster.cpp b/src/cpp/ripple/ripple_LedgerAcquireMaster.cpp new file mode 100644 index 00000000000..ff572d79482 --- /dev/null +++ b/src/cpp/ripple/ripple_LedgerAcquireMaster.cpp @@ -0,0 +1,221 @@ + +LedgerAcquire::pointer LedgerAcquireMaster::findCreate(uint256 const& hash, uint32 seq) +{ + assert(hash.isNonZero()); + boost::mutex::scoped_lock sl(mLock); + LedgerAcquire::pointer& ptr = mLedgers[hash]; + if (ptr) + { + ptr->touch(); + return ptr; + } + ptr = boost::make_shared(hash, seq); + if (!ptr->isDone()) + { + ptr->addPeers(); + ptr->setTimer(); // Cannot call in constructor + } + else + { + Ledger::pointer ledger = ptr->getLedger(); + ledger->setClosed(); + ledger->setImmutable(); + theApp->getLedgerMaster().storeLedger(ledger); + WriteLog (lsDEBUG, LedgerAcquire) << "Acquiring ledger we already have: " << hash; + } + return ptr; +} + +LedgerAcquire::pointer LedgerAcquireMaster::find(uint256 const& hash) +{ + assert(hash.isNonZero()); + boost::mutex::scoped_lock sl(mLock); + std::map::iterator it = mLedgers.find(hash); + if (it != mLedgers.end()) + { + it->second->touch(); + return it->second; + } + return LedgerAcquire::pointer(); +} + +bool LedgerAcquireMaster::hasLedger(uint256 const& hash) +{ + assert(hash.isNonZero()); + boost::mutex::scoped_lock sl(mLock); + return mLedgers.find(hash) != mLedgers.end(); +} + +void LedgerAcquireMaster::dropLedger(uint256 const& hash) +{ + assert(hash.isNonZero()); + boost::mutex::scoped_lock sl(mLock); + mLedgers.erase(hash); +} + +bool LedgerAcquireMaster::awaitLedgerData(uint256 const& ledgerHash) +{ + LedgerAcquire::pointer ledger = find(ledgerHash); + if (!ledger) + return false; + ledger->awaitData(); + return true; +} + +void LedgerAcquireMaster::gotLedgerData(Job&, uint256 hash, + boost::shared_ptr packet_ptr, boost::weak_ptr wPeer) +{ + ripple::TMLedgerData& packet = *packet_ptr; + Peer::pointer peer = wPeer.lock(); + + WriteLog (lsTRACE, LedgerAcquire) << "Got data (" << packet.nodes().size() << ") for acquiring ledger: " << hash; + + LedgerAcquire::pointer ledger = find(hash); + if (!ledger) + { + WriteLog (lsTRACE, LedgerAcquire) << "Got data for ledger we're not acquiring"; + if (peer) + peer->punishPeer(LT_InvalidRequest); + return; + } + ledger->noAwaitData(); + + if (!peer) + return; + + if (packet.type() == ripple::liBASE) + { + if (packet.nodes_size() < 1) + { + WriteLog (lsWARNING, LedgerAcquire) << "Got empty base data"; + peer->punishPeer(LT_InvalidRequest); + return; + } + if (!ledger->takeBase(packet.nodes(0).nodedata())) + { + WriteLog (lsWARNING, LedgerAcquire) << "Got invalid base data"; + peer->punishPeer(LT_InvalidRequest); + return; + } + SHAMapAddNode san = SHAMapAddNode::useful(); + if ((packet.nodes().size() > 1) && !ledger->takeAsRootNode(strCopy(packet.nodes(1).nodedata()), san)) + { + WriteLog (lsWARNING, LedgerAcquire) << "Included ASbase invalid"; + } + if ((packet.nodes().size() > 2) && !ledger->takeTxRootNode(strCopy(packet.nodes(2).nodedata()), san)) + { + WriteLog (lsWARNING, LedgerAcquire) << "Included TXbase invalid"; + } + if (!san.isInvalid()) + { + ledger->progress(); + ledger->trigger(peer); + } + else + WriteLog (lsDEBUG, LedgerAcquire) << "Peer sends invalid base data"; + return; + } + + if ((packet.type() == ripple::liTX_NODE) || (packet.type() == ripple::liAS_NODE)) + { + std::list nodeIDs; + std::list< Blob > nodeData; + + if (packet.nodes().size() <= 0) + { + WriteLog (lsINFO, LedgerAcquire) << "Got response with no nodes"; + peer->punishPeer(LT_InvalidRequest); + return; + } + for (int i = 0; i < packet.nodes().size(); ++i) + { + const ripple::TMLedgerNode& node = packet.nodes(i); + if (!node.has_nodeid() || !node.has_nodedata()) + { + WriteLog (lsWARNING, LedgerAcquire) << "Got bad node"; + peer->punishPeer(LT_InvalidRequest); + return; + } + + nodeIDs.push_back(SHAMapNode(node.nodeid().data(), node.nodeid().size())); + nodeData.push_back(Blob (node.nodedata().begin(), node.nodedata().end())); + } + SHAMapAddNode ret; + if (packet.type() == ripple::liTX_NODE) + ledger->takeTxNode(nodeIDs, nodeData, ret); + else + ledger->takeAsNode(nodeIDs, nodeData, ret); + if (!ret.isInvalid()) + { + ledger->progress(); + ledger->trigger(peer); + } + else + WriteLog (lsDEBUG, LedgerAcquire) << "Peer sends invalid node data"; + return; + } + + WriteLog (lsWARNING, LedgerAcquire) << "Not sure what ledger data we got"; + peer->punishPeer(LT_InvalidRequest); +} + +void LedgerAcquireMaster::sweep() +{ + mRecentFailures.sweep(); + + int now = UptimeTimer::getInstance().getElapsedSeconds(); + boost::mutex::scoped_lock sl(mLock); + + std::map::iterator it = mLedgers.begin(); + while (it != mLedgers.end()) + { + if (it->second->getLastAction() > now) + { + it->second->touch(); + ++it; + } + else if ((it->second->getLastAction() + 60) < now) + mLedgers.erase(it++); + else + ++it; + } +} + +int LedgerAcquireMaster::getFetchCount(int& timeoutCount) +{ + timeoutCount = 0; + int ret = 0; + { + typedef std::pair u256_acq_pair; + boost::mutex::scoped_lock sl(mLock); + BOOST_FOREACH(const u256_acq_pair& it, mLedgers) + { + if (it.second->isActive()) + { + ++ret; + timeoutCount += it.second->getTimeouts(); + } + } + } + return ret; +} + +void LedgerAcquireMaster::gotFetchPack(Job&) +{ + std::vector acquires; + { + boost::mutex::scoped_lock sl(mLock); + + acquires.reserve(mLedgers.size()); + typedef std::pair u256_acq_pair; + BOOST_FOREACH(const u256_acq_pair& it, mLedgers) + acquires.push_back(it.second); + } + + BOOST_FOREACH(const LedgerAcquire::pointer& acquire, acquires) + { + acquire->checkLocal(); + } +} + +// vim:ts=4 diff --git a/src/cpp/ripple/ripple_LedgerAcquireMaster.h b/src/cpp/ripple/ripple_LedgerAcquireMaster.h new file mode 100644 index 00000000000..c20dbfd4d1c --- /dev/null +++ b/src/cpp/ripple/ripple_LedgerAcquireMaster.h @@ -0,0 +1,42 @@ +#ifndef RIPPLE_LEDGERACQUIREMASTER_H +#define RIPPLE_LEDGERACQUIREMASTER_H + +/** Manages the acquisition of ledgers. +*/ +// VFALCO TODO Rename to IncomingLedgerManager +// VFALCO TODO Create abstract interface +class LedgerAcquireMaster +{ +public: + // How long before we try again to acquire the same ledger + static const int kReacquireIntervalSeconds = 600; + + LedgerAcquireMaster () + : mRecentFailures ("LedgerAcquireRecentFailures", 0, kReacquireIntervalSeconds) + { + } + + LedgerAcquire::pointer findCreate(uint256 const& hash, uint32 seq); + LedgerAcquire::pointer find(uint256 const& hash); + bool hasLedger(uint256 const& ledgerHash); + void dropLedger(uint256 const& ledgerHash); + + bool awaitLedgerData(uint256 const& ledgerHash); + void gotLedgerData(Job&, uint256 hash, boost::shared_ptr packet, boost::weak_ptr peer); + + int getFetchCount(int& timeoutCount); + void logFailure(uint256 const& h) { mRecentFailures.add(h); } + bool isFailure(uint256 const& h) { return mRecentFailures.isPresent(h, false); } + + void gotFetchPack(Job&); + void sweep(); + +private: + boost::mutex mLock; + std::map mLedgers; + KeyCache mRecentFailures; +}; + +#endif + +// vim:ts=4 diff --git a/src/cpp/ripple/LedgerHistory.cpp b/src/cpp/ripple/ripple_LedgerHistory.cpp similarity index 97% rename from src/cpp/ripple/LedgerHistory.cpp rename to src/cpp/ripple/ripple_LedgerHistory.cpp index 9b3f4419fc1..9ab682450df 100644 --- a/src/cpp/ripple/LedgerHistory.cpp +++ b/src/cpp/ripple/ripple_LedgerHistory.cpp @@ -1,4 +1,6 @@ +// VFALCO TODO replace macros + #ifndef CACHED_LEDGER_NUM #define CACHED_LEDGER_NUM 96 #endif @@ -67,7 +69,7 @@ Ledger::pointer LedgerHistory::getLedgerBySeq(uint32 index) return (ret->getLedgerSeq() == index) ? ret : Ledger::pointer(); } -Ledger::pointer LedgerHistory::getLedgerByHash(const uint256& hash) +Ledger::pointer LedgerHistory::getLedgerByHash(uint256 const& hash) { Ledger::pointer ret = mLedgersByHash.fetch(hash); if (ret) diff --git a/src/cpp/ripple/ripple_LedgerHistory.h b/src/cpp/ripple/ripple_LedgerHistory.h new file mode 100644 index 00000000000..33653781874 --- /dev/null +++ b/src/cpp/ripple/ripple_LedgerHistory.h @@ -0,0 +1,42 @@ +#ifndef RIPPLE_LEDGERHISTORY_H +#define RIPPLE_LEDGERHISTORY_H + +// VFALCO TODO Rename to OldLedgers ? +class LedgerHistory +{ +public: + LedgerHistory (); + + void addLedger (Ledger::pointer ledger); + + void addAcceptedLedger (Ledger::pointer ledger, bool fromConsensus); + + float getCacheHitRate() + { + return mLedgersByHash.getHitRate(); + } + + Ledger::pointer getLedgerBySeq (LedgerIndex ledgerIndex); + + // VFALCO NOTE shouldn't this call the function above? + LedgerHash getLedgerHash (LedgerIndex ledgerIndex); + + Ledger::pointer getLedgerByHash (LedgerHash const& ledgerHash); + + Ledger::pointer canonicalizeLedger (Ledger::pointer ledger, bool cache); + + void tune (int size, int age); + + void sweep () + { + mLedgersByHash.sweep (); + } + +private: + TaggedCache mLedgersByHash; + + // Maps ledger indexes to the corresponding hash. + std::map mLedgersByIndex; // accepted ledgers +}; + +#endif diff --git a/src/cpp/ripple/ripple_Peer.cpp b/src/cpp/ripple/ripple_Peer.cpp index 75212b09803..650d3435fa2 100644 --- a/src/cpp/ripple/ripple_Peer.cpp +++ b/src/cpp/ripple/ripple_Peer.cpp @@ -59,9 +59,9 @@ class PeerImp : public Peer bool isInbound() const { return mInbound; } bool isOutbound() const { return !mInbound; } - const uint256& getClosedLedgerHash() const { return mClosedLedgerHash; } - bool hasLedger(const uint256& hash, uint32 seq) const; - bool hasTxSet(const uint256& hash) const; + uint256 const& getClosedLedgerHash() const { return mClosedLedgerHash; } + bool hasLedger(uint256 const& hash, uint32 seq) const; + bool hasTxSet(uint256 const& hash) const; uint64 getPeerId() const { return mPeerId; } const RippleAddress& getNodePublic() const { return mNodePublic; } @@ -143,8 +143,8 @@ class PeerImp : public Peer void getSessionCookie(std::string& strDst); - void addLedger(const uint256& ledger); - void addTxSet(const uint256& TxSet); + void addLedger(uint256 const& ledger); + void addTxSet(uint256 const& TxSet); void doFetchPack(const boost::shared_ptr& packet); @@ -1868,7 +1868,7 @@ void PeerImp::recvLedger(const boost::shared_ptr& packet_p nodeIDs.push_back(SHAMapNode(node.nodeid().data(), node.nodeid().size())); nodeData.push_back(Blob (node.nodedata().begin(), node.nodedata().end())); } - SMAddNode san = theApp->getOPs().gotTXData(shared_from_this(), hash, nodeIDs, nodeData); + SHAMapAddNode san = theApp->getOPs().gotTXData(shared_from_this(), hash, nodeIDs, nodeData); if (san.isInvalid()) punishPeer(LT_UnwantedData); return; @@ -1882,19 +1882,19 @@ void PeerImp::recvLedger(const boost::shared_ptr& packet_p punishPeer(LT_UnwantedData); } -bool PeerImp::hasLedger(const uint256& hash, uint32 seq) const +bool PeerImp::hasLedger(uint256 const& hash, uint32 seq) const { if ((seq != 0) && (seq >= mMinLedger) && (seq <= mMaxLedger)) return true; - BOOST_FOREACH(const uint256& ledger, mRecentLedgers) + BOOST_FOREACH(uint256 const& ledger, mRecentLedgers) if (ledger == hash) return true; return false; } -void PeerImp::addLedger(const uint256& hash) +void PeerImp::addLedger(uint256 const& hash) { - BOOST_FOREACH(const uint256& ledger, mRecentLedgers) + BOOST_FOREACH(uint256 const& ledger, mRecentLedgers) if (ledger == hash) return; if (mRecentLedgers.size() == 128) @@ -1902,17 +1902,17 @@ void PeerImp::addLedger(const uint256& hash) mRecentLedgers.push_back(hash); } -bool PeerImp::hasTxSet(const uint256& hash) const +bool PeerImp::hasTxSet(uint256 const& hash) const { - BOOST_FOREACH(const uint256& set, mRecentTxSets) + BOOST_FOREACH(uint256 const& set, mRecentTxSets) if (set == hash) return true; return false; } -void PeerImp::addTxSet(const uint256& hash) +void PeerImp::addTxSet(uint256 const& hash) { - BOOST_FOREACH(const uint256& set, mRecentTxSets) + BOOST_FOREACH(uint256 const& set, mRecentTxSets) if (set == hash) return; if (mRecentTxSets.size() == 128) diff --git a/src/cpp/ripple/ripple_Peer.h b/src/cpp/ripple/ripple_Peer.h index f62b61890a2..84f1eece7f2 100644 --- a/src/cpp/ripple/ripple_Peer.h +++ b/src/cpp/ripple/ripple_Peer.h @@ -63,11 +63,11 @@ class Peer : public boost::enable_shared_from_this virtual bool isOutbound () const = 0; - virtual const uint256& getClosedLedgerHash () const = 0; + virtual uint256 const& getClosedLedgerHash () const = 0; - virtual bool hasLedger (const uint256& hash, uint32 seq) const = 0; + virtual bool hasLedger (uint256 const& hash, uint32 seq) const = 0; - virtual bool hasTxSet (const uint256& hash) const = 0; + virtual bool hasTxSet (uint256 const& hash) const = 0; virtual uint64 getPeerId () const = 0; diff --git a/src/cpp/ripple/ripple_PeerSet.cpp b/src/cpp/ripple/ripple_PeerSet.cpp new file mode 100644 index 00000000000..a4851b4a606 --- /dev/null +++ b/src/cpp/ripple/ripple_PeerSet.cpp @@ -0,0 +1,87 @@ + +class LedgerAcquire; + +PeerSet::PeerSet (uint256 const& hash, int interval) + : mHash (hash) + , mTimerInterval (interval) + , mTimeouts(0) + , mComplete (false) + , mFailed (false) + , mProgress (true) + , mAggressive (false) + , mTimer (theApp->getIOService()) +{ + mLastAction = UptimeTimer::getInstance().getElapsedSeconds(); + assert((mTimerInterval > 10) && (mTimerInterval < 30000)); +} + +void PeerSet::peerHas(Peer::ref ptr) +{ + boost::recursive_mutex::scoped_lock sl(mLock); + if (!mPeers.insert(std::make_pair(ptr->getPeerId(), 0)).second) + return; + newPeer(ptr); +} + +void PeerSet::badPeer(Peer::ref ptr) +{ + boost::recursive_mutex::scoped_lock sl(mLock); + mPeers.erase(ptr->getPeerId()); +} + +void PeerSet::setTimer() +{ + mTimer.expires_from_now(boost::posix_time::milliseconds(mTimerInterval)); + mTimer.async_wait(boost::bind(&PeerSet::TimerEntry, pmDowncast(), boost::asio::placeholders::error)); +} + +void PeerSet::invokeOnTimer() +{ + boost::recursive_mutex::scoped_lock sl(mLock); + if (isDone()) + return; + + if (!mProgress) + { + ++mTimeouts; + WriteLog (lsWARNING, LedgerAcquire) << "Timeout(" << mTimeouts << ") pc=" << mPeers.size() << " acquiring " << mHash; + onTimer(false); + } + else + { + mProgress = false; + onTimer(true); + } + + if (!isDone()) + setTimer(); +} + +void PeerSet::TimerEntry(boost::weak_ptr wptr, const boost::system::error_code& result) +{ + if (result == boost::asio::error::operation_aborted) + return; + boost::shared_ptr ptr = wptr.lock(); + if (ptr) + { + int jc = theApp->getJobQueue().getJobCountTotal(jtLEDGER_DATA); + if (jc > 4) + { + WriteLog (lsDEBUG, LedgerAcquire) << "Deferring PeerSet timer due to load"; + ptr->setTimer(); + } + else + theApp->getJobQueue().addJob(jtLEDGER_DATA, "timerEntry", BIND_TYPE(&PeerSet::TimerJobEntry, P_1, ptr)); + } +} + +void PeerSet::TimerJobEntry(Job&, boost::shared_ptr ptr) +{ + ptr->invokeOnTimer(); +} + +bool PeerSet::isActive() +{ + boost::recursive_mutex::scoped_lock sl(mLock); + return !isDone(); +} diff --git a/src/cpp/ripple/ripple_PeerSet.h b/src/cpp/ripple/ripple_PeerSet.h new file mode 100644 index 00000000000..6196cf3d8b5 --- /dev/null +++ b/src/cpp/ripple/ripple_PeerSet.h @@ -0,0 +1,71 @@ +#ifndef RIPPLE_PEERSET_H +#define RIPPLE_PEERSET_H + +/** A set of peers used to acquire data. + + A peer set is used to acquire a ledger or a transaction set. +*/ +class PeerSet +{ +public: + uint256 const& getHash() const { return mHash; } + bool isComplete() const { return mComplete; } + bool isFailed() const { return mFailed; } + int getTimeouts() const { return mTimeouts; } + + bool isActive(); + void progress() { mProgress = true; mAggressive = false; } + bool isProgress() { return mProgress; } + void touch() { mLastAction = UptimeTimer::getInstance().getElapsedSeconds(); } + int getLastAction() { return mLastAction; } + + void peerHas(Peer::ref); + void badPeer(Peer::ref); + void setTimer(); + + int takePeerSetFrom(const PeerSet& s); + int getPeerCount() const; + virtual bool isDone() const { return mComplete || mFailed; } + +private: + static void TimerEntry (boost::weak_ptr, const boost::system::error_code& result); + static void TimerJobEntry (Job&, boost::shared_ptr); + + // VFALCO TODO try to make some of these private +protected: + PeerSet (uint256 const& hash, int interval); + virtual ~PeerSet() { } + + virtual void newPeer(Peer::ref) = 0; + virtual void onTimer(bool progress) = 0; + virtual boost::weak_ptr pmDowncast() = 0; + + void setComplete() { mComplete = true; } + void setFailed() { mFailed = true; } + void invokeOnTimer(); + + void sendRequest(const ripple::TMGetLedger& message); + void sendRequest(const ripple::TMGetLedger& message, Peer::ref peer); + +protected: + uint256 mHash; + int mTimerInterval; + int mTimeouts; + bool mComplete; + bool mFailed; + bool mProgress; + bool mAggressive; + int mLastAction; + + + boost::recursive_mutex mLock; + // VFALCO TODO move the responsibility for the timer to a higher level + boost::asio::deadline_timer mTimer; + + // VFALCO TODO Verify that these are used in the way that the names suggest. + typedef uint64 PeerIdentifier; + typedef int ReceivedChunkCount; + boost::unordered_map mPeers; +}; + +#endif diff --git a/src/cpp/ripple/ripple_ProofOfWork.cpp b/src/cpp/ripple/ripple_ProofOfWork.cpp index 84bde9870f9..45f8d65854f 100644 --- a/src/cpp/ripple/ripple_ProofOfWork.cpp +++ b/src/cpp/ripple/ripple_ProofOfWork.cpp @@ -46,8 +46,8 @@ const int ProofOfWork::sMaxDifficulty(30); ProofOfWork::ProofOfWork (const std::string& token, int iterations, - const uint256& challenge, - const uint256& target) + uint256 const& challenge, + uint256 const& target) : mToken (token) , mChallenge (challenge) , mTarget (target) @@ -76,7 +76,7 @@ bool ProofOfWork::isValid() const return false; } -uint64 ProofOfWork::getDifficulty(const uint256& target, int iterations) +uint64 ProofOfWork::getDifficulty(uint256 const& target, int iterations) { // calculate the approximate number of hashes required to solve this proof of work if ((iterations > sMaxIterations) || (target < sMinTarget)) { @@ -141,7 +141,7 @@ uint256 ProofOfWork::solve(int maxIterations) const return uint256(); } -bool ProofOfWork::checkSolution(const uint256& solution) const +bool ProofOfWork::checkSolution(uint256 const& solution) const { if (mIterations > sMaxIterations) return false; diff --git a/src/cpp/ripple/ripple_ProofOfWork.h b/src/cpp/ripple/ripple_ProofOfWork.h index 9a5872ab607..2b8b1019e83 100644 --- a/src/cpp/ripple/ripple_ProofOfWork.h +++ b/src/cpp/ripple/ripple_ProofOfWork.h @@ -10,18 +10,18 @@ class ProofOfWork ProofOfWork (const std::string& token, int iterations, - const uint256& challenge, - const uint256& target); + uint256 const& challenge, + uint256 const& target); explicit ProofOfWork (const std::string& token); bool isValid() const; uint256 solve(int maxIterations = 2 * sMaxIterations) const; - bool checkSolution(const uint256& solution) const; + bool checkSolution(uint256 const& solution) const; const std::string& getToken() const { return mToken; } - const uint256& getChallenge() const { return mChallenge; } + uint256 const& getChallenge() const { return mChallenge; } uint64 getDifficulty() const { @@ -29,7 +29,7 @@ class ProofOfWork } // approximate number of hashes needed to solve - static uint64 getDifficulty (const uint256& target, int iterations); + static uint64 getDifficulty (uint256 const& target, int iterations); static bool validateToken (const std::string& strToken); diff --git a/src/cpp/ripple/ripple_ProofOfWorkFactory.cpp b/src/cpp/ripple/ripple_ProofOfWorkFactory.cpp index 805080e4b68..d837f3d418d 100644 --- a/src/cpp/ripple/ripple_ProofOfWorkFactory.cpp +++ b/src/cpp/ripple/ripple_ProofOfWorkFactory.cpp @@ -7,7 +7,7 @@ class ProofOfWorkFactory : public IProofOfWorkFactory ProofOfWorkFactory (); ProofOfWork getProof(); - POWResult checkProof(const std::string& token, const uint256& solution); + POWResult checkProof(const std::string& token, uint256 const& solution); uint64 getDifficulty() { return ProofOfWork::getDifficulty(mTarget, mIterations); } void setDifficulty(int i); @@ -15,10 +15,10 @@ class ProofOfWorkFactory : public IProofOfWorkFactory void loadLow(); void sweep(void); - const uint256& getSecret() const { return mSecret; } - void setSecret(const uint256& secret) { mSecret = secret; } + uint256 const& getSecret() const { return mSecret; } + void setSecret(uint256 const& secret) { mSecret = secret; } - static int getPowEntry (const uint256& target, int iterations); + static int getPowEntry (uint256 const& target, int iterations); private: uint256 mSecret; @@ -57,7 +57,7 @@ ProofOfWork ProofOfWorkFactory::getProof() return ProofOfWork(s, mIterations, challenge, mTarget); } -POWResult ProofOfWorkFactory::checkProof(const std::string& token, const uint256& solution) +POWResult ProofOfWorkFactory::checkProof(const std::string& token, uint256 const& solution) { // challenge - target - iterations - time - validator std::vector fields; @@ -208,7 +208,7 @@ PowEntry PowEntries[ProofOfWork::sMaxDifficulty + 1] = { "00003FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 262144}, // 77309411328, 8 MB }; -int ProofOfWorkFactory::getPowEntry(const uint256& target, int iterations) +int ProofOfWorkFactory::getPowEntry(uint256 const& target, int iterations) { for (int i = 0; i < 31; ++i) if (PowEntries[i].iterations == iterations) diff --git a/src/cpp/ripple/SHAMap.cpp b/src/cpp/ripple/ripple_SHAMap.cpp similarity index 92% rename from src/cpp/ripple/SHAMap.cpp rename to src/cpp/ripple/ripple_SHAMap.cpp index 3ebffb6d2c2..41319332196 100644 --- a/src/cpp/ripple/SHAMap.cpp +++ b/src/cpp/ripple/ripple_SHAMap.cpp @@ -1,11 +1,4 @@ -#include - -#include -#include -#include -#include -#include #ifndef STATE_MAP_BUCKETS #define STATE_MAP_BUCKETS 1024 @@ -14,8 +7,6 @@ SETUP_LOG (SHAMap) DECLARE_INSTANCE(SHAMap); -DECLARE_INSTANCE(SHAMapItem); -DECLARE_INSTANCE(SHAMapTreeNode); void SHAMapNode::setMHash() const { @@ -47,7 +38,7 @@ SHAMap::SHAMap(SHAMapType t, uint32 seq) : mSeq(seq), mLedgerSeq(0), mState(smsM mTNByID[*root] = root; } -SHAMap::SHAMap(SHAMapType t, const uint256& hash) : mSeq(1), mLedgerSeq(0), mState(smsSynching), mType(t) +SHAMap::SHAMap(SHAMapType t, uint256 const& hash) : mSeq(1), mLedgerSeq(0), mState(smsSynching), mType(t) { // FIXME: Need to acquire root node if (t == smtSTATE) mTNByID.rehash(STATE_MAP_BUCKETS); @@ -70,7 +61,7 @@ SHAMap::pointer SHAMap::snapShot(bool isMutable) return ret; } -std::stack SHAMap::getStack(const uint256& id, bool include_nonmatching_leaf, bool partialOk) +std::stack SHAMap::getStack(uint256 const& id, bool include_nonmatching_leaf, bool partialOk) { // Walk the tree as far as possible to the specified identifier // produce a stack of nodes along the way, with the terminal node at the top @@ -105,7 +96,7 @@ std::stack SHAMap::getStack(const uint256& id, bool inc return stack; } -void SHAMap::dirtyUp(std::stack& stack, const uint256& target, uint256 prevHash) +void SHAMap::dirtyUp(std::stack& stack, uint256 const& target, uint256 prevHash) { // walk the tree up from through the inner nodes to the root // update linking hashes and add nodes to dirty list @@ -145,7 +136,7 @@ SHAMapTreeNode::pointer SHAMap::checkCacheNode(const SHAMapNode& iNode) return it->second; } -SHAMapTreeNode::pointer SHAMap::walkTo(const uint256& id, bool modify) +SHAMapTreeNode::pointer SHAMap::walkTo(uint256 const& id, bool modify) { // walk down to the terminal node for this ID SHAMapTreeNode::pointer inNode = root; @@ -174,7 +165,7 @@ SHAMapTreeNode::pointer SHAMap::walkTo(const uint256& id, bool modify) return inNode; } -SHAMapTreeNode* SHAMap::walkToPointer(const uint256& id) +SHAMapTreeNode* SHAMap::walkToPointer(uint256 const& id) { SHAMapTreeNode* inNode = root.get(); while (!inNode->isLeaf()) @@ -189,7 +180,7 @@ SHAMapTreeNode* SHAMap::walkToPointer(const uint256& id) return (inNode->getTag() == id) ? inNode : NULL; } -SHAMapTreeNode::pointer SHAMap::getNode(const SHAMapNode& id, const uint256& hash, bool modify) +SHAMapTreeNode::pointer SHAMap::getNode(const SHAMapNode& id, uint256 const& hash, bool modify) { // retrieve a node whose node hash is known SHAMapTreeNode::pointer node = checkCacheNode(id); if (node) @@ -211,7 +202,7 @@ SHAMapTreeNode::pointer SHAMap::getNode(const SHAMapNode& id, const uint256& has return fetchNodeExternal(id, hash); } -SHAMapTreeNode* SHAMap::getNodePointer(const SHAMapNode& id, const uint256& hash) +SHAMapTreeNode* SHAMap::getNodePointer(const SHAMapNode& id, uint256 const& hash) { // fast, but you do not hold a reference boost::unordered_map::iterator it = mTNByID.find(id); if (it != mTNByID.end()) @@ -220,7 +211,7 @@ SHAMapTreeNode* SHAMap::getNodePointer(const SHAMapNode& id, const uint256& hash return fetchNodeExternal(id, hash).get(); } -SHAMapTreeNode* SHAMap::getNodePointer(const SHAMapNode& id, const uint256& hash, SHAMapSyncFilter* filter) +SHAMapTreeNode* SHAMap::getNodePointer(const SHAMapNode& id, uint256 const& hash, SHAMapSyncFilter* filter) { try { @@ -270,14 +261,6 @@ void SHAMap::trackNewNode(SHAMapTreeNode::pointer& node) (*mDirtyNodes)[*node] = node; } -SHAMapItem::SHAMapItem(const uint256& tag, Blob const& data) - : mTag(tag), mData(data) -{ ; } - -SHAMapItem::SHAMapItem(const uint256& tag, const Serializer& data) - : mTag(tag), mData(data.peekData()) -{ ; } - SHAMapTreeNode* SHAMap::firstBelow(SHAMapTreeNode* node) { // Return the first item below this node @@ -401,14 +384,14 @@ SHAMapItem::pointer SHAMap::peekLastItem() return node->peekItem(); } -SHAMapItem::pointer SHAMap::peekNextItem(const uint256& id) +SHAMapItem::pointer SHAMap::peekNextItem(uint256 const& id) { SHAMapTreeNode::TNType type; return peekNextItem(id, type); } -SHAMapItem::pointer SHAMap::peekNextItem(const uint256& id, SHAMapTreeNode::TNType& type) +SHAMapItem::pointer SHAMap::peekNextItem(uint256 const& id, SHAMapTreeNode::TNType& type) { // Get a pointer to the next item in the tree after a given item - item must be in tree boost::recursive_mutex::scoped_lock sl(mLock); @@ -444,7 +427,7 @@ SHAMapItem::pointer SHAMap::peekNextItem(const uint256& id, SHAMapTreeNode::TNTy } // Get a pointer to the previous item in the tree after a given item - item must be in tree -SHAMapItem::pointer SHAMap::peekPrevItem(const uint256& id) +SHAMapItem::pointer SHAMap::peekPrevItem(uint256 const& id) { boost::recursive_mutex::scoped_lock sl(mLock); @@ -481,7 +464,7 @@ SHAMapItem::pointer SHAMap::peekPrevItem(const uint256& id) return no_item; } -SHAMapItem::pointer SHAMap::peekItem(const uint256& id) +SHAMapItem::pointer SHAMap::peekItem(uint256 const& id) { boost::recursive_mutex::scoped_lock sl(mLock); SHAMapTreeNode* leaf = walkToPointer(id); @@ -490,7 +473,7 @@ SHAMapItem::pointer SHAMap::peekItem(const uint256& id) return leaf->peekItem(); } -SHAMapItem::pointer SHAMap::peekItem(const uint256& id, SHAMapTreeNode::TNType& type) +SHAMapItem::pointer SHAMap::peekItem(uint256 const& id, SHAMapTreeNode::TNType& type) { boost::recursive_mutex::scoped_lock sl(mLock); SHAMapTreeNode* leaf = walkToPointer(id); @@ -500,7 +483,7 @@ SHAMapItem::pointer SHAMap::peekItem(const uint256& id, SHAMapTreeNode::TNType& return leaf->peekItem(); } -SHAMapItem::pointer SHAMap::peekItem(const uint256& id, uint256& hash) +SHAMapItem::pointer SHAMap::peekItem(uint256 const& id, uint256& hash) { boost::recursive_mutex::scoped_lock sl(mLock); SHAMapTreeNode* leaf = walkToPointer(id); @@ -511,7 +494,7 @@ SHAMapItem::pointer SHAMap::peekItem(const uint256& id, uint256& hash) } -bool SHAMap::hasItem(const uint256& id) +bool SHAMap::hasItem(uint256 const& id) { // does the tree have an item with this ID boost::recursive_mutex::scoped_lock sl(mLock); @@ -519,7 +502,7 @@ bool SHAMap::hasItem(const uint256& id) return (leaf != NULL); } -bool SHAMap::delItem(const uint256& id) +bool SHAMap::delItem(uint256 const& id) { // delete the item with this ID boost::recursive_mutex::scoped_lock sl(mLock); assert(mState != smsImmutable); @@ -707,7 +690,7 @@ void SHAMapItem::dump() WriteLog (lsINFO, SHAMap) << "SHAMapItem(" << mTag << ") " << mData.size() << "bytes"; } -SHAMapTreeNode::pointer SHAMap::fetchNodeExternal(const SHAMapNode& id, const uint256& hash) +SHAMapTreeNode::pointer SHAMap::fetchNodeExternal(const SHAMapNode& id, uint256 const& hash) { if (!theApp->running()) throw SHAMapMissingNode(mType, id, hash); @@ -754,7 +737,7 @@ SHAMapTreeNode::pointer SHAMap::fetchNodeExternal(const SHAMapNode& id, const ui } } -void SHAMap::fetchRoot(const uint256& hash, SHAMapSyncFilter* filter) +void SHAMap::fetchRoot(uint256 const& hash, SHAMapSyncFilter* filter) { if (hash == root->getNodeHash()) return; @@ -790,12 +773,12 @@ int SHAMap::armDirty() return ++mSeq; } -int SHAMap::flushDirty(SHADirtyMap& map, int maxNodes, HashedObjectType t, uint32 seq) +int SHAMap::flushDirty(DirtyMap& map, int maxNodes, HashedObjectType t, uint32 seq) { int flushed = 0; Serializer s; - for(SHADirtyMap::iterator it = map.begin(); it != map.end(); it = map.erase(it)) + for(DirtyMap::iterator it = map.begin(); it != map.end(); it = map.erase(it)) { // tLog(t == hotTRANSACTION_NODE, lsDEBUG) << "TX node write " << it->first; // tLog(t == hotACCOUNT_NODE, lsDEBUG) << "STATE node write " << it->first; @@ -820,11 +803,11 @@ int SHAMap::flushDirty(SHADirtyMap& map, int maxNodes, HashedObjectType t, uint3 return flushed; } -boost::shared_ptr SHAMap::disarmDirty() +boost::shared_ptr SHAMap::disarmDirty() { // stop saving dirty nodes boost::recursive_mutex::scoped_lock sl(mLock); - boost::shared_ptr ret; + boost::shared_ptr ret; ret.swap(mDirtyNodes); return ret; } @@ -850,7 +833,7 @@ SHAMapTreeNode::pointer SHAMap::getNode(const SHAMapNode& nodeID) return node; } -bool SHAMap::getPath(const uint256& index, std::vector< Blob >& nodes, SHANodeFormat format) +bool SHAMap::getPath(uint256 const& index, std::vector< Blob >& nodes, SHANodeFormat format) { // Return the path of nodes to the specified index in the specified format // Return value: true = node present, false = node not present diff --git a/src/cpp/ripple/ripple_SHAMap.h b/src/cpp/ripple/ripple_SHAMap.h new file mode 100644 index 00000000000..a12d9c6d89a --- /dev/null +++ b/src/cpp/ripple/ripple_SHAMap.h @@ -0,0 +1,173 @@ +#ifndef RIPPLE_SHAMAP_H +#define RIPPLE_SHAMAP_H + +DEFINE_INSTANCE(SHAMap); + +enum SHAMapState +{ + smsModifying = 0, // Objects can be added and removed (like an open ledger) + smsImmutable = 1, // Map cannot be changed (like a closed ledger) + smsSynching = 2, // Map's hash is locked in, valid nodes can be added (like a peer's closing ledger) + smsFloating = 3, // Map is free to change hash (like a synching open ledger) + smsInvalid = 4, // Map is known not to be valid (usually synching a corrupt ledger) +}; + +class SHAMap : public IS_INSTANCE(SHAMap) +{ +public: + typedef boost::shared_ptr pointer; + typedef const boost::shared_ptr& ref; + + typedef std::pair DeltaItem; + typedef std::map Delta; + typedef boost::unordered_map DirtyMap; + +public: + // build new map + SHAMap(SHAMapType t, uint32 seq = 1); + SHAMap(SHAMapType t, uint256 const& hash); + + ~SHAMap() { mState = smsInvalid; } + + // Returns a new map that's a snapshot of this one. Force CoW + SHAMap::pointer snapShot(bool isMutable); + + // Remove nodes from memory + void dropCache(); + + void setLedgerSeq(uint32 lseq) { mLedgerSeq = lseq; } + + // hold the map stable across operations + ScopedLock Lock() const { return ScopedLock(mLock); } + + bool hasNode(const SHAMapNode& id); + void fetchRoot(uint256 const& hash, SHAMapSyncFilter* filter); + + // normal hash access functions + bool hasItem(uint256 const& id); + bool delItem(uint256 const& id); + bool addItem(const SHAMapItem& i, bool isTransaction, bool hasMeta); + bool updateItem(const SHAMapItem& i, bool isTransaction, bool hasMeta); + SHAMapItem getItem(uint256 const& id); + uint256 getHash() const { return root->getNodeHash(); } + uint256 getHash() { return root->getNodeHash(); } + + // save a copy if you have a temporary anyway + bool updateGiveItem(SHAMapItem::ref, bool isTransaction, bool hasMeta); + bool addGiveItem(SHAMapItem::ref, bool isTransaction, bool hasMeta); + + // save a copy if you only need a temporary + SHAMapItem::pointer peekItem(uint256 const& id); + SHAMapItem::pointer peekItem(uint256 const& id, uint256& hash); + SHAMapItem::pointer peekItem(uint256 const& id, SHAMapTreeNode::TNType& type); + + // traverse functions + SHAMapItem::pointer peekFirstItem(); + SHAMapItem::pointer peekFirstItem(SHAMapTreeNode::TNType& type); + SHAMapItem::pointer peekLastItem(); + SHAMapItem::pointer peekNextItem(uint256 const& ); + SHAMapItem::pointer peekNextItem(uint256 const& , SHAMapTreeNode::TNType& type); + SHAMapItem::pointer peekPrevItem(uint256 const& ); + + // comparison/sync functions + void getMissingNodes(std::vector& nodeIDs, std::vector& hashes, int max, + SHAMapSyncFilter* filter); + bool getNodeFat(const SHAMapNode& node, std::vector& nodeIDs, + std::list& rawNode, bool fatRoot, bool fatLeaves); + bool getRootNode(Serializer& s, SHANodeFormat format); + std::vector getNeededHashes(int max, SHAMapSyncFilter* filter); + SHAMapAddNode addRootNode(uint256 const& hash, Blob const& rootNode, SHANodeFormat format, + SHAMapSyncFilter* filter); + SHAMapAddNode addRootNode(Blob const& rootNode, SHANodeFormat format, + SHAMapSyncFilter* filter); + SHAMapAddNode addKnownNode(const SHAMapNode& nodeID, Blob const& rawNode, + SHAMapSyncFilter* filter); + + // status functions + void setImmutable() { assert(mState != smsInvalid); mState = smsImmutable; } + void clearImmutable() { mState = smsModifying; } + bool isSynching() const { return (mState == smsFloating) || (mState == smsSynching); } + void setSynching() { mState = smsSynching; } + void setFloating() { mState = smsFloating; } + void clearSynching() { mState = smsModifying; } + bool isValid() { return mState != smsInvalid; } + + // caution: otherMap must be accessed only by this function + // return value: true=successfully completed, false=too different + bool compare(SHAMap::ref otherMap, Delta& differences, int maxCount); + + int armDirty(); + static int flushDirty(DirtyMap& dirtyMap, int maxNodes, HashedObjectType t, uint32 seq); + boost::shared_ptr disarmDirty(); + + void setSeq(uint32 seq) { mSeq = seq; } + uint32 getSeq() { return mSeq; } + + // overloads for backed maps + boost::shared_ptr fetchNodeExternal(const SHAMapNode& id, uint256 const& hash); + + bool operator==(const SHAMap& s) { return getHash() == s.getHash(); } + + // trusted path operations - prove a particular node is in a particular ledger + std::list getTrustedPath(uint256 const& index); + static Blob checkTrustedPath(uint256 const& ledgerHash, uint256 const& leafIndex, + const std::list& path); + + void walkMap (std::vector& missingNodes, int maxMissing); + + bool getPath (uint256 const& index, std::vector< Blob >& nodes, SHANodeFormat format); + + bool deepCompare (SHAMap& other); + + virtual void dump (bool withHashes = false); + + typedef std::pair fetchPackEntry_t; + + std::list getFetchPack(SHAMap* have, bool includeLeaves, int max); + + static void sweep() { fullBelowCache.sweep(); } + +private: + static KeyCache fullBelowCache; + + void dirtyUp(std::stack& stack, uint256 const& target, uint256 prevHash); + std::stack getStack(uint256 const& id, bool include_nonmatching_leaf, bool partialOk); + SHAMapTreeNode::pointer walkTo(uint256 const& id, bool modify); + SHAMapTreeNode* walkToPointer(uint256 const& id); + SHAMapTreeNode::pointer checkCacheNode(const SHAMapNode&); + void returnNode(SHAMapTreeNode::pointer&, bool modify); + void trackNewNode(SHAMapTreeNode::pointer&); + + SHAMapTreeNode::pointer getNode(const SHAMapNode& id); + SHAMapTreeNode::pointer getNode(const SHAMapNode& id, uint256 const& hash, bool modify); + SHAMapTreeNode* getNodePointer(const SHAMapNode& id, uint256 const& hash); + SHAMapTreeNode* getNodePointer(const SHAMapNode& id, uint256 const& hash, SHAMapSyncFilter* filter); + SHAMapTreeNode* firstBelow(SHAMapTreeNode*); + SHAMapTreeNode* lastBelow(SHAMapTreeNode*); + + SHAMapItem::pointer onlyBelow(SHAMapTreeNode*); + void eraseChildren(SHAMapTreeNode::pointer); + void dropBelow(SHAMapTreeNode*); + bool hasInnerNode(const SHAMapNode& nodeID, uint256 const& hash); + bool hasLeafNode(uint256 const& tag, uint256 const& hash); + + bool walkBranch(SHAMapTreeNode* node, SHAMapItem::ref otherMapItem, bool isFirstMap, + Delta& differences, int& maxCount); + +private: + uint32 mSeq; + uint32 mLedgerSeq; // sequence number of ledger this is part of + mutable boost::recursive_mutex mLock; + boost::unordered_map mTNByID; + + boost::shared_ptr mDirtyNodes; + + SHAMapTreeNode::pointer root; + + SHAMapState mState; + + SHAMapType mType; +}; + +#endif +// vim:ts=4 diff --git a/src/cpp/ripple/ripple_SHAMapAddNode.h b/src/cpp/ripple/ripple_SHAMapAddNode.h new file mode 100644 index 00000000000..cd7e456db10 --- /dev/null +++ b/src/cpp/ripple/ripple_SHAMapAddNode.h @@ -0,0 +1,54 @@ +#ifndef RIPPLE_SHAMAPADDNODE_H +#define RIPPLE_SHAMAPADDNODE_H + +// results of adding nodes +class SHAMapAddNode +{ +public: + SHAMapAddNode() + : mInvalid (false) + , mUseful (false) + { + } + + void setInvalid() { mInvalid = true; } + void setUseful() { mUseful = true; } + void reset() { mInvalid = false; mUseful = false; } + + bool isInvalid() const { return mInvalid; } + bool isUseful() const { return mUseful; } + + bool combine (SHAMapAddNode const& n) + { + // VFALCO NOTE What is the meaning of these lines? + + if (n.mInvalid) + { + mInvalid = true; + return false; + } + + if (n.mUseful) + mUseful = true; + + return true; + } + + operator bool() const { return !mInvalid; } + + static SHAMapAddNode okay() { return SHAMapAddNode(false, false); } + static SHAMapAddNode useful() { return SHAMapAddNode(false, true); } + static SHAMapAddNode invalid() { return SHAMapAddNode(true, false); } + +private: + SHAMapAddNode (bool i, bool u) + : mInvalid (i) + , mUseful (u) + { + } + + bool mInvalid; + bool mUseful; +}; + +#endif diff --git a/src/cpp/ripple/SHAMapDiff.cpp b/src/cpp/ripple/ripple_SHAMapDelta.cpp similarity index 93% rename from src/cpp/ripple/SHAMapDiff.cpp rename to src/cpp/ripple/ripple_SHAMapDelta.cpp index 1fc636527e2..4b8d91c53b6 100644 --- a/src/cpp/ripple/SHAMapDiff.cpp +++ b/src/cpp/ripple/ripple_SHAMapDelta.cpp @@ -6,18 +6,18 @@ // makes no sense at all. (And our sync algorithm will avoid // synchronizing matching brances too.) -class SHAMapDiffNode +class SHAMapDeltaNode { public: SHAMapNode mNodeID; uint256 mOurHash, mOtherHash; - SHAMapDiffNode(const SHAMapNode& id, const uint256& ourHash, const uint256& otherHash) : + SHAMapDeltaNode(const SHAMapNode& id, uint256 const& ourHash, uint256 const& otherHash) : mNodeID(id), mOurHash(ourHash), mOtherHash(otherHash) { ; } }; bool SHAMap::walkBranch(SHAMapTreeNode* node, SHAMapItem::ref otherMapItem, bool isFirstMap, - SHAMapDiff& differences, int& maxCount) + Delta& differences, int& maxCount) { // Walk a branch of a SHAMap that's matched by an empty branch or single item in the other map std::stack nodeStack; @@ -92,7 +92,7 @@ bool SHAMap::walkBranch(SHAMapTreeNode* node, SHAMapItem::ref otherMapItem, bool return true; } -bool SHAMap::compare(SHAMap::ref otherMap, SHAMapDiff& differences, int maxCount) +bool SHAMap::compare(SHAMap::ref otherMap, Delta& differences, int maxCount) { // compare two hash trees, add up to maxCount differences to the difference table // return value: true=complete table of differences given, false=too many differences // throws on corrupt tables or missing nodes @@ -100,17 +100,17 @@ bool SHAMap::compare(SHAMap::ref otherMap, SHAMapDiff& differences, int maxCount assert(isValid() && otherMap && otherMap->isValid()); - std::stack nodeStack; // track nodes we've pushed + std::stack nodeStack; // track nodes we've pushed boost::recursive_mutex::scoped_lock sl(mLock); if (getHash() == otherMap->getHash()) return true; - nodeStack.push(SHAMapDiffNode(SHAMapNode(), getHash(), otherMap->getHash())); + nodeStack.push(SHAMapDeltaNode(SHAMapNode(), getHash(), otherMap->getHash())); while (!nodeStack.empty()) { - SHAMapDiffNode dNode(nodeStack.top()); + SHAMapDeltaNode dNode(nodeStack.top()); nodeStack.pop(); SHAMapTreeNode* ourNode = getNodePointer(dNode.mNodeID, dNode.mOurHash); @@ -174,7 +174,7 @@ bool SHAMap::compare(SHAMap::ref otherMap, SHAMapDiff& differences, int maxCount return false; } else // The two trees have different non-empty branches - nodeStack.push(SHAMapDiffNode(ourNode->getChildNodeID(i), + nodeStack.push(SHAMapDeltaNode(ourNode->getChildNodeID(i), ourNode->getChildHash(i), otherNode->getChildHash(i))); } } diff --git a/src/cpp/ripple/ripple_SHAMapItem.cpp b/src/cpp/ripple/ripple_SHAMapItem.cpp new file mode 100644 index 00000000000..c8580af23f0 --- /dev/null +++ b/src/cpp/ripple/ripple_SHAMapItem.cpp @@ -0,0 +1,16 @@ + +class SHAMap; + +DECLARE_INSTANCE(SHAMapItem); + +SHAMapItem::SHAMapItem (uint256 const& tag, Blob const& data) + : mTag (tag) + , mData (data) +{ +} + +SHAMapItem::SHAMapItem(uint256 const& tag, const Serializer& data) + : mTag (tag) + , mData (data.peekData()) +{ +} diff --git a/src/cpp/ripple/ripple_SHAMapItem.h b/src/cpp/ripple/ripple_SHAMapItem.h new file mode 100644 index 00000000000..896b7406ffc --- /dev/null +++ b/src/cpp/ripple/ripple_SHAMapItem.h @@ -0,0 +1,52 @@ +#ifndef RIPPLE_SHAMAPITEM_H +#define RIPPLE_SHAMAPITEM_H + +DEFINE_INSTANCE(SHAMapItem); + +// an item stored in a SHAMap +class SHAMapItem : public IS_INSTANCE(SHAMapItem) +{ +public: + typedef boost::shared_ptr pointer; + typedef const boost::shared_ptr& ref; + +public: + explicit SHAMapItem (uint256 const& tag) : mTag(tag) { ; } + explicit SHAMapItem(Blob const& data); // tag by hash + SHAMapItem (uint256 const& tag, Blob const& data); + SHAMapItem (uint256 const& tag, const Serializer& s); + + uint256 const& getTag() const { return mTag; } + Blob getData() const { return mData.getData(); } + Blob const& peekData() const { return mData.peekData(); } + Serializer& peekSerializer() { return mData; } + void addRaw(Blob& s) const { s.insert(s.end(), mData.begin(), mData.end()); } + + void updateData(Blob const& data) { mData=data; } + + bool operator==(const SHAMapItem& i) const { return mTag == i.mTag; } + bool operator!=(const SHAMapItem& i) const { return mTag != i.mTag; } + bool operator==(uint256 const& i) const { return mTag == i; } + bool operator!=(uint256 const& i) const { return mTag != i; } + +#if 0 + // This code is comment out because it is unused. It could work. + bool operator<(const SHAMapItem& i) const { return mTag < i.mTag; } + bool operator>(const SHAMapItem& i) const { return mTag > i.mTag; } + bool operator<=(const SHAMapItem& i) const { return mTag <= i.mTag; } + bool operator>=(const SHAMapItem& i) const { return mTag >= i.mTag; } + + bool operator<(uint256 const& i) const { return mTag < i; } + bool operator>(uint256 const& i) const { return mTag > i; } + bool operator<=(uint256 const& i) const { return mTag <= i; } + bool operator>=(uint256 const& i) const { return mTag >= i; } +#endif + + virtual void dump(); + +private: + uint256 mTag; + Serializer mData; +}; + +#endif diff --git a/src/cpp/ripple/ripple_SHAMapMissingNode.cpp b/src/cpp/ripple/ripple_SHAMapMissingNode.cpp new file mode 100644 index 00000000000..8014513e407 --- /dev/null +++ b/src/cpp/ripple/ripple_SHAMapMissingNode.cpp @@ -0,0 +1,21 @@ + +std::ostream& operator<<(std::ostream& out, const SHAMapMissingNode& mn) +{ + switch (mn.getMapType ()) + { + case smtTRANSACTION: + out << "Missing/TXN(" << mn.getNodeID() << "/" << mn.getNodeHash() << ")"; + break; + + case smtSTATE: + out << "Missing/STA(" << mn.getNodeID() << "/" << mn.getNodeHash() << ")"; + break; + + case smtFREE: + default: + out << "Missing/" << mn.getNodeID(); + break; + }; + + return out; +} diff --git a/src/cpp/ripple/ripple_SHAMapMissingNode.h b/src/cpp/ripple/ripple_SHAMapMissingNode.h new file mode 100644 index 00000000000..8be5fdce9b5 --- /dev/null +++ b/src/cpp/ripple/ripple_SHAMapMissingNode.h @@ -0,0 +1,79 @@ +#ifndef RIPPLE_SHAMAPMISSINGNODE_H +#define RIPPLE_SHAMAPMISSINGNODE_H + +enum SHAMapType +{ + smtTRANSACTION =1, // A tree of transactions + smtSTATE =2, // A tree of state nodes + smtFREE =3, // A tree not part of a ledger +}; + +class SHAMapMissingNode : public std::runtime_error +{ +public: + SHAMapMissingNode (SHAMapType t, + SHAMapNode const& nodeID, + uint256 const& nodeHash) + : std::runtime_error ("SHAMapMissingNode") + , mType (t) + , mNodeID (nodeID) + , mNodeHash (nodeHash) + { + } + + SHAMapMissingNode (SHAMapType t, + SHAMapNode const& nodeID, + uint256 const& nodeHash, + uint256 const& targetIndex) + : std::runtime_error (nodeID.getString ()) + , mType (t) + , mNodeID (nodeID) + , mNodeHash (nodeHash) + , mTargetIndex (targetIndex) + { + } + + virtual ~SHAMapMissingNode () throw() + { + } + + void setTargetNode (uint256 const& tn) + { + mTargetIndex = tn; + } + + SHAMapType getMapType () const + { + return mType; + } + + SHAMapNode const& getNodeID () const + { + return mNodeID; + } + + uint256 const& getNodeHash () const + { + return mNodeHash; + } + + uint256 const& getTargetIndex () const + { + return mTargetIndex; + } + + bool hasTargetIndex () const + { + return !mTargetIndex.isZero(); + } + +private: + SHAMapType mType; + SHAMapNode mNodeID; + uint256 mNodeHash; + uint256 mTargetIndex; +}; + +extern std::ostream& operator<< (std::ostream&, SHAMapMissingNode const&); + +#endif diff --git a/src/cpp/ripple/ripple_SHAMapNode.cpp b/src/cpp/ripple/ripple_SHAMapNode.cpp new file mode 100644 index 00000000000..08795351014 --- /dev/null +++ b/src/cpp/ripple/ripple_SHAMapNode.cpp @@ -0,0 +1,146 @@ + +SETUP_LOG (SHAMapNode) + +// canonicalize the hash to a node ID for this depth +SHAMapNode::SHAMapNode(int depth, uint256 const& hash) : mNodeID(hash), mDepth(depth), mHash(0) +{ + assert((depth >= 0) && (depth < 65)); + mNodeID &= smMasks[depth]; +} + +SHAMapNode::SHAMapNode(const void *ptr, int len) : mHash(0) +{ + if (len < 33) + mDepth = -1; + else + { + memcpy(mNodeID.begin(), ptr, 32); + mDepth = *(static_cast(ptr) + 32); + } +} +std::string SHAMapNode::getString() const +{ + static boost::format NodeID("NodeID(%s,%s)"); + + if ((mDepth == 0) && (mNodeID.isZero())) + return "NodeID(root)"; + + return str(boost::format(NodeID) + % boost::lexical_cast(mDepth) + % mNodeID.GetHex()); +} + +uint256 SHAMapNode::smMasks[65]; + +// VFALCO TODO use a static initializer to do this instead +bool SMN_j = SHAMapNode::ClassInit(); + +// set up the depth masks +bool SHAMapNode::ClassInit() +{ + uint256 selector; + for (int i = 0; i < 64; i += 2) + { + // VFALCO TODO group these statics together in an object + smMasks[i] = selector; + *(selector.begin() + (i / 2)) = 0xF0; + smMasks[i + 1] = selector; + *(selector.begin() + (i / 2)) = 0xFF; + } + smMasks[64] = selector; + return true; +} + + +bool SHAMapNode::operator<(const SHAMapNode &s) const +{ + if (s.mDepth < mDepth) return true; + if (s.mDepth > mDepth) return false; + return mNodeID < s.mNodeID; +} + +bool SHAMapNode::operator>(const SHAMapNode &s) const +{ + if (s.mDepth < mDepth) return false; + if (s.mDepth > mDepth) return true; + return mNodeID > s.mNodeID; +} + +bool SHAMapNode::operator<=(const SHAMapNode &s) const +{ + if (s.mDepth < mDepth) return true; + if (s.mDepth > mDepth) return false; + return mNodeID <= s.mNodeID; +} + +bool SHAMapNode::operator>=(const SHAMapNode &s) const +{ + if (s.mDepth < mDepth) return false; + if (s.mDepth > mDepth) return true; + return mNodeID >= s.mNodeID; +} + +uint256 SHAMapNode::getNodeID(int depth, uint256 const& hash) +{ + assert((depth >= 0) && (depth <= 64)); + return hash & smMasks[depth]; +} + +void SHAMapNode::addIDRaw(Serializer &s) const +{ + s.add256(mNodeID); + s.add8(mDepth); +} + +std::string SHAMapNode::getRawString() const +{ + Serializer s(33); + addIDRaw(s); + return s.getString(); +} + +// This can be optimized to avoid the << if needed +SHAMapNode SHAMapNode::getChildNodeID(int m) const +{ + assert((m >= 0) && (m < 16)); + + uint256 child(mNodeID); + child.begin()[mDepth/2] |= (mDepth & 1) ? m : (m << 4); + + return SHAMapNode(mDepth + 1, child, true); +} + +// Which branch would contain the specified hash +int SHAMapNode::selectBranch(uint256 const& hash) const +{ +#ifdef PARANOID + if (mDepth >= 64) + { + assert(false); + return -1; + } + + if ((hash & smMasks[mDepth]) != mNodeID) + { + std::cerr << "selectBranch(" << getString() << std::endl; + std::cerr << " " << hash << " off branch" << std::endl; + assert(false); + return -1; // does not go under this node + } +#endif + + int branch = *(hash.begin() + (mDepth / 2)); + if (mDepth & 1) + branch &= 0xf; + else + branch >>= 4; + + assert((branch >= 0) && (branch < 16)); + + return branch; +} + +void SHAMapNode::dump() const +{ + WriteLog (lsDEBUG, SHAMapNode) << getString(); +} diff --git a/src/cpp/ripple/ripple_SHAMapNode.h b/src/cpp/ripple/ripple_SHAMapNode.h new file mode 100644 index 00000000000..6e6780a9a1e --- /dev/null +++ b/src/cpp/ripple/ripple_SHAMapNode.h @@ -0,0 +1,67 @@ +#ifndef RIPPLE_SHAMAPNODE_H +#define RIPPLE_SHAMAPNODE_H + +// Identifies a node in a SHA256 hash map +class SHAMapNode +{ +public: + SHAMapNode() : mDepth(0), mHash(0) { ; } + SHAMapNode(int depth, uint256 const& hash); + + int getDepth() const { return mDepth; } + uint256 const& getNodeID() const { return mNodeID; } + bool isValid() const { return (mDepth >= 0) && (mDepth < 64); } + bool isRoot() const { return mDepth == 0; } + size_t getMHash() const { if (mHash == 0) setMHash(); return mHash; } + + virtual bool isPopulated() const { return false; } + + SHAMapNode getParentNodeID() const + { + assert(mDepth); + return SHAMapNode(mDepth - 1, mNodeID); + } + + SHAMapNode getChildNodeID(int m) const; + int selectBranch(uint256 const& hash) const; + + bool operator<(const SHAMapNode&) const; + bool operator>(const SHAMapNode&) const; + bool operator<=(const SHAMapNode&) const; + bool operator>=(const SHAMapNode&) const; + + bool operator==(const SHAMapNode& n) const { return (mDepth == n.mDepth) && (mNodeID == n.mNodeID); } + bool operator==(uint256 const& n) const { return n == mNodeID; } + bool operator!=(const SHAMapNode& n) const { return (mDepth != n.mDepth) || (mNodeID != n.mNodeID); } + bool operator!=(uint256 const& n) const { return n != mNodeID; } + + virtual std::string getString() const; + void dump() const; + + static bool ClassInit(); + static uint256 getNodeID(int depth, uint256 const& hash); + + // Convert to/from wire format (256-bit nodeID, 1-byte depth) + void addIDRaw(Serializer &s) const; + std::string getRawString() const; + static int getRawIDLength(void) { return 33; } + SHAMapNode(const void *ptr, int len); + +protected: + SHAMapNode(int depth, uint256 const& id, bool) : mNodeID(id), mDepth(depth), mHash(0) { ; } + +private: + static uint256 smMasks[65]; // AND with hash to get node id + + uint256 mNodeID; + int mDepth; + mutable size_t mHash; + + void setMHash() const; +}; + +extern std::size_t hash_value(const SHAMapNode& mn); + +inline std::ostream& operator<<(std::ostream& out, const SHAMapNode& node) { return out << node.getString(); } + +#endif diff --git a/src/cpp/ripple/SHAMapSync.cpp b/src/cpp/ripple/ripple_SHAMapSync.cpp similarity index 93% rename from src/cpp/ripple/SHAMapSync.cpp rename to src/cpp/ripple/ripple_SHAMapSync.cpp index 3dff92c6c20..bc81cd64ea2 100644 --- a/src/cpp/ripple/SHAMapSync.cpp +++ b/src/cpp/ripple/ripple_SHAMapSync.cpp @@ -1,13 +1,5 @@ -#include "SHAMap.h" - -#include -#include - -#include -#include - -#include +// VFALCO TODO tidy up this global static const uint256 uZero; @@ -47,7 +39,7 @@ void SHAMap::getMissingNodes(std::vector& nodeIDs, std::vectorisEmptyBranch(branch)) { - const uint256& childHash = node->getChildHash(branch); + uint256 const& childHash = node->getChildHash(branch); if (!fullBelowCache.isPresent(childHash)) { SHAMapNode childID = node->getChildNodeID(branch); @@ -114,7 +106,7 @@ std::vector SHAMap::getNeededHashes(int max, SHAMapSyncFilter* filter) int branch = (base + ii) % 16; if (!node->isEmptyBranch(branch)) { - const uint256& childHash = node->getChildHash(branch); + uint256 const& childHash = node->getChildHash(branch); if (!fullBelowCache.isPresent(childHash)) { SHAMapNode childID = node->getChildNodeID(branch); @@ -203,7 +195,7 @@ bool SHAMap::getRootNode(Serializer& s, SHANodeFormat format) return true; } -SMAddNode SHAMap::addRootNode(Blob const& rootNode, SHANodeFormat format, +SHAMapAddNode SHAMap::addRootNode(Blob const& rootNode, SHANodeFormat format, SHAMapSyncFilter* filter) { boost::recursive_mutex::scoped_lock sl(mLock); @@ -212,14 +204,14 @@ SMAddNode SHAMap::addRootNode(Blob const& rootNode, SHANodeFormat format, if (root->getNodeHash().isNonZero()) { WriteLog (lsTRACE, SHAMap) << "got root node, already have one"; - return SMAddNode::okay(); + return SHAMapAddNode::okay(); } assert(mSeq >= 1); SHAMapTreeNode::pointer node = boost::make_shared(SHAMapNode(), rootNode, mSeq - 1, format, uZero, false); if (!node) - return SMAddNode::invalid(); + return SHAMapAddNode::invalid(); #ifdef DEBUG node->dump(); @@ -239,10 +231,10 @@ SMAddNode SHAMap::addRootNode(Blob const& rootNode, SHANodeFormat format, filter->gotNode(false, *root, root->getNodeHash(), s.peekData(), root->getType()); } - return SMAddNode::useful(); + return SHAMapAddNode::useful(); } -SMAddNode SHAMap::addRootNode(const uint256& hash, Blob const& rootNode, SHANodeFormat format, +SHAMapAddNode SHAMap::addRootNode(uint256 const& hash, Blob const& rootNode, SHANodeFormat format, SHAMapSyncFilter* filter) { boost::recursive_mutex::scoped_lock sl(mLock); @@ -252,14 +244,14 @@ SMAddNode SHAMap::addRootNode(const uint256& hash, Blob const& rootNode, SHANode { WriteLog (lsTRACE, SHAMap) << "got root node, already have one"; assert(root->getNodeHash() == hash); - return SMAddNode::okay(); + return SHAMapAddNode::okay(); } assert(mSeq >= 1); SHAMapTreeNode::pointer node = boost::make_shared(SHAMapNode(), rootNode, mSeq - 1, format, uZero, false); if (!node || node->getNodeHash() != hash) - return SMAddNode::invalid(); + return SHAMapAddNode::invalid(); root = node; mTNByID[*root] = root; @@ -275,23 +267,23 @@ SMAddNode SHAMap::addRootNode(const uint256& hash, Blob const& rootNode, SHANode filter->gotNode(false, *root, root->getNodeHash(), s.peekData(), root->getType()); } - return SMAddNode::useful(); + return SHAMapAddNode::useful(); } -SMAddNode SHAMap::addKnownNode(const SHAMapNode& node, Blob const& rawNode, +SHAMapAddNode SHAMap::addKnownNode(const SHAMapNode& node, Blob const& rawNode, SHAMapSyncFilter* filter) { // return value: true=okay, false=error assert(!node.isRoot()); if (!isSynching()) { WriteLog (lsTRACE, SHAMap) << "AddKnownNode while not synching"; - return SMAddNode::okay(); + return SHAMapAddNode::okay(); } boost::recursive_mutex::scoped_lock sl(mLock); if (checkCacheNode(node)) // Do we already have this node? - return SMAddNode::okay(); + return SHAMapAddNode::okay(); SHAMapTreeNode* iNode = root.get(); while (!iNode->isLeaf() && !iNode->isFullBelow() && (iNode->getDepth() < node.getDepth())) @@ -302,10 +294,10 @@ SMAddNode SHAMap::addKnownNode(const SHAMapNode& node, Blob const& rawNode, if (iNode->isEmptyBranch(branch)) { WriteLog (lsWARNING, SHAMap) << "Add known node for empty branch" << node; - return SMAddNode::invalid(); + return SHAMapAddNode::invalid(); } if (fullBelowCache.isPresent(iNode->getChildHash(branch))) - return SMAddNode::okay(); + return SHAMapAddNode::okay(); try { @@ -318,7 +310,7 @@ SMAddNode SHAMap::addKnownNode(const SHAMapNode& node, Blob const& rawNode, WriteLog (lsWARNING, SHAMap) << "unable to hook node " << node; WriteLog (lsINFO, SHAMap) << " stuck at " << *iNode; WriteLog (lsINFO, SHAMap) << "got depth=" << node.getDepth() << ", walked to= " << iNode->getDepth(); - return SMAddNode::invalid(); + return SHAMapAddNode::invalid(); } SHAMapTreeNode::pointer newNode = @@ -326,7 +318,7 @@ SMAddNode SHAMap::addKnownNode(const SHAMapNode& node, Blob const& rawNode, if (iNode->getChildHash(branch) != newNode->getNodeHash()) { WriteLog (lsWARNING, SHAMap) << "Corrupt node recevied"; - return SMAddNode::invalid(); + return SHAMapAddNode::invalid(); } if (filter) @@ -336,12 +328,12 @@ SMAddNode SHAMap::addKnownNode(const SHAMapNode& node, Blob const& rawNode, filter->gotNode(false, node, iNode->getChildHash(branch), s.peekData(), newNode->getType()); } mTNByID[node] = newNode; - return SMAddNode::useful(); + return SHAMapAddNode::useful(); } } WriteLog (lsTRACE, SHAMap) << "got node, already had it (late)"; - return SMAddNode::okay(); + return SHAMapAddNode::okay(); } bool SHAMap::deepCompare(SHAMap& other) @@ -406,7 +398,7 @@ bool SHAMap::deepCompare(SHAMap& other) return true; } -bool SHAMap::hasInnerNode(const SHAMapNode& nodeID, const uint256& nodeHash) +bool SHAMap::hasInnerNode(const SHAMapNode& nodeID, uint256 const& nodeHash) { SHAMapTreeNode* node = root.get(); while (node->isInner() && (node->getDepth() < nodeID.getDepth())) @@ -419,7 +411,7 @@ bool SHAMap::hasInnerNode(const SHAMapNode& nodeID, const uint256& nodeHash) return node->getNodeHash() == nodeHash; } -bool SHAMap::hasLeafNode(const uint256& tag, const uint256& nodeHash) +bool SHAMap::hasLeafNode(uint256 const& tag, uint256 const& nodeHash) { SHAMapTreeNode* node = root.get(); while (node->isInner()) @@ -489,7 +481,7 @@ std::list SHAMap::getFetchPack(SHAMap* have, bool incl { if (!node->isEmptyBranch(i)) { - const uint256& childHash = node->getChildHash(i); + uint256 const& childHash = node->getChildHash(i); SHAMapNode childID = node->getChildNodeID(i); SHAMapTreeNode *next = getNodePointer(childID, childHash); @@ -562,7 +554,7 @@ static bool confuseMap(SHAMap &map, int count) return true; } -std::list SHAMap::getTrustedPath(const uint256& index) +std::list SHAMap::getTrustedPath(uint256 const& index) { boost::recursive_mutex::scoped_lock sl(mLock); std::stack stack = SHAMap::getStack(index, false, false); diff --git a/src/cpp/ripple/ripple_SHAMapSyncFilter.h b/src/cpp/ripple/ripple_SHAMapSyncFilter.h new file mode 100644 index 00000000000..5db57dca322 --- /dev/null +++ b/src/cpp/ripple/ripple_SHAMapSyncFilter.h @@ -0,0 +1,34 @@ +#ifndef RIPPLE_SHAMAPSYNCFILTER_H +#define RIPPLE_SHAMAPSYNCFILTER_H + +/** Callback for filtering SHAMap during sync. +*/ +class SHAMapSyncFilter +{ +public: + SHAMapSyncFilter () + { + } + + virtual ~SHAMapSyncFilter () + { + } + + virtual void gotNode (bool fromFilter, + SHAMapNode const& id, + uint256 const& nodeHash, + Blob const& nodeData, + SHAMapTreeNode::TNType type) + { + } + + virtual bool haveNode (SHAMapNode const& id, + uint256 const& nodeHash, + Blob&nodeData) + { + return false; + } +}; + +#endif +// vim:ts=4 diff --git a/src/cpp/ripple/ripple_SHAMapSyncFilters.cpp b/src/cpp/ripple/ripple_SHAMapSyncFilters.cpp new file mode 100644 index 00000000000..ea05668fbd3 --- /dev/null +++ b/src/cpp/ripple/ripple_SHAMapSyncFilters.cpp @@ -0,0 +1,100 @@ + +ConsensusTransSetSF::ConsensusTransSetSF () +{ +} + +void ConsensusTransSetSF::gotNode(bool fromFilter, const SHAMapNode& id, uint256 const& nodeHash, + Blob const& nodeData, SHAMapTreeNode::TNType type) +{ + if (fromFilter) + return; + theApp->getTempNodeCache().store(nodeHash, nodeData); + if ((type == SHAMapTreeNode::tnTRANSACTION_NM) && (nodeData.size() > 16)) + { // this is a transaction, and we didn't have it + WriteLog (lsDEBUG, TransactionAcquire) << "Node on our acquiring TX set is TXN we don't have"; + try + { + Serializer s(nodeData.begin() + 4, nodeData.end()); // skip prefix + SerializerIterator sit(s); + SerializedTransaction::pointer stx = boost::make_shared(boost::ref(sit)); + assert(stx->getTransactionID() == nodeHash); + theApp->getJobQueue().addJob(jtTRANSACTION, "TXS->TXN", + BIND_TYPE(&NetworkOPs::submitTransaction, &theApp->getOPs(), P_1, stx, NetworkOPs::stCallback())); + } + catch (...) + { + WriteLog (lsWARNING, TransactionAcquire) << "Fetched invalid transaction in proposed set"; + } + } +} + +bool ConsensusTransSetSF::haveNode(const SHAMapNode& id, uint256 const& nodeHash, + Blob& nodeData) +{ + if (theApp->getTempNodeCache().retrieve(nodeHash, nodeData)) + return true; + + Transaction::pointer txn = Transaction::load(nodeHash); + if (txn) + { // this is a transaction, and we have it + WriteLog (lsDEBUG, TransactionAcquire) << "Node in our acquiring TX set is TXN we have"; + Serializer s; + s.add32(sHP_TransactionID); + txn->getSTransaction()->add(s, true); + assert(s.getSHA512Half() == nodeHash); + nodeData = s.peekData(); + return true; + } + + return false; +} + +//------------------------------------------------------------------------------ + +AccountStateSF::AccountStateSF (uint32 ledgerSeq) + : mLedgerSeq (ledgerSeq) +{ +} + +void AccountStateSF::gotNode (bool fromFilter, + SHAMapNode const& id, + uint256 const& nodeHash, + Blob const& nodeData, + SHAMapTreeNode::TNType) +{ + theApp->getHashedObjectStore ().store (hotACCOUNT_NODE, mLedgerSeq, nodeData, nodeHash); +} + +bool AccountStateSF::haveNode (SHAMapNode const& id, + uint256 const& nodeHash, + Blob& nodeData) +{ + return theApp->getOPs().getFetchPack(nodeHash, nodeData); +} + +//------------------------------------------------------------------------------ + +TransactionStateSF::TransactionStateSF (uint32 ledgerSeq) + : mLedgerSeq (ledgerSeq) +{ +} + +void TransactionStateSF::gotNode (bool fromFilter, + SHAMapNode const& id, + uint256 const& nodeHash, + Blob const& nodeData, + SHAMapTreeNode::TNType type) +{ + theApp->getHashedObjectStore().store( + (type == SHAMapTreeNode::tnTRANSACTION_NM) ? hotTRANSACTION : hotTRANSACTION_NODE, + mLedgerSeq, + nodeData, + nodeHash); +} + +bool TransactionStateSF::haveNode (SHAMapNode const& id, + uint256 const& nodeHash, + Blob& nodeData) +{ + return theApp->getOPs().getFetchPack (nodeHash, nodeData); +} diff --git a/src/cpp/ripple/ripple_SHAMapSyncFilters.h b/src/cpp/ripple/ripple_SHAMapSyncFilters.h new file mode 100644 index 00000000000..69a886e302b --- /dev/null +++ b/src/cpp/ripple/ripple_SHAMapSyncFilters.h @@ -0,0 +1,67 @@ +#ifndef RIPPLE_SHAMAPSYNCFILTERS_H +#define RIPPLE_SHAMAPSYNCFILTERS_H + +// Sync filters allow low-level SHAMapSync code to interact correctly with +// higher-level structures such as caches and transaction stores + +// This class is needed on both add and check functions +// sync filter for transaction sets during consensus building +class ConsensusTransSetSF : public SHAMapSyncFilter +{ +public: + ConsensusTransSetSF (); + + void gotNode (bool fromFilter, + SHAMapNode const& id, + uint256 const& nodeHash, + Blob const& nodeData, + SHAMapTreeNode::TNType); + + bool haveNode (SHAMapNode const& id, + uint256 const& nodeHash, + Blob& nodeData); +}; + +// This class is only needed on add functions +// sync filter for account state nodes during ledger sync +class AccountStateSF : public SHAMapSyncFilter +{ +public: + explicit AccountStateSF (uint32 ledgerSeq); + + void gotNode (bool fromFilter, + SHAMapNode const& id, + uint256 const& nodeHash, + Blob const& nodeData, + SHAMapTreeNode::TNType); + + bool haveNode (SHAMapNode const& id, + uint256 const& nodeHash, + Blob& nodeData); + +private: + uint32 mLedgerSeq; +}; + +// This class is only needed on add functions +// sync filter for transactions tree during ledger sync +class TransactionStateSF : public SHAMapSyncFilter +{ +public: + explicit TransactionStateSF (uint32 ledgerSeq); + + void gotNode (bool fromFilter, + SHAMapNode const& id, + uint256 const& nodeHash, + Blob const& nodeData, + SHAMapTreeNode::TNType); + + bool haveNode (SHAMapNode const& id, + uint256 const& nodeHash, + Blob& nodeData); + +private: + uint32 mLedgerSeq; +}; + +#endif diff --git a/src/cpp/ripple/SHAMapNodes.cpp b/src/cpp/ripple/ripple_SHAMapTreeNode.cpp similarity index 71% rename from src/cpp/ripple/SHAMapNodes.cpp rename to src/cpp/ripple/ripple_SHAMapTreeNode.cpp index 813c2fdc136..bd41e12d6cf 100644 --- a/src/cpp/ripple/SHAMapNodes.cpp +++ b/src/cpp/ripple/ripple_SHAMapTreeNode.cpp @@ -1,142 +1,4 @@ - -SETUP_LOG (SHAMapNode) - -std::string SHAMapNode::getString() const -{ - static boost::format NodeID("NodeID(%s,%s)"); - - if ((mDepth == 0) && (mNodeID.isZero())) - return "NodeID(root)"; - - return str(boost::format(NodeID) - % boost::lexical_cast(mDepth) - % mNodeID.GetHex()); -} - -uint256 SHAMapNode::smMasks[65]; - -bool SHAMapNode::operator<(const SHAMapNode &s) const -{ - if (s.mDepth < mDepth) return true; - if (s.mDepth > mDepth) return false; - return mNodeID < s.mNodeID; -} - -bool SHAMapNode::operator>(const SHAMapNode &s) const -{ - if (s.mDepth < mDepth) return false; - if (s.mDepth > mDepth) return true; - return mNodeID > s.mNodeID; -} - -bool SHAMapNode::operator<=(const SHAMapNode &s) const -{ - if (s.mDepth < mDepth) return true; - if (s.mDepth > mDepth) return false; - return mNodeID <= s.mNodeID; -} - -bool SHAMapNode::operator>=(const SHAMapNode &s) const -{ - if (s.mDepth < mDepth) return false; - if (s.mDepth > mDepth) return true; - return mNodeID >= s.mNodeID; -} - -bool SMN_j = SHAMapNode::ClassInit(); - -bool SHAMapNode::ClassInit() -{ // set up the depth masks - uint256 selector; - for (int i = 0; i < 64; i += 2) - { - smMasks[i] = selector; - *(selector.begin() + (i / 2)) = 0xF0; - smMasks[i + 1] = selector; - *(selector.begin() + (i / 2)) = 0xFF; - } - smMasks[64] = selector; - return true; -} - -uint256 SHAMapNode::getNodeID(int depth, const uint256& hash) -{ - assert((depth >= 0) && (depth <= 64)); - return hash & smMasks[depth]; -} - -SHAMapNode::SHAMapNode(int depth, const uint256 &hash) : mNodeID(hash), mDepth(depth), mHash(0) -{ // canonicalize the hash to a node ID for this depth - assert((depth >= 0) && (depth < 65)); - mNodeID &= smMasks[depth]; -} - -SHAMapNode::SHAMapNode(const void *ptr, int len) : mHash(0) -{ - if (len < 33) - mDepth = -1; - else - { - memcpy(mNodeID.begin(), ptr, 32); - mDepth = *(static_cast(ptr) + 32); - } -} - -void SHAMapNode::addIDRaw(Serializer &s) const -{ - s.add256(mNodeID); - s.add8(mDepth); -} - -std::string SHAMapNode::getRawString() const -{ - Serializer s(33); - addIDRaw(s); - return s.getString(); -} - -SHAMapNode SHAMapNode::getChildNodeID(int m) const -{ // This can be optimized to avoid the << if needed - assert((m >= 0) && (m < 16)); - - uint256 child(mNodeID); - child.begin()[mDepth/2] |= (mDepth & 1) ? m : (m << 4); - - return SHAMapNode(mDepth + 1, child, true); -} - -int SHAMapNode::selectBranch(const uint256& hash) const -{ // Which branch would contain the specified hash -#ifdef PARANOID - if (mDepth >= 64) - { - assert(false); - return -1; - } - - if ((hash & smMasks[mDepth]) != mNodeID) - { - std::cerr << "selectBranch(" << getString() << std::endl; - std::cerr << " " << hash << " off branch" << std::endl; - assert(false); - return -1; // does not go under this node - } -#endif - - int branch = *(hash.begin() + (mDepth / 2)); - if (mDepth & 1) - branch &= 0xf; - else - branch >>= 4; - - assert((branch >= 0) && (branch < 16)); - return branch; -} - -void SHAMapNode::dump() const -{ - WriteLog (lsDEBUG, SHAMapNode) << getString(); -} +DECLARE_INSTANCE(SHAMapTreeNode); SHAMapTreeNode::SHAMapTreeNode(uint32 seq, const SHAMapNode& nodeID) : SHAMapNode(nodeID), mHash(0), mSeq(seq), mAccessSeq(seq), mType(tnERROR), mIsBranch(0), mFullBelow(false) @@ -160,7 +22,7 @@ SHAMapTreeNode::SHAMapTreeNode(const SHAMapNode& node, SHAMapItem::ref item, TNT } SHAMapTreeNode::SHAMapTreeNode(const SHAMapNode& id, Blob const& rawNode, uint32 seq, - SHANodeFormat format, const uint256& hash, bool hashValid) : + SHANodeFormat format, uint256 const& hash, bool hashValid) : SHAMapNode(id), mSeq(seq), mType(tnERROR), mIsBranch(0), mFullBelow(false) { if (format == snfWIRE) @@ -529,7 +391,7 @@ std::string SHAMapTreeNode::getString() const return ret; } -bool SHAMapTreeNode::setChildHash(int m, const uint256 &hash) +bool SHAMapTreeNode::setChildHash(int m, uint256 const& hash) { assert((m >= 0) && (m < 16)); assert(mType == tnINNER); @@ -542,16 +404,3 @@ bool SHAMapTreeNode::setChildHash(int m, const uint256 &hash) mIsBranch &= ~(1 << m); return updateHash(); } - -std::ostream& operator<<(std::ostream& out, const SHAMapMissingNode& mn) -{ - if (mn.getMapType() == smtTRANSACTION) - out << "Missing/TXN(" << mn.getNodeID() << "/" << mn.getNodeHash() << ")"; - else if (mn.getMapType() == smtSTATE) - out << "Missing/STA(" << mn.getNodeID() << "/" << mn.getNodeHash() << ")"; - else - out << "Missing/" << mn.getNodeID(); - return out; -} - -// vim:ts=4 diff --git a/src/cpp/ripple/ripple_SHAMapTreeNode.h b/src/cpp/ripple/ripple_SHAMapTreeNode.h new file mode 100644 index 00000000000..03c3e6ab0fc --- /dev/null +++ b/src/cpp/ripple/ripple_SHAMapTreeNode.h @@ -0,0 +1,106 @@ +#ifndef RIPPLE_SHAMAPTREENODE_H +#define RIPPLE_SHAMAPTREENODE_H + +DEFINE_INSTANCE(SHAMapTreeNode); + +class SHAMap; + +enum SHANodeFormat +{ + snfPREFIX = 1, // Form that hashes to its official hash + snfWIRE = 2, // Compressed form used on the wire + snfHASH = 3, // just the hash +}; + +class SHAMapTreeNode : public SHAMapNode, public IS_INSTANCE(SHAMapTreeNode) +{ +public: + typedef boost::shared_ptr pointer; + typedef const boost::shared_ptr& ref; + + enum TNType + { + tnERROR = 0, + tnINNER = 1, + tnTRANSACTION_NM = 2, // transaction, no metadata + tnTRANSACTION_MD = 3, // transaction, with metadata + tnACCOUNT_STATE = 4 + }; + +public: + SHAMapTreeNode(uint32 seq, const SHAMapNode& nodeID); // empty node + SHAMapTreeNode(const SHAMapTreeNode& node, uint32 seq); // copy node from older tree + SHAMapTreeNode(const SHAMapNode& nodeID, SHAMapItem::ref item, TNType type, uint32 seq); + + // raw node functions + SHAMapTreeNode(const SHAMapNode& id, Blob const& data, uint32 seq, + SHANodeFormat format, uint256 const& hash, bool hashValid); + void addRaw(Serializer &, SHANodeFormat format); + + virtual bool isPopulated() const { return true; } + + // node functions + uint32 getSeq() const { return mSeq; } + void setSeq(uint32 s) { mAccessSeq = mSeq = s; } + void touch(uint32 s) { mAccessSeq = s; } + uint256 const& getNodeHash() const { return mHash; } + TNType getType() const { return mType; } + + // type functions + bool isLeaf() const { return (mType == tnTRANSACTION_NM) || (mType == tnTRANSACTION_MD) || + (mType == tnACCOUNT_STATE); } + bool isInner() const { return mType == tnINNER; } + bool isValid() const { return mType != tnERROR; } + bool isTransaction() const { return (mType == tnTRANSACTION_NM) || (mType == tnTRANSACTION_MD); } + bool hasMetaData() const { return mType == tnTRANSACTION_MD; } + bool isAccountState() const { return mType == tnACCOUNT_STATE; } + + // inner node functions + bool isInnerNode() const { return !mItem; } + bool setChildHash(int m, uint256 const& hash); + bool isEmptyBranch(int m) const { return (mIsBranch & (1 << m)) == 0; } + bool isEmpty() const; + int getBranchCount() const; + void makeInner(); + uint256 const& getChildHash(int m) const + { + assert((m >= 0) && (m < 16) && (mType == tnINNER)); + return mHashes[m]; + } + + // item node function + bool hasItem() const { return !!mItem; } + SHAMapItem::ref peekItem() { return mItem; } + SHAMapItem::pointer getItem() const; + bool setItem(SHAMapItem::ref i, TNType type); + uint256 const& getTag() const { return mItem->getTag(); } + Blob const& peekData() { return mItem->peekData(); } + Blob getData() const { return mItem->getData(); } + + // sync functions + bool isFullBelow(void) const { return mFullBelow; } + void setFullBelow(void) { mFullBelow = true; } + + virtual void dump(); + virtual std::string getString() const; + +private: + // VFALCO TODO derive from Uncopyable + SHAMapTreeNode(const SHAMapTreeNode&); // no implementation + SHAMapTreeNode& operator=(const SHAMapTreeNode&); // no implementation + + // VFALCO TODO remove the use of friend + friend class SHAMap; + + uint256 mHash; + uint256 mHashes[16]; + SHAMapItem::pointer mItem; + uint32 mSeq, mAccessSeq; + TNType mType; + int mIsBranch; + bool mFullBelow; + + bool updateHash(); +}; + +#endif diff --git a/src/cpp/ripple/SerializedLedger.cpp b/src/cpp/ripple/ripple_SerializedLedger.cpp similarity index 96% rename from src/cpp/ripple/SerializedLedger.cpp rename to src/cpp/ripple/ripple_SerializedLedger.cpp index 72d72e77f18..d78cacf50f5 100644 --- a/src/cpp/ripple/SerializedLedger.cpp +++ b/src/cpp/ripple/ripple_SerializedLedger.cpp @@ -2,11 +2,11 @@ DECLARE_INSTANCE(SerializedLedgerEntry) // For logging -struct SerializedLedgerLog { }; +struct SerializedLedgerLog; SETUP_LOG (SerializedLedgerLog) -SerializedLedgerEntry::SerializedLedgerEntry(SerializerIterator& sit, const uint256& index) +SerializedLedgerEntry::SerializedLedgerEntry(SerializerIterator& sit, uint256 const& index) : STObject(sfLedgerEntry), mIndex(index), mMutable(true) { set(sit); @@ -19,7 +19,7 @@ SerializedLedgerEntry::SerializedLedgerEntry(SerializerIterator& sit, const uint throw std::runtime_error("ledger entry not valid for type"); } -SerializedLedgerEntry::SerializedLedgerEntry(const Serializer& s, const uint256& index) +SerializedLedgerEntry::SerializedLedgerEntry(const Serializer& s, uint256 const& index) : STObject(sfLedgerEntry), mIndex(index), mMutable(true) { SerializerIterator sit(const_cast(s)); // we know 's' isn't going away @@ -38,7 +38,7 @@ SerializedLedgerEntry::SerializedLedgerEntry(const Serializer& s, const uint256& } } -SerializedLedgerEntry::SerializedLedgerEntry(LedgerEntryType type, const uint256& index) : +SerializedLedgerEntry::SerializedLedgerEntry(LedgerEntryType type, uint256 const& index) : STObject(sfLedgerEntry), mIndex(index), mType(type), mMutable(true) { mFormat = LedgerEntryFormat::getLgrFormat(type); @@ -102,7 +102,7 @@ uint32 SerializedLedgerEntry::getThreadedLedger() return getFieldU32(sfPreviousTxnLgrSeq); } -bool SerializedLedgerEntry::thread(const uint256& txID, uint32 ledgerSeq, uint256& prevTxID, uint32& prevLedgerID) +bool SerializedLedgerEntry::thread(uint256 const& txID, uint32 ledgerSeq, uint256& prevTxID, uint32& prevLedgerID) { uint256 oldPrevTxID = getFieldH256(sfPreviousTxnID); WriteLog (lsTRACE, SerializedLedgerLog) << "Thread Tx:" << txID << " prev:" << oldPrevTxID; diff --git a/src/cpp/ripple/SerializedLedger.h b/src/cpp/ripple/ripple_SerializedLedger.h similarity index 76% rename from src/cpp/ripple/SerializedLedger.h rename to src/cpp/ripple/ripple_SerializedLedger.h index 867f80f98e0..7f87733cb26 100644 --- a/src/cpp/ripple/SerializedLedger.h +++ b/src/cpp/ripple/ripple_SerializedLedger.h @@ -1,9 +1,9 @@ -#ifndef __SERIALIZEDLEDGER__ -#define __SERIALIZEDLEDGER__ +#ifndef RIPPLE_SERIALIZEDLEDGER_H +#define RIPPLE_SERIALIZEDLEDGER_H DEFINE_INSTANCE(SerializedLedgerEntry); -// VFALCO TODO rename this to SerializedLedger +// VFALCO TODO rename this to SerializedLedger? class SerializedLedgerEntry : public STObject, private IS_INSTANCE(SerializedLedgerEntry) { public: @@ -11,17 +11,17 @@ class SerializedLedgerEntry : public STObject, private IS_INSTANCE(SerializedLed typedef const boost::shared_ptr& ref; public: - SerializedLedgerEntry(const Serializer& s, const uint256& index); - SerializedLedgerEntry(SerializerIterator& sit, const uint256& index); - SerializedLedgerEntry(LedgerEntryType type, const uint256& index); + SerializedLedgerEntry(const Serializer& s, uint256 const& index); + SerializedLedgerEntry(SerializerIterator& sit, uint256 const& index); + SerializedLedgerEntry(LedgerEntryType type, uint256 const& index); SerializedTypeID getSType() const { return STI_LEDGERENTRY; } std::string getFullText() const; std::string getText() const; Json::Value getJson(int options) const; - const uint256& getIndex() const { return mIndex; } - void setIndex(const uint256& i) { mIndex = i; } + uint256 const& getIndex() const { return mIndex; } + void setIndex(uint256 const& i) { mIndex = i; } void setImmutable() { mMutable = false; } bool isMutable() { return mMutable; } @@ -40,16 +40,17 @@ class SerializedLedgerEntry : public STObject, private IS_INSTANCE(SerializedLed RippleAddress getSecondOwner(); uint256 getThreadedTransaction(); uint32 getThreadedLedger(); - bool thread(const uint256& txID, uint32 ledgerSeq, uint256& prevTxID, uint32& prevLedgerID); + bool thread(uint256 const& txID, uint32 ledgerSeq, uint256& prevTxID, uint32& prevLedgerID); std::vector getOwners(); // nodes notified if this node is deleted +private: + SerializedLedgerEntry* duplicate() const { return new SerializedLedgerEntry(*this); } + private: uint256 mIndex; LedgerEntryType mType; const LedgerEntryFormat* mFormat; bool mMutable; - - SerializedLedgerEntry* duplicate() const { return new SerializedLedgerEntry(*this); } }; typedef SerializedLedgerEntry SLE; diff --git a/src/cpp/ripple/SerializedTransaction.cpp b/src/cpp/ripple/ripple_SerializedTransaction.cpp similarity index 100% rename from src/cpp/ripple/SerializedTransaction.cpp rename to src/cpp/ripple/ripple_SerializedTransaction.cpp diff --git a/src/cpp/ripple/SerializedTransaction.h b/src/cpp/ripple/ripple_SerializedTransaction.h similarity index 96% rename from src/cpp/ripple/SerializedTransaction.h rename to src/cpp/ripple/ripple_SerializedTransaction.h index cbe4e50ecc6..114e9fad7ef 100644 --- a/src/cpp/ripple/SerializedTransaction.h +++ b/src/cpp/ripple/ripple_SerializedTransaction.h @@ -1,5 +1,5 @@ -#ifndef __SERIALIZEDTRANSACTION__ -#define __SERIALIZEDTRANSACTION__ +#ifndef RIPPLE_SERIALIZEDTRANSACTION_H +#define RIPPLE_SERIALIZEDTRANSACTION_H // VFALCO TODO eliminate these macros @@ -82,7 +82,8 @@ class SerializedTransaction : public STObject, private IS_INSTANCE(SerializedTra SerializedTransaction* duplicate() const { return new SerializedTransaction(*this); } - mutable bool mSigGood, mSigBad; + mutable bool mSigGood; + mutable bool mSigBad; }; #endif diff --git a/src/cpp/ripple/ripple_Validations.cpp b/src/cpp/ripple/ripple_Validations.cpp index 4dea97ed5be..1784847e3e5 100644 --- a/src/cpp/ripple/ripple_Validations.cpp +++ b/src/cpp/ripple/ripple_Validations.cpp @@ -22,7 +22,7 @@ class Validations : public IValidations bool mWriting; private: - boost::shared_ptr findCreateSet(const uint256& ledgerHash) + boost::shared_ptr findCreateSet(uint256 const& ledgerHash) { VSpointer j = mValidations.fetch(ledgerHash); if (!j) @@ -33,7 +33,7 @@ class Validations : public IValidations return j; } - boost::shared_ptr findSet(const uint256& ledgerHash) + boost::shared_ptr findSet(uint256 const& ledgerHash) { return mValidations.fetch(ledgerHash); } @@ -108,7 +108,7 @@ class Validations : public IValidations mValidations.setTargetAge(age); } - ValidationSet getValidations(const uint256& ledger) + ValidationSet getValidations(uint256 const& ledger) { { boost::mutex::scoped_lock sl(mValidationLock); @@ -119,7 +119,7 @@ class Validations : public IValidations return ValidationSet(); } - void getValidationCount(const uint256& ledger, bool currentOnly, int& trusted, int &untrusted) + void getValidationCount(uint256 const& ledger, bool currentOnly, int& trusted, int &untrusted) { trusted = untrusted = 0; boost::mutex::scoped_lock sl(mValidationLock); @@ -149,7 +149,7 @@ class Validations : public IValidations WriteLog (lsTRACE, Validations) << "VC: " << ledger << "t:" << trusted << " u:" << untrusted; } - void getValidationTypes(const uint256& ledger, int& full, int& partial) + void getValidationTypes(uint256 const& ledger, int& full, int& partial) { full = partial = 0; boost::mutex::scoped_lock sl(mValidationLock); @@ -171,7 +171,7 @@ class Validations : public IValidations } - int getTrustedValidationCount(const uint256& ledger) + int getTrustedValidationCount(uint256 const& ledger) { int trusted = 0; boost::mutex::scoped_lock sl(mValidationLock); @@ -187,7 +187,7 @@ class Validations : public IValidations return trusted; } - int getNodesAfter(const uint256& ledger) + int getNodesAfter(uint256 const& ledger) { // Number of trusted nodes that have moved past this ledger int count = 0; boost::mutex::scoped_lock sl(mValidationLock); From 0b7e0b132a2d04e7b8c425086bdae476f5bca8f6 Mon Sep 17 00:00:00 2001 From: Vinnie Falco Date: Sun, 9 Jun 2013 14:12:28 -0700 Subject: [PATCH 2/3] Clean up STObject::set() for clarity --- .../protocol/ripple_SerializedObject.cpp | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/modules/ripple_data/protocol/ripple_SerializedObject.cpp b/modules/ripple_data/protocol/ripple_SerializedObject.cpp index 08c93edd935..6be78790b0f 100644 --- a/modules/ripple_data/protocol/ripple_SerializedObject.cpp +++ b/modules/ripple_data/protocol/ripple_SerializedObject.cpp @@ -203,6 +203,8 @@ bool STObject::isFieldAllowed(SField::ref field) return mType->getIndex(field) != -1; } +// OLD +/* bool STObject::set(SerializerIterator& sit, int depth) { // return true = terminated with end-of-object mData.clear(); @@ -222,6 +224,51 @@ bool STObject::set(SerializerIterator& sit, int depth) } return false; } +*/ + +// return true = terminated with end-of-object +bool STObject::set(SerializerIterator& sit, int depth) +{ + bool reachedEndOfObject = false; + + // Empty the destination buffer + // + mData.clear (); + + // Consume data in the pipe until we run out or reach the end + // + while (!reachedEndOfObject && !sit.empty ()) + { + int type; + int field; + + // Get the metadata for the next field + // + sit.getFieldID(type, field); + + reachedEndOfObject = (type == STI_OBJECT) && (field == 1); + + if (!reachedEndOfObject) + { + // Figure out the field + // + SField::ref fn = SField::getField(type, field); + + if (fn.isInvalid ()) + { + WriteLog (lsWARNING, STObject) << "Unknown field: field_type=" << type << ", field_name=" << field; + throw std::runtime_error("Unknown field"); + } + + // Unflatten the field + // + giveObject (makeDeserializedObject (fn.fieldType, fn, sit, depth + 1)); + } + } + + return reachedEndOfObject; +} + UPTR_T STObject::deserialize(SerializerIterator& sit, SField::ref name) { From cf3593b01b8cd2cbbc980615987f13a14dbaf4b2 Mon Sep 17 00:00:00 2001 From: Vinnie Falco Date: Sun, 9 Jun 2013 18:08:24 -0700 Subject: [PATCH 3/3] Tidy up and annotate --- RefactoringNotes.txt | 51 ++++++++++++++ StyleCheatSheet.txt | 2 + .../ripple_data/protocol/ripple_FieldNames.h | 69 +++++++++++++------ .../protocol/ripple_SerializeDeclarations.h | 9 +++ .../protocol/ripple_SerializedObject.cpp | 6 +- .../protocol/ripple_SerializedObject.h | 35 +++++++--- .../ripple_SerializedObjectTemplate.h | 8 ++- newcoin.vcxproj | 1 + newcoin.vcxproj.filters | 1 + src/cpp/ripple/LedgerConsensus.h | 3 +- src/cpp/ripple/LedgerMaster.cpp | 4 +- src/cpp/ripple/LedgerMaster.h | 16 +++-- src/cpp/ripple/ripple_AcceptedLedger.h | 7 +- src/cpp/ripple/ripple_AcceptedLedgerTx.h | 12 +++- src/cpp/ripple/ripple_CanonicalTXSet.h | 15 +++- src/cpp/ripple/ripple_LedgerAcquire.h | 2 +- src/cpp/ripple/ripple_LedgerAcquireMaster.h | 4 +- src/cpp/ripple/ripple_SHAMap.h | 4 +- 18 files changed, 197 insertions(+), 52 deletions(-) create mode 100644 RefactoringNotes.txt diff --git a/RefactoringNotes.txt b/RefactoringNotes.txt new file mode 100644 index 00000000000..8c76d2d9602 --- /dev/null +++ b/RefactoringNotes.txt @@ -0,0 +1,51 @@ +-------------------------------------------------------------------------------- + +Naming + +Some names don't make sense. + + LedgerAcquire + Not a noun. + Is it really an InboundLedger ? + Does it continue to exist after the ledger is received? + +Inconsistent names + + We have full names like SerializedType and then acronyms like STObject + Two names for some things, e.g. SerializedLedgerEntry and SLE + + Shared/Smart pointer typedefs in classes have a variety of different names + for the same thing. e.g. "pointer", "ptr", "ptr_t", "wptr" + +Verbose names + + The prefix "Flat" is more appealing than "Serialized" because its shorter and + easier to pronounce. + +Ledger "Skip List" + + Is not really a skip list + +-------------------------------------------------------------------------------- + +Interfaces + + Serializer + + Upon analysis this class does two incompatible things. Flattening, and + unflattening. The interface should be reimplemented as two distinct + abstract classes, InputStream and OutputStream with suitable implementations + such as to and from a block of memory or dynamically allocated buffer. + + The name and conflation of dual roles serves to confuse code at the point + of call. Does set(Serializer& s) flatten or unflatten the data? This + would be more clear: + bool write (OutputStream& stream); + + + +Implementation + + LoadManager + + What is going on in the destructor diff --git a/StyleCheatSheet.txt b/StyleCheatSheet.txt index a168e8c22c8..451b42f97aa 100644 --- a/StyleCheatSheet.txt +++ b/StyleCheatSheet.txt @@ -3,3 +3,5 @@ - Place each ctor-initializer on its own line. - Create typedefs for primitive types to describe them. - Return descriptive local variables instead of constants. +- Use "explicit" for single-argument ctors + diff --git a/modules/ripple_data/protocol/ripple_FieldNames.h b/modules/ripple_data/protocol/ripple_FieldNames.h index cdfaf387cc4..a5315f2ca55 100644 --- a/modules/ripple_data/protocol/ripple_FieldNames.h +++ b/modules/ripple_data/protocol/ripple_FieldNames.h @@ -23,6 +23,11 @@ enum SerializedTypeID STI_VALIDATION = 10003, }; +/** Identifies fields. + + Fields are necessary to tag data in signed transactions so that + the binary format of the transaction can be canonicalized. +*/ // VFALCO TODO rename this to NamedField class SField { @@ -43,40 +48,58 @@ class SField const int fieldCode; // (type<<16)|index const SerializedTypeID fieldType; // STI_* const int fieldValue; // Code number for protocol - std::string fieldName; + std::string fieldName; int fieldMeta; int fieldNum; bool signingField; - SField(int fc, SerializedTypeID tid, int fv, const char* fn) : - fieldCode(fc), fieldType(tid), fieldValue(fv), fieldName(fn), fieldMeta(sMD_Default), signingField(true) + SField (int fc, SerializedTypeID tid, int fv, const char* fn) + : fieldCode (fc) + , fieldType (tid) + , fieldValue (fv) + , fieldName (fn) + , fieldMeta (sMD_Default) + , signingField (true) { boost::mutex::scoped_lock sl(mapMutex); - codeToField[fieldCode] = this; - fieldNum = ++num; + + codeToField[fieldCode] = this; + + fieldNum = ++num; } - SField(SerializedTypeID tid, int fv, const char *fn) : - fieldCode(FIELD_CODE(tid, fv)), fieldType(tid), fieldValue(fv), fieldName(fn), - fieldMeta(sMD_Default), signingField(true) + SField (SerializedTypeID tid, int fv, const char *fn) + : fieldCode (FIELD_CODE (tid, fv)) + , fieldType (tid) + , fieldValue (fv) + , fieldName (fn) + , fieldMeta (sMD_Default) + , signingField (true) { boost::mutex::scoped_lock sl(mapMutex); - codeToField[fieldCode] = this; - fieldNum = ++num; + + codeToField[fieldCode] = this; + + fieldNum = ++num; } - SField(int fc) : fieldCode(fc), fieldType(STI_UNKNOWN), fieldValue(0), fieldMeta(sMD_Never), signingField(true) + explicit SField (int fc) + : fieldCode (fc) + , fieldType (STI_UNKNOWN) + , fieldValue (0) + , fieldMeta (sMD_Never) + , signingField (true) { boost::mutex::scoped_lock sl(mapMutex); fieldNum = ++num; } - ~SField(); + ~SField (); - static SField::ref getField(int fieldCode); - static SField::ref getField(const std::string& fieldName); - static SField::ref getField(int type, int value) { return getField(FIELD_CODE(type, value)); } - static SField::ref getField(SerializedTypeID type, int value) { return getField(FIELD_CODE(type, value)); } + static SField::ref getField (int fieldCode); + static SField::ref getField (const std::string& fieldName); + static SField::ref getField (int type, int value) { return getField(FIELD_CODE(type, value)); } + static SField::ref getField (SerializedTypeID type, int value) { return getField(FIELD_CODE(type, value)); } std::string getName() const; bool hasName() const { return !fieldName.empty(); } @@ -86,8 +109,11 @@ class SField bool isUseful() const { return fieldCode > 0; } bool isKnown() const { return fieldType != STI_UNKNOWN; } bool isBinary() const { return fieldValue < 256; } - bool isDiscardable() const { return fieldValue > 256; } - int getCode() const { return fieldCode; } + + // VFALCO NOTE What is a discardable field? + bool isDiscardable() const { return fieldValue > 256; } + + int getCode() const { return fieldCode; } int getNum() const { return fieldNum; } static int getNumFields() { return num; } @@ -99,8 +125,9 @@ class SField bool shouldInclude(bool withSigningField) const { return (fieldValue < 256) && (withSigningField || signingField); } - bool operator==(const SField& f) const { return fieldCode == f.fieldCode; } - bool operator!=(const SField& f) const { return fieldCode != f.fieldCode; } + bool operator== (const SField& f) const { return fieldCode == f.fieldCode; } + + bool operator!= (const SField& f) const { return fieldCode != f.fieldCode; } static int compare(SField::ref f1, SField::ref f2); @@ -110,7 +137,7 @@ class SField static boost::mutex mapMutex; static int num; - SField(SerializedTypeID id, int val); + SField (SerializedTypeID id, int val); }; extern SField sfInvalid, sfGeneric, sfLedgerEntry, sfTransaction, sfValidation; diff --git a/modules/ripple_data/protocol/ripple_SerializeDeclarations.h b/modules/ripple_data/protocol/ripple_SerializeDeclarations.h index 45593cce703..0730d992bda 100644 --- a/modules/ripple_data/protocol/ripple_SerializeDeclarations.h +++ b/modules/ripple_data/protocol/ripple_SerializeDeclarations.h @@ -1,6 +1,15 @@ // This is not really a header file, but it can be used as one with // appropriate #define statements. +/* + Common type common field - 1 byte + Common type uncommon field - 2 bytes + ... + + Rarity of fields determines the number of bytes. + This is done to reduce the average size of the messages. +*/ + // types (common) TYPE(Int16, UINT16, 1) TYPE(Int32, UINT32, 2) diff --git a/modules/ripple_data/protocol/ripple_SerializedObject.cpp b/modules/ripple_data/protocol/ripple_SerializedObject.cpp index 6be78790b0f..f4b7c362c47 100644 --- a/modules/ripple_data/protocol/ripple_SerializedObject.cpp +++ b/modules/ripple_data/protocol/ripple_SerializedObject.cpp @@ -62,6 +62,7 @@ UPTR_T STObject::makeDefaultObject(SerializedTypeID id, SField:: } } +// VFALCO TODO Remove the 'depth' parameter UPTR_T STObject::makeDeserializedObject(SerializedTypeID id, SField::ref name, SerializerIterator& sit, int depth) { @@ -184,7 +185,8 @@ bool STObject::setType(const SOTemplate &type) bool STObject::isValidForType() { boost::ptr_vector::iterator it = mData.begin(); - BOOST_FOREACH(const SOElement* elem, mType->peek()) + + BOOST_FOREACH(const SOElement* elem, mType->peek()) { if (it == mData.end()) return false; @@ -227,7 +229,7 @@ bool STObject::set(SerializerIterator& sit, int depth) */ // return true = terminated with end-of-object -bool STObject::set(SerializerIterator& sit, int depth) +bool STObject::set (SerializerIterator& sit, int depth) { bool reachedEndOfObject = false; diff --git a/modules/ripple_data/protocol/ripple_SerializedObject.h b/modules/ripple_data/protocol/ripple_SerializedObject.h index b687b2d2e68..cd3ce32bc97 100644 --- a/modules/ripple_data/protocol/ripple_SerializedObject.h +++ b/modules/ripple_data/protocol/ripple_SerializedObject.h @@ -38,8 +38,13 @@ class STObject : public SerializedType, private IS_INSTANCE(SerializedObject) virtual bool isDefault() const { return mData.empty(); } virtual void add(Serializer& s) const { add(s, true); } // just inner elements - void add(Serializer& s, bool withSignature) const; + + void add(Serializer& s, bool withSignature) const; + + // VFALCO NOTE does this return an expensive copy of an object with a dynamic buffer? + // VFALCO TODO Remove this function and fix the few callers. Serializer getSerializer() const { Serializer s; add(s); return s; } + std::string getFullText() const; std::string getText() const; virtual Json::Value getJson(int options) const; @@ -116,14 +121,24 @@ class STObject : public SerializedType, private IS_INSTANCE(SerializedObject) bool delField(SField::ref field); void delField(int index); - static UPTR_T makeDefaultObject(SerializedTypeID id, SField::ref name); - static UPTR_T makeDeserializedObject(SerializedTypeID id, SField::ref name, - SerializerIterator&, int depth); + static UPTR_T makeDefaultObject(SerializedTypeID id, SField::ref name); - static UPTR_T makeNonPresentObject(SField::ref name) - { return makeDefaultObject(STI_NOTPRESENT, name); } - static UPTR_T makeDefaultObject(SField::ref name) - { return makeDefaultObject(name.fieldType, name); } + // VFALCO TODO remove the 'depth' parameter + static UPTR_T makeDeserializedObject ( + SerializedTypeID id, + SField::ref name, + SerializerIterator &, + int depth); + + static UPTR_T makeNonPresentObject (SField::ref name) + { + return makeDefaultObject(STI_NOTPRESENT, name); + } + + static UPTR_T makeDefaultObject (SField::ref name) + { + return makeDefaultObject(name.fieldType, name); + } // field iterator stuff typedef boost::ptr_vector::iterator iterator; @@ -148,9 +163,7 @@ class STObject : public SerializedType, private IS_INSTANCE(SerializedObject) { mData.swap(data); } }; -// allow ptr_* collections of STObject's -inline STObject* new_clone(const STObject& s) { return s.oClone().release(); } -inline void delete_clone(const STObject* s) { boost::checked_delete(s); } +//------------------------------------------------------------------------------ inline STObject::iterator range_begin(STObject& x) { return x.begin(); } inline STObject::iterator range_end(STObject &x) { return x.end(); } diff --git a/modules/ripple_data/protocol/ripple_SerializedObjectTemplate.h b/modules/ripple_data/protocol/ripple_SerializedObjectTemplate.h index 23eac3f9355..5b3bab9f418 100644 --- a/modules/ripple_data/protocol/ripple_SerializedObjectTemplate.h +++ b/modules/ripple_data/protocol/ripple_SerializedObjectTemplate.h @@ -50,6 +50,9 @@ class SOTemplate */ SOTemplate (); + // VFALCO NOTE Why do we even bother with the 'private' keyword if + // this function is present? + // std::vector const& peek() const { return mTypes; @@ -64,8 +67,9 @@ class SOTemplate int getIndex (SField::ref) const; private: - std::vector mTypes; - std::vector mIndex; // field num -> index + std::vector mTypes; + + std::vector mIndex; // field num -> index }; #endif diff --git a/newcoin.vcxproj b/newcoin.vcxproj index b56489ad5b7..67d836442c5 100644 --- a/newcoin.vcxproj +++ b/newcoin.vcxproj @@ -1838,6 +1838,7 @@ + diff --git a/newcoin.vcxproj.filters b/newcoin.vcxproj.filters index e9835473e81..9a48bf5bdd4 100644 --- a/newcoin.vcxproj.filters +++ b/newcoin.vcxproj.filters @@ -1606,5 +1606,6 @@ + \ No newline at end of file diff --git a/src/cpp/ripple/LedgerConsensus.h b/src/cpp/ripple/LedgerConsensus.h index 17fb8997355..f21683c4df8 100644 --- a/src/cpp/ripple/LedgerConsensus.h +++ b/src/cpp/ripple/LedgerConsensus.h @@ -8,6 +8,7 @@ DEFINE_INSTANCE(LedgerConsensus); DEFINE_INSTANCE(TransactionAcquire); +// VFALCO TODO rename to PeerTxRequest // A transaction set we are trying to acquire class TransactionAcquire : private IS_INSTANCE (TransactionAcquire) @@ -18,7 +19,7 @@ class TransactionAcquire typedef boost::shared_ptr pointer; public: - TransactionAcquire(uint256 const& hash); + explicit TransactionAcquire(uint256 const& hash); virtual ~TransactionAcquire() { ; } SHAMap::ref getMap() { return mMap; } diff --git a/src/cpp/ripple/LedgerMaster.cpp b/src/cpp/ripple/LedgerMaster.cpp index ecc6b3d0f9e..ad5e22a276d 100644 --- a/src/cpp/ripple/LedgerMaster.cpp +++ b/src/cpp/ripple/LedgerMaster.cpp @@ -132,7 +132,9 @@ Ledger::pointer LedgerMaster::closeLedger(bool recover) } } CondLog (recovers != 0, lsINFO, LedgerMaster) << "Recovered " << recovers << " held transactions"; - mHeldTransactions.reset(closingLedger->getHash()); + + // VFALCO TODO recreate the CanonicalTxSet object instead of resetting it + mHeldTransactions.reset(closingLedger->getHash()); } mCurrentLedger = boost::make_shared(boost::ref(*closingLedger), true); diff --git a/src/cpp/ripple/LedgerMaster.h b/src/cpp/ripple/LedgerMaster.h index 96aa89edaba..b03b360fbd9 100644 --- a/src/cpp/ripple/LedgerMaster.h +++ b/src/cpp/ripple/LedgerMaster.h @@ -14,10 +14,17 @@ class LedgerMaster typedef FUNCTION_TYPE callback; public: - LedgerMaster() : mHeldTransactions(uint256()), mMissingSeq(0), - mMinValidations(0), mLastValidateSeq(0), mPubThread(false), - mPathFindThread(false), mPathFindNewLedger(false), mPathFindNewRequest(false) - { ; } + LedgerMaster () + : mHeldTransactions (uint256()) + , mMissingSeq (0) + , mMinValidations (0) + , mLastValidateSeq (0) + , mPubThread (false) + , mPathFindThread (false) + , mPathFindNewLedger (false) + , mPathFindNewRequest (false) + { + } uint32 getCurrentLedgerIndex(); @@ -133,6 +140,7 @@ class LedgerMaster void pubThread(); void updatePaths(); +private: boost::recursive_mutex mLock; TransactionEngine mEngine; diff --git a/src/cpp/ripple/ripple_AcceptedLedger.h b/src/cpp/ripple/ripple_AcceptedLedger.h index d723bdd5fdb..04a9d760828 100644 --- a/src/cpp/ripple/ripple_AcceptedLedger.h +++ b/src/cpp/ripple/ripple_AcceptedLedger.h @@ -2,7 +2,12 @@ #define ACCEPTED_LEDGER_H /** - + + An accepted ledger is a ledger that has a sufficient number of + validations to convince the local server that it is irrevocable. + + The existence of an accepted ledger implies all preceding ledgers + are accepted. */ class AcceptedLedger { diff --git a/src/cpp/ripple/ripple_AcceptedLedgerTx.h b/src/cpp/ripple/ripple_AcceptedLedgerTx.h index a7a29e5ea9f..a62df63b8e8 100644 --- a/src/cpp/ripple/ripple_AcceptedLedgerTx.h +++ b/src/cpp/ripple/ripple_AcceptedLedgerTx.h @@ -1,12 +1,20 @@ #ifndef RIPPLE_ACCEPTEDLEDGERTX_H #define RIPPLE_ACCEPTEDLEDGERTX_H -/*============================================================================*/ +//------------------------------------------------------------------------------ + /** A transaction that is in a closed ledger. Description + An accepted ledger transaction contains additional information that the + server needs to tell clients about the transaction. For example, + - The transaction in JSON form + - Which accounts are affected + * This is used by InfoSub to report to clients + - Cached stuff + @code @endcode @@ -17,7 +25,7 @@ class AcceptedLedgerTx { public: - typedef boost::shared_ptr pointer; + typedef boost::shared_ptr pointer; typedef const pointer& ref; public: diff --git a/src/cpp/ripple/ripple_CanonicalTXSet.h b/src/cpp/ripple/ripple_CanonicalTXSet.h index 9e89ec7ea33..4b4bcc3710e 100644 --- a/src/cpp/ripple/ripple_CanonicalTXSet.h +++ b/src/cpp/ripple/ripple_CanonicalTXSet.h @@ -1,6 +1,14 @@ #ifndef RIPPLE_CANONICALTXSET_H #define RIPPLE_CANONICALTXSET_H +/** Holds transactions which were deferred to the next pass of consensus. + + "Canonical" refers to the order in which transactions are applied. + + - Puts transactions from the same account in sequence order + +*/ +// VFALCO TODO rename to SortedTxSet class CanonicalTXSet { public: @@ -25,7 +33,8 @@ class CanonicalTXSet uint256 const& getTXID() const { return mTXid; } private: - uint256 mAccount, mTXid; + uint256 mAccount; + uint256 mTXid; uint32 mSeq; }; @@ -33,13 +42,14 @@ class CanonicalTXSet typedef std::map ::const_iterator const_iterator; public: - CanonicalTXSet (LedgerHash const& lastClosedLedgerHash) + explicit CanonicalTXSet (LedgerHash const& lastClosedLedgerHash) : mSetHash (lastClosedLedgerHash) { } void push_back (SerializedTransaction::ref txn); + // VFALCO TODO remove this function void reset (LedgerHash const& newLastClosedLedgerHash) { mSetHash = newLastClosedLedgerHash; @@ -57,6 +67,7 @@ class CanonicalTXSet bool empty() const { return mMap.empty(); } private: + // Used to salt the accounts so people can't mine for low account numbers uint256 mSetHash; std::map mMap; diff --git a/src/cpp/ripple/ripple_LedgerAcquire.h b/src/cpp/ripple/ripple_LedgerAcquire.h index 2755d1200f5..31c5df47688 100644 --- a/src/cpp/ripple/ripple_LedgerAcquire.h +++ b/src/cpp/ripple/ripple_LedgerAcquire.h @@ -3,7 +3,7 @@ DEFINE_INSTANCE(LedgerAcquire); -// VFALCO TODO Rename to IncomingLedger +// VFALCO TODO Rename to InboundLedger // A ledger we are trying to acquire class LedgerAcquire : private IS_INSTANCE(LedgerAcquire) , public PeerSet diff --git a/src/cpp/ripple/ripple_LedgerAcquireMaster.h b/src/cpp/ripple/ripple_LedgerAcquireMaster.h index c20dbfd4d1c..0a21bf460ce 100644 --- a/src/cpp/ripple/ripple_LedgerAcquireMaster.h +++ b/src/cpp/ripple/ripple_LedgerAcquireMaster.h @@ -1,9 +1,9 @@ #ifndef RIPPLE_LEDGERACQUIREMASTER_H #define RIPPLE_LEDGERACQUIREMASTER_H -/** Manages the acquisition of ledgers. +/** Manages the lifetime of inbound ledgers. */ -// VFALCO TODO Rename to IncomingLedgerManager +// VFALCO TODO Rename to InboundLedgerManager // VFALCO TODO Create abstract interface class LedgerAcquireMaster { diff --git a/src/cpp/ripple/ripple_SHAMap.h b/src/cpp/ripple/ripple_SHAMap.h index a12d9c6d89a..5937ce612df 100644 --- a/src/cpp/ripple/ripple_SHAMap.h +++ b/src/cpp/ripple/ripple_SHAMap.h @@ -24,8 +24,8 @@ class SHAMap : public IS_INSTANCE(SHAMap) public: // build new map - SHAMap(SHAMapType t, uint32 seq = 1); - SHAMap(SHAMapType t, uint256 const& hash); + explicit SHAMap (SHAMapType t, uint32 seq = 1); + SHAMap (SHAMapType t, uint256 const& hash); ~SHAMap() { mState = smsInvalid; }