rippled
Loading...
Searching...
No Matches
RippleLineCache.cpp
1#include <xrpld/app/paths/RippleLineCache.h>
2#include <xrpld/app/paths/TrustLine.h>
3
4namespace ripple {
5
9 : ledger_(ledger), journal_(j)
10{
11 JLOG(journal_.debug()) << "created for ledger " << ledger_->info().seq;
12}
13
15{
16 JLOG(journal_.debug()) << "destroyed for ledger " << ledger_->info().seq
17 << " with " << lines_.size() << " accounts and "
18 << totalLineCount_ << " distinct trust lines.";
19}
20
23 AccountID const& accountID,
24 LineDirection direction)
25{
26 auto const hash = hasher_(accountID);
27 AccountKey key(accountID, direction, hash);
28 AccountKey otherkey(
29 accountID,
32 hash);
33
35
36 auto [it, inserted] = [&]() {
37 if (auto otheriter = lines_.find(otherkey); otheriter != lines_.end())
38 {
39 // The whole point of using the direction flag is to reduce the
40 // number of trust line objects held in memory. Ensure that there is
41 // only a single set of trustlines in the cache per account.
42 auto const size = otheriter->second ? otheriter->second->size() : 0;
43 JLOG(journal_.info())
44 << "Request for "
45 << (direction == LineDirection::outgoing ? "outgoing"
46 : "incoming")
47 << " trust lines for account " << accountID << " found " << size
48 << (direction == LineDirection::outgoing ? " incoming"
49 : " outgoing")
50 << " trust lines. "
51 << (direction == LineDirection::outgoing
52 ? "Deleting the subset of incoming"
53 : "Returning the superset of outgoing")
54 << " trust lines. ";
55 if (direction == LineDirection::outgoing)
56 {
57 // This request is for the outgoing set, but there is already a
58 // subset of incoming lines in the cache. Erase that subset
59 // to be replaced by the full set. The full set will be built
60 // below, and will be returned, if needed, on subsequent calls
61 // for either value of outgoing.
62 XRPL_ASSERT(
63 size <= totalLineCount_,
64 "ripple::RippleLineCache::getRippleLines : maximum lines");
65 totalLineCount_ -= size;
66 lines_.erase(otheriter);
67 }
68 else
69 {
70 // This request is for the incoming set, but there is
71 // already a superset of the outgoing trust lines in the cache.
72 // The path finding engine will disregard the non-rippling trust
73 // lines, so to prevent them from being stored twice, return the
74 // outgoing set.
75 key = otherkey;
76 return std::pair{otheriter, false};
77 }
78 }
79 return lines_.emplace(key, nullptr);
80 }();
81
82 if (inserted)
83 {
84 XRPL_ASSERT(
85 it->second == nullptr,
86 "ripple::RippleLineCache::getRippleLines : null lines");
87 auto lines =
88 PathFindTrustLine::getItems(accountID, *ledger_, direction);
89 if (lines.size())
90 {
92 std::move(lines));
93 totalLineCount_ += it->second->size();
94 }
95 }
96
97 XRPL_ASSERT(
98 !it->second || (it->second->size() > 0),
99 "ripple::RippleLineCache::getRippleLines : null or nonempty lines");
100 auto const size = it->second ? it->second->size() : 0;
101 JLOG(journal_.trace()) << "getRippleLines for ledger "
102 << ledger_->info().seq << " found " << size
104 ? " outgoing"
105 : " incoming")
106 << " lines for " << (inserted ? "new " : "existing ")
107 << accountID << " out of a total of "
108 << lines_.size() << " accounts and "
109 << totalLineCount_ << " trust lines";
110
111 return it->second;
112}
113
114} // namespace ripple
A generic endpoint for log messages.
Definition Journal.h:41
Stream debug() const
Definition Journal.h:309
Stream info() const
Definition Journal.h:315
Stream trace() const
Severity stream access functions.
Definition Journal.h:303
static std::vector< PathFindTrustLine > getItems(AccountID const &accountID, ReadView const &view, LineDirection direction)
Definition TrustLine.cpp:70
std::shared_ptr< ReadView const > ledger_
ripple::hardened_hash hasher_
hash_map< AccountKey, std::shared_ptr< std::vector< PathFindTrustLine > >, AccountKey::Hash > lines_
std::shared_ptr< std::vector< PathFindTrustLine > > getRippleLines(AccountID const &accountID, LineDirection direction)
Find the trust lines associated with an account.
RippleLineCache(std::shared_ptr< ReadView const > const &l, beast::Journal j)
static constexpr std::size_t size()
Definition base_uint.h:507
T is_same_v
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6
LineDirection
Describes how an account was found in a path, and how to find the next set of paths.
Definition TrustLine.h:22