xrpld
Loading...
Searching...
No Matches
RCLValidations.cpp
1#include <xrpld/app/consensus/RCLValidations.h>
2
3#include <xrpld/app/ledger/InboundLedger.h>
4#include <xrpld/app/ledger/InboundLedgers.h>
5#include <xrpld/app/ledger/LedgerMaster.h>
6#include <xrpld/app/main/Application.h>
7#include <xrpld/app/misc/ValidatorList.h>
8#include <xrpld/core/TimeKeeper.h>
9
10#include <xrpl/basics/Log.h>
11#include <xrpl/basics/chrono.h>
12#include <xrpl/beast/utility/instrumentation.h>
13#include <xrpl/consensus/Validations.h>
14#include <xrpl/core/Job.h>
15#include <xrpl/core/JobQueue.h>
16#include <xrpl/core/PerfLog.h>
17#include <xrpl/ledger/entries/LedgerHashesEntry.h>
18#include <xrpl/protocol/PublicKey.h>
19#include <xrpl/protocol/RippleLedgerHash.h>
20#include <xrpl/protocol/SField.h>
21#include <xrpl/protocol/tokens.h>
22
23#include <algorithm>
24#include <memory>
25#include <optional>
26
27namespace xrpl {
28
30 : ledgerID_{0}, ledgerSeq_{0}, j_{beast::Journal::getNullSink()}
31{
32}
33
37 : ledgerID_{ledger->header().hash}, ledgerSeq_{ledger->seq()}, j_{j}
38{
39 LedgerHashesEntryR const hashIndex(*ledger, j_);
40 if (hashIndex)
41 {
42 XRPL_ASSERT(
43 hashIndex->getFieldU32(sfLastLedgerSequence) == (seq() - 1),
44 "xrpl::RCLValidatedLedger::RCLValidatedLedger(Ledger) : valid "
45 "last ledger sequence");
46 ancestors_ = hashIndex->getFieldV256(sfHashes).value();
47 }
48 else
49 {
50 JLOG(j_.warn()) << "Ledger " << ledgerSeq_ << ":" << ledgerID_
51 << " missing recent ancestor hashes";
52 }
53}
54
55auto
57{
58 return seq() - std::min(seq(), static_cast<Seq>(ancestors_.size()));
59}
60
61auto
63{
64 return ledgerSeq_;
65}
66auto
68{
69 return ledgerID_;
70}
71
72auto
74{
75 if (s >= minSeq() && s <= seq())
76 {
77 if (s == seq())
78 return ledgerID_;
79 Seq const diff = seq() - s;
80 return ancestors_[ancestors_.size() - diff];
81 }
82
83 JLOG(j_.warn()) << "Unable to determine hash of ancestor seq=" << s
84 << " from ledger hash=" << ledgerID_ << " seq=" << ledgerSeq_
85 << " (available: " << minSeq() << "-" << seq() << ")";
86 // Default ID that is less than all others
87 return ID{0};
88}
89
90// Return the sequence number of the earliest possible mismatching ancestor
93{
95
96 // Find overlapping interval for known sequence for the ledgers
97 Seq const lower = std::max(a.minSeq(), b.minSeq());
98 Seq const upper = std::min(a.seq(), b.seq());
99
100 Seq curr = upper;
101 while (curr != Seq{0} && a[curr] != b[curr] && curr >= lower)
102 --curr;
103
104 // If the searchable interval mismatches entirely, then we have to
105 // assume the ledgers mismatch starting post genesis ledger
106 return (curr < lower) ? Seq{1} : (curr + Seq{1});
107}
108
112
115{
116 return app_.getTimeKeeper().closeTime();
117}
118
121{
122 using namespace std::chrono_literals;
123 auto const ledger = perf::measureDurationAndLog(
124 [&] { return app_.getLedgerMaster().getLedgerByHash(hash); }, "getLedgerByHash", 10ms, j_);
125
126 if (!ledger)
127 {
128 JLOG(j_.debug()) << "Need validated ledger for preferred ledger analysis " << hash;
129
130 Application* pApp = &app_;
131
132 app_.getJobQueue().addJob(JtAdvance, "GetConsL2", [pApp, hash, this] {
133 JLOG(j_.debug()) << "JOB advanceLedger getConsensusLedger2 started";
135 });
136 return std::nullopt;
137 }
138
139 XRPL_ASSERT(
140 !ledger->open() && ledger->isImmutable(),
141 "xrpl::RCLValidationsAdaptor::acquire : valid ledger state");
142 XRPL_ASSERT(
143 ledger->header().hash == hash, "xrpl::RCLValidationsAdaptor::acquire : ledger hash match");
144
145 return RCLValidatedLedger(ledger, j_);
146}
147
148void
150 Application& app,
152 std::string const& source,
153 BypassAccept const bypassAccept,
155{
156 auto const& signingKey = val->getSignerPublic();
157 auto const& hash = val->getLedgerHash();
158 auto const seq = val->getFieldU32(sfLedgerSequence);
159
160 // Ensure validation is marked as trusted if signer currently trusted
161 auto masterKey = app.getValidators().getTrustedKey(signingKey);
162
163 if (!val->isTrusted() && masterKey)
164 val->setTrusted();
165
166 // If not currently trusted, see if signer is currently listed
167 if (!masterKey)
168 masterKey = app.getValidators().getListedKey(signingKey);
169
170 auto& validations = app.getValidations();
171
172 // masterKey is seated only if validator is trusted or listed
173 auto const outcome = validations.add(calcNodeID(masterKey.value_or(signingKey)), val);
174
175 if (outcome == ValStatus::Current)
176 {
177 if (val->isTrusted())
178 {
179 if (bypassAccept == BypassAccept::Yes)
180 {
181 XRPL_ASSERT(j, "xrpl::handleNewValidation : journal is available");
182 if (j.has_value())
183 {
184 JLOG(j->trace())
185 << "Bypassing checkAccept for validation " << val->getLedgerHash();
186 }
187 }
188 else
189 {
190 app.getLedgerMaster().checkAccept(hash, seq);
191 }
192 }
193 return;
194 }
195
196 // Ensure that problematic validations from validators we trust are
197 // logged at the highest possible level.
198 //
199 // One might think that we should more than just log: we ought to also
200 // not relay validations that fail these checks. Alas, and somewhat
201 // counterintuitively, we *especially* want to forward such validations,
202 // so that our peers will also observe them and take independent notice of
203 // such validators, informing their operators.
204 if (auto const ls = val->isTrusted() ? 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 {
219 ls << "Byzantine Behavior Detector: " << (val->isTrusted() ? "trusted " : "untrusted ")
220 << id << ": Conflicting validation for " << seq << "!\n["
221 << val->getSerializer().slice() << "]";
222 }
223
224 if (outcome == ValStatus::Multiple)
225 {
226 ls << "Byzantine Behavior Detector: " << (val->isTrusted() ? "trusted " : "untrusted ")
227 << id << ": Multiple validations for " << seq << "/" << hash << "!\n["
228 << val->getSerializer().slice() << "]";
229 }
230 }
231}
232
233} // namespace xrpl
A generic endpoint for log messages.
Definition Journal.h:44
virtual void acquireAsync(UInt256 const &hash, std::uint32_t seq, InboundLedger::Reason reason)=0
void checkAccept(std::shared_ptr< Ledger const > const &ledger)
If the ledger passes canBeCurrent(), is newer than the validated ledger and has a quorum,...
std::chrono::time_point< NetClock > time_point
Definition chrono.h:48
Wraps a ledger instance for use in generic Validations LedgerTrie.
std::vector< UInt256 > ancestors_
Seq seq() const
The sequence (index) of the ledger.
ID id() const
The ID (hash) of the ledger.
ID operator[](Seq const &s) const
Lookup the ID of the ancestor ledger.
std::optional< RCLValidatedLedger > acquire(LedgerHash const &id)
Attempt to acquire the ledger with given id from the network.
NetClock::time_point now() const
Current time used to determine if validations are stale.
RCLValidationsAdaptor(Application &app, beast::Journal j)
std::uint32_t getFieldU32(SField const &field) const
Definition STObject.cpp:601
STVector256 const & getFieldV256(SField const &field) const
Definition STObject.cpp:671
std::vector< UInt256 > const & value() const
virtual ValidatorList & getValidators()=0
virtual RCLValidations & getValidations()=0
virtual InboundLedgers & getInboundLedgers()=0
virtual LedgerMaster & getLedgerMaster()=0
ValStatus add(NodeID const &nodeID, Validation const &val)
Add a new validation.
std::optional< PublicKey > getListedKey(PublicKey const &identity) const
Returns listed master public if public key is included on any lists.
std::optional< PublicKey > getTrustedKey(PublicKey const &identity) const
Returns master public key if public key is trusted.
T max(T... args)
T min(T... args)
auto measureDurationAndLog(Func &&func, std::string_view actionDescription, std::chrono::duration< Rep, Period > maxDelay, beast::Journal const &journal)
Definition PerfLog.h:174
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
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.
std::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
Definition AccountID.cpp:95
UInt256 LedgerHash
@ 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.
@ JtAdvance
Definition Job.h:53
NodeID calcNodeID(PublicKey const &)
Calculate the 160-bit node ID from a node public key.
RCLValidatedLedger::Seq mismatch(RCLValidatedLedger const &a, RCLValidatedLedger const &b)
LedgerHashesEntry< ReadView > LedgerHashesEntryR
T has_value(T... args)