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
18RCLValidatedLedger::RCLValidatedLedger(MakeGenesis) : ledgerID_{0}, ledgerSeq_{0}, j_{beast::Journal::getNullSink()}
19{
20}
21
23 : ledgerID_{ledger->header().hash}, ledgerSeq_{ledger->seq()}, j_{j}
24{
25 auto const hashIndex = ledger->read(keylet::skip());
26 if (hashIndex)
27 {
28 XRPL_ASSERT(
29 hashIndex->getFieldU32(sfLastLedgerSequence) == (seq() - 1),
30 "xrpl::RCLValidatedLedger::RCLValidatedLedger(Ledger) : valid "
31 "last ledger sequence");
32 ancestors_ = hashIndex->getFieldV256(sfHashes).value();
33 }
34 else
35 JLOG(j_.warn()) << "Ledger " << ledgerSeq_ << ":" << ledgerID_ << " missing recent ancestor hashes";
36}
37
38auto
40{
41 return seq() - std::min(seq(), static_cast<Seq>(ancestors_.size()));
42}
43
44auto
46{
47 return ledgerSeq_;
48}
49auto
51{
52 return ledgerID_;
53}
54
55auto
57{
58 if (s >= minSeq() && s <= seq())
59 {
60 if (s == seq())
61 return ledgerID_;
62 Seq const diff = seq() - s;
63 return ancestors_[ancestors_.size() - diff];
64 }
65
66 JLOG(j_.warn()) << "Unable to determine hash of ancestor seq=" << s << " from ledger hash=" << ledgerID_
67 << " seq=" << ledgerSeq_ << " (available: " << minSeq() << "-" << seq() << ")";
68 // Default ID that is less than all others
69 return ID{0};
70}
71
72// Return the sequence number of the earliest possible mismatching ancestor
75{
77
78 // Find overlapping interval for known sequence for the ledgers
79 Seq const lower = std::max(a.minSeq(), b.minSeq());
80 Seq const upper = std::min(a.seq(), b.seq());
81
82 Seq curr = upper;
83 while (curr != Seq{0} && a[curr] != b[curr] && curr >= lower)
84 --curr;
85
86 // If the searchable interval mismatches entirely, then we have to
87 // assume the ledgers mismatch starting post genesis ledger
88 return (curr < lower) ? Seq{1} : (curr + Seq{1});
89}
90
94
97{
98 return app_.timeKeeper().closeTime();
99}
100
103{
104 using namespace std::chrono_literals;
105 auto ledger = perf::measureDurationAndLog(
106 [&]() { return app_.getLedgerMaster().getLedgerByHash(hash); }, "getLedgerByHash", 10ms, j_);
107
108 if (!ledger)
109 {
110 JLOG(j_.warn()) << "Need validated ledger for preferred ledger analysis " << hash;
111
112 Application* pApp = &app_;
113
114 app_.getJobQueue().addJob(jtADVANCE, "GetConsL2", [pApp, hash, this]() {
115 JLOG(j_.debug()) << "JOB advanceLedger getConsensusLedger2 started";
117 });
118 return std::nullopt;
119 }
120
121 XRPL_ASSERT(!ledger->open() && ledger->isImmutable(), "xrpl::RCLValidationsAdaptor::acquire : valid ledger state");
122 XRPL_ASSERT(ledger->header().hash == hash, "xrpl::RCLValidationsAdaptor::acquire : ledger hash match");
123
124 return RCLValidatedLedger(std::move(ledger), j_);
125}
126
127void
129 Application& app,
131 std::string const& source,
132 BypassAccept const bypassAccept,
134{
135 auto const& signingKey = val->getSignerPublic();
136 auto const& hash = val->getLedgerHash();
137 auto const seq = val->getFieldU32(sfLedgerSequence);
138
139 // Ensure validation is marked as trusted if signer currently trusted
140 auto masterKey = app.validators().getTrustedKey(signingKey);
141
142 if (!val->isTrusted() && masterKey)
143 val->setTrusted();
144
145 // If not currently trusted, see if signer is currently listed
146 if (!masterKey)
147 masterKey = app.validators().getListedKey(signingKey);
148
149 auto& validations = app.getValidations();
150
151 // masterKey is seated only if validator is trusted or listed
152 auto const outcome = validations.add(calcNodeID(masterKey.value_or(signingKey)), val);
153
154 if (outcome == ValStatus::current)
155 {
156 if (val->isTrusted())
157 {
158 if (bypassAccept == BypassAccept::yes)
159 {
160 XRPL_ASSERT(j, "xrpl::handleNewValidation : journal is available");
161 if (j.has_value())
162 {
163 JLOG(j->trace()) << "Bypassing checkAccept for validation " << val->getLedgerHash();
164 }
165 }
166 else
167 {
168 app.getLedgerMaster().checkAccept(hash, seq);
169 }
170 }
171 return;
172 }
173
174 // Ensure that problematic validations from validators we trust are
175 // logged at the highest possible level.
176 //
177 // One might think that we should more than just log: we ought to also
178 // not relay validations that fail these checks. Alas, and somewhat
179 // counterintuitively, we *especially* want to forward such validations,
180 // so that our peers will also observe them and take independent notice of
181 // such validators, informing their operators.
182 if (auto const ls =
183 val->isTrusted() ? validations.adaptor().journal().error() : validations.adaptor().journal().info();
184 ls.active())
185 {
186 auto const id = [&masterKey, &signingKey]() {
187 auto ret = toBase58(TokenType::NodePublic, signingKey);
188
189 if (masterKey && masterKey != signingKey)
190 ret += ":" + toBase58(TokenType::NodePublic, *masterKey);
191
192 return ret;
193 }();
194
195 if (outcome == ValStatus::conflicting)
196 ls << "Byzantine Behavior Detector: " << (val->isTrusted() ? "trusted " : "untrusted ") << id
197 << ": Conflicting validation for " << seq << "!\n[" << val->getSerializer().slice() << "]";
198
199 if (outcome == ValStatus::multiple)
200 ls << "Byzantine Behavior Detector: " << (val->isTrusted() ? "trusted " : "untrusted ") << id
201 << ": Multiple validations for " << seq << "/" << hash << "!\n[" << val->getSerializer().slice() << "]";
202 }
203}
204
205} // namespace xrpl
A generic endpoint for log messages.
Definition Journal.h:41
Stream debug() const
Definition Journal.h:301
Stream warn() const
Definition Journal.h:313
virtual InboundLedgers & getInboundLedgers()=0
virtual LedgerMaster & getLedgerMaster()=0
virtual TimeKeeper & timeKeeper()=0
virtual RCLValidations & getValidations()=0
virtual JobQueue & getJobQueue()=0
virtual ValidatorList & validators()=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: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)
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:172
auto measureDurationAndLog(Func &&func, std::string const &actionDescription, std::chrono::duration< Rep, Period > maxDelay, beast::Journal const &journal)
Definition PerfLog.h:159
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: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:47
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)