rippled
Loading...
Searching...
No Matches
RippleLineCache.cpp
1#include <xrpld/app/paths/RippleLineCache.h>
2#include <xrpld/app/paths/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 " << lines_.size()
15 << " accounts and " << totalLineCount_ << " distinct trust lines.";
16}
17
20{
21 auto const hash = hasher_(accountID);
22 AccountKey key(accountID, direction, hash);
23 AccountKey otherkey(
25
27
28 auto [it, inserted] = [&]() {
29 if (auto otheriter = lines_.find(otherkey); otheriter != lines_.end())
30 {
31 // The whole point of using the direction flag is to reduce the
32 // number of trust line objects held in memory. Ensure that there is
33 // only a single set of trustlines in the cache per account.
34 auto const size = otheriter->second ? otheriter->second->size() : 0;
35 JLOG(journal_.info()) << "Request for " << (direction == LineDirection::outgoing ? "outgoing" : "incoming")
36 << " trust lines for account " << accountID << " found " << size
37 << (direction == LineDirection::outgoing ? " incoming" : " outgoing")
38 << " trust lines. "
39 << (direction == LineDirection::outgoing ? "Deleting the subset of incoming"
40 : "Returning the superset of outgoing")
41 << " trust lines. ";
42 if (direction == LineDirection::outgoing)
43 {
44 // This request is for the outgoing set, but there is already a
45 // subset of incoming lines in the cache. Erase that subset
46 // to be replaced by the full set. The full set will be built
47 // below, and will be returned, if needed, on subsequent calls
48 // for either value of outgoing.
49 XRPL_ASSERT(size <= totalLineCount_, "xrpl::RippleLineCache::getRippleLines : maximum lines");
50 totalLineCount_ -= size;
51 lines_.erase(otheriter);
52 }
53 else
54 {
55 // This request is for the incoming set, but there is
56 // already a superset of the outgoing trust lines in the cache.
57 // The path finding engine will disregard the non-rippling trust
58 // lines, so to prevent them from being stored twice, return the
59 // outgoing set.
60 key = otherkey;
61 return std::pair{otheriter, false};
62 }
63 }
64 return lines_.emplace(key, nullptr);
65 }();
66
67 if (inserted)
68 {
69 XRPL_ASSERT(it->second == nullptr, "xrpl::RippleLineCache::getRippleLines : null lines");
70 auto lines = PathFindTrustLine::getItems(accountID, *ledger_, direction);
71 if (lines.size())
72 {
73 it->second = std::make_shared<std::vector<PathFindTrustLine>>(std::move(lines));
74 totalLineCount_ += it->second->size();
75 }
76 }
77
78 XRPL_ASSERT(
79 !it->second || (it->second->size() > 0), "xrpl::RippleLineCache::getRippleLines : null or nonempty lines");
80 auto const size = it->second ? it->second->size() : 0;
81 JLOG(journal_.trace()) << "getRippleLines for ledger " << ledger_->header().seq << " found " << size
82 << (key.direction_ == LineDirection::outgoing ? " outgoing" : " incoming") << " lines for "
83 << (inserted ? "new " : "existing ") << accountID << " out of a total of " << lines_.size()
84 << " accounts and " << totalLineCount_ << " trust lines";
85
86 return it->second;
87}
88
89} // namespace xrpl
A generic endpoint for log messages.
Definition Journal.h:40
Stream debug() const
Definition Journal.h:300
Stream info() const
Definition Journal.h:306
Stream trace() const
Severity stream access functions.
Definition Journal.h:294
static std::vector< PathFindTrustLine > getItems(AccountID const &accountID, ReadView const &view, LineDirection direction)
Definition TrustLine.cpp:58
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:494
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