Skip to content

Commit

Permalink
Rm rate from trust line cache for the path finder:
Browse files Browse the repository at this point in the history
* Rename PathFindTrustLine.{h,cpp} to TrustLine.{h,cpp}
* Rm old comments
* Rm unused include file
* Additional protection agains instantiating TrustLineBase
  • Loading branch information
seelabs committed Feb 11, 2022
1 parent 2510eba commit caaedcf
Show file tree
Hide file tree
Showing 10 changed files with 128 additions and 59 deletions.
2 changes: 1 addition & 1 deletion Builds/CMake/RippledCore.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -394,12 +394,12 @@ target_sources (rippled PRIVATE
src/ripple/app/paths/AccountCurrencies.cpp
src/ripple/app/paths/Credit.cpp
src/ripple/app/paths/Flow.cpp
src/ripple/app/paths/PathFindTrustLine.cpp
src/ripple/app/paths/PathRequest.cpp
src/ripple/app/paths/PathRequests.cpp
src/ripple/app/paths/Pathfinder.cpp
src/ripple/app/paths/RippleCalc.cpp
src/ripple/app/paths/RippleLineCache.cpp
src/ripple/app/paths/TrustLine.cpp
src/ripple/app/paths/impl/BookStep.cpp
src/ripple/app/paths/impl/DirectStep.cpp
src/ripple/app/paths/impl/PaySteps.cpp
Expand Down
3 changes: 2 additions & 1 deletion src/ripple/app/paths/RippleLineCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#include <ripple/app/paths/RippleLineCache.h>
#include <ripple/ledger/OpenView.h>
#include "app/paths/TrustLine.h"

namespace ripple {

Expand All @@ -40,7 +41,7 @@ RippleLineCache::getRippleLines(AccountID const& accountID)
auto [it, inserted] = lines_.emplace(key, std::vector<PathFindTrustLine>());

if (inserted)
it->second = getRippleStateItems(accountID, *mLedger);
it->second = PathFindTrustLine::getItems(accountID, *mLedger);

return it->second;
}
Expand Down
2 changes: 1 addition & 1 deletion src/ripple/app/paths/RippleLineCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#define RIPPLE_APP_PATHS_RIPPLELINECACHE_H_INCLUDED

#include <ripple/app/ledger/Ledger.h>
#include <ripple/app/paths/PathFindTrustLine.h>
#include <ripple/app/paths/TrustLine.h>
#include <ripple/basics/CountedObject.h>
#include <ripple/basics/hardened_hash.h>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,65 +18,96 @@
//==============================================================================

#include <ripple/app/main/Application.h>
#include <ripple/app/paths/PathFindTrustLine.h>
#include <ripple/app/paths/TrustLine.h>
#include <ripple/protocol/STAmount.h>
#include <cstdint>
#include <memory>

namespace ripple {

PathFindTrustLine::PathFindTrustLine(
TrustLineBase::TrustLineBase(
std::shared_ptr<SLE const> const& sle,
AccountID const& viewAccount)
: key_(sle->key())
, mLowLimit(sle->getFieldAmount(sfLowLimit))
, mHighLimit(sle->getFieldAmount(sfHighLimit))
, mBalance(sle->getFieldAmount(sfBalance))
, lowQualityIn_(sle->getFieldU32(sfLowQualityIn))
, lowQualityOut_(sle->getFieldU32(sfLowQualityOut))
, highQualityIn_(sle->getFieldU32(sfHighQualityIn))
, highQualityOut_(sle->getFieldU32(sfHighQualityOut))
, mFlags(sle->getFieldU32(sfFlags))
, mViewLowest(mLowLimit.getIssuer() == viewAccount)
{
if (!mViewLowest)
mBalance.negate();
}

Json::Value
TrustLineBase::getJson(int)
{
Json::Value ret(Json::objectValue);
ret["low_id"] = to_string(mLowLimit.getIssuer());
ret["high_id"] = to_string(mHighLimit.getIssuer());
return ret;
}

std::optional<PathFindTrustLine>
PathFindTrustLine::makeItem(
AccountID const& accountID,
std::shared_ptr<SLE const> const& sle)
{
// VFALCO Does this ever happen in practice?
if (!sle || sle->getType() != ltRIPPLE_STATE)
return {};
return std::optional{PathFindTrustLine{sle, accountID}};
}

Json::Value
PathFindTrustLine::getJson(int)
namespace detail {
template <class T>
std::vector<T>
getTrustLineItems(AccountID const& accountID, ReadView const& view)
{
Json::Value ret(Json::objectValue);
ret["low_id"] = to_string(mLowLimit.getIssuer());
ret["high_id"] = to_string(mHighLimit.getIssuer());
return ret;
}

std::vector<PathFindTrustLine>
getRippleStateItems(AccountID const& accountID, ReadView const& view)
{
std::vector<PathFindTrustLine> items;
std::vector<T> items;
forEachItem(
view,
accountID,
[&items, &accountID](std::shared_ptr<SLE const> const& sleCur) {
auto ret = PathFindTrustLine::makeItem(accountID, sleCur);
auto ret = T::makeItem(accountID, sleCur);
if (ret)
items.push_back(std::move(*ret));
});

return items;
}
} // namespace detail

std::vector<PathFindTrustLine>
PathFindTrustLine::getItems(AccountID const& accountID, ReadView const& view)
{
return detail::getTrustLineItems<PathFindTrustLine>(accountID, view);
}

RPCTrustLine::RPCTrustLine(
std::shared_ptr<SLE const> const& sle,
AccountID const& viewAccount)
: TrustLineBase(sle, viewAccount)
, lowQualityIn_(sle->getFieldU32(sfLowQualityIn))
, lowQualityOut_(sle->getFieldU32(sfLowQualityOut))
, highQualityIn_(sle->getFieldU32(sfHighQualityIn))
, highQualityOut_(sle->getFieldU32(sfHighQualityOut))
{
}

std::optional<RPCTrustLine>
RPCTrustLine::makeItem(
AccountID const& accountID,
std::shared_ptr<SLE const> const& sle)
{
if (!sle || sle->getType() != ltRIPPLE_STATE)
return {};
return std::optional{RPCTrustLine{sle, accountID}};
}

std::vector<RPCTrustLine>
RPCTrustLine::getItems(AccountID const& accountID, ReadView const& view)
{
return detail::getTrustLineItems<RPCTrustLine>(accountID, view);
}

} // namespace ripple
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
#include <ripple/protocol/STLedgerEntry.h>

#include <cstdint>
#include <memory>
#include <optional>

namespace ripple {
Expand All @@ -42,19 +41,22 @@ namespace ripple {
tens of millions of instances of this class. When modifying this class think
carefully about the memory implications.
*/
class PathFindTrustLine final : public CountedObject<PathFindTrustLine>
class TrustLineBase
{
public:
PathFindTrustLine() = delete;

static std::optional<PathFindTrustLine>
makeItem(AccountID const& accountID, std::shared_ptr<SLE const> const& sle);

// Must be public, for make_shared
PathFindTrustLine(
protected:
// This class should not be instantiated directly. Use one of the derived
// classes.
TrustLineBase(
std::shared_ptr<SLE const> const& sle,
AccountID const& viewAccount);

~TrustLineBase() = default;
TrustLineBase(TrustLineBase const&) = default;
TrustLineBase&
operator=(TrustLineBase const&) = delete;
TrustLineBase(TrustLineBase&&) = default;

public:
/** Returns the state map key for the ledger entry. */
uint256 const&
key() const
Expand Down Expand Up @@ -139,6 +141,52 @@ class PathFindTrustLine final : public CountedObject<PathFindTrustLine>
return !mViewLowest ? mLowLimit : mHighLimit;
}

Json::Value
getJson(int);

protected:
uint256 key_;

STAmount const mLowLimit;
STAmount const mHighLimit;

STAmount mBalance;

std::uint32_t mFlags;

bool mViewLowest;
};

// This wrapper is used for the path finder
class PathFindTrustLine final : public TrustLineBase,
public CountedObject<PathFindTrustLine>
{
using TrustLineBase::TrustLineBase;

public:
PathFindTrustLine() = delete;

static std::optional<PathFindTrustLine>
makeItem(AccountID const& accountID, std::shared_ptr<SLE const> const& sle);

static std::vector<PathFindTrustLine>
getItems(AccountID const& accountID, ReadView const& view);
};

// This wrapper is used for the `AccountLines` command and includes the quality
// in and quality out values.
class RPCTrustLine final : public TrustLineBase,
public CountedObject<RPCTrustLine>
{
using TrustLineBase::TrustLineBase;

public:
RPCTrustLine() = delete;

RPCTrustLine(
std::shared_ptr<SLE const> const& sle,
AccountID const& viewAccount);

Rate const&
getQualityIn() const
{
Expand All @@ -151,30 +199,19 @@ class PathFindTrustLine final : public CountedObject<PathFindTrustLine>
return mViewLowest ? lowQualityOut_ : highQualityOut_;
}

Json::Value
getJson(int);

private:
uint256 key_;

STAmount const mLowLimit;
STAmount const mHighLimit;
static std::optional<RPCTrustLine>
makeItem(AccountID const& accountID, std::shared_ptr<SLE const> const& sle);

STAmount mBalance;
static std::vector<RPCTrustLine>
getItems(AccountID const& accountID, ReadView const& view);

private:
Rate lowQualityIn_;
Rate lowQualityOut_;
Rate highQualityIn_;
Rate highQualityOut_;

std::uint32_t mFlags;

bool mViewLowest;
};

std::vector<PathFindTrustLine>
getRippleStateItems(AccountID const& accountID, ReadView const& view);

} // namespace ripple

#endif
4 changes: 2 additions & 2 deletions src/ripple/rpc/handlers/AccountCurrenciesHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
//==============================================================================

#include <ripple/app/main/Application.h>
#include <ripple/app/paths/PathFindTrustLine.h>
#include <ripple/app/paths/TrustLine.h>
#include <ripple/ledger/ReadView.h>
#include <ripple/net/RPCErr.h>
#include <ripple/protocol/ErrorCodes.h>
Expand Down Expand Up @@ -58,7 +58,7 @@ doAccountCurrencies(RPC::JsonContext& context)
return rpcError(rpcACT_NOT_FOUND);

std::set<Currency> send, receive;
for (auto const& rspEntry : getRippleStateItems(accountID, *ledger))
for (auto const& rspEntry : RPCTrustLine::getItems(accountID, *ledger))
{
STAmount const& saBalance = rspEntry.getBalance();

Expand Down
10 changes: 5 additions & 5 deletions src/ripple/rpc/handlers/AccountLines.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
//==============================================================================

#include <ripple/app/main/Application.h>
#include <ripple/app/paths/PathFindTrustLine.h>
#include <ripple/app/paths/TrustLine.h>
#include <ripple/ledger/ReadView.h>
#include <ripple/net/RPCErr.h>
#include <ripple/protocol/ErrorCodes.h>
Expand All @@ -32,7 +32,7 @@ namespace ripple {

struct VisitData
{
std::vector<PathFindTrustLine> items;
std::vector<RPCTrustLine> items;
AccountID const& accountID;
bool hasPeer;
AccountID const& raPeerAccount;
Expand All @@ -42,7 +42,7 @@ struct VisitData
};

void
addLine(Json::Value& jsonLines, PathFindTrustLine const& line)
addLine(Json::Value& jsonLines, RPCTrustLine const& line)
{
STAmount const& saBalance(line.getBalance());
STAmount const& saLimit(line.getLimit());
Expand Down Expand Up @@ -223,8 +223,8 @@ doAccountLines(RPC::JsonContext& context)

if (!ignore && count <= limit)
{
auto const line = PathFindTrustLine::makeItem(
visitData.accountID, sleCur);
auto const line =
RPCTrustLine::makeItem(visitData.accountID, sleCur);

if (line &&
(!visitData.hasPeer ||
Expand Down
2 changes: 1 addition & 1 deletion src/ripple/rpc/handlers/GatewayBalances.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
//==============================================================================

#include <ripple/app/main/Application.h>
#include <ripple/app/paths/PathFindTrustLine.h>
#include <ripple/app/paths/TrustLine.h>
#include <ripple/ledger/ReadView.h>
#include <ripple/protocol/AccountID.h>
#include <ripple/protocol/ErrorCodes.h>
Expand Down
2 changes: 1 addition & 1 deletion src/ripple/rpc/handlers/NoRippleCheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

#include <ripple/app/main/Application.h>
#include <ripple/app/misc/LoadFeeTrack.h>
#include <ripple/app/paths/PathFindTrustLine.h>
#include <ripple/app/paths/TrustLine.h>
#include <ripple/ledger/ReadView.h>
#include <ripple/net/RPCErr.h>
#include <ripple/protocol/ErrorCodes.h>
Expand Down
2 changes: 1 addition & 1 deletion src/ripple/rpc/impl/RPCHelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#include <ripple/app/ledger/LedgerMaster.h>
#include <ripple/app/ledger/OpenLedger.h>
#include <ripple/app/misc/Transaction.h>
#include <ripple/app/paths/PathFindTrustLine.h>
#include <ripple/app/paths/TrustLine.h>
#include <ripple/app/rdb/RelationalDBInterface.h>
#include <ripple/ledger/View.h>
#include <ripple/net/RPCErr.h>
Expand Down

0 comments on commit caaedcf

Please sign in to comment.