xrpld
Loading...
Searching...
No Matches
LedgerPersistence.cpp
1#include <xrpld/app/ledger/LedgerPersistence.h>
2
3#include <xrpld/app/ledger/LedgerMaster.h>
4
5#include <xrpl/basics/Log.h>
6#include <xrpl/basics/base_uint.h>
7#include <xrpl/beast/utility/instrumentation.h>
8#include <xrpl/core/HashRouter.h>
9#include <xrpl/core/Job.h>
10#include <xrpl/core/JobQueue.h>
11#include <xrpl/core/ServiceRegistry.h>
12#include <xrpl/ledger/PendingSaves.h>
13#include <xrpl/ledger/entries/FeeSettingsEntry.h>
14#include <xrpl/protocol/Indexes.h> // IWYU pragma: keep
15#include <xrpl/protocol/Rules.h>
16#include <xrpl/protocol/SystemParameters.h> // IWYU pragma: keep
17#include <xrpl/rdb/RelationalDatabase.h>
18
19#include <cstdint>
20#include <memory>
21#include <optional>
22#include <string>
23#include <tuple>
24
25namespace xrpl {
26
43static bool
45 ServiceRegistry& registry,
47 bool current)
48{
49 auto const j = registry.getJournal("Ledger");
50 auto const seq = ledger->header().seq;
51 if (!registry.getPendingSaves().startWork(seq))
52 {
53 // The save was completed synchronously
54 JLOG(j.debug()) << "Save aborted";
55 return true;
56 }
57
58 auto& db = registry.getRelationalDatabase();
59
60 bool res = false;
61 try
62 {
63 res = db.saveValidatedLedger(ledger, current);
64 }
65 catch (...)
66 {
67 // The ledger's row may be gone, so stop advertising the sequence as complete.
68 registry.getLedgerMaster().clearLedger(seq);
69
70 // Neither the in-progress mark nor the SAVED flag may outlive a save that did not happen,
71 // or a retry would be deduplicated away. Release them in the reverse of the order
72 // pendSaveValidated() takes them, or a concurrent enqueue can re-arm the flag.
73 registry.getPendingSaves().finishWork(seq);
74 registry.getHashRouter().clearFlags(ledger->header().hash, HashRouterFlags::SAVED);
75 throw;
76 }
77
78 // Clients can now trust the database for
79 // information about this ledger sequence.
80 registry.getPendingSaves().finishWork(seq);
81 return res;
82}
83
84bool
86 ServiceRegistry& registry,
88 bool isSynchronous,
89 bool isCurrent)
90{
91 if (!registry.getHashRouter().setFlags(ledger->header().hash, HashRouterFlags::SAVED))
92 {
93 // We have tried to save this ledger recently
94 auto const stream = registry.getJournal("Ledger").debug();
95 JLOG(stream) << "Double pend save for " << ledger->header().seq;
96
97 if (!isSynchronous || !registry.getPendingSaves().pending(ledger->header().seq))
98 {
99 // Either we don't need it to be finished
100 // or it is finished
101 return true;
102 }
103 }
104
105 XRPL_ASSERT(ledger->isImmutable(), "xrpl::pendSaveValidated : immutable ledger");
106
107 if (!registry.getPendingSaves().shouldWork(ledger->header().seq, isSynchronous))
108 {
109 auto const stream = registry.getJournal("Ledger").debug();
110 JLOG(stream) << "Pend save with seq in pending saves " << ledger->header().seq;
111
112 return true;
113 }
114
115 // See if we can use the JobQueue.
116 if (!isSynchronous &&
117 registry.getJobQueue().addJob(
119 "Pub" + std::to_string(ledger->seq()),
120 [&registry, ledger, isCurrent] { saveValidatedLedger(registry, ledger, isCurrent); }))
121 {
122 return true;
123 }
124
125 // The JobQueue won't do the Job. Do the save synchronously.
126 return saveValidatedLedger(registry, ledger, isCurrent);
127}
128
131 LedgerHeader const& info,
132 Rules const& rules,
133 Fees const& fees,
134 ServiceRegistry& registry,
135 bool acquire)
136{
137 bool loaded = false;
138 auto ledger = std::make_shared<Ledger>(
139 info,
140 loaded,
141 acquire,
142 rules,
143 fees,
144 registry.getNodeFamily(),
145 registry.getJournal("Ledger"));
146
147 if (!loaded)
148 ledger.reset();
149
150 return ledger;
151}
152
160static void
162{
163 if (!ledger)
164 return;
165
166 XRPL_ASSERT(
167 ledger->header().seq < kXrpLedgerEarliestFees || FeeSettingsEntryR(*ledger),
168 "xrpl::finishLoadByIndexOrHash : valid ledger fees");
169 // Loaded locally. See Ledger::setImmutable().
170 if (!ledger->setImmutable())
171 {
172 // LCOV_EXCL_START
173 JLOG(j.error()) << "Invalid map for ledger " << ledger->header().seq
174 << "; not marking it as loaded";
175 UNREACHABLE("xrpl::finishLoadByIndexOrHash : map is invalid");
176 // Discarded rather than left un-full, since nothing gates usability on the full flag.
177 ledger.reset();
178 return;
179 // LCOV_EXCL_STOP
180 }
181
182 JLOG(j.trace()) << "Loaded ledger: " << to_string(ledger->header().hash);
183
184 ledger->setFull();
185}
186
188getLatestLedger(Rules const& rules, Fees const& fees, ServiceRegistry& registry)
189{
191 if (!info)
192 return {std::shared_ptr<Ledger>(), {}, {}};
193 return {loadLedgerHelper(*info, rules, fees, registry, true), info->seq, info->hash};
194}
195
198 std::uint32_t ledgerIndex,
199 Rules const& rules,
200 Fees const& fees,
201 ServiceRegistry& registry,
202 bool acquire)
203{
205 registry.getRelationalDatabase().getLedgerInfoByIndex(ledgerIndex))
206 {
207 std::shared_ptr<Ledger> ledger = loadLedgerHelper(*info, rules, fees, registry, acquire);
208 finishLoadByIndexOrHash(ledger, registry.getJournal("Ledger"));
209 return ledger;
210 }
211 return {};
212}
213
216 UInt256 const& ledgerHash,
217 Rules const& rules,
218 Fees const& fees,
219 ServiceRegistry& registry,
220 bool acquire)
221{
223 registry.getRelationalDatabase().getLedgerInfoByHash(ledgerHash))
224 {
225 std::shared_ptr<Ledger> ledger = loadLedgerHelper(*info, rules, fees, registry, acquire);
226 finishLoadByIndexOrHash(ledger, registry.getJournal("Ledger"));
227 XRPL_ASSERT(
228 !ledger || ledger->header().hash == ledgerHash,
229 "xrpl::loadByHash : ledger hash match if loaded");
230 return ledger;
231 }
232 return {};
233}
234
235} // namespace xrpl
A generic endpoint for log messages.
Definition Journal.h:44
Stream error() const
Definition Journal.h:362
Stream debug() const
Definition Journal.h:344
Stream trace() const
Severity stream access functions.
Definition Journal.h:338
bool setFlags(UInt256 const &key, HashRouterFlags flags)
Set the flags on a hash.
bool clearFlags(UInt256 const &key, HashRouterFlags flags)
Clear flags on a hash.
bool addJob(JobType type, std::string const &name, JobHandler &&jobHandler)
Adds a job to the JobQueue.
Definition JobQueue.h:176
void clearLedger(std::uint32_t seq)
Marks a ledger no longer resident.
void finishWork(LedgerIndex seq)
Finish working on a ledger.
bool shouldWork(LedgerIndex seq, bool isSynchronous)
Check if a ledger should be dispatched.
bool startWork(LedgerIndex seq)
Start working on a ledger.
bool pending(LedgerIndex seq)
Return true if a ledger is in the progress of being saved.
virtual std::optional< LedgerHeader > getNewestLedgerInfo()=0
getNewestLedgerInfo Returns the info of the newest saved ledger.
virtual std::optional< LedgerHeader > getLedgerInfoByHash(UInt256 const &ledgerHash)=0
getLedgerInfoByHash Returns the info of the ledger with given hash.
virtual std::optional< LedgerHeader > getLedgerInfoByIndex(LedgerIndex ledgerSeq)=0
getLedgerInfoByIndex Returns a ledger by its sequence.
Rules controlling protocol behavior.
Definition Rules.h:40
Service registry for dependency injection.
virtual PendingSaves & getPendingSaves()=0
virtual JobQueue & getJobQueue()=0
virtual RelationalDatabase & getRelationalDatabase()=0
virtual beast::Journal getJournal(std::string const &name)=0
virtual LedgerMaster & getLedgerMaster()=0
virtual HashRouter & getHashRouter()=0
virtual Family & getNodeFamily()=0
T make_shared(T... args)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
bool isCurrent(ValidationParms const &p, NetClock::time_point now, NetClock::time_point signTime, NetClock::time_point seenTime)
Whether a validation is still current.
std::tuple< std::shared_ptr< Ledger >, std::uint32_t, UInt256 > getLatestLedger(Rules const &rules, Fees const &fees, ServiceRegistry &registry)
Fetch the ledger with the highest sequence contained in the database.
bool pendSaveValidated(ServiceRegistry &registry, std::shared_ptr< Ledger const > const &ledger, bool isSynchronous, bool isCurrent)
Save, or arrange to save, a fully-validated ledger.
std::shared_ptr< Ledger > loadLedgerHelper(LedgerHeader const &info, Rules const &rules, Fees const &fees, ServiceRegistry &registry, bool acquire)
Make ledger using info loaded from database.
static void finishLoadByIndexOrHash(std::shared_ptr< Ledger > &ledger, beast::Journal j)
Settle a ledger just loaded from local storage, or discard it.
std::shared_ptr< Ledger > loadByIndex(std::uint32_t ledgerIndex, Rules const &rules, Fees const &fees, ServiceRegistry &registry, bool acquire)
Load a ledger by its sequence number.
std::string to_string(BaseUInt< Bits, Tag > const &a)
Definition base_uint.h:658
static constexpr std::uint32_t kXrpLedgerEarliestFees
The XRP Ledger mainnet's earliest ledger with a FeeSettings object.
BaseUInt< 256 > UInt256
Definition base_uint.h:581
static bool saveValidatedLedger(ServiceRegistry &registry, std::shared_ptr< Ledger const > const &ledger, bool current)
Save a validated ledger to the relational database, once.
std::shared_ptr< Ledger > loadByHash(UInt256 const &ledgerHash, Rules const &rules, Fees const &fees, ServiceRegistry &registry, bool acquire)
Load a ledger by its hash.
@ JtPuboldledger
Definition Job.h:30
@ JtPubledger
Definition Job.h:54
FeeSettingsEntry< ReadView > FeeSettingsEntryR
T reset(T... args)
Reflects the fee settings for a particular ledger.
Information about the notional ledger backing the view.
T to_string(T... args)