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