xrpld
Loading...
Searching...
No Matches
STValidation.cpp
1#include <xrpl/protocol/STValidation.h>
2
3#include <xrpl/basics/Blob.h>
4#include <xrpl/basics/Log.h>
5#include <xrpl/basics/Slice.h>
6#include <xrpl/basics/base_uint.h>
7#include <xrpl/basics/chrono.h>
8#include <xrpl/beast/utility/instrumentation.h>
9#include <xrpl/protocol/HashPrefix.h>
10#include <xrpl/protocol/KeyType.h> // IWYU pragma: keep
11#include <xrpl/protocol/PublicKey.h>
12#include <xrpl/protocol/SField.h>
13#include <xrpl/protocol/SOTemplate.h>
14#include <xrpl/protocol/STBase.h>
15#include <xrpl/protocol/STObject.h>
16#include <xrpl/protocol/Serializer.h>
17
18#include <cstddef>
19#include <exception>
20#include <utility>
21
22namespace xrpl {
23
24STBase*
26{
27 return emplace(n, buf, *this);
28}
29
30STBase*
32{
33 return emplace(n, buf, std::move(*this));
34}
35
36SOTemplate const&
38{
39 // We can't have this be a magic static at namespace scope because
40 // it relies on the SField's below being initialized, and we can't
41 // guarantee the initialization order.
42 // clang-format off
43 static SOTemplate const kFormat{
44 {sfFlags, SoeRequired},
45 {sfLedgerHash, SoeRequired},
46 {sfLedgerSequence, SoeRequired},
47 {sfCloseTime, SoeOptional},
48 {sfLoadFee, SoeOptional},
49 {sfAmendments, SoeOptional},
50 {sfBaseFee, SoeOptional},
51 {sfReserveBase, SoeOptional},
52 {sfReserveIncrement, SoeOptional},
53 {sfSigningTime, SoeRequired},
54 {sfSigningPubKey, SoeRequired},
55 {sfSignature, SoeRequired},
56 {sfConsensusHash, SoeOptional},
57 {sfCookie, SoeDefault},
58 {sfValidatedHash, SoeOptional},
59 {sfServerVersion, SoeOptional},
60 // featureXRPFees
61 {sfBaseFeeDrops, SoeOptional},
62 {sfReserveBaseDrops, SoeOptional},
63 {sfReserveIncrementDrops, SoeOptional},
64 // featureSmartEscrow
65 {sfGasLimit, SoeOptional},
66 {sfBytecodeSizeLimit, SoeOptional},
67 {sfGasPrice, SoeOptional},
68 };
69 // clang-format on
70
71 return kFormat;
72};
73
79
82{
83 return getFieldH256(sfLedgerHash);
84}
85
88{
89 return getFieldH256(sfConsensusHash);
90}
91
97
100{
101 return seenTime_;
102}
103
104bool
105STValidation::isValid() const noexcept
106{
107 if (!valid_)
108 {
109 XRPL_ASSERT(
111 "xrpl::STValidation::isValid : valid key type");
112
113 // Log that the signature was never checked, so an operator does not
114 // read this as a bad key. The log is guarded because it can throw too.
115 auto reportUncheckable = [this](char const* reason) noexcept {
116 try
117 {
118 JLOG(debugLog().error())
119 << "Cannot check the signature of the validation for ledger " << getLedgerHash()
120 << ": " << reason;
121 }
122 catch (...) // NOLINT(bugprone-empty-catch)
123 {
124 // Nothing can be reported when reporting is what failed.
125 }
126 };
127
128 // The signing hash re-serializes the fields, which can fail. This
129 // function is noexcept, so report the validation as invalid instead of
130 // throwing. valid_ stays unset, so a later call checks again.
131 try
132 {
136 makeSlice(getFieldVL(sfSignature)),
137 (getFlags() & kVfFullyCanonicalSig) != 0u);
138 }
139 catch (std::exception const& e)
140 {
141 reportUncheckable(e.what());
142 return false;
143 }
144 catch (...)
145 {
146 reportUncheckable("unknown exception");
147 return false;
148 }
149 }
150
151 return valid_.value();
152}
153
154bool
155STValidation::isFull() const noexcept
156{
157 return (getFlags() & kVfFullValidation) != 0;
158}
159
160Blob
162{
163 return getFieldVL(sfSignature);
164}
165
166Blob
168{
169 Serializer s;
170 add(s);
171 return s.peekData();
172}
173
174} // namespace xrpl
std::chrono::time_point< NetClock > time_point
Definition chrono.h:48
std::chrono::duration< rep, period > duration
Definition chrono.h:47
Defines the fields and their attributes within a STObject.
Definition SOTemplate.h:105
A type which can be exported to a well known binary format.
Definition STBase.h:129
static STBase * emplace(std::size_t n, void *buf, T &&val)
Definition STBase.h:226
Blob getFieldVL(SField const &field) const
Definition STObject.cpp:649
UInt256 getFieldH256(SField const &field) const
Definition STObject.cpp:631
UInt256 getSigningHash(HashPrefix prefix) const
Definition STObject.cpp:384
std::uint32_t getFieldU32(SField const &field) const
Definition STObject.cpp:601
void add(Serializer &s) const override
Definition STObject.cpp:123
std::uint32_t getFlags() const
Definition STObject.cpp:517
NetClock::time_point getSignTime() const
static SOTemplate const & validationFormat()
STBase * move(std::size_t n, void *buf) override
Blob getSignature() const
NetClock::time_point getSeenTime() const noexcept
std::optional< bool > valid_
UInt256 getSigningHash() const
STBase * copy(std::size_t n, void *buf) const override
UInt256 getConsensusHash() const
bool isValid() const noexcept
Whether this validation carries a good signature.
bool isFull() const noexcept
PublicKey const & getSignerPublic() const noexcept
Blob getSerialized() const
NetClock::time_point seenTime_
UInt256 getLedgerHash() const
Blob const & peekData() const
Definition Serializer.h:273
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
bool verifyDigest(PublicKey const &publicKey, UInt256 const &digest, Slice const &sig, bool mustBeFullyCanonical=true) noexcept
Verify a secp256k1 signature on the digest of a message.
beast::Journal debugLog()
Returns a debug journal.
Definition Log.cpp:399
@ SoeDefault
Definition SOTemplate.h:24
@ SoeOptional
Definition SOTemplate.h:23
@ SoeRequired
Definition SOTemplate.h:22
std::optional< KeyType > publicKeyType(Slice const &slice)
Returns the type of public key.
Slice makeSlice(std::array< T, N > const &a)
Definition Slice.h:228
BaseUInt< 256 > UInt256
Definition base_uint.h:580
constexpr std::uint32_t kVfFullValidation
constexpr std::uint32_t kVfFullyCanonicalSig
@ Validation
validation for signing
Definition HashPrefix.h:74
std::vector< unsigned char > Blob
Storage for linear binary data.
Definition Blob.h:11
T what(T... args)