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/Log.h>
10#include <xrpl/basics/chrono.h>
11#include <xrpl/core/JobQueue.h>
12#include <xrpl/core/PerfLog.h>
13
14#include <memory>
15
16namespace xrpl {
17
19 : ledgerID_{0}, ledgerSeq_{0}, j_{beast::Journal::getNullSink()}
20{
21}
22
26 : ledgerID_{ledger->header().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 "xrpl::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_ << " seq=" << ledgerSeq_
72 << " (available: " << minSeq() << "-" << seq() << ")";
73 // Default ID that is less than all others
74 return ID{0};
75}
76
77// Return the sequence number of the earliest possible mismatching ancestor
80{
82
83 // Find overlapping interval for known sequence for the ledgers
84 Seq const lower = std::max(a.minSeq(), b.minSeq());
85 Seq const upper = std::min(a.seq(), b.seq());
86
87 Seq curr = upper;
88 while (curr != Seq{0} && a[curr] != b[curr] && curr >= lower)
89 --curr;
90
91 // If the searchable interval mismatches entirely, then we have to
92 // assume the ledgers mismatch starting post genesis ledger
93 return (curr < lower) ? Seq{1} : (curr + Seq{1});
94}
95
99
102{
103 return app_.timeKeeper().closeTime();
104}
105
108{
109 using namespace std::chrono_literals;
110 auto ledger = perf::measureDurationAndLog(
111 [&]() { return app_.getLedgerMaster().getLedgerByHash(hash); },
112 "getLedgerByHash",
113 10ms,
114 j_);
115
116 if (!ledger)
117 {
118 JLOG(j_.warn()) << "Need validated ledger for preferred ledger analysis " << hash;
119
120 Application* pApp = &app_;
121
122 app_.getJobQueue().addJob(jtADVANCE, "GetConsL2", [pApp, hash, this]() {
123 JLOG(j_.debug()) << "JOB advanceLedger getConsensusLedger2 started";
125 });
126 return std::nullopt;
127 }
128
129 XRPL_ASSERT(
130 !ledger->open() && ledger->isImmutable(),
131 "xrpl::RCLValidationsAdaptor::acquire : valid ledger state");
132 XRPL_ASSERT(
133 ledger->header().hash == hash, "xrpl::RCLValidationsAdaptor::acquire : ledger hash match");
134
135 return RCLValidatedLedger(std::move(ledger), j_);
136}
137
138void
140 Application& app,
142 std::string const& source,
143 BypassAccept const bypassAccept,
145{
146 auto const& signingKey = val->getSignerPublic();
147 auto const& hash = val->getLedgerHash();
148 auto const seq = val->getFieldU32(sfLedgerSequence);
149
150 // Ensure validation is marked as trusted if signer currently trusted
151 auto masterKey = app.validators().getTrustedKey(signingKey);
152
153 if (!val->isTrusted() && masterKey)
154 val->setTrusted();
155
156 // If not currently trusted, see if signer is currently listed
157 if (!masterKey)
158 masterKey = app.validators().getListedKey(signingKey);
159
160 auto& validations = app.getValidations();
161
162 // masterKey is seated only if validator is trusted or listed
163 auto const outcome = validations.add(calcNodeID(masterKey.value_or(signingKey)), val);
164
165 if (outcome == ValStatus::current)
166 {
167 if (val->isTrusted())
168 {
169 if (bypassAccept == BypassAccept::yes)
170 {
171 XRPL_ASSERT(j, "xrpl::handleNewValidation : journal is available");
172 if (j.has_value())
173 {
174 JLOG(j->trace())
175 << "Bypassing checkAccept for validation " << val->getLedgerHash();
176 }
177 }
178 else
179 {
180 app.getLedgerMaster().checkAccept(hash, seq);
181 }
182 }
183 return;
184 }
185
186 // Ensure that problematic validations from validators we trust are
187 // logged at the highest possible level.
188 //
189 // One might think that we should more than just log: we ought to also
190 // not relay validations that fail these checks. Alas, and somewhat
191 // counterintuitively, we *especially* want to forward such validations,
192 // so that our peers will also observe them and take independent notice of
193 // such validators, informing their operators.
194 if (auto const ls = val->isTrusted() ? validations.adaptor().journal().error()
195 : validations.adaptor().journal().info();
196 ls.active())
197 {
198 auto const id = [&masterKey, &signingKey]() {
199 auto ret = toBase58(TokenType::NodePublic, signingKey);
200
201 if (masterKey && masterKey != signingKey)
202 ret += ":" + toBase58(TokenType::NodePublic, *masterKey);
203
204 return ret;
205 }();
206
207 if (outcome == ValStatus::conflicting)
208 ls << "Byzantine Behavior Detector: " << (val->isTrusted() ? "trusted " : "untrusted ")
209 << id << ": Conflicting validation for " << seq << "!\n["
210 << val->getSerializer().slice() << "]";
211
212 if (outcome == ValStatus::multiple)
213 ls << "Byzantine Behavior Detector: " << (val->isTrusted() ? "trusted " : "untrusted ")
214 << id << ": Multiple validations for " << seq << "/" << hash << "!\n["
215 << val->getSerializer().slice() << "]";
216 }
217}
218
219} // 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:146
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 & validators()=0
virtual RCLValidations & getValidations()=0
virtual InboundLedgers & getInboundLedgers()=0
virtual LedgerMaster & getLedgerMaster()=0
virtual TimeKeeper & timeKeeper()=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)