rippled
Loading...
Searching...
No Matches
STValidation.h
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2012, 2013 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#ifndef RIPPLE_PROTOCOL_STVALIDATION_H_INCLUDED
21#define RIPPLE_PROTOCOL_STVALIDATION_H_INCLUDED
22
23#include <xrpl/basics/Log.h>
24#include <xrpl/beast/utility/instrumentation.h>
25#include <xrpl/protocol/FeeUnits.h>
26#include <xrpl/protocol/PublicKey.h>
27#include <xrpl/protocol/STObject.h>
28#include <xrpl/protocol/SecretKey.h>
29#include <cstdint>
30#include <functional>
31#include <memory>
32#include <optional>
33
34namespace ripple {
35
36// Validation flags
37
38// This is a full (as opposed to a partial) validation
39constexpr std::uint32_t vfFullValidation = 0x00000001;
40
41// The signature is fully canonical
42constexpr std::uint32_t vfFullyCanonicalSig = 0x80000000;
43
44class STValidation final : public STObject, public CountedObject<STValidation>
45{
46 bool mTrusted = false;
47
48 // Determines the validity of the signature in this validation; unseated
49 // optional if we haven't yet checked it, a boolean otherwise.
51
52 // The public key associated with the key used to sign this validation
54
55 // The ID of the validator that issued this validation. For validators
56 // that use manifests this will be derived from the master public key.
58
60
61public:
75 template <class LookupNodeID>
77 SerialIter& sit,
78 LookupNodeID&& lookupNodeID,
79 bool checkSignature);
80
89 template <typename F>
91 NetClock::time_point signTime,
92 PublicKey const& pk,
93 SecretKey const& sk,
94 NodeID const& nodeID,
95 F&& f);
96
97 // Hash of the validated ledger
99 getLedgerHash() const;
100
101 // Hash of consensus transaction set used to generate ledger
102 uint256
103 getConsensusHash() const;
104
106 getSignTime() const;
107
109 getSeenTime() const noexcept;
110
111 PublicKey const&
112 getSignerPublic() const noexcept;
113
114 NodeID const&
115 getNodeID() const noexcept;
116
117 bool
118 isValid() const noexcept;
119
120 bool
121 isFull() const noexcept;
122
123 bool
124 isTrusted() const noexcept;
125
126 uint256
127 getSigningHash() const;
128
129 void
130 setTrusted();
131
132 void
133 setUntrusted();
134
135 void
136 setSeen(NetClock::time_point s);
137
138 Blob
139 getSerialized() const;
140
141 Blob
142 getSignature() const;
143
144private:
145 static SOTemplate const&
147
148 STBase*
149 copy(std::size_t n, void* buf) const override;
150 STBase*
151 move(std::size_t n, void* buf) override;
152
153 friend class detail::STVar;
154};
155
156template <class LookupNodeID>
158 SerialIter& sit,
159 LookupNodeID&& lookupNodeID,
160 bool checkSignature)
161 : STObject(validationFormat(), sit, sfValidation)
162 , signingPubKey_([this]() {
163 auto const spk = getFieldVL(sfSigningPubKey);
164
166 Throw<std::runtime_error>("Invalid public key in validation");
167
168 return PublicKey{makeSlice(spk)};
169 }())
170 , nodeID_(lookupNodeID(signingPubKey_))
171{
172 if (checkSignature && !isValid())
173 {
174 JLOG(debugLog().error()) << "Invalid signature in validation: "
176 Throw<std::runtime_error>("Invalid signature in validation");
177 }
178
179 XRPL_ASSERT(
181 "ripple::STValidation::STValidation(SerialIter) : nonzero node");
182}
183
192template <typename F>
194 NetClock::time_point signTime,
195 PublicKey const& pk,
196 SecretKey const& sk,
197 NodeID const& nodeID,
198 F&& f)
199 : STObject(validationFormat(), sfValidation)
200 , signingPubKey_(pk)
201 , nodeID_(nodeID)
202 , seenTime_(signTime)
203{
204 XRPL_ASSERT(
206 "ripple::STValidation::STValidation(PublicKey, SecretKey) : nonzero "
207 "node");
208
209 // First, set our own public key:
211 LogicError("We can only use secp256k1 keys for signing validations");
212
213 setFieldVL(sfSigningPubKey, pk.slice());
214 setFieldU32(sfSigningTime, signTime.time_since_epoch().count());
215
216 // Perform additional initialization
217 f(*this);
218
219 // Finally, sign the validation and mark it as trusted:
221 setFieldVL(sfSignature, signDigest(pk, sk, getSigningHash()));
222 setTrusted();
223
224 // Check to ensure that all required fields are present.
225 for (auto const& e : validationFormat())
226 {
227 if (e.style() == soeREQUIRED && !isFieldPresent(e.sField()))
229 "Required field '" + e.sField().getName() +
230 "' missing from validation.");
231 }
232
233 // We just signed this, so it should be valid.
234 valid_ = true;
235}
236
237inline PublicKey const&
239{
240 return signingPubKey_;
241}
242
243inline NodeID const&
245{
246 return nodeID_;
247}
248
249inline bool
251{
252 return mTrusted;
253}
254
255inline void
257{
258 mTrusted = true;
259}
260
261inline void
263{
264 mTrusted = false;
265}
266
267inline void
269{
270 seenTime_ = s;
271}
272
273} // namespace ripple
274
275#endif
Tracks the number of instances of an object.
A public key.
Definition: PublicKey.h:62
Slice slice() const noexcept
Definition: PublicKey.h:123
Defines the fields and their attributes within a STObject.
Definition: SOTemplate.h:113
A type which can be exported to a well known binary format.
Definition: STBase.h:124
Blob getFieldVL(SField const &field) const
Definition: STObject.cpp:627
bool setFlag(std::uint32_t)
Definition: STObject.cpp:477
bool isFieldPresent(SField const &field) const
Definition: STObject.cpp:454
Json::Value getJson(JsonOptions options) const override
Definition: STObject.cpp:795
void setFieldU32(SField const &field, std::uint32_t)
Definition: STObject.cpp:711
void setFieldVL(SField const &field, Blob const &)
Definition: STObject.cpp:747
PublicKey const & getSignerPublic() const noexcept
Definition: STValidation.h:238
uint256 getConsensusHash() const
std::optional< bool > valid_
Definition: STValidation.h:50
STValidation(SerialIter &sit, LookupNodeID &&lookupNodeID, bool checkSignature)
Construct a STValidation from a peer from serialized data.
Definition: STValidation.h:157
Blob getSerialized() const
NetClock::time_point getSeenTime() const noexcept
void setSeen(NetClock::time_point s)
Definition: STValidation.h:268
friend class detail::STVar
Definition: STValidation.h:153
bool isTrusted() const noexcept
Definition: STValidation.h:250
NodeID const nodeID_
Definition: STValidation.h:57
PublicKey const signingPubKey_
Definition: STValidation.h:53
static SOTemplate const & validationFormat()
NodeID const & getNodeID() const noexcept
Definition: STValidation.h:244
bool isFull() const noexcept
NetClock::time_point seenTime_
Definition: STValidation.h:59
STBase * copy(std::size_t n, void *buf) const override
uint256 getLedgerHash() const
bool isValid() const noexcept
uint256 getSigningHash() const
Blob getSignature() const
STBase * move(std::size_t n, void *buf) override
NetClock::time_point getSignTime() const
A secret key.
Definition: SecretKey.h:37
bool isNonZero() const
Definition: base_uint.h:544
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
std::optional< KeyType > publicKeyType(Slice const &slice)
Returns the type of public key.
Definition: PublicKey.cpp:207
std::enable_if_t< std::is_same< T, char >::value||std::is_same< T, unsigned char >::value, Slice > makeSlice(std::array< T, N > const &a)
Definition: Slice.h:243
beast::Journal debugLog()
Returns a debug journal.
Definition: Log.cpp:452
Buffer signDigest(PublicKey const &pk, SecretKey const &sk, uint256 const &digest)
Generate a signature for a message digest.
Definition: SecretKey.cpp:212
constexpr std::uint32_t vfFullyCanonicalSig
Definition: STValidation.h:42
constexpr std::uint32_t vfFullValidation
Definition: STValidation.h:39
@ soeREQUIRED
Definition: SOTemplate.h:35
void LogicError(std::string const &how) noexcept
Called when faulty logic causes a broken invariant.
Definition: contract.cpp:48
STL namespace.
T time_since_epoch(T... args)