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 {sfSigningTime, SoeRequired},
51 {sfSigningPubKey, SoeRequired},
52 {sfSignature, SoeRequired},
53 {sfConsensusHash, SoeOptional},
54 {sfCookie, SoeDefault},
55 {sfValidatedHash, SoeOptional},
56 {sfServerVersion, SoeOptional},
57 {sfBaseFeeDrops, SoeOptional},
58 {sfReserveBaseDrops, SoeOptional},
59 {sfReserveIncrementDrops, SoeOptional},
60 // featureSmartEscrow
61 {sfGasLimit, SoeOptional},
62 {sfBytecodeSizeLimit, SoeOptional},
63 {sfGasPrice, SoeOptional},
64 };
65 // clang-format on
66
67 return kFormat;
68};
69
75
78{
79 return getFieldH256(sfLedgerHash);
80}
81
84{
85 return getFieldH256(sfConsensusHash);
86}
87
93
96{
97 return seenTime_;
98}
99
100bool
101STValidation::isValid() const noexcept
102{
103 if (!valid_)
104 {
105 XRPL_ASSERT(
107 "xrpl::STValidation::isValid : valid key type");
108
109 // Log that the signature was never checked, so an operator does not
110 // read this as a bad key. The log is guarded because it can throw too.
111 auto const reportUncheckable = [this](char const* reason) noexcept {
112 try
113 {
114 JLOG(debugLog().error())
115 << "Cannot check the signature of the validation for ledger " << getLedgerHash()
116 << ": " << reason;
117 }
118 catch (...) // NOLINT(bugprone-empty-catch)
119 {
120 // Nothing can be reported when reporting is what failed.
121 }
122 };
123
124 // The signing hash re-serializes the fields, which can fail. This
125 // function is noexcept, so report the validation as invalid instead of
126 // throwing. valid_ stays unset, so a later call checks again.
127 try
128 {
132 makeSlice(getFieldVL(sfSignature)),
133 (getFlags() & kVfFullyCanonicalSig) != 0u);
134 }
135 catch (std::exception const& e)
136 {
137 reportUncheckable(e.what());
138 return false;
139 }
140 catch (...)
141 {
142 reportUncheckable("unknown exception");
143 return false;
144 }
145 }
146
147 return valid_.value();
148}
149
150bool
151STValidation::isFull() const noexcept
152{
153 return (getFlags() & kVfFullValidation) != 0;
154}
155
156Blob
158{
159 return getFieldVL(sfSignature);
160}
161
162Blob
164{
165 Serializer s;
166 add(s);
167 return s.peekData();
168}
169
170} // 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:581
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)