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/TimeKeeper.h>
8
9#include <xrpl/basics/chrono.h>
10#include <xrpl/core/JobQueue.h>
11#include <xrpl/core/PerfLog.h>
12
13#include <memory>
14
15namespace xrpl {
16
18 : ledgerID_{0}, ledgerSeq_{0}, j_{beast::Journal::getNullSink()}
19{
20}
21
25 : ledgerID_{ledger->header().hash}, ledgerSeq_{ledger->seq()}, j_{j}
26{
27 auto const hashIndex = ledger->read(keylet::skip());
28 if (hashIndex)
29 {
30 XRPL_ASSERT(
31 hashIndex->getFieldU32(sfLastLedgerSequence) == (seq() - 1),
32 "xrpl::RCLValidatedLedger::RCLValidatedLedger(Ledger) : valid "
33 "last ledger sequence");
34 ancestors_ = hashIndex->getFieldV256(sfHashes).value();
35 }
36 else
37 JLOG(j_.warn()) << "Ledger " << ledgerSeq_ << ":" << ledgerID_
38 << " missing recent ancestor hashes";
39}
40
41auto
43{
44 return seq() - std::min(seq(), static_cast<Seq>(ancestors_.size()));
45}
46
47auto
49{
50 return ledgerSeq_;
51}
52auto
54{
55 return ledgerID_;
56}
57
58auto
60{
61 if (s >= minSeq() && s <= seq())
62 {
63 if (s == seq())
64 return ledgerID_;
65 Seq const diff = seq() - s;
66 return ancestors_[ancestors_.size() - diff];
67 }
68
69 JLOG(j_.warn()) << "Unable to determine hash of ancestor seq=" << s
70 << " from ledger hash=" << ledgerID_ << " seq=" << ledgerSeq_
71 << " (available: " << minSeq() << "-" << seq() << ")";
72 // Default ID that is less than all others
73 return ID{0};
74}
75
76// Return the sequence number of the earliest possible mismatching ancestor
79{
81
82 // Find overlapping interval for known sequence for the ledgers
83 Seq const lower = std::max(a.minSeq(), b.minSeq());
84 Seq const upper = std::min(a.seq(), b.seq());
85
86 Seq curr = upper;
87 while (curr != Seq{0} && a[curr] != b[curr] && curr >= lower)
88 --curr;
89
90 // If the searchable interval mismatches entirely, then we have to
91 // assume the ledgers mismatch starting post genesis ledger
92 return (curr < lower) ? Seq{1} : (curr + Seq{1});
93}
94
98
101{
102 return app_.getTimeKeeper().closeTime();
103}
104
107{
108 using namespace std::chrono_literals;
109 auto ledger = perf::measureDurationAndLog(
110 [&]() { return app_.getLedgerMaster().getLedgerByHash(hash); },
111 "getLedgerByHash",
112 10ms,
113 j_);
114
115 if (!ledger)
116 {
117 JLOG(j_.warn()) << "Need validated ledger for preferred ledger analysis " << hash;
118
119 Application* pApp = &app_;
120
121 app_.getJobQueue().addJob(jtADVANCE, "GetConsL2", [pApp, hash, this]() {
122 JLOG(j_.debug()) << "JOB advanceLedger getConsensusLedger2 started";
124 });
125 return std::nullopt;
126 }
127
128 XRPL_ASSERT(
129 !ledger->open() && ledger->isImmutable(),
130 "xrpl::RCLValidationsAdaptor::acquire : valid ledger state");
131 XRPL_ASSERT(
132 ledger->header().hash == hash, "xrpl::RCLValidationsAdaptor::acquire : ledger hash match");
133
134 return RCLValidatedLedger(ledger, j_);
135}
136
137void
139 Application& app,
141 std::string const& source,
142 BypassAccept const bypassAccept,
144{
145 auto const& signingKey = val->getSignerPublic();
146 auto const& hash = val->getLedgerHash();
147 auto const seq = val->getFieldU32(sfLedgerSequence);
148
149 // Ensure validation is marked as trusted if signer currently trusted
150 auto masterKey = app.getValidators().getTrustedKey(signingKey);
151
152 if (!val->isTrusted() && masterKey)
153 val->setTrusted();
154
155 // If not currently trusted, see if signer is currently listed
156 if (!masterKey)
157 masterKey = app.getValidators().getListedKey(signingKey);
158
159 auto& validations = app.getValidations();
160
161 // masterKey is seated only if validator is trusted or listed
162 auto const outcome = validations.add(calcNodeID(masterKey.value_or(signingKey)), val);
163
164 if (outcome == ValStatus::current)
165 {
166 if (val->isTrusted())
167 {
168 if (bypassAccept == BypassAccept::yes)
169 {
170 XRPL_ASSERT(j, "xrpl::handleNewValidation : journal is available");
171 if (j.has_value())
172 {
173 JLOG(j->trace())
174 << "Bypassing checkAccept for validation " << val->getLedgerHash();
175 }
176 }
177 else
178 {
179 app.getLedgerMaster().checkAccept(hash, seq);
180 }
181 }
182 return;
183 }
184
185 // Ensure that problematic validations from validators we trust are
186 // logged at the highest possible level.
187 //
188 // One might think that we should more than just log: we ought to also
189 // not relay validations that fail these checks. Alas, and somewhat
190 // counterintuitively, we *especially* want to forward such validations,
191 // so that our peers will also observe them and take independent notice of
192 // such validators, informing their operators.
193 if (auto const ls = val->isTrusted() ? validations.adaptor().journal().error()
194 : validations.adaptor().journal().info();
195 ls.active())
196 {
197 auto const id = [&masterKey, &signingKey]() {
198 auto ret = toBase58(TokenType::NodePublic, signingKey);
199
200 if (masterKey && masterKey != signingKey)
201 ret += ":" + toBase58(TokenType::NodePublic, *masterKey);
202
203 return ret;
204 }();
205
206 if (outcome == ValStatus::conflicting)
207 {
208 ls << "Byzantine Behavior Detector: " << (val->isTrusted() ? "trusted " : "untrusted ")
209 << id << ": Conflicting validation for " << seq << "!\n["
210 << val->getSerializer().slice() << "]";
211 }
212
213 if (outcome == ValStatus::multiple)
214 {
215 ls << "Byzantine Behavior Detector: " << (val->isTrusted() ? "trusted " : "untrusted ")
216 << id << ": Multiple validations for " << seq << "/" << hash << "!\n["
217 << val->getSerializer().slice() << "]";
218 }
219 }
220}
221
222} // namespace xrpl
A generic endpoint for log messages.
Definition Journal.h:40
Stream debug() const
Definition Journal.h:301
Stream warn() const
Definition Journal.h:313
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:147
void checkAccept(std::shared_ptr< Ledger const > const &ledger)
Wraps a ledger instance for use in generic Validations LedgerTrie.
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::vector< uint256 > ancestors_
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)
virtual JobQueue & getJobQueue()=0
virtual ValidatorList & getValidators()=0
virtual RCLValidations & getValidations()=0
virtual InboundLedgers & getInboundLedgers()=0
virtual LedgerMaster & getLedgerMaster()=0
virtual TimeKeeper & getTimeKeeper()=0
time_point closeTime() const
Returns the predicted close time, in network time.
Definition TimeKeeper.h:56
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 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:162
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
std::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
Definition AccountID.cpp:92
@ 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:46
NodeID calcNodeID(PublicKey const &)
Calculate the 160-bit node ID from a node public key.
RCLValidatedLedger::Seq mismatch(RCLValidatedLedger const &a, RCLValidatedLedger const &b)
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.
T has_value(T... args)