From 9a988963e9198d9517984f75c6e48fd98b6cf6f5 Mon Sep 17 00:00:00 2001 From: Nik Bougalis Date: Sun, 4 Sep 2016 01:16:17 -0700 Subject: [PATCH] Refactor STObject, STLedgerEntry: * Normalize names * Remove unused and deprecated members --- src/ripple/app/ledger/Ledger.cpp | 6 +- src/ripple/app/ledger/OrderBookDB.cpp | 2 +- src/ripple/app/misc/NetworkOPs.cpp | 2 +- src/ripple/app/paths/RippleState.h | 2 +- src/ripple/app/tx/impl/CreateTicket.cpp | 4 +- src/ripple/app/tx/impl/Offer.h | 2 +- src/ripple/app/tx/impl/OfferStream.cpp | 8 +-- src/ripple/ledger/impl/TxMeta.cpp | 2 +- src/ripple/ledger/impl/View.cpp | 10 +-- src/ripple/protocol/STLedgerEntry.h | 44 +++--------- src/ripple/protocol/STObject.h | 6 -- src/ripple/protocol/impl/STLedgerEntry.cpp | 77 ++++++++++----------- src/ripple/protocol/impl/STObject.cpp | 47 ------------- src/ripple/rpc/handlers/AccountChannels.cpp | 2 +- src/ripple/rpc/handlers/AccountOffers.cpp | 2 +- 15 files changed, 62 insertions(+), 154 deletions(-) diff --git a/src/ripple/app/ledger/Ledger.cpp b/src/ripple/app/ledger/Ledger.cpp index 357a943f9..aec258d18 100644 --- a/src/ripple/app/ledger/Ledger.cpp +++ b/src/ripple/app/ledger/Ledger.cpp @@ -346,7 +346,7 @@ Ledger::setAccepted(NetClock::time_point closeTime, bool Ledger::addSLE (SLE const& sle) { - SHAMapItem item (sle.getIndex(), sle.getSerializer()); + SHAMapItem item (sle.key(), sle.getSerializer()); return stateMap_->addItem(std::move(item), false, false); } @@ -421,8 +421,6 @@ Ledger::read (Keylet const& k) const item->size()}, item->key()); if (! k.check(*sle)) return nullptr; - // VFALCO TODO Eliminate "immutable" runtime property - sle->setImmutable(); // need move otherwise makes a copy // because return type is different return std::move(sle); @@ -632,8 +630,6 @@ Ledger::peek (Keylet const& k) const SerialIter{value->data(), value->size()}, value->key()); if (! k.check(*sle)) return nullptr; - // VFALCO TODO Eliminate "immutable" runtime property - sle->setImmutable(); return sle; } diff --git a/src/ripple/app/ledger/OrderBookDB.cpp b/src/ripple/app/ledger/OrderBookDB.cpp index fe01ed84f..6ec52fce5 100644 --- a/src/ripple/app/ledger/OrderBookDB.cpp +++ b/src/ripple/app/ledger/OrderBookDB.cpp @@ -103,7 +103,7 @@ void OrderBookDB::update( { if (sle->getType () == ltDIR_NODE && sle->isFieldPresent (sfExchangeRate) && - sle->getFieldH256 (sfRootIndex) == sle->getIndex()) + sle->getFieldH256 (sfRootIndex) == sle->key()) { Book book; book.in.currency.copyFrom(sle->getFieldH160( diff --git a/src/ripple/app/misc/NetworkOPs.cpp b/src/ripple/app/misc/NetworkOPs.cpp index 2447f5503..07d4290a9 100644 --- a/src/ripple/app/misc/NetworkOPs.cpp +++ b/src/ripple/app/misc/NetworkOPs.cpp @@ -2901,7 +2901,7 @@ void NetworkOPsImp::getBookPage ( } else { - uTipIndex = sleOfferDir->getIndex (); + uTipIndex = sleOfferDir->key(); saDirRate = amountFromQuality (getQuality (uTipIndex)); cdirFirst (view, diff --git a/src/ripple/app/paths/RippleState.h b/src/ripple/app/paths/RippleState.h index 116648540..9ade7e48a 100644 --- a/src/ripple/app/paths/RippleState.h +++ b/src/ripple/app/paths/RippleState.h @@ -59,7 +59,7 @@ public: uint256 key() const { - return sle_->getIndex(); + return sle_->key(); } // VFALCO Take off the "get" from each function name diff --git a/src/ripple/app/tx/impl/CreateTicket.cpp b/src/ripple/app/tx/impl/CreateTicket.cpp index 118b5d740..e0dfdf7fe 100644 --- a/src/ripple/app/tx/impl/CreateTicket.cpp +++ b/src/ripple/app/tx/impl/CreateTicket.cpp @@ -105,10 +105,10 @@ CreateTicket::doApply () auto viewJ = ctx_.app.journal ("View"); auto result = dirAdd(view(), hint, keylet::ownerDir (account_), - sleTicket->getIndex (), describeOwnerDir (account_), viewJ); + sleTicket->key(), describeOwnerDir (account_), viewJ); JLOG(j_.trace()) << - "Creating ticket " << to_string (sleTicket->getIndex ()) << + "Creating ticket " << to_string (sleTicket->key()) << ": " << transHuman (result.first); if (result.first == tesSUCCESS) diff --git a/src/ripple/app/tx/impl/Offer.h b/src/ripple/app/tx/impl/Offer.h index 4fb068253..2b1a81304 100644 --- a/src/ripple/app/tx/impl/Offer.h +++ b/src/ripple/app/tx/impl/Offer.h @@ -120,7 +120,7 @@ public: std::string id () const { - return to_string (m_entry->getIndex()); + return to_string (m_entry->key()); } Issue issueIn () const; diff --git a/src/ripple/app/tx/impl/OfferStream.cpp b/src/ripple/app/tx/impl/OfferStream.cpp index 59924b765..ae9a64bc5 100644 --- a/src/ripple/app/tx/impl/OfferStream.cpp +++ b/src/ripple/app/tx/impl/OfferStream.cpp @@ -152,7 +152,7 @@ TOfferStreamBase::step () tp{d{(*entry)[sfExpiration]}} <= expire_) { JLOG(j_.trace()) << - "Removing expired offer " << entry->getIndex(); + "Removing expired offer " << entry->key(); permRmOffer (entry); continue; } @@ -165,7 +165,7 @@ TOfferStreamBase::step () if (amount.empty()) { JLOG(j_.warn()) << - "Removing bad offer " << entry->getIndex(); + "Removing bad offer " << entry->key(); permRmOffer (entry); offer_ = TOffer{}; continue; @@ -189,12 +189,12 @@ TOfferStreamBase::step () { permRmOffer (entry); JLOG(j_.trace()) << - "Removing unfunded offer " << entry->getIndex(); + "Removing unfunded offer " << entry->key(); } else { JLOG(j_.trace()) << - "Removing became unfunded offer " << entry->getIndex(); + "Removing became unfunded offer " << entry->key(); } offer_ = TOffer{}; continue; diff --git a/src/ripple/ledger/impl/TxMeta.cpp b/src/ripple/ledger/impl/TxMeta.cpp index 1a5601863..9f47c7b13 100644 --- a/src/ripple/ledger/impl/TxMeta.cpp +++ b/src/ripple/ledger/impl/TxMeta.cpp @@ -170,7 +170,7 @@ TxMeta::getAffectedAccounts() const STObject& TxMeta::getAffectedNode (SLE::ref node, SField const& type) { - uint256 index = node->getIndex (); + uint256 index = node->key(); for (auto& n : mNodes) { if (n.getFieldH256 (sfLedgerIndex) == index) diff --git a/src/ripple/ledger/impl/View.cpp b/src/ripple/ledger/impl/View.cpp index e14760287..0bc7dc686 100644 --- a/src/ripple/ledger/impl/View.cpp +++ b/src/ripple/ledger/impl/View.cpp @@ -1061,14 +1061,14 @@ trustCreate (ApplyView& view, std::tie (terResult, std::ignore) = dirAdd (view, uLowNode, keylet::ownerDir (uLowAccountID), - sleRippleState->getIndex (), + sleRippleState->key(), describeOwnerDir (uLowAccountID), j); if (tesSUCCESS == terResult) { std::tie (terResult, std::ignore) = dirAdd (view, uHighNode, keylet::ownerDir (uHighAccountID), - sleRippleState->getIndex (), + sleRippleState->key(), describeOwnerDir (uHighAccountID), j); } @@ -1156,7 +1156,7 @@ trustDelete (ApplyView& view, false, uLowNode, getOwnerDirIndex (uLowAccountID), - sleRippleState->getIndex (), + sleRippleState->key(), false, !bLowNode, j); @@ -1169,7 +1169,7 @@ trustDelete (ApplyView& view, false, uHighNode, getOwnerDirIndex (uHighAccountID), - sleRippleState->getIndex (), + sleRippleState->key(), false, !bHighNode, j); @@ -1188,7 +1188,7 @@ offerDelete (ApplyView& view, { if (! sle) return tesSUCCESS; - auto offerIndex = sle->getIndex (); + auto offerIndex = sle->key(); auto owner = sle->getAccountID (sfAccount); // Detect legacy directories. diff --git a/src/ripple/protocol/STLedgerEntry.h b/src/ripple/protocol/STLedgerEntry.h index e79af5476..24a85f407 100644 --- a/src/ripple/protocol/STLedgerEntry.h +++ b/src/ripple/protocol/STLedgerEntry.h @@ -45,14 +45,11 @@ public: { } - STLedgerEntry (const Serializer & s, uint256 const& index); - STLedgerEntry (SerialIter & sit, uint256 const& index); STLedgerEntry(SerialIter&& sit, uint256 const& index) : STLedgerEntry(sit, index) {} - - STLedgerEntry (const STObject & object, uint256 const& index); + STLedgerEntry (STObject const& object, uint256 const& index); STBase* copy (std::size_t n, void* buf) const override @@ -87,42 +84,19 @@ public: return key_; } - // DEPRECATED - uint256 const& getIndex () const - { - return key_; - } - - void setImmutable () - { - mMutable = false; - } - - bool isMutable () - { - return mMutable; - } - LedgerEntryType getType () const { return type_; } - std::uint16_t getVersion () const - { - return getFieldU16 (sfLedgerEntryType); - } + // is this a ledger entry that can be threaded + bool isThreadedType() const; - bool isThreadedType() const; // is this a ledger entry that can be threaded - - bool isThreaded () const; // is this ledger entry actually threaded - - uint256 getThreadedTransaction () const; - - std::uint32_t getThreadedLedger () const; - - bool thread (uint256 const& txID, std::uint32_t ledgerSeq, uint256 & prevTxID, - std::uint32_t & prevLedgerID); + bool thread ( + uint256 const& txID, + std::uint32_t ledgerSeq, + uint256 & prevTxID, + std::uint32_t & prevLedgerID); private: /* Make STObject comply with the template for this SLE type @@ -133,8 +107,6 @@ private: private: uint256 key_; LedgerEntryType type_; - LedgerFormats::Item const* mFormat; - bool mMutable; }; using SLE = STLedgerEntry; diff --git a/src/ripple/protocol/STObject.h b/src/ripple/protocol/STObject.h index a2a0cff09..993129215 100644 --- a/src/ripple/protocol/STObject.h +++ b/src/ripple/protocol/STObject.h @@ -330,8 +330,6 @@ public: ResultOfSetTypeFromSField setTypeFromSField (SField const&); - bool isValidForType (); - bool isFieldAllowed (SField const&); bool isFree () const { return mType == nullptr; @@ -424,7 +422,6 @@ public: // these throw if the field type doesn't match, or return default values // if the field is optional but not present - std::string getFieldString (SField const& field) const; unsigned char getFieldU8 (SField const& field) const; std::uint16_t getFieldU16 (SField const& field) const; std::uint32_t getFieldU32 (SField const& field) const; @@ -440,7 +437,6 @@ public: STPathSet const& getFieldPathSet (SField const& field) const; const STVector256& getFieldV256 (SField const& field) const; const STArray& getFieldArray (SField const& field) const; - const STObject& getFieldObject (SField const& field) const; /** Return the value of a field. @@ -499,10 +495,8 @@ public: void setAccountID (SField const& field, AccountID const&); void setFieldAmount (SField const& field, STAmount const&); - void setFieldPathSet (SField const& field, STPathSet const&); void setFieldV256 (SField const& field, STVector256 const& v); void setFieldArray (SField const& field, STArray const& v); - void setFieldObject (SField const& field, STObject const& v); template void setFieldH160 (SField const& field, base_uint<160, Tag> const& v) diff --git a/src/ripple/protocol/impl/STLedgerEntry.cpp b/src/ripple/protocol/impl/STLedgerEntry.cpp index 928322561..9e98b8ba6 100644 --- a/src/ripple/protocol/impl/STLedgerEntry.cpp +++ b/src/ripple/protocol/impl/STLedgerEntry.cpp @@ -32,55 +32,54 @@ STLedgerEntry::STLedgerEntry (Keylet const& k) : STObject(sfLedgerEntry) , key_ (k.key) , type_ (k.type) - , mMutable (true) { - mFormat = + auto const format = LedgerFormats::getInstance().findByType (type_); - if (mFormat == nullptr) + + if (format == nullptr) Throw ("invalid ledger entry type"); - set (mFormat->elements); + + set (format->elements); + setFieldU16 (sfLedgerEntryType, - static_cast (mFormat->getType ())); + static_cast (type_)); } STLedgerEntry::STLedgerEntry ( - SerialIter& sit, uint256 const& index) - : STObject (sfLedgerEntry), key_ (index), mMutable (true) + SerialIter& sit, + uint256 const& index) + : STObject (sfLedgerEntry) + , key_ (index) { set (sit); setSLEType (); } STLedgerEntry::STLedgerEntry ( - const Serializer& s, uint256 const& index) - : STObject (sfLedgerEntry), key_ (index), mMutable (true) -{ - SerialIter sit (s.slice()); - set (sit); - setSLEType (); -} - -STLedgerEntry::STLedgerEntry ( - const STObject & object, uint256 const& index) - : STObject (object), key_(index), mMutable (true) + STObject const& object, + uint256 const& index) + : STObject (object) + , key_ (index) { setSLEType (); } void STLedgerEntry::setSLEType () { - mFormat = LedgerFormats::getInstance().findByType ( - static_cast (getFieldU16 (sfLedgerEntryType))); + auto format = LedgerFormats::getInstance().findByType ( + static_cast ( + getFieldU16 (sfLedgerEntryType))); - if (mFormat == nullptr) + if (format == nullptr) Throw ("invalid ledger entry type"); - type_ = mFormat->getType (); - if (!setType (mFormat->elements)) + type_ = format->getType (); + + if (!setType (format->elements)) { if (auto j = debugLog().error()) { - j << "Ledger entry not valid for type " << mFormat->getName (); + j << "Ledger entry not valid for type " << format->getName (); j << "Object: " << getJson (0); } @@ -90,10 +89,16 @@ void STLedgerEntry::setSLEType () std::string STLedgerEntry::getFullText () const { + auto const format = + LedgerFormats::getInstance().findByType (type_); + + if (format == nullptr) + Throw ("invalid ledger entry type"); + std::string ret = "\""; ret += to_string (key_); ret += "\" = { "; - ret += mFormat->getName (); + ret += format->getName (); ret += ", "; ret += STObject::getFullText (); ret += "}"; @@ -121,23 +126,11 @@ bool STLedgerEntry::isThreadedType () const return getFieldIndex (sfPreviousTxnID) != -1; } -bool STLedgerEntry::isThreaded () const -{ - return isFieldPresent (sfPreviousTxnID); -} - -uint256 STLedgerEntry::getThreadedTransaction () const -{ - return getFieldH256 (sfPreviousTxnID); -} - -std::uint32_t STLedgerEntry::getThreadedLedger () const -{ - return getFieldU32 (sfPreviousTxnLgrSeq); -} - -bool STLedgerEntry::thread (uint256 const& txID, std::uint32_t ledgerSeq, - uint256& prevTxID, std::uint32_t& prevLedgerID) +bool STLedgerEntry::thread ( + uint256 const& txID, + std::uint32_t ledgerSeq, + uint256& prevTxID, + std::uint32_t& prevLedgerID) { uint256 oldPrevTxID = getFieldH256 (sfPreviousTxnID); diff --git a/src/ripple/protocol/impl/STObject.cpp b/src/ripple/protocol/impl/STObject.cpp index 482acfb98..59d4c0a56 100644 --- a/src/ripple/protocol/impl/STObject.cpp +++ b/src/ripple/protocol/impl/STObject.cpp @@ -164,28 +164,6 @@ STObject::setTypeFromSField (SField const& sField) return ret; } -bool STObject::isValidForType () -{ - auto it = v_.begin(); - for (auto const& elem : mType->all()) - { - if (it == v_.end()) - return false; - if (elem->e_field != it->get().getFName()) - return false; - ++it; - } - return true; -} - -bool STObject::isFieldAllowed (SField const& field) -{ - if (mType == nullptr) - return true; - - return mType->getIndex (field) != -1; -} - // return true = terminated with end-of-object bool STObject::set (SerialIter& sit, int depth) { @@ -504,15 +482,6 @@ void STObject::delField (int index) v_.erase (v_.begin () + index); } -std::string STObject::getFieldString (SField const& field) const -{ - const STBase* rf = peekAtPField (field); - - if (! rf) Throw ("Field not found"); - - return rf->getText (); -} - unsigned char STObject::getFieldU8 (SField const& field) const { return getFieldByValue (field); @@ -584,12 +553,6 @@ const STArray& STObject::getFieldArray (SField const& field) const return getFieldByConstRef (field, empty); } -const STObject& STObject::getFieldObject (SField const& field) const -{ - static STObject const empty{sfInvalid}; - return getFieldByConstRef (field, empty); -} - void STObject::set (std::unique_ptr v) { @@ -664,21 +627,11 @@ void STObject::setFieldAmount (SField const& field, STAmount const& v) setFieldUsingAssignment (field, v); } -void STObject::setFieldPathSet (SField const& field, STPathSet const& v) -{ - setFieldUsingAssignment (field, v); -} - void STObject::setFieldArray (SField const& field, STArray const& v) { setFieldUsingAssignment (field, v); } -void STObject::setFieldObject (SField const& field, STObject const& v) -{ - setFieldUsingAssignment (field, v); -} - Json::Value STObject::getJson (int options) const { Json::Value ret (Json::objectValue); diff --git a/src/ripple/rpc/handlers/AccountChannels.cpp b/src/ripple/rpc/handlers/AccountChannels.cpp index 6a82aa00f..251b9d29f 100644 --- a/src/ripple/rpc/handlers/AccountChannels.cpp +++ b/src/ripple/rpc/handlers/AccountChannels.cpp @@ -167,7 +167,7 @@ Json::Value doAccountChannels (RPC::Context& context) { result[jss::limit] = limit; - result[jss::marker] = to_string (visitData.items.back()->getIndex()); + result[jss::marker] = to_string (visitData.items.back()->key()); visitData.items.pop_back (); } diff --git a/src/ripple/rpc/handlers/AccountOffers.cpp b/src/ripple/rpc/handlers/AccountOffers.cpp index e3cb8548e..8ff79805e 100644 --- a/src/ripple/rpc/handlers/AccountOffers.cpp +++ b/src/ripple/rpc/handlers/AccountOffers.cpp @@ -141,7 +141,7 @@ Json::Value doAccountOffers (RPC::Context& context) { result[jss::limit] = limit; - result[jss::marker] = to_string (offers.back ()->getIndex ()); + result[jss::marker] = to_string (offers.back ()->key ()); offers.pop_back (); }