rippled
Loading...
Searching...
No Matches
RCLValidations.cpp
1#include <xrpld/app/consensus/RCLValidations.h>
2#include <xrpld/app/ledger/InboundLedger.h>
3#include <xrpld/app/ledger/InboundLedgers.h>
4#include <xrpld/app/ledger/LedgerMaster.h>
5#include <xrpld/app/main/Application.h>
6#include <xrpld/app/misc/ValidatorList.h>
7#include <xrpld/core/JobQueue.h>
8#include <xrpld/core/TimeKeeper.h>
9#include <xrpld/perflog/PerfLog.h>
10
11#include <xrpl/basics/Log.h>
12#include <xrpl/basics/chrono.h>
13
14#include <memory>
15
16namespace ripple {
17
19 : ledgerID_{0}, ledgerSeq_{0}, j_{beast::Journal::getNullSink()}
20{
21}
22
26 : ledgerID_{ledger->info().hash}, ledgerSeq_{ledger->seq()}, j_{j}
27{
28 auto const hashIndex = ledger->read(keylet::skip());
29 if (hashIndex)
30 {
31 XRPL_ASSERT(
32 hashIndex->getFieldU32(sfLastLedgerSequence) == (seq() - 1),
33 "ripple::RCLValidatedLedger::RCLValidatedLedger(Ledger) : valid "
34 "last ledger sequence");
35 ancestors_ = hashIndex->getFieldV256(sfHashes).value();
36 }
37 else
38 JLOG(j_.warn()) << "Ledger " << ledgerSeq_ << ":" << ledgerID_
39 << " missing recent ancestor hashes";
40}
41
42auto
44{
45 return seq() - std::min(seq(), static_cast<Seq>(ancestors_.size()));
46}
47
48auto
50{
51 return ledgerSeq_;
52}
53auto
55{
56 return ledgerID_;
57}
58
59auto
61{
62 if (s >= minSeq() && s <= seq())
63 {
64 if (s == seq())
65 return ledgerID_;
66 Seq const diff = seq() - s;
67 return ancestors_[ancestors_.size() - diff];
68 }
69
70 JLOG(j_.warn()) << "Unable to determine hash of ancestor seq=" << s
71 << " from ledger hash=" << ledgerID_
72 << " seq=" << ledgerSeq_ << " (available: " << minSeq()
73 << "-" << seq() << ")";
74 // Default ID that is less than all others
75 return ID{0};
76}
77
78// Return the sequence number of the earliest possible mismatching ancestor
81{
83
84 // Find overlapping interval for known sequence for the ledgers
85 Seq const lower = std::max(a.minSeq(), b.minSeq());
86 Seq const upper = std::min(a.seq(), b.seq());
87
88 Seq curr = upper;
89 while (curr != Seq{0} && a[curr] != b[curr] && curr >= lower)
90 --curr;
91
92 // If the searchable interval mismatches entirely, then we have to
93 // assume the ledgers mismatch starting post genesis ledger
94 return (curr < lower) ? Seq{1} : (curr + Seq{1});
95}
96
101
104{
105 return app_.timeKeeper().closeTime();
106}
107
110{
111 using namespace std::chrono_literals;
112 auto ledger = perf::measureDurationAndLog(
113 [&]() { return app_.getLedgerMaster().getLedgerByHash(hash); },
114 "getLedgerByHash",
115 10ms,
116 j_);
117
118 if (!ledger)
119 {
120 JLOG(j_.warn())
121 << "Need validated ledger for preferred ledger analysis " << hash;
122
123 Application* pApp = &app_;
124
126 jtADVANCE, "getConsensusLedger2", [pApp, hash, this]() {
127 JLOG(j_.debug())
128 << "JOB advanceLedger getConsensusLedger2 started";
131 });
132 return std::nullopt;
133 }
134
135 XRPL_ASSERT(
136 !ledger->open() && ledger->isImmutable(),
137 "ripple::RCLValidationsAdaptor::acquire : valid ledger state");
138 XRPL_ASSERT(
139 ledger->info().hash == hash,
140 "ripple::RCLValidationsAdaptor::acquire : ledger hash match");
141
142 return RCLValidatedLedger(std::move(ledger), j_);
143}
144
145void
147 Application& app,
149 std::string const& source,
150 BypassAccept const bypassAccept,
152{
153 auto const& signingKey = val->getSignerPublic();
154 auto const& hash = val->getLedgerHash();
155 auto const seq = val->getFieldU32(sfLedgerSequence);
156
157 // Ensure validation is marked as trusted if signer currently trusted
158 auto masterKey = app.validators().getTrustedKey(signingKey);
159
160 if (!val->isTrusted() && masterKey)
161 val->setTrusted();
162
163 // If not currently trusted, see if signer is currently listed
164 if (!masterKey)
165 masterKey = app.validators().getListedKey(signingKey);
166
167 auto& validations = app.getValidations();
168
169 // masterKey is seated only if validator is trusted or listed
170 auto const outcome =
171 validations.add(calcNodeID(masterKey.value_or(signingKey)), val);
172
173 if (outcome == ValStatus::current)
174 {
175 if (val->isTrusted())
176 {
177 if (bypassAccept == BypassAccept::yes)
178 {
179 XRPL_ASSERT(
180 j, "ripple::handleNewValidation : journal is available");
181 if (j.has_value())
182 {
183 JLOG(j->trace()) << "Bypassing checkAccept for validation "
184 << val->getLedgerHash();
185 }
186 }
187 else
188 {
189 app.getLedgerMaster().checkAccept(hash, seq);
190 }
191 }
192 return;
193 }
194
195 // Ensure that problematic validations from validators we trust are
196 // logged at the highest possible level.
197 //
198 // One might think that we should more than just log: we ought to also
199 // not relay validations that fail these checks. Alas, and somewhat
200 // counterintuitively, we *especially* want to forward such validations,
201 // so that our peers will also observe them and take independent notice of
202 // such validators, informing their operators.
203 if (auto const ls = val->isTrusted()
204 ? validations.adaptor().journal().error()
205 : validations.adaptor().journal().info();
206 ls.active())
207 {
208 auto const id = [&masterKey, &signingKey]() {
209 auto ret = toBase58(TokenType::NodePublic, signingKey);
210
211 if (masterKey && masterKey != signingKey)
212 ret += ":" + toBase58(TokenType::NodePublic, *masterKey);
213
214 return ret;
215 }();
216
217 if (outcome == ValStatus::conflicting)
218 ls << "Byzantine Behavior Detector: "
219 << (val->isTrusted() ? "trusted " : "untrusted ") << id
220 << ": Conflicting validation for " << seq << "!\n["
221 << val->getSerializer().slice() << "]";
222
223 if (outcome == ValStatus::multiple)
224 ls << "Byzantine Behavior Detector: "
225 << (val->isTrusted() ? "trusted " : "untrusted ") << id
226 << ": Multiple validations for " << seq << "/" << hash << "!\n["
227 << val->getSerializer().slice() << "]";
228 }
229}
230
231} // namespace ripple
A generic endpoint for log messages.
Definition Journal.h:41
Stream debug() const
Definition Journal.h:309
Stream warn() const
Definition Journal.h:321
virtual RCLValidations & getValidations()=0
virtual TimeKeeper & timeKeeper()=0
virtual JobQueue & getJobQueue()=0
virtual InboundLedgers & getInboundLedgers()=0
virtual ValidatorList & validators()=0
virtual LedgerMaster & getLedgerMaster()=0
virtual void acquireAsync(uint256 const &hash, std::uint32_t seq, InboundLedger::Reason reason)=0
bool addJob(JobType type, std::string const &name, JobHandler &&jobHandler)
Adds a job to the JobQueue.
Definition JobQueue.h:149
void checkAccept(std::shared_ptr< Ledger const > const &ledger)
Wraps a ledger instance for use in generic Validations LedgerTrie.
ID id() const
The ID (hash) of the ledger.
ID operator[](Seq const &s) const
Lookup the ID of the ancestor ledger.
std::vector< uint256 > ancestors_
Seq seq() const
The sequence (index) of the ledger.
std::optional< RCLValidatedLedger > acquire(LedgerHash const &id)
Attempt to acquire the ledger with given id from the network.
RCLValidationsAdaptor(Application &app, beast::Journal j)
NetClock::time_point now() const
Current time used to determine if validations are stale.
time_point closeTime() const
Returns the predicted close time, in network time.
Definition TimeKeeper.h:57
ValStatus add(NodeID const &nodeID, Validation const &val)
Add a new validation.
std::optional< PublicKey > getTrustedKey(PublicKey const &identity) const
Returns master public key if public key is trusted.
std::optional< PublicKey > getListedKey(PublicKey const &identity) const
Returns listed master public if public key is included on any lists.
T is_same_v
T max(T... args)
T min(T... args)
Keylet const & skip() noexcept
The index of the "short" skip list.
Definition Indexes.cpp:177
auto measureDurationAndLog(Func &&func, std::string const &actionDescription, std::chrono::duration< Rep, Period > maxDelay, beast::Journal const &journal)
Definition PerfLog.h:168
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6
std::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
Definition AccountID.cpp:95
void handleNewValidation(Application &app, std::shared_ptr< STValidation > const &val, std::string const &source, BypassAccept const bypassAccept, std::optional< beast::Journal > j)
Handle a new validation.
RCLValidatedLedger::Seq mismatch(RCLValidatedLedger const &a, RCLValidatedLedger const &b)
@ current
This was a new validation and was added.
@ conflicting
Multiple validations by a validator for different ledgers.
@ multiple
Multiple validations by a validator for the same ledger.
NodeID calcNodeID(PublicKey const &)
Calculate the 160-bit node ID from a node public key.
@ jtADVANCE
Definition Job.h:48
T has_value(T... args)